Subversion Repositories gelsvn

Rev

Rev 385 | Rev 595 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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