Skip to content

Commit

Permalink
Merge pull request #1884 from hannobraun/operations
Browse files Browse the repository at this point in the history
Improve `Sketch` operations
  • Loading branch information
hannobraun committed Jun 13, 2023
2 parents d899c4a + 6a69d24 commit d5c6502
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 52 deletions.
1 change: 1 addition & 0 deletions crates/fj-core/src/operations/build/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod cycle;
pub mod edge;
pub mod face;
pub mod region;
pub mod shell;
pub mod sketch;
pub mod solid;
Expand Down
33 changes: 33 additions & 0 deletions crates/fj-core/src/operations/build/region.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use fj_math::{Point, Scalar};

use crate::{
objects::{Cycle, Region},
operations::{BuildCycle, Insert},
services::Services,
};

/// Build a [`Region`]
pub trait BuildRegion {
/// Build a circle
fn circle(
center: impl Into<Point<2>>,
radius: impl Into<Scalar>,
services: &mut Services,
) -> Region {
let exterior = Cycle::circle(center, radius, services).insert(services);
Region::new(exterior, [], None)
}

/// Build a polygon
fn polygon<P, Ps>(points: Ps, services: &mut Services) -> Region
where
P: Into<Point<2>>,
Ps: IntoIterator<Item = P>,
Ps::IntoIter: Clone + ExactSizeIterator,
{
let exterior = Cycle::polygon(points, services).insert(services);
Region::new(exterior, [], None)
}
}

impl BuildRegion for Region {}
1 change: 1 addition & 0 deletions crates/fj-core/src/operations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub use self::{
cycle::BuildCycle,
edge::BuildHalfEdge,
face::{BuildFace, Polygon},
region::BuildRegion,
shell::{BuildShell, TetrahedronShell},
sketch::BuildSketch,
solid::{BuildSolid, Tetrahedron},
Expand Down
43 changes: 1 addition & 42 deletions crates/fj-core/src/operations/update/sketch.rs
Original file line number Diff line number Diff line change
@@ -1,57 +1,16 @@
use fj_math::{Point, Scalar};

use crate::{
objects::{Cycle, Region, Sketch},
operations::{BuildCycle, Insert},
services::Services,
objects::{Region, Sketch},
storage::Handle,
};

/// Update a [`Sketch`]
pub trait UpdateSketch {
/// Add a region to the sketch
fn add_region(&self, region: Handle<Region>) -> Self;

/// Add a circle to the sketch
fn add_circle(
&self,
center: impl Into<Point<2>>,
radius: impl Into<Scalar>,
services: &mut Services,
) -> Self;

/// Add a polygon to the sketch
fn add_polygon<P, Ps>(&self, points: Ps, services: &mut Services) -> Self
where
P: Into<Point<2>>,
Ps: IntoIterator<Item = P>,
Ps::IntoIter: Clone + ExactSizeIterator;
}

impl UpdateSketch for Sketch {
fn add_region(&self, region: Handle<Region>) -> Self {
Sketch::new(self.regions().cloned().chain([region]))
}

fn add_circle(
&self,
center: impl Into<Point<2>>,
radius: impl Into<Scalar>,
services: &mut Services,
) -> Self {
let exterior = Cycle::circle(center, radius, services).insert(services);
let region = Region::new(exterior, [], None).insert(services);
self.add_region(region)
}

fn add_polygon<P, Ps>(&self, points: Ps, services: &mut Services) -> Self
where
P: Into<Point<2>>,
Ps: IntoIterator<Item = P>,
Ps::IntoIter: Clone + ExactSizeIterator,
{
let exterior = Cycle::polygon(points, services).insert(services);
let region = Region::new(exterior, [], None).insert(services);
self.add_region(region)
}
}
23 changes: 13 additions & 10 deletions models/cuboid/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use fj::{
core::{
algorithms::sweep::Sweep,
objects::{Sketch, Solid},
operations::{BuildSketch, Insert, UpdateSketch},
objects::{Region, Sketch, Solid},
operations::{BuildRegion, BuildSketch, Insert, UpdateSketch},
services::Services,
storage::Handle,
},
Expand All @@ -13,14 +13,17 @@ pub fn cuboid(x: f64, y: f64, z: f64) -> Handle<Solid> {
let mut services = Services::new();

let sketch = Sketch::empty()
.add_polygon(
[
[-x / 2., -y / 2.],
[x / 2., -y / 2.],
[x / 2., y / 2.],
[-x / 2., y / 2.],
],
&mut services,
.add_region(
Region::polygon(
[
[-x / 2., -y / 2.],
[x / 2., -y / 2.],
[x / 2., y / 2.],
[-x / 2., y / 2.],
],
&mut services,
)
.insert(&mut services),
)
.insert(&mut services);

Expand Down

0 comments on commit d5c6502

Please sign in to comment.