Subversion Repositories gelsvn

Rev

Rev 299 | Rev 341 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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