• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • Examples
  • File List
  • File Members

/home/hauberg/Dokumenter/Capture/humim-tracker-0.1/src/OpenTissue/OpenTissue/collision/collision_ray_triangle.h

Go to the documentation of this file.
00001 #ifndef OPENTISSUE_COLLISION_COLLISION_RAY_TRIANGLE_H
00002 #define OPENTISSUE_COLLISION_COLLISION_RAY_TRIANGLE_H
00003 //
00004 // OpenTissue Template Library
00005 // - A generic toolbox for physics-based modeling and simulation.
00006 // Copyright (C) 2008 Department of Computer Science, University of Copenhagen.
00007 //
00008 // OTTL is licensed under zlib: http://opensource.org/licenses/zlib-license.php
00009 //
00010 #include <OpenTissue/configuration.h>
00011 
00012 namespace OpenTissue
00013 {
00014   namespace collision
00015   {
00016 
00030     template<typename vector3_type,typename real_type>
00031     bool ray_triangle(
00032       vector3_type const & p
00033       , vector3_type const & r
00034       , vector3_type const & p0
00035       , vector3_type const & p1
00036       , vector3_type const & p2
00037       , real_type & t
00038       )
00039     {
00040       assert(r(0)!=0 || r(1)!=0 || r(2)!=0 || !"ray_triangle(): ray vector was zero!");
00041 
00042       vector3_type  e0 = p1 - p0;   //--- triangle edge
00043       vector3_type  e1 = p2 - p0;   //--- triangle edge
00044       vector3_type  n = e0 % e1;    //--- triangle normal
00045 
00046       real_type nr  = n*r;
00047 
00048       if(nr>=0)//--- backfacee culling
00049         return false;
00050 
00051       t = (n*(p0-p))/nr;  //--- Compute Intersection point between face plane and ray
00052       if(t<0)//--- Test if intertection point is along the ray direction.
00053         return false;
00054 
00055       vector3_type pp = p + (r*t);  //--- This is the intersection point.
00056 
00057       //--- Now we want to determine if the intersection point is inside the triangle
00058       real_type w1,w2,w3;
00059       OpenTissue::geometry::barycentric_algebraic(p0,p1,p2,pp,w1,w2,w3);//--- hmmm, I wonder why this does not work with p instead of pp?
00060 
00061       real_type delta = 10e-5;
00062       real_type lower = -delta;
00063       real_type upper = 1.+delta;
00064       if(
00065         (w1>lower)&&(w1<upper)
00066         &&
00067         (w2>lower)&&(w2<upper)
00068         &&
00069         (w3>lower)&&(w3<upper)
00070         )
00071         return true;
00072 
00073       //--- This were the old barycentric coordinate test, it did not work?
00074       //vector3_type Q  = pp - p0;
00075       //real_type e00 = e0*e0;
00076       //real_type e01 = e0*e1;
00077       //real_type e11 = e1*e1;
00078       //real_type q0 = e0*Q;
00079       //real_type q1 = e1*Q;
00080       //real_type delta = e00*e11 -(e01*e01);
00082       //real_type sigma0 = (e11*q0 - e01*q1);
00083       //real_type sigma1 = (e00*q1 - e01*q0);
00089       //if( (sigma0>=-0.0001) && (sigma1>=-0.0001) && ((sigma0+sigma1)<=delta))
00090       //  return true;
00091       return false;
00092     }
00093 
00094     template<typename vector3_type, typename triangle_type>
00095     bool ray_triangle(vector3_type const & p,vector3_type const &  r, triangle_type const & triangle)
00096 
00097     {
00098       return ray_triangle(p,r,triangle.p0(),triangle.p1(),triangle.p2());
00099     }
00100 
00101     template<typename ray_type, typename triangle_type>
00102     bool ray_triangle(ray_type const & ray, triangle_type const & triangle)
00103 
00104     {
00105       return ray_triangle(ray.p(),ray.r(),triangle.p0(),triangle.p1(),triangle.p2());
00106     }
00107 
00108   } //End of namespace collision
00109 } // namespace OpenTissue
00110 
00111 //OPENTISSUE_COLLISION_COLLISION_RAY_TRIANGLE_H
00112 #endif

Generated on Thu Dec 1 2011 12:50:45 for HUMIM Tracker by  doxygen 1.7.1