GEL
2
GEL is a library for Geometry and Linear Algebra
|
00001 #ifndef __GEOMETRY_BOUNDINGLNODE_H 00002 #define __GEOMETRY_BOUNDINGLNODE_H 00003 00004 #include "Ray.h" 00005 #include "BoundingNode.h" 00006 00007 #define USE_LEAF_BOXES 0 00008 00009 namespace Geometry 00010 { 00011 00013 template<class BoxType> 00014 class BoundingLNode: public BoundingNode<BoxType> 00015 { 00016 Triangle tri; 00017 public: 00018 00019 #if USE_LEAF_BOXES 00020 BoundingLNode(const BoxType& box, 00021 const Triangle& _tri): BoundingNode<BoxType>(box), tri(_tri) {} 00022 #else 00023 BoundingLNode(const Triangle& _tri): BoundingNode<BoxType>(BoxType()), tri(_tri) {} 00024 #endif 00025 00026 virtual ~BoundingLNode() {} 00027 00028 bool intersect(const CGLA::Vec3f&,const CGLA::Vec3f&,float&) const; 00029 void intersect(Ray&r) const; 00030 int intersect_cnt(const CGLA::Vec3f&,const CGLA::Vec3f&) const; 00031 00032 void sq_distance(const CGLA::Vec3f&, float&, float&, float&) const; 00033 00034 virtual bool is_leaf() const {return true;} 00035 00036 const Triangle& get_tri() const {return tri;} 00037 }; 00038 00039 template<class BoxType> 00040 inline bool BoundingLNode<BoxType>::intersect(const CGLA::Vec3f& p, 00041 const CGLA::Vec3f& dir, 00042 float& tmin) const 00043 { 00044 #if USE_LEAF_BOXES 00045 if(BoxType::intersect(p,dir)) 00046 return tri.intersect(p,dir,tmin); 00047 return false; 00048 #else 00049 return tri.intersect(p,dir,tmin); 00050 #endif 00051 00052 } 00053 00054 template<class BoxType> 00055 inline void BoundingLNode<BoxType>::intersect(Ray& r) const 00056 { 00057 CGLA::Vec3f p = r.origin; 00058 CGLA::Vec3f d = r.direction; 00059 float t = FLT_MAX; 00060 if(tri.intersect(p,d,t) && t < r.dist) 00061 { 00062 r.has_hit = true; 00063 r.dist = t; 00064 r.hit_pos = p + t*d; 00065 r.hit_normal = tri.get_face_norm(); 00066 } 00067 } 00068 00069 template<class BoxType> 00070 inline int BoundingLNode<BoxType>::intersect_cnt(const CGLA::Vec3f& p, 00071 const CGLA::Vec3f& dir) const 00072 { 00073 #if USE_LEAF_BOXES 00074 float tmin=1.0e33f; 00075 if(BoxType::intersect(p,dir) && 00076 tri.intersect(p,dir,tmin) && 00077 tmin > 0) 00078 return 1; 00079 return 0; 00080 #else 00081 float tmin=1e33; 00082 if(tri.intersect(p,dir,tmin) && 00083 tmin > 0) 00084 return 1; 00085 return 0; 00086 #endif 00087 } 00088 00089 template<class BoxType> 00090 inline void BoundingLNode<BoxType>::sq_distance(const CGLA::Vec3f& p, 00091 float& dmin, 00092 float& dmax, float& s) const 00093 { 00094 bool did_work = tri.signed_distance(p,dmax,s); 00095 if(!did_work) std::cout << dmax << std::endl; 00096 assert(did_work); 00097 dmin = dmax; 00098 } 00099 00100 } 00101 #endif