• 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_aabb.h

Go to the documentation of this file.
00001 #ifndef OPENTISSUE_COLLISION_COLLISION_RAY_AABB_H
00002 #define OPENTISSUE_COLLISION_COLLISION_RAY_AABB_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 
00013 namespace OpenTissue
00014 {
00015   namespace collision
00016   {
00017 
00028     template<typename vector3_type>
00029     inline bool ray_aabb(vector3_type const & p,vector3_type const &  r, vector3_type const &  min_coord,vector3_type const &  max_coord)
00030     {
00031       using std::fabs;
00032 
00033       typedef typename vector3_type::value_traits   value_traits;
00034 
00035       assert(min_coord(0) <= max_coord(0)  || !"ray_aabb(): aabb was incorrect");
00036       assert(min_coord(1) <= max_coord(1)  || !"ray_aabb(): aabb was incorrect");
00037       assert(min_coord(2) <= max_coord(2)  || !"ray_aabb(): aabb was incorrect");
00038       assert(r(0)!=0 || r(1)!=0 || r(2)!=0 || !"ray_aabb(): ray vector was zero!");
00039 
00040       vector3_type e =  (max_coord - min_coord)*value_traits::half();  //--- Compute extents of box
00041       vector3_type d =  p - (e + min_coord);                           //--- Compute vector from center of box to origin of ray
00042 
00043       //--- Test if one of three axes of the box is a separation axe
00044       if (fabs(d(0)) > e(0) && d(0)*r(0) >= 0)  return false;
00045       if (fabs(d(1)) > e(1) && d(1)*r(1) >= 0)  return false;
00046       if (fabs(d(2)) > e(2) && d(2)*r(2) >= 0)  return false;
00047 
00048       //--- Test if one of three cross producs of the axes and the ray direction is a separation axe
00049       vector3_type rXd = cross(r , d);
00050 
00051       if ( fabs(rXd(0)) > ( e(1)*fabs(r(2)) + e(2)*fabs(r(1)) ) )  return false;
00052       if ( fabs(rXd(1)) > ( e(0)*fabs(r(2)) + e(2)*fabs(r(0)) ) )  return false;
00053       if ( fabs(rXd(2)) > ( e(0)*fabs(r(1)) + e(1)*fabs(r(0)) ) )  return false;
00054 
00055       //--- No sepration axe exist, we must have a collision
00056       return true;
00057     }
00058 
00059     template<typename vector3_type, typename aabb_type>
00060     inline bool ray_aabb(vector3_type const & p,vector3_type const &  r, aabb_type const & aabb)
00061     {
00062       return ray_aabb(p,r,aabb.min_coord(),aabb.max_coord());
00063     }
00064 
00065     template<typename ray_type, typename aabb_type>
00066     inline bool ray_aabb(ray_type const & ray, aabb_type const & aabb)
00067     {
00068       return ray_aabb(ray.p(),ray.r(),aabb.min_coord(),aabb.max_coord());
00069     }
00070 
00071   } //End of namespace collision
00072 } // namespace OpenTissue
00073 
00074 //OPENTISSUE_COLLISION_COLLISION_RAY_AABB_H
00075 #endif

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