-
Notifications
You must be signed in to change notification settings - Fork 16
/
test.js
249 lines (212 loc) · 6.81 KB
/
test.js
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
import fs from 'fs';
import test from 'node:test';
import assert from 'node:assert/strict';
import robustOrientation from 'robust-orientation';
import nextafter from 'nextafter';
import {
orient2d, orient2dfast,
orient3d, orient3dfast,
incircle, incirclefast,
insphere, inspherefast
} from '../index.js';
test('orient2d', () => {
assert.ok(orient2d(0, 0, 1, 1, 0, 1) < 0, 'clockwise');
assert.ok(orient2d(0, 0, 0, 1, 1, 1) > 0, 'counterclockwise');
assert.ok(orient2d(0, 0, 0.5, 0.5, 1, 1) === 0, 'collinear');
const r = 0.95;
const q = 18;
const p = 16.8;
const w = Math.pow(2, -43);
for (let i = 0; i < 128; i++) {
for (let j = 0; j < 128; j++) {
const x = r + w * i / 128;
const y = r + w * j / 128;
const o = orient2d(x, y, q, q, p, p);
const o2 = robustOrientation[3]([x, y], [q, q], [p, p]);
assert.ok(Math.sign(o) === Math.sign(o2), `${x},${y}, ${q},${q}, ${p},${p}: ${o} vs ${o2}`);
}
}
// 512x512 near-collinear
const lines = fs.readFileSync(new URL('./fixtures/orient2d.txt', import.meta.url), 'utf8').trim().split(/\r?\n/);
for (const line of lines) {
const [, ax, ay, bx, by, cx, cy, sign] = line.split(' ').map(Number);
const result = orient2d(ax, ay, bx, by, cx, cy);
assert.ok(Math.sign(result) === -sign, `${line}: ${result} vs ${-sign}`);
}
// 1000 hard fixtures
});
test('orient2dfast', () => {
assert.ok(orient2dfast(0, 0, 1, 1, 0, 1) < 0, 'counterclockwise');
assert.ok(orient2dfast(0, 0, 0, 1, 1, 1) > 0, 'clockwise');
assert.ok(orient2dfast(0, 0, 0.5, 0.5, 1, 1) === 0, 'collinear');
});
test('incircle', () => {
assert.ok(incircle(0, -1, 0, 1, 1, 0, -0.5, 0) < 0, 'inside');
assert.ok(incircle(0, -1, 1, 0, 0, 1, -1, 0) === 0, 'on circle');
assert.ok(incircle(0, -1, 0, 1, 1, 0, -1.5, 0) > 0, 'outside');
const a = nextafter(-1, 0);
const b = nextafter(-1, -2);
assert.ok(incircle(1, 0, -1, 0, 0, 1, 0, a) < 0, 'near inside');
assert.ok(incircle(1, 0, -1, 0, 0, 1, 0, b) > 0, 'near outside');
let x = 1e-64;
for (let i = 0; i < 128; i++) {
assert.ok(incircle(0, x, -x, -x, x, -x, 0, 0) > 0, `incircle test ${x}, outside`);
assert.ok(incircle(0, x, -x, -x, x, -x, 0, 2 * x) < 0, `incircle test ${x}, inside`);
assert.ok(incircle(0, x, -x, -x, x, -x, 0, x) === 0, `incircle test ${x}, cocircular`);
x *= 10;
}
// 384 incircle tests
const lines = fs.readFileSync(new URL('./fixtures/incircle.txt', import.meta.url), 'utf8').trim().split(/\r?\n/);
for (const line of lines) {
const [, ax, ay, bx, by, cx, cy, dx, dy, sign] = line.split(' ').map(Number);
const result = incircle(ax, ay, bx, by, cx, cy, dx, dy);
assert.ok(Math.sign(result) === sign, `${line}: ${result} vs ${sign}`);
}
// 1000 hard fixtures
});
test('incirclefast', () => {
assert.ok(incirclefast(0, -1, 0, 1, 1, 0, -0.5, 0) < 0, 'inside');
assert.ok(incirclefast(0, -1, 0, 1, 1, 0, -1, 0) === 0, 'on circle');
assert.ok(incirclefast(0, -1, 0, 1, 1, 0, -1.5, 0) > 0, 'outside');
});
test('orient3d', () => {
assert.ok(orient3d(
0, 0, 0,
0, 1, 0,
1, 0, 0,
0, 0, 1
) > 0, 'above');
assert.ok(orient3d(
0, 0, 0,
0, 1, 0,
1, 0, 0,
0, 0, -1
) < 0, 'below');
assert.ok(orient3d(
0, 0, 0,
0, 1, 0,
1, 0, 0,
0, 0, 0
) === 0, 'coplanar');
const a = nextafter(0, 1);
const b = nextafter(0, -1);
assert.ok(orient3d(
0, 0, 0,
0, 1, 0,
1, 0, 0,
0, 0, a
) > 0, 'near above');
assert.ok(orient3d(
0, 0, 0,
0, 1, 0,
1, 0, 0,
0, 0, b
) < 0, 'near below');
const lines = fs.readFileSync(new URL('./fixtures/orient3d.txt', import.meta.url), 'utf8').trim().split(/\r?\n/);
for (const line of lines) {
const [, ax, ay, az, bx, by, bz, cx, cy, cz, dx, dy, dz, sign] = line.split(' ').map(Number);
const result = orient3d(ax, ay, az, bx, by, bz, cx, cy, cz, dx, dy, dz);
assert.ok(Math.sign(result) === sign, `${line}: ${result} vs ${sign}`);
assert.ok(Math.sign(result) === Math.sign(orient3d(dx, dy, dz, bx, by, bz, ax, ay, az, cx, cy, cz)), 'symmetry');
}
// 1000 hard fixtures
const tol = 5.0e-14;
for (let i = 0; i < 1000; i++) {
const ax = 0.5 + tol * Math.random();
const ay = 0.5 + tol * Math.random();
const az = 0.5 + tol * Math.random();
const b = 12, c = 24, d = 48;
assert.ok(orient3d(b, b, b, c, c, c, d, d, d, ax, ay, az) === 0, 'degenerate');
assert.ok(orient3d(c, c, c, d, d, d, ax, ay, az, b, b, b) === 0, 'degenerate');
}
// 1000 degenerate cases
});
test('orient3dfast', () => {
assert.ok(orient3dfast(
0, 0, 0,
0, 1, 0,
1, 0, 0,
0, 0, 1
) > 0, 'above');
assert.ok(orient3dfast(
0, 0, 0,
0, 1, 0,
1, 0, 0,
0, 0, -1
) < 0, 'below');
assert.ok(orient3dfast(
0, 0, 0,
0, 1, 0,
1, 0, 0,
0, 0, 0
) === 0, 'coplanar');
});
test('insphere', () => {
assert.ok(insphere(
1, 0, 0,
0, -1, 0,
0, 1, 0,
0, 0, 1,
0, 0, 0
) < 0, 'inside');
assert.ok(insphere(
1, 0, 0,
0, -1, 0,
0, 1, 0,
0, 0, 1,
0, 0, 2
) > 0, 'outside');
assert.ok(insphere(
1, 0, 0,
0, -1, 0,
0, 1, 0,
0, 0, 1,
0, 0, -1
) === 0, 'cospherical');
const a = nextafter(-1, 0);
const b = nextafter(-1, -2);
assert.ok(insphere(
1, 0, 0,
0, -1, 0,
0, 1, 0,
0, 0, 1,
0, 0, a
) < 0, 'near inside');
assert.ok(insphere(
1, 0, 0,
0, -1, 0,
0, 1, 0,
0, 0, 1,
0, 0, b
) > 0, 'near outside');
const lines = fs.readFileSync(new URL('./fixtures/insphere.txt', import.meta.url), 'utf8').trim().split(/\r?\n/);
for (const line of lines) {
const [, ax, ay, az, bx, by, bz, cx, cy, cz, dx, dy, dz, ex, ey, ez, sign] = line.split(' ').map(Number);
const result = insphere(ax, ay, az, bx, by, bz, cx, cy, cz, dx, dy, dz, ex, ey, ez);
assert.ok(Math.sign(result) === -sign, `${line}: ${result} vs ${-sign}`);
}
// 1000 hard fixtures
});
test('inspherefast', () => {
assert.ok(inspherefast(
1, 0, 0,
0, -1, 0,
0, 1, 0,
0, 0, 1,
0, 0, 0
) < 0, 'inside');
assert.ok(inspherefast(
1, 0, 0,
0, -1, 0,
0, 1, 0,
0, 0, 1,
0, 0, 2
) > 0, 'outside');
assert.ok(inspherefast(
1, 0, 0,
0, -1, 0,
0, 1, 0,
0, 0, 1,
0, 0, -1
) === 0, 'cospherical');
});