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

Move reference to Surface from HalfEdge to Cycle #1598

Merged
merged 21 commits into from
Feb 17, 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
7 changes: 6 additions & 1 deletion crates/fj-kernel/src/algorithms/approx/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//!
//! See [`CycleApprox`].

use std::ops::Deref;

use fj_math::Segment;

use crate::objects::Cycle;
Expand All @@ -23,7 +25,10 @@ impl Approx for &Cycle {

let half_edges = self
.half_edges()
.map(|half_edge| half_edge.approx_with_cache(tolerance, cache))
.map(|half_edge| {
(half_edge, self.surface().deref())
.approx_with_cache(tolerance, cache)
})
.collect();

CycleApprox { half_edges }
Expand Down
21 changes: 12 additions & 9 deletions crates/fj-kernel/src/algorithms/approx/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
//! approximations are usually used to build cycle approximations, and this way,
//! the caller doesn't have to call with duplicate vertices.

use std::ops::Deref;

use crate::{objects::HalfEdge, storage::Handle};
use crate::{
objects::{HalfEdge, Surface},
storage::Handle,
};

use super::{
curve::{CurveApprox, CurveCache},
path::RangeOnPath,
Approx, ApproxPoint, Tolerance,
};

impl Approx for &Handle<HalfEdge> {
impl Approx for (&Handle<HalfEdge>, &Surface) {
type Approximation = HalfEdgeApprox;
type Cache = CurveCache;

Expand All @@ -24,15 +25,17 @@ impl Approx for &Handle<HalfEdge> {
tolerance: impl Into<Tolerance>,
cache: &mut Self::Cache,
) -> Self::Approximation {
let boundary = self.boundary();
let (half_edge, surface) = self;

let boundary = half_edge.boundary();
let range = RangeOnPath { boundary };

let first = ApproxPoint::new(
self.start_vertex().position(),
self.start_vertex().global_form().position(),
half_edge.start_vertex().position(),
half_edge.start_vertex().global_form().position(),
)
.with_source((self.clone(), self.boundary()[0]));
let curve_approx = (self.curve(), self.surface().deref(), range)
.with_source((half_edge.clone(), half_edge.boundary()[0]));
let curve_approx = (half_edge.curve(), surface, range)
.approx_with_cache(tolerance, cache);

HalfEdgeApprox {
Expand Down
36 changes: 15 additions & 21 deletions crates/fj-kernel/src/algorithms/intersect/curve_edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ mod tests {

use crate::{
builder::{CurveBuilder, HalfEdgeBuilder},
partial::{Partial, PartialCurve, PartialHalfEdge, PartialObject},
partial::{PartialCurve, PartialHalfEdge, PartialObject},
services::Services,
};

Expand All @@ -86,16 +86,14 @@ mod tests {
fn compute_edge_in_front_of_curve_origin() {
let mut services = Services::new();

let surface = Partial::from(services.objects.surfaces.xy_plane());
let surface = services.objects.surfaces.xy_plane();
let mut curve = PartialCurve::default();
curve.update_as_u_axis();
let curve = curve.build(&mut services.objects);
let half_edge = {
let mut half_edge = PartialHalfEdge::default();
half_edge.update_as_line_segment_from_points(
surface,
[[1., -1.], [1., 1.]],
);
half_edge.update_as_line_segment_from_points([[1., -1.], [1., 1.]]);
half_edge.infer_vertex_positions_if_necessary(&surface.geometry());

half_edge.build(&mut services.objects)
};
Expand All @@ -114,16 +112,15 @@ mod tests {
fn compute_edge_behind_curve_origin() {
let mut services = Services::new();

let surface = Partial::from(services.objects.surfaces.xy_plane());
let surface = services.objects.surfaces.xy_plane();
let mut curve = PartialCurve::default();
curve.update_as_u_axis();
let curve = curve.build(&mut services.objects);
let half_edge = {
let mut half_edge = PartialHalfEdge::default();
half_edge.update_as_line_segment_from_points(
surface,
[[-1., -1.], [-1., 1.]],
);
half_edge
.update_as_line_segment_from_points([[-1., -1.], [-1., 1.]]);
half_edge.infer_vertex_positions_if_necessary(&surface.geometry());

half_edge.build(&mut services.objects)
};
Expand All @@ -142,16 +139,15 @@ mod tests {
fn compute_edge_parallel_to_curve() {
let mut services = Services::new();

let surface = Partial::from(services.objects.surfaces.xy_plane());
let surface = services.objects.surfaces.xy_plane();
let mut curve = PartialCurve::default();
curve.update_as_u_axis();
let curve = curve.build(&mut services.objects);
let half_edge = {
let mut half_edge = PartialHalfEdge::default();
half_edge.update_as_line_segment_from_points(
surface,
[[-1., -1.], [1., -1.]],
);
half_edge
.update_as_line_segment_from_points([[-1., -1.], [1., -1.]]);
half_edge.infer_vertex_positions_if_necessary(&surface.geometry());

half_edge.build(&mut services.objects)
};
Expand All @@ -165,16 +161,14 @@ mod tests {
fn compute_edge_on_curve() {
let mut services = Services::new();

let surface = Partial::from(services.objects.surfaces.xy_plane());
let surface = services.objects.surfaces.xy_plane();
let mut curve = PartialCurve::default();
curve.update_as_u_axis();
let curve = curve.build(&mut services.objects);
let half_edge = {
let mut half_edge = PartialHalfEdge::default();
half_edge.update_as_line_segment_from_points(
surface,
[[-1., 0.], [1., 0.]],
);
half_edge.update_as_line_segment_from_points([[-1., 0.], [1., 0.]]);
half_edge.infer_vertex_positions_if_necessary(&surface.geometry());

half_edge.build(&mut services.objects)
};
Expand Down
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/algorithms/reverse/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ impl Reverse for Handle<Cycle> {

edges.reverse();

Cycle::new(edges).insert(objects)
Cycle::new(self.surface().clone(), edges).insert(objects)
}
}
1 change: 0 additions & 1 deletion crates/fj-kernel/src/algorithms/reverse/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ impl Reverse for Handle<HalfEdge> {
};

HalfEdge::new(
self.surface().clone(),
self.curve().clone(),
vertices,
self.global_form().clone(),
Expand Down
53 changes: 24 additions & 29 deletions crates/fj-kernel/src/algorithms/sweep/edge.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
use std::ops::Deref;

use fj_interop::{ext::ArrayExt, mesh::Color};
use fj_math::{Point, Scalar, Vector};

use crate::{
builder::{CycleBuilder, HalfEdgeBuilder},
insert::Insert,
objects::{Face, HalfEdge, Objects},
objects::{Face, HalfEdge, Objects, Surface},
partial::{Partial, PartialFace, PartialObject},
services::Service,
storage::Handle,
};

use super::{Sweep, SweepCache};

impl Sweep for (Handle<HalfEdge>, Color) {
impl Sweep for (Handle<HalfEdge>, &Surface, Color) {
type Swept = (Handle<Face>, Handle<HalfEdge>);

fn sweep_with_cache(
Expand All @@ -23,7 +21,7 @@ impl Sweep for (Handle<HalfEdge>, Color) {
cache: &mut SweepCache,
objects: &mut Service<Objects>,
) -> Self::Swept {
let (edge, color) = self;
let (edge, surface, color) = self;
let path = path.into();

// The result of sweeping an edge is a face. Let's create that.
Expand All @@ -36,7 +34,7 @@ impl Sweep for (Handle<HalfEdge>, Color) {
// be created by sweeping a curve, so let's sweep the curve of the edge
// we're sweeping.
face.exterior.write().surface = Partial::from(
(edge.curve().clone(), edge.surface().deref())
(edge.curve().clone(), surface)
.sweep_with_cache(path, cache, objects),
);

Expand Down Expand Up @@ -150,6 +148,8 @@ impl Sweep for (Handle<HalfEdge>, Color) {

#[cfg(test)]
mod tests {
use std::ops::Deref;

use fj_interop::{ext::ArrayExt, mesh::Color};
use fj_math::Point;
use pretty_assertions::assert_eq;
Expand All @@ -168,38 +168,33 @@ mod tests {
fn sweep() {
let mut services = Services::new();

let surface = services.objects.surfaces.xy_plane();

let half_edge = {
let mut half_edge = PartialHalfEdge::default();
half_edge.update_as_line_segment_from_points(
services.objects.surfaces.xy_plane(),
[[0., 0.], [1., 0.]],
);
half_edge.update_as_line_segment_from_points([[0., 0.], [1., 0.]]);
half_edge.infer_vertex_positions_if_necessary(&surface.geometry());

half_edge
.build(&mut services.objects)
.insert(&mut services.objects)
};

let (face, _) = (half_edge, Color::default())
let (face, _) = (half_edge, surface.deref(), Color::default())
.sweep([0., 0., 1.], &mut services.objects);

let expected_face = {
let surface = Partial::from(services.objects.surfaces.xz_plane());
let surface = services.objects.surfaces.xz_plane();

let bottom = {
let mut half_edge = PartialHalfEdge::default();
half_edge.update_as_line_segment_from_points(
surface.clone(),
[[0., 0.], [1., 0.]],
);
half_edge
.update_as_line_segment_from_points([[0., 0.], [1., 0.]]);

half_edge
};
let side_up = {
let mut side_up = PartialHalfEdge {
surface: surface.clone(),
..Default::default()
};
let mut side_up = PartialHalfEdge::default();

{
let [back, front] = side_up
Expand All @@ -219,10 +214,7 @@ mod tests {
side_up
};
let top = {
let mut top = PartialHalfEdge {
surface: surface.clone(),
..Default::default()
};
let mut top = PartialHalfEdge::default();

{
let [(back, back_surface), (front, front_surface)] =
Expand All @@ -238,6 +230,7 @@ mod tests {

top.infer_global_form();
top.update_as_line_segment();
top.infer_vertex_positions_if_necessary(&surface.geometry());

Partial::from(
top.build(&mut services.objects)
Expand All @@ -247,10 +240,7 @@ mod tests {
.clone()
};
let side_down = {
let mut side_down = PartialHalfEdge {
surface,
..Default::default()
};
let mut side_down = PartialHalfEdge::default();

let [(back, back_surface), (front, front_surface)] =
side_down.vertices.each_mut_ext();
Expand All @@ -263,6 +253,8 @@ mod tests {

side_down.infer_global_form();
side_down.update_as_line_segment();
side_down
.infer_vertex_positions_if_necessary(&surface.geometry());

Partial::from(
side_down
Expand All @@ -273,7 +265,10 @@ mod tests {
.clone()
};

let mut cycle = PartialCycle::default();
let mut cycle = PartialCycle {
surface: Partial::from(surface),
..Default::default()
};
cycle.half_edges.extend(
[bottom, side_up, top, side_down].map(Partial::from_partial),
);
Expand Down
Loading