-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
857 lines (705 loc) · 26.5 KB
/
index.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
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
// Note: QIE paper states that only LOD1–3 geometries are to be tested,
// therefore any cavities (interior shells) in solids can be ignored. This
// implies that a solid has exactly 1 shell representing its exterior surfaces.
//
// See: https://github.com/tudelft3d/CityGML-QIE-3Dvalidation/blob/master/errors/errors.md#shell
// TODO: Test against a CityGML dataset containing valid and invalid geometry
// TODO: Support inner polygons
var _ = require("lodash");
var async = require("async");
var sylvester = require("sylvester");
var polygonjs = require("polygon");
var citygmlBoundaries = require("citygml-boundaries");
var citygmlPoints = require("citygml-points");
var points3dto2d = require("points-3d-to-2d");
// Input: [polygon, polygon, ...]
var citygmlValidateShell = function(shell, callback) {
// Validate shell
// Validation errors are stored within results array
async.series([
GE_S_TOO_FEW_POLYGONS(shell),
GE_S_NOT_CLOSED(shell),
GE_S_NON_MANIFOLD_VERTEX(shell),
GE_S_NON_MANIFOLD_EDGE(shell),
GE_S_MULTIPLE_CONNECTED_COMPONENTS(shell),
GE_S_SELF_INTERSECTION(shell),
GE_S_POLYGON_WRONG_ORIENTATION(shell)
// GE_S_ALL_POLYGONS_WRONG_ORIENTATION(shell)
], function(err, results) {
// Remove passes (undefined)
var failures = _.filter(results, function(result) {
return (result && result[0]);
});
callback(err, failures);
});
};
var GE_S_TOO_FEW_POLYGONS = function(shell) {
// A shell should have at least 4 polygons - the simplest volumetric shape in
// 3D is a tetrahedron.
// Async pattern as per Pro JavaScript Frameworks book
// Missing process.nextTick trick inside the function
return function(callback) {
if (shell.length > 3) {
callback(null);
} else {
callback(null, [new Error("GE_S_TOO_FEW_POLYGONS: A shell should have at least 4 polygons"), shell.length]);
}
};
};
// IMPL: Look for edges that are only referenced by 1 polygon
// TODO: Store reference to edges
// TODO: Count number of times each edge is referenced
// TODO: Fail if edge count is 1
var GE_S_NOT_CLOSED = function(shell) {
// The shell must not have 'holes', ie it must be 'watertight'. This refers
// only to the topology of the shell, not to its geometry (see
// GE_S_SELF_INTERSECTION).
//
// Example: https://github.com/tudelft3d/CityGML-QIE-3Dvalidation/raw/master/errors/figs/302.png
return function(callback) {
var polygonPoints = [];
_.each(shell, function(polygonXML) {
var boundaries = citygmlBoundaries(polygonXML);
_.each(boundaries.exterior.concat(boundaries.interior), function(boundary) {
var bPoints = citygmlPoints(boundary);
polygonPoints.push(bPoints);
});
});
var edges = findEdges(polygonPoints);
var edgeCounts = countMatchingEdges(edges);
var holes = [];
_.each(edgeCounts, function(count, edgeId) {
if (count < 2) {
holes.push(edges[edgeId]);
}
});
if (holes.length === 0) {
callback(null);
} else {
callback(null, [new Error("GE_S_NOT_CLOSED: The shell must be watertight (ie. no holes)"), holes]);
}
};
};
// NOTE: Most non-manifold errors will fail GE_S_NOT_CLOSED before this test
// IMPL: Look for vertices that are referenced by 2 or more faces but don’t share an edge
// IMPL: Look for polygons that share vertices (all of them, otherwise fail GE_S_MULTIPLE_CONNECTED_COMPONENTS?)
// IMPL: Look for polygons that share edges
// IMPL: Fail if polygons that share a vertex don't also share an edge
var GE_S_NON_MANIFOLD_VERTEX = function(shell) {
// Polygons shared by a vertex must also share an edge
//
// Example: https://github.com/tudelft3d/CityGML-QIE-3Dvalidation/raw/master/errors/figs/303.png
return function(callback) {
var polygonPoints = [];
_.each(shell, function(polygonXML) {
var boundaries = citygmlBoundaries(polygonXML);
_.each(boundaries.exterior.concat(boundaries.interior), function(boundary) {
var bPoints = citygmlPoints(boundary);
polygonPoints.push(bPoints);
});
});
var edges = findEdges(polygonPoints);
var edgesByPolygon = findEdgesByPolygon(edges);
var polygonsByVertex = {};
_.each(polygonPoints, function(checkRingPoints, polygonIndex) {
_.each(checkRingPoints, function(point) {
var pointId = point.toString();
if (!polygonsByVertex[pointId]) {
polygonsByVertex[pointId] = [];
}
// Skip polygon if added already
if (_.indexOf(polygonsByVertex[pointId], polygonIndex) < 0) {
polygonsByVertex[pointId].push(polygonIndex);
}
});
});
// });
var badVertices = [];
_.each(polygonsByVertex, function(polygonIndexes, vertexId) {
// Skip if only one polygon is using this vertex
// This shouldn't fail here but will fail GE_S_NOT_CLOSED
if (polygonIndexes.length < 2) {
return;
}
var indexesCopy = _.clone(polygonIndexes);
var polygonIndex;
var polygonEdges;
var checkEdges;
var edgeMatch;
var polygonEdgeIdSplit;
var checkEdgeIdSplit;
while (indexesCopy.length > 0) {
polygonIndex = indexesCopy.shift();
polygonEdges = edgesByPolygon[polygonIndex];
// Find an edge shared by the current polygon and one of the others
// sharing this vertex
_.each(indexesCopy, function(checkIndex) {
checkEdges = edgesByPolygon[checkIndex];
edgeMatch = _.find(polygonEdges, function(polygonEdge) {
polygonEdgeIdSplit = polygonEdge.split(":");
return _.find(checkEdges, function(checkEdge) {
checkEdgeIdSplit = checkEdge.split(":");
return (polygonEdgeIdSplit[0] === checkEdgeIdSplit[1] && polygonEdgeIdSplit[1] === checkEdgeIdSplit[0]);
});
});
// Exit the each loop early
if (edgeMatch) {
return false;
}
});
// Exit the while loop early
if (edgeMatch) {
break;
}
}
// Check for lack of required edges
if (!edgeMatch) {
badVertices.push(vertexId);
}
});
if (badVertices.length === 0) {
callback(null)
} else {
callback(null, [new Error("GE_S_NON_MANIFOLD_VERTEX: Polygons shared by a vertex must also share an edge"), badVertices]);
}
};
};
// IMPL: Look for edges that are referenced more than twice
var GE_S_NON_MANIFOLD_EDGE = function(shell) {
// Each edge of a shell should have exactly 2 incident polygons.
//
// Example: https://github.com/tudelft3d/CityGML-QIE-3Dvalidation/raw/master/errors/figs/304.png
return function(callback) {
var polygonPoints = [];
_.each(shell, function(polygonXML) {
var boundaries = citygmlBoundaries(polygonXML);
_.each(boundaries.exterior.concat(boundaries.interior), function(boundary) {
var bPoints = citygmlPoints(boundary);
polygonPoints.push(bPoints);
});
});
var edges = findEdges(polygonPoints);
var edgeCounts = countMatchingEdges(edges);
var nonManifolds = [];
_.each(edgeCounts, function(count, edgeId) {
if (count !== 2) {
nonManifolds.push(edges[edgeId]);
}
});
if (nonManifolds.length === 0) {
callback(null);
} else {
callback(null, [new Error("GE_S_NON_MANIFOLD_EDGE: Each edge of a shell should have exactly 2 incident polygons"), nonManifolds]);
}
};
};
// IMPL: http://stackoverflow.com/a/21901612/997339
var GE_S_MULTIPLE_CONNECTED_COMPONENTS = function(shell) {
// Polygons that are not connected to the shell should be reported as an
// error.
//
// Example: https://github.com/tudelft3d/CityGML-QIE-3Dvalidation/raw/master/errors/figs/305.png
return function(callback) {
var polygonPoints = [];
_.each(shell, function(polygonXML) {
var boundaries = citygmlBoundaries(polygonXML);
_.each(boundaries.exterior.concat(boundaries.interior), function(boundary) {
var bPoints = citygmlPoints(boundary);
polygonPoints.push(bPoints);
});
});
var edges = findEdges(polygonPoints);
// List of edges as strings: [["0,0,0", "0,1,0"], [...]]
var edgeList = _.map(edges, function(edge) {
return [edge[0].toString(), edge[1].toString()];
});
var edgeListAdj = convert_edgelist_to_adjlist(edgeList);
var groups = [];
var visited = {};
var v;
for (v in edgeListAdj) {
if (edgeListAdj.hasOwnProperty(v) && !visited[v]) {
groups.push(bfs(v, edgeListAdj, visited));
}
}
// Fail if groups is more than 1
if (groups.length === 1) {
callback(null);
} else {
callback(null, [new Error("GE_S_MULTIPLE_CONNECTED_COMPONENTS: All polygons must be connected to the shell"), groups]);
}
};
};
// Based on: https://github.com/mrdoob/three.js/blob/master/src/math/Plane.js
// TODO: Support polygon holes
// TODO: Support i_GE_S_SELF_INTERSECTION_3.gml failure
// TODO: Fix NaN intersection results in i_GE_S_SELF_INTERSECTION_4.gml failure
var GE_S_SELF_INTERSECTION = function(shell) {
// If topology of the shell is correct and the shell is closed (thus no error
// GE_S_TOO_FEW_POLYGONS / GE_S_NOT_CLOSED / GE_S_NON_MANIFOLD_VERTEX /
// GE_S_NON_MANIFOLD_EDGE / GE_S_MULTIPLE_CONNECTED_COMPONENTS), it is
// possible that the geometry introduces errors, eg intersections. For
// instance, the topology of both these shells is identical, but the geometry
// differs. The left shell is valid while the right one is invalid.
//
// Example: https://github.com/tudelft3d/CityGML-QIE-3Dvalidation/raw/master/errors/figs/306.png
return function(callback) {
var polygonPoints = [];
_.each(shell, function(polygonXML) {
var boundaries = citygmlBoundaries(polygonXML);
_.each(boundaries.exterior.concat(boundaries.interior), function(boundary) {
var bPoints = citygmlPoints(boundary);
polygonPoints.push(bPoints);
});
});
// var checkPolygons = _.clone(shell);
// var edges = findEdges(shell);
var edges = findEdges(polygonPoints);
var intersections = [];
_.each(polygonPoints, function(checkRingPoints, polygonIndex) {
// var checkBoundaries = citygmlBoundaries(polygonXML);
// var checkRingPoints = citygmlPoints(checkBoundaries.exterior[0]);
// TODO: Should be abstracted into a Plane module as it's also used in
// GE_P_NON_PLANAR_POLYGON_DISTANCE_PLANE
var plane = {
normal: undefined,
constant: undefined
};
// Describe plane
plane.normal = $V(normalUnit(checkRingPoints[0], checkRingPoints[1], checkRingPoints[2]));
plane.constant = - $V(checkRingPoints[0]).dot(plane.normal);
_.each(edges, function(edge) {
// Ignore edges of the same polygon
if (_.contains(edge[2], polygonIndex)) {
return;
}
// Check each edge for intersection against the plane
var intersection = lineIntersectsPlane(plane, edge);
if (intersection) {
intersections.push(intersection);
}
});
});
if (intersections.length === 0) {
callback(null);
} else {
callback(null, [new Error("GE_S_SELF_INTERSECTION: Shell must not intersect itself"), intersections]);
}
};
};
// Note: This also tests for GE_S_ALL_POLYGONS_WRONG_ORIENTATION
//
// Note: See note at top about cavities being ignored in QIE testing. In short,
// you can assume that the given shell is outside and so all exterior boundaries
// should be anti-clockwise.
//
// Note: Wising depends entirely on which side you want to be viewing the
// polygon from.
//
// Note: This overlooks issues with collinear points (which should fail before
// anyway)
//
// IMPL: Find a polygon we know is facing up/down based on horizontal plane (eg.
// top-most must have normal angled above horizontal, bottom-most below). Seems
// impossible to have a valid shell with a "top" face that points at or below
// horizontal.
// IMPL: Look at all neighbouring polygons and check that orientation is opposite
var GE_S_POLYGON_WRONG_ORIENTATION = function(shell) {
// If a polygon is used to construct a shell, its exterior ring must be
// oriented in such as way that when viewed from outside the shell the points
// are ordered counterclockwise.
//
// And GE_S_ALL_POLYGONS_WRONG_ORIENTATION, if all the polygons have the wrong
// orientation (as defined in GE_S_POLYGON_WRONG_ORIENTATION), ie they all
// point inwards.
// TODO: This approach is way too complex – find a simpler way
// TODO: Run more checks to ensure this approach is valid in all circumstances
// - Overhanging top face /|
// - Pyramid /\
// - Wedge on top \ /
return function(callback) {
var polygonIndexes = [];
var polygonPoints = [];
var polygonWindings = [];
_.each(shell, function(polygonXML, polygonIndex) {
var boundaries = citygmlBoundaries(polygonXML);
var exteriorPoints = citygmlPoints(boundaries.exterior[0]);
polygonIndexes.push(polygonIndex);
polygonPoints.push(exteriorPoints);
});
if (polygonPoints.length === 0) {
callback(new Error("GE_S_POLYGON_WRONG_ORIENTATION: No polygons found"));
return;
}
// Polygons that share the top-most Z position
var topPolygons = {};
var maxZ;
_.each(polygonPoints, function(pPoints) {
var points2d = points3dto2d(pPoints);
var poly = polygonjs(points2d.points);
// Find winding
polygonWindings.push(poly.winding());
// Find top-most point
_.each(pPoints, function(point) {
if (maxZ === undefined || point[2] > maxZ) {
maxZ = point[2];
}
});
});
_.each(polygonPoints, function(pPoints, polygonIndex) {
var top;
var totalZ = 0;
var avgZ;
// Find polygons that use top-most point
_.each(pPoints, function(point) {
if (point[2] === maxZ) {
top = true;
}
totalZ += point[2];
});
if (top) {
avgZ = totalZ / pPoints.length;
topPolygons[polygonIndex] = [pPoints, avgZ];
}
});
var maxNormalZ;
var outerPolygon;
var outerPolygonIndex;
var outerPolygonNormal;
var flipped = [];
var outerFlipped = false;
// Pick outer polygon with the largest absolute Z normal as this will always // be on top after the previous filters in a valid solid (has the shallowest
// angle) and will filter out overhanging polygons
_.each(topPolygons, function(oPolygon, oPolygonIndex) {
var p0, p1, p2;
var normal;
var collinearThreshold = 0.05;
// Try and pick points that will give the best non-collinear result
// Otherwise, fall back to looping over all points
//
// TODO: De-dupe this and the other checks after this
if (oPolygon[0].length > 5) {
var offset = Math.floor(oPolygon[0].length / 3);
p0 = $V(oPolygon[0][0]);
p1 = $V(oPolygon[0][offset]);
p2 = $V(oPolygon[0][offset*2]);
// Colinear or near-colinear?
var cross = p0.subtract(p1).cross(p0.subtract(p2));
// Exit if non-collinear points are found
if (Math.abs(cross.e(1)) > collinearThreshold || Math.abs(cross.e(2)) > collinearThreshold || Math.abs(cross.e(3)) > collinearThreshold) {
normal = normalUnit(p0, p1, p2);
}
}
if (!normal) {
var firstPoint;
// Find first sequence of points that aren't collinear
_.each(oPolygon[0], function(point, pIndex) {
// Exit if no more points are available
if (pIndex === oPolygon[0].length - 1) {
return false;
}
if (pIndex === 0 && !firstPoint) {
firstPoint = $V(point);
return;
}
p0 = firstPoint;
p1 = $V(point);
p2 = $V(oPolygon[0][pIndex+1]);
// Colinear or near-colinear?
var cross = p0.subtract(p1).cross(p0.subtract(p2));
// Exit if non-collinear points are found
if (Math.abs(cross.e(1)) > collinearThreshold || Math.abs(cross.e(2)) > collinearThreshold || Math.abs(cross.e(3)) > collinearThreshold) {
normal = normalUnit(p0, p1, p2);
return false;
}
});
}
// Skip polygon if normal can't be found
if (normal === undefined) {
return;
}
// Skip first point to avoid normal inconsistancies with polygons that
// have identical start and end points.
// var normal = normalUnit(oPolygon[0][0], oPolygon[0][1], oPolygon[0][2]);
var absNormalZ = Math.abs(normal.e(3));
if (maxNormalZ === undefined || absNormalZ >= maxNormalZ) {
// It's possible for multiple polygons to share both the same top vertex
// and have the same absolute Z normal. This is likely because one of
// the polygons is invalid (eg. not planar) and so the polygon with the
// most vertices shared with the top vertex is chosen.
if (absNormalZ >= maxNormalZ) {
// Compare average Z values to pick top-most polygon
if (topPolygons[oPolygonIndex][1] < topPolygons[outerPolygonIndex][1]) {
return;
}
}
maxNormalZ = absNormalZ;
outerPolygon = oPolygon;
outerPolygonIndex = Number(oPolygonIndex);
outerPolygonNormal = normal;
}
});
if (!outerPolygon) {
callback(new Error("GE_S_POLYGON_WRONG_ORIENTATION: Outer polygon not found"));
return;
}
var checkPoints2d = points3dto2d(outerPolygon[0], false);
var checkPolygon = polygonjs(checkPoints2d.points);
var checkWinding = checkPolygon.winding();
// If normal has a negative Z then fail as shallowest (top-most) polygon
// will always have a positive Z normal
if (outerPolygonNormal.e(3) < 0) {
var insideOut = true;
var indexes = [];
// Fail GE_S_ALL_POLYGONS_WRONG_ORIENTATION if all polygons have the same,
// incorrect winding
_.each(polygonPoints, function(ePoints, eIndex) {
var ePoints2d = points3dto2d(ePoints, false);
var ePolygon = polygonjs(ePoints2d.points);
var eWinding = ePolygon.winding();
indexes.push(eIndex);
if (eWinding != checkWinding) {
insideOut = false;
return false;
}
});
if (insideOut) {
callback(null, [new Error("GE_S_ALL_POLYGONS_WRONG_ORIENTATION: All the polygons have the wrong orientation"), indexes]);
return;
} else {
// This will cause only flipped polygons to be valid, as they match up
// with the flipped outer polygon.
outerFlipped = true;
}
}
// This polygon is pointing outward and can be used to walk the rest of
// the solid to check and fix normals
//
// All other polygons should have the same winding as this one, assuming
// the solid is manifold
// Find edges
var edges = findEdges(polygonPoints);
var edgesByPolygon = findEdgesByPolygon(edges);
var edgeCounts = countMatchingEdges(edges);
// If an edge count is anything other than 2 then there has to be a polygon
// that has opposite winding to those adjacent to it (ie. flipped normal)
//
// TODO: This isn't robust enough – it fails when polygons aren't properly
// connected, which aren't valid either but aren't part of this test
_.each(edgeCounts, function(count, edgeId) {
if (count !== 2) {
// At this point we know edges[edgeId][2] contains the indexes for all
// the polygons which have incorrectly-matched edges
_.each(edges[edgeId][2], function(pIndex) {
var pEdges = edgesByPolygon[pIndex];
var pFlipped = true;
// If all edges have a count of 1 then this polygon is flipped,
// otherwise it's valid as it shares edges with other polygons
_.each(pEdges, function(pEdge) {
if (edgeCounts[pEdge] === 2) {
pFlipped = false;
}
});
if (pFlipped) {
if (!_.contains(flipped, pIndex)) {
flipped.push(pIndex);
}
}
// REMOVED: Winding checks can't be relied on – winding can be either
// the same or opposite depending on how 2D projection happens
//
// var polyWinding = polygonWindings[pIndex];
//
// // Compare windings (different === failure)
// if (polyWinding !== checkWinding) {
// if (!_.contains(flipped, pIndex)) {
// // Add polygon index to failures
// flipped.push(pIndex);
// }
// }
});
}
});
// All edges match up, now check windings match
// It's possible that a polygon can have matched edges yet still be pointing
// the wrong way. Eg. A flipped polygon would behave this way.
// We can't just check winding *or* edges as it's also possible for all
// polygons to be wound correctly yet not share edges properly.
if (flipped.length === 0) {
_.each(polygonWindings, function(winding, index) {
if (winding !== checkWinding) {
if (!_.contains(flipped, index)) {
flipped.push(index);
}
}
});
}
// If outer polygon is flipped then everything in the flipped array is
// actually valid (as it was compared against a flipped polygon). We can
// diff the flipped array with the original indexes to get the actually
// flipped polygons.
if (outerFlipped) {
flipped = _.difference(polygonIndexes, flipped);
}
callback(null, [new Error("GE_S_POLYGON_WRONG_ORIENTATION: When an exterior polygon is viewed from outside the shell the points must be ordered counterclockwise"), flipped]);
};
};
var findEdges = function(polygons) {
var edges = {};
// REMOVED: CityGML-specific processing should be done externally
// _.each(shell, function(polygonXML, polygonIndex) {
// var boundaries = citygmlBoundaries(polygonXML);
//
// var checkRings = boundaries.exterior.concat(boundaries.interior);
// var checkRingXML;
// var checkRingPoints;
// var prevPoint;
//
// _.each(checkRings, function(checkRingXML) {
// checkRingPoints = citygmlPoints(checkRingXML);
_.each(polygons, function(polygon, polygonIndex) {
var prevPoint;
_.each(polygon, function(point) {
if (!prevPoint) {
prevPoint = point;
return;
}
// Serialise edge
var edgeId = prevPoint.toString() + ":" + point.toString();
// First time create new edge definition, second time only add polygon
// index to edge
if (!edges[edgeId]) {
edges[edgeId] = [prevPoint, point, [polygonIndex]];
} else {
edges[edgeId][2].push(polygonIndex);
}
prevPoint = point;
});
});
// });
// });
return edges;
};
var countMatchingEdges = function(edges) {
var edgeCounts = {};
_.each(edges, function(edge, edgeId) {
var idParts = edgeId.split(":");
if (edgeCounts[edgeId]) {
edgeCounts[edgeId] += 1;
} else {
edgeCounts[edgeId] = 1;
}
// Look for reversed edge (ie. a shared one)
var sharedEdgeId = idParts[1] + ":" + idParts[0];
var sharedEdge = edges[sharedEdgeId];
if (sharedEdge) {
if (edgeCounts[sharedEdgeId]) {
edgeCounts[sharedEdgeId] += 1;
} else {
edgeCounts[sharedEdgeId] = 1;
}
}
});
return edgeCounts;
};
var findEdgesByPolygon = function(edges) {
var edgesByPolygon = {};
_.each(edges, function(edge, edgeId) {
_.each(edge[2], function(pIndex) {
if (!edgesByPolygon[pIndex]) {
edgesByPolygon[pIndex] = [];
}
edgesByPolygon[pIndex].push(edgeId);
});
});
return edgesByPolygon;
};
// From: http://stackoverflow.com/a/21901612/997339
// Converts an edgelist to an adjacency list representation
// In this program, we use a dictionary as an adjacency list,
// where each key is a vertex, and each value is a list of all
// vertices adjacent to that vertex
var convert_edgelist_to_adjlist = function(edgelist) {
var adjlist = {};
var i, len, pair, u, v;
for (i = 0, len = edgelist.length; i < len; i += 1) {
pair = edgelist[i];
u = pair[0];
v = pair[1];
if (adjlist[u]) {
// append vertex v to edgelist of vertex u
adjlist[u].push(v);
} else {
// vertex u is not in adjlist, create new adjacency list for it
adjlist[u] = [v];
}
if (adjlist[v]) {
adjlist[v].push(u);
} else {
adjlist[v] = [u];
}
}
return adjlist;
};
// From: http://stackoverflow.com/a/21901612/997339
// Breadth First Search using adjacency list
var bfs = function(v, adjlist, visited) {
var q = [];
var current_group = [];
var i, len, adjV, nextVertex;
q.push(v);
visited[v] = true;
while (q.length > 0) {
v = q.shift();
current_group.push(v);
// Go through adjacency list of vertex v, and push any unvisited
// vertex onto the queue.
// This is more efficient than our earlier approach of going
// through an edge list.
adjV = adjlist[v];
for (i = 0, len = adjV.length; i < len; i += 1) {
nextVertex = adjV[i];
if (!visited[nextVertex]) {
q.push(nextVertex);
visited[nextVertex] = true;
}
}
}
return current_group;
};
var normalUnit = function(p1, p2, p3) {
var v1 = $V(p1);
var v2 = $V(p2);
var v3 = $V(p3);
var a = v2.subtract(v1);
var b = v3.subtract(v1);
return a.cross(b).toUnitVector();
};
// From: https://github.com/mrdoob/three.js/blob/master/src/math/Plane.js
// TODO: Fix NaN intersection results (eg. in i_GE_S_SELF_INTERSECTION_4.gml)
var lineIntersectsPlane = function(plane, line) {
var startVector = $V(line[0]);
var endVector = $V(line[1]);
// End vector minus start vector
var direction = endVector.subtract(startVector);
var denominator = plane.normal.dot(direction);
if (denominator === 0) {
// Note: Removed as unsure we need this value returned
// // Line is coplanar, return origin
// if (this.distanceToPoint(line.start) === 0) {
// return result.copy(line.start);
// }
// Unsure if this is the correct method to handle this case.
return undefined;
}
var t = - (startVector.dot(plane.normal) + plane.constant) / denominator;
// TODO: Just less / more than, or equal to as well?
// Leaving as just less / more than seems to fail even for valid shells (eg. a
// simple box retuns t values of 0 and 1)
if (t <= 0 || t >= 1) {
return undefined;
}
var result = direction.dup().multiply(t).add(startVector);
return result;
};
module.exports = citygmlValidateShell;