Subversion Repositories gelsvn

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
61 jab 1
/**********************************************************************
2
 
3
polygonizer.h
4
 
5
This is Jules Bloomenthal's implicit surface polygonizer from GRAPHICS 
6
GEMS IV. Bloomenthal's polygonizer is still used and the present code
7
is simply the original code morphed into C++.
8
 
9
J. Andreas Bærentzen 2003.
10
 
11
**********************************************************************/
12
 
385 jab 13
#include <string>
61 jab 14
#include <stdlib.h>
15
#include <math.h>
16
#include <iostream>
17
#include <vector>
18
#include <list>
19
#include <sys/types.h>
417 jrf 20
#include "CGLA/CGLA.h"
61 jab 21
#include "Polygonizer.h"
22
 
23
using namespace std;
417 jrf 24
using namespace CGLA;
61 jab 25
 
64 jab 26
namespace Geometry
61 jab 27
{
28
 
29
	namespace
30
	{
31
		const int RES =	10; /* # converge iterations    */
32
 
33
		const int L =	0;  /* left direction:	-x, -i */
34
		const int R =	1;  /* right direction:	+x, +i */
35
		const int B =	2;  /* bottom direction: -y, -j */
36
		const int T =	3;  /* top direction:	+y, +j */
37
		const int N =	4;  /* near direction:	-z, -k */
38
		const int F =	5;  /* far direction:	+z, +k */
39
		const int LBN =	0;  /* left bottom near corner  */
40
		const int LBF =	1;  /* left bottom far corner   */
41
		const int LTN =	2;  /* left top near corner     */
42
		const int LTF =	3;  /* left top far corner      */
43
		const int RBN =	4;  /* right bottom near corner */
44
		const int RBF =	5;  /* right bottom far corner  */
45
		const int RTN =	6;  /* right top near corner    */
46
		const int RTF =	7;  /* right top far corner     */
47
 
48
 
49
		/* the LBN corner of cube (i, j, k), corresponds with location
50
		 * (start.x+(i-.5)*size, start.y+(j-.5)*size, start.z+(k-.5)*size) */
51
 
52
		inline float RAND() 
53
		{
417 jrf 54
			return (gel_rand()&GEL_RAND_MAX)/static_cast<float>(GEL_RAND_MAX);
61 jab 55
		}
56
 
57
		const int HASHBIT = 5;
58
 
59
		const int HASHSIZE = (size_t)(1<<(3*HASHBIT));   
60
 
61
		const int MASK = ((1<<HASHBIT)-1);
62
 
63
		inline int HASH( int i, int j,int k) 
64
		{ 
65
			return (((((i&MASK)<<HASHBIT)|j&MASK)<<HASHBIT)|k&MASK);
66
		} 
67
 
68
		inline int BIT(int i, int bit) 
69
		{ 
70
			return (i>>bit)&1; 
71
		}
72
 
73
		// flip the given bit of i 
74
		inline int FLIP(int i, int bit) 
75
		{
76
			return i^1<<bit;
77
		}
78
 
79
		struct TEST {		   /* test the function for a signed value */
80
			POINT p;			   /* location of test */
81
			float value;		   /* function value at p */
82
			int ok;			   /* if value is of correct sign */
83
		};
84
 
85
		struct CORNER {		   /* corner of a cube */
86
			int i, j, k;		   /* (i, j, k) is index within lattice */
87
			float x, y, z, value;	   /* location and function value */
88
		};
89
 
90
		struct CUBE {		   /* partitioning cell (cube) */
91
			int i, j, k;		   /* lattice location of cube */
92
			CORNER *corners[8];		   /* eight corners */
93
		};
94
 
95
		struct CENTERELEMENT {	   /* list of cube locations */
96
			int i, j, k;		   /* cube location */
97
			CENTERELEMENT(int _i, int _j, int _k): i(_i), j(_j), k(_k) {}
98
		};
99
		typedef list<CENTERELEMENT> CENTERLIST;
100
 
101
		struct CORNERELEMENT {	   /* list of corners */
102
			int i, j, k;		   /* corner id */
103
			float value;		   /* corner value */
104
			CORNERELEMENT(int _i, int _j, int _k, float _value):
105
				i(_i), j(_j), k(_k), value(_value) {}
106
		};
107
		typedef list<CORNERELEMENT> CORNERLIST;
108
 
109
		struct EDGEELEMENT {	   /* list of edges */
110
			int i1, j1, k1, i2, j2, k2;	   /* edge corner ids */
111
			int vid;			   /* vertex id */
112
		};
113
 
114
		typedef list<EDGEELEMENT> EDGELIST;
115
		typedef list<int> INTLIST;
116
		typedef list<INTLIST> INTLISTS;
117
 
118
 
119
		//----------------------------------------------------------------------
120
		// Implicit surface evaluation functions
121
		//----------------------------------------------------------------------
122
 
123
		/* converge: from two points of differing sign, converge to zero crossing */
124
 
125
		void converge (POINT* p1, POINT* p2, float v, 
126
									 ImplicitFunction* function, POINT* p)
127
		{
128
			int i = 0;
129
			POINT pos, neg;
130
			if (v < 0) {
131
				pos.x = p2->x; pos.y = p2->y; pos.z = p2->z;
132
				neg.x = p1->x; neg.y = p1->y; neg.z = p1->z;
133
			}
134
			else {
135
				pos.x = p1->x; pos.y = p1->y; pos.z = p1->z;
136
				neg.x = p2->x; neg.y = p2->y; neg.z = p2->z;
137
			}
138
			while (1) {
102 bj 139
				p->x = 0.5f*(pos.x + neg.x);
140
				p->y = 0.5f*(pos.y + neg.y);
141
				p->z = 0.5f*(pos.z + neg.z);
61 jab 142
				if (i++ == RES) return;
143
				if ((function->eval(p->x, p->y, p->z)) > 0.0)
144
					{pos.x = p->x; pos.y = p->y; pos.z = p->z;}
145
				else {neg.x = p->x; neg.y = p->y; neg.z = p->z;}
146
			}
147
		}
148
 
149
 
150
		/* vnormal: compute unit length surface normal at point */
151
 
152
		inline void vnormal (ImplicitFunction* function, POINT* point, POINT* n, float delta)
153
		{
154
			float f = function->eval(point->x, point->y, point->z);
155
			n->x = function->eval(point->x+delta, point->y, point->z)-f;
156
			n->y = function->eval(point->x, point->y+delta, point->z)-f;
157
			n->z = function->eval(point->x, point->y, point->z+delta)-f;
158
			f = sqrt(n->x*n->x + n->y*n->y + n->z*n->z);
159
			if (f != 0.0) {n->x /= f; n->y /= f; n->z /= f;}
160
		}
161
 
162
 
163
 
164
		// ----------------------------------------------------------------------
165
 
166
		class EDGETABLE
167
		{
168
			vector<EDGELIST> table;		   /* edge and vertex id hash table */
169
 
170
		public:
171
 
172
			EDGETABLE(): table(2*HASHSIZE) {}
173
 
174
			void setedge (int i1, int j1, int k1, 
175
										int i2, int j2, int k2, int vid);
176
			int getedge (int i1, int j1, int k1, 
177
									 int i2, int j2, int k2);
178
 
179
 
180
		};
181
 
182
		/* setedge: set vertex id for edge */
183
		void EDGETABLE::setedge (int i1, int j1, int k1, 
184
														 int i2, int j2, int k2, int vid)
185
		{
186
			unsigned int index;
187
 
188
			if (i1>i2 || (i1==i2 && (j1>j2 || (j1==j2 && k1>k2)))) {
189
				int t=i1; i1=i2; i2=t; t=j1; j1=j2; j2=t; t=k1; k1=k2; k2=t;
190
			}
191
			index = HASH(i1, j1, k1) + HASH(i2, j2, k2);
192
 
193
			EDGEELEMENT new_obj;
194
			new_obj.i1 = i1; 
195
			new_obj.j1 = j1; 
196
			new_obj.k1 = k1;
197
			new_obj.i2 = i2; 
198
			new_obj.j2 = j2; 
199
			new_obj.k2 = k2;
200
			new_obj.vid = vid;
201
			table[index].push_front(new_obj);
202
		}
203
 
204
 
205
		/* getedge: return vertex id for edge; return -1 if not set */
206
 
207
		int EDGETABLE::getedge (int i1, int j1, int k1, int i2, int j2, int k2)
208
		{
209
 
210
			if (i1>i2 || (i1==i2 && (j1>j2 || (j1==j2 && k1>k2)))) {
211
				int t=i1; i1=i2; i2=t; t=j1; j1=j2; j2=t; t=k1; k1=k2; k2=t;
212
			};
213
			int hashval = HASH(i1, j1, k1)+HASH(i2, j2, k2);
214
			EDGELIST::const_iterator q = table[hashval].begin();
215
			for (; q != table[hashval].end(); ++q)
216
				if (q->i1 == i1 && q->j1 == j1 && q->k1 == k1 &&
217
						q->i2 == i2 && q->j2 == j2 && q->k2 == k2)
218
					return q->vid;
219
			return -1;
220
		}
221
 
222
 
223
		// ----------------------------------------------------------------------
224
		class PROCESS
225
		{	   /* parameters, function, storage */
226
 
227
			std::vector<NORMAL>* gnormals;  
228
			std::vector<VERTEX>* gvertices;  
229
			std::vector<TRIANGLE> *gtriangles;
230
 
231
			ImplicitFunction* function;	   /* implicit surface function */
232
 
233
			float size, delta;		   /* cube size, normal delta */
234
			int bounds;			   /* cube range within lattice */
235
			POINT start;		   /* start point on surface */
236
			bool use_normals;
237
 
238
			// Global list of corners (keeps track of memory)
239
			list<CORNER> corner_lst;
240
			list<CUBE> cubes;		   /* active cubes */
241
			vector<CENTERLIST> centers;	   /* cube center hash table */
242
			vector<CORNERLIST> corners;	   /* corner value hash table */
243
			EDGETABLE edges;
244
 
245
			CORNER *setcorner (int i, int j, int k);
246
 
247
			void testface (int i, int j, int k, CUBE* old, 
248
										 int face, int c1, int c2, int c3, int c4); 
249
 
250
			TEST find (int sign, float x, float y, float z);
251
 
252
			int vertid (CORNER* c1, CORNER* c2);
253
 
254
			int dotet (CUBE* cube, int c1, int c2, int c3, int c4);
255
 
256
			int docube (CUBE* cube);
257
 
258
			int triangle (int i1, int i2, int i3)
259
			{
260
				TRIANGLE t;
261
				t.v0 = i1;
262
				t.v1 = i2;
263
				t.v2 = i3;
264
				(*gtriangles).push_back(t);
265
				return 1;
266
			}
267
 
268
		public:
269
			PROCESS(ImplicitFunction* _function,
270
							float _size, float _delta, 
271
							int _bounds,
272
							vector<VERTEX>& _gvertices,
273
							vector<NORMAL>& _gnormals,
274
							vector<TRIANGLE>& _gtriangles,
275
							bool _compute_normals=false);
276
 
277
 
278
			~PROCESS() {}
279
 
280
			void march(int mode, float x, float y, float z);
281
 
282
		};
283
 
284
 
285
		/* setcenter: set (i,j,k) entry of table[]
286
			 return 1 if already set; otherwise, set and return 0 */
287
		int setcenter(vector<CENTERLIST>& table,
288
									int i, int j, int k)
289
		{
290
			int index = HASH(i,j,k);
291
			CENTERLIST::const_iterator q = table[index].begin();
292
			for(; q != table[index].end(); ++q)
293
				if(q->i == i && q->j==j && q->k == k) return 1;
294
 
295
			CENTERELEMENT elem(i,j,k);
296
			table[index].push_front(elem);
297
			return 0;
298
		}
299
 
300
		/* setcorner: return corner with the given lattice location
301
			 set (and cache) its function value */
302
		CORNER* PROCESS::setcorner (int i, int j, int k)
303
		{
304
			/* for speed, do corner value caching here */
305
			corner_lst.push_back(CORNER());
306
			CORNER *c = &corner_lst.back();
307
			int index = HASH(i, j, k);
308
 
102 bj 309
			c->i = i; c->x = start.x+((float)i-.5f)*size;
310
			c->j = j; c->y = start.y+((float)j-.5f)*size;
311
			c->k = k; c->z = start.z+((float)k-.5f)*size;
61 jab 312
 
313
			CORNERLIST::const_iterator l = corners[index].begin();
314
			for (; l != corners[index].end(); ++l)
315
				if (l->i == i && l->j == j && l->k == k) {
316
					c->value = l->value;
317
					return c;
318
				}
319
 
320
			c->value = function->eval(c->x, c->y, c->z);
321
			CORNERELEMENT elem(i,j,k,c->value);
322
			corners[index].push_front(elem);
323
			return c;
324
		}
325
 
326
 
327
 
328
		/* testface: given cube at lattice (i, j, k), and four corners of face,
329
		 * if surface crosses face, compute other four corners of adjacent cube
330
		 * and add new cube to cube stack */
331
 
332
		inline void PROCESS::testface (int i, int j, int k, CUBE* old, 
333
														int face, int c1, int c2, int c3, int c4) 
334
		{
335
			CUBE new_obj;
336
 
337
			static int facebit[6] = {2, 2, 1, 1, 0, 0};
338
			int n, pos = old->corners[c1]->value > 0.0 ? 1 : 0, bit = facebit[face];
339
 
340
			/* test if no surface crossing, cube out of bounds, or already visited: */
341
			if ((old->corners[c2]->value > 0) == pos &&
342
					(old->corners[c3]->value > 0) == pos &&
343
					(old->corners[c4]->value > 0) == pos) return;
344
			if (abs(i) > bounds || abs(j) > bounds || abs(k) > bounds) return;
345
			if (setcenter(centers, i, j, k)) return;
346
 
347
			/* create new_obj cube: */
348
			new_obj.i = i;
349
			new_obj.j = j;
350
			new_obj.k = k;
351
			for (n = 0; n < 8; n++) new_obj.corners[n] = 0;
352
			new_obj.corners[FLIP(c1, bit)] = old->corners[c1];
353
			new_obj.corners[FLIP(c2, bit)] = old->corners[c2];
354
			new_obj.corners[FLIP(c3, bit)] = old->corners[c3];
355
			new_obj.corners[FLIP(c4, bit)] = old->corners[c4];
356
			for (n = 0; n < 8; n++)
357
				if (new_obj.corners[n] == 0)
358
					new_obj.corners[n] = setcorner(i+BIT(n,2), j+BIT(n,1), k+BIT(n,0));
359
 
360
			// Add new cube to top of stack
361
			cubes.push_front(new_obj);
362
		}
363
 
364
		/* find: search for point with value of given sign (0: neg, 1: pos) */
365
 
366
		TEST PROCESS::find (int sign, float x, float y, float z)
367
		{
368
			int i;
369
			TEST test;
370
			float range = size;
371
			test.ok = 1;
372
			for (i = 0; i < 10000; i++) {
102 bj 373
				test.p.x = x+range*(RAND()-0.5f);
374
				test.p.y = y+range*(RAND()-0.5f);
375
				test.p.z = z+range*(RAND()-0.5f);
61 jab 376
				test.value = function->eval(test.p.x, test.p.y, test.p.z);
377
				if (sign == (test.value > 0.0)) return test;
102 bj 378
				range = range*1.0005f; /* slowly expand search outwards */
61 jab 379
			}
380
			test.ok = 0;
381
			return test;
382
		}
383
 
384
 
385
		/* vertid: return index for vertex on edge:
386
		 * c1->value and c2->value are presumed of different sign
387
		 * return saved index if any; else compute vertex and save */
388
 
389
		int PROCESS::vertid (CORNER* c1, CORNER* c2)
390
		{
391
			VERTEX v;
392
			NORMAL n;
393
			POINT a, b;
394
			int vid = edges.getedge(c1->i, c1->j, c1->k, c2->i, c2->j, c2->k);
395
			if (vid != -1) return vid;			     /* previously computed */
396
			a.x = c1->x; a.y = c1->y; a.z = c1->z;
397
			b.x = c2->x; b.y = c2->y; b.z = c2->z;
398
			converge(&a, &b, c1->value, function, &v); /* position */
399
			(*gvertices).push_back(v);			   /* save vertex */
400
			if(use_normals)
401
				{
402
					vnormal(function, &v, &n, delta);			   /* normal */
403
					(*gnormals).push_back(n);			   /* save vertex */
404
				}
405
			vid = gvertices->size()-1;
406
			edges.setedge(c1->i, c1->j, c1->k, c2->i, c2->j, c2->k, vid);
407
			return vid;
408
		}
409
 
410
 
411
 
412
 
413
		/**** Tetrahedral Polygonization ****/
414
 
415
 
416
		/* dotet: triangulate the tetrahedron
417
		 * b, c, d should appear clockwise when viewed from a
418
		 * return 0 if client aborts, 1 otherwise */
419
 
420
		int PROCESS::dotet (CUBE* cube, int c1, int c2, int c3, int c4)
421
		{
422
			CORNER *a = cube->corners[c1];
423
			CORNER *b = cube->corners[c2];
424
			CORNER *c = cube->corners[c3];
425
			CORNER *d = cube->corners[c4];
426
			int index = 0, apos, bpos, cpos, dpos, e1, e2, e3, e4, e5, e6;
136 jab 427
			if ((apos = (a->value > 0.0))) index += 8;
428
			if ((bpos = (b->value > 0.0))) index += 4;
429
			if ((cpos = (c->value > 0.0))) index += 2;
430
			if ((dpos = (d->value > 0.0))) index += 1;
61 jab 431
			/* index is now 4-bit number representing one of the 16 possible cases */
432
			if (apos != bpos) e1 = vertid(a, b);
433
			if (apos != cpos) e2 = vertid(a, c);
434
			if (apos != dpos) e3 = vertid(a, d);
435
			if (bpos != cpos) e4 = vertid(b, c);
436
			if (bpos != dpos) e5 = vertid(b, d);
437
			if (cpos != dpos) e6 = vertid(c, d);
438
			/* 14 productive tetrahedral cases (0000 and 1111 do not yield polygons */
439
			switch (index) {
440
			case 1:	 return triangle(e5, e6, e3);
441
			case 2:	 return triangle(e2, e6, e4);
442
			case 3:	 return triangle(e3, e5, e4) &&
443
								 triangle(e3, e4, e2);
444
			case 4:	 return triangle(e1, e4, e5);
445
			case 5:	 return triangle(e3, e1, e4) &&
446
								 triangle(e3, e4, e6);
447
			case 6:	 return triangle(e1, e2, e6) &&
448
								 triangle(e1, e6, e5);
449
			case 7:	 return triangle(e1, e2, e3);
450
			case 8:	 return triangle(e1, e3, e2);
451
			case 9:	 return triangle(e1, e5, e6) &&
452
								 triangle(e1, e6, e2);
453
			case 10: return triangle(e1, e3, e6) &&
454
								 triangle(e1, e6, e4);
455
			case 11: return triangle(e1, e5, e4);
456
			case 12: return triangle(e3, e2, e4) &&
457
								 triangle(e3, e4, e5);
458
			case 13: return triangle(e6, e2, e4);
459
			case 14: return triangle(e5, e3, e6);
460
			}
461
			return 1;
462
		}
463
 
464
 
465
		/**** Cubical Polygonization (optional) ****/
466
 
467
 
468
		const int LB =	0;  /* left bottom edge	*/
469
		const int LT =	1;  /* left top edge	*/
470
		const int LN =	2;  /* left near edge	*/
471
		const int LF =	3;  /* left far edge	*/
472
		const int RB =	4;  /* right bottom edge */
473
		const int RT =	5;  /* right top edge	*/
474
		const int RN =	6;  /* right near edge	*/
475
		const int RF =	7;  /* right far edge	*/
476
		const int BN =	8;  /* bottom near edge	*/
477
		const int BF =	9;  /* bottom far edge	*/
478
		const int TN =	10; /* top near edge	*/
479
		const int TF =	11; /* top far edge	*/
480
 
481
 
482
		class CUBETABLE
483
		{
484
			vector<INTLISTS> ctable;
485
			int nextcwedge (int edge, int face);
486
			int otherface (int edge, int face);
487
 
488
		public:
489
 
490
			CUBETABLE();
491
 
492
			const INTLISTS& get_lists(int i) const
493
			{
494
				return ctable[i];
495
			}
496
		};
497
 
498
		/*			edge: LB, LT, LN, LF, RB, RT, RN, RF, BN, BF, TN, TF */
499
		const int corner1[12]	   = {LBN,LTN,LBN,LBF,RBN,RTN,RBN,RBF,LBN,LBF,LTN,LTF};
500
		const int corner2[12]	   = {LBF,LTF,LTN,LTF,RBF,RTF,RTN,RTF,RBN,RBF,RTN,RTF};
501
		const int leftface[12]	   = {B,  L,  L,  F,  R,  T,  N,  R,  N,  B,  T,  F};
502
		/* face on left when going corner1 to corner2 */
503
		const int rightface[12]   = {L,  T,  N,  L,  B,  R,  R,  F,  B,  F,  N,  T};
504
		/* face on right when going corner1 to corner2 */
505
 
506
 
507
		/* nextcwedge: return next clockwise edge from given edge around given face */
508
 
509
		inline int CUBETABLE::nextcwedge (int edge, int face)
510
		{
511
			switch (edge) {
512
			case LB: return (face == L)? LF : BN;
513
			case LT: return (face == L)? LN : TF;
514
			case LN: return (face == L)? LB : TN;
515
			case LF: return (face == L)? LT : BF;
516
			case RB: return (face == R)? RN : BF;
517
			case RT: return (face == R)? RF : TN;
518
			case RN: return (face == R)? RT : BN;
519
			case RF: return (face == R)? RB : TF;
520
			case BN: return (face == B)? RB : LN;
521
			case BF: return (face == B)? LB : RF;
522
			case TN: return (face == T)? LT : RN;
523
			case TF: return (face == T)? RT : LF;
524
			}
102 bj 525
			return -1;
61 jab 526
		}
527
 
528
		/* otherface: return face adjoining edge that is not the given face */
529
 
530
		inline int CUBETABLE::otherface (int edge, int face)
531
		{
532
			int other = leftface[edge];
533
			return face == other? rightface[edge] : other;
534
		}
535
 
536
 
537
		CUBETABLE::CUBETABLE(): ctable(256)
538
		{
539
			int i, e, c, done[12], pos[8];
540
			for (i = 0; i < 256; i++) 
541
				{
542
					for (e = 0; e < 12; e++) 
543
						done[e] = 0;
544
					for (c = 0; c < 8; c++) 
545
						pos[c] = BIT(i, c);
546
					for (e = 0; e < 12; e++)
547
						if (!done[e] && (pos[corner1[e]] != pos[corner2[e]])) 
548
							{
549
								INTLIST ints;
550
								int start = e, edge = e;
551
 
552
								/* get face that is to right of edge from pos to neg corner: */
553
								int face = pos[corner1[e]]? rightface[e] : leftface[e];
554
								while (1) 
555
									{
556
										edge = nextcwedge(edge, face);
557
										done[edge] = 1;
558
										if (pos[corner1[edge]] != pos[corner2[edge]]) 
559
											{
560
												ints.push_front(edge);
561
												if (edge == start) 
562
													break;
563
												face = otherface(edge, face);
564
											}
565
									}
566
								ctable[i].push_front(ints);
567
							}
568
				}
569
		}
570
 
571
		inline const INTLISTS& get_cubetable_entry(int i) 
572
		{
573
			static CUBETABLE c;
574
			return c.get_lists(i);
575
		}
576
 
577
 
578
		/* docube: triangulate the cube directly, without decomposition */
579
 
580
		int PROCESS::docube (CUBE* cube)
581
		{
582
			int index = 0;
583
			for (int i = 0; i < 8; i++) 
584
				if (cube->corners[i]->value > 0.0) 
585
					index += (1<<i);
586
 
587
			INTLISTS intlists = get_cubetable_entry(index);
588
			INTLISTS::const_iterator polys = intlists.begin();
589
			for (; polys != intlists.end(); ++polys) 
590
				{
591
					INTLIST::const_iterator edges = polys->begin();
592
					int a = -1, b = -1, count = 0;
593
					for (; edges != polys->end(); ++edges) 
594
						{
595
							CORNER *c1 = cube->corners[corner1[(*edges)]];
596
							CORNER *c2 = cube->corners[corner2[(*edges)]];
597
							int c = vertid(c1, c2);
598
							if (++count > 2 && ! triangle(a, b, c)) 
599
								return 0;
600
							if (count < 3) 
601
								a = b;
602
							b = c;
603
						}
604
				}
605
			return 1;
606
		}
607
 
608
 
609
 
610
 
611
 
612
 
613
 
614
		/**** An Implicit Surface Polygonizer ****/
615
 
616
 
617
		/* polygonize: polygonize the implicit surface function
618
		 *   arguments are:
619
		 *	 ImplicitFunction
620
		 *	     the implicit surface function
621
		 *	     return negative for inside, positive for outside
622
		 *	 float size
623
		 *	     width of the partitioning cube
624
		 *   float delta 
625
		 *       a small step - used for gradient computation
626
		 *	 int bounds
627
		 *	     max. range of cubes (+/- on the three axes) from first cube
628
		 *   _gvertices, _gnormals, _gtriangles
629
		 *       the data structures into which information is put.
630
		 */
631
 
632
		PROCESS::PROCESS(ImplicitFunction* _function,
633
										 float _size, float _delta, 
634
										 int _bounds, 
635
										 vector<VERTEX>& _gvertices,
636
										 vector<NORMAL>& _gnormals,
637
										 vector<TRIANGLE>& _gtriangles,
638
										 bool _use_normals):
639
			function(_function), size(_size), delta(_delta), bounds(_bounds),
640
			centers(HASHSIZE), corners(HASHSIZE), 
641
			gvertices(&_gvertices),
642
			gnormals(&_gnormals),
643
			gtriangles(&_gtriangles),
644
			use_normals(_use_normals)
645
		{}
646
 
647
		void PROCESS::march(int mode, float x, float y, float z)
648
		{
649
			int noabort;
650
			TEST in, out;
651
 
652
			/* find point on surface, beginning search at (x, y, z): */
417 jrf 653
			gel_srand(1);
61 jab 654
			in = find(1, x, y, z);
655
			out = find(0, x, y, z);
656
			if (!in.ok || !out.ok) 
385 jab 657
            {
658
                exit(1);
659
            }
61 jab 660
			converge(&in.p, &out.p, in.value, function, &start);
661
 
662
			/* push initial cube on stack: */
663
			CUBE cube;
664
			cube.i = cube.j = cube.k = 0;
665
			cubes.push_front(cube);
666
 
667
			/* set corners of initial cube: */
668
			for (int n = 0; n < 8; n++)
669
				cubes.front().corners[n] = setcorner(BIT(n,2), BIT(n,1), BIT(n,0));
670
 
671
			setcenter(centers, 0, 0, 0);
672
 
673
			while (cubes.size() != 0) 
674
				{
675
					/* process active cubes till none left */
676
					CUBE c = cubes.front();
677
 
678
					noabort = mode == TET?
679
						/* either decompose into tetrahedra and polygonize: */
680
						dotet(&c, LBN, LTN, RBN, LBF) &&
681
						dotet(&c, RTN, LTN, LBF, RBN) &&
682
						dotet(&c, RTN, LTN, LTF, LBF) &&
683
						dotet(&c, RTN, RBN, LBF, RBF) &&
684
						dotet(&c, RTN, LBF, LTF, RBF) &&
685
						dotet(&c, RTN, LTF, RTF, RBF)
686
						:
687
						/* or polygonize the cube directly: */
688
						docube(&c);
385 jab 689
                        if (! noabort) {
690
                           exit(1);
691
                        }
61 jab 692
 
693
					/* pop current cube from stack */
694
					cubes.pop_front();
695
 
696
					/* test six face directions, maybe add to stack: */
697
					testface(c.i-1, c.j, c.k, &c, L, LBN, LBF, LTN, LTF);
698
					testface(c.i+1, c.j, c.k, &c, R, RBN, RBF, RTN, RTF);
699
					testface(c.i, c.j-1, c.k, &c, B, LBN, LBF, RBN, RBF);
700
					testface(c.i, c.j+1, c.k, &c, T, LTN, LTF, RTN, RTF);
701
					testface(c.i, c.j, c.k-1, &c, N, LBN, LTN, RBN, RTN);
702
					testface(c.i, c.j, c.k+1, &c, F, LBF, LTF, RBF, RTF);
703
				}
704
		}
705
	}
706
 
707
	void Polygonizer::march(float x, float y, float z)
708
		{
709
			gvertices.clear();
710
			gnormals.clear();
711
			gtriangles.clear();
712
			PROCESS p(func, size, size/(float)(RES*RES), bounds, 
713
								gvertices, gnormals, gtriangles, use_normals);
714
			p.march(use_tetra?TET:NOTET,x,y,z);
715
		}
716
 
717
}