10 #define EPSILON 0.000001
11 #define CROSS(dest,v1,v2) \
12 dest[0]=v1[1]*v2[2]-v1[2]*v2[1]; \
13 dest[1]=v1[2]*v2[0]-v1[0]*v2[2]; \
14 dest[2]=v1[0]*v2[1]-v1[1]*v2[0];
15 #define DOT(v1,v2) (v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2])
16 #define SUB(dest,v1,v2) \
17 dest[0]=v1[0]-v2[0]; \
18 dest[1]=v1[1]-v2[1]; \
20 #define PLUCKER(pline, pt1, pt2) \
21 pline[0] = pt1[0]*pt2[1] - pt2[0]*pt1[1]; \
22 pline[1] = pt1[0]*pt2[2] - pt2[0]*pt1[2]; \
23 pline[2] = pt1[0] - pt2[0]; \
24 pline[3] = pt1[1]*pt2[2] - pt2[1]*pt1[2]; \
25 pline[4] = pt1[2] - pt2[2]; \
26 pline[5] = pt2[1] - pt1[1];
27 #define SIDE(p,q) (p[0]*q[4]+p[1]*q[5]+p[2]*q[3]+p[3]*q[2]+p[4]*q[0]+p[5]*q[1])
28 #define RAYPOINT(result,start,end,dist) { result[0]=start[0]+d*(end[0]-start[0]); result[1]=start[1]+d*(end[1]-start[1]); result[2]=start[2]+d*(end[2]-start[2]); }
57 double *v1,
double *v2,
Vec3D & I)
60 double *
q,*L,*e1,*e2,*e3, s1, s2,s3;
62 for(
int i=0;
i<3;++
i)q[
i] = p[
i]+d[
i];
78 if(s1==0.0 && s2==0.0 && s3==0.0)
80 int pts[4][3] = {{0,0,0},{0,1,0},{0,0,1},{1,0,0}};
81 int edge[3] = {0,0,0};
82 int vertex[3] = {0,0,0};
93 if(!(cs1==0 && cs2==0 && cs3==0))
break;
122 else if(cs2==0 && cs3==0)
128 vertex[(i+1)-3*((i+1)/3)]++;
140 if(vertex[0]!=0 && !flag)
142 I[0] = v0[0]; I[1]=v0[1]; I[2]=v0[2];
145 if(vertex[1]!=0 && !flag)
147 I[0] = v1[0]; I[1]=v1[1]; I[2]=v1[2];
150 if(vertex[2]!=0 && !flag)
152 I[0] = v2[0]; I[1]=v2[1]; I[2]=v2[2];
156 if(!flag && (edge[0]==1||edge[0]==2) )
158 I[0]=(v0[0]+v1[0])/2;I[1]=(v0[1]+v1[1])/2;I[2]=(v0[2]+v1[2])/2;
161 if(!flag && (edge[1]==1||edge[1]==2) )
163 I[0]=(v1[0]+v2[0])/2;I[1]=(v1[1]+v2[1])/2;I[2]=(v1[2]+v2[2])/2;
166 if(!flag && (edge[2]==1||edge[2]==2) )
168 I[0]=(v2[0]+v0[0])/2;I[1]=(v0[1]+v2[1])/2;I[2]=(v0[2]+v2[2])/2;
172 }
else if ((s1>0 && s2>0 && s3>0) || (s1<0 && s2<0 && s3<0)){
178 I[
i] = al*v2[
i] + be*v0[
i] + ga*v1[
i];
180 }
else if ( s1==0 && s2*s3>0){
181 I[0]=(v0[0]+v1[0])/2;I[1]=(v0[1]+v1[1])/2;I[2]=(v0[2]+v1[2])/2;
183 }
else if ( s2==0 && s1*s3>0){
184 I[0]=(v1[0]+v2[0])/2;I[1]=(v1[1]+v2[1])/2;I[2]=(v1[2]+v2[2])/2;
186 }
else if ( s3==0 && s1*s2>0){
187 I[0]=(v2[0]+v0[0])/2;I[1]=(v0[1]+v2[1])/2;I[2]=(v0[2]+v2[2])/2;
189 }
else if ( s1==0 && s2==0){
190 I[0]=v1[0];I[1]=v1[1];I[2]=v1[2];
192 }
else if ( s1==0 && s3==0){
193 I[0]=v0[0];I[1]=v0[1];I[2]=v0[2];
195 }
else if ( s2==0 && s3==0){
196 I[0]=v2[0];I[1]=v2[1];I[2]=v2[2];
212 float e0[3],e1[3],e2[3],
norm[3],point[3],
v[3],av[3],vb[3],vc[3];
217 float pd =
DOT(norm, rt->
v0);
219 float signSrc =
DOT(norm, rt->
org) -pd;
220 float signDst =
DOT(norm, rt->
end) -pd;
222 if(signSrc*signDst > 0.0)
return 0;
224 float d = signSrc/(signSrc - signDst);
227 SUB(v, point, rt->
v0);
231 if(
DOT(av,vb) >= 0.0)
234 SUB(v, point, rt->
v1);
236 if(
DOT(av,vc) >= 0.0)
return 1;
268 #define SMALL_NUM 0.00000001 // anything that avoids division overflow
289 if (n ==
Vec3D(0,0,0))
314 float uu, uv, vv, wu, wv, D;
321 D = (uv * uv) -( uu * vv);
325 s = (uv * wv - vv * wu) / D;
326 if (s < 0.0 || s > 1.0)
328 t = (uv * wu - uu * wv) / D;
329 if (t < 0.0 || (s + t) > 1.0)
340 double vert0[3],
double vert1[3],
double vert2[3],
341 double *t,
double *u,
double *
v)
343 double edge1[3], edge2[3], tvec[3], pvec[3], qvec[3];
347 SUB(edge1, vert1, vert0);
348 SUB(edge2, vert2, vert0);
351 CROSS(pvec, dir, edge2);
354 det =
DOT(edge1, pvec);
361 SUB(tvec, orig, vert0);
364 *u =
DOT(tvec, pvec) * inv_det;
365 if (*u < 0.0 || *u > 1.0)
369 CROSS(qvec, tvec, edge1);
372 *v =
DOT(dir, qvec) * inv_det;
373 if (*v < 0.0 || *u + *v > 1.0)
377 *t =
DOT(edge2, qvec) * inv_det;
389 double vert0[3],
double vert1[3],
double vert2[3],
390 double *t,
double *u,
double *
v)
392 double edge1[3], edge2[3], tvec[3], pvec[3], qvec[3];
396 SUB(edge1, vert1, vert0);
397 SUB(edge2, vert2, vert0);
400 CROSS(pvec, dir, edge2);
403 det =
DOT(edge1, pvec);
408 SUB(tvec, orig, vert0);
411 *u =
DOT(tvec, pvec);
412 if (*u < 0.0 || *u > det)
416 CROSS(qvec, tvec, edge1);
420 if (*v < 0.0 || *u + *v > det)
427 SUB(tvec, orig, vert0);
430 *u =
DOT(tvec, pvec);
433 if (*u > 0.0 || *u < det)
437 CROSS(qvec, tvec, edge1);
440 *v =
DOT(dir, qvec) ;
441 if (*v > 0.0 || *u + *v < det)
450 *t =
DOT(edge2, qvec) * inv_det;
460 double vert0[3],
double vert1[3],
double vert2[3],
461 double *t,
double *u,
double *
v)
463 double edge1[3], edge2[3], tvec[3], pvec[3], qvec[3];
467 SUB(edge1, vert1, vert0);
468 SUB(edge2, vert2, vert0);
471 CROSS(pvec, dir, edge2);
474 det =
DOT(edge1, pvec);
477 SUB(tvec, orig, vert0);
483 *u =
DOT(tvec, pvec);
484 if (*u < 0.0 || *u > det)
488 CROSS(qvec, tvec, edge1);
492 if (*v < 0.0 || *u + *v > det)
499 *u =
DOT(tvec, pvec);
500 if (*u > 0.0 || *u < det)
504 CROSS(qvec, tvec, edge1);
507 *v =
DOT(dir, qvec) ;
508 if (*v > 0.0 || *u + *v < det)
514 *t =
DOT(edge2, qvec) * inv_det;
525 double vert0[3],
double vert1[3],
double vert2[3],
526 double *t,
double *u,
double *
v)
528 double edge1[3], edge2[3], tvec[3], pvec[3], qvec[3];
532 SUB(edge1, vert1, vert0);
533 SUB(edge2, vert2, vert0);
536 CROSS(pvec, dir, edge2);
539 det =
DOT(edge1, pvec);
542 SUB(tvec, orig, vert0);
545 CROSS(qvec, tvec, edge1);
549 *u =
DOT(tvec, pvec);
550 if (*u < 0.0 || *u > det)
555 if (*v < 0.0 || *u + *v > det)
562 *u =
DOT(tvec, pvec);
563 if (*u > 0.0 || *u < det)
567 *v =
DOT(dir, qvec) ;
568 if (*v > 0.0 || *u + *v < det)
573 *t =
DOT(edge2, qvec) * inv_det;
582 double *v0,
double *v1,
double *v2,
Vec3D & I)
614 int result =
P_triRay(p,d,v0,v1,v2,I);
625 I[
j]=p[
j]+ (*t)*d[
j];
NVec< 3, T > cross(const NVec< 3, T > &u, const NVec< 3, T > &v)
void intersect(T_VertexSet s1, T_VertexSet s2, T_VertexSet *inter)
int intersect_triangle3(double orig[3], double dir[3], double vert0[3], double vert1[3], double vert2[3], double *t, double *u, double *v)
int intersect_ray_triangle(const Vec3D &RP0, const Vec3D &dir, Vec3D &TV0, Vec3D &TV1, Vec3D &TV2, Vec3D &I)
int intersect_triangle2(double orig[3], double dir[3], double vert0[3], double vert1[3], double vert2[3], double *t, double *u, double *v)
T norm(const NVec< DIM, T > &v)
#define SUB(dest, v1, v2)
*********************************************************************Illinois Open Source License ****University of Illinois NCSA **Open Source License University of Illinois All rights reserved ****Developed free of to any person **obtaining a copy of this software and associated documentation to deal with the Software without including without limitation the rights to and or **sell copies of the and to permit persons to whom the **Software is furnished to do subject to the following this list of conditions and the following disclaimers ****Redistributions in binary form must reproduce the above **copyright this list of conditions and the following **disclaimers in the documentation and or other materials **provided with the distribution ****Neither the names of the Center for Simulation of Advanced the University of nor the names of its **contributors may be used to endorse or promote products derived **from this Software without specific prior written permission ****THE SOFTWARE IS PROVIDED AS WITHOUT WARRANTY OF ANY **EXPRESS OR INCLUDING BUT NOT LIMITED TO THE WARRANTIES **OF FITNESS FOR A PARTICULAR PURPOSE AND **NONINFRINGEMENT IN NO EVENT SHALL THE CONTRIBUTORS OR **COPYRIGHT HOLDERS BE LIABLE FOR ANY DAMAGES OR OTHER WHETHER IN AN ACTION OF TORT OR **ARISING OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE **USE OR OTHER DEALINGS WITH THE SOFTWARE v
#define RAYPOINT(result, start, end, dist)
#define PLUCKER(pline, pt1, pt2)
bool rayIntersectsTriangle(double *p, double *d, double *v0, double *v1, double *v2, Vec3D &I)
double det(const Matrix3D &A)
#define CROSS(dest, v1, v2)
int P_triRay(double *p, double *d, double *v0, double *v1, double *v2, Vec3D &I)
int intersect_triangle1(double orig[3], double dir[3], double vert0[3], double vert1[3], double vert2[3], double *t, double *u, double *v)
int intersect_triangle(double orig[3], double dir[3], double vert0[3], double vert1[3], double vert2[3], double *t, double *u, double *v)
int c2005(const RAYTRI *rt)