Subversion Repositories gelsvn

Rev

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

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