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; }
//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); }