-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathvalidators.ts
More file actions
467 lines (418 loc) · 14.4 KB
/
validators.ts
File metadata and controls
467 lines (418 loc) · 14.4 KB
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
import Constrainautor from './Constrainautor';
import robustIntersect from 'robust-segment-intersect';
import {incircle, orient2d} from 'robust-predicates';
import type {Test} from 'tape';
import type {DelaunatorLike} from './Constrainautor';
type P2 = [number, number];
type PTS = P2[];
export function nextEdge(e: number){ return (e % 3 === 2) ? e - 2 : e + 1; }
export function prevEdge(e: number){ return (e % 3 === 0) ? e + 2 : e - 1; }
/**
* Maps keys to sets of values.
*
* @extends Map
*/
class SetMap<Key, Value> extends Map<Key, Set<Value>> {
/**
* Add a value to the set of `key` to the set-map. Adds a new set if the
* key didn't have one yet.
*
* @override
* @param key The key.
* @param val The value.
* @return this.
*/
set(key: Key, val: Set<Value>): this;
set(key: Key, val: Value): this;
set(key: Key, val: Value | Set<Value>): this {
let set = this.get(key);
if(!set){
set = new Set<Value>();
super.set(key, set);
}
if(val instanceof Set){
for(const v of val){ set.add(v); }
}else{
set.add(val);
}
return this;
}
/**
* Delete a value and/or an entire key from the set-map. If `val` is not
* given the entire set for the key is deleted, otherwise only the value
* is removed from the set.
*
* @override
* @param key The key.
* @param val The value.
* @return True if the value/key was in the set-map.
*/
delete(key: Key, val?: Value){
if(val !== undefined){
const set = this.get(key);
if(set){
const ret = set.delete(val);
if(!set.size){
super.delete(key);
}
return ret;
}
return false;
}
return super.delete(key);
}
}
/**
* Validate the output from Delaunator:
* - Half-edges not on the hull link back to eachother.
* - Linked half-edges have the same end-points.
* - Hull is convex.
* - Sum of all triangle areas equals area of the hull.
*
* @source delaunator
* @param t The tape test argument.
* @param points The points to delaunate.
* @param del The Delaunator output.
*/
function validateDelaunator(t: Test, points: PTS, del: DelaunatorLike){
let failed = false;
//for(let pnt = 0; pnt < points.length; pnt++){
// if(!Array.prototype.includes.call(del.triangles, pnt)){
// t.fail(`point ${pnt} not triangulated`);
// failed = true;
// }
//}
// validate halfedges
for(let edg = 0; edg < del.triangles.length; edg++){
const adj = del.halfedges[edg];
if(adj === -1){
continue;
}
if(del.halfedges[adj] !== edg){
t.fail(`invalid halfedge connection: ${edg} -> ${adj} != ${del.halfedges[adj]} -> ${edg}`);
failed = true;
}
const e1 = del.triangles[edg];
const e2 = del.triangles[nextEdge(edg)];
const a1 = del.triangles[adj];
const a2 = del.triangles[nextEdge(adj)];
if(e1 !== a2 && e2 !== a1){
t.fail(`halfedges ${edg}/${adj} do not share end-points (${e1}, ${e2}) / (${a2}, ${a1})`);
failed = true;
}
}
// validate triangulation
const hullAreas = [];
const hulLen = del.hull.length;
for(let i = 0, j = hulLen - 1; i < hulLen; j = i++){
const [x0, y0] = points[del.hull[j]];
const [x, y] = points[del.hull[i]];
hullAreas.push((x - x0) * (y + y0));
const c = convex(
points[del.hull[j]],
points[del.hull[(j + 1) % del.hull.length]],
points[del.hull[(j + 3) % del.hull.length]]
);
if(!c){
t.fail(`hull is not convex at ${j}`);
failed = true;
}
}
const hullArea = sum(hullAreas);
const triangleAreas = [];
for(let i = 0; i < del.triangles.length; i += 3){
const [ax, ay] = points[del.triangles[i]];
const [bx, by] = points[del.triangles[i + 1]];
const [cx, cy] = points[del.triangles[i + 2]];
triangleAreas.push(Math.abs((by - ay) * (cx - bx) - (bx - ax) * (cy - by)));
}
const trianglesArea = sum(triangleAreas);
const err = Math.abs((hullArea - trianglesArea) / hullArea);
if(err > Math.pow(2, -51)){
t.fail(`triangulation is broken: ${err} error`);
failed = true;
}
t.assert(!failed, `triangulation is valid`);
return failed;
}
function convex(r: P2, q: P2, p: P2){
return (orient2d(...p, ...r, ...q) ||
orient2d(...r, ...q, ...p) ||
orient2d(...q, ...p, ...r)) >= 0;
}
// Kahan and Babuska summation, Neumaier variant; accumulates less FP error
function sum(x: number[]){
let sum = x[0];
let err = 0;
for(let i = 1; i < x.length; i++){
const k = x[i];
const m = sum + k;
err += Math.abs(sum) >= Math.abs(k) ? sum - m + k : k - m + sum;
sum = m;
}
return sum + err;
}
/**
* Validate the vertMap of a Constrainautor:
* - Un-triangulated points are found by untriangulatedPoints.
* - All incoming edges to a point can be reached by walking around the point
* starting at the edge in `con.vertMap`.
*
* @param t The tape test argument.
* @param points The points of the triangulation.
* @param con The constrainautor.
*/
function validateVertMap(t: Test, points: PTS, con: Constrainautor){
const del = con.del;
const numPoints = points.length;
const numEdges = del.triangles.length;
const edgeMap = new SetMap<number, number>();
const unTried = new Set(con.untriangulatedPoints());
let failed = false;
for(let edg = 0; edg < numEdges; edg++){
const p1 = del.triangles[edg];
// points *to*
edgeMap.set(p1, prevEdge(edg));
}
for(let i = 0; i < numPoints; i++){
const inc = edgeMap.get(i);
if(inc === undefined){
if(!unTried.has(i)){
t.fail(`point ${i} has no incoming edges, but not found as un-triangulated`);
failed = true;
}else{
unTried.delete(i);
}
continue;
}
const start = con.vertMap[i];
if(start > numEdges){
t.fail(`invalid start-edge in vertMap: vertMap[${i}] = ${start}`);
failed = true;
continue;
}
let edg = start;
do{
if(!inc.has(edg)){
t.fail(`edge ${edg} incorrectly marked as incoming to point ${i}`);
failed = true;
}
inc.delete(edg);
const nxt = nextEdge(edg);
const adj = del.halfedges[nxt];
edg = adj;
}while(edg !== -1 && edg !== start);
if(inc.size){
t.fail(`edges missed while walking around point: ${i}: ${inc}`);
failed = true;
}
edgeMap.delete(i);
}
if(edgeMap.size){
t.fail(`invalid points in edge map: ${edgeMap}`);
failed = true;
}
if(unTried.size){
t.fail(`${unTried.size} points were found as un-triangulated, but had incoming edges`);
failed = true;
}
t.assert(!failed, `vertMap is valid`);
return failed;
}
/**
* Validate the flips array of a Constrainautor:
* - All entries have either the IGND, CONSD, or FLIPD value, and no other.
* - Linked half-edges have the same flip value.
* - If requested, FLIPD values were cleared by delaunify.
*
* @param t The tape test argument.
* @param con The constrainautor.
* @param edges The edge ids of constraint edges, as returned by constrainOne.
* @param clear If `true`, disallow FLIPD values.
*/
function validateFlips(t: Test, con: Constrainautor, clear = true){
const del = con.del;
const numEdges = del.triangles.length;
let failed = false;
for(let edg = 0; edg < numEdges; edg++){
const adj = del.halfedges[edg];
if(clear && con.flips.has(edg)){
t.fail(`flip not cleared for ${edg}/${adj}: ${con.flips.has(edg)}`);
failed = true;
}
if(adj === -1){
continue;
}
if(con.isConstrained(edg) !== con.isConstrained(adj) || con.flips.has(edg) !== con.flips.has(adj)){
t.fail(`flip status inconsistent for ${edg}/${adj}: ${con.flips.has(edg)}/${con.flips.has(adj)}`);
failed = true;
}
}
t.assert(!failed, `flips array is valid`);
return failed;
}
/**
* Check that non-constrained edges are Delaunay.
*
* @param t The tape test argument.
* @param con The constrainautor.
*/
function validateDelaunay(t: Test, con: Constrainautor){
const del = con.del;
const pts = del.coords;
const len = del.triangles.length;
let failed = false;
for(let edg = 0; edg < len; edg++){
const adj = del.halfedges[edg];
if(con.isConstrained(edg) || adj < edg){ // also catches adj === -1
continue;
}
/*
* e2/a1
* o
* / | \
* / | \
* / | \
* / | \
* e3 o edg | adj o a3
* \ | /
* \ | /
* \ | /
* \ | /
* o
* e1/a2
*/
const e1 = del.triangles[edg];
const e2 = del.triangles[nextEdge(edg)];
const e3 = del.triangles[nextEdge(nextEdge(edg))];
const a3 = del.triangles[nextEdge(nextEdge(adj))];
const p1x = pts[e1 * 2], p1y = pts[e1 * 2 + 1];
const p2x = pts[e2 * 2], p2y = pts[e2 * 2 + 1];
const p3x = pts[e3 * 2], p3y = pts[e3 * 2 + 1];
const p4x = pts[a3 * 2], p4y = pts[a3 * 2 + 1];
const isD = incircle(p1x, p1y, p2x, p2y, p3x, p3y, p4x, p4y);
if(isD < 0){
t.fail(`triangles shared by ${edg}/${adj} not Delaunay (${isD})`);
failed = true;
}
}
t.assert(!failed, `all edges are Delaunay`)
return failed;
}
function isNegativeZero(val: any){
return Object.is(val, -0);
}
/**
* Validate that an edge was correctly constrained:
* - `constrainOne` returned the correct value.
* - The constrained edge occurs exactly once.
* - If not on the hull, the adjacent edge occurs exactly once.
* - No edge intersects the constrained edge.
* - The constrained edge is marked in the flips array.
*
* @param t The tape test argument.
* @param points The points of the triangulation.
* @param con The constrainautor.
* @param ret The return value from `con.constrainOne(p1, p2)`.
* @param p1 The index of point 1.
* @param p2 The index of point 2.
*/
function validateConstraint(t: Test, points: PTS, con: Constrainautor, ret: number | undefined, p1: number, p2: number){
const del = con.del;
const numEdges = del.triangles.length;
const [x1, y1] = points[p1];
const [x2, y2] = points[p2];
const re1 = ret === undefined ? -1 : (ret < 0 || isNegativeZero(ret) ? del.triangles[nextEdge(-ret)] : del.triangles[ret]);
const re2 = ret === undefined ? -1 : (ret < 0 || isNegativeZero(ret) ? del.triangles[-ret] : del.triangles[nextEdge(ret)]);
const find = con.findEdge(p1, p2);
let failed = false;
if(ret !== undefined && (re1 !== p1 || re2 !== p2)){
t.fail(`invalid edge returned from constrainOne: ${ret}: ${re1} -> ${re2} !== ${p1} -> ${p2}`);
failed = true;
}
let found = -1;
let foundAdj = -1;
for(let edg = 0; edg < numEdges; edg++){
const e1 = del.triangles[edg];
const e2 = del.triangles[nextEdge(edg)];
if(e1 === p1 && e2 === p2){
if(found !== -1){
t.fail(`edge ${edg} (${e1} -> ${e2}) is duplicate of constraint`);
failed = true;
}
found = edg;
}else if(e1 === p2 && e2 === p1){
if(foundAdj !== -1){
t.fail(`edge ${edg} (${e1} -> ${e2}) is reversed duplicate of constraint`);
failed = true;
}
foundAdj = edg;
}
if(e1 === p1 || e1 === p2 || e2 === p1 || e2 === p2){
continue;
}
const [x3, y3] = points[e1];
const [x4, y4] = points[e2];
if(robustIntersect([x1, y1], [x2, y2], [x3, y3], [x4, y4])){
t.fail(`edge ${edg} (${e1} -> ${e2}) intersects constrained edge ${p1} -> ${p2}`);
failed = true;
}
}
if(found === -1 && foundAdj === -1){
t.fail(`constrained edge not in triangulation`);
failed = true;
}
if(found !== -1){
if(!con.isConstrained(found)){
t.fail(`constrained edge not marked`);
failed = true;
}
t.equal(find, found, `findEdge returned found edge: ${find} === ${found}`);
}
if(foundAdj !== -1){
if(!con.isConstrained(foundAdj)){
t.fail(`reverse constrained edge not marked`);
failed = true;
}
if(found === -1){
t.equal(find, -foundAdj, `findEdge returned -found edge: ${find} === -${foundAdj}`);
}
}
t.assert(!failed, `constraint ${p1} -> ${p2}: ${ret === undefined ? `(${found})` : ret} is valid`);
return failed;
}
function validateAllConstraints(t: Test, points: PTS, edges: PTS, con: Constrainautor){
for(const [p1, p2] of edges){
validateConstraint(t, points, con, undefined, p1, p2);
}
const del = con.del;
const triangles = del.triangles;
const numEdges = triangles.length;
const conEdges = new Set<number>();
for(const [p1, p2] of edges){
const edg = con.findEdge(p1, p2);
const adj = con.findEdge(p2, p1);
if(edg >= 0){
conEdges.add(edg);
}
if(adj >= 0){
conEdges.add(adj);
}
}
for(let edg = 0; edg < numEdges; edg++){
if(conEdges.has(edg)){
t.assert(con.isConstrained(edg), `constrained edge ${edg} (${triangles[edg]} -> ${triangles[nextEdge(edg)]}) marked as constrained`);
}else if(con.isConstrained(edg)){
t.fail(`non-constrained edge ${edg} (${triangles[edg]} -> ${triangles[nextEdge(edg)]}) marked as constrained`);
}
}
}
export {
validateDelaunator,
validateVertMap,
validateConstraint,
validateFlips,
validateDelaunay,
validateAllConstraints,
SetMap
};