Skip to content

Commit 92526cd

Browse files
authored
Self intersection (#103)
1 parent 89e2b0b commit 92526cd

File tree

2 files changed

+55
-17
lines changed

2 files changed

+55
-17
lines changed

src/extras/BuildingShapeUtils.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,24 @@ class BuildingShapeUtils extends ShapeUtils {
8181
* @return {[DOM.Element]} array of closed ways.
8282
*/
8383
static combineWays(ways) {
84+
const validWays = [];
85+
86+
for (const way of ways) {
87+
if (BuildingShapeUtils.isSelfIntersecting(way)) {
88+
const id = way.getAttribute('id');
89+
const msg = 'Way ' + id + ' is self-intersecting';
90+
window.printError(msg);
91+
} else {
92+
const i = 3 + 'q';
93+
validWays.push(way);
94+
}
95+
}
96+
8497
const closedWays = [];
8598
const wayBegins = {};
8699
const wayEnds = {};
87100

88-
ways.forEach(w => {
101+
validWays.forEach(w => {
89102
const firstNodeID = w.querySelector('nd').getAttribute('ref');
90103
if (wayBegins[firstNodeID]) {
91104
wayBegins[firstNodeID].push(w);
@@ -142,7 +155,7 @@ class BuildingShapeUtils extends ShapeUtils {
142155
return [];
143156
}
144157

145-
ways.forEach(w => {
158+
validWays.forEach(w => {
146159
const wayID = w.getAttribute('id');
147160
if (usedWays.has(wayID)){
148161
return;

test/combine_ways.test.js

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,88 +13,99 @@ import { Shape } from 'three';
1313
import { BuildingShapeUtils } from '../src/extras/BuildingShapeUtils.js';
1414
// import { JSDOM } from 'jsdom';
1515

16+
beforeEach(() => {
17+
errorMsgs = [];
18+
});
19+
1620
describe.each([
1721
[
1822
['<way id="1"><nd ref="1"/><nd ref="2"/><nd ref="3"/><nd ref="4"/></way>',
19-
], 0, 0, 'Single Open Way',
23+
], 0, 0, [], 'Single Open Way',
2024
],
2125
[
2226
[
2327
'<way id="1"><nd ref="1"/><nd ref="2"/></way>',
2428
'<way id="2"><nd ref="2"/><nd ref="3"/></way>',
2529
'<way id="3"><nd ref="3"/><nd ref="1"/></way>',
26-
], 1, 4, 'Test combining 3 ways 1->2->3',
30+
], 1, 4, [], 'Test combining 3 ways 1->2->3',
2731
],
2832
[
2933
[
3034
'<way id="1"><nd ref="1"/><nd ref="2"/></way>',
3135
'<way id="2"><nd ref="3"/><nd ref="1"/></way>',
3236
'<way id="3"><nd ref="2"/><nd ref="3"/></way>',
33-
], 1, 4, 'Test combining 3 ways 2->1->3',
37+
], 1, 4, [], 'Test combining 3 ways 2->1->3',
3438
],
3539
[
3640
[
3741
'<way id="1"><nd ref="1"/><nd ref="2"/></way>',
3842
'<way id="2"><nd ref="3"/><nd ref="2"/></way>',
3943
'<way id="3"><nd ref="3"/><nd ref="1"/></way>',
40-
], 1, 4, 'Test combining tip to tip',
44+
], 1, 4, [], 'Test combining tip to tip',
4145
],
4246
[
4347
[
4448
'<way id="1"><nd ref="1"/><nd ref="2"/></way>',
4549
'<way id="2"><nd ref="1"/><nd ref="3"/></way>',
4650
'<way id="3"><nd ref="2"/><nd ref="3"/></way>',
47-
], 1, 4, 'Test combining tail to tail',
51+
], 1, 4, [], 'Test combining tail to tail',
4852
],
4953
[
5054
[
5155
'<way id="1"><nd ref="1"/><nd ref="2"/></way>',
5256
'<way id="2"><nd ref="3"/><nd ref="4"/></way>',
5357
'<way id="3"><nd ref="4"/><nd ref="1"/></way>',
5458
'<way id="4"><nd ref="2"/><nd ref="3"/></way>',
55-
], 1, 5, 'Test combining 4 ways',
59+
], 1, 5, [], 'Test combining 4 ways',
5660
],
5761
[
5862
[
5963
'<way id="1"><nd ref="1"/><nd ref="2"/></way>',
6064
'<way id="2"><nd ref="3"/><nd ref="2"/></way>',
61-
], 0, 0, 'Test combining 2 open ways into one open way',
65+
], 0, 0, [], 'Test combining 2 open ways into one open way',
6266
],
6367
[
6468
[
6569
'<way id="1"><nd ref="1"/><nd ref="2"/></way>',
6670
'<way id="2"><nd ref="3"/><nd ref="2"/></way>',
6771
'<way id="3"><nd ref="4"/><nd ref="5"/></way>',
68-
], 0, 0, 'Test combining 3 open ways into 2 open ways',
72+
], 0, 0, [], 'Test combining 3 open ways into 2 open ways',
6973
],
7074
[
7175
[
7276
'<way id="1"><nd ref="1"/><nd ref="2"/></way>',
7377
'<way id="2"><nd ref="2"/><nd ref="4"/></way>',
7478
'<way id="3"><nd ref="2"/><nd ref="3"/></way>',
7579
'<way id="4"><nd ref="1"/><nd ref="3"/></way>',
76-
], 1, 4, 'Combining 4 open ways into 1 closed & 1 remaining open way',
80+
], 1, 4, [], 'Combining 4 open ways into 1 closed & 1 remaining open way',
7781
],
7882
[
7983
[
8084
'<way id="1"><nd ref="1"/><nd ref="2"/><nd ref="3"/></way>',
8185
'<way id="2"><nd ref="3"/><nd ref="5"/><nd ref="1"/></way>',
8286
'<way id="3"><nd ref="3"/><nd ref="4"/><nd ref="1"/></way>',
83-
], 1, 5, 'Dealing with amiguity. Only make one closed way',
87+
], 1, 5, [], 'Dealing with amiguity. Only make one closed way',
8488
],
89+
//[
90+
// [
91+
// '<way id="1"><nd ref="1"/><nd ref="2"/><nd ref="3"/></way>',
92+
// '<way id="2"><nd ref="3"/><nd ref="4"/><nd ref="2"/><nd ref="5"/><nd ref="1"/></way>',
93+
// ], 0, 0, [], 'Closed way is self intersecting',
94+
//],
8595
[
8696
[
87-
'<way id="1"><nd ref="1"/><nd ref="2"/><nd ref="3"/></way>',
88-
'<way id="2"><nd ref="3"/><nd ref="4"/><nd ref="2"/><nd ref="5"/><nd ref="1"/></way>',
89-
], 0, 0, 'Closed way is self intersecting',
97+
'<way id="1"><nd ref="1"/><nd ref="2"/></way>',
98+
'<way id="2"><nd ref="2"/><nd ref="3"/><nd ref="4"/><nd ref="5"/><nd ref="3"/><nd ref="1"/></way>',
99+
], 0, 0, ['Way 2 is self-intersecting'], 'Open way is self intersecting',
90100
],
91101
[
92102
[
93103
'<way id="1"><nd ref="1"/><nd ref="2"/></way>',
94104
'<way id="2"><nd ref="2"/><nd ref="3"/><nd ref="4"/><nd ref="5"/><nd ref="3"/><nd ref="1"/></way>',
95-
], 0, 0, 'Open way is self intersecting',
105+
'<way id="3"><nd ref="2"/><nd ref="3"/><nd ref="1"/></way>',
106+
], 1, 4, ['Way 2 is self-intersecting'], 'Open way is self intersecting, but ring formed',
96107
],
97-
])('Combine Ways', (ways, length, nodes, description) => {
108+
])('Combine Ways', (ways, length, nodes, errors, description) => {
98109
test(`${description}`, () => {
99110
let parser = new window.DOMParser();
100111
const xml = [];
@@ -103,10 +114,24 @@ describe.each([
103114
}
104115
let result = BuildingShapeUtils.combineWays(xml);
105116
expect(result.length).toBe(length);
117+
expect(errorMsgs.length).toBe(errors.length);
118+
if (errors.length) {
119+
for (const error of errors) {
120+
expect(errorMsgs.shift()).toBe(error);
121+
}
122+
}
106123
if (length) {
107124
expect(BuildingShapeUtils.isClosed(result[0]));
108125
expect(BuildingShapeUtils.isSelfIntersecting(result[0])).toBe(false);
109126
expect(result[0].getElementsByTagName('nd').length).toBe(nodes);
110127
}
111128
});
112129
});
130+
131+
window.printError = printError;
132+
133+
var errorMsgs = [];
134+
135+
function printError(txt) {
136+
errorMsgs.push(txt);
137+
}

0 commit comments

Comments
 (0)