Fix incorrect order of points

#41
This commit is contained in:
Casper Lamboo 2018-03-06 17:01:52 +01:00
parent 16e6e11e2f
commit 8bb97527f4

View File

@ -85,7 +85,9 @@ export default function intersectionsToShapes(layerPoints, layerFaceIndexes, fac
} }
for (const objectIndex in shapes) { for (const objectIndex in shapes) {
const shape = shapes[objectIndex].map(lineSegment => lineSegment.map(pointIndex => points[pointIndex])); const shape = shapes[objectIndex]
.map(lineSegment => lineSegment.map(pointIndex => points[pointIndex]))
.filter(lineSegment => lineSegment.some(i => !almostEquals(lineSegment[0], lineSegment[1])));
const openShape = openObjectIndexes[objectIndex]; const openShape = openObjectIndexes[objectIndex];
const connectPoints = []; const connectPoints = [];
@ -93,46 +95,49 @@ export default function intersectionsToShapes(layerPoints, layerFaceIndexes, fac
const path = shape[pathIndex]; const path = shape[pathIndex];
if (almostEquals(path[0], path[path.length - 1])) { if (almostEquals(path[0], path[path.length - 1])) {
if (path.some(point => !almostEquals(point, path[0]))) lineShapesClosed.push(path); lineShapesClosed.push(path);
continue; continue;
} }
let shapeStartPoint = path[0]; let shapeStartPoint = path[0];
for (const point of connectPoints) { const connectStart = connectPoints.find(({ point }) => almostEquals(point, shapeStartPoint));
if (almostEquals(point.point, shapeStartPoint)) { if (connectStart) {
point.start = pathIndex; connectStart.start = pathIndex;
shapeStartPoint = null; } else {
break; connectPoints.push({ point: shapeStartPoint, start: pathIndex, end: -1 });
} }
}
if (shapeStartPoint) connectPoints.push({ point: shapeStartPoint, start: pathIndex, end: null });
let shapeEndPoint = path[path.length - 1]; let shapeEndPoint = path[path.length - 1];
for (const point of connectPoints) { const connectEnd = connectPoints.find(({ point }) => almostEquals(point, shapeEndPoint));
if (almostEquals(point.point, shapeEndPoint)) { if (connectEnd) {
point.end = pathIndex; connectEnd.end = pathIndex;
shapeEndPoint = null; } else {
break; connectPoints.push({ point: shapeEndPoint, start: -1, end: pathIndex });
} }
} }
if (shapeEndPoint) connectPoints.push({ point: shapeEndPoint, start: null, end: pathIndex });
} connectPoints.sort(({ start }) => start);
while (connectPoints.length !== 0) { while (connectPoints.length !== 0) {
let { start, end } = connectPoints.pop(); const { start, end } = connectPoints.pop();
const line = []; const line = [];
if (start !== null) line.push(...shape[start]); if (end !== -1) line.push(...shape[end]);
let newPoint; let next = start;
while (end !== null && (newPoint = connectPoints.find(point => point.start === end))) { while (true) {
line.push(...shape[newPoint.start]); const pointIndex = connectPoints.findIndex(point => point.end === next);
connectPoints.splice(connectPoints.indexOf(newPoint), 1); if (pointIndex === -1) break;
if (newPoint.end !== null && newPoint.end === start) break; const point = connectPoints[pointIndex];
line.push(...shape[point.end]);
end = newPoint.end; connectPoints.splice(pointIndex, 1);
start = newPoint.start;
if (point.start === -1) break;
if (point.start === end) break;
next = point.start;
} }
if (openShape) { if (openShape) {