Doodle3D-Slicer/src/sliceActions/intersectionsToShapes.js

126 lines
3.7 KiB
JavaScript
Raw Normal View History

2017-07-18 12:38:03 +02:00
import * as THREE from 'three';
2017-07-20 11:51:48 +02:00
import Shape from 'clipper-js';
export default function intersectionsToShapes(layerIntersectionIndexes, layerIntersectionPoints, lines, settings) {
2016-04-23 00:24:01 +02:00
const layers = [];
2016-04-23 00:24:01 +02:00
for (let layer = 1; layer < layerIntersectionIndexes.length; layer ++) {
const intersectionIndexes = layerIntersectionIndexes[layer];
const intersectionPoints = layerIntersectionPoints[layer];
2016-04-23 00:24:01 +02:00
if (intersectionIndexes.length === 0) continue;
const fillShapes = [];
const lineShapesOpen = [];
const lineShapesClosed = [];
2016-04-23 00:24:01 +02:00
for (let i = 0; i < intersectionIndexes.length; i ++) {
let index = intersectionIndexes[i];
2017-06-22 10:19:15 +02:00
if (typeof intersectionPoints[index] === 'undefined') continue;
2016-04-23 00:24:01 +02:00
const shape = [];
2016-04-23 00:24:01 +02:00
const firstPoints = [index];
2017-07-18 10:34:20 +02:00
const { openGeometry } = lines[index];
2016-04-23 00:24:01 +02:00
let isFirstPoint = true;
let openShape = true;
while (index !== -1) {
2016-04-23 00:24:01 +02:00
const intersection = intersectionPoints[index];
// uppercase X and Y because clipper vector
2016-04-21 22:14:22 +02:00
shape.push(intersection);
delete intersectionPoints[index];
2016-04-23 00:24:01 +02:00
const connects = lines[index].connects;
const faceNormals = lines[index].normals;
2016-04-23 00:24:01 +02:00
for (let i = 0; i < connects.length; i ++) {
index = connects[i];
if (firstPoints.includes(index) && shape.length > 2) {
openShape = false;
index = -1;
break;
}
// Check if index has an intersection or is already used
2017-06-22 10:19:15 +02:00
if (typeof intersectionPoints[index] !== 'undefined') {
2016-04-23 00:24:01 +02:00
const faceNormal = faceNormals[Math.floor(i / 2)];
2016-04-23 00:24:01 +02:00
const a = new THREE.Vector2(intersection.x, intersection.y);
const b = new THREE.Vector2(intersectionPoints[index].x, intersectionPoints[index].y);
// can't calculate normal between points if distance is smaller as 0.0001
if ((faceNormal.x === 0 && faceNormal.y === 0) || a.distanceTo(b) < 0.0001) {
if (isFirstPoint) {
firstPoints.push(index);
}
delete intersectionPoints[index];
2016-04-23 00:24:01 +02:00
connects.push(...lines[index].connects);
faceNormals.push(...lines[index].normals);
index = -1;
2016-04-23 00:24:01 +02:00
} else {
// make sure the path goes the right direction
// THREE.Vector2.normal is not yet implimented
2016-04-23 00:24:01 +02:00
// const normal = a.sub(b).normal().normalize();
const normal = a.sub(b);
normal.set(-normal.y, normal.x).normalize();
if (normal.dot(faceNormal) > 0) {
break;
2016-04-23 00:24:01 +02:00
} else {
index = -1;
}
}
2016-04-23 00:24:01 +02:00
} else {
index = -1;
}
}
isFirstPoint = false;
}
if (openShape) {
2016-04-23 00:24:01 +02:00
index = firstPoints[0];
while (index !== -1) {
2017-05-26 17:14:14 +02:00
if (!firstPoints.includes(index)) {
2016-04-23 00:24:01 +02:00
const intersection = intersectionPoints[index];
2016-04-21 22:14:22 +02:00
shape.unshift(intersection);
delete intersectionPoints[index];
}
2016-04-23 00:24:01 +02:00
const connects = lines[index].connects;
2016-04-23 00:24:01 +02:00
for (let i = 0; i < connects.length; i ++) {
index = connects[i];
2017-06-22 10:19:15 +02:00
if (typeof intersectionPoints[index] !== 'undefined') {
break;
2016-04-23 00:24:01 +02:00
} else {
index = -1;
}
}
}
}
if (openGeometry) {
if (openShape) {
lineShapesOpen.push(shape);
} else {
lineShapesClosed.push(shape);
}
} else {
fillShapes.push(shape);
}
}
layers.push({ fillShapes, lineShapesOpen, lineShapesClosed });
}
2016-04-21 22:14:22 +02:00
return layers;
}