-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathImplicitCurve.cpp
338 lines (321 loc) · 12.5 KB
/
ImplicitCurve.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include "ImplicitCurve.h"
#include <limits>
namespace nV {
namespace Graphics {
ImplicitCurve::ImplicitCurve(F2P* f2p, double xmin, double xmax, double ymin, double ymax) {
int n;
TEST in, out;
f = f2p;
spx = xmax - xmin;
spy = ymax - ymin;
m_start.x = (xmax + xmin) / 2;
m_start.y = (ymax + ymin) / 2;
sizex = spx / CUBE_NUM_CURVE;
sizey = spy / CUBE_NUM_CURVE;
m_bounds = (CUBE_NUM_CURVE - 1) / 2;
/* allocate hash tables and build cube polygon table: */
m_centers = new CENTERLIST*[HASHSIZE*2];
for (int i = 0; i < HASHSIZE*2; i++) {
m_centers[i] = 0;
}
m_corners = new CORNERLIST*[HASHSIZE*2];
for (int i = 0; i < HASHSIZE*2; i++) {
m_corners[i] = 0;
}
m_edges = new EDGELIST*[HASHSIZE*2];
for (int i = 0; i < HASHSIZE*2; i++) {
m_edges[i] = 0;
}
m_cubes = new CUBES; /* list of 1 */
m_cubes->cube.i = m_cubes->cube.j = 0;
m_cubes->next = NULL;
/* set corners of initial cube: */
for (n = 0; n < 4; n++)
m_cubes->cube.corners[n] = setcorner(BIT(n, 1), BIT(n, 0));
setcenter(m_centers, 0, 0);
while (m_cubes != NULL) { /* process active cubes till none left */
CUBE c;
CUBES *temp = m_cubes;
c = m_cubes->cube;
//
dotriangle(&c, 0, 1, 2);
dotriangle(&c, 1, 2, 3);
/* pop current cube from stack */
m_cubes = m_cubes->next;
delete[] temp;
/* test six face directions, maybe add to stack: */
//
testface(c.i - 1, c.j, &c, _L, LB, LT);
testface(c.i + 1, c.j, &c, _R, RB_CURVE, RT_CURVE);
testface(c.i, c.j - 1, &c, _B, LB, RB_CURVE);
testface(c.i, c.j + 1, &c, _T, LT, RT_CURVE);
}
}
ImplicitCurve::~ImplicitCurve() {
free_all();
for (unsigned int i = 0; i < m_vertices.size(); i++) {
delete m_vertices[i];
}
m_vertices.clear();
m_line.clear();
}
/* free_all: free allthe memory we¡¯ve allocated (except cubetable) */
void ImplicitCurve::free_all () {
int index;
CORNERLIST *l, *lnext;
CENTERLIST *cl, *clnext;
EDGELIST *edge, *edgenext;
for (index = 0; index < HASHSIZE*2; index++) {
for (l = m_corners[index]; l; l = lnext) {
lnext = l->next;
delete[] l; /* free CORNERLIST */
}
for (cl = m_centers[index]; cl; cl = clnext) {
clnext = cl->next;
delete[] cl; /* free CENTERLIST */
}
for (edge = m_edges[index]; edge; edge = edgenext) {
edgenext = edge->next;
delete[] edge; /* free EDGELIST */
}
}
delete[] m_edges; /* free array of EDGELIST pointers */
delete[] m_corners; /* free array of CORNERLIST pointers */
delete[] m_centers; /* free array of CENTERLIST pointers */
}
/* testface: given cube at lattice (i, j, k), and four corners of face,
* if surface crosses face, compute other four corners of adjacent cube
* and add new cube to cube stack */
void ImplicitCurve::testface (int i, int j, CUBE *old, int face, int c1, int c2) {
CUBE newc;
CUBES *oldcubes = m_cubes;
static int facebit[4] = {1, 1, 0, 0};
int n, bit = facebit[face];
if (std::abs(i) > m_bounds || std::abs(j) > m_bounds)
return;
if (setcenter(m_centers, i, j))
return;
/* create new cube: */
newc.i = i;
newc.j = j;
for (n = 0; n < 4; n++) newc.corners[n] = NULL;
newc.corners[FLIP(c1, bit)] = old->corners[c1];
newc.corners[FLIP(c2, bit)] = old->corners[c2];
for (n = 0; n < 4; n++)
if (newc.corners[n] == NULL)
newc.corners[n] = setcorner(i + BIT(n, 1), j + BIT(n, 0));
/*add cube to top of stack: */
m_cubes = new CUBES;
m_cubes->cube = newc;
m_cubes->next = oldcubes;
}
/* setcorner: return corner with the given lattice location set (and cache) its function value */
ImplicitCurve::CORNER* ImplicitCurve::setcorner (int i, int j) {
/* for speed, do corner value caching here */
CORNER *c = new CORNER;
int index = HASH(i, j, 0);
CORNERLIST *l = m_corners[index];
c->i = i;
c->x = m_start.x + ((double)i - .5) * sizex;
c->j = j;
c->y = m_start.y + ((double)j - .5) * sizey;
for (; l != NULL; l = l->next) {
if (l->i == i && l->j == j) {
c->value = l->value;
return c;
}
}
l = new CORNERLIST;
l->i = i;
l->j = j;
l->value = c->value = getFunctionValueSafely(f, c->x, c->y);
l->next = m_corners[index];
m_corners[index] = l;
return c;
}
/* setcenter: set (i,j,k) entry of table[]
* return 1 if already set; otherwise, set and return 0 */
int ImplicitCurve::setcenter(CENTERLIST *table[], int i, int j) {
int index = HASH(i, j, 0);
CENTERLIST *newc, *l, *q = table[index];
for (l = q; l != NULL; l = l->next)
if (l->i == i && l->j == j)
return 1;
newc = new CENTERLIST;
newc->i = i;
newc->j = j;
newc->next = q;
table[index] = newc;
return 0;
}
/* find: search for point with value of given sign (0: neg, 1: pos) */
ImplicitCurve::TEST ImplicitCurve::find (int sign, double x, double y) {
int i;
TEST test;
double drand48();
double rangex = sizex;
double rangey = sizey;
test.ok = 1;
for (i = 0; i < 10000; i++) {
test.p.x = x + rangex * (RAND() - 0.5);
test.p.y = y + rangey * (RAND() - 0.5);
test.value = getFunctionValueSafely(f, test.p.x, test.p.y);
if (sign == (test.value > 0.0))
return test;
rangex *= 1.05; /* slowly expand search outwards */
rangey *= 1.05;
}
test.ok = 0;
return test;
}
/**** Tetrahedral Polygonization ****/
/* dotet: triangulate the tetrahedron
* b, c, d should appear clockwise when viewed from a
* return 0 if client aborts, 1 otherwise */
void ImplicitCurve::dotriangle (CUBE *cube, int c1, int c2, int c3) {
CORNER *a = cube->corners[c1];
CORNER *b = cube->corners[c2];
CORNER *c = cube->corners[c3];
int index = 0, apos, bpos, cpos, e1, e2, e3;
if (apos = (a->value > 0.0)) index += 4;
if (bpos = (b->value > 0.0)) index += 2;
if (cpos = (c->value > 0.0)) index += 1;
/* index is now 4-bit number representing one of the 16 possible cases */
if (apos != bpos) e1 = vertid(a, b);
if (apos != cpos) e2 = vertid(a, c);
if (bpos != cpos) e3 = vertid(b, c);
if (index > 3) index ^= 0x7;
switch (index) {
case 1:
line(e2, e3);
break;
case 2:
line(e1, e3);
break;
case 3:
line(e1, e2);
break;
}
}
/* setedge: set vertex id for edge */
void ImplicitCurve::setedge (EDGELIST *table[], int i1, int j1, int i2, int j2, int vid) {
unsigned int index;
EDGELIST *newe;
if (i1 > i2 || (i1 == i2 && j1 > j2)) {
int t = i1;
i1 = i2;
i2 = t;
t = j1;
j1 = j2;
j2 = t;
}
index = HASH(i1, j1, 0) + HASH(i2, j2, 0);
newe = new EDGELIST;
newe->i1 = i1;
newe->j1 = j1;
newe->i2 = i2;
newe->j2 = j2;
newe->vid = vid;
newe->next = table[index];
table[index] = newe;
}
/* getedge: return vertex id for edge; return -1 if not set */
int ImplicitCurve::getedge (EDGELIST *table[], int i1, int j1, int i2, int j2) {
//
EDGELIST *q;
if (i1 > i2 || (i1 == i2 && j1 > j2)) {
int t = i1;
i1 = i2;
i2 = t;
t = j1;
j1 = j2;
j2 = t;
}
q = table[HASH(i1, j1, 0)+HASH(i2, j2, 0)];
for (; q != NULL; q = q->next) {
if (q->i1 == i1 && q->j1 == j1 &&
q->i2 == i2 && q->j2 == j2)
return q->vid;
}
return -1;
}
/**** Vertices ****/
/* vertid: return index for vertex on edge:
* c1->value and c2->value are presumed of different sign
* return saved index if any; else compute vertex and save */
int ImplicitCurve::vertid (CORNER *c1, CORNER *c2) {
//
Point2d a, b;
int vid = getedge(m_edges, c1->i, c1->j, c2->i, c2->j);
if (vid != -1) {
//
return vid; /* previously computed */
}
//
a.x = c1->x;
a.y = c1->y;
b.x = c2->x;
b.y = c2->y;
VERTEX *v = new VERTEX;
converge(&a, &b, c1->value, &(v->position)); /* position */
addtovertices(v); /* save vertex */
vid = m_vertices.size() - 1;
setedge(m_edges, c1->i, c1->j, c2->i, c2->j, vid);
return vid;
}
/* addtovertices: add v to sequence of vertices */
void ImplicitCurve::addtovertices (VERTEX *v) {
//
m_vertices.add(v);
}
/* converge: from two points of differing sign, converge to zero crossing */
void ImplicitCurve::converge (Point2d *p1, Point2d *p2, double v, Point2d *p) {
int i = 0;
Point2d pos, neg;
if (v < 0) {
pos.x = p2->x;
pos.y = p2->y;
neg.x = p1->x;
neg.y = p1->y;
} else {
pos.x = p1->x;
pos.y = p1->y;
neg.x = p2->x;
neg.y = p2->y;
}
while (1) {
p->x = 0.5 * (pos.x + neg.x);
p->y = 0.5 * (pos.y + neg.y);
if (i++ == RES_CURVE) return;
if ((getFunctionValueSafely(f, p->x, p->y)) > 0.0) {
pos.x = p->x;
pos.y = p->y;
} else {
neg.x = p->x;
neg.y = p->y;
}
}
}
void ImplicitCurve::line (int i1, int i2) {
m_line.add(i1);
m_line.add(i2);
}
double getFunctionValueSafely(F2P *f_s, double x, double y) {
double z = f_s->getSingleData(x, y);
if(z == std::numeric_limits<double>::infinity()) {
z = std::numeric_limits<double>::max();
return z;
}
if(z == -std::numeric_limits<double>::infinity()) {
z = std::numeric_limits<double>::min();
return z;
}
if(isNaN(z)) {
z = std::numeric_limits<double>::max();
//z = 0.0;
return z;
}
return z;
}
}
}