Skip to content

Commit

Permalink
Merge pull request hannobraun#1797 from hannobraun/builder
Browse files Browse the repository at this point in the history
Expand operations API
  • Loading branch information
hannobraun authored Apr 26, 2023
2 parents f678e59 + a59304f commit a4d75d8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
30 changes: 29 additions & 1 deletion crates/fj-kernel/src/operations/build/cycle.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
use crate::objects::Cycle;
use fj_math::Point;
use itertools::Itertools;

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

use super::BuildHalfEdge;

/// Build a [`Cycle`]
pub trait BuildCycle {
/// Build an empty cycle
fn empty() -> Cycle {
Cycle::new([])
}

/// Build a polygon
fn polygon<P, Ps>(points: Ps, services: &mut Services) -> Cycle
where
P: Into<Point<2>>,
Ps: IntoIterator<Item = P>,
Ps::IntoIter: Clone + ExactSizeIterator,
{
let half_edges = points
.into_iter()
.map(Into::into)
.circular_tuple_windows()
.map(|(start, end)| {
HalfEdge::line_segment([start, end], None, services)
.insert(services)
});

Cycle::new(half_edges)
}
}

impl BuildCycle for Cycle {}
8 changes: 2 additions & 6 deletions crates/fj-kernel/src/validate/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ mod tests {

use crate::{
assert_contains_err,
builder::CycleBuilder,
objects::{Cycle, HalfEdge},
operations::{BuildCycle, BuildHalfEdge, Insert, UpdateCycle},
services::Services,
Expand All @@ -107,11 +106,8 @@ mod tests {
fn half_edges_connected() -> anyhow::Result<()> {
let mut services = Services::new();

let valid = CycleBuilder::polygon(
[[0.0, 0.0], [1.0, 0.0], [1.0, 1.0]],
&mut services,
)
.build(&mut services);
let valid =
Cycle::polygon([[0.0, 0.0], [1.0, 0.0], [1.0, 1.0]], &mut services);

valid.validate_and_return_first_error()?;

Expand Down

0 comments on commit a4d75d8

Please sign in to comment.