Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compute Vertex position on-demand instead of storing it #1647

Merged
merged 16 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 77 additions & 65 deletions crates/fj-kernel/src/algorithms/approx/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

use std::collections::BTreeMap;

use fj_math::Point;

use crate::{
geometry::curve::{Curve, GlobalPath},
objects::{GlobalEdge, HalfEdge, Surface},
objects::{GlobalEdge, HalfEdge, Surface, Vertex},
storage::{Handle, ObjectId},
};

Expand All @@ -29,26 +31,39 @@ impl Approx for (&Handle<HalfEdge>, &Surface) {
let boundary = half_edge.boundary();
let range = RangeOnPath { boundary };

let first = ApproxPoint::new(
half_edge.start_position(),
half_edge.start_vertex().position(),
)
.with_source((half_edge.clone(), half_edge.boundary()[0]));
let position_surface = half_edge.start_position();
let position_global = match cache.get_position(half_edge.start_vertex())
{
Some(position) => position,
None => {
let position_global = surface
.geometry()
.point_from_surface_coords(position_surface);
cache.insert_position(half_edge.start_vertex(), position_global)
}
};

let first = ApproxPoint::new(position_surface, position_global)
.with_source((half_edge.clone(), half_edge.boundary()[0]));

let points = {
let approx = match cache.get(half_edge.global_form().clone(), range)
{
Some(approx) => approx,
None => {
let approx = approx_edge(
&half_edge.curve(),
surface,
range,
tolerance,
);
cache.insert(half_edge.global_form().clone(), range, approx)
}
};
let approx =
match cache.get_edge(half_edge.global_form().clone(), range) {
Some(approx) => approx,
None => {
let approx = approx_edge(
&half_edge.curve(),
surface,
range,
tolerance,
);
cache.insert_edge(
half_edge.global_form().clone(),
range,
approx,
)
}
};

approx
.points
Expand Down Expand Up @@ -169,7 +184,8 @@ fn approx_edge(
/// A cache for results of an approximation
#[derive(Default)]
pub struct EdgeCache {
inner: BTreeMap<(ObjectId, RangeOnPath), GlobalEdgeApprox>,
edge_approx: BTreeMap<(ObjectId, RangeOnPath), GlobalEdgeApprox>,
vertex_approx: BTreeMap<ObjectId, Point<3>>,
}

impl EdgeCache {
Expand All @@ -178,34 +194,51 @@ impl EdgeCache {
Self::default()
}

/// Insert the approximation of a [`GlobalEdge`]
pub fn insert(
&mut self,
handle: Handle<GlobalEdge>,
range: RangeOnPath,
approx: GlobalEdgeApprox,
) -> GlobalEdgeApprox {
self.inner.insert((handle.id(), range), approx.clone());
approx
}

/// Access the approximation for the given [`GlobalEdge`], if available
pub fn get(
pub fn get_edge(
&self,
handle: Handle<GlobalEdge>,
range: RangeOnPath,
) -> Option<GlobalEdgeApprox> {
if let Some(approx) = self.inner.get(&(handle.id(), range)) {
if let Some(approx) = self.edge_approx.get(&(handle.id(), range)) {
return Some(approx.clone());
}
if let Some(approx) = self.inner.get(&(handle.id(), range.reverse())) {
if let Some(approx) =
self.edge_approx.get(&(handle.id(), range.reverse()))
{
// If we have a cache entry for the reverse range, we need to use
// that too!
return Some(approx.clone().reverse());
}

None
}

/// Insert the approximation of a [`GlobalEdge`]
pub fn insert_edge(
&mut self,
handle: Handle<GlobalEdge>,
range: RangeOnPath,
approx: GlobalEdgeApprox,
) -> GlobalEdgeApprox {
self.edge_approx
.insert((handle.id(), range), approx.clone())
.unwrap_or(approx)
}

fn get_position(&self, handle: &Handle<Vertex>) -> Option<Point<3>> {
self.vertex_approx.get(&handle.id()).cloned()
}

fn insert_position(
&mut self,
handle: &Handle<Vertex>,
position: Point<3>,
) -> Point<3> {
self.vertex_approx
.insert(handle.id(), position)
.unwrap_or(position)
}
}

/// An approximation of a [`GlobalEdge`]
Expand Down Expand Up @@ -247,14 +280,9 @@ mod tests {
let half_edge = {
let mut cycle = PartialCycle::new(&mut services.objects);

let [mut half_edge, next_half_edge, _] = cycle
.update_as_polygon_from_points(
[[1., 1.], [2., 1.], [1., 2.]],
&mut services.objects,
);
half_edge.write().infer_vertex_positions_if_necessary(
&surface.geometry(),
next_half_edge.read().start_vertex.clone(),
let [half_edge, _, _] = cycle.update_as_polygon_from_points(
[[1., 1.], [2., 1.], [1., 2.]],
&mut services.objects,
);

let half_edge = half_edge.read().clone();
Expand All @@ -281,14 +309,9 @@ mod tests {
let half_edge = {
let mut cycle = PartialCycle::new(&mut services.objects);

let [mut half_edge, next_half_edge, _] = cycle
.update_as_polygon_from_points(
[[1., 1.], [2., 1.], [1., 2.]],
&mut services.objects,
);
half_edge.write().infer_vertex_positions_if_necessary(
&surface.geometry(),
next_half_edge.read().start_vertex.clone(),
let [half_edge, _, _] = cycle.update_as_polygon_from_points(
[[1., 1.], [2., 1.], [1., 2.]],
&mut services.objects,
);

let half_edge = half_edge.read().clone();
Expand Down Expand Up @@ -318,20 +341,14 @@ mod tests {
let half_edge = {
let mut cycle = PartialCycle::new(&mut services.objects);

let [mut half_edge, next_half_edge, _] = cycle
.update_as_polygon_from_points(
[[0., 1.], [1., 1.], [1., 2.]],
&mut services.objects,
);
let [mut half_edge, _, _] = cycle.update_as_polygon_from_points(
[[0., 1.], [1., 1.], [1., 2.]],
&mut services.objects,
);

half_edge.write().boundary[0] = Some(range.boundary[0]);
half_edge.write().boundary[1] = Some(range.boundary[1]);

half_edge.write().infer_vertex_positions_if_necessary(
&surface.geometry(),
next_half_edge.read().start_vertex.clone(),
);

let half_edge = half_edge.read().clone();
half_edge
.build(&mut services.objects)
Expand Down Expand Up @@ -364,11 +381,6 @@ mod tests {
let mut half_edge = PartialHalfEdge::new(&mut services.objects);

half_edge.update_as_circle_from_radius(1.);
let next_vertex = half_edge.start_vertex.clone();
half_edge.infer_vertex_positions_if_necessary(
&surface.geometry(),
next_vertex,
);

half_edge
.build(&mut services.objects)
Expand Down
50 changes: 13 additions & 37 deletions crates/fj-kernel/src/algorithms/intersect/curve_edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ mod tests {
use fj_math::Point;

use crate::{
builder::{CycleBuilder, HalfEdgeBuilder},
builder::CycleBuilder,
geometry::curve::Curve,
partial::{PartialCycle, PartialObject},
services::Services,
Expand All @@ -84,19 +84,13 @@ mod tests {
fn compute_edge_in_front_of_curve_origin() {
let mut services = Services::new();

let surface = services.objects.surfaces.xy_plane();
let curve = Curve::u_axis();
let half_edge = {
let mut cycle = PartialCycle::new(&mut services.objects);

let [mut half_edge, next_half_edge, _] = cycle
.update_as_polygon_from_points(
[[1., -1.], [1., 1.], [0., 1.]],
&mut services.objects,
);
half_edge.write().infer_vertex_positions_if_necessary(
&surface.geometry(),
next_half_edge.read().start_vertex.clone(),
let [half_edge, _, _] = cycle.update_as_polygon_from_points(
[[1., -1.], [1., 1.], [0., 1.]],
&mut services.objects,
);

half_edge.build(&mut services.objects)
Expand All @@ -116,19 +110,13 @@ mod tests {
fn compute_edge_behind_curve_origin() {
let mut services = Services::new();

let surface = services.objects.surfaces.xy_plane();
let curve = Curve::u_axis();
let half_edge = {
let mut cycle = PartialCycle::new(&mut services.objects);

let [mut half_edge, next_half_edge, _] = cycle
.update_as_polygon_from_points(
[[-1., -1.], [-1., 1.], [0., 1.]],
&mut services.objects,
);
half_edge.write().infer_vertex_positions_if_necessary(
&surface.geometry(),
next_half_edge.read().start_vertex.clone(),
let [half_edge, _, _] = cycle.update_as_polygon_from_points(
[[-1., -1.], [-1., 1.], [0., 1.]],
&mut services.objects,
);

half_edge.build(&mut services.objects)
Expand All @@ -148,19 +136,13 @@ mod tests {
fn compute_edge_parallel_to_curve() {
let mut services = Services::new();

let surface = services.objects.surfaces.xy_plane();
let curve = Curve::u_axis();
let half_edge = {
let mut cycle = PartialCycle::new(&mut services.objects);

let [mut half_edge, next_half_edge, _] = cycle
.update_as_polygon_from_points(
[[-1., -1.], [1., -1.], [1., 1.]],
&mut services.objects,
);
half_edge.write().infer_vertex_positions_if_necessary(
&surface.geometry(),
next_half_edge.read().start_vertex.clone(),
let [half_edge, _, _] = cycle.update_as_polygon_from_points(
[[-1., -1.], [1., -1.], [1., 1.]],
&mut services.objects,
);

half_edge.build(&mut services.objects)
Expand All @@ -175,19 +157,13 @@ mod tests {
fn compute_edge_on_curve() {
let mut services = Services::new();

let surface = services.objects.surfaces.xy_plane();
let curve = Curve::u_axis();
let half_edge = {
let mut cycle = PartialCycle::new(&mut services.objects);

let [mut half_edge, next_half_edge, _] = cycle
.update_as_polygon_from_points(
[[-1., 0.], [1., 0.], [1., 1.]],
&mut services.objects,
);
half_edge.write().infer_vertex_positions_if_necessary(
&surface.geometry(),
next_half_edge.read().start_vertex.clone(),
let [half_edge, _, _] = cycle.update_as_polygon_from_points(
[[-1., 0.], [1., 0.], [1., 1.]],
&mut services.objects,
);

half_edge.build(&mut services.objects)
Expand Down
6 changes: 2 additions & 4 deletions crates/fj-kernel/src/algorithms/sweep/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@ impl Sweep for Handle<Vertex> {

fn sweep_with_cache(
self,
path: impl Into<Vector<3>>,
_: impl Into<Vector<3>>,
cache: &mut SweepCache,
objects: &mut Service<Objects>,
) -> Self::Swept {
let a = self.clone();
let b = cache
.global_vertex
.entry(self.id())
.or_insert_with(|| {
Vertex::new(self.position() + path.into()).insert(objects)
})
.or_insert_with(|| Vertex::new().insert(objects))
.clone();

let vertices = [a, b];
Expand Down
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/algorithms/transform/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl TransformObject for GlobalEdge {
) -> Self {
// There's nothing to actually transform here, as `GlobalEdge` holds no
// data. We still need this implementation though, as a new `GlobalEdge`
// must be created to represent the new and transformed edge.
// object must be created to represent the new and transformed edge.
Self::new()
}
}
8 changes: 5 additions & 3 deletions crates/fj-kernel/src/algorithms/transform/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ use super::{TransformCache, TransformObject};
impl TransformObject for Vertex {
fn transform_with_cache(
self,
transform: &Transform,
_: &Transform,
_: &mut Service<Objects>,
_: &mut TransformCache,
) -> Self {
let position = transform.transform_point(&self.position());
Self::new(position)
// There's nothing to actually transform here, as `Vertex` holds no
// data. We still need this implementation though, as a new `Vertex`
// object must be created to represent the new and transformed vertex.
Self::new()
}
}
20 changes: 0 additions & 20 deletions crates/fj-kernel/src/builder/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,6 @@ pub trait CycleBuilder {
) -> O::SameSize<Partial<HalfEdge>>
where
O: ObjectArgument<Partial<HalfEdge>>;

/// Infer the positions of all vertices, if necessary
fn infer_vertex_positions_if_necessary(
&mut self,
surface: &SurfaceGeometry,
);
}

impl CycleBuilder for PartialCycle {
Expand Down Expand Up @@ -109,18 +103,4 @@ impl CycleBuilder for PartialCycle {
this
})
}

fn infer_vertex_positions_if_necessary(
&mut self,
surface: &SurfaceGeometry,
) {
for (mut half_edge, next_half_edge) in
self.half_edges.iter().cloned().circular_tuple_windows()
{
let next_vertex = next_half_edge.read().start_vertex.clone();
half_edge
.write()
.infer_vertex_positions_if_necessary(surface, next_vertex);
}
}
}
Loading