Subversion Repositories gelsvn

Rev

Rev 324 | Rev 510 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
443 jab 1
#ifndef __GEOMETRY_BOUNDINGLNODE_H
2
#define __GEOMETRY_BOUNDINGLNODE_H
290 jrf 3
 
309 jab 4
#include "Ray.h"
290 jrf 5
#include "BoundingNode.h"
6
 
7
#define USE_LEAF_BOXES 1
8
 
291 jrf 9
namespace Geometry
10
{
11
 
290 jrf 12
/// Leaf node of a bounding box tree. References triangle.
13
template<class BoxType>
14
class BoundingLNode: public BoundingNode<BoxType>
15
{
16
	Triangle tri;
17
 public:
18
 
19
#if USE_LEAF_BOXES
20
	BoundingLNode(const BoxType& box,
21
								const Triangle& _tri): BoundingNode<BoxType>(box), tri(_tri) {}
22
#else
23
	BoundingLNode(const Triangle& _tri): BoundingNode<BoxType>(BoxType()), tri(_tri) {}
24
#endif
25
 
324 jab 26
	virtual ~BoundingLNode() {}
290 jrf 27
 
28
	bool intersect(const CGLA::Vec3f&,const CGLA::Vec3f&,float&) const;
309 jab 29
	void intersect(Ray&r) const;
290 jrf 30
	int intersect_cnt(const CGLA::Vec3f&,const CGLA::Vec3f&) const;
31
 
32
	void sq_distance(const CGLA::Vec3f&, float&, float&, float&) const;
33
 
34
	virtual bool is_leaf() const {return true;}
35
 
36
	const Triangle& get_tri() const {return tri;}
37
};
38
 
39
template<class BoxType>
40
inline bool BoundingLNode<BoxType>::intersect(const CGLA::Vec3f& p, 
41
																							const CGLA::Vec3f& dir,
42
																							float& tmin) const
43
{
44
#if USE_LEAF_BOXES
45
	if(BoxType::intersect(p,dir))
46
 		return tri.intersect(p,dir,tmin);
47
 	return false;
48
#else
49
	return tri.intersect(p,dir,tmin);
50
#endif
51
 
52
}
53
 
54
template<class BoxType>
309 jab 55
inline void BoundingLNode<BoxType>::intersect(Ray& r) const
56
{
57
		CGLA::Vec3f p = r.origin;
58
		CGLA::Vec3f d = r.direction;
59
		float t = FLT_MAX;
60
		if(tri.intersect(p,d,t) && t < r.dist)
61
		{
62
				r.has_hit = true;
63
				r.dist = t;
64
				r.hit_pos = p + t*d;
65
				r.hit_normal = tri.get_face_norm();
66
		}
67
}
68
 
69
template<class BoxType>
290 jrf 70
inline int BoundingLNode<BoxType>::intersect_cnt(const CGLA::Vec3f& p, 
71
																								 const CGLA::Vec3f& dir) const
72
{
73
#if USE_LEAF_BOXES
291 jrf 74
	float tmin=1.0e33f;
290 jrf 75
	if(BoxType::intersect(p,dir) && 
76
		 tri.intersect(p,dir,tmin) &&
77
		 tmin > 0)
78
		return 1;
79
	return 0;
80
#else
81
	float tmin=1e33;
82
	if(tri.intersect(p,dir,tmin) &&
83
		 tmin > 0)
84
		return 1;
85
	return 0;
86
#endif
87
}
88
 
89
template<class BoxType>
90
inline void BoundingLNode<BoxType>::sq_distance(const CGLA::Vec3f& p, 
91
																								float& dmin, 
92
																								float& dmax, float& s) const
93
{
94
 	bool did_work = tri.signed_distance(p,dmax,s);
95
	if(!did_work) std::cout << dmax << std::endl;
96
	assert(did_work);
97
 	dmin = dmax; 
98
}
99
 
291 jrf 100
}
290 jrf 101
#endif