Subversion Repositories gelsvn

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
688 khor 1
#include <iostream>
2
 
3
#include <GEL/Util/Timer.h>
4
#include <GEL/Util/ArgExtracter.h>
5
 
6
#include <GEL/CGLA/Mat4x4d.h>
7
 
8
#include <GEL/Geometry/RGrid.h>
9
#include <GEL/Geometry/HGrid.h>
10
#include <GEL/Geometry/save_raw.h>
11
#include <GEL/Geometry/GridAlgorithm.h>
12
#include <GEL/Geometry/build_bbtree.h>
13
#include <GEL/Geometry/AABox.h>
14
 
15
#include <GEL/HMesh/triangulate.h>
16
#include <GEL/HMesh/Manifold.h>
17
 
18
#include <GEL/HMesh/load.h>
19
#include <GEL/HMesh/x3d_load.h>
20
#include <GEL/HMesh/x3d_save.h>
21
 
22
 
23
using namespace std;
24
using namespace HMesh;
25
using namespace Geometry;
26
using namespace CGLA;
27
using namespace Util;
28
 
29
namespace
30
{
31
 
32
	Vec3i vol_dim(64);
33
 
34
    const Mat4x4d fit_bounding_volume(const Vec3d& p0,
35
                                      const Vec3d& p7,
36
                                      float buf_reg)
37
	{
38
		Vec3d sz = p7 - p0;
39
		Vec3i dims = vol_dim;
40
		Vec3d scal_vec = (Vec3d(dims)-Vec3d(2*buf_reg+2))/sz;
41
		float scal = min(scal_vec[0], min(scal_vec[1],scal_vec[2]));
42
 
43
		Mat4x4d m = translation_Mat4x4d(Vec3d(0)+Vec3d(buf_reg+1));
44
		m *= scaling_Mat4x4d(Vec3d(scal));
45
		m *= translation_Mat4x4d(-p0);
46
		return m;
47
	}
48
 
49
	bool do_ray_tests = false;
50
	bool do_obb = true;
51
	bool do_aabb = false;
52
	bool flip_normals = false;
53
 
54
}
55
 
56
 
57
 
58
template<class BBTree>
59
class DistCompCache
60
{
61
	BBTree *T;
62
	float old_d;
63
	Vec3i old_p;
64
public:
65
 
66
	DistCompCache(BBTree* _T): T(_T), old_p(-99999) {}
67
 
68
	void operator()(const CGLA::Vec3i& pi, float& vox_val)
69
	{
70
		Vec3f p(pi);
71
 		if(sqr_length(pi-old_p)==1)
72
            vox_val = T->compute_signed_distance(p,CGLA::sqr(1.001+fabs(old_d)));
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
		Vec3d 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
    string fn = "../../data/bunny-little.x3d";
126
    if(argc>1)
127
	{
128
		ArgExtracter ae(argc, argv);
129
 
130
		do_aabb = ae.extract("-A");
131
		do_obb = ae.extract("-O");
132
		ae.extract("-x", vol_dim[0]);
133
		ae.extract("-y", vol_dim[1]);
134
		ae.extract("-z", vol_dim[2]);
135
		do_ray_tests = ae.extract("-R");
136
		flip_normals = ae.extract("-f");
137
		fn = ae.get_last_arg();
138
    }
139
    if(!load(fn, m))
140
    {
141
        cout << "Failed to load mesh" << endl;
142
        exit(1);
143
    }
144
    string file_prefix = fn.substr(0, fn.length()-4) + "-";
145
	cout << "Volume dimensions " << vol_dim << endl;
146
	if(!valid(m))
147
	{
148
		cout << "Not a valid manifold" << endl;
149
		exit(0);
150
	}
151
	triangulate_by_edge_face_split(m);
152
 
153
	Vec3d p0, p7;
154
	bbox(m, p0, p7);
155
 
156
  cout << p0 << endl;
157
  cout << p7 << endl;
158
 
159
	Mat4x4d T = fit_bounding_volume(p0,p7,10);
160
 
161
  cout << "Transformation " << T << endl;
162
 
163
	for(VertexIDIterator v = m.vertices_begin(); v != m.vertices_end(); ++v)
164
		m.pos(*v) = T.mul_3D_point(m.pos(*v));
165
 
166
 
167
 	RGridf grid(vol_dim,FLT_MAX);
168
	Util::Timer tim;
169
 
170
 
171
	float T_build_obb=0, T_build_aabb=0, T_dist_obb=0,
172
    T_dist_aabb=0, T_ray_obb=0, T_ray_aabb=0;
173
 
174
	if(do_obb)
175
	{
176
		cout << "Building OBB Tree" << endl;
177
		tim.start();
178
		OBBTree obb_tree;
179
		build_OBBTree(m, obb_tree);
180
		T_build_obb = tim.get_secs();
181
 
182
		cout << "Computing distances from OBB Tree" << endl;
183
		tim.start();
184
		DistCompCache<OBBTree> dist(&obb_tree);
185
		for_each_voxel(grid, dist);
186
		T_dist_obb = tim.get_secs();
187
 
188
		cout << "Saving distance field" << endl;
189
		save_raw_float(file_prefix+"obb_dist.raw", grid);
190
 
191
		if(do_ray_tests)
192
		{
193
			cout << "Ray tests on OBB Tree" << endl;
194
			tim.start();
195
			RayCast<OBBTree> ray(&obb_tree);
196
			for_each_voxel(grid, ray);
197
			T_ray_obb = tim.get_secs();
198
 
199
			cout << "Saving ray volume" << endl;
200
			save_raw_float(file_prefix+"obb_ray.raw", grid);
201
		}
202
	}
203
 
204
	if(do_aabb)
205
	{
206
		cout << "Building AABB Tree" << endl;
207
		tim.start();
208
		AABBTree aabb_tree;
209
		build_AABBTree(m, aabb_tree);
210
		T_build_aabb = tim.get_secs();
211
 
212
		cout << "Computing distances from AABB Tree" << endl;
213
		tim.start();
214
		DistCompCache<AABBTree> dist(&aabb_tree);
215
		for_each_voxel(grid, dist);
216
		T_dist_aabb = tim.get_secs();
217
 
218
		cout << "Saving distance field" << endl;
219
		save_raw_float(file_prefix+"aabb_dist.raw", grid);
220
 
221
		if(do_ray_tests)
222
		{
223
			cout << "Ray tests on AABB tree" << endl;
224
			tim.start();
225
			RayCast<AABBTree> ray(&aabb_tree);
226
			for_each_voxel(grid, ray);
227
			T_ray_aabb = tim.get_secs();
228
 
229
			cout << "Saving ray volume" << endl;
230
			save_raw_float(file_prefix+"aabb_ray.raw", grid);
231
		}
232
	}
233
	cout.width(10);
234
	cout << "Poly";
235
	cout.width(11);
236
	cout <<"build_obb";
237
	cout.width(12);
238
	cout << "build_aabb";
239
	cout.width(10);
240
	cout << "dist_obb" ;
241
	cout.width(10);
242
	cout << "dist_aabb";
243
	cout.width(10);
244
	cout << "ray_obb" ;
245
	cout.width(10);
246
	cout << "ray_aabb";
247
	cout << endl;
248
 
249
	cout.precision(4);
250
	cout.width(10);
251
	cout << m.no_faces() << " ";
252
	cout.width(10);
253
	cout << T_build_obb;
254
	cout.width(12);
255
	cout << T_build_aabb;
256
	cout.width(10);
257
	cout << T_dist_obb;
258
	cout.width(10);
259
	cout << T_dist_aabb;
260
	cout.width(10);
261
	cout << T_ray_obb;
262
	cout.width(10);
263
	cout << T_ray_aabb;
264
	cout << endl;
265
  system("PAUSE");
266
}