forked from msokalski/delabella
-
Notifications
You must be signed in to change notification settings - Fork 0
/
speedtest.cpp
354 lines (281 loc) · 7.73 KB
/
speedtest.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
345
346
347
348
349
350
351
352
353
354
/*
DELABELLA - Delaunay triangulation library
Copyright (C) 2018 GUMIX - Marcin Sokalski
*/
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <tuple>
#include "delaunator-cpp.h"
#include "delabella.h"
#include "NewtonApple_hull3D.h"
extern "C"
{
#include "qhull/libqhull_r.h"
#include "qhull/qhull_ra.h"
}
#include <algorithm>
#include "s_hull_pro.h"
#include <windows.h>
bool pointSortPredicate(const Shx& a, const Shx& b)
{
if (a.r < b.r)
return true;
else if (a.r > b.r)
return false;
else if (a.c < b.c)
return true;
else
return false;
};
bool pointComparisonPredicate(const Shx& a, const Shx& b)
{
return a.r == b.r && a.c == b.c;
}
void print_points(int n)
{
if (n < 1000)
printf("% 14d", n);
else
if (n < 1000000)
printf("% 10d,%03d", n/1000, n%1000);
else
if (n < 1000000000)
printf("% 6d,%03d,%03d", n / 1000000, n/1000 % 1000, n%1000);
}
struct MyPoint
{
int x, y;
void get_xy(double xy[2], void* cookie)
{
xy[0] = x;
xy[1] = y;
}
};
int main(int argc, char* argv[])
{
LARGE_INTEGER t0, t1, fr;
QueryPerformanceFrequency(&fr);
double dt;
int POINTS;
const static int test[18] =
{
10,25,50,
100,250,500,
1000,2500,5000,
10000,25000,50000,
100000,250000,500000,
1000000,2500000,5000000
};
int passes = sizeof(test) / sizeof(int);
printf("| points | QHULL | S-HULL | S-HULL-3D | DELAUNATOR | DELABELLA |\n");
printf("| --------------:| ------------:| ------------:| ------------:| ------------:| ------------:|\n");
for (int pass = -5; pass < passes; pass++)
{
if (pass<0)
POINTS = test[0];
else
POINTS = test[pass];
int max_tris = 2 * POINTS - 5;
int* abc = new int[3 * max_tris];
double* xy = new double[2 * POINTS];
double dt_qhull, dt_shull, dt_shull3d, dt_delaunator, dt_delabella;
dt_qhull = dt_shull = dt_shull3d = dt_delaunator = dt_delabella = -1;
srand(pass + 32109);
for (int i = 0; i < 2 * POINTS; i++)
{
double quot = (rand() & 0x3FFF) - 8192; // +-8k (14bits)
unsigned int frac = ((rand() & 0x7FFF) << 15) | (rand() & 0x7FFF); // 30 bits
xy[i] = quot + frac / (double)(1 << 30);
}
if (1)
{
char hidden_options[] = " d n v H U Qb QB Qc Qf Qg Qi Qm Qr QR Qv Qx TR E V FC Fi Fo Ft Fp FV Q0 Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 ";
// int curlong, totlong; /* used !qh_NOmem */
int numpoints = POINTS, dim = 3;
coordT *points = (coordT*)malloc(sizeof(coordT)*POINTS * 3);
for (int i = 0; i < POINTS; i++)
{
points[3 * i + 0] = xy[2 * i + 0];
points[3 * i + 1] = xy[2 * i + 1];
points[3 * i + 2] = xy[2 * i + 0] * xy[2 * i + 0] + xy[2 * i + 1] * xy[2 * i + 1];
}
boolT ismalloc = false;
qhT qh_qh;
qhT *qh = &qh_qh;
qh_init_A(qh, stdin, stdout, stderr, argc, argv); /* sets qh->qhull_command */
qh->NOerrexit = False;
qh_option(qh, "delaunay Qbbound-last", NULL, NULL);
qh->DELAUNAY = True; /* 'd' */
qh->TRIangulate = True; /* Qt*/
qh->SCALElast = True; /* 'Qbb' */
qh->KEEPcoplanar = True; /* 'Qc', to keep coplanars in 'p' */
qh_checkflags(qh, qh->qhull_command, hidden_options);
qh_initflags(qh, qh->qhull_command);
// points = qh_readpoints(qh, &numpoints, &dim, &ismalloc);
if (dim >= 5) {
qh_option(qh, "Qxact_merge", NULL, NULL);
qh->MERGEexact = True; /* 'Qx' always */
}
qh_init_B(qh, points, numpoints, dim, ismalloc);
QueryPerformanceCounter(&t0);
qh_qhull(qh);
qh_findgood_all(qh, qh->facet_list);
QueryPerformanceCounter(&t1);
/*
qh_check_output(qh);
qh_produce_output(qh);
if (qh->VERIFYoutput && !qh->FORCEoutput && !qh->STOPpoint && !qh->STOPcone)
qh_check_points(qh);
*/
int tris = qh->num_good;
dt = (double)(t1.QuadPart - t0.QuadPart) / (double)fr.QuadPart;
//printf("QHL tris = %d, dt = %f ms\n", tris, dt * 1000);
free(points);
dt_qhull = dt;
}
if (1)
{
std::vector<Shx> pts;
std::vector<Triad> triads;
for (int i = 0; i < POINTS; i++)
{
Shx pt;
pt.id = i;
pt.r = (float)xy[2 * i + 0];
pt.c = (float)xy[2 * i + 1];
pts.push_back(pt);
}
QueryPerformanceCounter(&t0);
std::sort(pts.begin(), pts.end(), pointSortPredicate);
std::vector<Shx>::iterator newEnd = std::unique(pts.begin(), pts.end(), pointComparisonPredicate);
pts.resize(newEnd - pts.begin());
s_hull_pro(pts, triads);
QueryPerformanceCounter(&t1);
double dt = (double)(t1.QuadPart - t0.QuadPart) / (double)fr.QuadPart;
//printf("SHL tris = %d, dt = %f ms\n", (int)triads.size(), dt * 1000);
dt_shull = dt;
}
if (1)
{
std::vector<R3> pts;
std::vector<Tri> triads;
for (int i = 0; i < POINTS; i++)
{
R3 pt;
pt.id = i;
pt.r = xy[2 * i + 0];
pt.c = xy[2 * i + 1];
pt.z = pt.r*pt.r + pt.c*pt.c;
pts.push_back(pt);
}
QueryPerformanceCounter(&t0);
NewtonApple_Delaunay(pts, triads);
QueryPerformanceCounter(&t1);
dt = (double)(t1.QuadPart - t0.QuadPart) / (double)fr.QuadPart;
//printf("SH3 tris = %d, dt = %f ms\n", (int)triads.size(), dt * 1000);
dt_shull3d = dt;
}
if (1)
{
std::vector<double> coords;
for (int i = 0; i < POINTS; i++)
{
coords.push_back(xy[2 * i + 0]);
coords.push_back(xy[2 * i + 1]);
}
QueryPerformanceCounter(&t0);
delaunator::Delaunator d(coords);
size_t verts = d.triangles.size();
QueryPerformanceCounter(&t1);
dt = (double)(t1.QuadPart - t0.QuadPart) / (double)fr.QuadPart;
//printf("DTR tris = %d, dt = %f ms\n", verts/3, dt * 1000);
dt_delaunator = dt;
}
if (1)
{
IDelaBella* idb = IDelaBella::Create();
struct MyErrLogStream
{
MyErrLogStream(FILE* f) : file(f) {}
static int errlog(void* stream, const char* fmt, ...)
{
int ret;
MyErrLogStream* s = (MyErrLogStream*)stream;
va_list arglist;
va_start(arglist, fmt);
ret = vfprintf(s->file, fmt, arglist);
va_end(arglist);
return ret;
}
FILE* file;
};
MyErrLogStream myerrlog(stderr);
idb->SetErrLog(MyErrLogStream::errlog, &myerrlog);
QueryPerformanceCounter(&t0);
int verts = idb->Triangulate(POINTS, xy, xy+1, sizeof(double[2]));
QueryPerformanceCounter(&t1);
idb->Destroy();
dt = (double)(t1.QuadPart - t0.QuadPart) / (double)fr.QuadPart;
//printf("DEL tris = %d, dt = %f ms\n", verts / 3, dt * 1000);
dt_delabella = dt;
}
if (pass >= 0)
{
printf("| ");
print_points(POINTS);
printf(" | %9.3f ms | %9.3f ms | %9.3f ms | %9.3f ms | %9.3f ms |\n",
dt_qhull * 1000,
dt_shull * 1000,
dt_shull3d * 1000,
dt_delaunator * 1000,
dt_delabella * 1000);
}
delete[] xy;
delete[] abc;
}
return 0;
}
void a()
{
// somewhere in your code ...
int POINTS = 1000000;
struct MyPoint
{
char something;
float x;
int something_else;
float y;
float foo[5];
};
MyPoint* cloud = new MyPoint[POINTS];
srand(36341);
// gen some random input
for (int i = 0; i < POINTS; i++)
{
cloud[i].x = rand();
cloud[i].y = rand();
}
IDelaBella* idb = IDelaBella::Create();
int verts = idb->Triangulate(POINTS, &cloud->x, &cloud->y, sizeof(MyPoint));
// if positive, all ok
if (verts>0)
{
int tris = verts / 3;
const DelaBella_Triangle* dela = idb->GetFirstDelaunayTriangle();
for (int i = 0; i<tris; i++)
{
// do something with dela triangle
// ...
dela = dela->next;
}
}
else
{
// no points given or all points are colinear
// make emergency call ...
}
delete[] cloud;
idb->Destroy();
// ...
}