calcContoursForLayerSegment = (segments, segIdx) => {
if (segments.length === 0) {
return [];
}
let contours = [];
let currContour = [];
let unvisitedSegments = segments.slice();
let currSegment = unvisitedSegments[0];
let pointToPush = currSegment[0];
let pointForFindingNextSeg = currSegment[1];
while (unvisitedSegments.length > 0) {
currContour.push(pointToPush);
unvisitedSegments.splice(unvisitedSegments.indexOf(currSegment), 1);
let foundNextSegment = unvisitedSegments.some((potentialSegment) => {
if (potentialSegment[0].approxEqual(pointForFindingNextSeg)) {
currSegment = potentialSegment;
pointToPush = potentialSegment[0];
pointForFindingNextSeg = potentialSegment[1];
return true;
}
if (potentialSegment[1].approxEqual(pointForFindingNextSeg)) {
currSegment = potentialSegment;
pointToPush = potentialSegment[1];
pointForFindingNextSeg = potentialSegment[0];
return true;
}
});
if (!foundNextSegment) {
contours.push(currContour.slice());
currContour = [];
if (unvisitedSegments.length > 0) {
currSegment = unvisitedSegments[0];
pointToPush = currSegment[0];
pointForFindingNextSeg = currSegment[1];
}
}
}
return contours;
}