Subversion Repositories gelsvn

Rev

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