Subversion Repositories gelsvn

Rev

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