Subversion Repositories gelsvn

Rev

Rev 299 | Rev 324 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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