Skip to content

Commit

Permalink
Merge pull request #2398 from hannobraun/approx
Browse files Browse the repository at this point in the history
Prepare approximation code for vertex geometry changes
  • Loading branch information
hannobraun authored Jun 25, 2024
2 parents 6faa38d + f7783d1 commit acc0c00
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 45 deletions.
26 changes: 22 additions & 4 deletions crates/fj-core/src/algorithms/approx/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ use crate::{
};

use super::{
half_edge::{approx_half_edge, HalfEdgeApprox, HalfEdgeApproxCache},
ApproxPoint, Tolerance,
half_edge::{approx_half_edge, HalfEdgeApprox},
vertex::approx_vertex,
ApproxCache, ApproxPoint, Tolerance,
};

/// Approximate the provided cycle
pub fn approx_cycle(
cycle: &Cycle,
surface: &Handle<Surface>,
tolerance: impl Into<Tolerance>,
cache: &mut HalfEdgeApproxCache,
cache: &mut ApproxCache,
geometry: &Geometry,
) -> CycleApprox {
let tolerance = tolerance.into();
Expand All @@ -30,8 +31,25 @@ pub fn approx_cycle(
.iter()
.map(|half_edge| {
let boundary = geometry.of_half_edge(half_edge).boundary;
let [start_position_curve, _] = boundary.inner;

let start = approx_vertex(
half_edge.start_vertex().clone(),
half_edge.curve(),
surface,
start_position_curve,
&mut cache.vertex,
geometry,
);

approx_half_edge(
half_edge, surface, boundary, tolerance, cache, geometry,
half_edge,
surface,
start,
boundary,
tolerance,
&mut cache.curve,
geometry,
)
})
.collect();
Expand Down
7 changes: 3 additions & 4 deletions crates/fj-core/src/algorithms/approx/face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ use crate::{

use super::{
cycle::{approx_cycle, CycleApprox},
half_edge::HalfEdgeApproxCache,
Approx, ApproxPoint, Tolerance,
Approx, ApproxCache, ApproxPoint, Tolerance,
};

impl Approx for &ObjectSet<Face> {
type Approximation = BTreeSet<FaceApprox>;
type Cache = HalfEdgeApproxCache;
type Cache = ApproxCache;

fn approx_with_cache(
self,
Expand Down Expand Up @@ -68,7 +67,7 @@ impl Approx for &ObjectSet<Face> {
pub fn approx_face(
face: Handle<Face>,
tolerance: impl Into<Tolerance>,
cache: &mut HalfEdgeApproxCache,
cache: &mut ApproxCache,
geometry: &Geometry,
) -> FaceApprox {
let tolerance = tolerance.into();
Expand Down
24 changes: 3 additions & 21 deletions crates/fj-core/src/algorithms/approx/half_edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,27 @@ use crate::{

use super::{
curve::{approx_curve_with_cache, CurveApproxCache},
vertex::{approx_vertex, VertexApproxCache},
ApproxPoint, Tolerance,
};

/// Approximate the provided half-edge
pub fn approx_half_edge(
half_edge: &Handle<HalfEdge>,
surface: &Handle<Surface>,
start: ApproxPoint<1>,
boundary: CurveBoundary<Point<1>>,
tolerance: impl Into<Tolerance>,
cache: &mut HalfEdgeApproxCache,
cache: &mut CurveApproxCache,
geometry: &Geometry,
) -> HalfEdgeApprox {
let tolerance = tolerance.into();

let [start_position_curve, _] = boundary.inner;

let start = approx_vertex(
half_edge.start_vertex().clone(),
half_edge.curve(),
surface,
start_position_curve,
&mut cache.start_position,
geometry,
);

let rest = approx_curve_with_cache(
half_edge.curve(),
surface,
boundary,
tolerance,
&mut cache.curve,
cache,
geometry,
);

Expand Down Expand Up @@ -78,10 +67,3 @@ pub struct HalfEdgeApprox {
/// The points that approximate the half-edge
pub points: Vec<ApproxPoint<2>>,
}

/// Cache for half-edge approximations
#[derive(Default)]
pub struct HalfEdgeApproxCache {
start_position: VertexApproxCache,
curve: CurveApproxCache,
}
12 changes: 12 additions & 0 deletions crates/fj-core/src/algorithms/approx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ use std::{
hash::{Hash, Hasher},
};

use curve::CurveApproxCache;
use fj_math::Point;
use vertex::VertexApproxCache;

use crate::geometry::Geometry;

Expand Down Expand Up @@ -58,6 +60,16 @@ pub trait Approx: Sized {
) -> Self::Approximation;
}

/// Cache for half-edge approximations
#[derive(Default)]
pub struct ApproxCache {
/// Cache for vertex approximations
pub vertex: VertexApproxCache,

/// Cache for curve approximations
pub curve: CurveApproxCache,
}

/// A point from an approximation, with local and global forms
#[derive(Clone, Copy, Debug)]
pub struct ApproxPoint<const D: usize> {
Expand Down
6 changes: 2 additions & 4 deletions crates/fj-core/src/algorithms/approx/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ use std::collections::BTreeSet;

use crate::{geometry::Geometry, topology::Shell};

use super::{
face::FaceApprox, half_edge::HalfEdgeApproxCache, Approx, Tolerance,
};
use super::{face::FaceApprox, Approx, ApproxCache, Tolerance};

impl Approx for &Shell {
type Approximation = BTreeSet<FaceApprox>;
type Cache = HalfEdgeApproxCache;
type Cache = ApproxCache;

fn approx_with_cache(
self,
Expand Down
6 changes: 2 additions & 4 deletions crates/fj-core/src/algorithms/approx/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ use std::collections::BTreeSet;

use crate::{geometry::Geometry, topology::Sketch};

use super::{
face::FaceApprox, half_edge::HalfEdgeApproxCache, Approx, Tolerance,
};
use super::{face::FaceApprox, Approx, ApproxCache, Tolerance};

impl Approx for &Sketch {
type Approximation = BTreeSet<FaceApprox>;
type Cache = HalfEdgeApproxCache;
type Cache = ApproxCache;

fn approx_with_cache(
self,
Expand Down
6 changes: 2 additions & 4 deletions crates/fj-core/src/algorithms/approx/solid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ use std::collections::BTreeSet;

use crate::{geometry::Geometry, topology::Solid};

use super::{
face::FaceApprox, half_edge::HalfEdgeApproxCache, Approx, Tolerance,
};
use super::{face::FaceApprox, Approx, ApproxCache, Tolerance};

impl Approx for &Solid {
type Approximation = BTreeSet<FaceApprox>;
type Cache = HalfEdgeApproxCache;
type Cache = ApproxCache;

fn approx_with_cache(
self,
Expand Down
6 changes: 2 additions & 4 deletions crates/fj-core/src/algorithms/triangulate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ mod tests {
use fj_math::{Point, Scalar};

use crate::{
algorithms::approx::{
face::approx_face, half_edge::HalfEdgeApproxCache, Tolerance,
},
algorithms::approx::{face::approx_face, ApproxCache, Tolerance},
operations::{
build::{BuildCycle, BuildFace},
insert::Insert,
Expand Down Expand Up @@ -314,7 +312,7 @@ mod tests {
Ok(approx_face(
face,
tolerance,
&mut HalfEdgeApproxCache::default(),
&mut ApproxCache::default(),
&core.layers.geometry,
)
.triangulate(core))
Expand Down

0 comments on commit acc0c00

Please sign in to comment.