16 #define CROSS(dest,v1,v2) \
17 dest[0]=v1[1]*v2[2]-v1[2]*v2[1]; \
18 dest[1]=v1[2]*v2[0]-v1[0]*v2[2]; \
19 dest[2]=v1[0]*v2[1]-v1[1]*v2[0];
20 #define DOT(v1,v2) (v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2])
21 #define SUB(dest,v1,v2) \
22 dest[0]=v1[0]-v2[0]; \
23 dest[1]=v1[1]-v2[1]; \
25 #define PLUCKER(pline, pt1, pt2) \
26 pline[0] = pt1[0]*pt2[1] - pt2[0]*pt1[1]; \
27 pline[1] = pt1[0]*pt2[2] - pt2[0]*pt1[2]; \
28 pline[2] = pt1[0] - pt2[0]; \
29 pline[3] = pt1[1]*pt2[2] - pt2[1]*pt1[2]; \
30 pline[4] = pt1[2] - pt2[2]; \
31 pline[5] = pt2[1] - pt1[1];
32 #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])
33 #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]); }
49 double v0[3],v1[3],v2[3];
62 double *v1,
double *v2,
Vec3D & I)
65 double *
q,*L,*e1,*e2,*e3, s1, s2,s3;
67 for(
int i=0;
i<3;++
i)q[
i] = p[
i]+d[
i];
83 if(s1==0.0 && s2==0.0 && s3==0.0)
85 int pts[4][3] = {{0,0,0},{0,1,0},{0,0,1},{1,0,0}};
86 int edge[3] = {0,0,0};
87 int vertex[3] = {0,0,0};
98 if(!(cs1==0 && cs2==0 && cs3==0))
break;
127 else if(cs2==0 && cs3==0)
133 vertex[(i+1)-3*((i+1)/3)]++;
145 if(vertex[0]!=0 && !flag)
147 I[0] = v0[0]; I[1]=v0[1]; I[2]=v0[2];
150 if(vertex[1]!=0 && !flag)
152 I[0] = v1[0]; I[1]=v1[1]; I[2]=v1[2];
155 if(vertex[2]!=0 && !flag)
157 I[0] = v2[0]; I[1]=v2[1]; I[2]=v2[2];
161 if(!flag && (edge[0]==1||edge[0]==2) )
163 I[0]=(v0[0]+v1[0])/2;I[1]=(v0[1]+v1[1])/2;I[2]=(v0[2]+v1[2])/2;
166 if(!flag && (edge[1]==1||edge[1]==2) )
168 I[0]=(v1[0]+v2[0])/2;I[1]=(v1[1]+v2[1])/2;I[2]=(v1[2]+v2[2])/2;
171 if(!flag && (edge[2]==1||edge[2]==2) )
173 I[0]=(v2[0]+v0[0])/2;I[1]=(v0[1]+v2[1])/2;I[2]=(v0[2]+v2[2])/2;
177 }
else if ((s1>0 && s2>0 && s3>0) || (s1<0 && s2<0 && s3<0)){
183 I[
i] = al*v2[
i] + be*v0[
i] + ga*v1[
i];
185 }
else if ( s1==0 && s2*s3>0){
186 I[0]=(v0[0]+v1[0])/2;I[1]=(v0[1]+v1[1])/2;I[2]=(v0[2]+v1[2])/2;
188 }
else if ( s2==0 && s1*s3>0){
189 I[0]=(v1[0]+v2[0])/2;I[1]=(v1[1]+v2[1])/2;I[2]=(v1[2]+v2[2])/2;
191 }
else if ( s3==0 && s1*s2>0){
192 I[0]=(v2[0]+v0[0])/2;I[1]=(v0[1]+v2[1])/2;I[2]=(v0[2]+v2[2])/2;
194 }
else if ( s1==0 && s2==0){
195 I[0]=v1[0];I[1]=v1[1];I[2]=v1[2];
197 }
else if ( s1==0 && s3==0){
198 I[0]=v0[0];I[1]=v0[1];I[2]=v0[2];
200 }
else if ( s2==0 && s3==0){
201 I[0]=v2[0];I[1]=v2[1];I[2]=v2[2];
217 double e0[3],e1[3],e2[3],
norm[3],point[3],
v[3],av[3],vb[3],vc[3];
222 double pd =
DOT(norm, rt->
v0);
224 double signSrc =
DOT(norm, rt->
org) -pd;
225 double signDst =
DOT(norm, rt->
end) -pd;
227 if(signSrc*signDst > 0.0)
return 0;
229 double d = signSrc/(signSrc - signDst);
232 SUB(v, point, rt->
v0);
236 if(
DOT(av,vb) >= 0.0)
239 SUB(v, point, rt->
v1);
241 if(
DOT(av,vc) >= 0.0)
return 1;
273 #define SMALL_NUM 1e-24 // anything that avoids division overflow
294 if (n ==
Vec3D(0,0,0))
319 double uu, uv, vv, wu, wv, D;
326 D = (uv * uv) -( uu * vv);
330 s = (uv * wv - vv * wu) / D;
331 if (s < 0.0 || s > 1.0)
333 t = (uv * wu - uu * wv) / D;
334 if (t < 0.0 || (s + t) > 1.0)
345 double vert0[3],
double vert1[3],
double vert2[3],
346 double *t,
double *u,
double *
v)
348 double edge1[3], edge2[3], tvec[3], pvec[3], qvec[3];
352 SUB(edge1, vert1, vert0);
353 SUB(edge2, vert2, vert0);
356 CROSS(pvec, dir, edge2);
359 det =
DOT(edge1, pvec);
366 SUB(tvec, orig, vert0);
369 *u =
DOT(tvec, pvec) * inv_det;
370 if (*u < 0.0 || *u > 1.0)
374 CROSS(qvec, tvec, edge1);
377 *v =
DOT(dir, qvec) * inv_det;
378 if (*v < 0.0 || *u + *v > 1.0)
382 *t =
DOT(edge2, qvec) * inv_det;
394 double vert0[3],
double vert1[3],
double vert2[3],
395 double *t,
double *u,
double *
v)
397 double edge1[3], edge2[3], tvec[3], pvec[3], qvec[3];
401 SUB(edge1, vert1, vert0);
402 SUB(edge2, vert2, vert0);
405 CROSS(pvec, dir, edge2);
408 det =
DOT(edge1, pvec);
413 SUB(tvec, orig, vert0);
416 *u =
DOT(tvec, pvec);
417 if (*u < 0.0 || *u > det)
421 CROSS(qvec, tvec, edge1);
425 if (*v < 0.0 || *u + *v > det)
432 SUB(tvec, orig, vert0);
435 *u =
DOT(tvec, pvec);
438 if (*u > 0.0 || *u < det)
442 CROSS(qvec, tvec, edge1);
445 *v =
DOT(dir, qvec) ;
446 if (*v > 0.0 || *u + *v < det)
455 *t =
DOT(edge2, qvec) * inv_det;
465 double vert0[3],
double vert1[3],
double vert2[3],
466 double *t,
double *u,
double *
v)
468 double edge1[3], edge2[3], tvec[3], pvec[3], qvec[3];
472 SUB(edge1, vert1, vert0);
473 SUB(edge2, vert2, vert0);
476 CROSS(pvec, dir, edge2);
479 det =
DOT(edge1, pvec);
482 SUB(tvec, orig, vert0);
488 *u =
DOT(tvec, pvec);
489 if (*u < 0.0 || *u > det)
493 CROSS(qvec, tvec, edge1);
497 if (*v < 0.0 || *u + *v > det)
504 *u =
DOT(tvec, pvec);
505 if (*u > 0.0 || *u < det)
509 CROSS(qvec, tvec, edge1);
512 *v =
DOT(dir, qvec) ;
513 if (*v > 0.0 || *u + *v < det)
519 *t =
DOT(edge2, qvec) * inv_det;
530 double vert0[3],
double vert1[3],
double vert2[3],
531 double *t,
double *u,
double *
v)
533 double edge1[3], edge2[3], tvec[3], pvec[3], qvec[3];
537 SUB(edge1, vert1, vert0);
538 SUB(edge2, vert2, vert0);
541 CROSS(pvec, dir, edge2);
544 det =
DOT(edge1, pvec);
547 SUB(tvec, orig, vert0);
550 CROSS(qvec, tvec, edge1);
554 *u =
DOT(tvec, pvec);
555 if (*u < 0.0 || *u > det)
560 if (*v < 0.0 || *u + *v > det)
567 *u =
DOT(tvec, pvec);
568 if (*u > 0.0 || *u < det)
572 *v =
DOT(dir, qvec) ;
573 if (*v > 0.0 || *u + *v < det)
578 *t =
DOT(edge2, qvec) * inv_det;
587 double *v0,
double *v1,
double *v2,
Vec3D & I)
619 int result =
P_triRay(p,d,v0,v1,v2,I);
630 I[
j]=p[
j]+ (*t)*d[
j];
#define SUB(dest, v1, v2)
NVec< 3, T > cross(const NVec< 3, T > &u, const NVec< 3, T > &v)
void int int REAL REAL * y
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)
*********************************************************************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
bool rayIntersectsTriangle(double *p, double *d, double *v0, double *v1, double *v2, Vec3D &I)
double det(const Matrix3D &A)
#define PLUCKER(pline, pt1, pt2)
#define RAYPOINT(result, start, end, dist)
#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)