feat: stuff

This commit is contained in:
2024-03-03 13:23:44 +01:00
parent c42656f243
commit 805ec392aa
9 changed files with 1787 additions and 85 deletions

1
.envrc Normal file
View File

@@ -0,0 +1 @@
use flake

130
flake.lock generated Normal file
View File

@@ -0,0 +1,130 @@
{
"nodes": {
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1709126324,
"narHash": "sha256-q6EQdSeUZOG26WelxqkmR7kArjgWCdw5sfJVHPH/7j8=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "d465f4819400de7c8d874d50b982301f28a84605",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"flake-utils_2": {
"inputs": {
"systems": "systems_2"
},
"locked": {
"lastModified": 1705309234,
"narHash": "sha256-uNRRNRKmJyCRC/8y1RqBkqWBLM034y4qN7EprSdmgyA=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "1ef2e671c3b0c19053962c07dbda38332dcebf26",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1709386671,
"narHash": "sha256-VPqfBnIJ+cfa78pd4Y5Cr6sOWVW8GYHRVucxJGmRf8Q=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "fa9a51752f1b5de583ad5213eb621be071806663",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1706487304,
"narHash": "sha256-LE8lVX28MV2jWJsidW13D2qrHU/RUUONendL2Q/WlJg=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "90f456026d284c22b3e3497be980b2e47d0b28ac",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs",
"rust-overlay": "rust-overlay"
}
},
"rust-overlay": {
"inputs": {
"flake-utils": "flake-utils_2",
"nixpkgs": "nixpkgs_2"
},
"locked": {
"lastModified": 1709431943,
"narHash": "sha256-CqTcEJGITB3rfSuAcWC1QZnbVnIipXmIDbZHfxsAy80=",
"owner": "oxalica",
"repo": "rust-overlay",
"rev": "362184acf4a991f27fc222864e94a2e81b3c3c9f",
"type": "github"
},
"original": {
"owner": "oxalica",
"repo": "rust-overlay",
"type": "github"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
},
"systems_2": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

60
flake.nix Normal file
View File

@@ -0,0 +1,60 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
rust-overlay.url = "github:oxalica/rust-overlay";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = {
self,
nixpkgs,
flake-utils,
rust-overlay,
}:
flake-utils.lib.eachDefaultSystem (system: let
overlays = [(import rust-overlay)];
pkgs = import nixpkgs {inherit system overlays;};
rust-bin = pkgs.rust-bin.stable.latest.default.override {
extensions = ["rust-src" "rust-std" "clippy" "rust-analyzer"];
};
fontMin = pkgs.python311.withPackages (ps: with ps; [brotli fonttools] ++ (with fonttools.optional-dependencies; [woff]));
tauriPkgs = nixpkgs.legacyPackages.${system};
libraries = with tauriPkgs; [
webkitgtk
gtk3
cairo
gdk-pixbuf
glib
dbus
openssl_3
librsvg
];
packages =
(with pkgs; [
nodejs_18
nodePackages.pnpm
rust-bin
fontMin
])
++ (with tauriPkgs; [
curl
wget
pkg-config
dbus
openssl_3
glib
gtk3
libsoup
webkitgtk
librsvg
# serial plugin
udev
]);
in {
devShell = pkgs.mkShell {
buildInputs = packages;
shellHook = ''
export LD_LIBRARY_PATH=${pkgs.lib.makeLibraryPath libraries}:$LD_LIBRARY_PATH
'';
};
});
}

View File

@@ -15,19 +15,22 @@
"@sveltejs/adapter-auto": "^3.0.0",
"@sveltejs/kit": "^2.0.0",
"@sveltejs/vite-plugin-svelte": "^3.0.0",
"@types/three": "^0.159.0",
"prettier": "^3.1.1",
"prettier-plugin-svelte": "^3.1.2",
"svelte": "^4.2.7",
"svelte-check": "^3.6.0",
"tslib": "^2.4.1",
"typescript": "^5.0.0",
"vite": "^5.0.3",
"@types/three": "^0.159.0"
"vite": "^5.0.3"
},
"type": "module",
"dependencies": {
"three": "^0.159.0",
"@dimforge/rapier3d": "^0.12.0",
"@threlte/core": "^7.1.2",
"@threlte/extras": "^8.8.1"
"@threlte/extras": "^8.8.1",
"three": "^0.159.0",
"three-mesh-bvh": "^0.7.3",
"vite-plugin-wasm": "^3.3.0"
}
}

1335
pnpm-lock.yaml generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,8 +1,8 @@
<script lang="ts">
import { Canvas } from '@threlte/core'
import Scene from './Scene.svelte'
import { Canvas } from '@threlte/core';
import Scene from './Scene.svelte';
</script>
<Canvas>
<Scene />
<Scene />
</Canvas>

View File

@@ -1,81 +1,253 @@
<script lang="ts">
import { T } from '@threlte/core'
import { ContactShadows, Float, Grid, OrbitControls } from '@threlte/extras'
import { T } from '@threlte/core';
import { Gizmo, Grid, OrbitControls } from '@threlte/extras';
import { STLLoader } from 'three/examples/jsm/loaders/STLLoader';
import { useLoader } from '@threlte/core';
import {
BufferGeometry,
Float32BufferAttribute,
MathUtils,
Vector3,
Mesh,
Points,
Triangle,
DoubleSide
} from 'three';
import { degToRad } from 'three/src/math/MathUtils.js';
import { MeshBVH } from 'three-mesh-bvh';
import type { Readable } from 'svelte/store';
export let buildSurface = [300, 300, 300];
export let layerHeight = 0.2;
export let nozzleSize = 0.4;
export let tolerance = 0.005;
export let maxNonPlanarAngle = MathUtils.degToRad(20);
export let bedNormal = new Vector3(0, 0, 1);
export let origin = new Vector3(150, 150, 0);
const stl: Readable<BufferGeometry> = useLoader(STLLoader).load('/benchy.stl');
let mesh: Mesh;
let surface: BufferGeometry | undefined;
$: if ($stl) {
//slice(mesh);
// we don't really care about the faces, since the vertices bound the area anyways
// which is the only thing that matters when creating non-planar slices.
// sort vertices by z, then x, then y in separate index arrays
// add face index that maps an index/vertex to multiple faces (indices)
// on each layer get the closest vertex in z, then find the next closest and
// determine the angle between them. If it's less than the maxNonPlanarAngle
// add it to the current slice and set it to consumed, if not
// build bvh for the mesh, query the clostest point.
// only store the indices for each slice.
// query the mesh bvh for the closest point while discarding points not
// in the slice. Keep track of candidates while querying the bvh.
// need to build bvh live while generating the slices, so angle checks can be done with
// respect to the closest point in the slice
const bvh = new MeshBVH($stl);
let indices: [number, number, number][] = [];
const positions = $stl.getAttribute('position');
console.log($stl.index);
const triangle = new Triangle();
const normal = new Vector3();
for (let i = 0; i < $stl.index!.count; i += 3) {
triangle.setFromAttributeAndIndices(
positions,
$stl.index!.array[i],
$stl.index!.array[i + 1],
$stl.index!.array[i + 2]
);
triangle.getNormal(normal);
const angle = normal.angleTo(bedNormal);
if ((angle > Math.PI / 2 ? Math.PI - angle : angle) < maxNonPlanarAngle) {
indices.push([$stl.index!.array[i], $stl.index!.array[i + 1], $stl.index!.array[i + 2]]);
}
}
const pointIndex = Array.from({ length: 3 }, (_, j) =>
Array.from({ length: positions.count }, (_, i) => i).sort(
(a, b) => positions.array[a * 3 + j] - positions.array[b * 3 + j]
)
);
function findNearby(i: number): number[] {
const a = [positions.array[i * 3], positions.array[i * 3 + 1], positions.array[i * 3 + 2]];
const ia = [-1, -1, -1];
// binary search for the closest points in x, y and z
for (let j = 0; j < 3; j++) {
let d = Math.floor(pointIndex[j].length / 2);
inner: while (d / 2 >= 1) {
const value = positions.array[pointIndex[j][d] * 3 + j];
const diff = value - a[j];
if (Math.abs(diff) < tolerance) {
ia[j] = d;
break inner;
} else if (value < a[j]) {
d = Math.floor(d / 2);
} else {
d = Math.floor(d + d / 2);
}
}
if (ia[j] === -1) return [];
}
while ()
}
bvh.shapecast({
intersectsBounds(box, isLeaf, score, depth, nodeIndex) {
// TODO
},
intersectsTriangle(triangle, triangleIndex, contained, depth) {
// TODO
}
})
const connectedPoints: number[] = Array.from({ length: positions.count });
let connection = 0;
for (let i = 0; i < connectedPoints.length; i++) {
if (connectedPoints[i] !== undefined) continue;
connectedPoints[i] = i;
let connected;
while ((connected = connectedPoints[pointIndex[0][connection]]) !== undefined) {
connectedPoints[pointIndex[0][connection]] = i;
connection++;
}
connection++;
}
const faceConnections = new Map<number, number[]>();
function spatialHash(i: number) {
return (
(positions.getX(i) * 19349663) ^
(positions.getY(i) * 83492791) ^
(positions.getZ(i) * 73856093)
);
}
for (let faceIndex = 0; faceIndex < indices.length; faceIndex++) {
let surface: number[] | undefined = undefined;
const values = indices[faceIndex].map((i) => {
const hash = spatialHash(i);
const value = faceConnections.get(hash);
surface ??= value;
return [hash, value] as const;
});
surface ??= [];
surface.push(faceIndex);
for (const [hash, original] of values) {
faceConnections.set(hash, surface);
if (original && original !== surface) {
surface.concat(original);
}
}
}
const surfaceSet = new Set(faceConnections.values());
const iterator = surfaceSet.values();
const surfaces = Array.from({ length: surfaceSet.size }, () => {
const value = iterator.next().value;
return Array.from(
{ length: value.length * 3 },
(_, i) => indices[value[Math.floor(i / 3)]][i % 3]
);
});
surface = new BufferGeometry();
surface.setAttribute('position', positions);
surface.setAttribute('normal', $stl.getAttribute('normal'));
surface.setIndex(surfaces[1].flat());
/*const hull: [position: Vector3, index: number][][] = [];
let limit = 0;
while (points.length > 0) {
const consumed = points.map(() => false);
const currentHull: [position: Vector3, index: number][] = [[points[0][0], 0]];
consumed[0] = true;
for (let i = 1; i < points.length; i++) {
inner: do {
const b = points[i][0].clone().sub(currentHull[currentHull.length - 1][0]);
const angle = Math.asin(
Math.abs(b.clone().dot(bedNormal)) /
(Math.abs(b.length()) * Math.abs(bedNormal.length()))
);
if (angle <= maxNonPlanarAngle) {
currentHull.push([points[i][0], points[i][2]]);
consumed[i] = true;
break inner;
} else if (points[i][0].z < currentHull[currentHull.length - 1][0].z) {
consumed[currentHull.pop()![1]] = false;
if (currentHull.length === 0) {
currentHull.push([points[i][0], points[i][2]]);
consumed[i] = true;
break inner;
}
} else {
break inner;
}
} while (true);
}
points = points.filter((_, j) => !consumed[j]);
hull.push(currentHull);
if (limit++ > 100) break;
}
console.log(hull);*/
}
async function slice(mesh: Mesh) {
const { World, ColliderDesc, Ray } = await import('@dimforge/rapier3d');
console.log(mesh.geometry.getAttribute('indices'));
const positions = mesh.geometry.getAttribute('position');
const collider = ColliderDesc.trimesh(
new Float32Array(positions.array),
new Uint32Array(
mesh.geometry.index?.array ?? Array.from({ length: positions.count }, (_, i) => i)
)
);
collider.setTranslation(mesh.position.x, mesh.position.y, mesh.position.z);
collider.setRotation(mesh.quaternion);
collider.shape;
const rayNormal = new Vector3(0, 0, 1);
const shapePos = new Vector3(0, 0, 0);
const shapeRot = { x: 0, y: 0, z: 0, w: 0 };
console.log(((buildSurface[0] / nozzleSize) * buildSurface[1]) / nozzleSize);
for (let x = 0; x < buildSurface[0]; x += nozzleSize) {
for (let y = 0; y < buildSurface[1]; y += nozzleSize) {
// collider.shape.castRay(new Ray({ x, y, z: 0 }, rayNormal), shapePos, shapeRot, 20, false);
}
}
}
</script>
<T.PerspectiveCamera
makeDefault
position={[-10, 10, 10]}
fov={15}
>
<OrbitControls
autoRotate
enableZoom={false}
enableDamping
autoRotateSpeed={0.5}
target.y={1.5}
/>
<T.PerspectiveCamera makeDefault position={buildSurface} fov={60} up={[0, 0, 1]}>
<OrbitControls enableDamping target.y={1.5} />
</T.PerspectiveCamera>
<T.DirectionalLight
intensity={0.8}
position.x={5}
position.y={10}
/>
<T.AmbientLight intensity={0.2} />
<Gizmo />
<Grid
position.y={-0.001}
cellColor="#ffffff"
sectionColor="#ffffff"
sectionThickness={0}
fadeDistance={25}
cellSize={2}
position.y={-0.001}
cellColor="#ffffff"
sectionColor="#ff1111"
fadeDistance={buildSurface[0] * 2}
cellSize={10}
sectionSize={0}
plane="xy"
gridSize={[buildSurface[0], buildSurface[1]]}
/>
<ContactShadows
scale={10}
blur={2}
far={2.5}
opacity={0.5}
/>
{#if surface}
<T.Mesh geometry={surface}>
<T.MeshNormalMaterial side={DoubleSide} />
</T.Mesh>
{/if}
<Float
floatIntensity={1}
floatingRange={[0, 1]}
>
<T.Mesh
position.y={1.2}
position.z={-0.75}
>
<T.BoxGeometry />
<T.MeshStandardMaterial color="#0059BA" />
</T.Mesh>
</Float>
<Float
floatIntensity={1}
floatingRange={[0, 1]}
>
<T.Mesh
position={[1.2, 1.5, 0.75]}
rotation.x={5}
rotation.y={71}
>
<T.TorusKnotGeometry args={[0.5, 0.15, 100, 12, 2, 3]} />
<T.MeshStandardMaterial color="#F85122" />
</T.Mesh>
</Float>
<Float
floatIntensity={1}
floatingRange={[0, 1]}
>
<T.Mesh
position={[-1.4, 1.5, 0.75]}
rotation={[-5, 128, 10]}
>
<T.IcosahedronGeometry />
<T.MeshStandardMaterial color="#F8EBCE" />
</T.Mesh>
</Float>
{#if $stl && false}
<T.Mesh geometry={$stl} bind:ref={mesh}>
<T.MeshNormalMaterial />
</T.Mesh>
{/if}

BIN
static/benchy.stl Normal file

Binary file not shown.

View File

@@ -1,9 +1,10 @@
import { sveltekit } from '@sveltejs/kit/vite'
import { defineConfig } from 'vite'
import { sveltekit } from '@sveltejs/kit/vite';
import wasm from 'vite-plugin-wasm';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [sveltekit()],
ssr: {
noExternal: ['three']
}
})
plugins: [sveltekit(), wasm()],
ssr: {
noExternal: ['three']
}
});