-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquadric.cpp
345 lines (310 loc) · 10.3 KB
/
quadric.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
339
340
341
342
343
344
#include "quadric.h"
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <list>
#include <float.h>
#include <math.h>
#include <assert.h>
#include <errno.h>
#include <error.h>
#include <string.h>
//#include <boost/circular_buffer.hpp>
#define EPSILON 0.50
/* Quadratic surfaces are also called quadrics, and there are 17
* standard-form types. A quadratic surface intersects every plane in a
* (proper or degenerate) conic section. In addition, the cone consisting
* of all tangents from a fixed point to a quadratic surface cuts every plane
* in a conic section, and the points of contact of this cone with the
* surface form a conic section (Hilbert and Cohn-Vossen 1999, p. 12). */
/* ax^2 + by^2 + cz^2 + 2fyz + 2gzx + 2hxy + 2px + 2qy + 2rz + d = 0. */
double (*eval_funcs[])(const quadric *, const vector *) = {eval_ext, eval_int};
double (*eval)(const quadric *, const vector *) = eval_ext;
double eval_int(const quadric *q, const vector *v) {
double result = q->a * v->x * v->x +
q->b * v->y * v->y +
q->c * v->z * v->z +
q->d * v->y * v->z +
q->e * v->x * v->z +
q->f * v->x * v->y+
q->g * v->x +
q->h * v->y +
q->i * v->z +
q->j;
// bias of 0 should go to negative
return result == 0.0 ? -result : result;
}
double eval_ext(const quadric *q, const vector *v) {
double result = q->a * v->x * v->x +
q->b * v->y * v->y +
q->c * v->z * v->z +
q->d * v->y * v->z +
q->e * v->x * v->z +
q->f * v->x * v->y+
q->g * v->x +
q->h * v->y +
q->i * v->z +
q->j;
// bias of 0 should go to positive
return result;
}
/* if eval() of all neighboring points are of all the same sign, then
* v cannot be on the surface. Otherwise, it may be*/
int is_surface(const quadric *q, const vector *v) {
double val = eval(q, v);
if (val == 0.0)
return 1;
vector tmp;
int i = 1;
uint64_t sign1, sign2;
for (; i <= 4; i <<= 1) {
tmp.x = v->x + EPSILON * (i & 0x1);
tmp.y = v->y + EPSILON * ((i & 0x2) >> 1);
tmp.z = v->z + EPSILON * ((i & 0x4) >> 2);
val = eval(q, &tmp);
if (val == 0.0)
return 0;
sign1 = val > 0.0;
tmp.x = v->x - EPSILON * (i & 0x1);
tmp.y = v->y - EPSILON * ((i & 0x2) >> 1);
tmp.z = v->z - EPSILON * ((i & 0x4) >> 2);
val = eval(q, &tmp);
sign2 = val > 0.0;
if (val == 0.0)
return 0;
if(sign1 != sign2)
return 1;
}
return 0;
}
subspace *subspace_init(int64_t x_min, int64_t y_min,
int64_t z_min, int64_t x_max, int64_t y_max, int64_t z_max) {
subspace *s = (subspace *)malloc(sizeof(subspace));
s->x_min = x_min;
s->y_min = y_min;
s->z_min = z_min;
s->x_max = x_max;
s->y_max = y_max;
s->z_max = z_max;
size_t space_size = (x_max - x_min) * (y_max - y_min) * (z_max - z_min);
sem_init(&s->points_plotted, 0, 0);
s->points = (point *)calloc(space_size, sizeof(point));
size_t i;
for (i = 0; i < space_size; i++) {
// initialize x, y, and z
s->points[i].x = _x(s, i);
s->points[i].y = _y(s, i);
s->points[i].z = _z(s, i);
if(sem_init(&s->points[i].sema, 0, 1) == -1) {
free(s->points);
free(s);
return NULL;
}
}
return s;
}
void subspace_free(subspace *s) {
free(s->points);
free(s);
}
void frozen_subspace_free(frozen_subspace *f) {
free(f->points);
free(f);
}
/* Given a starting point v, traces a path until it arrives at a surface
* point. Returns 1 if successful, 0 otherwise. Surface is modified with the
* coordinates of the surface point. If 0 is returned, the new coordinates in
* surface are undefined
*/
int find_surface(quadric *q, const vector *v, vector *surface) {
vector tmp, closest;
double dist, shortest_dist;
int i;
shortest_dist = DBL_MAX;
int progress = 1;
*surface = *v;
while(!is_surface(q, surface) && progress) {
progress = 0;
for (i = 1; i <= 4; i <<=1) {
tmp.x = surface ->x + (i & 0x1);
tmp.y = surface ->y + ((i & 0x2) >> 1);
tmp.z = surface ->z + ((i & 0x4) >> 2);
if ((dist = fabs(eval(q, &tmp))) < shortest_dist) {
shortest_dist = dist;
closest = tmp;
progress = 1;
}
tmp.x = surface->x - (i & 0x1);
tmp.y = surface->y - ((i & 0x2) >> 1);
tmp.z = surface->z - ((i & 0x4) >> 2);
if ((dist = fabs(eval(q, &tmp))) < shortest_dist) {
shortest_dist = dist;
closest = tmp;
progress = 1;
}
}
*surface = closest;
}
return progress;
}
/* precondition: v is surface point
* Depth first trace of all surface points
* */
void depth_first_surface(subspace *s, const quadric *q, const vector *v, int positive) {
/* if out of bounding volume */
if (v->x < s->x_min || v->x >= s->x_max ||
v->y < s->y_min || v->y >= s->y_max ||
v->z < s->z_min || v->z >= s->z_max)
return;
uint64_t index = _index(s, v->x, v->y, v->z);
/* if already visited */
if (sem_trywait(&s->points[index].sema) == -1 &&
errno == EAGAIN)
return;
/* if not a surface point */
if (!is_surface(q, v))
return;
s->points[index].plotted = positive;
touch(s);
vector tmp;
int i, j, k;
for (i = -1; i <= 1; i++) {
for (j = -1; j <= 1; j++) {
for (k = -1; k <= 1; k++) {
if (!i && !j && !k)
continue;
tmp.x = v->x + i;
tmp.y = v->y + j;
tmp.z = v->z + k;
depth_first_surface(s, q, &tmp, positive);
}
}
}
}
void depth_first_fill(subspace *s, const quadric *q, const vector *v, int positive) {
/* if out of bounding volume */
if (v->x < s->x_min || v->x >= s->x_max ||
v->y < s->y_min || v->y >= s->y_max ||
v->z < s->z_min || v->z >= s->z_max)
return;
uint64_t index = _index(s, v->x, v->y, v->z);
/* if already visited */
if (sem_trywait(&s->points[index].sema) == -1 &&
errno == EAGAIN)
return;
/* if not a surface point */
if (!is_surface(q, v) && eval(q, v) > 0)
return;
s->points[index].plotted = positive;
touch(s);
vector tmp;
int i, j, k;
for (i = -1; i <= 1; i++) {
for (j = -1; j <= 1; j++) {
for (k = -1; k <= 1; k++) {
if (!i && !j && !k)
continue;
tmp.x = v->x + i;
tmp.y = v->y + j;
tmp.z = v->z + k;
depth_first_fill(s, q, &tmp, positive);
}
}
}
}
void print_func(void *data) {
printf("%p\n", data);
}
void breadth_first_surface(subspace *s, const quadric *q, const vector *v, int positive) {
std::list<vector *> queue = std::list<vector *>();
vector *tmp, *current = (vector *)malloc(sizeof(vector));
current->x = v->x; current->y = v->y; current->z = v->z;
queue.push_back(current);
int i, j, k;
uint64_t surface_points = 0;
while (!queue.empty()) {
current = queue.front();
queue.pop_front();
uint64_t index;
if (current->x < s->x_min || current->x >= s->x_max ||
current->y < s->y_min || current->y >= s->y_max ||
current->z < s->z_min || current->z >= s->z_max)
goto cleanup;
index = _index(s, current->x, current->y, current->z);
/* If point is not on surface, do not visit */
if(sem_trywait(&s->points[index].sema) == -1 &&
errno == EAGAIN)
goto cleanup;
if (!is_surface(q, current))
goto cleanup;
s->points[index].plotted = positive;
touch(s);
for (i = -1; i <= 1; i++) {
for (j = -1; j <= 1; j++) {
for (k = -1; k <= 1; k++) {
if (!i && !j && !k)
continue;
tmp = (vector *)malloc(sizeof(vector));
tmp->x = current->x + i;
tmp->y = current->y + j;
tmp->z = current->z + k;
queue.push_back(tmp);
}
}
}
cleanup:
free(current);
}
return;
}
void breadth_first_fill(subspace *s, const quadric *q, const vector *v, int positive) {
std::list<vector *> queue = std::list<vector *>();
vector *tmp, *current = (vector *)malloc(sizeof(vector));
current->x = v->x; current->y = v->y; current->z = v->z;
queue.push_back(current);
int i, j, k;
uint64_t surface_points = 0;
while (!queue.empty()) {
current = queue.front();
queue.pop_front();
uint64_t index;
if (current->x < s->x_min || current->x >= s->x_max ||
current->y < s->y_min || current->y >= s->y_max ||
current->z < s->z_min || current->z >= s->z_max)
goto cleanup;
index = _index(s, current->x, current->y, current->z);
/* If point is not on surface or the interior, do not visit */
if (!is_surface(q, current) && eval(q, current) > 0) {
goto cleanup;
}
if(sem_trywait(&s->points[index].sema) == -1 &&
errno == EAGAIN)
goto cleanup;
s->points[index].plotted = positive;
touch(s);
for (i = -1; i <= 1; i++) {
for (j = -1; j <= 1; j++) {
for (k = -1; k <= 1; k++) {
if (!i && !j && !k)
continue;
tmp = (vector *)malloc(sizeof(vector));
tmp->x = current->x + i;
tmp->y = current->y + j;
tmp->z = current->z + k;
queue.push_back(tmp);
}
}
}
cleanup:
free(current);
}
return;
}
void print_subspace(const subspace *s) {
printf("x: %lld to %lld, y: %lld to %lld, z: %lld to %lld\n",
s->x_min, s->x_max, s->y_min, s->y_max, s->z_min, s->z_max);
}
void print_vector(const vector *v) {
printf("{%lf, %lf, %lf}\n", v->x, v->y, v->z);
}