Skip to content

Commit

Permalink
Merge pull request #823 from hannobraun/solid
Browse files Browse the repository at this point in the history
Add `Solid` object
  • Loading branch information
hannobraun authored Jul 15, 2022
2 parents 5cb21fa + e59b0c0 commit f1f0742
Show file tree
Hide file tree
Showing 7 changed files with 259 additions and 8 deletions.
6 changes: 3 additions & 3 deletions crates/fj-kernel/src/algorithms/sweep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
iter::ObjectIters,
local::Local,
objects::{
Curve, Cycle, Edge, Face, GlobalVertex, Sketch, Surface, Vertex,
Curve, Cycle, Edge, Face, GlobalVertex, Sketch, Solid, Surface, Vertex,
VerticesOfEdge,
},
};
Expand All @@ -17,7 +17,7 @@ pub fn sweep(
path: impl Into<Vector<3>>,
tolerance: Tolerance,
color: [u8; 4],
) -> Vec<Face> {
) -> Solid {
let path = path.into();

let is_sweep_along_negative_direction =
Expand Down Expand Up @@ -62,7 +62,7 @@ pub fn sweep(
}
}

target
Solid::from_faces(target)
}

fn create_bottom_faces(
Expand Down
10 changes: 9 additions & 1 deletion crates/fj-kernel/src/algorithms/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
local::Local,
objects::{
Curve, Cycle, CyclesInFace, Edge, Face, FaceBRep, GlobalVertex, Sketch,
Surface, Vertex,
Solid, Surface, Vertex,
},
};

Expand Down Expand Up @@ -116,6 +116,14 @@ impl TransformObject for Sketch {
}
}

impl TransformObject for Solid {
fn transform(self, transform: &Transform) -> Self {
let mut faces = self.into_faces();
transform_faces(&mut faces, transform);
Self::from_faces(faces)
}
}

impl TransformObject for Surface {
fn transform(self, transform: &Transform) -> Self {
match self {
Expand Down
186 changes: 184 additions & 2 deletions crates/fj-kernel/src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::collections::VecDeque;

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

/// Access iterators over all objects of a shape, or part of it
Expand All @@ -29,6 +29,9 @@ pub trait ObjectIters {
/// Iterate over all sketches
fn sketch_iter(&self) -> Iter<Sketch>;

/// Iterate over all solids
fn solid_iter(&self) -> Iter<Solid>;

/// Iterate over all surfaces
fn surface_iter(&self) -> Iter<Surface>;

Expand Down Expand Up @@ -61,6 +64,10 @@ impl ObjectIters for Curve<3> {
Iter::empty()
}

fn solid_iter(&self) -> Iter<Solid> {
Iter::empty()
}

fn surface_iter(&self) -> Iter<Surface> {
Iter::empty()
}
Expand Down Expand Up @@ -125,6 +132,16 @@ impl ObjectIters for Cycle {
iter
}

fn solid_iter(&self) -> Iter<Solid> {
let mut iter = Iter::empty();

for edge in self.edges() {
iter = iter.with(edge.solid_iter());
}

iter
}

fn surface_iter(&self) -> Iter<Surface> {
let mut iter = Iter::empty();

Expand Down Expand Up @@ -201,6 +218,16 @@ impl ObjectIters for Edge {
iter
}

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

for vertex in self.vertices().into_iter().flatten() {
iter = iter.with(vertex.solid_iter());
}

iter
}

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

Expand Down Expand Up @@ -298,6 +325,20 @@ impl ObjectIters for Face {
Iter::empty()
}

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

for cycle in face.all_cycles() {
iter = iter.with(cycle.solid_iter());
}

return iter;
}

Iter::empty()
}

fn surface_iter(&self) -> Iter<Surface> {
if let Face::Face(face) = self {
let mut iter = Iter::empty().with(face.surface().surface_iter());
Expand Down Expand Up @@ -352,6 +393,10 @@ impl ObjectIters for GlobalVertex {
Iter::empty()
}

fn solid_iter(&self) -> Iter<Solid> {
Iter::empty()
}

fn surface_iter(&self) -> Iter<Surface> {
Iter::empty()
}
Expand Down Expand Up @@ -416,6 +461,102 @@ impl ObjectIters for Sketch {
Iter::from_object(self.clone())
}

fn solid_iter(&self) -> Iter<Solid> {
let mut iter = Iter::empty();

for edge in self.faces() {
iter = iter.with(edge.solid_iter());
}

iter
}

fn surface_iter(&self) -> Iter<Surface> {
let mut iter = Iter::empty();

for edge in self.faces() {
iter = iter.with(edge.surface_iter());
}

iter
}

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

for edge in self.faces() {
iter = iter.with(edge.vertex_iter());
}

iter
}
}

impl ObjectIters for Solid {
fn curve_iter(&self) -> Iter<Curve<3>> {
let mut iter = Iter::empty();

for edge in self.faces() {
iter = iter.with(edge.curve_iter());
}

iter
}

fn cycle_iter(&self) -> Iter<Cycle> {
let mut iter = Iter::empty();

for edge in self.faces() {
iter = iter.with(edge.cycle_iter());
}

iter
}

fn edge_iter(&self) -> Iter<Edge> {
let mut iter = Iter::empty();

for edge in self.faces() {
iter = iter.with(edge.edge_iter());
}

iter
}

fn face_iter(&self) -> Iter<Face> {
let mut iter = Iter::empty();

for edge in self.faces() {
iter = iter.with(edge.face_iter());
}

iter
}

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

for edge in self.faces() {
iter = iter.with(edge.global_vertex_iter());
}

iter
}

fn sketch_iter(&self) -> Iter<Sketch> {
let mut iter = Iter::empty();

for edge in self.faces() {
iter = iter.with(edge.sketch_iter());
}

iter
}

fn solid_iter(&self) -> Iter<Solid> {
Iter::from_object(self.clone())
}

fn surface_iter(&self) -> Iter<Surface> {
let mut iter = Iter::empty();

Expand Down Expand Up @@ -462,6 +603,10 @@ impl ObjectIters for Surface {
Iter::empty()
}

fn solid_iter(&self) -> Iter<Solid> {
Iter::empty()
}

fn surface_iter(&self) -> Iter<Surface> {
Iter::from_object(*self)
}
Expand Down Expand Up @@ -496,6 +641,10 @@ impl ObjectIters for Vertex {
self.global().sketch_iter()
}

fn solid_iter(&self) -> Iter<Solid> {
self.global().solid_iter()
}

fn surface_iter(&self) -> Iter<Surface> {
self.global().surface_iter()
}
Expand Down Expand Up @@ -575,6 +724,16 @@ where
iter
}

fn solid_iter(&self) -> Iter<Solid> {
let mut iter = Iter::empty();

for object in self.into_iter() {
iter = iter.with(object.solid_iter());
}

iter
}

fn surface_iter(&self) -> Iter<Surface> {
let mut iter = Iter::empty();

Expand Down Expand Up @@ -637,7 +796,7 @@ impl<T> Iterator for Iter<T> {
#[cfg(test)]
mod tests {
use crate::objects::{
Curve, Cycle, Edge, Face, GlobalVertex, Sketch, Surface, Vertex,
Curve, Cycle, Edge, Face, GlobalVertex, Sketch, Solid, Surface, Vertex,
};

use super::ObjectIters as _;
Expand All @@ -652,6 +811,7 @@ mod tests {
assert_eq!(0, object.face_iter().count());
assert_eq!(0, object.global_vertex_iter().count());
assert_eq!(0, object.sketch_iter().count());
assert_eq!(0, object.solid_iter().count());
assert_eq!(0, object.surface_iter().count());
assert_eq!(0, object.vertex_iter().count());
}
Expand All @@ -669,6 +829,7 @@ mod tests {
assert_eq!(0, object.face_iter().count());
assert_eq!(3, object.global_vertex_iter().count());
assert_eq!(0, object.sketch_iter().count());
assert_eq!(0, object.solid_iter().count());
assert_eq!(0, object.surface_iter().count());
assert_eq!(6, object.vertex_iter().count());
}
Expand All @@ -686,6 +847,7 @@ mod tests {
assert_eq!(0, object.face_iter().count());
assert_eq!(2, object.global_vertex_iter().count());
assert_eq!(0, object.sketch_iter().count());
assert_eq!(0, object.solid_iter().count());
assert_eq!(0, object.surface_iter().count());
assert_eq!(2, object.vertex_iter().count());
}
Expand All @@ -702,6 +864,7 @@ mod tests {
assert_eq!(1, object.face_iter().count());
assert_eq!(3, object.global_vertex_iter().count());
assert_eq!(0, object.sketch_iter().count());
assert_eq!(0, object.solid_iter().count());
assert_eq!(1, object.surface_iter().count());
assert_eq!(6, object.vertex_iter().count());
}
Expand All @@ -716,6 +879,7 @@ mod tests {
assert_eq!(0, object.face_iter().count());
assert_eq!(1, object.global_vertex_iter().count());
assert_eq!(0, object.sketch_iter().count());
assert_eq!(0, object.solid_iter().count());
assert_eq!(0, object.surface_iter().count());
assert_eq!(0, object.vertex_iter().count());
}
Expand All @@ -733,10 +897,26 @@ mod tests {
assert_eq!(1, object.face_iter().count());
assert_eq!(3, object.global_vertex_iter().count());
assert_eq!(1, object.sketch_iter().count());
assert_eq!(0, object.solid_iter().count());
assert_eq!(1, object.surface_iter().count());
assert_eq!(6, object.vertex_iter().count());
}

#[test]
fn solid() {
let object = Solid::cube_from_edge_length(1.);

assert_eq!(18, object.curve_iter().count());
assert_eq!(6, object.cycle_iter().count());
assert_eq!(20, object.edge_iter().count());
assert_eq!(6, object.face_iter().count());
assert_eq!(8, object.global_vertex_iter().count());
assert_eq!(0, object.sketch_iter().count());
assert_eq!(1, object.solid_iter().count());
assert_eq!(6, object.surface_iter().count());
assert_eq!(16, object.vertex_iter().count());
}

#[test]
fn surface() {
let object = Surface::xy_plane();
Expand All @@ -747,6 +927,7 @@ mod tests {
assert_eq!(0, object.face_iter().count());
assert_eq!(0, object.global_vertex_iter().count());
assert_eq!(0, object.sketch_iter().count());
assert_eq!(0, object.solid_iter().count());
assert_eq!(1, object.surface_iter().count());
assert_eq!(0, object.vertex_iter().count());
}
Expand All @@ -762,6 +943,7 @@ mod tests {
assert_eq!(0, object.face_iter().count());
assert_eq!(1, object.global_vertex_iter().count());
assert_eq!(0, object.sketch_iter().count());
assert_eq!(0, object.solid_iter().count());
assert_eq!(0, object.surface_iter().count());
assert_eq!(1, object.vertex_iter().count());
}
Expand Down
2 changes: 2 additions & 0 deletions crates/fj-kernel/src/objects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod edge;
mod face;
mod global_vertex;
mod sketch;
mod solid;
mod surface;
mod vertex;

Expand All @@ -20,6 +21,7 @@ pub use self::{
face::{CyclesInFace, Face, FaceBRep},
global_vertex::GlobalVertex,
sketch::Sketch,
solid::Solid,
surface::{Surface, SweptCurve},
vertex::Vertex,
};
Loading

0 comments on commit f1f0742

Please sign in to comment.