Merge pull request #1813 from hannobraun/builder

Migrate last piece of code from builder to operations API; remove builder API
This commit is contained in:
Hanno Braun 2023-05-04 10:23:58 +02:00 committed by GitHub
commit d7b4fcab94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 91 deletions

View File

@ -5,10 +5,9 @@ use itertools::Itertools;
use crate::{ use crate::{
algorithms::{reverse::Reverse, transform::TransformObject}, algorithms::{reverse::Reverse, transform::TransformObject},
builder::CycleBuilder,
geometry::curve::GlobalPath, geometry::curve::GlobalPath,
objects::{Face, Shell}, objects::{Cycle, Face, Shell},
operations::Insert, operations::{BuildCycle, Insert, JoinCycle},
services::Services, services::Services,
storage::Handle, storage::Handle,
}; };
@ -82,13 +81,14 @@ impl Sweep for Handle<Face> {
)); ));
} }
let top_cycle = CycleBuilder::connect_to_edges(top_edges, services) let top_cycle = Cycle::empty()
.build(services); .add_joined_edges(top_edges, services)
.insert(services);
if i == 0 { if i == 0 {
exterior = Some(top_cycle.insert(services)); exterior = Some(top_cycle);
} else { } else {
interiors.push(top_cycle.insert(services)); interiors.push(top_cycle);
}; };
} }

View File

@ -1,76 +0,0 @@
use fj_math::Point;
use itertools::Itertools;
use crate::{
geometry::curve::Curve,
objects::{Cycle, HalfEdge},
operations::{BuildHalfEdge, Insert, UpdateHalfEdge},
services::Services,
storage::Handle,
};
/// Builder API for [`Cycle`]
#[derive(Default)]
pub struct CycleBuilder {
half_edges: Vec<HalfEdge>,
}
impl CycleBuilder {
/// Create an instance of `CycleBuilder`
pub fn new() -> Self {
Self::default()
}
/// Create a cycle whose half-edges are connected to the provided half-edges
///
/// The half-edges of the new circle will be coincident with the provided
/// half-edges, but will point in the opposite direction.
///
/// Assumes that the provided half-edges, once translated into local
/// equivalents of this cycle, form a cycle themselves.
pub fn connect_to_edges<Es>(edges: Es, services: &mut Services) -> Self
where
Es: IntoIterator<Item = (Handle<HalfEdge>, Curve, [Point<1>; 2])>,
Es::IntoIter: Clone + ExactSizeIterator,
{
let half_edges = edges
.into_iter()
.circular_tuple_windows()
.map(|((prev, _, _), (half_edge, curve, boundary))| {
HalfEdge::unjoined(curve, boundary, services)
.replace_start_vertex(prev.start_vertex().clone())
.replace_global_form(half_edge.global_form().clone())
})
.collect();
Self { half_edges }
}
/// Create a polygon
pub fn polygon<P, Ps>(points: Ps, services: &mut Services) -> Self
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)
})
.collect();
Self { half_edges }
}
/// Build the cycle
pub fn build(self, services: &mut Services) -> Cycle {
let half_edges = self
.half_edges
.into_iter()
.map(|half_edge| half_edge.insert(services));
Cycle::new(half_edges)
}
}

View File

@ -1,5 +0,0 @@
//! API for building objects
mod cycle;
pub use self::cycle::CycleBuilder;

View File

@ -88,7 +88,6 @@
#![warn(missing_docs)] #![warn(missing_docs)]
pub mod algorithms; pub mod algorithms;
pub mod builder;
pub mod geometry; pub mod geometry;
pub mod objects; pub mod objects;
pub mod operations; pub mod operations;

View File

@ -1,13 +1,24 @@
use std::ops::RangeInclusive; use std::ops::RangeInclusive;
use fj_math::Point;
use itertools::Itertools;
use crate::{ use crate::{
objects::Cycle, geometry::curve::Curve,
operations::{Insert, UpdateCycle, UpdateHalfEdge}, objects::{Cycle, HalfEdge},
operations::{BuildHalfEdge, Insert, UpdateCycle, UpdateHalfEdge},
services::Services, services::Services,
storage::Handle,
}; };
/// Join a [`Cycle`] to another /// Join a [`Cycle`] to another
pub trait JoinCycle { pub trait JoinCycle {
/// Create a cycle that is joined to the provided edges
fn add_joined_edges<Es>(&self, edges: Es, services: &mut Services) -> Self
where
Es: IntoIterator<Item = (Handle<HalfEdge>, Curve, [Point<1>; 2])>,
Es::IntoIter: Clone + ExactSizeIterator;
/// Join the cycle to another /// Join the cycle to another
/// ///
/// Joins the cycle to the other at the provided ranges. The ranges specify /// Joins the cycle to the other at the provided ranges. The ranges specify
@ -47,6 +58,21 @@ pub trait JoinCycle {
} }
impl JoinCycle for Cycle { impl JoinCycle for Cycle {
fn add_joined_edges<Es>(&self, edges: Es, services: &mut Services) -> Self
where
Es: IntoIterator<Item = (Handle<HalfEdge>, Curve, [Point<1>; 2])>,
Es::IntoIter: Clone + ExactSizeIterator,
{
self.add_half_edges(edges.into_iter().circular_tuple_windows().map(
|((prev, _, _), (half_edge, curve, boundary))| {
HalfEdge::unjoined(curve, boundary, services)
.replace_start_vertex(prev.start_vertex().clone())
.replace_global_form(half_edge.global_form().clone())
.insert(services)
},
))
}
fn join_to( fn join_to(
&self, &self,
other: &Cycle, other: &Cycle,