Subversion Repositories gelsvn

Rev

Rev 309 | Rev 382 | 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
 
17
#include "HMesh/obj_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");
130
		do_ray_tests = ae.extract("-R");
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);
136
		}
137
		else
138
			x3d_load(file, m);
309 jab 139
	}
140
    else
141
	{
341 jab 142
		string fn("../../data/bunny-little.x3d");
143
		x3d_load(fn, m);
309 jab 144
	}
341 jab 145
 
309 jab 146
	if(!m.is_valid())
341 jab 147
	{
148
		cout << "Not a valid manifold" << endl;
149
		exit(0);
150
	}
309 jab 151
	triangulate(m);
341 jab 152
 
309 jab 153
	Vec3f p0,p7;
154
	m.get_bbox(p0, p7);
341 jab 155
 
309 jab 156
	Mat4x4f T = fit_bounding_volume(p0,p7,3);
341 jab 157
 
309 jab 158
	for(VertexIter v = m.vertices_begin(); v != m.vertices_end(); ++v)
341 jab 159
		v->pos = T.mul_3D_point(v->pos);
309 jab 160
 
161
 
162
 	RGridf grid(vol_dim,FLT_MAX);
163
	Util::Timer tim;
341 jab 164
 
165
 
309 jab 166
	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;
341 jab 168
 
309 jab 169
	if(do_obb)
170
	{
171
		tim.start();
172
		OBBTree obb_tree;
173
		build_OBBTree(m, obb_tree);
174
		T_build_obb = tim.get_secs();
175
 
176
		tim.start();
177
		DistCompCache<OBBTree> dist(&obb_tree);
178
		for_each_voxel(grid, dist);
179
		T_dist_obb = tim.get_secs();
180
 
181
		save_raw_float("obb_dist.raw", grid);
341 jab 182
 
309 jab 183
		if(do_ray_tests)
341 jab 184
		{
185
			tim.start();
186
			RayCast<OBBTree> ray(&obb_tree);
187
			for_each_voxel(grid, ray);
188
			T_ray_obb = tim.get_secs();
189
 
190
			save_raw_float("obb_ray.raw", grid);
191
		}
309 jab 192
	}
341 jab 193
 
309 jab 194
	if(do_aabb)
195
	{
196
		tim.start();
197
		AABBTree aabb_tree;
198
		build_AABBTree(m, aabb_tree);
199
		T_build_aabb = tim.get_secs();
200
 
201
		tim.start();
202
		DistCompCache<AABBTree> dist(&aabb_tree);
203
		for_each_voxel(grid, dist);
204
		T_dist_aabb = tim.get_secs();
205
 
206
		save_raw_float("aabb_dist.raw", grid);
341 jab 207
 
309 jab 208
		if(do_ray_tests)
341 jab 209
		{
210
			tim.start();
211
			RayCast<AABBTree> ray(&aabb_tree);
212
			for_each_voxel(grid, ray);
213
			T_ray_aabb = tim.get_secs();
214
 
215
			save_raw_float("aabb_ray.raw", grid);
216
		}
309 jab 217
	}
218
	cout.width(10);
219
	cout << "Poly";
220
	cout.width(11);
221
	cout <<"build_obb";
222
	cout.width(12);
223
	cout << "build_aabb";
224
	cout.width(10);
225
	cout << "dist_obb" ;
226
	cout.width(10);
227
	cout << "dist_aabb";
228
	cout.width(10);
229
	cout << "ray_obb" ;
230
	cout.width(10);
231
	cout << "ray_aabb";
232
	cout << endl;
341 jab 233
 
309 jab 234
	cout.precision(4);
235
	cout.width(10);
236
	cout << m.no_faces() << " ";
237
	cout.width(10);
238
	cout << T_build_obb;
239
	cout.width(12);
240
	cout << T_build_aabb;
241
	cout.width(10);
242
	cout << T_dist_obb;
243
	cout.width(10);
244
	cout << T_dist_aabb;
245
	cout.width(10);
246
	cout << T_ray_obb;
247
	cout.width(10);
248
	cout << T_ray_aabb;
249
	cout << endl;
250
}
251
 
252
 
253
 
254
// Brute force code - never use ...
255
//  	cout << "Computing distances" << endl;
256
// 	for(int i=0;i<triangle_vec_global.size(); ++i)
257
// 		{
258
// 			k=0;
259
// 			TRI = triangle_vec_global[i];
260
// 			for_each_voxel(grid, dist_brute);
261
// 		}
262
 
263