-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvolume.c
361 lines (320 loc) · 9.67 KB
/
volume.c
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include "volume.h"
void print_convexhull(ConvexHull * ch) {
ConvexHull * convexhull = ch;
while (convexhull) {
printf("(%d %d %d) ", convexhull->i0, convexhull->i1, convexhull->i2);
convexhull = convexhull->next;
} printf("\n");
}
/* build plane with normal n with 3 points */
void plane(double * X, double * Y, double * Z, int i0, int i1, int i2, double * n) {
/* vectors */
double _a0 = X[i1] - X[i0];
double _a1 = Y[i1] - Y[i0];
double _a2 = Z[i1] - Z[i0];
double _b0 = X[i2] - X[i0];
double _b1 = Y[i2] - Y[i0];
double _b2 = Z[i2] - Z[i0];
/* cross product */
n[0] = _a1 * _b2 - _a2 * _b1; // a
n[1] = _a2 * _b0 - _a0 * _b2; // b
n[2] = _a0 * _b1 - _a1 * _b0; // c
/* equation of plane: ax + by + cz + d = 0 */
n[3] = - n[0] * X[i0] - n[1] * Y[i0] - n[2] * Z[i0]; // d
}
/* build plane with normal n oriented in the opposite direction of i3 */
void oriented_plane(double * X, double * Y, double * Z, int i0, int i1, int i2, int i3, double * n) {
plane(X, Y, Z, i0, i1, i2, n);
double _a0 = X[i0] - X[i3];
double _a1 = Y[i0] - Y[i3];
double _a2 = Z[i0] - Z[i3];
double _dot = n[0] * _a0 + n[1] * _a1 + n[2] * _a2;
n[0] *= _dot;
n[1] *= _dot;
n[2] *= _dot;
n[3] *= _dot;
}
/* _a0 _b0 _c0 */
/* _a1 _b1 _c1 */
/* _a2 _b2 _c2 */
double volume_tetrahedron(double * X, double * Y, double * Z, int i0, int i1, int i2, int i3) {
/* vectors */
double _a0 = X[i1] - X[i0];
double _a1 = Y[i1] - Y[i0];
double _a2 = Z[i1] - Z[i0];
double _b0 = X[i2] - X[i0];
double _b1 = Y[i2] - Y[i0];
double _b2 = Z[i2] - Z[i0];
double _c0 = X[i3] - X[i0];
double _c1 = Y[i3] - Y[i0];
double _c2 = Z[i3] - Z[i0];
/* determinant */
double _det = _a0 * _b1 * _c2;
_det += _b0 * _c1 * _a2;
_det += _c0 * _a1 * _b2;
_det -= _c0 * _b1 * _a2;
_det -= _b0 * _a1 * _c2;
_det -= _a0 * _c1 * _b2;
/* volume */
return (_det < 0 ? -_det : _det)/6.0;
}
double distance_to_plane(double x, double y, double z, double * n) {
return n[0] * x + n[1] * y + n[2] * z + n[3];
}
/* pop a triangle from the convex hull */
void pop(ConvexHull ** ch, ConvexHull * old) {
if (*ch == old) {
*ch = old->next;
}
else {
ConvexHull * convexhull = *ch;
while (convexhull->next != old) {
convexhull = convexhull->next;
}
convexhull->next = convexhull->next->next;
}
free(old);
}
/* add a triangle to the convex hull */
void add_triangle(double * X, double * Y, double * Z, ConvexHull ** ch, int i0, int i1, int i2, int i3) {
/* find the back of the convex hull */
ConvexHull * convexhull = *ch;
while (convexhull->next) {
/* convex hull can't have twice the same triangle, this is an intersection, pop it ! */
if (((convexhull->next->i0 == i0 && convexhull->next->i1 == i1) || (convexhull->next->i0 == i1 && convexhull->next->i1 == i0)) && convexhull->next->i2 == i2) {
pop(ch, convexhull->next);
return;
}
convexhull = convexhull->next;
}
/* build the new triangle */
ConvexHull * ch_new = (ConvexHull *) malloc(sizeof(ConvexHull));
ch_new->i0 = i0;
ch_new->i1 = i1;
ch_new->i2 = i2;
oriented_plane(X, Y, Z, i0, i1, i2, i3, ch_new->n);
ch_new->next = NULL;
/* link the new triangle */
convexhull->next = ch_new;
}
/* build a tetrahedron */
ConvexHull * new_convexhull(double * X, double * Y, double * Z, int i0, int i1, int i2, int i3) {
ConvexHull * ch = (ConvexHull *) malloc(sizeof(ConvexHull));
ch->i0 = i0;
ch->i1 = i1;
ch->i2 = i2;
oriented_plane(X, Y, Z, i0, i1, i2, i3, ch->n);
ch->next = NULL;
/* minimal convex hull is tetrahedron */
add_triangle(X, Y, Z, &ch, i0, i1, i3, i2);
add_triangle(X, Y, Z, &ch, i0, i2, i3, i1);
add_triangle(X, Y, Z, &ch, i1, i2, i3, i0);
return ch;
}
/* find the farthest point upper the first triangle */
int farthest_point(double * X, double * Y, double * Z, int nb_points, ConvexHull ** ch, int * list_points, FILE * fp) {
int i;
#ifdef DEBUG
printf("\nAvailable points [ ");
for (i = 0 ; i < nb_points ; i++) {
printf("%d ", list_points[i]);
} printf("]\n\n");
#endif
int max = -1;
double distance_max = 0.0;
/* iterate over all surface of the convex hull */
ConvexHull * convexhull = *ch;
while (convexhull) {
for (i = 0 ; i < nb_points ; i++) {
/* check if the point is not used */
if (list_points[i]) {
double distance = distance_to_plane(X[i], Y[i], Z[i], convexhull->n);
/* check if the distance is greater than the max */
if (distance > distance_max) {
distance_max = distance;
max = i;
}
}
}
/* check if a maximum is found for this triangle */
if (max != -1) {
return max;
}
/* else there is no point on this side, pop this triangle for performance */
else {
ConvexHull * temp = convexhull;
convexhull = convexhull->next;
#ifdef PLOT
write_init(fp, temp->i0, temp->i1, temp->i2, 1);
#endif
pop(ch, temp);
}
}
return -1;
}
/* try to expand the convex hull with a point */
void expand(double * X, double * Y, double * Z, int nb_points, ConvexHull ** ch, int index, int * list_points, double * vol, FILE * fp) {
/* mark this point as used */
list_points[index] = 0;
/* iterate over triangles to merge a larger convex hull */
ConvexHull * convexhull = *ch;
while (convexhull) {
double distance = distance_to_plane(X[index], Y[index], Z[index], convexhull->n);
/* 1e-15 is a threshold to avoid imprecision */
if (distance > 1e-15 && convexhull->i2 != index) {
/* sum the volume of the created tetrahedron */
double v = volume_tetrahedron(X, Y, Z, convexhull->i0, convexhull->i1, convexhull->i2, index);
*vol += v;
#ifdef INSIDE
write(fp, convexhull->i0, convexhull->i1, convexhull->i2, index, 1);
#endif
/* build the new tetrahedron */
add_triangle(X, Y, Z, &convexhull, convexhull->i0, convexhull->i1, index, convexhull->i2);
add_triangle(X, Y, Z, &convexhull, convexhull->i0, convexhull->i2, index, convexhull->i1);
add_triangle(X, Y, Z, &convexhull, convexhull->i1, convexhull->i2, index, convexhull->i0);
/* pop the used triangle */
ConvexHull * temp = convexhull;
convexhull = convexhull->next;
pop(ch, temp);
}
else {
convexhull = convexhull->next;
}
}
}
/* initialize 4 points for our first tetrahedron */
void init(double * X, double * Y, double * Z, int nb_points, int * i0, int * i1, int * i2, int * i3, int * list_points) {
int i;
double max_x = X[0], min_x = X[0];
double max_y = Y[0], min_y = Y[0];
double max_z = Z[0], min_z = Z[0];
int index_max_x = 0, index_min_x = 0;
int index_max_y = 0, index_min_y = 0;
int index_max_z = 0, index_min_z = 0;
for (i = 0 ; i < nb_points ; i++) {
if (X[i] > max_x) {
max_x = X[i];
index_max_x = i;
}
if (X[i] < min_x) {
min_x = X[i];
index_min_x = i;
}
if (Y[i] > max_y) {
max_y = Y[i];
index_max_y = i;
}
if (Y[i] < min_y) {
min_y = Y[i];
index_min_y = i;
}
if (Z[i] > max_z) {
max_z = Z[i];
index_max_z = i;
}
if (Z[i] < min_z) {
min_z = Z[i];
index_min_z = i;
}
}
if (index_max_x != index_min_x) {
*i0 = index_max_x;
*i1 = index_min_x;
}
else if (index_max_y != index_min_y) {
*i0 = index_max_y;
*i1 = index_min_y;
}
else if (index_max_z != index_min_z) {
*i0 = index_max_z;
*i1 = index_min_z;
}
for (i = 0 ; i < nb_points ; i++) {
if (i != *i0 && i != *i1) {
*i2 = i;
break;
}
}
if (*i0 == *i1 || *i0 == *i2 || *i1 == *i2) {
printf("Abort: no plane\n");
exit(1);
}
double n[4];
plane(X, Y, Z, *i0, *i1, *i2, n);
double distance_max = -1.0;
int index_max = -1;
for (i = 0 ; i < nb_points ; i++) {
if (i != *i0 && i != *i1 && i != *i2) {
list_points[i] = 1;
double distance = distance_to_plane(X[i], Y[i], Z[i], n);
distance = distance < 0 ? -distance : distance;
if (distance > distance_max) {
distance_max = distance;
index_max = i;
}
}
else {
list_points[i] = 0;
}
}
list_points[index_max] = 0;
#ifdef PRINT
printf("max_x = %.2lf min_x = %.2lf\n", max_x, min_x);
printf("max_y = %.2lf min_y = %.2lf\n", max_y, min_y);
printf("max_z = %.2lf min_z = %.2lf\n", max_z, min_z);
printf("(i0, i1, i2, i3) = (%d, %d, %d, %d)\n", index_max_x, index_min_x, *i2, index_max);
#endif
*i3 = index_max;
if (*i3 == -1) {
printf("Abort: no volume\n");
exit(1);
}
}
/* find the volume of a polytope described by a cloud of points */
double volume(double * X, double * Y, double * Z, int nb_points) {
/* 4 points are needed to get a 3D convex hull */
if (nb_points < 4) {
printf("Abort: 4 points are needed\n");
return -1;
}
int list_points[nb_points];
int i0, i1, i2, i3;
init(X, Y, Z, nb_points, &i0, &i1, &i2, &i3, list_points);
#ifdef PLOT
FILE * fp = open_file(X, Y, Z, nb_points);
int i = 0;
write_step(fp, i);
write_init(fp, i0, i1, i2, 0);
write(fp, i0, i1, i2, i3, 0);
#endif
#ifndef PLOT
FILE * fp = NULL;
#endif
/* initialize the first tetrahedron */
ConvexHull * ch = new_convexhull(X, Y, Z, i0, i1, i2, i3);
double vol = volume_tetrahedron(X, Y, Z, i0, i1, i2, i3);
int index = farthest_point(X, Y, Z, nb_points, &ch, list_points, fp);
#ifdef DEBUG
print_convexhull(ch);
#endif
/* while it is growable */
while (index != -1) {
#ifdef DEBUG
printf("adding %d ...\n", index);
#endif
#ifdef PLOT
i++;
write_step(fp, i);
#endif
/* expand the convex hull */
expand(X, Y, Z, nb_points, &ch, index, list_points, &vol, fp);
#ifdef DEBUG
print_convexhull(ch);
#endif
index = farthest_point(X, Y, Z, nb_points, &ch, list_points, fp);
}
#ifdef PLOT
close_file(fp);
#endif
return vol;
}