This commit is contained in:
2024-03-03 21:22:33 +01:00
parent 3f14c5a71f
commit 470d93233f

View File

@@ -13,9 +13,10 @@
Plane, Plane,
Line3, Line3,
Float32BufferAttribute, Float32BufferAttribute,
Box3 Box3,
Matrix4
} from 'three'; } from 'three';
import { ExtendedTriangle, MeshBVH } from 'three-mesh-bvh'; import { ExtendedTriangle, MeshBVH, type HitPointInfo } from 'three-mesh-bvh';
import type { Readable } from 'svelte/store'; import type { Readable } from 'svelte/store';
export let buildSurface = [300, 300, 300]; export let buildSurface = [300, 300, 300];
@@ -110,48 +111,67 @@
indices[i * 3 + 2] = $stl.index!.array[pos + 2]; indices[i * 3 + 2] = $stl.index!.array[pos + 2];
} }
geometry.setIndex(indices); geometry.setIndex(indices);
return new MeshBVH(geometry); const bvh = new MeshBVH(geometry);
geometry.boundsTree = bvh;
return bvh;
}); });
const activeNonPlanarSurfaces: [number, MeshBVH][] = []; const activeNonPlanarSurfaces: [number, MeshBVH][] = [];
const consumedNonPlanarSurfaces = nonPlanarSurfaces.map(() => false); const consumedNonPlanarSurfaces = nonPlanarSurfaces.map(() => false);
const withheldLines: number[][][] = nonPlanarSurfaces.map(() => [[]]); const withheldLines: number[][][] = nonPlanarSurfaces.map(() => [[]]);
const withheldSurfaces: [number, MeshBVH][][] = nonPlanarSurfaces.map(() => []);
const blacklist = Array.from({ length: index.count / 3 }).map(() => false); const blacklist = Array.from({ length: index.count / 3 }).map(() => false);
const line = new Line3(); const line = new Line3();
const targetVector1 = new Vector3(); const targetVector1 = new Vector3();
const targetVector2 = new Vector3(); const targetVector2 = new Vector3();
const targetVector3 = new Vector3(); const targetVector3 = new Vector3();
const hit1: HitPointInfo = { point: new Vector3(), distance: 0, faceIndex: 0 };
const hit2: HitPointInfo = { point: new Vector3(), distance: 0, faceIndex: 0 };
const layerPlane = new Plane(); const layerPlane = new Plane();
const layerBox = new Box3(); function deactivateSurface(surface: MeshBVH, index: number) {
layers.push({ type: 'surface', geometry: surface.geometry });
for (const lines of withheldLines[index]) {
if (lines.length === 0) continue;
const additionalGeometry = new BufferGeometry();
additionalGeometry.setAttribute('position', new Float32BufferAttribute(lines, 3));
layers.push({ type: 'line', geometry: additionalGeometry });
}
for (const surface of withheldSurfaces[index]) {
deactivateSurface(surface[1], surface[0]);
}
delete withheldLines[index];
}
for (let layer = 0; layer < $stl.boundingBox!.max.z; layer += layerHeight) { for (let layer = 0; layer < $stl.boundingBox!.max.z; layer += layerHeight) {
layerPlane.set(bedNormal, -layer); layerPlane.set(bedNormal, -layer);
layerBox.set(
new Vector3(0, 0, layer),
new Vector3(buildSurface[0], buildSurface[1], layer + layerHeight)
);
const layerGeometry = new BufferGeometry(); const layerGeometry = new BufferGeometry();
const positions: number[] = []; const positions: number[] = [];
for (let i = 0; i < nonPlanarSurfaces.length; i++) { for (let i = 0; i < nonPlanarSurfaces.length; i++) {
if (consumedNonPlanarSurfaces[i]) continue; if (consumedNonPlanarSurfaces[i]) continue;
if (layerBox.intersectsBox(nonPlanarSurfaces[i].geometry.boundingBox!)) { if (nonPlanarSurfaces[i].geometry.boundingBox!.min.z > layer) {
activeNonPlanarSurfaces.push([i, nonPlanarSurfaces[i]]);
consumedNonPlanarSurfaces[i] = true; consumedNonPlanarSurfaces[i] = true;
activeNonPlanarSurfaces.push([i, nonPlanarSurfaces[i]]);
} }
} }
for (let i = 0; i < activeNonPlanarSurfaces.length; i++) { deactivate: for (let i = 0; i < activeNonPlanarSurfaces.length; i++) {
const [index, surface] = activeNonPlanarSurfaces[i]; const [index, surface] = activeNonPlanarSurfaces[i];
withheldLines[index].push([]); withheldLines[index].push([]);
if (!layerBox.intersectsBox(surface.geometry.boundingBox!)) { if (surface.geometry.boundingBox!.max.z <= layer) {
activeNonPlanarSurfaces.splice(i, 1); activeNonPlanarSurfaces.splice(i, 1);
i--; i--;
layers.push({ type: 'surface', geometry: surface.geometry });
for (const lines of withheldLines[index]) { for (const [activeIndex, active] of activeNonPlanarSurfaces) {
if (lines.length === 0) continue; if (activeIndex === index) continue;
const additionalGeometry = new BufferGeometry(); const hit = active.closestPointToGeometry(surface.geometry, new Matrix4(), hit1, hit2);
additionalGeometry.setAttribute('position', new Float32BufferAttribute(lines, 3)); if (
layers.push({ type: 'line', geometry: additionalGeometry }); hit &&
hit1.point.z < hit2.point.z &&
hit1.point.clone().sub(hit2.point).angleTo(bedNormal) > maxNonPlanarAngle
) {
withheldSurfaces[activeIndex].push([index, surface]);
continue deactivate;
}
} }
delete withheldLines[index]; deactivateSurface(surface, index);
} }
} }
@@ -207,6 +227,9 @@
layers.push({ type: 'line', geometry: layerGeometry }); layers.push({ type: 'line', geometry: layerGeometry });
console.log('layer', Math.round(layer / layerHeight), positions.length); console.log('layer', Math.round(layer / layerHeight), positions.length);
} }
for (const [index, surface] of activeNonPlanarSurfaces) {
deactivateSurface(surface, index);
}
layers = [...layers]; layers = [...layers];
} }
</script> </script>