UNPKG

awv3

Version:
78 lines (64 loc) 2.46 kB
import { calculateIntersection, calculateInCenter, roundVector } from "./calculations"; export function getPositionFromSelection(selectedMaterials) { var position = undefined; var meta = selectedMaterials.map(function (m) { return { type: m.meta.type, start: m.meta.start, end: m.meta.end, position: m.meta.position, center: m.meta.center }; }); switch (meta.length) { case 1: { if (meta[0].type === 'point') { // 1 vertex => take position position = meta[0].position; } else if (meta[0].type === 'line') { // TODO: detect b-spline-curve, it doesn't work with them // 1 line => calculate midpoint position = meta[0].start.clone().add(meta[0].end).divideScalar(2); } else if (meta[0].type === 'arc') { // 1 arc => take center position = meta[0].center; } break; } case 2: { if (meta[0].type === 'point' && meta[1].type === 'point') { // 2 vertices => calculate midpoint position = meta[0].position.clone().add(meta[1].position).divideScalar(2); } else if (meta[0].type === 'line' && meta[1].type === 'line') { // TODO: detect b-spline-curve, it doesn't work with them // 2 lines => calculate intersection var intersectionPoint = calculateIntersection(meta[0].start, meta[0].end, meta[1].start, meta[1].end); if (intersectionPoint === undefined) { console.log('%c no intersection found', 'color: orange'); break; } // round intersection point coordinates to match json protocol accuracy intersectionPoint = roundVector(intersectionPoint); position = intersectionPoint; } break; } case 3: { if (meta[0].type === 'line' && meta[1].type === 'line' && meta[2].type === 'line') { // TODO: detect b-spline-curve, it doesn't work with them // 3 lines => calculate incenter (Inkreismittelpunkt) var inCenter = calculateInCenter(meta[0].start, meta[0].end, meta[1].start, meta[1].end, meta[2].start, meta[2].end); if (inCenter === undefined) { console.log('%c no in center found', 'color: orange'); break; } position = inCenter; } break; } default: // leave position as it is } return position; }