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

Rename Vertex to GlobalVertex; clean it up #750

Merged
merged 13 commits into from
Jun 30, 2022
8 changes: 4 additions & 4 deletions crates/fj-kernel/src/algorithms/approx/edges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn approx_edge(
// the same vertex would be understood to refer to very close, but distinct
// vertices.
let vertices = vertices.convert(|vertex| {
geometry::Point::new(*vertex.local(), vertex.canonical().point)
geometry::Point::new(*vertex.local(), vertex.canonical().position())
});
if let Some([a, b]) = vertices {
points.insert(0, a);
Expand All @@ -36,7 +36,7 @@ mod test {

use crate::{
geometry,
objects::{Vertex, VerticesOfEdge},
objects::{GlobalVertex, VerticesOfEdge},
shape::LocalForm,
};

Expand All @@ -47,8 +47,8 @@ mod test {
let c = Point::from([3., 5., 8.]);
let d = Point::from([5., 8., 13.]);

let v1 = Vertex::from_point(a);
let v2 = Vertex::from_point(d);
let v1 = GlobalVertex::from_position(a);
let v2 = GlobalVertex::from_position(d);

let vertices = VerticesOfEdge::from_vertices([
LocalForm::new(Point::from([0.]), v1),
Expand Down
14 changes: 8 additions & 6 deletions crates/fj-kernel/src/algorithms/sweep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use fj_math::{Point, Scalar, Transform, Triangle, Vector};

use crate::{
iter::ObjectIters,
objects::{Curve, Cycle, Edge, Face, Surface, Vertex, VerticesOfEdge},
objects::{
Curve, Cycle, Edge, Face, GlobalVertex, Surface, VerticesOfEdge,
},
shape::LocalForm,
};

Expand Down Expand Up @@ -87,14 +89,14 @@ fn create_top_face(
fn create_non_continuous_side_face(
path: Vector<3>,
is_sweep_along_negative_direction: bool,
vertices_bottom: [Vertex; 2],
vertices_bottom: [GlobalVertex; 2],
color: [u8; 4],
target: &mut Vec<Face>,
) {
let vertices = {
let vertices_top = vertices_bottom.map(|vertex| {
let point = vertex.point + path;
Vertex { point }
let position = vertex.position() + path;
GlobalVertex::from_position(position)
});

let [[a, b], [c, d]] = [vertices_bottom, vertices_top];
Expand All @@ -107,7 +109,7 @@ fn create_non_continuous_side_face(
};

let surface = {
let [a, b, _, c] = vertices.map(|vertex| vertex.point);
let [a, b, _, c] = vertices.map(|vertex| vertex.position());
Surface::plane_from_points([a, b, c])
};

Expand All @@ -131,7 +133,7 @@ fn create_non_continuous_side_face(
let curve = {
let local = Curve::line_from_points([a.0, b.0]);

let global = [a, b].map(|vertex| vertex.1.point);
let global = [a, b].map(|vertex| vertex.1.position());
let global = Curve::line_from_points(global);

LocalForm::new(local, global)
Expand Down
14 changes: 7 additions & 7 deletions crates/fj-kernel/src/algorithms/transform.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use fj_math::Transform;

use crate::{
objects::{Cycle, CyclesInFace, Edge, Face, FaceBRep, Vertex},
objects::{Cycle, CyclesInFace, Edge, Face, FaceBRep, GlobalVertex},
shape::LocalForm,
};

Expand Down Expand Up @@ -58,11 +58,11 @@ pub fn transform_cycles(

let vertices =
edge.canonical().clone().vertices.map(|vertex| {
let point = vertex.canonical().point;
let point = transform.transform_point(&point);
let position = vertex.canonical().position();
let position = transform.transform_point(&position);

let local = *vertex.local();
let canonical = Vertex { point };
let canonical = GlobalVertex::from_position(position);

LocalForm::new(local, canonical)
});
Expand Down Expand Up @@ -91,11 +91,11 @@ pub fn transform_cycles(
LocalForm::canonical_only(curve)
};
let vertices = edge.vertices.clone().map(|vertex| {
let point = vertex.canonical().point;
let point = transform.transform_point(&point);
let position = vertex.canonical().position();
let position = transform.transform_point(&position);

let local = *vertex.local();
let canonical = Vertex { point };
let canonical = GlobalVertex::from_position(position);

LocalForm::new(local, canonical)
});
Expand Down
24 changes: 12 additions & 12 deletions crates/fj-kernel/src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::collections::VecDeque;

use crate::objects::{Curve, Cycle, Edge, Face, Surface, Vertex};
use crate::objects::{Curve, Cycle, Edge, Face, GlobalVertex, Surface};

/// Access iterators over all objects of a shape, or part of it
///
Expand All @@ -25,7 +25,7 @@ pub trait ObjectIters {
fn surface_iter(&self) -> Iter<Surface>;

/// Iterate over all vertices
fn vertex_iter(&self) -> Iter<Vertex>;
fn vertex_iter(&self) -> Iter<GlobalVertex>;
}

impl ObjectIters for Curve<3> {
Expand All @@ -49,7 +49,7 @@ impl ObjectIters for Curve<3> {
Iter::empty()
}

fn vertex_iter(&self) -> Iter<Vertex> {
fn vertex_iter(&self) -> Iter<GlobalVertex> {
Iter::empty()
}
}
Expand Down Expand Up @@ -99,7 +99,7 @@ impl ObjectIters for Cycle<3> {
iter
}

fn vertex_iter(&self) -> Iter<Vertex> {
fn vertex_iter(&self) -> Iter<GlobalVertex> {
let mut iter = Iter::empty();

for edge in self.edges() {
Expand Down Expand Up @@ -155,7 +155,7 @@ impl ObjectIters for Edge<3> {
iter
}

fn vertex_iter(&self) -> Iter<Vertex> {
fn vertex_iter(&self) -> Iter<GlobalVertex> {
let mut iter = Iter::empty().with(self.curve().vertex_iter());

for vertex in self.vertices().into_iter().flatten() {
Expand Down Expand Up @@ -227,7 +227,7 @@ impl ObjectIters for Face {
Iter::empty()
}

fn vertex_iter(&self) -> Iter<Vertex> {
fn vertex_iter(&self) -> Iter<GlobalVertex> {
if let Face::Face(face) = self {
let mut iter = Iter::empty().with(face.surface().vertex_iter());

Expand Down Expand Up @@ -263,12 +263,12 @@ impl ObjectIters for Surface {
Iter::from_object(*self)
}

fn vertex_iter(&self) -> Iter<Vertex> {
fn vertex_iter(&self) -> Iter<GlobalVertex> {
Iter::empty()
}
}

impl ObjectIters for Vertex {
impl ObjectIters for GlobalVertex {
fn curve_iter(&self) -> Iter<Curve<3>> {
Iter::empty()
}
Expand All @@ -289,7 +289,7 @@ impl ObjectIters for Vertex {
Iter::empty()
}

fn vertex_iter(&self) -> Iter<Vertex> {
fn vertex_iter(&self) -> Iter<GlobalVertex> {
Iter::from_object(*self)
}
}
Expand Down Expand Up @@ -354,7 +354,7 @@ where
iter
}

fn vertex_iter(&self) -> Iter<Vertex> {
fn vertex_iter(&self) -> Iter<GlobalVertex> {
let mut iter = Iter::empty();

for face in self.into_iter() {
Expand Down Expand Up @@ -405,7 +405,7 @@ impl<T> Iterator for Iter<T> {

#[cfg(test)]
mod tests {
use crate::objects::{Curve, Cycle, Edge, Face, Surface, Vertex};
use crate::objects::{Curve, Cycle, Edge, Face, GlobalVertex, Surface};

use super::ObjectIters as _;

Expand Down Expand Up @@ -478,7 +478,7 @@ mod tests {

#[test]
fn vertex() {
let vertex = Vertex::from_point([0., 0., 0.]);
let vertex = GlobalVertex::from_position([0., 0., 0.]);

assert_eq!(0, vertex.curve_iter().count());
assert_eq!(0, vertex.cycle_iter().count());
Expand Down
40 changes: 24 additions & 16 deletions crates/fj-kernel/src/objects/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use fj_math::{Circle, Line, Point, Scalar, Vector};

use crate::shape::LocalForm;

use super::{Curve, Vertex};
use super::{Curve, GlobalVertex};

/// An edge of a shape
#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
Expand Down Expand Up @@ -36,7 +36,7 @@ impl<const D: usize> Edge<D> {
///
/// This is a convenience method that saves the caller from dealing with the
/// [`Handle`]s.
pub fn vertices(&self) -> Option<[Vertex; 2]> {
pub fn vertices(&self) -> Option<[GlobalVertex; 2]> {
self.vertices
.0
.as_ref()
Expand Down Expand Up @@ -76,18 +76,18 @@ impl Edge<3> {
pub fn line_segment_from_points(
vertices: [impl Into<Point<3>>; 2],
) -> Self {
let vertices = vertices.map(|point| {
let point = point.into();
Vertex { point }
let vertices = vertices.map(|position| {
let position = position.into();
GlobalVertex::from_position(position)
});

Self::line_segment_from_vertices(vertices)
}

/// Create a line segment from two vertices
pub fn line_segment_from_vertices([a, b]: [Vertex; 2]) -> Self {
pub fn line_segment_from_vertices([a, b]: [GlobalVertex; 2]) -> Self {
let curve = {
let points = [a, b].map(|vertex| vertex.point);
let points = [a, b].map(|vertex| vertex.position());
Curve::Line(Line::from_points(points))
};

Expand All @@ -107,7 +107,7 @@ impl<const D: usize> fmt::Display for Edge<D> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.vertices() {
Some(vertices) => {
let [a, b] = vertices.map(|vertex| vertex.point);
let [a, b] = vertices.map(|vertex| vertex.position());
write!(f, "edge from {:?} to {:?}", a, b)?
}
None => write!(f, "continuous edge")?,
Expand All @@ -121,16 +121,20 @@ impl<const D: usize> fmt::Display for Edge<D> {

/// The vertices that bound an edge
#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct VerticesOfEdge(Option<[LocalForm<Point<1>, Vertex>; 2]>);
pub struct VerticesOfEdge(Option<[LocalForm<Point<1>, GlobalVertex>; 2]>);

impl VerticesOfEdge {
/// Construct an instance of `VerticesOfEdge` from zero or two vertices
pub fn new(vertices: Option<[LocalForm<Point<1>, Vertex>; 2]>) -> Self {
pub fn new(
vertices: Option<[LocalForm<Point<1>, GlobalVertex>; 2]>,
) -> Self {
Self(vertices)
}

/// Construct an instance of `VerticesOfEdge` from two vertices
pub fn from_vertices(vertices: [LocalForm<Point<1>, Vertex>; 2]) -> Self {
pub fn from_vertices(
vertices: [LocalForm<Point<1>, GlobalVertex>; 2],
) -> Self {
Self(Some(vertices))
}

Expand Down Expand Up @@ -164,12 +168,14 @@ impl VerticesOfEdge {
/// # Panics
///
/// Panics, if the edge has no vertices.
pub fn expect_vertices(self) -> [LocalForm<Point<1>, Vertex>; 2] {
pub fn expect_vertices(self) -> [LocalForm<Point<1>, GlobalVertex>; 2] {
self.0.expect("Expected edge to have vertices")
}

/// Iterate over the vertices, if any
pub fn iter(&self) -> impl Iterator<Item = &LocalForm<Point<1>, Vertex>> {
pub fn iter(
&self,
) -> impl Iterator<Item = &LocalForm<Point<1>, GlobalVertex>> {
self.0.iter().flatten()
}

Expand All @@ -188,23 +194,25 @@ impl VerticesOfEdge {
/// Map each vertex using the provided function
pub fn map<F>(self, f: F) -> Self
where
F: FnMut(LocalForm<Point<1>, Vertex>) -> LocalForm<Point<1>, Vertex>,
F: FnMut(
LocalForm<Point<1>, GlobalVertex>,
) -> LocalForm<Point<1>, GlobalVertex>,
{
Self(self.convert(f))
}

/// Convert each vertex using the provided function
pub fn convert<F, T>(self, f: F) -> Option<[T; 2]>
where
F: FnMut(LocalForm<Point<1>, Vertex>) -> T,
F: FnMut(LocalForm<Point<1>, GlobalVertex>) -> T,
{
self.0.map(|vertices| vertices.map(f))
}

/// Convert each vertex using the provided fallible function
pub fn try_convert<F, T, E>(self, f: F) -> Result<Option<[T; 2]>, E>
where
F: FnMut(LocalForm<Point<1>, Vertex>) -> Result<T, E>,
F: FnMut(LocalForm<Point<1>, GlobalVertex>) -> Result<T, E>,
{
// Can be cleaned up using `try_map`, once that is stable:
// https://doc.rust-lang.org/std/primitive.array.html#method.try_map
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@ use fj_math::Point;
/// between distinct vertices can be configured using
/// [`Shape::with_minimum_distance`].
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct Vertex {
/// The point that defines the location of the vertex
pub point: Point<3>,
pub struct GlobalVertex {
position: Point<3>,
}

impl Vertex {
impl GlobalVertex {
/// Construct a `Vertex` from a point
pub fn from_point(point: impl Into<Point<3>>) -> Self {
let point = point.into();
Self { point }
pub fn from_position(position: impl Into<Point<3>>) -> Self {
let position = position.into();
Self { position }
}

/// The position of the vertex
pub fn position(&self) -> Point<3> {
self.position
}
}
4 changes: 2 additions & 2 deletions crates/fj-kernel/src/objects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ mod curve;
mod cycle;
mod edge;
mod face;
mod global_vertex;
mod surface;
mod vertex;

pub use self::{
curve::Curve,
cycle::Cycle,
edge::{Edge, VerticesOfEdge},
face::{CyclesInFace, Face, FaceBRep},
global_vertex::GlobalVertex,
surface::{Surface, SweptCurve},
vertex::Vertex,
};
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/validation/coherence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn validate_edge(
for vertex in edge.vertices.iter() {
let local = *vertex.local();
let local_as_canonical = edge.curve().point_from_curve_coords(local);
let canonical = vertex.canonical().point;
let canonical = vertex.canonical().position();
let distance = (local_as_canonical - canonical).magnitude();

if distance > max_distance {
Expand Down
Loading