int PQP_Collide(PQP_CollideResult *res, PQP_REAL R1[3][3], PQP_REAL T1[3], PQP_Model *o1, PQP_REAL R2[3][3], PQP_REAL T2[3], PQP_Model *o2, int flag) { double t1 = GetTime(); // make sure that the models are built if (o1->build_state != PQP_BUILD_STATE_PROCESSED) return PQP_ERR_UNPROCESSED_MODEL; if (o2->build_state != PQP_BUILD_STATE_PROCESSED) return PQP_ERR_UNPROCESSED_MODEL; // clear the stats res->num_bv_tests = 0; res->num_tri_tests = 0; // don't release the memory, but reset the num_pairs counter res->num_pairs = 0; // Okay, compute what transform [R,T] that takes us from cs1 to cs2. // [R,T] = [R1,T1]'[R2,T2] = [R1',-R1'T][R2,T2] = [R1'R2, R1'(T2-T1)] // First compute the rotation part, then translation part MTxM(res->R,R1,R2); PQP_REAL Ttemp[3]; VmV(Ttemp, T2, T1); MTxV(res->T, R1, Ttemp); // compute the transform from o1->child(0) to o2->child(0) PQP_REAL Rtemp[3][3], R[3][3], T[3]; MxM(Rtemp,res->R,o2->child(0)->R); MTxM(R,o1->child(0)->R,Rtemp); #if PQP_BV_TYPE & OBB_TYPE MxVpV(Ttemp,res->R,o2->child(0)->To,res->T); VmV(Ttemp,Ttemp,o1->child(0)->To); #else MxVpV(Ttemp,res->R,o2->child(0)->Tr,res->T); VmV(Ttemp,Ttemp,o1->child(0)->Tr); #endif MTxV(T,o1->child(0)->R,Ttemp); // now start with both top level BVs CollideRecurse(res,R,T,o1,0,o2,0,flag); double t2 = GetTime(); res->query_time_secs = t2 - t1; return PQP_OK; }
void make_parent_relative(PQP_Model *m, int bn, const PQP_REAL parentR[3][3] #if PQP_BV_TYPE & RSS_TYPE ,const PQP_REAL parentTr[3] #endif #if PQP_BV_TYPE & OBB_TYPE ,const PQP_REAL parentTo[3] #endif ) { PQP_REAL Rpc[3][3], Tpc[3]; if (!m->child(bn)->Leaf()) { // make children parent-relative make_parent_relative(m,m->child(bn)->first_child, m->child(bn)->R #if PQP_BV_TYPE & RSS_TYPE ,m->child(bn)->Tr #endif #if PQP_BV_TYPE & OBB_TYPE ,m->child(bn)->To #endif ); make_parent_relative(m,m->child(bn)->first_child+1, m->child(bn)->R #if PQP_BV_TYPE & RSS_TYPE ,m->child(bn)->Tr #endif #if PQP_BV_TYPE & OBB_TYPE ,m->child(bn)->To #endif ); } // make self parent relative MTxM(Rpc,parentR,m->child(bn)->R); McM(m->child(bn)->R,Rpc); #if PQP_BV_TYPE & RSS_TYPE VmV(Tpc,m->child(bn)->Tr,parentTr); MTxV(m->child(bn)->Tr,parentR,Tpc); #endif #if PQP_BV_TYPE & OBB_TYPE VmV(Tpc,m->child(bn)->To,parentTo); MTxV(m->child(bn)->To,parentR,Tpc); #endif }
void BV::FitToTris(PQP_REAL O[3][3], Tri *tris, int num_tris) { // store orientation McM(R,O); // project points of tris to R coordinates int num_points = 3*num_tris; PQP_REAL (*P)[3] = new PQP_REAL[num_points][3]; int point = 0; int i; for (i = 0; i < num_tris; i++) { MTxV(P[point],R,tris[i].p1); point++; MTxV(P[point],R,tris[i].p2); point++; MTxV(P[point],R,tris[i].p3); point++; } PQP_REAL minx, maxx, miny, maxy, minz, maxz, c[3]; #if PQP_BV_TYPE & OBB_TYPE minx = maxx = P[0][0]; miny = maxy = P[0][1]; minz = maxz = P[0][2]; for (i = 1; i < num_points; i++) { if (P[i][0] < minx) minx = P[i][0]; else if (P[i][0] > maxx) maxx = P[i][0]; if (P[i][1] < miny) miny = P[i][1]; else if (P[i][1] > maxy) maxy = P[i][1]; if (P[i][2] < minz) minz = P[i][2]; else if (P[i][2] > maxz) maxz = P[i][2]; } c[0] = (PQP_REAL)0.5*(maxx + minx); c[1] = (PQP_REAL)0.5*(maxy + miny); c[2] = (PQP_REAL)0.5*(maxz + minz); MxV(To,R,c); d[0] = (PQP_REAL)0.5*(maxx - minx); d[1] = (PQP_REAL)0.5*(maxy - miny); d[2] = (PQP_REAL)0.5*(maxz - minz); #endif #if PQP_BV_TYPE & RSS_TYPE // compute thickness, which determines radius, and z of rectangle corner PQP_REAL cz,radsqr; minz = maxz = P[0][2]; for (i = 1; i < num_points; i++) { if (P[i][2] < minz) minz = P[i][2]; else if (P[i][2] > maxz) maxz = P[i][2]; } r = (PQP_REAL)0.5*(maxz - minz); radsqr = r*r; cz = (PQP_REAL)0.5*(maxz + minz); // compute an initial length of rectangle along x direction // find minx and maxx as starting points int minindex, maxindex; minindex = maxindex = 0; for (i = 1; i < num_points; i++) { if (P[i][0] < P[minindex][0]) minindex = i; else if (P[i][0] > P[maxindex][0]) maxindex = i; } PQP_REAL x, dz; dz = P[minindex][2] - cz; minx = P[minindex][0] + sqrt(MaxOfTwo(radsqr - dz*dz,0)); dz = P[maxindex][2] - cz; maxx = P[maxindex][0] - sqrt(MaxOfTwo(radsqr - dz*dz,0)); // grow minx for (i = 0; i < num_points; i++) { if (P[i][0] < minx) { dz = P[i][2] - cz; x = P[i][0] + sqrt(MaxOfTwo(radsqr - dz*dz,0)); if (x < minx) minx = x; } } // grow maxx for (i = 0; i < num_points; i++) { if (P[i][0] > maxx) { dz = P[i][2] - cz; x = P[i][0] - sqrt(MaxOfTwo(radsqr - dz*dz,0)); if (x > maxx) maxx = x; } } // compute an initial length of rectangle along y direction // find miny and maxy as starting points minindex = maxindex = 0; for (i = 1; i < num_points; i++) { if (P[i][1] < P[minindex][1]) minindex = i; else if (P[i][1] > P[maxindex][1]) maxindex = i; } PQP_REAL y; dz = P[minindex][2] - cz; miny = P[minindex][1] + sqrt(MaxOfTwo(radsqr - dz*dz,0)); dz = P[maxindex][2] - cz; maxy = P[maxindex][1] - sqrt(MaxOfTwo(radsqr - dz*dz,0)); // grow miny for (i = 0; i < num_points; i++) { if (P[i][1] < miny) { dz = P[i][2] - cz; y = P[i][1] + sqrt(MaxOfTwo(radsqr - dz*dz,0)); if (y < miny) miny = y; } } // grow maxy for (i = 0; i < num_points; i++) { if (P[i][1] > maxy) { dz = P[i][2] - cz; y = P[i][1] - sqrt(MaxOfTwo(radsqr - dz*dz,0)); if (y > maxy) maxy = y; } } // corners may have some points which are not covered - grow lengths if // necessary PQP_REAL dx, dy, u, t; PQP_REAL a = sqrt((PQP_REAL)0.5); for (i = 0; i < num_points; i++) { if (P[i][0] > maxx) { if (P[i][1] > maxy) { dx = P[i][0] - maxx; dy = P[i][1] - maxy; u = dx*a + dy*a; t = (a*u - dx)*(a*u - dx) + (a*u - dy)*(a*u - dy) + (cz - P[i][2])*(cz - P[i][2]); u = u - sqrt(MaxOfTwo(radsqr - t,0)); if (u > 0) { maxx += u*a; maxy += u*a; } } else if (P[i][1] < miny) { dx = P[i][0] - maxx; dy = P[i][1] - miny; u = dx*a - dy*a; t = (a*u - dx)*(a*u - dx) + (-a*u - dy)*(-a*u - dy) + (cz - P[i][2])*(cz - P[i][2]); u = u - sqrt(MaxOfTwo(radsqr - t,0)); if (u > 0) { maxx += u*a; miny -= u*a; } } } else if (P[i][0] < minx) { if (P[i][1] > maxy) { dx = P[i][0] - minx; dy = P[i][1] - maxy; u = dy*a - dx*a; t = (-a*u - dx)*(-a*u - dx) + (a*u - dy)*(a*u - dy) + (cz - P[i][2])*(cz - P[i][2]); u = u - sqrt(MaxOfTwo(radsqr - t,0)); if (u > 0) { minx -= u*a; maxy += u*a; } } else if (P[i][1] < miny) { dx = P[i][0] - minx; dy = P[i][1] - miny; u = -dx*a - dy*a; t = (-a*u - dx)*(-a*u - dx) + (-a*u - dy)*(-a*u - dy) + (cz - P[i][2])*(cz - P[i][2]); u = u - sqrt(MaxOfTwo(radsqr - t,0)); if (u > 0) { minx -= u*a; miny -= u*a; } } } } c[0] = minx; c[1] = miny; c[2] = cz; MxV(Tr,R,c); l[0] = maxx - minx; if (l[0] < 0) l[0] = 0; l[1] = maxy - miny; if (l[1] < 0) l[1] = 0; #endif delete [] P; }
int PQP_Distance(PQP_DistanceResult *res, PQP_REAL R1[3][3], PQP_REAL T1[3], PQP_Model *o1, PQP_REAL R2[3][3], PQP_REAL T2[3], PQP_Model *o2, PQP_REAL rel_err, PQP_REAL abs_err, int qsize) { double time1 = GetTime(); // make sure that the models are built if (o1->build_state != PQP_BUILD_STATE_PROCESSED) return PQP_ERR_UNPROCESSED_MODEL; if (o2->build_state != PQP_BUILD_STATE_PROCESSED) return PQP_ERR_UNPROCESSED_MODEL; // Okay, compute what transform [R,T] that takes us from cs2 to cs1. // [R,T] = [R1,T1]'[R2,T2] = [R1',-R1'T][R2,T2] = [R1'R2, R1'(T2-T1)] // First compute the rotation part, then translation part MTxM(res->R,R1,R2); PQP_REAL Ttemp[3]; VmV(Ttemp, T2, T1); MTxV(res->T, R1, Ttemp); // establish initial upper bound using last triangles which // provided the minimum distance PQP_REAL p[3],q[3]; res->distance = TriDistance(res->R,res->T,o1->last_tri,o2->last_tri,p,q); VcV(res->p1,p); VcV(res->p2,q); // initialize error bounds res->abs_err = abs_err; res->rel_err = rel_err; // clear the stats res->num_bv_tests = 0; res->num_tri_tests = 0; // compute the transform from o1->child(0) to o2->child(0) PQP_REAL Rtemp[3][3], R[3][3], T[3]; MxM(Rtemp,res->R,o2->child(0)->R); MTxM(R,o1->child(0)->R,Rtemp); #if PQP_BV_TYPE & RSS_TYPE MxVpV(Ttemp,res->R,o2->child(0)->Tr,res->T); VmV(Ttemp,Ttemp,o1->child(0)->Tr); #else MxVpV(Ttemp,res->R,o2->child(0)->To,res->T); VmV(Ttemp,Ttemp,o1->child(0)->To); #endif MTxV(T,o1->child(0)->R,Ttemp); // choose routine according to queue size if (qsize <= 2) { DistanceRecurse(res,R,T,o1,0,o2,0); } else { res->qsize = qsize; DistanceQueueRecurse(res,R,T,o1,0,o2,0); } // res->p2 is in cs 1 ; transform it to cs 2 PQP_REAL u[3]; VmV(u, res->p2, res->T); MTxV(res->p2, res->R, u); double time2 = GetTime(); res->query_time_secs = time2 - time1; return PQP_OK; }
void DistanceQueueRecurse(PQP_DistanceResult *res, PQP_REAL R[3][3], PQP_REAL T[3], PQP_Model *o1, int b1, PQP_Model *o2, int b2) { BVTQ bvtq(res->qsize); BVT min_test; min_test.b1 = b1; min_test.b2 = b2; McM(min_test.R,R); VcV(min_test.T,T); while(1) { int l1 = o1->child(min_test.b1)->Leaf(); int l2 = o2->child(min_test.b2)->Leaf(); if (l1 && l2) { // both leaves. Test the triangles beneath them. res->num_tri_tests++; PQP_REAL p[3], q[3]; Tri *t1 = &o1->tris[-o1->child(min_test.b1)->first_child - 1]; Tri *t2 = &o2->tris[-o2->child(min_test.b2)->first_child - 1]; PQP_REAL d = TriDistance(res->R,res->T,t1,t2,p,q); if (d < res->distance) { res->distance = d; VcV(res->p1, p); // p already in c.s. 1 VcV(res->p2, q); // q must be transformed // into c.s. 2 later o1->last_tri = t1; o2->last_tri = t2; } } else if (bvtq.GetNumTests() == bvtq.GetSize() - 1) { // queue can't get two more tests, recur DistanceQueueRecurse(res,min_test.R,min_test.T, o1,min_test.b1,o2,min_test.b2); } else { // decide how to descend to children PQP_REAL sz1 = o1->child(min_test.b1)->GetSize(); PQP_REAL sz2 = o2->child(min_test.b2)->GetSize(); res->num_bv_tests += 2; BVT bvt1,bvt2; PQP_REAL Ttemp[3]; if (l2 || (!l1 && (sz1 > sz2))) { // put new tests on queue consisting of min_test.b2 // with children of min_test.b1 int c1 = o1->child(min_test.b1)->first_child; int c2 = c1 + 1; // init bv test 1 bvt1.b1 = c1; bvt1.b2 = min_test.b2; MTxM(bvt1.R,o1->child(c1)->R,min_test.R); #if PQP_BV_TYPE & RSS_TYPE VmV(Ttemp,min_test.T,o1->child(c1)->Tr); #else VmV(Ttemp,min_test.T,o1->child(c1)->To); #endif MTxV(bvt1.T,o1->child(c1)->R,Ttemp); bvt1.d = BV_Distance(bvt1.R,bvt1.T, o1->child(bvt1.b1),o2->child(bvt1.b2)); // init bv test 2 bvt2.b1 = c2; bvt2.b2 = min_test.b2; MTxM(bvt2.R,o1->child(c2)->R,min_test.R); #if PQP_BV_TYPE & RSS_TYPE VmV(Ttemp,min_test.T,o1->child(c2)->Tr); #else VmV(Ttemp,min_test.T,o1->child(c2)->To); #endif MTxV(bvt2.T,o1->child(c2)->R,Ttemp); bvt2.d = BV_Distance(bvt2.R,bvt2.T, o1->child(bvt2.b1),o2->child(bvt2.b2)); } else { // put new tests on queue consisting of min_test.b1 // with children of min_test.b2 int c1 = o2->child(min_test.b2)->first_child; int c2 = c1 + 1; // init bv test 1 bvt1.b1 = min_test.b1; bvt1.b2 = c1; MxM(bvt1.R,min_test.R,o2->child(c1)->R); #if PQP_BV_TYPE & RSS_TYPE MxVpV(bvt1.T,min_test.R,o2->child(c1)->Tr,min_test.T); #else MxVpV(bvt1.T,min_test.R,o2->child(c1)->To,min_test.T); #endif bvt1.d = BV_Distance(bvt1.R,bvt1.T, o1->child(bvt1.b1),o2->child(bvt1.b2)); // init bv test 2 bvt2.b1 = min_test.b1; bvt2.b2 = c2; MxM(bvt2.R,min_test.R,o2->child(c2)->R); #if PQP_BV_TYPE & RSS_TYPE MxVpV(bvt2.T,min_test.R,o2->child(c2)->Tr,min_test.T); #else MxVpV(bvt2.T,min_test.R,o2->child(c2)->To,min_test.T); #endif bvt2.d = BV_Distance(bvt2.R,bvt2.T, o1->child(bvt2.b1),o2->child(bvt2.b2)); } bvtq.AddTest(bvt1); bvtq.AddTest(bvt2); } if (bvtq.Empty()) { break; } else { min_test = bvtq.ExtractMinTest(); if ((min_test.d + res->abs_err >= res->distance) && ((min_test.d * (1 + res->rel_err)) >= res->distance)) { break; } } } }
void DistanceRecurse(PQP_DistanceResult *res, PQP_REAL R[3][3], PQP_REAL T[3], // b2 relative to b1 PQP_Model *o1, int b1, PQP_Model *o2, int b2) { PQP_REAL sz1 = o1->child(b1)->GetSize(); PQP_REAL sz2 = o2->child(b2)->GetSize(); int l1 = o1->child(b1)->Leaf(); int l2 = o2->child(b2)->Leaf(); if (l1 && l2) { // both leaves. Test the triangles beneath them. res->num_tri_tests++; PQP_REAL p[3], q[3]; Tri *t1 = &o1->tris[-o1->child(b1)->first_child - 1]; Tri *t2 = &o2->tris[-o2->child(b2)->first_child - 1]; PQP_REAL d = TriDistance(res->R,res->T,t1,t2,p,q); if (d < res->distance) { res->distance = d; VcV(res->p1, p); // p already in c.s. 1 VcV(res->p2, q); // q must be transformed // into c.s. 2 later o1->last_tri = t1; o2->last_tri = t2; } return; } // First, perform distance tests on the children. Then traverse // them recursively, but test the closer pair first, the further // pair second. int a1,a2,c1,c2; // new bv tests 'a' and 'c' PQP_REAL R1[3][3], T1[3], R2[3][3], T2[3], Ttemp[3]; if (l2 || (!l1 && (sz1 > sz2))) { // visit the children of b1 a1 = o1->child(b1)->first_child; a2 = b2; c1 = o1->child(b1)->first_child+1; c2 = b2; MTxM(R1,o1->child(a1)->R,R); #if PQP_BV_TYPE & RSS_TYPE VmV(Ttemp,T,o1->child(a1)->Tr); #else VmV(Ttemp,T,o1->child(a1)->To); #endif MTxV(T1,o1->child(a1)->R,Ttemp); MTxM(R2,o1->child(c1)->R,R); #if PQP_BV_TYPE & RSS_TYPE VmV(Ttemp,T,o1->child(c1)->Tr); #else VmV(Ttemp,T,o1->child(c1)->To); #endif MTxV(T2,o1->child(c1)->R,Ttemp); } else { // visit the children of b2 a1 = b1; a2 = o2->child(b2)->first_child; c1 = b1; c2 = o2->child(b2)->first_child+1; MxM(R1,R,o2->child(a2)->R); #if PQP_BV_TYPE & RSS_TYPE MxVpV(T1,R,o2->child(a2)->Tr,T); #else MxVpV(T1,R,o2->child(a2)->To,T); #endif MxM(R2,R,o2->child(c2)->R); #if PQP_BV_TYPE & RSS_TYPE MxVpV(T2,R,o2->child(c2)->Tr,T); #else MxVpV(T2,R,o2->child(c2)->To,T); #endif } res->num_bv_tests += 2; PQP_REAL d1 = BV_Distance(R1, T1, o1->child(a1), o2->child(a2)); PQP_REAL d2 = BV_Distance(R2, T2, o1->child(c1), o2->child(c2)); if (d2 < d1) { if ((d2 < (res->distance - res->abs_err)) || (d2*(1 + res->rel_err) < res->distance)) { DistanceRecurse(res, R2, T2, o1, c1, o2, c2); } if ((d1 < (res->distance - res->abs_err)) || (d1*(1 + res->rel_err) < res->distance)) { DistanceRecurse(res, R1, T1, o1, a1, o2, a2); } } else { if ((d1 < (res->distance - res->abs_err)) || (d1*(1 + res->rel_err) < res->distance)) { DistanceRecurse(res, R1, T1, o1, a1, o2, a2); } if ((d2 < (res->distance - res->abs_err)) || (d2*(1 + res->rel_err) < res->distance)) { DistanceRecurse(res, R2, T2, o1, c1, o2, c2); } } }
void CollideRecurse(PQP_CollideResult *res, PQP_REAL R[3][3], PQP_REAL T[3], // b2 relative to b1 PQP_Model *o1, int b1, PQP_Model *o2, int b2, int flag) { // first thing, see if we're overlapping res->num_bv_tests++; if (!BV_Overlap(R, T, o1->child(b1), o2->child(b2))) return; // if we are, see if we test triangles next int l1 = o1->child(b1)->Leaf(); int l2 = o2->child(b2)->Leaf(); if (l1 && l2) { res->num_tri_tests++; #if 1 // transform the points in b2 into space of b1, then compare Tri *t1 = &o1->tris[-o1->child(b1)->first_child - 1]; Tri *t2 = &o2->tris[-o2->child(b2)->first_child - 1]; PQP_REAL q1[3], q2[3], q3[3]; PQP_REAL *p1 = t1->p1; PQP_REAL *p2 = t1->p2; PQP_REAL *p3 = t1->p3; MxVpV(q1, res->R, t2->p1, res->T); MxVpV(q2, res->R, t2->p2, res->T); MxVpV(q3, res->R, t2->p3, res->T); if (TriContact(p1, p2, p3, q1, q2, q3)) { // add this to result res->Add(t1->id, t2->id); } #else PQP_REAL p[3], q[3]; Tri *t1 = &o1->tris[-o1->child(b1)->first_child - 1]; Tri *t2 = &o2->tris[-o2->child(b2)->first_child - 1]; if (TriDistance(res->R,res->T,t1,t2,p,q) == 0.0) { // add this to result res->Add(t1->id, t2->id); } #endif return; } // we dont, so decide whose children to visit next PQP_REAL sz1 = o1->child(b1)->GetSize(); PQP_REAL sz2 = o2->child(b2)->GetSize(); PQP_REAL Rc[3][3],Tc[3],Ttemp[3]; if (l2 || (!l1 && (sz1 > sz2))) { int c1 = o1->child(b1)->first_child; int c2 = c1 + 1; MTxM(Rc,o1->child(c1)->R,R); #if PQP_BV_TYPE & OBB_TYPE VmV(Ttemp,T,o1->child(c1)->To); #else VmV(Ttemp,T,o1->child(c1)->Tr); #endif MTxV(Tc,o1->child(c1)->R,Ttemp); CollideRecurse(res,Rc,Tc,o1,c1,o2,b2,flag); if ((flag == PQP_FIRST_CONTACT) && (res->num_pairs > 0)) return; MTxM(Rc,o1->child(c2)->R,R); #if PQP_BV_TYPE & OBB_TYPE VmV(Ttemp,T,o1->child(c2)->To); #else VmV(Ttemp,T,o1->child(c2)->Tr); #endif MTxV(Tc,o1->child(c2)->R,Ttemp); CollideRecurse(res,Rc,Tc,o1,c2,o2,b2,flag); } else { int c1 = o2->child(b2)->first_child; int c2 = c1 + 1; MxM(Rc,R,o2->child(c1)->R); #if PQP_BV_TYPE & OBB_TYPE MxVpV(Tc,R,o2->child(c1)->To,T); #else MxVpV(Tc,R,o2->child(c1)->Tr,T); #endif CollideRecurse(res,Rc,Tc,o1,b1,o2,c1,flag); if ((flag == PQP_FIRST_CONTACT) && (res->num_pairs > 0)) return; MxM(Rc,R,o2->child(c2)->R); #if PQP_BV_TYPE & OBB_TYPE MxVpV(Tc,R,o2->child(c2)->To,T); #else MxVpV(Tc,R,o2->child(c2)->Tr,T); #endif CollideRecurse(res,Rc,Tc,o1,b1,o2,c2,flag); } }
int PQP_Tolerance(PQP_ToleranceResult *res, PQP_REAL R1[3][3], PQP_REAL T1[3], PQP_Model *o1, PQP_REAL R2[3][3], PQP_REAL T2[3], PQP_Model *o2, PQP_REAL tolerance, int qsize) { double time1 = GetTime(); // make sure that the models are built if (o1->build_state != PQP_BUILD_STATE_PROCESSED) return PQP_ERR_UNPROCESSED_MODEL; if (o2->build_state != PQP_BUILD_STATE_PROCESSED) return PQP_ERR_UNPROCESSED_MODEL; // Compute the transform [R,T] that takes us from cs2 to cs1. // [R,T] = [R1,T1]'[R2,T2] = [R1',-R1'T][R2,T2] = [R1'R2, R1'(T2-T1)] MTxM(res->R,R1,R2); PQP_REAL Ttemp[3]; VmV(Ttemp, T2, T1); MTxV(res->T, R1, Ttemp); // set tolerance, used to prune the search if (tolerance < 0.0) tolerance = 0.0; res->tolerance = tolerance; // clear the stats res->num_bv_tests = 0; res->num_tri_tests = 0; // initially assume not closer than tolerance res->closer_than_tolerance = 0; // compute the transform from o1->child(0) to o2->child(0) PQP_REAL Rtemp[3][3], R[3][3], T[3]; MxM(Rtemp,res->R,o2->child(0)->R); MTxM(R,o1->child(0)->R,Rtemp); #if PQP_BV_TYPE & RSS_TYPE MxVpV(Ttemp,res->R,o2->child(0)->Tr,res->T); VmV(Ttemp,Ttemp,o1->child(0)->Tr); #else MxVpV(Ttemp,res->R,o2->child(0)->To,res->T); VmV(Ttemp,Ttemp,o1->child(0)->To); #endif MTxV(T,o1->child(0)->R,Ttemp); // find a distance lower bound for trivial reject PQP_REAL d = BV_Distance(R, T, o1->child(0), o2->child(0)); if (d <= res->tolerance) { // more work needed - choose routine according to queue size if (qsize <= 2) { ToleranceRecurse(res, R, T, o1, 0, o2, 0); } else { res->qsize = qsize; ToleranceQueueRecurse(res, R, T, o1, 0, o2, 0); } } // res->p2 is in cs 1 ; transform it to cs 2 PQP_REAL u[3]; VmV(u, res->p2, res->T); MTxV(res->p2, res->R, u); double time2 = GetTime(); res->query_time_secs = time2 - time1; return PQP_OK; }
void ToleranceQueueRecurse(PQP_ToleranceResult *res, PQP_REAL R[3][3], PQP_REAL T[3], PQP_Model *o1, int b1, PQP_Model *o2, int b2) { BVTQ bvtq(res->qsize); BVT min_test; min_test.b1 = b1; min_test.b2 = b2; McM(min_test.R,R); VcV(min_test.T,T); while(1) { int l1 = o1->child(min_test.b1)->Leaf(); int l2 = o2->child(min_test.b2)->Leaf(); if (l1 && l2) { // both leaves - find if tri pair within tolerance res->num_tri_tests++; PQP_REAL p[3], q[3]; Tri *t1 = &o1->tris[-o1->child(min_test.b1)->first_child - 1]; Tri *t2 = &o2->tris[-o2->child(min_test.b2)->first_child - 1]; PQP_REAL d = TriDistance(res->R,res->T,t1,t2,p,q); if (d <= res->tolerance) { // triangle pair distance less than tolerance res->closer_than_tolerance = 1; res->distance = d; VcV(res->p1, p); // p already in c.s. 1 VcV(res->p2, q); // q must be transformed // into c.s. 2 later return; } } else if (bvtq.GetNumTests() == bvtq.GetSize() - 1) { // queue can't get two more tests, recur ToleranceQueueRecurse(res,min_test.R,min_test.T, o1,min_test.b1,o2,min_test.b2); if (res->closer_than_tolerance == 1) return; } else { // decide how to descend to children PQP_REAL sz1 = o1->child(min_test.b1)->GetSize(); PQP_REAL sz2 = o2->child(min_test.b2)->GetSize(); res->num_bv_tests += 2; BVT bvt1,bvt2; PQP_REAL Ttemp[3]; if (l2 || (!l1 && (sz1 > sz2))) { // add two new tests to queue, consisting of min_test.b2 // with the children of min_test.b1 int c1 = o1->child(min_test.b1)->first_child; int c2 = c1 + 1; // init bv test 1 bvt1.b1 = c1; bvt1.b2 = min_test.b2; MTxM(bvt1.R,o1->child(c1)->R,min_test.R); #if PQP_BV_TYPE & RSS_TYPE VmV(Ttemp,min_test.T,o1->child(c1)->Tr); #else VmV(Ttemp,min_test.T,o1->child(c1)->To); #endif MTxV(bvt1.T,o1->child(c1)->R,Ttemp); bvt1.d = BV_Distance(bvt1.R,bvt1.T, o1->child(bvt1.b1),o2->child(bvt1.b2)); // init bv test 2 bvt2.b1 = c2; bvt2.b2 = min_test.b2; MTxM(bvt2.R,o1->child(c2)->R,min_test.R); #if PQP_BV_TYPE & RSS_TYPE VmV(Ttemp,min_test.T,o1->child(c2)->Tr); #else VmV(Ttemp,min_test.T,o1->child(c2)->To); #endif MTxV(bvt2.T,o1->child(c2)->R,Ttemp); bvt2.d = BV_Distance(bvt2.R,bvt2.T, o1->child(bvt2.b1),o2->child(bvt2.b2)); } else { // add two new tests to queue, consisting of min_test.b1 // with the children of min_test.b2 int c1 = o2->child(min_test.b2)->first_child; int c2 = c1 + 1; // init bv test 1 bvt1.b1 = min_test.b1; bvt1.b2 = c1; MxM(bvt1.R,min_test.R,o2->child(c1)->R); #if PQP_BV_TYPE & RSS_TYPE MxVpV(bvt1.T,min_test.R,o2->child(c1)->Tr,min_test.T); #else MxVpV(bvt1.T,min_test.R,o2->child(c1)->To,min_test.T); #endif bvt1.d = BV_Distance(bvt1.R,bvt1.T, o1->child(bvt1.b1),o2->child(bvt1.b2)); // init bv test 2 bvt2.b1 = min_test.b1; bvt2.b2 = c2; MxM(bvt2.R,min_test.R,o2->child(c2)->R); #if PQP_BV_TYPE & RSS_TYPE MxVpV(bvt2.T,min_test.R,o2->child(c2)->Tr,min_test.T); #else MxVpV(bvt2.T,min_test.R,o2->child(c2)->To,min_test.T); #endif bvt2.d = BV_Distance(bvt2.R,bvt2.T, o1->child(bvt2.b1),o2->child(bvt2.b2)); } // put children tests in queue if (bvt1.d <= res->tolerance) bvtq.AddTest(bvt1); if (bvt2.d <= res->tolerance) bvtq.AddTest(bvt2); } if (bvtq.Empty() || (bvtq.MinTest() > res->tolerance)) { res->closer_than_tolerance = 0; return; } else { min_test = bvtq.ExtractMinTest(); } } }
// Tolerance Stuff // //--------------------------------------------------------------------------- void ToleranceRecurse(PQP_ToleranceResult *res, PQP_REAL R[3][3], PQP_REAL T[3], PQP_Model *o1, int b1, PQP_Model *o2, int b2) { PQP_REAL sz1 = o1->child(b1)->GetSize(); PQP_REAL sz2 = o2->child(b2)->GetSize(); int l1 = o1->child(b1)->Leaf(); int l2 = o2->child(b2)->Leaf(); if (l1 && l2) { // both leaves - find if tri pair within tolerance res->num_tri_tests++; PQP_REAL p[3], q[3]; Tri *t1 = &o1->tris[-o1->child(b1)->first_child - 1]; Tri *t2 = &o2->tris[-o2->child(b2)->first_child - 1]; PQP_REAL d = TriDistance(res->R,res->T,t1,t2,p,q); if (d <= res->tolerance) { // triangle pair distance less than tolerance res->closer_than_tolerance = 1; res->distance = d; VcV(res->p1, p); // p already in c.s. 1 VcV(res->p2, q); // q must be transformed // into c.s. 2 later } return; } int a1,a2,c1,c2; // new bv tests 'a' and 'c' PQP_REAL R1[3][3], T1[3], R2[3][3], T2[3], Ttemp[3]; if (l2 || (!l1 && (sz1 > sz2))) { // visit the children of b1 a1 = o1->child(b1)->first_child; a2 = b2; c1 = o1->child(b1)->first_child+1; c2 = b2; MTxM(R1,o1->child(a1)->R,R); #if PQP_BV_TYPE & RSS_TYPE VmV(Ttemp,T,o1->child(a1)->Tr); #else VmV(Ttemp,T,o1->child(a1)->To); #endif MTxV(T1,o1->child(a1)->R,Ttemp); MTxM(R2,o1->child(c1)->R,R); #if PQP_BV_TYPE & RSS_TYPE VmV(Ttemp,T,o1->child(c1)->Tr); #else VmV(Ttemp,T,o1->child(c1)->To); #endif MTxV(T2,o1->child(c1)->R,Ttemp); } else { // visit the children of b2 a1 = b1; a2 = o2->child(b2)->first_child; c1 = b1; c2 = o2->child(b2)->first_child+1; MxM(R1,R,o2->child(a2)->R); #if PQP_BV_TYPE & RSS_TYPE MxVpV(T1,R,o2->child(a2)->Tr,T); #else MxVpV(T1,R,o2->child(a2)->To,T); #endif MxM(R2,R,o2->child(c2)->R); #if PQP_BV_TYPE & RSS_TYPE MxVpV(T2,R,o2->child(c2)->Tr,T); #else MxVpV(T2,R,o2->child(c2)->To,T); #endif } res->num_bv_tests += 2; PQP_REAL d1 = BV_Distance(R1, T1, o1->child(a1), o2->child(a2)); PQP_REAL d2 = BV_Distance(R2, T2, o1->child(c1), o2->child(c2)); if (d2 < d1) { if (d2 <= res->tolerance) ToleranceRecurse(res, R2, T2, o1, c1, o2, c2); if (res->closer_than_tolerance) return; if (d1 <= res->tolerance) ToleranceRecurse(res, R1, T1, o1, a1, o2, a2); } else { if (d1 <= res->tolerance) ToleranceRecurse(res, R1, T1, o1, a1, o2, a2); if (res->closer_than_tolerance) return; if (d2 <= res->tolerance) ToleranceRecurse(res, R2, T2, o1, c1, o2, c2); } }
int box::split_recurse(int *t) { // For a single triangle, orientation is easily determined. // The major axis is parallel to the longest edge. // The minor axis is normal to the triangle. // The in-between axis is determine by these two. // this->pR, this->d, and this->pT are set herein. P = N = 0; tri *ptr = RAPID_tri + t[0]; // Find the major axis: parallel to the longest edge. double u12[3], u23[3], u31[3]; // First compute the squared-lengths of each edge VmV(u12, ptr->p1, ptr->p2); double d12 = VdotV(u12,u12); VmV(u23, ptr->p2, ptr->p3); double d23 = VdotV(u23,u23); VmV(u31, ptr->p3, ptr->p1); double d31 = VdotV(u31,u31); // Find the edge of longest squared-length, normalize it to // unit length, and put result into a0. double a0[3]; double l; if (d12 > d23) { if (d12 > d31) { l = 1.0 / sqrt(d12); a0[0] = u12[0] * l; a0[1] = u12[1] * l; a0[2] = u12[2] * l; } else { l = 1.0 / sqrt(d31); a0[0] = u31[0] * l; a0[1] = u31[1] * l; a0[2] = u31[2] * l; } } else { if (d23 > d31) { l = 1.0 / sqrt(d23); a0[0] = u23[0] * l; a0[1] = u23[1] * l; a0[2] = u23[2] * l; } else { l = 1.0 / sqrt(d31); a0[0] = u31[0] * l; a0[1] = u31[1] * l; a0[2] = u31[2] * l; } } // Now compute unit normal to triangle, and put into a2. double a2[3]; VcrossV(a2, u12, u23); l = 1.0 / Vlength(a2); a2[0] *= l; a2[1] *= l; a2[2] *= l; // a1 is a2 cross a0. double a1[3]; VcrossV(a1, a2, a0); // Now make the columns of this->pR the vectors a0, a1, and a2. pR[0][0] = a0[0]; pR[0][1] = a1[0]; pR[0][2] = a2[0]; pR[1][0] = a0[1]; pR[1][1] = a1[1]; pR[1][2] = a2[1]; pR[2][0] = a0[2]; pR[2][1] = a1[2]; pR[2][2] = a2[2]; // Now compute the maximum and minimum extents of each vertex // along each of the box axes. From this we will compute the // box center and box dimensions. double minval[3], maxval[3]; double c[3]; MTxV(c, pR, ptr->p1); minval[0] = maxval[0] = c[0]; minval[1] = maxval[1] = c[1]; minval[2] = maxval[2] = c[2]; MTxV(c, pR, ptr->p2); minmax(minval[0], maxval[0], c[0]); minmax(minval[1], maxval[1], c[1]); minmax(minval[2], maxval[2], c[2]); MTxV(c, pR, ptr->p3); minmax(minval[0], maxval[0], c[0]); minmax(minval[1], maxval[1], c[1]); minmax(minval[2], maxval[2], c[2]); // With the max and min data, determine the center point and dimensions // of the box c[0] = (minval[0] + maxval[0])*0.5; c[1] = (minval[1] + maxval[1])*0.5; c[2] = (minval[2] + maxval[2])*0.5; pT[0] = c[0] * pR[0][0] + c[1] * pR[0][1] + c[2] * pR[0][2]; pT[1] = c[0] * pR[1][0] + c[1] * pR[1][1] + c[2] * pR[1][2]; pT[2] = c[0] * pR[2][0] + c[1] * pR[2][1] + c[2] * pR[2][2]; d[0] = (maxval[0] - minval[0])*0.5; d[1] = (maxval[1] - minval[1])*0.5; d[2] = (maxval[2] - minval[2])*0.5; // Assign the one triangle to this box trp = ptr; return RAPID_OK; }
int box::split_recurse(int *t, int n) { // The orientation for the parent box is already assigned to this->pR. // The axis along which to split will be column 0 of this->pR. // The mean point is passed in on this->pT. // When this routine completes, the position and orientation in model // space will be established, as well as its dimensions. Child boxes // will be constructed and placed in the parent's CS. if (n == 1) { return split_recurse(t); } // walk along the tris for the box, and do the following: // 1. collect the max and min of the vertices along the axes of <or>. // 2. decide which group the triangle goes in, performing appropriate swap. // 3. accumulate the mean point and covariance data for that triangle. accum M1, M2; double C[3][3]; double c[3]; double minval[3], maxval[3]; int rc; // for return code on procedure calls. int in; tri *ptr; int i; double axdmp; int n1 = 0; // The number of tris in group 1. // Group 2 will have n - n1 tris. // project approximate mean point onto splitting axis, and get coord. axdmp = (pR[0][0] * pT[0] + pR[1][0] * pT[1] + pR[2][0] * pT[2]); clear_accum(M1); clear_accum(M2); MTxV(c, pR, RAPID_tri[t[0]].p1); minval[0] = maxval[0] = c[0]; minval[1] = maxval[1] = c[1]; minval[2] = maxval[2] = c[2]; for(i=0; i<n; i++) { in = t[i]; ptr = RAPID_tri + in; MTxV(c, pR, ptr->p1); minmax(minval[0], maxval[0], c[0]); minmax(minval[1], maxval[1], c[1]); minmax(minval[2], maxval[2], c[2]); MTxV(c, pR, ptr->p2); minmax(minval[0], maxval[0], c[0]); minmax(minval[1], maxval[1], c[1]); minmax(minval[2], maxval[2], c[2]); MTxV(c, pR, ptr->p3); minmax(minval[0], maxval[0], c[0]); minmax(minval[1], maxval[1], c[1]); minmax(minval[2], maxval[2], c[2]); // grab the mean point of the in'th triangle, project // it onto the splitting axis (1st column of pR) and // see where it lies with respect to axdmp. mean_from_moment(c, RAPID_moment[in]); if (((pR[0][0]*c[0] + pR[1][0]*c[1] + pR[2][0]*c[2]) < axdmp) && ((n!=2)) || ((n==2) && (i==0))) { // accumulate first and second order moments for group 1 accum_moment(M1, RAPID_moment[in]); // put it in group 1 by swapping t[i] with t[n1] int temp = t[i]; t[i] = t[n1]; t[n1] = temp; n1++; } else { // accumulate first and second order moments for group 2 accum_moment(M2, RAPID_moment[in]); // leave it in group 2 // do nothing...it happens by default } } // done using this->pT as a mean point. // error check! if ((n1 == 0) || (n1 == n)) { // our partitioning has failed: all the triangles fell into just // one of the groups. So, we arbitrarily partition them into // equal parts, and proceed. n1 = n/2; // now recompute accumulated stuff reaccum_moments(M1, t, n1); reaccum_moments(M2, t + n1, n - n1); } // With the max and min data, determine the center point and dimensions // of the parent box. c[0] = (minval[0] + maxval[0])*0.5; c[1] = (minval[1] + maxval[1])*0.5; c[2] = (minval[2] + maxval[2])*0.5; pT[0] = c[0] * pR[0][0] + c[1] * pR[0][1] + c[2] * pR[0][2]; pT[1] = c[0] * pR[1][0] + c[1] * pR[1][1] + c[2] * pR[1][2]; pT[2] = c[0] * pR[2][0] + c[1] * pR[2][1] + c[2] * pR[2][2]; d[0] = (maxval[0] - minval[0])*0.5; d[1] = (maxval[1] - minval[1])*0.5; d[2] = (maxval[2] - minval[2])*0.5; // allocate new boxes P = RAPID_boxes + RAPID_boxes_inited++; N = RAPID_boxes + RAPID_boxes_inited++; // Compute the orienations for the child boxes (eigenvectors of // covariance matrix). Select the direction of maximum spread to be // the split axis for each child. double tR[3][3]; if (n1 > 1) { mean_from_accum(P->pT, M1); covariance_from_accum(C, M1); if (eigen_and_sort1(tR, C) > 30) { // unable to find an orientation. We'll just pick identity. Midentity(tR); } McM(P->pR, tR); if ((rc = P->split_recurse(t, n1)) != RAPID_OK) return rc; } else { if ((rc = P->split_recurse(t)) != RAPID_OK) return rc; } McM(C, P->pR); MTxM(P->pR, pR, C); // and F1 VmV(c, P->pT, pT); MTxV(P->pT, pR, c); if ((n-n1) > 1) { mean_from_accum(N->pT, M2); covariance_from_accum (C, M2); if (eigen_and_sort1(tR, C) > 30) { // unable to find an orientation. We'll just pick identity. Midentity(tR); } McM(N->pR, tR); if ((rc = N->split_recurse(t + n1, n - n1)) != RAPID_OK) return rc; } else { if ((rc = N->split_recurse(t+n1)) != RAPID_OK) return rc; } McM(C, N->pR); MTxM(N->pR, pR, C); VmV(c, N->pT, pT); MTxV(N->pT, pR, c); return RAPID_OK; }