Skip to content

feat(brep): make the BRep builder public - #36

Open
Basnederveen wants to merge 4 commits into
OpenGeometry-io:mainfrom
Bas-Automation-Services:feat/public-brep-builder
Open

Basnederveen wants to merge 4 commits into
OpenGeometry-io:mainfrom
Bas-Automation-Services:feat/public-brep-builder

Conversation

@Basnederveen

@Basnederveen Basnederveen commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

What

The kernel builds every primitive and boolean result face by face with brep::Builder, but the builder is crate-private. A crate that depends on OpenGeometry and needs a solid the primitives don't cover (for example a part made of planar faces and cylinder sectors, laid out by its own rules) can't build one, except by writing BRep JSON by hand and reading it back with BrepEnvelope::from_json. That path skips the checks the builder does as it goes (an edge used by a third loop, a missing edge) and gives no face ids back.

This makes the builder usable from outside the crate, with the smallest surface that can describe a face:

  • brep::Builder: new, vertex, edge, face, face_with_holes, finish_solid, plus two new methods:
    • brep(): read access to the body under construction (e.g. to look up an edge's curve);
    • set_face_sense(face, Orientation) and face_sense(face): a face whose material normal opposes its surface normal (the inside of a tube), without reaching into the topology.
  • face and face_with_holes return the new face's id instead of (), so set_face_sense can use it. The 14 internal call sites that used the call as a tail expression now end in ?; Ok(()).
  • brep::{Use, boundary, plane_boundary, uv_line, uv_conic, UvCurve}: the loop edge uses. UvCurve is opaque: uv_line and uv_conic make one, and boundary takes it. PcurveGeometry stays crate-private.
  • Types those signatures need, made public: brep::Orientation (already part of the BRep JSON schema) and math::Interval::new.

The builder trusts its caller less than it did inside the crate: an unknown edge, curve or vertex id is a MissingReference instead of a panic, and face/face_with_holes check every use (ids, an edge's third use, empty loops) and the UV bounds before writing, so a rejected face leaves the builder unchanged. finish_solid validates topology, not orientation: the caller owns each face's sense. developer.md has a short section saying so and how to check it.

edge_geometry, face_loop, finish_wire, WireUse, dimensions and padded_uv_bounds stay crate-private. Nothing changes for the TypeScript API.

Tests

  • New tests/public_brep_builder.rs, using only the public API:
    • a 2 x 3 x 4 box built face by face: face ids 0 to 5, finish_solid and validate pass, 12 edges and 24 half-edges, volume 24, a point inside classifies as Inside and one outside as Outside, STEP export writes 6 faces;
    • a 4 x 4 x 1 plate with a radius-1 through-hole: holed bottom and top faces (via plane_boundary and via boundary + uv_conic), a cylindrical bore with a seam edge turned to Reverse; volume 16 − π;
    • every solid the tests build has each face probed: a point just off it along face_normal_at classifies as Outside;
    • an all-Reverse box passes finish_solid and every probe is Inside (the builder doesn't check orientation);
    • set_face_sense / face_sense by face id; unknown edge, vertex and face ids are MissingReference;
    • a face rejected for a third use of an edge, or an unknown vertex, leaves the builder unchanged, and the corrected face is then added and the solid finishes.
  • cargo fmt --check and cargo clippy --all-targets -D warnings: clean.
  • cargo test --all-targets --no-fail-fast (Windows x86_64): 945 pass, and the same 6 stored-fixture and digest comparisons fail as on main on this machine (listed on feat(exchange): export_step_in for bodies in a unit other than metres #35). With CI=true, cases, booleans, golden and step_oracle all pass.
  • npm run check:source, check:duplicates, check:cycles: clean. check:api reads the built dist/ and was not run (this change has no TypeScript or WASM surface).

Files

  • main/opengeometry/src/brep/builder/{brep_builder,boundary,mod}.rs, src/brep/mod.rs, src/brep/topology/{mod,orientation}.rs, src/math/interval.rs: visibility, the face id return, brep(), face_sense/set_face_sense, UvCurve/uv_conic, checks before writing
  • developer.md: "Building a B-rep from Rust"
  • 3 files under src/operations/modifying/boolean/handlers/: uv_line results converted where a PcurveGeometry is stored
  • 10 files under src/primitives/ and src/operations/modifying/boolean/handlers/cylinder_cylinder/: tail calls of face now return Ok(())
  • main/opengeometry/tests/public_brep_builder.rs (new)

🤖 Generated with Claude Code

A crate that depends on OpenGeometry and needs a solid the primitives
don't cover can only build one by writing BRep JSON by hand. The builder
the kernel uses for every primitive and boolean result is crate-private.

- brep::{Builder, Use, boundary, plane_boundary, uv_line} are public:
  Builder::{new, vertex, edge, face, face_with_holes, finish_solid}
- face() and face_with_holes() return the new face id
- Builder::brep() gives read access while building
- Builder::set_face_sense() sets a face's sense by id
- the types those signatures need are public: brep::Orientation,
  brep::PcurveGeometry (and IntersectionSide), math::Interval::new
- the 14 internal tail calls of face() now end in `?; Ok(())`

tests/public_brep_builder.rs builds a box through the public API only:
valid, volume 24, inside/outside classification, STEP export; a face
sense set by id flips face_normal_at; an edge used by a third loop is
rejected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VFZvqx1RmXzPtbL9VXP4H7

@aka-blackboots aka-blackboots left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, the need is real. The builder assumes trusted callers, though, so before it goes public I'd like the five points in the inline comments addressed.

}

pub(crate) fn plane_boundary(
pub fn plane_boundary(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bad ids panic. plane_boundary indexes edges[edge] and face_loop indexes vertices[u.from] unchecked.

Suggestion: use .get(..).ok_or(GeometryError::MissingReference { .. }), as set_face_sense already does, and check to as well.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 1db7aa6. plane_boundary looks up the edge and its curve with .get(..) and returns MissingReference { kind: "edge" | "curve", .. }. face and face_with_holes check every use's edge and both its from and to vertices before they write anything (next thread), with MissingReference { kind: "edge" | "vertex", .. }. face_loop indexes only after that check.

self.face_with_holes(key, surface, bounds, uses, Vec::new())
}
pub(crate) fn face_with_holes(
pub fn face_with_holes(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A rejected face() leaves the builder half-written, so a corrected retry fails with "nonmanifold primitive edge".

Suggestion: in face_with_holes, check every use (edge exists, vertices exist, edge not already used twice) and build the two Intervals first, then write.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 1db7aa6. face_with_holes first builds the two Intervals, then checks every loop together: no empty loop, the edge and vertices exist, and no edge ends up with more than two uses, counting the uses it already has plus those in this face, so a seam used twice in one face is counted correctly. Only then does it push the surface, the loops and the face. The writing step can't fail. a_rejected_face_leaves_the_builder_unchanged builds five box faces and tries the sixth with a third use of an edge, then with an unknown vertex. Both are rejected, the builder's Debug output is unchanged, and the corrected sixth face gets id 5 and finishes as a solid.

});
Ok(face)
}
pub fn set_face_sense(&mut self, face: u32, sense: Orientation) -> Result<(), GeometryError> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

finish_solid accepts an inside-out solid after set_face_sense; validate() never reads a face's sense.

Suggestion: say in the docs that the builder does not check orientation and the caller owns it, add a face_sense getter, and have the tests prove it for the solids they build: for each face, a point just off the face along face_normal_at classifies as Outside.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 1db7aa6:

  • Docs: OG's zero-comments rule also covers ///, so the note is a short "Building a B-rep from Rust" section in developer.md. It says finish_solid validates topology and not orientation, that the caller owns each face's sense, and how to check it: a point just off each face along face_normal_at should classify as Outside. If you'd rather have it somewhere else, tell me where.
  • Getter: Builder::face_sense(face) -> Result<Orientation, GeometryError>, with MissingReference for an unknown id.
  • Tests: assert_faces_point_outward checks exactly that for every face of the solids the tests build (the box, the plate with a reversed bore, and the box finished after a rejected face). the_builder_leaves_orientation_to_the_caller turns every face of a box to Reverse, and finish_solid accepts it. Every off-face point then classifies as Inside, which pins the documented behaviour.

Comment thread main/opengeometry/src/brep/pcurve.rs Outdated
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", deny_unknown_fields)]
pub(crate) enum PcurveGeometry {
pub enum PcurveGeometry {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PcurveGeometry is exposed whole, including two variants an outside caller can't build.

Suggestion: keep the enum private and add a uv_conic(origin, axis_a, axis_b) beside uv_line, both returning an opaque value that boundary() takes. If it must stay public, mark it #[non_exhaustive].

@Basnederveen Basnederveen Nov 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 1db7aa6. PcurveGeometry and IntersectionSide are crate-private again, as on main. The public surface is an opaque brep::UvCurve. uv_line(origin, direction) and the new uv_conic(origin, axis_a, axis_b) return it, and boundary(edge, from, to, sense, pcurve: impl Into<UvCurve>) takes it. Inside the crate, a From<PcurveGeometry> impl lets the ~120 internal boundary calls keep passing their pcurves unchanged. Five internal call sites, in three files, used uv_line as a PcurveGeometry and now convert it. plane_boundary now builds its conics with uv_conic.

}

#[test]
fn a_box_built_through_the_public_builder_is_a_valid_exportable_solid() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests only cover a planar box.

Suggestion: add a face with a hole, a curved face with a seam edge, and a reversed face taken through finish_solid, plus one test each for the bad-id, rejected-face and orientation points.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 1db7aa6 (tests/public_brep_builder.rs, public API only):

  • a_plate_with_a_reversed_bore_has_holed_faces_and_a_seam_edge: a 4 × 4 × 1 plate with a radius-1 through-hole. The bottom and top faces each have a circular hole (one made with plane_boundary, one with boundary + uv_conic). The bore is a cylinder face with a seam edge, turned to Reverse with set_face_sense and taken through finish_solid. It checks volume 16 − π, the hole centre Outside, and every face probed outward.
  • Bad ids: unknown_ids_are_missing_references (edge in plane_boundary; edge, from and to in face; face_sense and set_face_sense).
  • Rejected face: a_rejected_face_leaves_the_builder_unchanged.
  • Orientation: the_builder_leaves_orientation_to_the_caller, plus the outward probe on every solid the tests build.

Basnederveen and others added 2 commits November 18, 2026 08:21
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- plane_boundary and face/face_with_holes check edge, curve and vertex ids
  (MissingReference) instead of indexing.
- face_with_holes checks every use (ids, edge already used twice, empty
  loops) and the UV bounds before writing anything, so a rejected face
  leaves the builder unchanged and a corrected retry works.
- Builder::face_sense reads a face's sense back. The builder does not check
  orientation; developer.md says the caller owns it and how to check it.
- PcurveGeometry is crate-private again. Callers get an opaque UvCurve from
  uv_line or the new uv_conic, which boundary() takes.
- Tests: a plate with a round through-hole (holed planar faces, a cylinder
  bore with a seam edge, reversed with set_face_sense), every face probed
  outward; an inside-out box accepted as built; unknown ids; a rejected
  face leaving the builder unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Basnederveen

Copy link
Copy Markdown
Contributor Author

Thanks for the review. All five points are in 1db7aa6, and I've replied inline. PcurveGeometry is crate-private again behind an opaque UvCurve, a rejected face leaves the builder unchanged, and developer.md says the caller owns orientation. Inline doc comments are ruled out by the zero-comments check.

Windows x86_64: fmt and clippy (-D warnings) are clean. cargo test --all-targets --no-fail-fast gives 945 passed and 6 failed, the same stored-fixture and digest comparisons as on main on this machine. With CI=true, cases, booleans, golden and step_oracle pass. check:source, check:duplicates and check:cycles are clean.

Basnederveen added a commit to Bas-Automation-Services/OpenGeometry that referenced this pull request Nov 20, 2026
…ration/3.0

Keeps integration's projected_boundary beside the new UvCurve and uv_conic;
projected_boundary reports a missing edge as MissingReference too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants