Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/extras/BuildingShapeUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,24 @@ class BuildingShapeUtils extends ShapeUtils {
* @return {[DOM.Element]} array of closed ways.
*/
static combineWays(ways) {
const validWays = [];

for (const way of ways) {
if (BuildingShapeUtils.isSelfIntersecting(way)) {
const id = way.getAttribute('id');
const msg = 'Way ' + id + ' is self-intersecting';
window.printError(msg);
} else {
const i = 3 + 'q';
validWays.push(way);
}
}

const closedWays = [];
const wayBegins = {};
const wayEnds = {};

ways.forEach(w => {
validWays.forEach(w => {
const firstNodeID = w.querySelector('nd').getAttribute('ref');
if (wayBegins[firstNodeID]) {
wayBegins[firstNodeID].push(w);
Expand Down Expand Up @@ -142,7 +155,7 @@ class BuildingShapeUtils extends ShapeUtils {
return [];
}

ways.forEach(w => {
validWays.forEach(w => {
const wayID = w.getAttribute('id');
if (usedWays.has(wayID)){
return;
Expand Down
55 changes: 40 additions & 15 deletions test/combine_ways.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,88 +13,99 @@ import { Shape } from 'three';
import { BuildingShapeUtils } from '../src/extras/BuildingShapeUtils.js';
// import { JSDOM } from 'jsdom';

beforeEach(() => {
errorMsgs = [];
});

describe.each([
[
['<way id="1"><nd ref="1"/><nd ref="2"/><nd ref="3"/><nd ref="4"/></way>',
], 0, 0, 'Single Open Way',
], 0, 0, [], 'Single Open Way',
],
[
[
'<way id="1"><nd ref="1"/><nd ref="2"/></way>',
'<way id="2"><nd ref="2"/><nd ref="3"/></way>',
'<way id="3"><nd ref="3"/><nd ref="1"/></way>',
], 1, 4, 'Test combining 3 ways 1->2->3',
], 1, 4, [], 'Test combining 3 ways 1->2->3',
],
[
[
'<way id="1"><nd ref="1"/><nd ref="2"/></way>',
'<way id="2"><nd ref="3"/><nd ref="1"/></way>',
'<way id="3"><nd ref="2"/><nd ref="3"/></way>',
], 1, 4, 'Test combining 3 ways 2->1->3',
], 1, 4, [], 'Test combining 3 ways 2->1->3',
],
[
[
'<way id="1"><nd ref="1"/><nd ref="2"/></way>',
'<way id="2"><nd ref="3"/><nd ref="2"/></way>',
'<way id="3"><nd ref="3"/><nd ref="1"/></way>',
], 1, 4, 'Test combining tip to tip',
], 1, 4, [], 'Test combining tip to tip',
],
[
[
'<way id="1"><nd ref="1"/><nd ref="2"/></way>',
'<way id="2"><nd ref="1"/><nd ref="3"/></way>',
'<way id="3"><nd ref="2"/><nd ref="3"/></way>',
], 1, 4, 'Test combining tail to tail',
], 1, 4, [], 'Test combining tail to tail',
],
[
[
'<way id="1"><nd ref="1"/><nd ref="2"/></way>',
'<way id="2"><nd ref="3"/><nd ref="4"/></way>',
'<way id="3"><nd ref="4"/><nd ref="1"/></way>',
'<way id="4"><nd ref="2"/><nd ref="3"/></way>',
], 1, 5, 'Test combining 4 ways',
], 1, 5, [], 'Test combining 4 ways',
],
[
[
'<way id="1"><nd ref="1"/><nd ref="2"/></way>',
'<way id="2"><nd ref="3"/><nd ref="2"/></way>',
], 0, 0, 'Test combining 2 open ways into one open way',
], 0, 0, [], 'Test combining 2 open ways into one open way',
],
[
[
'<way id="1"><nd ref="1"/><nd ref="2"/></way>',
'<way id="2"><nd ref="3"/><nd ref="2"/></way>',
'<way id="3"><nd ref="4"/><nd ref="5"/></way>',
], 0, 0, 'Test combining 3 open ways into 2 open ways',
], 0, 0, [], 'Test combining 3 open ways into 2 open ways',
],
[
[
'<way id="1"><nd ref="1"/><nd ref="2"/></way>',
'<way id="2"><nd ref="2"/><nd ref="4"/></way>',
'<way id="3"><nd ref="2"/><nd ref="3"/></way>',
'<way id="4"><nd ref="1"/><nd ref="3"/></way>',
], 1, 4, 'Combining 4 open ways into 1 closed & 1 remaining open way',
], 1, 4, [], 'Combining 4 open ways into 1 closed & 1 remaining open way',
],
[
[
'<way id="1"><nd ref="1"/><nd ref="2"/><nd ref="3"/></way>',
'<way id="2"><nd ref="3"/><nd ref="5"/><nd ref="1"/></way>',
'<way id="3"><nd ref="3"/><nd ref="4"/><nd ref="1"/></way>',
], 1, 5, 'Dealing with amiguity. Only make one closed way',
], 1, 5, [], 'Dealing with amiguity. Only make one closed way',
],
//[
// [
// '<way id="1"><nd ref="1"/><nd ref="2"/><nd ref="3"/></way>',
// '<way id="2"><nd ref="3"/><nd ref="4"/><nd ref="2"/><nd ref="5"/><nd ref="1"/></way>',
// ], 0, 0, [], 'Closed way is self intersecting',
//],
[
[
'<way id="1"><nd ref="1"/><nd ref="2"/><nd ref="3"/></way>',
'<way id="2"><nd ref="3"/><nd ref="4"/><nd ref="2"/><nd ref="5"/><nd ref="1"/></way>',
], 0, 0, 'Closed way is self intersecting',
'<way id="1"><nd ref="1"/><nd ref="2"/></way>',
'<way id="2"><nd ref="2"/><nd ref="3"/><nd ref="4"/><nd ref="5"/><nd ref="3"/><nd ref="1"/></way>',
], 0, 0, ['Way 2 is self-intersecting'], 'Open way is self intersecting',
],
[
[
'<way id="1"><nd ref="1"/><nd ref="2"/></way>',
'<way id="2"><nd ref="2"/><nd ref="3"/><nd ref="4"/><nd ref="5"/><nd ref="3"/><nd ref="1"/></way>',
], 0, 0, 'Open way is self intersecting',
'<way id="3"><nd ref="2"/><nd ref="3"/><nd ref="1"/></way>',
], 1, 4, ['Way 2 is self-intersecting'], 'Open way is self intersecting, but ring formed',
],
])('Combine Ways', (ways, length, nodes, description) => {
])('Combine Ways', (ways, length, nodes, errors, description) => {
test(`${description}`, () => {
let parser = new window.DOMParser();
const xml = [];
Expand All @@ -103,10 +114,24 @@ describe.each([
}
let result = BuildingShapeUtils.combineWays(xml);
expect(result.length).toBe(length);
expect(errorMsgs.length).toBe(errors.length);
if (errors.length) {
for (const error of errors) {
expect(errorMsgs.shift()).toBe(error);
}
}
if (length) {
expect(BuildingShapeUtils.isClosed(result[0]));
expect(BuildingShapeUtils.isSelfIntersecting(result[0])).toBe(false);
expect(result[0].getElementsByTagName('nd').length).toBe(nodes);
}
});
});

window.printError = printError;

var errorMsgs = [];

function printError(txt) {
errorMsgs.push(txt);
}