Subversion Repositories gelsvn

Rev

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

Rev 309 Rev 341
1
#include <iostream>
1
#include <iostream>
2
 
2
 
3
#include "Util/Timer.h"
3
#include "Util/Timer.h"
4
#include "Util/ArgExtracter.h"
4
#include "Util/ArgExtracter.h"
5
 
5
 
6
#include "CGLA/Mat4x4f.h"
6
#include "CGLA/Mat4x4f.h"
7
 
7
 
8
 
8
 
9
#include "Geometry/RGrid.h"
9
#include "Geometry/RGrid.h"
10
#include "Geometry/save_raw.h"
10
#include "Geometry/save_raw.h"
11
#include "Geometry/GridAlgorithm.h"
11
#include "Geometry/GridAlgorithm.h"
12
#include "Geometry/build_bbtree.h"
12
#include "Geometry/build_bbtree.h"
13
#include "Geometry/AABox.h"
13
#include "Geometry/AABox.h"
14
 
14
 
15
#include "HMesh/triangulate.h"
15
#include "HMesh/triangulate.h"
-
 
16
 
-
 
17
#include "HMesh/obj_load.h"
16
#include "HMesh/x3d_load.h"
18
#include "HMesh/x3d_load.h"
17
#include "HMesh/x3d_save.h"
19
#include "HMesh/x3d_save.h"
18
 
20
 
19
using namespace std;
21
using namespace std;
20
using namespace HMesh;
22
using namespace HMesh;
21
using namespace Geometry;
23
using namespace Geometry;
22
using namespace CGLA;
24
using namespace CGLA;
23
using namespace Util;
25
using namespace Util;
24
 
26
 
25
namespace
27
namespace
26
{
28
{
27
 
29
 
28
	Vec3i vol_dim(64);
30
	Vec3i vol_dim(64);
29
	
31
	
30
		const Mat4x4f fit_bounding_volume(const Vec3f& p0,
32
		const Mat4x4f fit_bounding_volume(const Vec3f& p0,
31
																		const Vec3f& p7,
33
																		const Vec3f& p7,
32
																		float buf_reg) 
34
																		float buf_reg) 
33
	{
35
	{
34
		Vec3f sz = p7 - p0;
36
		Vec3f sz = p7 - p0;
35
		Vec3i dims = vol_dim;
37
		Vec3i dims = vol_dim;
36
		Vec3f scal_vec = (Vec3f(dims)-Vec3f(2*buf_reg+2))/sz;
38
		Vec3f scal_vec = (Vec3f(dims)-Vec3f(2*buf_reg+2))/sz;
37
		float scal = min(scal_vec[0], min(scal_vec[1],scal_vec[2]));
39
		float scal = min(scal_vec[0], min(scal_vec[1],scal_vec[2]));
38
		
40
		
39
		Mat4x4f m = translation_Mat4x4f(Vec3f(0)+Vec3f(buf_reg+1));
41
		Mat4x4f m = translation_Mat4x4f(Vec3f(0)+Vec3f(buf_reg+1));
40
		m *= scaling_Mat4x4f(Vec3f(scal));
42
		m *= scaling_Mat4x4f(Vec3f(scal));
41
		m *= translation_Mat4x4f(-p0);
43
		m *= translation_Mat4x4f(-p0);
42
		return m;
44
		return m;
43
	}
45
	}
44
 
46
 
45
	bool do_ray_tests = false;
47
	bool do_ray_tests = false;
46
	bool do_obb = true;
48
	bool do_obb = true;
47
	bool do_aabb = false;
49
	bool do_aabb = false;
48
	bool flip_normals = false;
50
	bool flip_normals = false;
49
 
51
 
50
}
52
}
51
 
53
 
52
 
54
 
53
 
55
 
54
template<class BBTree>
56
template<class BBTree>
55
class DistCompCache
57
class DistCompCache
56
{
58
{
57
	BBTree *T;
59
	BBTree *T;
58
	float old_d;
60
	float old_d;
59
	Vec3i old_p;
61
	Vec3i old_p;
60
public:
62
public:
61
 
63
 
62
	DistCompCache(BBTree* _T): T(_T), old_p(-99999) {}
64
	DistCompCache(BBTree* _T): T(_T), old_p(-99999) {}
63
 
65
 
64
	void operator()(const CGLA::Vec3i& pi, float& vox_val)
66
	void operator()(const CGLA::Vec3i& pi, float& vox_val)
65
	{
67
	{
66
		Vec3f p(pi);
68
		Vec3f p(pi);
67
 		if(sqr_length(pi-old_p)==1)
69
 		if(sqr_length(pi-old_p)==1)
68
 			{
70
 			{
69
 				vox_val = T->compute_signed_distance(p,CGLA::sqr(1.001+fabs(old_d)));
71
 				vox_val = T->compute_signed_distance(p,CGLA::sqr(1.001+fabs(old_d)));
70
 			}
72
 			}
71
 		else
73
 		else
72
			vox_val = T->compute_signed_distance(p);
74
			vox_val = T->compute_signed_distance(p);
73
		if(flip_normals) vox_val = -vox_val;
75
		if(flip_normals) vox_val = -vox_val;
74
		old_p = pi;
76
		old_p = pi;
75
		old_d = vox_val;		
77
		old_d = vox_val;		
76
	}
78
	}
77
};
79
};
78
 
80
 
79
template<class BBTree>
81
template<class BBTree>
80
class DistComp
82
class DistComp
81
{
83
{
82
	BBTree *T;
84
	BBTree *T;
83
public:
85
public:
84
 
86
 
85
	DistComp(BBTree* _T): T(_T) {}
87
	DistComp(BBTree* _T): T(_T) {}
86
 
88
 
87
	void operator()(const CGLA::Vec3i& pi, float& vox_val)
89
	void operator()(const CGLA::Vec3i& pi, float& vox_val)
88
	{
90
	{
89
		Vec3f p(pi);
91
		Vec3f p(pi);
90
		vox_val =  T->compute_signed_distance(p);
92
		vox_val =  T->compute_signed_distance(p);
91
		if(flip_normals) vox_val = -vox_val;
93
		if(flip_normals) vox_val = -vox_val;
92
	}
94
	}
93
};
95
};
94
 
96
 
95
 
97
 
96
template<class BBTree>
98
template<class BBTree>
97
class RayCast
99
class RayCast
98
{
100
{
99
	BBTree *T;
101
	BBTree *T;
100
 
102
 
101
public:
103
public:
102
	
104
	
103
	RayCast(BBTree* _T): T(_T) {}
105
	RayCast(BBTree* _T): T(_T) {}
104
 
106
 
105
	void operator()(const CGLA::Vec3i& pi, float& vox_val)
107
	void operator()(const CGLA::Vec3i& pi, float& vox_val)
106
	{
108
	{
107
		int n = T->intersect_cnt(Vec3f(pi), Vec3f(1,0,0));
109
		int n = T->intersect_cnt(Vec3f(pi), Vec3f(1,0,0));
108
		if(n%2==0)
110
		if(n%2==0)
109
			vox_val=1000;
111
			vox_val=1000;
110
		else
112
		else
111
			vox_val=-1000;
113
			vox_val=-1000;
112
	}
114
	}
113
};
115
};
114
 
116
 
115
 
117
 
116
typedef RGrid<float> RGridf;
118
typedef RGrid<float> RGridf;
117
 
119
 
118
 
120
 
119
int main(int argc, char** argv)
121
int main(int argc, char** argv)
120
{	
122
{	
121
	// LOAD OBJ
123
	// LOAD OBJ
122
    Manifold m;
124
    Manifold m;
123
    if(argc>1)
125
    if(argc>1)
124
	{
126
	{
125
	  ArgExtracter ae(argc, argv);
127
		ArgExtracter ae(argc, argv);
126
 
128
		
127
	  do_aabb = ae.extract("-A");
129
		do_aabb = ae.extract("-A");
128
	  do_ray_tests = ae.extract("-R");
130
		do_ray_tests = ae.extract("-R");
129
	  flip_normals = ae.extract("-f");
131
		flip_normals = ae.extract("-f");
-
 
132
		string file = ae.get_last_arg();
-
 
133
		if(file.substr(file.length()-4,file.length())==".obj")
-
 
134
		{
-
 
135
			obj_load(file, m);
130
 
136
		}
-
 
137
		else
131
	  x3d_load(ae.get_last_arg(), m);
138
			x3d_load(file, m);
132
	}
139
	}
133
    else
140
    else
134
	{
141
	{
135
	  string fn("../../data/bunny-little.x3d");
142
		string fn("../../data/bunny-little.x3d");
136
	  x3d_load(fn, m);
143
		x3d_load(fn, m);
137
	}
144
	}
138
 
145
	
139
	if(!m.is_valid())
146
	if(!m.is_valid())
140
		{
147
	{
141
			cout << "Not a valid manifold" << endl;
148
		cout << "Not a valid manifold" << endl;
142
			exit(0);
149
		exit(0);
143
		}
150
	}
144
	triangulate(m);
151
	triangulate(m);
145
 
152
	
146
	Vec3f p0,p7;
153
	Vec3f p0,p7;
147
	m.get_bbox(p0, p7);
154
	m.get_bbox(p0, p7);
148
 
155
	
149
	Mat4x4f T = fit_bounding_volume(p0,p7,3);
156
	Mat4x4f T = fit_bounding_volume(p0,p7,3);
150
 
157
	
151
	for(VertexIter v = m.vertices_begin(); v != m.vertices_end(); ++v)
158
	for(VertexIter v = m.vertices_begin(); v != m.vertices_end(); ++v)
152
			v->pos = T.mul_3D_point(v->pos);
159
		v->pos = T.mul_3D_point(v->pos);
153
	
160
	
154
	
161
	
155
 	RGridf grid(vol_dim,FLT_MAX);
162
 	RGridf grid(vol_dim,FLT_MAX);
156
	Util::Timer tim;
163
	Util::Timer tim;
157
 
164
	
158
 
165
	
159
	float T_build_obb=0, T_build_aabb=0, T_dist_obb=0, 
166
	float T_build_obb=0, T_build_aabb=0, T_dist_obb=0, 
160
		T_dist_aabb=0, T_ray_obb=0, T_ray_aabb=0;
167
		T_dist_aabb=0, T_ray_obb=0, T_ray_aabb=0;
161
 
168
	
162
	if(do_obb)
169
	if(do_obb)
163
	{
170
	{
164
		tim.start();
171
		tim.start();
165
		OBBTree obb_tree;
172
		OBBTree obb_tree;
166
		build_OBBTree(m, obb_tree);
173
		build_OBBTree(m, obb_tree);
167
		T_build_obb = tim.get_secs();
174
		T_build_obb = tim.get_secs();
168
		
175
		
169
		tim.start();
176
		tim.start();
170
		DistCompCache<OBBTree> dist(&obb_tree);
177
		DistCompCache<OBBTree> dist(&obb_tree);
171
		for_each_voxel(grid, dist);
178
		for_each_voxel(grid, dist);
172
		T_dist_obb = tim.get_secs();
179
		T_dist_obb = tim.get_secs();
173
		
180
		
174
		save_raw_float("obb_dist.raw", grid);
181
		save_raw_float("obb_dist.raw", grid);
175
 
182
		
176
		if(do_ray_tests)
183
		if(do_ray_tests)
177
			{
184
		{
178
				tim.start();
185
			tim.start();
179
				RayCast<OBBTree> ray(&obb_tree);
186
			RayCast<OBBTree> ray(&obb_tree);
180
				for_each_voxel(grid, ray);
187
			for_each_voxel(grid, ray);
181
				T_ray_obb = tim.get_secs();
188
			T_ray_obb = tim.get_secs();
182
				
189
			
183
				save_raw_float("obb_ray.raw", grid);
190
			save_raw_float("obb_ray.raw", grid);
184
			}
191
		}
185
	}
192
	}
186
 
193
	
187
	if(do_aabb)
194
	if(do_aabb)
188
	{
195
	{
189
		tim.start();
196
		tim.start();
190
		AABBTree aabb_tree;
197
		AABBTree aabb_tree;
191
		build_AABBTree(m, aabb_tree);
198
		build_AABBTree(m, aabb_tree);
192
		T_build_aabb = tim.get_secs();
199
		T_build_aabb = tim.get_secs();
193
		
200
		
194
		tim.start();
201
		tim.start();
195
		DistCompCache<AABBTree> dist(&aabb_tree);
202
		DistCompCache<AABBTree> dist(&aabb_tree);
196
		for_each_voxel(grid, dist);
203
		for_each_voxel(grid, dist);
197
		T_dist_aabb = tim.get_secs();
204
		T_dist_aabb = tim.get_secs();
198
		
205
		
199
		save_raw_float("aabb_dist.raw", grid);
206
		save_raw_float("aabb_dist.raw", grid);
200
 
-
 
201
		if(do_ray_tests)
-
 
202
			{
-
 
203
				tim.start();
-
 
204
				RayCast<AABBTree> ray(&aabb_tree);
-
 
205
				for_each_voxel(grid, ray);
-
 
206
				T_ray_aabb = tim.get_secs();
-
 
207
		
207
		
-
 
208
		if(do_ray_tests)
-
 
209
		{
-
 
210
			tim.start();
-
 
211
			RayCast<AABBTree> ray(&aabb_tree);
-
 
212
			for_each_voxel(grid, ray);
-
 
213
			T_ray_aabb = tim.get_secs();
-
 
214
			
208
				save_raw_float("aabb_ray.raw", grid);
215
			save_raw_float("aabb_ray.raw", grid);
209
			}
216
		}
210
	}
217
	}
211
	cout.width(10);
218
	cout.width(10);
212
	cout << "Poly";
219
	cout << "Poly";
213
	cout.width(11);
220
	cout.width(11);
214
	cout <<"build_obb";
221
	cout <<"build_obb";
215
	cout.width(12);
222
	cout.width(12);
216
	cout << "build_aabb";
223
	cout << "build_aabb";
217
	cout.width(10);
224
	cout.width(10);
218
	cout << "dist_obb" ;
225
	cout << "dist_obb" ;
219
	cout.width(10);
226
	cout.width(10);
220
	cout << "dist_aabb";
227
	cout << "dist_aabb";
221
	cout.width(10);
228
	cout.width(10);
222
	cout << "ray_obb" ;
229
	cout << "ray_obb" ;
223
	cout.width(10);
230
	cout.width(10);
224
	cout << "ray_aabb";
231
	cout << "ray_aabb";
225
	cout << endl;
232
	cout << endl;
226
 
233
	
227
	cout.precision(4);
234
	cout.precision(4);
228
	cout.width(10);
235
	cout.width(10);
229
	cout << m.no_faces() << " ";
236
	cout << m.no_faces() << " ";
230
	cout.width(10);
237
	cout.width(10);
231
	cout << T_build_obb;
238
	cout << T_build_obb;
232
	cout.width(12);
239
	cout.width(12);
233
	cout << T_build_aabb;
240
	cout << T_build_aabb;
234
	cout.width(10);
241
	cout.width(10);
235
	cout << T_dist_obb;
242
	cout << T_dist_obb;
236
	cout.width(10);
243
	cout.width(10);
237
	cout << T_dist_aabb;
244
	cout << T_dist_aabb;
238
	cout.width(10);
245
	cout.width(10);
239
	cout << T_ray_obb;
246
	cout << T_ray_obb;
240
	cout.width(10);
247
	cout.width(10);
241
	cout << T_ray_aabb;
248
	cout << T_ray_aabb;
242
	cout << endl;
249
	cout << endl;
243
}
250
}
244
 
251
 
245
 
252
 
246
 
253
 
247
// Brute force code - never use ...
254
// Brute force code - never use ...
248
//  	cout << "Computing distances" << endl;
255
//  	cout << "Computing distances" << endl;
249
// 	for(int i=0;i<triangle_vec_global.size(); ++i)
256
// 	for(int i=0;i<triangle_vec_global.size(); ++i)
250
// 		{
257
// 		{
251
// 			k=0;
258
// 			k=0;
252
// 			TRI = triangle_vec_global[i];
259
// 			TRI = triangle_vec_global[i];
253
// 			for_each_voxel(grid, dist_brute);
260
// 			for_each_voxel(grid, dist_brute);
254
// 		}
261
// 		}
255
 
262
 
256
 
263
 
257
 
264