35 const bool HIRUKAWA_DEBUG =
false;
38 enum { NOT_INTERSECT = 0,
39 EDGE1_NOT_INTERSECT = 1,
40 EDGE2_NOT_INTERSECT = 2,
41 EDGE3_NOT_INTERSECT = 3 };
44 const int INTERSECT = 1;
58 if(nm[0] < 0.0 && nm[1] < 0.0 && nm[2] < 0.0 ||
59 nm[0] > 0.0 && nm[1] > 0.0 && nm[2] > 0.0){
62 if(nm[0] < 0.0 && nm[1] < 0.0 && nm[2] > 0.0 ||
63 nm[0] > 0.0 && nm[1] > 0.0 && nm[2] < 0.0){
64 return EDGE1_NOT_INTERSECT;
66 if(nm[0] < 0.0 && nm[1] > 0.0 && nm[2] > 0.0 ||
67 nm[0] > 0.0 && nm[1] < 0.0 && nm[2] < 0.0){
68 return EDGE2_NOT_INTERSECT;
70 if(nm[0] > 0.0 && nm[1] < 0.0 && nm[2] > 0.0 ||
71 nm[0] < 0.0 && nm[1] > 0.0 && nm[2] < 0.0){
72 return EDGE3_NOT_INTERSECT;
91 double ef1P1 = ef1.dot(P1);
92 double ef1P3 = ef1.dot(P3);
93 double ef1Q = ef1.dot(Q);
95 double ef2P2 = ef2.dot(P2);
96 double ef2P1 = ef2.dot(P1);
97 double ef2Q = ef2.dot(Q);
99 double ef3P3 = ef3.dot(P3);
100 double ef3P2 = ef3.dot(P2);
101 double ef3Q = ef3.dot(Q);
103 if((ef1P3 > ef1P1 && ef1Q > ef1P1 ||
104 ef1P3 < ef1P1 && ef1Q < ef1P1 )
106 (ef2P1 > ef2P2 && ef2Q > ef2P2 ||
107 ef2P1 < ef2P2 && ef2Q < ef2P2 )
109 (ef3P2 > ef3P3 && ef3Q > ef3P3 ||
110 ef3P2 < ef3P3 && ef3Q < ef3P3 )) {
114 return NOT_INTERSECT;
125 if(mn1 == mn2)
return;
127 if(mn1 >0 && mn2 < 0){
128 ipt = (-(mn2*x1) + mn1*x2)/(mn1-mn2);
129 }
else if(mn1 < 0 && mn2 > 0){
130 ipt = (mn2*x1 - mn1*x2)/(-mn1+mn2);
141 ipt = (1.0 - p) * x1 + p * x2;
144 cout <<
"v1 = " << x1[0] <<
", " << x1[1] <<
", " << x1[2] << endl;
145 cout <<
"v2 = " << x2[0] <<
", " << x2[1] <<
", " << x2[2] << endl;
146 cout <<
"edge = " << x2[0]-x1[0] <<
", " << x2[1]-x1[1] <<
", " 147 << x2[2]-x1[2] << endl;
148 cout <<
"common pt = " << ipt[0] <<
" " << ipt[1] <<
" " << ipt[2] << endl;
164 cout <<
"calc_depth 1 = " << (ip1 - ip2).
dot(n) << endl;
167 return fabs((ip1 - ip2).
dot(n));
184 double d1 = fabs((ip1 - pt1).
dot(n));
185 double d2 = fabs((ip2 - pt2).
dot(n));
186 double d3 = fabs((ip1 - pt3).
dot(n));
188 double depth = (d1 < d2) ? d2 : d1;
194 cout <<
"calc_depth 3 = " << depth << endl;
219 const double p = pt.dot(ip - pt1) / pt.dot(pt);
238 cout <<
"ip = " << ip[0] <<
" " << ip[1] <<
" " << ip[2] << endl;
239 cout <<
"f12 = " << f12[0] <<
" " << f12[1] <<
" " << f12[2] << endl;
240 cout <<
"f23 = " << f23[0] <<
" " << f23[1] <<
" " << f23[2] << endl;
241 cout <<
"f31 = " << f31[0] <<
" " << f31[1] <<
" " << f31[2] << endl;
245 const double d1 = fabs((f12 - ip).
dot(n));
246 const double d2 = fabs((f23 - ip).
dot(n));
247 const double d3 = fabs((f31 - ip).
dot(n));
253 double depth = (d1 < d2) ? d1 : d2;
259 cout <<
"calc_depth 4 = " << depth << endl;
279 const double depth1 =
calc_depth(ip1, pt1, pt2, pt3, nn);
280 const double depth2 =
calc_depth(ip2, pt1, pt2, pt3, nn);
283 const double depth = (depth1 < depth2) ? depth2 : depth1;
286 cout <<
"calc_depth 5 = " << depth << endl;
303 vec = -(v1 - 0.5 * (v2 + v3)).normalized();
305 vec = (v1 - 0.5 * (v2 + v3)).normalized();
322 const double eps = 1.0e-3;
328 const double c11 = e.dot(e);
329 const double c12 = - e.dot(f);
330 const double c21 = - c12;
331 const double c22 = - f.dot(f);
333 const double det = c11 * c22 - c12 * c21;
340 const double a = e.dot(g);
341 const double b = f.dot(g);
342 const double t1 = ( c22 * a - c12 *
b) / det;
343 const double t2 = (-c21 * a + c11 *
b) / det;
346 if(t1<0 || 1<t1 || t2<0 || 1<t2)
return 0;
375 const double eps_applicability = 0.5;
379 return (eps_applicability < n1.dot(m1)) ? 0 : 1;
404 const double eps_applicability = 0.0;
405 const double eps_length = 1.0e-3;
406 const double eps_theta = 1.0e-1;
410 if(- eps_applicability < n1.dot(m1))
return 0;
412 const double ea_length = (va1 - va2).
norm();
413 const double eb_length = (vb1 - vb2).
norm();
416 if(ea_length < eps_length || eb_length < eps_length){
418 if(ea_length < eb_length) {
427 const double sv1_norm = sv1.norm();
429 const double sv2_norm = sv2.norm();
431 if(eps_length < sv1_norm && eps_length < sv2_norm){
433 if(- eps_applicability < sv1.dot(sv2) / (sv1_norm * sv2_norm)){
442 const double theta1 = ef.dot(n1) / n1.norm();
443 const double theta1_abs = fabs(theta1);
447 if(eps_length < sv1_norm){
448 theta2 = ef.dot(sv1) / sv1_norm;
449 theta2_abs = fabs(theta2);
456 const double theta3 = ef.dot(m1) / m1.norm();
457 const double theta3_abs = fabs(theta3);
461 if(eps_length < sv2_norm){
462 theta4 = ef.dot(sv2) / sv2_norm;
463 theta4_abs = fabs(theta4);
469 if(sv1_norm < eps_length || sv2_norm < eps_length){
472 if(theta1_abs < theta3_abs){
480 if(theta2_abs < eps_theta && theta4_abs < eps_theta){
495 if(theta1_abs < theta3_abs){
506 if(-eps_applicability < theta2 * theta4)
return 0;
512 if(theta1_abs < theta2_abs){
515 theta12 = -1.0 * theta1;
519 if(theta3_abs < theta4_abs){
520 theta34 = -1.0 * theta4;
526 if(fabs(theta12) < fabs(theta34)){
539 cout <<
"va1=" << va1[0] <<
" " << va1[1] <<
" " << va1[2] << endl;
540 cout <<
"va2=" << va2[0] <<
" " << va2[1] <<
" " << va2[2] << endl;
541 cout <<
"va3=" << v1[0] <<
" " << v1[1] <<
" " << v1[2] << endl;
542 cout <<
"vb1=" << vb1[0] <<
" " << vb1[1] <<
" " << vb1[2] << endl;
543 cout <<
"vb2=" << vb2[0] <<
" " << vb2[1] <<
" " << vb2[2] << endl;
544 cout <<
"vb3=" << v2[0] <<
" " << v2[1] <<
" " << v2[2] << endl;
545 cout <<
"n1=" << n1[0] <<
" " << n1[1] <<
" " << n1[2] << endl;
546 cout <<
"m1=" << m1[0] <<
" " << m1[1] <<
" " << m1[2] << endl;
547 cout <<
"ef=" << ef[0] <<
" " << ef[1] <<
" " << ef[2] << endl;
548 cout <<
"sv1=" << sv1[0] <<
" " << sv1[1] <<
" " << sv1[2] << endl;
549 cout <<
"sv2=" << sv2[0] <<
" " << sv2[1] <<
" " << sv2[2] << endl;
553 if(n_vector.dot(sv1) < eps_applicability || - eps_applicability < n_vector.dot(sv2)){
604 const double dp =
calc_depth2(ip1, ip2, q1, q2, q3, nv);
605 if(dp < col_p->depth){
609 ip3 = ip1; ip4 = ip2;
639 bool test_normal=
true)
648 || (
get_normal_vector_test(col_p->
n_vector, ef11, ip2, ip1, p3, q3, n1, m1, p1, p2, q1, q2) &&
651 ip3 = ip1; ip4 = ip2;
697 int edf1, edf2, edf3, ede1, ede2, ede3;
743 if(triQ == NOT_INTERSECT)
return 0;
745 double mq = m1.dot(q1);
746 mp[0] = m1.dot(p1) - mq;
747 mp[1] = m1.dot(p2) - mq;
748 mp[2] = m1.dot(p3) - mq;
750 if(triP == NOT_INTERSECT)
return 0;
762 edf1 = 0; edf2 = 0; edf3 = 0; ede1 = 0; ede2 = 0; ede3 = 0;
772 case EDGE1_NOT_INTERSECT:
777 case EDGE2_NOT_INTERSECT:
782 case EDGE3_NOT_INTERSECT:
788 int num_of_edges = edf1 + edf2 + edf3;
789 if(num_of_edges == 3){
794 if(num_of_edges < 2){
797 case EDGE1_NOT_INTERSECT:
807 case EDGE2_NOT_INTERSECT:
817 case EDGE3_NOT_INTERSECT:
827 if(num_of_edges == 0 && ede1+ede2+ede3 == 3){
833 int num = edf1+edf2+edf3+ede1+ede2+ede3;
839 printf(
"err of edge detection....");
858 else if(edf2==INTERSECT){
865 else if(edf3==INTERSECT){
872 else if(ede1==INTERSECT){
879 else if(ede2==INTERSECT){
886 else if(ede3==INTERSECT){
898 if(edf1==INTERSECT && edf2==INTERSECT){
899 if(HIRUKAWA_DEBUG) cout <<
"f1 f2" << endl;
901 if(!
find_collision_info(q2,q1,q3,nq[1],nq[0],nq[2],p1,p2,p3,e1,e2,e3,
902 m1,n1,ip3,ip4,ip5,ip6,col_p,-1.0))
905 else if(edf1==INTERSECT && edf3==INTERSECT){
906 if(HIRUKAWA_DEBUG) cout <<
"f1 f3" << endl;
908 if(!
find_collision_info(q1,q2,q3,nq[0],nq[1],nq[2],p1,p2,p3,e1,e2,e3,
909 m1,n1,ip3,ip4,ip5,ip6,col_p,-1.0))
912 else if(ede1==INTERSECT && edf1==INTERSECT){
913 if(HIRUKAWA_DEBUG) cout <<
"e1 f1" << endl;
915 if(!
find_collision_info(p1,p2,p3,mp[0],mp[1],q1,q2,q3,nq[0],nq[1],ef11,
916 n1,m1,ip3,ip4,col_p,nvc))
919 else if(ede2==INTERSECT && edf1==INTERSECT){
920 if(HIRUKAWA_DEBUG) cout <<
"e2 f1" << endl;
922 if(!
find_collision_info(p2,p3,p1,mp[1],mp[2],q1,q2,q3,nq[0],nq[1],ef21,
923 n1,m1,ip3,ip4,col_p,nvc))
926 else if(ede3==INTERSECT && edf1==INTERSECT){
927 if(HIRUKAWA_DEBUG) cout <<
"e3 f1" << endl;
929 if(!
find_collision_info(p3,p1,p2,mp[2],mp[0],q1,q2,q3,nq[0],nq[1],ef31,
930 n1,m1,ip3,ip4,col_p,nvc))
933 else if(edf2==INTERSECT && edf3==INTERSECT){
934 if(HIRUKAWA_DEBUG) cout <<
"f2 f3" << endl;
936 if(!
find_collision_info(q3,q2,q1,nq[2],nq[1],nq[0],p1,p2,p3,e1,e2,e3,
937 m1,n1,ip3,ip4,ip5,ip6,col_p,-1.0))
940 else if(ede1==INTERSECT && edf2==INTERSECT){
941 if(HIRUKAWA_DEBUG) cout <<
"e1 f2" << endl;
943 if(!
find_collision_info(p1,p2,p3,mp[0],mp[1],q2,q3,q1,nq[1],nq[2],ef12,
944 n1,m1,ip3,ip4,col_p,nvc))
947 else if(ede2==INTERSECT && edf2==INTERSECT){
948 if(HIRUKAWA_DEBUG) cout <<
"e2 f2" << endl;
950 if(!
find_collision_info(p2,p3,p1,mp[1],mp[2],q2,q3,q1,nq[1],nq[2],ef22,
951 n1,m1,ip3,ip4,col_p,nvc))
954 else if(ede3==INTERSECT && edf2==INTERSECT){
955 if(HIRUKAWA_DEBUG) cout <<
"e3 f2" << endl;
957 if(!
find_collision_info(p3,p1,p2,mp[2],mp[0],q2,q3,q1,nq[1],nq[2],ef32,
958 n1,m1,ip3,ip4,col_p,nvc))
961 else if(ede1==INTERSECT && edf3==INTERSECT){
962 if(HIRUKAWA_DEBUG) cout <<
"e1 f3" << endl;
964 if(!
find_collision_info(p1,p2,p3,mp[0],mp[1],q3,q1,q2,nq[2],nq[0],ef13,
965 n1,m1,ip3,ip4,col_p,nvc))
968 else if(ede2==INTERSECT && edf3==INTERSECT){
969 if(HIRUKAWA_DEBUG) cout <<
"e2 f3" << endl;
971 if(!
find_collision_info(p2,p3,p1,mp[1],mp[2],q3,q1,q2,nq[2],nq[0],ef23,
972 n1,m1,ip3,ip4,col_p,nvc))
975 else if(ede3==INTERSECT && edf3==INTERSECT){
976 if(HIRUKAWA_DEBUG) cout <<
"e3 f3" << endl;
978 if(!
find_collision_info(p3,p1,p2,mp[2],mp[0],q3,q1,q2,nq[2],nq[0],ef33,
979 n1,m1,ip3,ip4,col_p,nvc))
982 else if(ede1==INTERSECT && ede2==INTERSECT){
983 if(HIRUKAWA_DEBUG) cout <<
"e1 e2" << endl;
985 if(!
find_collision_info(p2,p1,p3,mp[1],mp[0],mp[2],q1,q2,q3,f1,f2,f3,
986 n1,m1,ip3,ip4,ip5,ip6,col_p,1.0))
989 else if(ede1==INTERSECT && ede3==INTERSECT){
990 if(HIRUKAWA_DEBUG) cout <<
"e1 e3" << endl;
992 if(!
find_collision_info(p1,p2,p3,mp[0],mp[1],mp[2],q1,q2,q3,f1,f2,f3,
993 n1,m1,ip3,ip4,ip5,ip6,col_p,1.0))
996 else if(ede2==INTERSECT && ede3==INTERSECT){
997 if(HIRUKAWA_DEBUG) cout <<
"e2 e3" << endl;
999 if(!
find_collision_info(p3,p2,p1,mp[2],mp[1],mp[0],q1,q2,q3,f1,f2,f3,
1000 n1,m1,ip3,ip4,ip5,ip6,col_p,1.0))
1037 cout <<
"P1 = " << p1w[0] <<
" " << p1w[1] <<
" " << p1w[2] << endl;
1038 cout <<
"P2 = " << p2w[0] <<
" " << p2w[1] <<
" " << p2w[2] << endl;
1039 cout <<
"P3 = " << p3w[0] <<
" " << p3w[1] <<
" " << p3w[2] << endl;
1040 cout <<
"Q1 = " << q1w[0] <<
" " << q1w[1] <<
" " << q1w[2] << endl;
1041 cout <<
"Q2 = " << q2w[0] <<
" " << q2w[1] <<
" " << q2w[2] << endl;
1042 cout <<
"Q3 = " << q3w[0] <<
" " << q3w[1] <<
" " << q3w[2] << endl;
1046 cout <<
i <<
"-th intersecting point = ";
1047 cout << i_pts_w[
i][0] <<
" " << i_pts_w[
i][1] <<
" " << i_pts_w[
i][2] << endl;
1050 cout <<
"n1 = " << n1[0] <<
" " << n1[1] <<
" " << n1[2] << endl;
1051 cout <<
"m1 = " << m1[0] <<
" " << m1[1] <<
" " << m1[2] << endl;
1052 cout <<
"mp[0] mp[1] mp[2] = " << mp[0] <<
" " << mp[1] <<
" " << mp[2] << endl;
1053 cout <<
"nq[0] nq[1] nq[2] = " << nq[0] <<
" " << nq[1] <<
" " << nq[2] << endl;
1055 <<
" " << col_p->
n_vector[2] << endl;
1056 cout <<
"depth = " << col_p->
depth << endl << endl;;
1061 printf(
"intersect point...in tri_contact..\n");
1062 printf(
" ip1x = %f ip1y = %f ip1z = %f\n ip2x = %f ip2y = %f ip2z = %f\n",
1066 printf(
"normal vector....it tri_conctact..\n");
bool normalVectorCorrection
flag to enable/disable normal vector correction
static int find_collision_info(const Vector3 &p1, const Vector3 &p2, const Vector3 &p3, double mp0, double mp1, double mp2, const Vector3 &q1, const Vector3 &q2, const Vector3 &q3, const Vector3 &f1, const Vector3 &f2, const Vector3 &f3, const Vector3 &n1, const Vector3 &m1, Vector3 &ip3, Vector3 &ip4, Vector3 &ip5, Vector3 &ip6, collision_data *col_p, double pq)
static void find_intersection_pt(Vector3 &ipt, const Vector3 &x1, const Vector3 &x2, double mn1, double mn2)
static void find_foot(const Vector3 &ip, const Vector3 &pt1, const Vector3 &pt2, Vector3 &f)
Matrix33 CD_Rot2
rotation of the second mesh
double det(const dmatrix &_a)
static void calcNormal(Vector3 &vec, const Vector3 &v1, const Vector3 &v2, const Vector3 &v3, double sgn)
double CD_s2
scale of the second mesh
#define HRP_COLLISION_EXPORT
Vector3 CD_Trans2
translation of the second mesh
HRP_COLLISION_EXPORT int tri_tri_overlap(const Vector3 &P1, const Vector3 &P2, const Vector3 &P3, const Vector3 &Q1, const Vector3 &Q2, const Vector3 &Q3, collision_data *col_p, CollisionPairInserterBase *collisionPairInserter)
double dot(const Vector3 &v1, const Vector3 &v2)
static double calc_depth(const Vector3 &ip1, const Vector3 &ip2, const Vector3 &n)
static int triangle_inside_test(const Vector3 &ef1, const Vector3 &ef2, const Vector3 &ef3, const Vector3 &P3, const Vector3 &P1, const Vector3 &P2, const Vector3 &Q)
static double calc_depth2(const Vector3 &ip1, const Vector3 &ip2, const Vector3 &pt1, const Vector3 &pt2, const Vector3 &pt3, const Vector3 &n)
static int get_normal_vector_test(const Vector3 &ip1, const Vector3 &v1, const Vector3 &ip2, const Vector3 &v2, const Vector3 &n1, const Vector3 &m1)
static int separability_test_by_face(const Vector3 &nm)
static int find_common_perpendicular(const Vector3 &p1, const Vector3 &p2, const Vector3 &q1, const Vector3 &q2, const Vector3 &ip1, const Vector3 &ip2, const Vector3 &n1, const Vector3 &m1, const Vector3 &n_vector, double &dp)