Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error handling in fj-operations #632

Merged
merged 8 commits into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/fj-operations/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ keywords = ["cad", "programmatic", "code-cad"]
categories = ["encoding", "mathematics", "rendering"]


[dependencies]
thiserror = "1.0.31"

[dependencies.fj]
version = "0.6.0"
path = "../fj"
Expand Down
11 changes: 4 additions & 7 deletions crates/fj-operations/src/circle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,12 @@ impl ToShape for fj::Circle {
// to be added here.

let edge = Edge::builder(&mut shape)
.build_circle(Scalar::from_f64(self.radius()))
.unwrap();
shape.insert(Cycle::new(vec![edge])).unwrap();
.build_circle(Scalar::from_f64(self.radius()))?;
shape.insert(Cycle::new(vec![edge]))?;

let cycles = shape.cycles();
let surface = shape.insert(Surface::xy_plane()).unwrap();
shape
.insert(Face::new(surface, cycles, Vec::new(), self.color()))
.unwrap();
let surface = shape.insert(Surface::xy_plane())?;
shape.insert(Face::new(surface, cycles, Vec::new(), self.color()))?;

Ok(shape)
}
Expand Down
18 changes: 11 additions & 7 deletions crates/fj-operations/src/difference_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ impl ToShape for fj::Difference2d {
let mut exteriors = Vec::new();
let mut interiors = Vec::new();

// Can be cleaned up, once `each_ref` is stable:
// https://doc.rust-lang.org/std/primitive.array.html#method.each_ref
// Can be cleaned up, once `each_ref` and `try_map` are stable:
// - https://doc.rust-lang.org/std/primitive.array.html#method.each_ref
// - https://doc.rust-lang.org/std/primitive.array.html#method.try_map
let [a, b] = self.shapes();
let [a, b] =
[a, b].map(|shape| shape.to_shape(tolerance, debug_info).unwrap());
let [a, b] = [a, b].map(|shape| shape.to_shape(tolerance, debug_info));
let [a, b] = [a?, b?];

if let Some(face) = a.faces().next() {
// If there's at least one face to subtract from, we can proceed.
Expand Down Expand Up @@ -72,9 +73,12 @@ impl ToShape for fj::Difference2d {
}
}

difference
.merge(Face::new(surface, exteriors, interiors, self.color()))
.unwrap();
difference.merge(Face::new(
surface,
exteriors,
interiors,
self.color(),
))?;
}

Ok(difference)
Expand Down
10 changes: 2 additions & 8 deletions crates/fj-operations/src/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ impl ToShape for fj::Group {
let a = self.a.to_shape(tolerance, debug_info)?;
let b = self.b.to_shape(tolerance, debug_info)?;

copy_shape(a, &mut shape);
copy_shape(b, &mut shape);
shape.merge_shape(&a)?;
shape.merge_shape(&b)?;

Ok(shape)
}
Expand All @@ -31,9 +31,3 @@ impl ToShape for fj::Group {
a.merged(&b)
}
}

fn copy_shape(orig: Shape, target: &mut Shape) {
for face_orig in orig.faces() {
target.merge(face_orig.get()).unwrap();
}
}
21 changes: 15 additions & 6 deletions crates/fj-operations/src/shape_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use fj_interop::{debug::DebugInfo, mesh::Mesh};
use fj_kernel::{
algorithms::{triangulate, Tolerance},
algorithms::{triangulate, InvalidTolerance, Tolerance},
shape::ValidationError,
};
use fj_math::{Aabb, Point, Scalar};
Expand All @@ -17,10 +17,7 @@ pub struct ShapeProcessor {

impl ShapeProcessor {
/// Process an [`fj::Shape`] into [`ProcessedShape`]
pub fn process(
&self,
shape: &fj::Shape,
) -> Result<ProcessedShape, ValidationError> {
pub fn process(&self, shape: &fj::Shape) -> Result<ProcessedShape, Error> {
let aabb = shape.bounding_volume();

let tolerance = match self.tolerance {
Expand All @@ -36,7 +33,7 @@ impl ShapeProcessor {
}

let tolerance = min_extent / Scalar::from_f64(1000.);
Tolerance::from_scalar(tolerance).unwrap()
Tolerance::from_scalar(tolerance)?
}
Some(user_defined_tolerance) => user_defined_tolerance,
};
Expand Down Expand Up @@ -69,3 +66,15 @@ pub struct ProcessedShape {
/// The debug info generated while processing the shape
pub debug_info: DebugInfo,
}

/// A shape processing error
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// Error converting to shape
#[error("Error converting to shape")]
ToShape(#[from] ValidationError),

/// Model has zero size
#[error("Model has an zero size")]
Extent(#[from] InvalidTolerance),
}
3 changes: 1 addition & 2 deletions crates/fj-operations/src/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ impl ToShape for fj::Sketch {

Face::builder(surface, &mut shape)
.with_exterior_polygon(points)
.build()
.unwrap();
.build()?;

Ok(shape)
}
Expand Down