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

Clean up partial object API, update its documentation #1148

Merged
merged 15 commits into from
Sep 28, 2022
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
1 change: 1 addition & 0 deletions crates/fj-kernel/src/algorithms/approx/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ mod tests {
use crate::{
algorithms::approx::{path::RangeOnPath, Approx, ApproxPoint},
objects::{Curve, Surface},
partial::HasPartial,
path::GlobalPath,
stores::Stores,
};
Expand Down
1 change: 1 addition & 0 deletions crates/fj-kernel/src/algorithms/intersect/curve_edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ mod tests {

use crate::{
objects::{Curve, HalfEdge, Surface},
partial::HasPartial,
stores::Stores,
};

Expand Down
1 change: 1 addition & 0 deletions crates/fj-kernel/src/algorithms/intersect/curve_face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ where
mod tests {
use crate::{
objects::{Curve, Face, Surface},
partial::HasPartial,
stores::Stores,
};

Expand Down
1 change: 1 addition & 0 deletions crates/fj-kernel/src/algorithms/intersect/face_face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ mod tests {
use crate::{
algorithms::intersect::CurveFaceIntersection,
objects::{Curve, Face, Surface},
partial::HasPartial,
stores::Stores,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ mod tests {
use crate::{
algorithms::transform::TransformObject,
objects::{Curve, Surface},
partial::HasPartial,
stores::Stores,
};

Expand Down
1 change: 1 addition & 0 deletions crates/fj-kernel/src/algorithms/sweep/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ mod tests {
use crate::{
algorithms::{reverse::Reverse, sweep::Sweep},
objects::{Cycle, Face, HalfEdge, Surface},
partial::HasPartial,
stores::Stores,
};

Expand Down
1 change: 1 addition & 0 deletions crates/fj-kernel/src/algorithms/sweep/face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ mod tests {
use crate::{
algorithms::{reverse::Reverse, transform::TransformObject},
objects::{Face, HalfEdge, Sketch, Surface},
partial::HasPartial,
stores::Stores,
};

Expand Down
10 changes: 6 additions & 4 deletions crates/fj-kernel/src/algorithms/sweep/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ use crate::{
Curve, GlobalCurve, GlobalEdge, GlobalVertex, HalfEdge, Surface,
SurfaceVertex, Vertex,
},
partial::HasPartial,
path::SurfacePath,
stores::Stores,
stores::{Handle, Stores},
};

use super::Sweep;
Expand Down Expand Up @@ -137,7 +138,7 @@ impl Sweep for GlobalVertex {
let a = self;
let b = GlobalVertex::from_position(self.position() + path.into());

let curve = GlobalCurve::partial()
let curve = Handle::<GlobalCurve>::partial()
.as_line_from_points([a.position(), b.position()])
.build(stores);

Expand All @@ -153,7 +154,8 @@ mod tests {
Curve, GlobalCurve, GlobalEdge, GlobalVertex, HalfEdge, Surface,
Vertex,
},
stores::Stores,
partial::HasPartial,
stores::{Handle, Stores},
};

#[test]
Expand Down Expand Up @@ -186,7 +188,7 @@ mod tests {
.sweep([0., 0., 1.], &stores);

let expected_edge = GlobalEdge::new(
GlobalCurve::partial().as_z_axis().build(&stores),
Handle::<GlobalCurve>::partial().as_z_axis().build(&stores),
[[0., 0., 0.], [0., 0., 1.]].map(GlobalVertex::from_position),
);
assert_eq!(edge, expected_edge);
Expand Down
1 change: 1 addition & 0 deletions crates/fj-kernel/src/algorithms/validate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ mod tests {
Curve, GlobalCurve, GlobalEdge, GlobalVertex, HalfEdge, Surface,
SurfaceVertex, Vertex,
},
partial::HasPartial,
path::{GlobalPath, SurfacePath},
stores::Stores,
};
Expand Down
1 change: 1 addition & 0 deletions crates/fj-kernel/src/builder/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use fj_math::Point;

use crate::{
objects::{Curve, Cycle, HalfEdge, Surface, SurfaceVertex, Vertex},
partial::HasPartial,
stores::Stores,
};

Expand Down
1 change: 1 addition & 0 deletions crates/fj-kernel/src/builder/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
objects::{
Curve, Cycle, Face, HalfEdge, Shell, Surface, SurfaceVertex, Vertex,
},
partial::HasPartial,
stores::Stores,
};

Expand Down
6 changes: 4 additions & 2 deletions crates/fj-kernel/src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,8 @@ mod tests {
Curve, Cycle, Face, GlobalCurve, GlobalVertex, HalfEdge, Shell,
Sketch, Solid, Surface, SurfaceVertex, Vertex,
},
stores::Stores,
partial::HasPartial,
stores::{Handle, Stores},
};

use super::ObjectIters as _;
Expand Down Expand Up @@ -440,7 +441,8 @@ mod tests {
fn global_curve() {
let stores = Stores::new();

let object = GlobalCurve::partial().as_x_axis().build(&stores);
let object =
Handle::<GlobalCurve>::partial().as_x_axis().build(&stores);

assert_eq!(0, object.curve_iter().count());
assert_eq!(0, object.cycle_iter().count());
Expand Down
17 changes: 0 additions & 17 deletions crates/fj-kernel/src/objects/curve.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::{
partial::{PartialCurve, PartialGlobalCurve},
path::{GlobalPath, SurfacePath},
stores::Handle,
};
Expand All @@ -15,14 +14,6 @@ pub struct Curve {
}

impl Curve {
/// Create a [`PartialCurve`]
///
/// This function exists just for convenience, and will just return a
/// default [`PartialCurve`].
pub fn partial() -> PartialCurve {
PartialCurve::default()
}

/// Construct a new instance of `Curve`
pub fn new(
surface: Surface,
Expand Down Expand Up @@ -59,14 +50,6 @@ pub struct GlobalCurve {
}

impl GlobalCurve {
/// Create a [`PartialGlobalCurve`]
///
/// This function exists just for convenience, and will just return a
/// default [`PartialGlobalCurve`].
pub fn partial() -> PartialGlobalCurve {
PartialGlobalCurve::default()
}

/// Construct a `GlobalCurve` from the path that defines it
pub fn from_path(path: GlobalPath) -> Self {
Self { path }
Expand Down
21 changes: 1 addition & 20 deletions crates/fj-kernel/src/objects/edge.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use std::fmt;

use crate::{
partial::{PartialGlobalEdge, PartialHalfEdge},
stores::Handle,
};
use crate::stores::Handle;

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

Expand All @@ -16,14 +13,6 @@ pub struct HalfEdge {
}

impl HalfEdge {
/// Create a [`PartialHalfEdge`]
///
/// This function exists just for convenience, and will just return a
/// default [`PartialHalfEdge`].
pub fn partial() -> PartialHalfEdge {
PartialHalfEdge::default()
}

/// Create a new instance of `HalfEdge`
///
/// # Panics
Expand Down Expand Up @@ -124,14 +113,6 @@ pub struct GlobalEdge {
}

impl GlobalEdge {
/// Create a [`PartialGlobalEdge`]
///
/// This function exists just for convenience, and will just return a
/// default [`PartialGlobalEdge`].
pub fn partial() -> PartialGlobalEdge {
PartialGlobalEdge::default()
}

/// Create a new instance
pub fn new(
curve: Handle<GlobalCurve>,
Expand Down
28 changes: 0 additions & 28 deletions crates/fj-kernel/src/objects/vertex.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use fj_math::Point;
use pretty_assertions::assert_eq;

use crate::partial::{
PartialGlobalVertex, PartialSurfaceVertex, PartialVertex,
};

use super::{Curve, Surface};

/// A vertex
Expand All @@ -21,14 +17,6 @@ pub struct Vertex {
}

impl Vertex {
/// Create a [`PartialVertex`]
///
/// This function exists just for convenience, and will just return a
/// default [`PartialVertex`].
pub fn partial() -> PartialVertex {
PartialVertex::default()
}

/// Construct an instance of `Vertex`
///
/// Panics, if `curve` and `surface_form` are not defined on the same
Expand Down Expand Up @@ -85,14 +73,6 @@ pub struct SurfaceVertex {
}

impl SurfaceVertex {
/// Create a [`PartialSurfaceVertex`]
///
/// This function exists just for convenience, and will just return a
/// default [`PartialSurfaceVertex`].
pub fn partial() -> PartialSurfaceVertex {
PartialSurfaceVertex::default()
}

/// Construct a new instance of `SurfaceVertex`
pub fn new(
position: impl Into<Point<2>>,
Expand Down Expand Up @@ -147,14 +127,6 @@ pub struct GlobalVertex {
}

impl GlobalVertex {
/// Create a [`PartialGlobalVertex`]
///
/// This function exists just for convenience, and will just return a
/// default [`PartialGlobalVertex`].
pub fn partial() -> PartialGlobalVertex {
PartialGlobalVertex::default()
}

/// Construct a `GlobalVertex` from a position
pub fn from_position(position: impl Into<Point<3>>) -> Self {
let position = position.into();
Expand Down
53 changes: 42 additions & 11 deletions crates/fj-kernel/src/partial/maybe_partial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,78 @@ use crate::{
stores::{Handle, Stores},
};

use super::HasPartialForm;
use super::{HasPartial, Partial};

/// Either a partial object or a full one
/// Can be used everywhere either a partial or full objects are accepted
///
/// Some convenience methods are available for specific instances of
/// `MaybePartial` (like, `MaybePartial<Curve>`, or `MaybePartial<Vertex>`).
///
/// # Implementation Note
///
/// The set of available convenience methods is far from complete. Please feel
/// free to just add more, if you need them.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub enum MaybePartial<T: HasPartialForm> {
pub enum MaybePartial<T: HasPartial> {
/// A full object
Full(T),

/// A partial object
Partial(T::PartialForm),
Partial(T::Partial),
}

impl<T: HasPartialForm> MaybePartial<T> {
impl<T: HasPartial> MaybePartial<T> {
/// If this is a partial object, update it
///
/// This is useful whenever a partial object can infer something about its
/// parts from other parts, and wants to update what was inferred, in case
/// it *can* be updated.
pub fn update_partial(
self,
f: impl FnOnce(T::PartialForm) -> T::PartialForm,
f: impl FnOnce(T::Partial) -> T::Partial,
) -> Self {
match self {
Self::Partial(partial) => Self::Partial(f(partial)),
_ => self,
}
}

/// Return the full object, either directly or by building it
/// Return or build a full object
///
/// If this already is a full object, it is returned. If this is a partial
/// object, the full object is built from it, using [`Partial::build`].
pub fn into_full(self, stores: &Stores) -> T {
match self {
Self::Partial(partial) => T::from_partial(partial, stores),
Self::Partial(partial) => partial.build(stores),
Self::Full(full) => full,
}
}

/// Return the partial object, either directly or via conversion
pub fn into_partial(self) -> T::PartialForm {
/// Return or convert a partial object
///
/// If this already is a partial object, is is returned. If this is a full
/// object, it is converted into a partial object using
/// [`HasPartial::to_partial`].
pub fn into_partial(self) -> T::Partial {
match self {
Self::Partial(partial) => partial,
Self::Full(full) => full.into(),
Self::Full(full) => full.to_partial(),
}
}
}

impl<T> From<T> for MaybePartial<T>
where
T: HasPartial,
{
fn from(full: T) -> Self {
Self::Full(full)
}
}

// Unfortunately, we can't add a blanket implementation from `T::Partial` for
// `MaybePartial<T>`, as that would conflict.

impl MaybePartial<Curve> {
/// Access the global form
pub fn global_form(&self) -> Option<MaybePartial<Handle<GlobalCurve>>> {
Expand Down
Loading