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

Simplify partial object constructions #1448

Merged
merged 11 commits into from
Dec 13, 2022
6 changes: 3 additions & 3 deletions crates/fj-kernel/src/builder/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ impl ShellBuilder {
[a.clone(), b, c, d, a]
};

let mut edges = Vec::new();
let mut half_edges = Vec::new();
for (surface_vertices, edge) in surface_vertices
.as_slice()
.array_windows_ext()
Expand All @@ -339,11 +339,11 @@ impl ShellBuilder {

half_edge.update_as_line_segment();

edges.push(Partial::from_partial(half_edge));
half_edges.push(Partial::from_partial(half_edge));
}

PartialFace {
exterior: Partial::from_partial(PartialCycle::new(edges)),
exterior: Partial::from_partial(PartialCycle { half_edges }),
..Default::default()
}
};
Expand Down
53 changes: 8 additions & 45 deletions crates/fj-kernel/src/partial/objects/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
};

/// A partial [`Curve`]
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Default)]
pub struct PartialCurve {
/// The path that defines the curve
pub path: Option<SurfacePath>,
Expand All @@ -18,33 +18,15 @@ pub struct PartialCurve {
pub global_form: Partial<GlobalCurve>,
}

impl PartialCurve {
/// Construct an instance of `PartialCurve`
pub fn new(
path: Option<SurfacePath>,
surface: Option<Partial<Surface>>,
global_form: Option<Partial<GlobalCurve>>,
) -> Self {
let surface = surface.unwrap_or_default();
let global_form = global_form.unwrap_or_default();

Self {
path,
surface,
global_form,
}
}
}

impl PartialObject for PartialCurve {
type Full = Curve;

fn from_full(curve: &Self::Full, cache: &mut FullToPartialCache) -> Self {
Self::new(
Some(curve.path()),
Some(Partial::from_full(curve.surface().clone(), cache)),
Some(Partial::from_full(curve.global_form().clone(), cache)),
)
Self {
path: Some(curve.path()),
surface: Partial::from_full(curve.surface().clone(), cache),
global_form: Partial::from_full(curve.global_form().clone(), cache),
}
}

fn build(self, objects: &mut Service<Objects>) -> Self::Full {
Expand All @@ -56,37 +38,18 @@ impl PartialObject for PartialCurve {
}
}

impl Default for PartialCurve {
fn default() -> Self {
Self::new(None, None, None)
}
}

/// A partial [`GlobalCurve`]
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Default)]
pub struct PartialGlobalCurve;

impl PartialGlobalCurve {
/// Construct an instance of `PartialGlobalCurve`
pub fn new() -> Self {
Self
}
}

impl PartialObject for PartialGlobalCurve {
type Full = GlobalCurve;

fn from_full(_: &Self::Full, _: &mut FullToPartialCache) -> Self {
Self::new()
Self
}

fn build(self, _: &mut Service<Objects>) -> Self::Full {
GlobalCurve
}
}

impl Default for PartialGlobalCurve {
fn default() -> Self {
Self::new()
}
}
19 changes: 4 additions & 15 deletions crates/fj-kernel/src/partial/objects/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,13 @@ use crate::{
};

/// A partial [`Cycle`]
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Default)]
pub struct PartialCycle {
/// The half-edges that make up the cycle
pub half_edges: Vec<Partial<HalfEdge>>,
}

impl PartialCycle {
/// Construct an instance of `PartialCycle`
pub fn new(half_edges: Vec<Partial<HalfEdge>>) -> Self {
Self { half_edges }
}

/// Access the surface of the [`Cycle`]
pub fn surface(&self) -> Option<Partial<Surface>> {
self.half_edges
Expand All @@ -29,13 +24,13 @@ impl PartialObject for PartialCycle {
type Full = Cycle;

fn from_full(cycle: &Self::Full, cache: &mut FullToPartialCache) -> Self {
Self::new(
cycle
Self {
half_edges: cycle
.half_edges()
.cloned()
.map(|half_edge| Partial::from_full(half_edge, cache))
.collect(),
)
}
}

fn build(self, objects: &mut Service<Objects>) -> Self::Full {
Expand All @@ -47,9 +42,3 @@ impl PartialObject for PartialCycle {
Cycle::new(half_edges)
}
}

impl Default for PartialCycle {
fn default() -> Self {
Self::new(Vec::new())
}
}
111 changes: 42 additions & 69 deletions crates/fj-kernel/src/partial/objects/edge.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::array;

use fj_interop::ext::ArrayExt;

use crate::{
Expand All @@ -19,44 +21,6 @@ pub struct PartialHalfEdge {
}

impl PartialHalfEdge {
/// Construct an instance of `PartialHalfEdge`
pub fn new(
vertices: [Option<Partial<Vertex>>; 2],
global_form: Option<Partial<GlobalEdge>>,
) -> Self {
let curve = Partial::<Curve>::new();

let vertices = vertices.map(|vertex| {
vertex.unwrap_or_else(|| {
Partial::from_partial(PartialVertex::new(
None,
Some(curve.clone()),
None,
))
})
});

let global_curve = curve.read().global_form.clone();
let global_vertices =
vertices.each_ref_ext().map(|vertex: &Partial<Vertex>| {
let surface_vertex = vertex.read().surface_form.clone();
let global_vertex = surface_vertex.read().global_form.clone();
Some(global_vertex)
});

let global_form = global_form.unwrap_or_else(|| {
Partial::from_partial(PartialGlobalEdge::new(
Some(global_curve),
global_vertices,
))
});

Self {
vertices,
global_form,
}
}

/// Access the curve the partial edge is defined on
pub fn curve(&self) -> Partial<Curve> {
let [vertex, _] = &self.vertices;
Expand All @@ -71,13 +35,16 @@ impl PartialObject for PartialHalfEdge {
half_edge: &Self::Full,
cache: &mut FullToPartialCache,
) -> Self {
Self::new(
half_edge
Self {
vertices: half_edge
.vertices()
.clone()
.map(|vertex| Some(Partial::from_full(vertex, cache))),
Some(Partial::from_full(half_edge.global_form().clone(), cache)),
)
.map(|vertex| Partial::from_full(vertex, cache)),
global_form: Partial::from_full(
half_edge.global_form().clone(),
cache,
),
}
}

fn build(self, objects: &mut Service<Objects>) -> Self::Full {
Expand All @@ -90,12 +57,37 @@ impl PartialObject for PartialHalfEdge {

impl Default for PartialHalfEdge {
fn default() -> Self {
Self::new([None, None], None)
let curve = Partial::<Curve>::new();

let vertices = array::from_fn(|_| {
Partial::from_partial(PartialVertex {
curve: curve.clone(),
..Default::default()
})
});

let global_curve = curve.read().global_form.clone();
let global_vertices =
vertices.each_ref_ext().map(|vertex: &Partial<Vertex>| {
let surface_vertex = vertex.read().surface_form.clone();
let global_vertex = surface_vertex.read().global_form.clone();
global_vertex
});

let global_form = Partial::from_partial(PartialGlobalEdge {
curve: global_curve,
vertices: global_vertices,
});

Self {
vertices,
global_form,
}
}
}

/// A partial [`GlobalEdge`]
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Default)]
pub struct PartialGlobalEdge {
/// The curve that defines the edge's geometry
pub curve: Partial<GlobalCurve>,
Expand All @@ -104,33 +96,20 @@ pub struct PartialGlobalEdge {
pub vertices: [Partial<GlobalVertex>; 2],
}

impl PartialGlobalEdge {
/// Construct an instance of `PartialGlobalEdge`
pub fn new(
curve: Option<Partial<GlobalCurve>>,
vertices: [Option<Partial<GlobalVertex>>; 2],
) -> Self {
let curve = curve.unwrap_or_default();
let vertices = vertices.map(Option::unwrap_or_default);

Self { curve, vertices }
}
}

impl PartialObject for PartialGlobalEdge {
type Full = GlobalEdge;

fn from_full(
global_edge: &Self::Full,
cache: &mut FullToPartialCache,
) -> Self {
Self::new(
Some(Partial::from_full(global_edge.curve().clone(), cache)),
global_edge
Self {
curve: Partial::from_full(global_edge.curve().clone(), cache),
vertices: global_edge
.vertices()
.access_in_normalized_order()
.map(|vertex| Some(Partial::from_full(vertex, cache))),
)
.map(|vertex| Partial::from_full(vertex, cache)),
}
}

fn build(self, objects: &mut Service<Objects>) -> Self::Full {
Expand All @@ -140,9 +119,3 @@ impl PartialObject for PartialGlobalEdge {
GlobalEdge::new(curve, vertices)
}
}

impl Default for PartialGlobalEdge {
fn default() -> Self {
Self::new(None, [None, None])
}
}
36 changes: 7 additions & 29 deletions crates/fj-kernel/src/partial/objects/face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
};

/// A partial [`Face`]
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Default)]
pub struct PartialFace {
/// The cycle that bounds the face on the outside
pub exterior: Partial<Cycle>,
Expand All @@ -21,34 +21,18 @@ pub struct PartialFace {
pub color: Option<Color>,
}

impl PartialFace {
/// Construct an instance of `PartialFace`
pub fn new(
exterior: Option<Partial<Cycle>>,
interiors: Vec<Partial<Cycle>>,
color: Option<Color>,
) -> Self {
let exterior = exterior.unwrap_or_default();

Self {
exterior,
interiors,
color,
}
}
}

impl PartialObject for PartialFace {
type Full = Face;

fn from_full(face: &Self::Full, cache: &mut FullToPartialCache) -> Self {
Self::new(
Some(Partial::from_full(face.exterior().clone(), cache)),
face.interiors()
Self {
exterior: Partial::from_full(face.exterior().clone(), cache),
interiors: face
.interiors()
.map(|cycle| Partial::from_full(cycle.clone(), cache))
.collect(),
Some(face.color()),
)
color: Some(face.color()),
}
}

fn build(self, objects: &mut Service<Objects>) -> Self::Full {
Expand All @@ -60,9 +44,3 @@ impl PartialObject for PartialFace {
Face::new(exterior, interiors, color)
}
}

impl Default for PartialFace {
fn default() -> Self {
Self::new(None, Vec::new(), None)
}
}
Loading