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

Move ShapeProcesssor to fj-operations #467

Merged
merged 2 commits into from
Apr 12, 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
68 changes: 6 additions & 62 deletions fj-app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ use std::time::Instant;

use anyhow::anyhow;
use fj_host::{Model, Parameters};
use fj_interop::{debug::DebugInfo, mesh::Mesh};
use fj_kernel::algorithms::{triangulate, Tolerance};
use fj_math::{Aabb, Point, Scalar};
use fj_operations::ToShape as _;
use fj_operations::shape_processor::ShapeProcessor;
use futures::executor::block_on;
use tracing::{trace, warn};
use tracing_subscriber::fmt::format;
Expand Down Expand Up @@ -117,7 +114,11 @@ fn main() -> anyhow::Result<()> {

if let Some(new_shape) = watcher.receive() {
let new_shape = shape_processor.process(&new_shape);
new_shape.update_geometry(&mut renderer);
renderer.update_geometry(
(&new_shape.mesh).into(),
(&new_shape.debug_info).into(),
new_shape.aabb,
);

if camera.is_none() {
camera = Some(Camera::new(&new_shape.aabb));
Expand Down Expand Up @@ -220,60 +221,3 @@ fn main() -> anyhow::Result<()> {
}
});
}

struct ShapeProcessor {
tolerance: Option<Tolerance>,
}

impl ShapeProcessor {
fn process(&self, shape: &fj::Shape) -> ProcessedShape {
let aabb = shape.bounding_volume();

let tolerance = match self.tolerance {
None => {
// Compute a reasonable default for the tolerance value. To do
// this, we just look at the smallest non-zero extent of the
// bounding box and divide that by some value.
let mut min_extent = Scalar::MAX;
for extent in aabb.size().components {
if extent > Scalar::ZERO && extent < min_extent {
min_extent = extent;
}
}

let tolerance = min_extent / Scalar::from_f64(1000.);
Tolerance::from_scalar(tolerance).unwrap()
}
Some(user_defined_tolerance) => user_defined_tolerance,
};

let mut debug_info = DebugInfo::new();
let mesh = triangulate(
shape.to_shape(tolerance, &mut debug_info),
tolerance,
&mut debug_info,
);

ProcessedShape {
aabb,
mesh,
debug_info,
}
}
}

struct ProcessedShape {
aabb: Aabb<3>,
mesh: Mesh<Point<3>>,
debug_info: DebugInfo,
}

impl ProcessedShape {
fn update_geometry(&self, renderer: &mut Renderer) {
renderer.update_geometry(
(&self.mesh).into(),
(&self.debug_info).into(),
self.aabb,
);
}
}
2 changes: 2 additions & 0 deletions fj-operations/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#![deny(missing_docs)]

pub mod shape_processor;

mod circle;
mod difference_2d;
mod group;
Expand Down
65 changes: 65 additions & 0 deletions fj-operations/src/shape_processor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//! API for processing shapes

use fj_interop::{debug::DebugInfo, mesh::Mesh};
use fj_kernel::algorithms::{triangulate, Tolerance};
use fj_math::{Aabb, Point, Scalar};

use crate::ToShape as _;

/// Processes an [`fj::Shape`] into a [`ProcessedShape`]
pub struct ShapeProcessor {
/// The tolerance value used for creating the triangle mesh
pub tolerance: Option<Tolerance>,
}

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

let tolerance = match self.tolerance {
None => {
// Compute a reasonable default for the tolerance value. To do
// this, we just look at the smallest non-zero extent of the
// bounding box and divide that by some value.
let mut min_extent = Scalar::MAX;
for extent in aabb.size().components {
if extent > Scalar::ZERO && extent < min_extent {
min_extent = extent;
}
}

let tolerance = min_extent / Scalar::from_f64(1000.);
Tolerance::from_scalar(tolerance).unwrap()
}
Some(user_defined_tolerance) => user_defined_tolerance,
};

let mut debug_info = DebugInfo::new();
let mesh = triangulate(
shape.to_shape(tolerance, &mut debug_info),
tolerance,
&mut debug_info,
);

ProcessedShape {
aabb,
mesh,
debug_info,
}
}
}

/// A processed shape
///
/// Created by [`ShapeProcessor::process`].
pub struct ProcessedShape {
/// The axis-aligned bounding box of the shape
pub aabb: Aabb<3>,

/// The triangle mesh that approximates the original shape
pub mesh: Mesh<Point<3>>,

/// The debug info generated while processing the shape
pub debug_info: DebugInfo,
}