feat: slicing

This commit is contained in:
2024-03-11 16:52:35 +01:00
parent 608c2b08ec
commit dcedfadd3f
5 changed files with 34 additions and 23 deletions

View File

@@ -56,7 +56,7 @@ pub fn slice(
slicable_triangles.push(triangle); slicable_triangles.push(triangle);
} else { } else {
slicable_triangles.push(triangle); slicable_triangles.push(triangle);
surface_triangles.push(triangle); //surface_triangles.push(triangle);
} }
} }
slicable_triangles.shrink_to_fit(); slicable_triangles.shrink_to_fit();
@@ -65,7 +65,7 @@ pub fn slice(
let slicer_options = SlicerOptions { layer_height }; let slicer_options = SlicerOptions { layer_height };
console_log!("Creating Surfaces"); console_log!("Creating Surfaces");
let surfaces = split_surface(surface_triangles); // let surfaces = split_surface(surface_triangles);
console_log!("Computing BVH"); console_log!("Computing BVH");
let slicable = Mesh::from(slicable_triangles); let slicable = Mesh::from(slicable_triangles);

View File

@@ -40,6 +40,8 @@ pub fn create_slices(options: &SlicerOptions, slicable: &Mesh<f32>) -> Vec<Slice
child_r_index, child_r_index,
child_r_aabb, child_r_aabb,
} => { } => {
assert!(child_l_aabb.min.z <= child_l_aabb.max.z);
assert!(child_r_aabb.min.z <= child_r_aabb.max.z);
if layer >= child_l_aabb.min.z && layer <= child_l_aabb.max.z { if layer >= child_l_aabb.min.z && layer <= child_l_aabb.max.z {
stack.push(child_l_index); stack.push(child_l_index);
} }

View File

@@ -22,28 +22,29 @@ pub fn find_slice_rings(mut slice: BaseSlice) -> Vec<SliceRing> {
let mut previous_len = usize::MAX; let mut previous_len = usize::MAX;
while relative_ne!(ring.points[0], right) { while relative_ne!(ring.points[0], right) {
if previous_len == ring.points.len() { if previous_len == slice.lines.len() {
console_log!( console_log!(
"Error: Could not find a ring for slice at z = {}, {} items left.", "Error: Could not find a ring for slice at z = {}, {} items left.",
slice.z, slice.z,
ring.points.len() slice.lines.len()
); );
break; break;
} }
previous_len = ring.points.len(); previous_len = slice.lines.len();
slice.lines.retain_mut(|line| { slice.lines.retain_mut(|line| {
if relative_eq!(line.start, right, epsilon = 0.001) { //if relative_eq!(line.start, right, epsilon = 0.001) {
ring.points.push(line.end); ring.points.push(line.start);
right = line.end; ring.points.push(line.end);
false right = line.end;
} else if relative_eq!(line.end, right, epsilon = 0.001) { false
/*} else if relative_eq!(line.end, right, epsilon = 0.001) {
ring.points.push(line.start); ring.points.push(line.start);
right = line.start; right = line.start;
false false
} else { } else {
true true
} }*/
}) })
} }

View File

@@ -1,9 +1,9 @@
use approx::{relative_eq, AbsDiffEq, RelativeEq}; use approx::{assert_relative_ne, relative_eq, AbsDiffEq, RelativeEq};
use bvh::{ use bvh::{
aabb::{Aabb, Bounded}, aabb::{Aabb, Bounded},
bounding_hierarchy::BHShape, bounding_hierarchy::BHShape,
}; };
use nalgebra::{ClosedAdd, ClosedMul, ClosedSub, Scalar, SimdPartialOrd, Vector3}; use nalgebra::{ClosedAdd, ClosedMul, ClosedSub, Point3, Scalar, SimdPartialOrd, Vector3};
use num::{Float, FromPrimitive}; use num::{Float, FromPrimitive};
use super::line::Line3; use super::line::Line3;
@@ -43,16 +43,24 @@ where
+ FromPrimitive, + FromPrimitive,
{ {
pub fn new(a: Vector3<T>, b: Vector3<T>, c: Vector3<T>) -> Self { pub fn new(a: Vector3<T>, b: Vector3<T>, c: Vector3<T>) -> Self {
let normal = (b - a).cross(&(c - a));
let mut aabb = Aabb::with_bounds(a.into(), b.into());
aabb.grow_mut(&c.into());
Self { Self {
a, a,
b, b,
c, c,
normal: normal.into(), normal: (b - a).cross(&(c - a)).into(),
node_index: 0, node_index: 0,
aabb, aabb: Aabb::with_bounds(
Point3::new(
T::min(T::min(a.x, b.x), c.x),
T::min(T::min(a.y, b.y), c.y),
T::min(T::min(a.z, b.z), c.z),
),
Point3::new(
T::max(T::max(a.x, b.x), c.x),
T::max(T::max(a.y, b.y), c.y),
T::max(T::max(a.z, b.z), c.z),
),
),
} }
} }
@@ -80,14 +88,14 @@ where
pub fn intersect_z(&self, z: T) -> Option<Line3<T>> { pub fn intersect_z(&self, z: T) -> Option<Line3<T>> {
let mut intersection = Vec::with_capacity(3); let mut intersection = Vec::with_capacity(3);
let mut last = self.c; let mut last = &self.c;
for point in [self.a, self.b, self.c].iter() { for point in [self.a, self.b, self.c].iter() {
if relative_eq!(point.z, z) { if relative_eq!(point.z, z) {
intersection.push(*point); intersection.push(*point);
} else if last.z < z && point.z > z || last.z > z && point.z < z { } else if (last.z < z && point.z > z) || (last.z > z && point.z < z) {
intersection.push(last.lerp(&point, (z - last.z) / (point.z - last.z))); intersection.push(last.lerp(&point, (z - last.z) / (point.z - last.z)));
} }
last = *point; last = point;
} }
if intersection.len() == 2 { if intersection.len() == 2 {
Some(Line3 { Some(Line3 {

View File

@@ -101,9 +101,9 @@
{@const visible = showSlices >= i / $layers.length} {@const visible = showSlices >= i / $layers.length}
{@const color = new Color(0, i / $layers.length, 0.2)} {@const color = new Color(0, i / $layers.length, 0.2)}
{#if type === LayerType.Line} {#if type === LayerType.Line}
<T.Line {geometry} {visible}> <T.LineSegments {geometry} {visible}>
<T.LineBasicMaterial {color} /> <T.LineBasicMaterial {color} />
</T.Line> </T.LineSegments>
{:else if type === LayerType.Surface} {:else if type === LayerType.Surface}
<T.Mesh {geometry} {visible}> <T.Mesh {geometry} {visible}>
<T.MeshMatcapMaterial {color} side={DoubleSide} /> <T.MeshMatcapMaterial {color} side={DoubleSide} />