Subversion Repositories gelsvn

Rev

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

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