improve intersections to shapes step

#36
This commit is contained in:
casperlamboo 2018-02-01 12:11:57 +01:00
parent 491067b070
commit 64d28affe6

View File

@ -14,32 +14,61 @@ export default function intersectionsToShapes(intersectionLayers, faces, openObj
const shapes = {}; const shapes = {};
const startConnects = {};
const endConnects = {};
for (let i = 0; i < faceIndexes.length; i ++) { for (let i = 0; i < faceIndexes.length; i ++) {
const { lineIndexes, objectIndex, flatNormal } = faces[faceIndexes[i]]; const faceIndex = faceIndexes[i];
const { lineIndexes, flatNormal, objectIndex } = faces[faceIndex];
const a = points[lineIndexes[0]]; const a = lineIndexes[0];
const b = points[lineIndexes[1]]; const b = lineIndexes[1];
const c = points[lineIndexes[2]]; const c = lineIndexes[2];
const lineSegment = []; let pointA;
if (a && b) { let pointB;
lineSegment.push(a, b); if (points[a] && points[b]) {
} else if (b && c) { pointA = a;
lineSegment.push(b, c); pointB = b;
} else if (c && a) { } else if (points[b] && points[c]) {
lineSegment.push(c, a); pointA = b;
pointB = c;
} else if (points[c] && points[a]) {
pointA = c;
pointB = a;
} else { } else {
// should never happen
continue; continue;
} }
const segmentNormal = normalize(normal(subtract(lineSegment[1], lineSegment[0]))); const segmentNormal = normalize(normal(subtract(points[pointA], points[pointB])));
if (dot(segmentNormal, flatNormal) < 0) lineSegment.reverse(); if (dot(segmentNormal, flatNormal) < 0) {
const temp = pointB;
pointB = pointA;
pointA = temp;
}
if (endConnects[pointA]) {
const lineSegment = endConnects[pointA];
lineSegment.push(points[pointB]);
delete endConnects[pointA];
endConnects[pointB] = lineSegment;
} else if (startConnects[pointB]) {
const lineSegment = startConnects[pointB];
lineSegment.unshift(points[pointA]);
delete startConnects[pointB];
startConnects[pointA] = lineSegment;
} else {
const lineSegment = [points[pointA], points[pointB]];
startConnects[pointA] = lineSegment;
endConnects[pointB] = lineSegment;
if (!shapes[objectIndex]) shapes[objectIndex] = { lineSegments: [] }; if (!shapes[objectIndex]) shapes[objectIndex] = { lineSegments: [] };
const shape = shapes[objectIndex]; const shape = shapes[objectIndex];
shape.lineSegments.push(lineSegment) shape.lineSegments.push(lineSegment)
} }
}
for (const objectIndex in shapes) { for (const objectIndex in shapes) {
const shape = shapes[objectIndex]; const shape = shapes[objectIndex];