mirror of
https://github.com/Doodle3D/Doodle3D-Slicer.git
synced 2024-11-24 14:37:58 +01:00
performance
This commit is contained in:
parent
6c8b8e9d44
commit
e966bc89b2
@ -1,14 +1,14 @@
|
|||||||
import { subtract, add, normalize, dot, distanceTo, divide, normal } from './vector2.js';
|
import { subtract, add, normalize, dot, distanceTo, divide, normal } from './vector2.js';
|
||||||
import earcut from 'earcut';
|
import earcut from 'earcut';
|
||||||
|
|
||||||
// const TRIANGULATED_OUTLINES = new WeakMap();
|
const TRIANGULATED_OUTLINES = new WeakMap();
|
||||||
|
|
||||||
export default function comb(outline, start, end) {
|
export default function comb(outline, start, end) {
|
||||||
if (distanceTo(start, end) < 10) return [start, end];
|
if (distanceTo(start, end) < 10) return [start, end];
|
||||||
|
|
||||||
// if (!TRIANGULATED_OUTLINES.has(outline)) TRIANGULATED_OUTLINES.set(outline, decompose(outline));
|
if (!TRIANGULATED_OUTLINES.has(outline)) TRIANGULATED_OUTLINES.set(outline, decompose(outline));
|
||||||
// const { convexPolygons, vertices } = TRIANGULATED_OUTLINES.get(outline);
|
const { convexPolygons, vertices } = TRIANGULATED_OUTLINES.get(outline);
|
||||||
const { convexPolygons, vertices } = decompose(outline);
|
|
||||||
const startPolygon = convexPolygons.findIndex(({ face }) => pointIsInsideConvex(start, face, vertices));
|
const startPolygon = convexPolygons.findIndex(({ face }) => pointIsInsideConvex(start, face, vertices));
|
||||||
const endPolygon = convexPolygons.findIndex(({ face }) => pointIsInsideConvex(end, face, vertices));
|
const endPolygon = convexPolygons.findIndex(({ face }) => pointIsInsideConvex(end, face, vertices));
|
||||||
if (startPolygon === -1 || endPolygon === -1) return [start, end];
|
if (startPolygon === -1 || endPolygon === -1) return [start, end];
|
||||||
@ -16,8 +16,8 @@ export default function comb(outline, start, end) {
|
|||||||
|
|
||||||
const path = findClosestPath(convexPolygons, startPolygon, endPolygon);
|
const path = findClosestPath(convexPolygons, startPolygon, endPolygon);
|
||||||
if (!path) return [start, end];
|
if (!path) return [start, end];
|
||||||
const line = containLineInPath(path, start, end, vertices);
|
|
||||||
|
|
||||||
|
const line = containLineInPath(path, start, end, vertices);
|
||||||
return line;
|
return line;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -113,100 +113,102 @@ function decompose(polygon) {
|
|||||||
return { vertices, convexPolygons };
|
return { vertices, convexPolygons };
|
||||||
}
|
}
|
||||||
|
|
||||||
function findClosestPath(convexPolygons, start, end, visited = [], path = []) {
|
// const distanceMap = new WeakMap();
|
||||||
if (start === end) return [];
|
// function findClosestPath(convexPolygons, start, end, visited = [], path = [], distance = 0) {
|
||||||
|
// if (start === end) return [];
|
||||||
visited = [...visited, start];
|
|
||||||
|
|
||||||
const { connects } = convexPolygons[start];
|
|
||||||
|
|
||||||
const finish = connects.find(({ to }) => to === end);
|
|
||||||
if (finish) return [...path, finish];
|
|
||||||
|
|
||||||
const posibilities = [];
|
|
||||||
for (const connect of connects) {
|
|
||||||
if (visited.includes(connect.to)) continue;
|
|
||||||
|
|
||||||
const posibility = findClosestPath(convexPolygons, connect.to, end, visited, [...path, connect]);
|
|
||||||
if (posibility) posibilities.push(posibility);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (posibilities.length === 0) {
|
|
||||||
return null;
|
|
||||||
} else if (posibilities.length === 1) {
|
|
||||||
return posibilities[0];
|
|
||||||
} else if (posibilities.length > 1) {
|
|
||||||
const distanceMap = new WeakMap();
|
|
||||||
for (const posibility of posibilities) {
|
|
||||||
const distance = posibility.reduce((totalDistance, connect) => totalDistance + connect.distance, 0);
|
|
||||||
distanceMap.set(posibility, distance);
|
|
||||||
}
|
|
||||||
|
|
||||||
return posibilities.sort((a, b) => distanceMap.get(a) - distanceMap.get(b))[0];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// const parse = string => parseFloat(string);
|
|
||||||
// function findClosestPath(map, start, end) {
|
|
||||||
// // dijkstra's algorithm
|
|
||||||
// const costs = { [start]: 0 };
|
|
||||||
// const open = { [0]: [start] };
|
|
||||||
// const predecessors = {};
|
|
||||||
//
|
//
|
||||||
// while (open) {
|
// visited = [...visited, start];
|
||||||
// const keys = Object.keys(open).map(parse);
|
|
||||||
// if (keys.length === 0) break;
|
|
||||||
// keys.sort();
|
|
||||||
//
|
//
|
||||||
// const [key] = keys;
|
// const { connects } = convexPolygons[start];
|
||||||
// const bucket = open[key];
|
|
||||||
// const node = bucket.shift();
|
|
||||||
// const currentCost = key;
|
|
||||||
// const { connects } = map[node];
|
|
||||||
//
|
//
|
||||||
// if (!bucket.length) delete open[key];
|
// const finish = connects.find(({ to }) => to === end);
|
||||||
|
// if (finish) return [...path, finish];
|
||||||
//
|
//
|
||||||
// for (const { distance, to } of connects) {
|
// const posibilities = [];
|
||||||
// const totalCost = distance + currentCost;
|
// for (let i = 0; i < connects.length; i ++) {
|
||||||
// const vertexCost = costs[to];
|
// const connect = connects[i];
|
||||||
|
// if (visited.includes(connect.to)) continue;
|
||||||
//
|
//
|
||||||
// if ((typeof vertexCost === 'undefined') || (vertexCost > totalCost)) {
|
// const positibiltyDistance = distance + connect.distance;
|
||||||
// costs[to] = totalCost;
|
// const posibility = findClosestPath(convexPolygons, connect.to, end, visited, [...path, connect], positibiltyDistance);
|
||||||
//
|
// if (posibility) {
|
||||||
// if (!open[totalCost]) open[totalCost] = [];
|
// posibilities.push(posibility);
|
||||||
// open[totalCost].push(to);
|
// distanceMap.set(posibility, positibiltyDistance);
|
||||||
//
|
|
||||||
// predecessors[to] = node;
|
|
||||||
// }
|
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// if (typeof costs[end] === 'undefined') return null;
|
// if (posibilities.length === 0) {
|
||||||
//
|
// return null;
|
||||||
// const nodes = [];
|
// } else if (posibilities.length === 1) {
|
||||||
// let node = end;
|
// return posibilities[0];
|
||||||
// while (typeof node !== 'undefined') {
|
// } else if (posibilities.length > 1) {
|
||||||
// nodes.push(node);
|
// return posibilities.sort((a, b) => distanceMap.get(a) - distanceMap.get(b))[0];
|
||||||
// node = predecessors[node];
|
|
||||||
// }
|
// }
|
||||||
// nodes.reverse();
|
|
||||||
//
|
|
||||||
// const path = [];
|
|
||||||
// for (let i = 1; i < nodes.length; i ++) {
|
|
||||||
// const from = nodes[i - 1];
|
|
||||||
// const to = nodes[i];
|
|
||||||
//
|
|
||||||
// const connection = map[from].connects.find(connect => connect.to === to);
|
|
||||||
// path.push(connection);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// return path;
|
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
const findKey = _key => ({ key }) => _key === key;
|
||||||
|
function findClosestPath(map, start, end) {
|
||||||
|
// dijkstra's algorithm
|
||||||
|
const distances = { [start]: 0 };
|
||||||
|
const open = [{ key: 0, nodes: [start] }];
|
||||||
|
const predecessors = {};
|
||||||
|
|
||||||
|
while (open.length !== 0) {
|
||||||
|
const key = Math.min(...open.map(n => n.key).sort());
|
||||||
|
const bucket = open.find(findKey(key));
|
||||||
|
const node = bucket.nodes.shift();
|
||||||
|
const currentDistance = key;
|
||||||
|
const { connects } = map[node];
|
||||||
|
|
||||||
|
if (bucket.nodes.length === 0) open.splice(open.indexOf(bucket), 1);
|
||||||
|
|
||||||
|
for (let i = 0; i < connects.length; i ++) {
|
||||||
|
const { distance, to } = connects[i];
|
||||||
|
const totalDistance = distance + currentDistance;
|
||||||
|
const vertexDistance = distances[to];
|
||||||
|
|
||||||
|
if ((typeof vertexDistance === 'undefined') || (vertexDistance > totalDistance)) {
|
||||||
|
distances[to] = totalDistance;
|
||||||
|
|
||||||
|
let openNode = open.find(findKey(totalDistance));
|
||||||
|
if (!openNode) {
|
||||||
|
openNode = { key: totalDistance, nodes: [] };
|
||||||
|
open.push(openNode);
|
||||||
|
}
|
||||||
|
openNode.nodes.push(to);
|
||||||
|
|
||||||
|
predecessors[to] = node;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof distances[end] === 'undefined') return null;
|
||||||
|
|
||||||
|
const nodes = [];
|
||||||
|
let node = end;
|
||||||
|
while (typeof node !== 'undefined') {
|
||||||
|
nodes.push(node);
|
||||||
|
node = predecessors[node];
|
||||||
|
}
|
||||||
|
nodes.reverse();
|
||||||
|
|
||||||
|
const path = [];
|
||||||
|
for (let i = 1; i < nodes.length; i ++) {
|
||||||
|
const from = nodes[i - 1];
|
||||||
|
const to = nodes[i];
|
||||||
|
|
||||||
|
const connection = map[from].connects.find(connect => connect.to === to);
|
||||||
|
path.push(connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
function containLineInPath(path, start, end, vertices) {
|
function containLineInPath(path, start, end, vertices) {
|
||||||
const line = [start];
|
const line = [start];
|
||||||
|
|
||||||
for (const { edge: [indexA, indexB] } of path) {
|
for (let i = 0; i < path.length; i ++) {
|
||||||
|
const { edge: [indexA, indexB] } = path[i];
|
||||||
const vertexA = vertices[indexA];
|
const vertexA = vertices[indexA];
|
||||||
const vertexB = vertices[indexB];
|
const vertexB = vertices[indexB];
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user