Subversion Repositories gelsvn

Rev

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

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