Skip to content

Commit

Permalink
Merge pull request #1127 from hannobraun/ready/edge-constructor
Browse files Browse the repository at this point in the history
Remove `HalfEdge::from_curve_and_vertices`
  • Loading branch information
hannobraun authored Sep 21, 2022
2 parents b22d416 + adf1017 commit 2c817c4
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 34 deletions.
19 changes: 17 additions & 2 deletions crates/fj-kernel/src/algorithms/reverse/edge.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::objects::HalfEdge;
use crate::objects::{GlobalEdge, HalfEdge};

use super::Reverse;

Expand All @@ -9,6 +9,21 @@ impl Reverse for HalfEdge {
[b, a]
};

HalfEdge::from_curve_and_vertices(self.curve().clone(), vertices)
HalfEdge::new(
self.curve().clone(),
vertices,
self.global_form().clone().reverse(),
)
}
}

impl Reverse for GlobalEdge {
fn reverse(self) -> Self {
let vertices = {
let &[a, b] = self.vertices();
[b, a]
};

GlobalEdge::new(self.curve().clone(), vertices)
}
}
25 changes: 22 additions & 3 deletions crates/fj-kernel/src/algorithms/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use fj_math::{Transform, Vector};

use crate::{
objects::{
Curve, Cycle, Face, Faces, GlobalCurve, GlobalVertex, HalfEdge, Shell,
Sketch, Solid, Surface, SurfaceVertex, Vertex,
Curve, Cycle, Face, Faces, GlobalCurve, GlobalEdge, GlobalVertex,
HalfEdge, Shell, Sketch, Solid, Surface, SurfaceVertex, Vertex,
},
path::GlobalPath,
stores::{Handle, Stores},
Expand Down Expand Up @@ -122,6 +122,23 @@ impl TransformObject for GlobalCurve {
}
}

impl TransformObject for GlobalEdge {
type Transformed = Self;

fn transform(
self,
transform: &Transform,
stores: &Stores,
) -> Self::Transformed {
let curve = self.curve().clone().transform(transform, stores);
let vertices = self
.vertices()
.map(|vertex| vertex.transform(transform, stores));

Self::new(curve, vertices)
}
}

impl TransformObject for GlobalPath {
type Transformed = Self;

Expand Down Expand Up @@ -153,8 +170,10 @@ impl TransformObject for HalfEdge {
.vertices()
.clone()
.map(|vertex| vertex.transform(transform, stores));
let global_form =
self.global_form().clone().transform(transform, stores);

Self::from_curve_and_vertices(curve, vertices)
Self::new(curve, vertices, global_form)
}
}

Expand Down
8 changes: 5 additions & 3 deletions crates/fj-kernel/src/algorithms/validate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ mod tests {
use crate::{
algorithms::validate::{Validate, ValidationConfig, ValidationError},
objects::{
Curve, GlobalCurve, GlobalVertex, HalfEdge, Surface, SurfaceVertex,
Vertex,
Curve, GlobalCurve, GlobalEdge, GlobalVertex, HalfEdge, Surface,
SurfaceVertex, Vertex,
},
path::{GlobalPath, SurfacePath},
stores::Stores,
Expand Down Expand Up @@ -236,7 +236,9 @@ mod tests {
);
let vertices = [a, b];

let half_edge = HalfEdge::from_curve_and_vertices(curve, vertices);
let global_edge = GlobalEdge::builder()
.build_from_curve_and_vertices(&curve, &vertices);
let half_edge = HalfEdge::new(curve, vertices, global_edge);

let result =
half_edge.clone().validate_with_config(&ValidationConfig {
Expand Down
33 changes: 29 additions & 4 deletions crates/fj-kernel/src/builder/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use fj_math::{Line, Point, Scalar};

use crate::{
objects::{
Curve, GlobalCurve, GlobalVertex, HalfEdge, Surface, SurfaceVertex,
Vertex,
Curve, GlobalCurve, GlobalEdge, GlobalVertex, HalfEdge, Surface,
SurfaceVertex, Vertex,
},
path::{GlobalPath, SurfacePath},
stores::Stores,
Expand Down Expand Up @@ -57,7 +57,10 @@ impl<'a> HalfEdgeBuilder<'a> {
)
};

HalfEdge::from_curve_and_vertices(curve, vertices)
let global_form = GlobalEdge::builder()
.build_from_curve_and_vertices(&curve, &vertices);

HalfEdge::new(curve, vertices, global_form)
}

/// Build a line segment from two points
Expand Down Expand Up @@ -121,6 +124,28 @@ impl<'a> HalfEdgeBuilder<'a> {
]
};

HalfEdge::from_curve_and_vertices(curve, vertices)
let global_form = GlobalEdge::builder()
.build_from_curve_and_vertices(&curve, &vertices);

HalfEdge::new(curve, vertices, global_form)
}
}

/// API for building a [`GlobalEdge`]
///
/// Also see [`GlobalEdge::builder`].
pub struct GlobalEdgeBuilder;

impl GlobalEdgeBuilder {
/// Build a [`GlobalEdge`] from the provided curve and vertices
pub fn build_from_curve_and_vertices(
self,
curve: &Curve,
vertices: &[Vertex; 2],
) -> GlobalEdge {
GlobalEdge::new(
curve.global_form().clone(),
vertices.clone().map(|vertex| *vertex.global_form()),
)
}
}
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ mod vertex;
pub use self::{
curve::{CurveBuilder, GlobalCurveBuilder},
cycle::CycleBuilder,
edge::HalfEdgeBuilder,
edge::{GlobalEdgeBuilder, HalfEdgeBuilder},
face::FaceBuilder,
shell::ShellBuilder,
sketch::SketchBuilder,
Expand Down
27 changes: 6 additions & 21 deletions crates/fj-kernel/src/objects/edge.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt;

use crate::{
builder::HalfEdgeBuilder,
builder::{GlobalEdgeBuilder, HalfEdgeBuilder},
stores::{Handle, Stores},
};

Expand All @@ -23,10 +23,6 @@ impl HalfEdge {

/// Create a new instance of `HalfEdge`
///
/// If you only have a curve and the edge vertices, please check out
/// [`HalfEdge::from_curve_and_vertices`], which is a convenience wrapper
/// around this method, which creates an instance of [`GlobalEdge`].
///
/// # Panics
///
/// Panics, if the provided `vertices` are not defined on the same curve as
Expand Down Expand Up @@ -76,22 +72,6 @@ impl HalfEdge {
}
}

/// Create a new instance of `HalfEdge` from a curve and vertices
///
/// The [`GlobalEdge`] instance is created from the provided curve and
/// vertices. Please refer to [`HalfEdge::new`], if you already have a
/// [`GlobalEdge`] instance that you can provide.
pub fn from_curve_and_vertices(
curve: Curve,
vertices: [Vertex; 2],
) -> Self {
let global = GlobalEdge::new(
curve.global_form().clone(),
vertices.clone().map(|vertex| *vertex.global_form()),
);
Self::new(curve, vertices, global)
}

/// Access the curve that defines the half-edge's geometry
///
/// The edge can be a segment of the curve that is bounded by two vertices,
Expand Down Expand Up @@ -134,6 +114,11 @@ pub struct GlobalEdge {
}

impl GlobalEdge {
/// Build a `GlobalEdge` using [`GlobalEdgeBuilder`]
pub fn builder() -> GlobalEdgeBuilder {
GlobalEdgeBuilder
}

/// Create a new instance
pub fn new(
curve: Handle<GlobalCurve>,
Expand Down

0 comments on commit 2c817c4

Please sign in to comment.