Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Rocon/include/triangle.h File Reference
#include <cmath>
#include "NVec.h"
Include dependency graph for Rocon/include/triangle.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define EPSILON   1e-24
 
#define CROSS(dest, v1, v2)
 
#define DOT(v1, v2)   (v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2])
 
#define SUB(dest, v1, v2)
 

Functions

int intersect_ray_triangle (const Vec3D &RP0, const Vec3D &dir, Vec3D &TV0, Vec3D &TV1, Vec3D &TV2, Vec3D &I)
 
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 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_triangle2 (double orig[3], double dir[3], double vert0[3], double vert1[3], double vert2[3], double *t, double *u, double *v)
 
int intersect_triangle3 (double orig[3], double dir[3], double vert0[3], double vert1[3], double vert2[3], double *t, double *u, double *v)
 
bool rayIntersectsTriangle (double *p, double *d, double *v0, double *v1, double *v2, Vec3D &I)
 

Macro Definition Documentation

#define CROSS (   dest,
  v1,
  v2 
)
Value:
dest[0]=v1[1]*v2[2]-v1[2]*v2[1]; \
dest[1]=v1[2]*v2[0]-v1[0]*v2[2]; \
dest[2]=v1[0]*v2[1]-v1[1]*v2[0];

Definition at line 16 of file Rocon/include/triangle.h.

#define DOT (   v1,
  v2 
)    (v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2])

Definition at line 20 of file Rocon/include/triangle.h.

#define EPSILON   1e-24

Definition at line 14 of file Rocon/include/triangle.h.

#define SUB (   dest,
  v1,
  v2 
)
Value:
dest[0]=v1[0]-v2[0]; \
dest[1]=v1[1]-v2[1]; \
dest[2]=v1[2]-v2[2];

Definition at line 21 of file Rocon/include/triangle.h.

Function Documentation

int intersect_ray_triangle ( const Vec3D RP0,
const Vec3D dir,
Vec3D TV0,
Vec3D TV1,
Vec3D TV2,
Vec3D I 
)

Definition at line 283 of file Rocon/src/ray-triangle.C.

285 {
286  Vec3D u, v, n; // triangle vectors
287  Vec3D w0, w; // ray vectors
288  double r, a, b; // params to calc ray-plane intersect
289 
290  // get triangle edge vectors and plane normal
291  u = TV1 - TV0;
292  v = TV2 - TV0;
293  n = cross(u,v); // cross product
294  if (n == Vec3D(0,0,0)) // triangle is degenerate
295  return 0; // do not deal with this case
296 
297  //dir = RP1 - RP0; // ray direction vector
298  w0 = RP0 - TV0;
299  a = -1.0*(n*w0);
300  b = n*dir;
301  if (fabs(b) < SMALL_NUM) { // ray is parallel to triangle plane
302  if (a != 0) // ray lies in triangle plane
303  //return 1;
304  //else
305  return 0; // ray disjoint from plane
306  }
307 
308  // get intersect point of ray with triangle plane
309  r = a / b;
310  if (r < 0.0) // ray goes away from triangle
311  return 0; // => no intersect
312  if (r > 1.0) // ray goes away from triangle
313  return 0; //change by prateek, 0 instead of 3
314  // for a segment, also test if (r > 1.0) => no intersect
315 
316  I = RP0 + (r * dir); // intersect point of ray and plane
317 
318  // is I inside T?
319  double uu, uv, vv, wu, wv, D;
320  uu = (u*u);
321  uv = (u*v);
322  vv = (v*v);
323  w = I - TV0;
324  wu = w*u;
325  wv = w*v;
326  D = (uv * uv) -( uu * vv);
327 
328  // get and test parametric coords
329  double s, t;
330  s = (uv * wv - vv * wu) / D;
331  if (s < 0.0 || s > 1.0) // I is outside T
332  return 0;
333  t = (uv * wu - uu * wv) / D;
334  if (t < 0.0 || (s + t) > 1.0) // I is outside T
335  return 0;
336 
337  return 1; // I is in T
338 }
NVec< 3, T > cross(const NVec< 3, T > &u, const NVec< 3, T > &v)
double s
Definition: blastest.C:80
NVec< 3, double > Vec3D
*********************************************************************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
Definition: roccomf90.h:20
const NT & n
#define SMALL_NUM
int intersect_triangle ( double  orig[3],
double  dir[3],
double  vert0[3],
double  vert1[3],
double  vert2[3],
double *  t,
double *  u,
double *  v 
)

Definition at line 344 of file Rocon/src/ray-triangle.C.

Referenced by rayIntersectsTriangle().

347 {
348  double edge1[3], edge2[3], tvec[3], pvec[3], qvec[3];
349  double det,inv_det;
350 
351  /* find vectors for two edges sharing vert0 */
352  SUB(edge1, vert1, vert0);
353  SUB(edge2, vert2, vert0);
354 
355  /* begin calculating determinant - also used to calculate U parameter */
356  CROSS(pvec, dir, edge2);
357 
358  /* if determinant is near zero, ray lies in plane of triangle */
359  det = DOT(edge1, pvec);
360 
361  if (det > -EPSILON && det < EPSILON)
362  return 0;
363  inv_det = 1.0 / det;
364 
365  /* calculate distance from vert0 to ray origin */
366  SUB(tvec, orig, vert0);
367 
368  /* calculate U parameter and test bounds */
369  *u = DOT(tvec, pvec) * inv_det;
370  if (*u < 0.0 || *u > 1.0)
371  return 0;
372 
373  /* prepare to test V parameter */
374  CROSS(qvec, tvec, edge1);
375 
376  /* calculate V parameter and test bounds */
377  *v = DOT(dir, qvec) * inv_det;
378  if (*v < 0.0 || *u + *v > 1.0)
379  return 0;
380 
381  /* calculate t, ray intersects triangle */
382  *t = DOT(edge2, qvec) * inv_det;
383 
384  if(*t<0 || *t>1)
385  return 0; //enforce that ray is line segment
386 
387  return 1;
388 }
#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
Definition: roccomf90.h:20
#define DOT(v1, v2)
double det(const Matrix3D &A)
#define EPSILON
#define CROSS(dest, v1, v2)

Here is the caller graph for this function:

int intersect_triangle1 ( double  orig[3],
double  dir[3],
double  vert0[3],
double  vert1[3],
double  vert2[3],
double *  t,
double *  u,
double *  v 
)

Definition at line 393 of file Rocon/src/ray-triangle.C.

396 {
397  double edge1[3], edge2[3], tvec[3], pvec[3], qvec[3];
398  double det,inv_det;
399 
400  /* find vectors for two edges sharing vert0 */
401  SUB(edge1, vert1, vert0);
402  SUB(edge2, vert2, vert0);
403 
404  /* begin calculating determinant - also used to calculate U parameter */
405  CROSS(pvec, dir, edge2);
406 
407  /* if determinant is near zero, ray lies in plane of triangle */
408  det = DOT(edge1, pvec);
409 
410  if (det > EPSILON)
411  {
412  /* calculate distance from vert0 to ray origin */
413  SUB(tvec, orig, vert0);
414 
415  /* calculate U parameter and test bounds */
416  *u = DOT(tvec, pvec);
417  if (*u < 0.0 || *u > det)
418  return 0;
419 
420  /* prepare to test V parameter */
421  CROSS(qvec, tvec, edge1);
422 
423  /* calculate V parameter and test bounds */
424  *v = DOT(dir, qvec);
425  if (*v < 0.0 || *u + *v > det)
426  return 0;
427 
428  }
429  else if(det < -EPSILON)
430  {
431  /* calculate distance from vert0 to ray origin */
432  SUB(tvec, orig, vert0);
433 
434  /* calculate U parameter and test bounds */
435  *u = DOT(tvec, pvec);
436 /* printf("*u=%f\n",(double)*u); */
437 /* printf("det=%f\n",det); */
438  if (*u > 0.0 || *u < det)
439  return 0;
440 
441  /* prepare to test V parameter */
442  CROSS(qvec, tvec, edge1);
443 
444  /* calculate V parameter and test bounds */
445  *v = DOT(dir, qvec) ;
446  if (*v > 0.0 || *u + *v < det)
447  return 0;
448  }
449  else return 0; /* ray is parallell to the plane of the triangle */
450 
451 
452  inv_det = 1.0 / det;
453 
454  /* calculate t, ray intersects triangle */
455  *t = DOT(edge2, qvec) * inv_det;
456  (*u) *= inv_det;
457  (*v) *= inv_det;
458 
459  return 1;
460 }
#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
Definition: roccomf90.h:20
#define DOT(v1, v2)
double det(const Matrix3D &A)
#define EPSILON
#define CROSS(dest, v1, v2)
int intersect_triangle2 ( double  orig[3],
double  dir[3],
double  vert0[3],
double  vert1[3],
double  vert2[3],
double *  t,
double *  u,
double *  v 
)

Definition at line 464 of file Rocon/src/ray-triangle.C.

467 {
468  double edge1[3], edge2[3], tvec[3], pvec[3], qvec[3];
469  double det,inv_det;
470 
471  /* find vectors for two edges sharing vert0 */
472  SUB(edge1, vert1, vert0);
473  SUB(edge2, vert2, vert0);
474 
475  /* begin calculating determinant - also used to calculate U parameter */
476  CROSS(pvec, dir, edge2);
477 
478  /* if determinant is near zero, ray lies in plane of triangle */
479  det = DOT(edge1, pvec);
480 
481  /* calculate distance from vert0 to ray origin */
482  SUB(tvec, orig, vert0);
483  inv_det = 1.0 / det;
484 
485  if (det > EPSILON)
486  {
487  /* calculate U parameter and test bounds */
488  *u = DOT(tvec, pvec);
489  if (*u < 0.0 || *u > det)
490  return 0;
491 
492  /* prepare to test V parameter */
493  CROSS(qvec, tvec, edge1);
494 
495  /* calculate V parameter and test bounds */
496  *v = DOT(dir, qvec);
497  if (*v < 0.0 || *u + *v > det)
498  return 0;
499 
500  }
501  else if(det < -EPSILON)
502  {
503  /* calculate U parameter and test bounds */
504  *u = DOT(tvec, pvec);
505  if (*u > 0.0 || *u < det)
506  return 0;
507 
508  /* prepare to test V parameter */
509  CROSS(qvec, tvec, edge1);
510 
511  /* calculate V parameter and test bounds */
512  *v = DOT(dir, qvec) ;
513  if (*v > 0.0 || *u + *v < det)
514  return 0;
515  }
516  else return 0; /* ray is parallell to the plane of the triangle */
517 
518  /* calculate t, ray intersects triangle */
519  *t = DOT(edge2, qvec) * inv_det;
520  (*u) *= inv_det;
521  (*v) *= inv_det;
522 
523  return 1;
524 }
#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
Definition: roccomf90.h:20
#define DOT(v1, v2)
double det(const Matrix3D &A)
#define EPSILON
#define CROSS(dest, v1, v2)
int intersect_triangle3 ( double  orig[3],
double  dir[3],
double  vert0[3],
double  vert1[3],
double  vert2[3],
double *  t,
double *  u,
double *  v 
)

Definition at line 529 of file Rocon/src/ray-triangle.C.

532 {
533  double edge1[3], edge2[3], tvec[3], pvec[3], qvec[3];
534  double det,inv_det;
535 
536  /* find vectors for two edges sharing vert0 */
537  SUB(edge1, vert1, vert0);
538  SUB(edge2, vert2, vert0);
539 
540  /* begin calculating determinant - also used to calculate U parameter */
541  CROSS(pvec, dir, edge2);
542 
543  /* if determinant is near zero, ray lies in plane of triangle */
544  det = DOT(edge1, pvec);
545 
546  /* calculate distance from vert0 to ray origin */
547  SUB(tvec, orig, vert0);
548  inv_det = 1.0 / det;
549 
550  CROSS(qvec, tvec, edge1);
551 
552  if (det > EPSILON)
553  {
554  *u = DOT(tvec, pvec);
555  if (*u < 0.0 || *u > det)
556  return 0;
557 
558  /* calculate V parameter and test bounds */
559  *v = DOT(dir, qvec);
560  if (*v < 0.0 || *u + *v > det)
561  return 0;
562 
563  }
564  else if(det < -EPSILON)
565  {
566  /* calculate U parameter and test bounds */
567  *u = DOT(tvec, pvec);
568  if (*u > 0.0 || *u < det)
569  return 0;
570 
571  /* calculate V parameter and test bounds */
572  *v = DOT(dir, qvec) ;
573  if (*v > 0.0 || *u + *v < det)
574  return 0;
575  }
576  else return 0; /* ray is parallell to the plane of the triangle */
577 
578  *t = DOT(edge2, qvec) * inv_det;
579  (*u) *= inv_det;
580  (*v) *= inv_det;
581 
582  return 1;
583 }
#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
Definition: roccomf90.h:20
#define DOT(v1, v2)
double det(const Matrix3D &A)
#define EPSILON
#define CROSS(dest, v1, v2)
bool rayIntersectsTriangle ( double *  p,
double *  d,
double *  v0,
double *  v1,
double *  v2,
Vec3D I 
)

Definition at line 586 of file Rocon/src/ray-triangle.C.

Referenced by MeshBndSurf::intersection().

588 {
589  /*Vec3D org(p[0],p[1],p[2]);
590  Vec3D dir(d[0],d[1],d[2]);
591  Vec3D ver0(v0[0],v0[1],v0[2]);
592  Vec3D ver1(v1[0],v1[1],v1[2]);
593  Vec3D ver2(v2[0],v2[1],v2[2]);
594  int result = intersect_ray_triangle(org,dir,ver0,ver1,ver2,I);*/
595 
596  /*RAYTRI * input = new RAYTRI;
597  for(int i =0;i<3;++i)
598  {
599  input->org[i] = p[i];
600  input->dir[i] = d[i];
601  input->v0[i] = v0[i];
602  input->v1[i] = v1[i];
603  input->v2[i] = v2[i];
604  input->end[i] = p[i]+d[i];
605  }
606  int result = c2005(input);*/
607 
608  /*double *t,*u,*v;
609  t = new double;
610  u = new double;
611  v = new double;
612  *t = *u = *v = 0;
613  int result = intersect_triangle(p,d,v0,v1,v2,t,u,v);
614  for(int j=0;j<3;j++)
615  {
616  I[j]=p[j]+ (*t)*d[j];//I[j]=v0[j]+ (*u)*v1[j] + (*v)*v2[j];
617  }*/
618 
619  int result = P_triRay(p,d,v0,v1,v2,I);
620 
621  if(result)
622  { double *t,*u,*v;
623  t = new double;
624  u = new double;
625  v = new double;
626  *t = *u = *v = 0;
627  result = intersect_triangle(p,d,v0,v1,v2,t,u,v);
628  for(int j=0;j<3;j++)
629  {
630  I[j]=p[j]+ (*t)*d[j];
631  }
632  delete t;
633  delete u;
634  delete v;
635  }
636  return result;
637 }
const NT & d
*********************************************************************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
Definition: roccomf90.h:20
j indices j
Definition: Indexing.h:6
int P_triRay(double *p, double *d, double *v0, double *v1, double *v2, Vec3D &I)
int intersect_triangle(double orig[3], double dir[3], double vert0[3], double vert1[3], double vert2[3], double *t, double *u, double *v)

Here is the caller graph for this function: