Subversion Repositories gelsvn

Rev

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

Rev 290 Rev 291
1
#include <cfloat>
1
#include <cfloat>
2
#include <GL/gl.h>
-
 
3
#include "AABox.h"
2
#include "AABox.h"
4
 
3
 
5
using namespace std;
4
using namespace std;
6
using namespace CGLA;
5
using namespace CGLA;
7
 
6
 
-
 
7
namespace Geometry
-
 
8
{
-
 
9
 
8
bool AABox::intersect(const CGLA::Vec3f& p, const CGLA::Vec3f& dir) const
10
bool AABox::intersect(const CGLA::Vec3f& p, const CGLA::Vec3f& dir) const
9
{
11
{
10
	Vec3f t0,t1;
12
	Vec3f t0,t1;
11
	for(int i=0;i<3;++i)
13
	for(int i=0;i<3;++i)
12
		{
14
		{
13
			t0[i] = (pmin[i]-p[i])/dir[i];
15
			t0[i] = (pmin[i]-p[i])/dir[i];
14
			t1[i] = (pmax[i]-p[i])/dir[i];
16
			t1[i] = (pmax[i]-p[i])/dir[i];
15
		}
17
		}
16
	float tmin = max(min(t0[0],t1[0]),max(min(t0[1],t1[1]),min(t0[2],t1[2])));
18
	float tmin = max(min(t0[0],t1[0]),max(min(t0[1],t1[1]),min(t0[2],t1[2])));
17
	float tmax = min(max(t0[0],t1[0]),min(max(t0[1],t1[1]),max(t0[2],t1[2])));
19
	float tmax = min(max(t0[0],t1[0]),min(max(t0[1],t1[1]),max(t0[2],t1[2])));
18
 
20
 
19
	return ( (tmin + CGLA::TINY) <tmax);
21
	return ( (tmin + CGLA::TINY) <tmax);
20
}
22
}
21
 
23
 
22
void AABox::gl_draw() const
-
 
23
{
-
 
24
	glBegin(GL_QUADS);
-
 
25
	Vec3f norm_neg[] = {Vec3f(0,0,-1), Vec3f(-1,0,0), Vec3f(0,-1,0)};
-
 
26
	Vec3f norm_pos[] = {Vec3f(0,0, 1), Vec3f( 1,0,0), Vec3f(0, 1,0)};
-
 
27
	for(int j=0;j<3;++j)
-
 
28
		{
-
 
29
			glNormal3fv(norm_neg[j].get());
-
 
30
			Vec3f p = pmin;
-
 
31
			glVertex3f(p[0], p[1], p[2]);
-
 
32
			p[(j+1)%3] = pmax[(j+1)%3];
-
 
33
			glVertex3f(p[0], p[1], p[2]);
-
 
34
			p[j] = pmax[j];
-
 
35
			glVertex3f(p[0], p[1], p[2]);
-
 
36
			p[(j+1)%3] = pmin[(j+1)%3];
-
 
37
			glVertex3f(p[0], p[1], p[2]);
-
 
38
		}
-
 
39
	glEnd();
-
 
40
	glBegin(GL_QUADS);
-
 
41
	for(int j=0;j<3;++j)
-
 
42
		{
-
 
43
			glNormal3fv(norm_pos[j].get());
-
 
44
			Vec3f p = pmax;
-
 
45
			glVertex3f(p[0], p[1], p[2]);
-
 
46
			p[j] = pmin[j];
-
 
47
			glVertex3f(p[0], p[1], p[2]);
-
 
48
			p[(j+1)%3] = pmin[(j+1)%3];
-
 
49
			glVertex3f(p[0], p[1], p[2]);
-
 
50
			p[j] = pmax[j];
-
 
51
			glVertex3f(p[0], p[1], p[2]);
-
 
52
		}
-
 
53
	glEnd();
-
 
54
}
-
 
55
 
-
 
56
void AABox::minmax_sq_dist(const CGLA::Vec3f& p, 
24
void AABox::minmax_sq_dist(const CGLA::Vec3f& p, 
57
													 float& dmin, float& dmax) const
25
													 float& dmin, float& dmax) const
58
{
26
{
59
	const Vec3f a = 0.5*pmax-0.5*pmin;
27
	const Vec3f a = 0.5*pmax-0.5*pmin;
60
	const Vec3f p0 = pmin + a;
28
	const Vec3f p0 = pmin + a;
61
 	Vec3f d = p-p0;
29
 	Vec3f d = p-p0;
62
 	Vec3f f(d);
30
 	Vec3f f(d);
63
 
31
 
64
	for(int i=0;i<3;++i)
32
	for(int i=0;i<3;++i)
65
		{
33
		{
66
 			if(f[i]>=0) 
34
 			if(f[i]>=0) 
67
 				f[i] = p[i]-pmin[i];
35
 				f[i] = p[i]-pmin[i];
68
 			else
36
 			else
69
 				f[i] = p[i]-pmax[i];
37
 				f[i] = p[i]-pmax[i];
70
			
38
			
71
			if(d[i]<-a[i])
39
			if(d[i]<-a[i])
72
				d[i] = p[i]-pmin[i];
40
				d[i] = p[i]-pmin[i];
73
			else if(d[i]>a[i])
41
			else if(d[i]>a[i])
74
				d[i] = p[i]-pmax[i];
42
				d[i] = p[i]-pmax[i];
75
			else 
43
			else 
76
				d[i] = 0;
44
				d[i] = 0;
77
		}
45
		}
78
	dmin = sqr_length(d);
46
	dmin = sqr_length(d);
79
//  	dmax = sqr_length(p-interior_point);
47
//  	dmax = sqr_length(p-interior_point);
80
	dmax = sqr_length(f);
48
	dmax = sqr_length(f);
81
	assert(dmin<=dmax);
49
	assert(dmin<=dmax);
82
}
50
}
83
 
51
 
84
AABox AABox::box_triangle(const Triangle& t)
52
AABox AABox::box_triangle(const Triangle& t)
85
{
53
{
86
	return AABox(t.get_pmin(), t.get_pmax(), t.get_centre());
54
	return AABox(t.get_pmin(), t.get_pmax(), t.get_centre());
87
}
55
}
88
 
56
 
89
 
57
 
90
AABox AABox::box_and_split(const std::vector<Triangle>& invec,
58
AABox AABox::box_and_split(const std::vector<Triangle>& invec,
91
													 std::vector<Triangle>& lvec,
59
													 std::vector<Triangle>& lvec,
92
													 std::vector<Triangle>& rvec)
60
													 std::vector<Triangle>& rvec)
93
{
61
{
94
	const int N = invec.size();
62
	const int N = invec.size();
95
	Vec3f tri_pmin(FLT_MAX), tri_pmax(-FLT_MAX);
63
	Vec3f tri_pmin(FLT_MAX), tri_pmax(-FLT_MAX);
96
			
64
			
97
	for(int i=0;i<N;++i)
65
	for(int i=0;i<N;++i)
98
		{
66
		{
99
			tri_pmin = v_min(invec[i].get_pmin(), tri_pmin);
67
			tri_pmin = v_min(invec[i].get_pmin(), tri_pmin);
100
			tri_pmax = v_max(invec[i].get_pmax(), tri_pmax);
68
			tri_pmax = v_max(invec[i].get_pmax(), tri_pmax);
101
		}
69
		}
102
	Vec3f diff = tri_pmax - tri_pmin;
70
	Vec3f diff = tri_pmax - tri_pmin;
103
 
71
 
104
	// Find the point closest to the centre.
72
	// Find the point closest to the centre.
105
	Vec3f centre = tri_pmin + diff;
73
	Vec3f centre = tri_pmin + diff;
106
	Vec3f centre_close = invec[0].get_v0();
74
	Vec3f centre_close = invec[0].get_v0();
107
	float min_dist = FLT_MAX;
75
	float min_dist = FLT_MAX;
108
	for(int i=0;i<N;++i)
76
	for(int i=0;i<N;++i)
109
		{
77
		{
110
			Vec3f v0 = invec[i].get_v0();
78
			Vec3f v0 = invec[i].get_v0();
111
			Vec3f v1 = invec[i].get_v1();
79
			Vec3f v1 = invec[i].get_v1();
112
			Vec3f v2 = invec[i].get_v2();
80
			Vec3f v2 = invec[i].get_v2();
113
			float sl0 = sqr_length(centre-v0);
81
			float sl0 = sqr_length(centre-v0);
114
			if(sl0 < min_dist)
82
			if(sl0 < min_dist)
115
				{
83
				{
116
					min_dist = sl0;
84
					min_dist = sl0;
117
					centre_close = v0;
85
					centre_close = v0;
118
				}
86
				}
119
			float sl1 = sqr_length(centre-v1);
87
			float sl1 = sqr_length(centre-v1);
120
			if(sl1 < min_dist)
88
			if(sl1 < min_dist)
121
				{
89
				{
122
					min_dist = sl1;
90
					min_dist = sl1;
123
					centre_close = v1;
91
					centre_close = v1;
124
				}
92
				}
125
			float sl2 = sqr_length(centre-v2);
93
			float sl2 = sqr_length(centre-v2);
126
			if(sl2 < min_dist)
94
			if(sl2 < min_dist)
127
				{
95
				{
128
					min_dist = sl2;
96
					min_dist = sl2;
129
					centre_close = v2;
97
					centre_close = v2;
130
				}
98
				}
131
		}
99
		}
132
 
100
 
133
	int k;
101
	int k;
134
	if(diff[0]>diff[1])
102
	if(diff[0]>diff[1])
135
		{
103
		{
136
			if(diff[0]>diff[2]) 
104
			if(diff[0]>diff[2]) 
137
				k = 0;
105
				k = 0;
138
			else 
106
			else 
139
				k = 2;
107
				k = 2;
140
		}
108
		}
141
	else
109
	else
142
		{
110
		{
143
			if(diff[1]>diff[2]) 
111
			if(diff[1]>diff[2]) 
144
				k = 1;
112
				k = 1;
145
			else 
113
			else 
146
				k = 2;
114
				k = 2;
147
		}
115
		}
148
 
116
 
149
	float thresh = diff[k]/2.0f + tri_pmin[k];
117
	float thresh = diff[k]/2.0f + tri_pmin[k];
150
 
118
 
151
 	for(int i=0;i<N;++i)
119
 	for(int i=0;i<N;++i)
152
		{
120
		{
153
			if(invec[i].get_centre()[k] > thresh)
121
			if(invec[i].get_centre()[k] > thresh)
154
				rvec.push_back(invec[i]);
122
				rvec.push_back(invec[i]);
155
			else
123
			else
156
				lvec.push_back(invec[i]);
124
				lvec.push_back(invec[i]);
157
		}
125
		}
158
	if(lvec.empty() || rvec.empty())
126
	if(lvec.empty() || rvec.empty())
159
		{
127
		{
160
			lvec.clear();
128
			lvec.clear();
161
			lvec.insert(lvec.end(),
129
			lvec.insert(lvec.end(),
162
									invec.begin(),
130
									invec.begin(),
163
									invec.begin()+N/2);
131
									invec.begin()+N/2);
164
			rvec.clear();
132
			rvec.clear();
165
			rvec.insert(rvec.end(),
133
			rvec.insert(rvec.end(),
166
									invec.begin()+N/2,
134
									invec.begin()+N/2,
167
									invec.end());
135
									invec.end());
168
		}
136
		}
169
	assert(!lvec.empty());
137
	assert(!lvec.empty());
170
	assert(!rvec.empty());
138
	assert(!rvec.empty());
171
	assert(lvec.size()+rvec.size() == invec.size());
139
	assert(lvec.size()+rvec.size() == invec.size());
172
	return AABox(tri_pmin, tri_pmax, centre_close);
140
	return AABox(tri_pmin, tri_pmax, centre_close);
173
}
141
}
174
 
142
 
-
 
143
}
175
 
144