Repository navigation
feat(brep): make the BRep builder public #36
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
base: main
Are you sure you want to change the base?
Changes from all commits
11a45f1
977bb1e
b2f653c
1db7aa6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,35 @@ | ||
| use crate::brep::accuracy::Accuracy; | ||
| use crate::brep::error::GeometryError; | ||
| use crate::brep::error::{missing, GeometryError}; | ||
| use crate::brep::geometry::{CurveGeometry, SurfaceGeometry}; | ||
| use crate::brep::pcurve::PcurveGeometry; | ||
| use crate::brep::topology::{ | ||
| BrepEnvelope, Edge, EdgeGeometry, Face, FaceProvenance, FaceRole, FaceSource, HalfEdge, | ||
| HalfEdgeGeometryUse, Loop, Orientation, Shell, SolidRegion, TrimRegion, Vertex, | ||
| }; | ||
| use crate::math::{Interval, Point3}; | ||
| use std::collections::BTreeMap; | ||
|
|
||
| pub(crate) struct Use { | ||
| pub struct Use { | ||
| pub(super) edge: u32, | ||
| pub(super) from: u32, | ||
| pub(super) to: u32, | ||
| pub(super) sense: Orientation, | ||
| pub(super) pcurve: PcurveGeometry, | ||
| } | ||
|
|
||
| pub(crate) struct Builder { | ||
| pub struct Builder { | ||
| pub(crate) brep: BrepEnvelope, | ||
| } | ||
| impl Builder { | ||
| pub(crate) fn new(id: String, accuracy: Accuracy) -> Result<Self, GeometryError> { | ||
| pub fn new(id: String, accuracy: Accuracy) -> Result<Self, GeometryError> { | ||
| Ok(Self { | ||
| brep: BrepEnvelope::new(id, accuracy)?, | ||
| }) | ||
| } | ||
| pub(crate) fn vertex(&mut self, p: Point3) -> u32 { | ||
| pub fn brep(&self) -> &BrepEnvelope { | ||
| &self.brep | ||
| } | ||
| pub fn vertex(&mut self, p: Point3) -> u32 { | ||
| let id = self.brep.topology.vertices.len() as u32; | ||
| self.brep.topology.vertices.push(Vertex { | ||
| id, | ||
|
|
@@ -35,7 +39,7 @@ impl Builder { | |
| }); | ||
| id | ||
| } | ||
| pub(crate) fn edge(&mut self, curve: CurveGeometry, range: Interval, seam: bool) -> u32 { | ||
| pub fn edge(&mut self, curve: CurveGeometry, range: Interval, seam: bool) -> u32 { | ||
| let curve_id = self.brep.geometry.curves.len() as u32; | ||
| self.brep.geometry.curves.push(curve); | ||
| self.edge_geometry( | ||
|
|
@@ -64,35 +68,59 @@ impl Builder { | |
| is_hole: bool, | ||
| uses: Vec<Use>, | ||
| ) -> Result<u32, GeometryError> { | ||
| self.check_loops([&uses])?; | ||
| Ok(self.write_loop(face, is_hole, uses)) | ||
| } | ||
| fn check_loops<'a>( | ||
| &self, | ||
| loops: impl IntoIterator<Item = &'a Vec<Use>>, | ||
| ) -> Result<(), GeometryError> { | ||
| let topology = &self.brep.topology; | ||
| let mut uses_of_edge = BTreeMap::new(); | ||
| for uses in loops { | ||
| if uses.is_empty() { | ||
| return Err(GeometryError::InvalidTopology( | ||
| "empty primitive face".into(), | ||
| )); | ||
| } | ||
| for u in uses { | ||
| let edge = topology | ||
| .edges | ||
| .get(u.edge as usize) | ||
| .ok_or_else(|| missing("edge", u.edge))?; | ||
| if let Some(&vertex) = [u.from, u.to] | ||
| .iter() | ||
| .find(|&&vertex| vertex as usize >= topology.vertices.len()) | ||
| { | ||
| return Err(missing("vertex", vertex)); | ||
| } | ||
| let uses = uses_of_edge.entry(u.edge).or_insert( | ||
| usize::from(edge.halfedge != u32::MAX) | ||
| + usize::from(edge.twin_halfedge.is_some()), | ||
| ); | ||
| *uses += 1; | ||
| if *uses > 2 { | ||
| return Err(GeometryError::InvalidTopology( | ||
| "nonmanifold primitive edge".into(), | ||
| )); | ||
| } | ||
| } | ||
| } | ||
| Ok(()) | ||
| } | ||
| fn write_loop(&mut self, face: u32, is_hole: bool, uses: Vec<Use>) -> u32 { | ||
| let loop_id = self.brep.topology.loops.len() as u32; | ||
| let start = self.brep.topology.halfedges.len() as u32; | ||
| let count = uses.len() as u32; | ||
| if count == 0 { | ||
| return Err(GeometryError::InvalidTopology( | ||
| "empty primitive face".into(), | ||
| )); | ||
| } | ||
| for (i, u) in uses.into_iter().enumerate() { | ||
| let id = start + i as u32; | ||
| let pcurve = self.brep.geometry.pcurves.len() as u32; | ||
| self.brep.geometry.pcurves.push(u.pcurve); | ||
| let e = self | ||
| .brep | ||
| .topology | ||
| .edges | ||
| .get_mut(u.edge as usize) | ||
| .ok_or_else(|| { | ||
| GeometryError::InvalidTopology("builder references missing edge".into()) | ||
| })?; | ||
| let e = &mut self.brep.topology.edges[u.edge as usize]; | ||
| let twin = if e.halfedge == u32::MAX { | ||
| e.halfedge = id; | ||
| None | ||
| } else { | ||
| if e.twin_halfedge.is_some() { | ||
| return Err(GeometryError::InvalidTopology( | ||
| "nonmanifold primitive edge".into(), | ||
| )); | ||
| } | ||
| e.twin_halfedge = Some(id); | ||
| self.brep.topology.halfedges[e.halfedge as usize].twin = Some(id); | ||
| Some(e.halfedge) | ||
|
|
@@ -127,44 +155,46 @@ impl Builder { | |
| face_ref: face, | ||
| is_hole, | ||
| }); | ||
| Ok(loop_id) | ||
| loop_id | ||
| } | ||
| pub(crate) fn face( | ||
| pub fn face( | ||
| &mut self, | ||
| key: &str, | ||
| surface: SurfaceGeometry, | ||
| bounds: [[f64; 2]; 2], | ||
| uses: Vec<Use>, | ||
| ) -> Result<(), GeometryError> { | ||
| ) -> Result<u32, GeometryError> { | ||
| self.face_with_holes(key, surface, bounds, uses, Vec::new()) | ||
| } | ||
| pub(crate) fn face_with_holes( | ||
| pub fn face_with_holes( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A rejected Suggestion: in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in 1db7aa6. |
||
| &mut self, | ||
| key: &str, | ||
| surface: SurfaceGeometry, | ||
| bounds: [[f64; 2]; 2], | ||
| outer_uses: Vec<Use>, | ||
| hole_uses: Vec<Vec<Use>>, | ||
| ) -> Result<(), GeometryError> { | ||
| ) -> Result<u32, GeometryError> { | ||
| let uv_bounds = [ | ||
| Interval::new(bounds[0][0], bounds[0][1])?, | ||
| Interval::new(bounds[1][0], bounds[1][1])?, | ||
| ]; | ||
| self.check_loops(std::iter::once(&outer_uses).chain(&hole_uses))?; | ||
| let surface_id = self.brep.geometry.surfaces.len() as u32; | ||
| self.brep.geometry.surfaces.push(surface); | ||
| let face = self.brep.topology.faces.len() as u32; | ||
| let outer = self.face_loop(face, false, outer_uses)?; | ||
| let mut holes = Vec::with_capacity(hole_uses.len()); | ||
| for uses in hole_uses { | ||
| holes.push(self.face_loop(face, true, uses)?); | ||
| } | ||
| let outer = self.write_loop(face, false, outer_uses); | ||
| let holes = hole_uses | ||
| .into_iter() | ||
| .map(|uses| self.write_loop(face, true, uses)) | ||
| .collect(); | ||
| self.brep.topology.faces.push(Face { | ||
| id: face, | ||
| key: key.into(), | ||
| surface: surface_id, | ||
| sense: Orientation::Forward, | ||
| trim: TrimRegion { | ||
| chart: 0, | ||
| uv_bounds: [ | ||
| Interval::new(bounds[0][0], bounds[0][1])?, | ||
| Interval::new(bounds[1][0], bounds[1][1])?, | ||
| ], | ||
| uv_bounds, | ||
| outer, | ||
| holes, | ||
| }, | ||
|
|
@@ -180,9 +210,26 @@ impl Builder { | |
| reversed: false, | ||
| }, | ||
| }); | ||
| Ok(face) | ||
| } | ||
| pub fn face_sense(&self, face: u32) -> Result<Orientation, GeometryError> { | ||
| self.brep | ||
| .topology | ||
| .faces | ||
| .get(face as usize) | ||
| .map(|face| face.sense) | ||
| .ok_or_else(|| missing("face", face)) | ||
| } | ||
| pub fn set_face_sense(&mut self, face: u32, sense: Orientation) -> Result<(), GeometryError> { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggestion: say in the docs that the builder does not check orientation and the caller owns it, add a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in 1db7aa6:
|
||
| self.brep | ||
| .topology | ||
| .faces | ||
| .get_mut(face as usize) | ||
| .ok_or_else(|| missing("face", face))? | ||
| .sense = sense; | ||
| Ok(()) | ||
| } | ||
| pub(crate) fn finish_solid(mut self) -> Result<BrepEnvelope, GeometryError> { | ||
| pub fn finish_solid(mut self) -> Result<BrepEnvelope, GeometryError> { | ||
| let faces = (0..self.brep.topology.faces.len() as u32).collect(); | ||
| self.brep.topology.shells.push(Shell { | ||
| id: 0, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bad ids panic.
plane_boundaryindexesedges[edge]andface_loopindexesvertices[u.from]unchecked.Suggestion: use
.get(..).ok_or(GeometryError::MissingReference { .. }), asset_face_sensealready does, and checktoas well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 1db7aa6.
plane_boundarylooks up the edge and its curve with.get(..)and returnsMissingReference { kind: "edge" | "curve", .. }.faceandface_with_holescheck every use's edge and both itsfromandtovertices before they write anything (next thread), withMissingReference { kind: "edge" | "vertex", .. }.face_loopindexes only after that check.