Subversion Repositories gelsvn

Rev

Rev 510 | Details | Compare with Previous | Last modification | View Log | RSS feed

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