void ColDetect::addPolygon(std::vector<Triangle3D> triangles, VCollide& vc) { int id; double v1[3], v2[3], v3[3]; vc.NewObject(&id); for (int j = 0; j < triangles.size(); j++) { auto tri = triangles[j]; auto p1 = tri.getPoint1(); v1[0] = p1.getX(); v1[1] = p1.getY(); v1[2] = p1.getZ(); auto p2 = tri.getPoint2(); v2[0] = p2.getX(); v2[1] = p2.getY(); v2[2] = p2.getZ(); auto p3 = tri.getPoint3(); v3[0] = p3.getX(); v3[1] = p3.getY(); v3[2] = p3.getZ(); vc.AddTri(v1, v2, v3, j); } vc.EndObject(); }
int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "USAGE: %s <simulation-file>\n", argv[0]); exit(1); } if ( (fp = fopen(argv[1], "r")) == NULL ) { fprintf(stderr, "%s: Error opening file %s\n", argv[0], argv[1]); exit(1); } //read in the geometry from input file. fscanf(fp, "%d", &num_of_polytopes); polytope = new Polytope*[num_of_polytopes]; char name[10], filename[20]; int color_index; int i; for (i=0; i<num_of_polytopes; i++) { fscanf(fp, "%s", name); fscanf(fp, "%s", filename); fscanf(fp, "%d", &color_index); cout<<"Reading in polytope from file "<<filename<<"\n"; polytope[i] = new Polytope(filename); } //add the object to the library. for (i=0; i<num_of_polytopes; i++) { Polytope *p = polytope[i]; vc.NewObject(&(p->id)); int j; for (j=0; j<p->num_tris; j++) vc.AddTri(p->vertex[(p->tris[j])[0]].v, p->vertex[(p->tris[j])[1]].v, p->vertex[(p->tris[j])[2]].v ); vc.EndObject(); } //perform the simulation. simulation_loop(&argc, argv); return 0; }
bool ColDetect::coldetectWithoutTransformation(Rectangle2D rect1, Rectangle2D rect2) { VCReport report; VCollide vc; addPolygon(rect1.toTriangles(), vc); addPolygon(rect2.toTriangles(), vc); vc.Collide(&report); //return true in case of collision return report.numObjPairs() > 0; }
//test for collisions and report the results. Also provide a //simple collision response to colliding objects. void CollisionTestReportAndRespond(void) { VCReport report; vc.Collide( &report ); //perform collision test. //default is VC_FIRST_CONTACT int j; for (j = 0; j < report.numObjPairs(); j++) { std::cout << "Detected collision between objects " << report.obj1ID(j) <<" and "<< report.obj2ID(j) <<"\n"; polyObject *p1 = GetObjectWithId(report.obj1ID(j)); polyObject *p2 = GetObjectWithId(report.obj2ID(j)); Vector normal; normal = p1->position - p2->position; normal.normalize(); ComputeResponse(normal, (p1->v), &(p1->v) ); normal = p2->position - p1->position; normal.normalize(); ComputeResponse(normal, (p2->v), &(p2->v) ); } SinkAndSource(); }
int coldetect(int ntri1, int ntri2, int ntrans1, int ntrans2, double *tri1, double *tri2, double *trans1, double *trans2) { double vc_trans[4][4]; VCReport report; VCollide vc; addTris(tri1, ntri1, vc); addTris(tri2, ntri2, vc); // Iterate through transformations for (int t(0); t < ntrans1; t++) { update(trans1, t, ntrans1, vc_trans); vc.UpdateTrans(0, vc_trans); update(trans2, t, ntrans2, vc_trans); vc.UpdateTrans(1, vc_trans); vc.Collide(&report); if (report.numObjPairs() > 0) { return t + 1; } } return 0; }
void addTris(double *tris, int n, VCollide &vc) { int id; double v1[3], v2[3], v3[3]; vc.NewObject(&id); for (int j(0); j < n; j++) { v1[0] = tris[j + 0 * n]; v1[1] = tris[j + 1 * n]; v1[2] = tris[j + 2 * n]; v2[0] = tris[j + 3 * n]; v2[1] = tris[j + 4 * n]; v2[2] = tris[j + 5 * n]; v3[0] = tris[j + 6 * n]; v3[1] = tris[j + 7 * n]; v3[2] = tris[j + 8 * n]; vc.AddTri(v1, v2, v3); } vc.EndObject(); }
int main(int argc, char *argv[]) { if (argc != 3) { fprintf(stderr, "USAGE: %s <bunny-file> <number-of-instances>\n", argv[0]); exit(1); } srand(32487723); polytope = new Polytope(argv[1]); int no_of_instances = atoi(argv[2]); WallDist = (int) pow(no_of_instances*100.0, 0.33) * 100; //maintains constant density. for (int i=0; i<no_of_instances; i++) { polyObject *t = new polyObject(polytope); vc.NewObject(&(t->id)); Polytope *p = t->p; for (int j=0; j<p->num_tris; j++) vc.AddTri(p->vertex[(p->tris[j])[0]].v, p->vertex[(p->tris[j])[1]].v, p->vertex[(p->tris[j])[2]].v ); vc.EndObject(); std::cout<<"created polytope with id = "<<t->id<<"\n"; t->next = list; list = t; num_of_polytopes++; } simulation_loop(&argc, argv); return 0; }
//test for collisions and report the results. void CollisionTestAndReport(void) { VCReport report; vc.Collide( &report ); //perform collision test. //default is VC_FIRST_CONTACT int j; for (j = 0; j < report.numObjPairs(); j++) { cout << "Detected collision between objects " << report.obj1ID(j) <<" and "<< report.obj2ID(j) <<"\n"; } }
//reads in the transformation and applies it the the corresponding //object from the library. void UpdatePolytope(int i, double ogl_trans[]) { q_xyz_quat_struct q; fscanf(fp, "%lf %lf %lf", &(q.xyz[0]), &(q.xyz[1]), &(q.xyz[2]) ); fscanf(fp, "%lf %lf %lf %lf", &(q.quat[3]), &(q.quat[0]), &(q.quat[1]), &(q.quat[2]) ); q_xyz_quat_to_ogl_matrix(ogl_trans, &q); //convert from quaternion to //OpenGL transformation matrix. double trans[4][4]; int j,k; for (j=0; j<4; j++) for (k=0; k<4; k++) { trans[j][k] = ogl_trans[4*k+j]; //convert the standard notation. } vc.UpdateTrans(polytope[i]->id, trans); //update the transformation of //corresponding object from the //library. }
int main(int argc, char *argv[]) { if (argc != 3) { std::cerr<<argv[0]<<": USAGE: "<<argv[0]<<" <input-file> <transformation-file>\n"; exit(1); } int num_tri; VCollide vc; int id[NO_OF_OBJECTS]; int i; for (i=0; i<NO_OF_OBJECTS; i++) //add the objects to the library. { std::cout<<"Reading object "<<i<<"\n"; vc.NewObject(&(id[i])); std::cout<<"Adding triangles\n"; FILE *fp = fopen(argv[1], "r"); fscanf(fp, "%d", &num_tri); for (int j=1; j<=num_tri; j++) { double v1[3], v2[3], v3[3]; fscanf(fp, "%lf %lf %lf", &(v1[0]), &(v1[1]), &(v1[2])); fscanf(fp, "%lf %lf %lf", &(v2[0]), &(v2[1]), &(v2[2])); fscanf(fp, "%lf %lf %lf", &(v3[0]), &(v3[1]), &(v3[2])); vc.AddTri(v1, v2, v3, j); // Each triangle has an id } fclose(fp); std::cout<<"Calling finish_object\n"; vc.EndObject(); std::cout<<"Inserted object "<<i<<"\n"; } FILE *fp = fopen(argv[2], "r"); for (i=1; i<=SIMULATION_STEPS; i++) //perform the simulation. { std::cout<<"Simulation step : "<<i<<"\n"; int j; for (j=0; j<NO_OF_OBJECTS; j++) { double trans[4][4]; //read in the transformation matrix. fscanf(fp, "%lf", &(trans[0][0])); fscanf(fp, "%lf", &(trans[0][1])); fscanf(fp, "%lf", &(trans[0][2])); fscanf(fp, "%lf", &(trans[0][3])); fscanf(fp, "%lf", &(trans[1][0])); fscanf(fp, "%lf", &(trans[1][1])); fscanf(fp, "%lf", &(trans[1][2])); fscanf(fp, "%lf", &(trans[1][3])); fscanf(fp, "%lf", &(trans[2][0])); fscanf(fp, "%lf", &(trans[2][1])); fscanf(fp, "%lf", &(trans[2][2])); fscanf(fp, "%lf", &(trans[2][3])); fscanf(fp, "%lf", &(trans[3][0])); fscanf(fp, "%lf", &(trans[3][1])); fscanf(fp, "%lf", &(trans[3][2])); fscanf(fp, "%lf", &(trans[3][3])); //update the object's transformation. vc.UpdateTrans(id[j], trans); } VCReport report; vc.Collide( &report, VC_ALL_CONTACTS); //perform collision test. //default is VC_FIRST_CONTACT for (j = 0; j < report.numObjPairs(); j++) { std::cout << "Detected collision between objects " << report.obj1ID(j) << " and " << report.obj2ID(j) << "\n"; std::cout << "\tNumber of contacts = " << report.numTriPairs(j) << "\n"; std::cout << "\tColliding triangle-pairs: "; int k; for ( k = 0; k < report.numTriPairs(j); k++ ) std::cout << "[" << report.tri1ID(j, k) << "," << report.tri2ID(j, k) << "] "; std::cout << "\n"; } } return 0; }
//update the transformation of library object with given id. void UpdatePolytope(int id, double trans[4][4]) { vc.UpdateTrans(id, trans); }
//removes bunnies which have crossed/collided with the y=YMAX wall. //for each bunny removed, one is added at the y=YMIN wall. void SinkAndSource(void) { polyObject *current; while (list != NULL) { if (list->position[1] >= WallDist) { current = list; list = list->next; vc.DeleteObject(current->id); std::cout<<"deleting id = "<<current->id<<"\n"; delete current; polyObject *t = new polyObject(polytope); t->position[1] = -WallDist + 0.5; vc.NewObject(&(t->id)); Polytope *p = t->p; int j; for (j=0; j<p->num_tris; j++) vc.AddTri(p->vertex[(p->tris[j])[0]].v, p->vertex[(p->tris[j])[1]].v, p->vertex[(p->tris[j])[2]].v ); vc.EndObject(); std::cout<<"created polytope with id = "<<t->id<<"\n"; t->next = list; list = t; num_of_polytopes++; } else { break; } } current = list; if (current != NULL) { while (current->next != NULL) { if (current->next->position[1] >= WallDist) { polyObject *t = current->next; current->next = current->next->next; vc.DeleteObject(t->id); std::cout<<"deleting id = "<<t->id<<"\n"; delete t; t = new polyObject(polytope); t->position[1] = -WallDist + 0.5; vc.NewObject(&(t->id)); Polytope *p = t->p; int j; for (j=0; j<p->num_tris; j++) vc.AddTri(p->vertex[(p->tris[j])[0]].v, p->vertex[(p->tris[j])[1]].v, p->vertex[(p->tris[j])[2]].v ); vc.EndObject(); std::cout<<"created polytope with id = "<<t->id<<"\n"; t->next = list; list = t; num_of_polytopes++; } else { current = current->next; } } } }