Subversion Repositories gelsvn

Rev

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

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