use std::ops::Deref;
use fj_math::Segment;
use crate::objects::{Cycle, Surface};
use super::{
edge::{HalfEdgeApprox, HalfEdgeApproxCache},
Approx, ApproxPoint, Tolerance,
};
impl Approx for (&Cycle, &Surface) {
type Approximation = CycleApprox;
type Cache = HalfEdgeApproxCache;
fn approx_with_cache(
self,
tolerance: impl Into<Tolerance>,
cache: &mut Self::Cache,
) -> Self::Approximation {
let (cycle, surface) = self;
let tolerance = tolerance.into();
let half_edges = cycle
.half_edges()
.iter()
.map(|edge| {
(edge.deref(), surface).approx_with_cache(tolerance, cache)
})
.collect();
CycleApprox { half_edges }
}
}
#[derive(Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct CycleApprox {
pub half_edges: Vec<HalfEdgeApprox>,
}
impl CycleApprox {
pub fn points(&self) -> Vec<ApproxPoint<2>> {
let mut points = Vec::new();
for approx in &self.half_edges {
points.extend(approx.points.iter().copied());
}
if let Some(point) = points.first() {
points.push(*point);
}
points
}
pub fn segments(&self) -> Vec<Segment<3>> {
let mut segments = Vec::new();
for segment in self.points().windows(2) {
let segment = [&segment[0], &segment[1]];
segments
.push(Segment::from(segment.map(|point| point.global_form)));
}
segments
}
}