Doodle3D-Slicer/src/sliceActions/createLines.js

54 lines
1.7 KiB
JavaScript
Raw Normal View History

import * as THREE from 'three.js';
2016-03-29 08:15:30 +02:00
function addLine(geometry, lineLookup, lines, a, b) {
2016-05-06 19:45:03 +02:00
const index = lines.length;
lineLookup[`${a}_${b}`] = index;
2016-03-29 08:15:30 +02:00
2016-05-06 19:45:03 +02:00
lines.push({
line: new THREE.Line3(geometry.vertices[a], geometry.vertices[b]),
connects: [],
normals: []
});
2016-03-29 08:15:30 +02:00
return index;
}
export default function createLines(geometry, settings, openClosed) {
console.log('constructing unique lines from geometry');
const lines = [];
const lineLookup = {};
for (let i = 0; i < geometry.faces.length; i ++) {
const face = geometry.faces[i];
const open = openClosed[i];
if (face.normal.y !== 1 && face.normal.y !== -1) {
const normal = new THREE.Vector2(face.normal.z, face.normal.x).normalize();
2016-05-06 19:45:03 +02:00
const lookupA = lineLookup[`${face.b}_${face.a}`];
const lookupB = lineLookup[`${face.c}_${face.b}`];
const lookupC = lineLookup[`${face.a}_${face.c}`];
// only add unique lines
// returns index of said line
2017-06-22 10:19:15 +02:00
const indexA = typeof lookupA !== 'undefined' ? lookupA : addLine(geometry, lineLookup, lines, face.a, face.b);
const indexB = typeof lookupB !== 'undefined' ? lookupB : addLine(geometry, lineLookup, lines, face.b, face.c);
const indexC = typeof lookupC !== 'undefined' ? lookupC : addLine(geometry, lineLookup, lines, face.c, face.a);
// set connecting lines (based on face)
2016-05-06 19:45:03 +02:00
lines[indexA].connects.push(indexB, indexC);
lines[indexB].connects.push(indexC, indexA);
lines[indexC].connects.push(indexA, indexB);
2016-05-06 19:45:03 +02:00
lines[indexA].normals.push(normal);
lines[indexB].normals.push(normal);
lines[indexC].normals.push(normal);
lines[indexA].open = open;
}
}
return lines;
}