Subversion Repositories gelsvn

Rev

Rev 510 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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