CSGRS is the constructive-solid-geometry grammar for the Hyper geometry workspace. It provides an OpenSCAD-like vocabulary for primitives, Boolean composition, transforms, extrusion, revolution, sweep, loft, conversion, and CAD interchange without introducing another geometry kernel.
- Hypercurve owns curves, paths, filled regions, and certified planar operations.
- Hypermesh owns indexed triangle geometry, certified solid Booleans, topology facts, and mesh queries.
- Hypertri owns triangulation.
- Hyperphysics owns physical-property analysis.
- CSGRS supplies the modeling language that composes those native types and connects them to file formats and application boundaries.
Core coordinates use hyperreal::Real. Primitive floats occur only at
explicit rendering, serialization, adapter, FFI, and WebAssembly boundaries.
CSGRS operates directly on its dependencies' native geometry:
CurveRegion2 ── extrude / revolve / sweep ──► TriangleMesh
│ │
├─ CurveString2 and CurvePath2 for open work ├─ Hypermesh Booleans
└─ Hypercurve topology and Booleans └─ queries and interchange
hypercurve::CurveRegion2is filled planar material, including holes.hypercurve::CurveString2is an open or closed curve string without implied fill.hypercurve::CurvePath2preserves higher-order path topology.hypermesh::TriangleMesh, re-exported ascsgrs::TriangleMesh, is reusable indexed triangle geometry and the input/output type for solid modeling.csgrs::AttributedMesh<M>is an optional, feature-gated boundary sidecar with one metadata value per triangle and optional authored normals. Modeling operations still consume itsTriangleMeshgeometry.Realis CSGRS's re-export of the exact-aware Hyperreal scalar.
Use Hypercurve or Hypermesh directly for kernel-level work. Use CSGRS when the task is to describe a model, cross between curve and solid representations, or exchange geometry with another format.
Install Rust with rustup, then:
cargo new drilled-cube
cd drilled-cube
cargo add csgrsReplace src/main.rs with:
The equivalent dependency entry is:
[dependencies]
csgrs = "0.23.0"use csgrs::{
GeometryContext, Real, curve,
io::stl::to_stl_binary,
solid::{self, SolidExt},
};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let body = solid::cube(Real::from(12));
let opening = curve::try_extrude(
&curve::square(Real::from(4)),
Real::from(16),
&GeometryContext::STRICT,
)?
.into_value()
.translated(Real::from(4), Real::from(4), Real::from(-2));
let part = body.try_difference(&opening)?;
std::fs::write("drilled_cube.stl", to_stl_binary(&part, "drilled_cube")?)?;
Ok(())
}Run it with cargo run. It writes drilled_cube.stl. The same program is
included as examples/basic.rs and runs with:
cargo run --example basicThe following is the useful public modeling surface. Generated API documentation contains complete signatures, feature conditions, and errors.
Functions in csgrs::curve return native Hypercurve types.
Rendered filled constructors:
curve::empty() is intentionally image-free because it contains no visible
geometry. polygon_points constructs the same polygon family from native
Hypercurve Point2 values.
Open constructors retain their native path/string topology:
The Hershey catalog is compiled into Hypercurve and exposed as
hypercurve::hershey::fonts; no font files or runtime parser are required.
Planar operations:
CurveRegionExt::{try_union, try_difference, try_intersection, try_xor};offsetandoffset_rounded;transformed,translated,rotated, andscaled;contains_xy,bounding_box,finite_profiles, andtriangulate.
finite_profiles is an explicit display/interchange projection. It is not a
replacement for the native curve topology.
These functions consume a CurveRegion2 and return a native TriangleMesh:
Functions in csgrs::solid return TriangleMesh:
solid::empty() is intentionally image-free. Diagnostic variants
metaballs_with_diagnostics and sdf_with_diagnostics produce the same shape
families while returning sampling reports. sdf_expr and
sdf_expr_with_diagnostics accept retained hypersdf::SdfExpr values and
produce the same SDF shape family.
Sampling arguments such as segments, stacks, slices, and resolution are explicit tessellation choices; CSGRS has no global tolerance setting.
solid::SolidExt supplies fluent native operations:
try_union,try_difference,try_intersection, andtry_xor;transformedandtranslated;exact_bounds;visit_native_triangles.
The corresponding free functions and additional operations are:
- Booleans and transforms:
boolean,transform,rotate,scale,mirror,inverse,translation, andtranslation_to; - positioning and repetition:
bounding_box,center,float,merge,distribute_linear,distribute_grid, anddistribute_arc; - topology/refinement:
subdivide,renormalized,materialize_finite,is_closed_manifold,convex_hull, andminkowski_sum; - geometric queries:
contains_point,ray_intersections,polyline_intersections, anddihedral_angle; - projections:
flattenandslice_z; - analysis:
exact_mass_properties; - application conversion:
to_bevy_meshwhenbevymeshis enabled; - surface forms of implicit fields:
gyroid,schwarz_p, andschwarz_d.
Hypermesh retains reusable bounds, topology, transform, convexity, and
Boolean facts on TriangleMesh. Cloning a mesh shares its immutable geometry
and retained facts.
Rendered solid operations:
The inverse dimensional conversions preserve native Hypercurve topology:
| Preview | Function |
|---|---|
![]() |
solid::flatten(mesh: &TriangleMesh) -> CurveRegion2 |
![]() |
solid::slice_z(mesh: &TriangleMesh, z: Real) -> (CurveRegion2, Vec<CurveString2>, Vec<CurvePath2>) |
flatten unions the XY projections of all triangle faces. slice_z returns
filled closed sections, open curve strings, and higher-order paths separately
instead of discarding their topology roles.
Enable attributed when a renderer or interchange boundary needs aligned
face information:
use csgrs::{AttributedMesh, Real, solid};
let geometry = solid::cube(Real::from(2));
let attributed = AttributedMesh::from_uniform(geometry, "housing");
assert_eq!(
attributed.face_metadata().len(),
attributed.geometry().triangles.len()
);AttributedMesh::new validates one metadata row per triangle.
with_authored_normals additionally validates one normal per position.
geometry, face_metadata, authored_normals, into_parts, and
map_metadata expose the sidecar without duplicating modeling behavior.
Feature-gated functions live under csgrs::io:
| Format | Import | Export |
|---|---|---|
| STL | stl::from_stl |
to_stl_ascii, to_stl_binary |
| OBJ | obj::from_obj |
to_obj, write_obj |
| PLY | — | to_ply, write_ply |
| AMF | — | to_amf, to_amf_with_color, write_amf |
| glTF/GLB | gltf::from_gltf |
to_gltf, write_gltf, to_gltf_scene, write_gltf_scene |
| VRML 2.0 | vrml::from_vrml |
— |
| DXF | dxf::from_dxf |
dxf::to_dxf |
| SVG | svg::import_svg |
svg::export_svg |
| Gerber | gerber::import_gerber |
gerber::export_gerber |
Triangle importers return TriangleMesh. Curve importers return distinct
filled, string, and path carriers where the source format can contain more
than one topology kind. Mesh exporters accept TriangleMesh directly, and
planar exporters accept native CurveRegion2 values.
csgrs::adapter provides opt-in primitive-scalar wrappers over native
geometry:
- scalar policies:
RawReal,F32,F64, andI128; - solid boundary:
ScalarMesh<A>; - curve boundary:
ScalarCurve<A>; - result carriers:
GraphicsMesh,IndexedMeshBuffers, andAabb3.
Conversions happen only at ingress and egress. Float output is fallible when coordinates are not finite; integer output requires an exact, in-range integer. The adapters do not implement another geometry kernel.
The C ABI is the sibling
csgrs-ffi crate. C++, Go, Python,
and TypeScript wrappers are documented in bindings.
The wasm feature provides browser-oriented wrappers over the same core.
Default features provide the common modeling and interchange surface. For
smaller builds use default-features = false.
| Feature | Adds |
|---|---|
curve |
native curve grammar and curve-to-solid operations |
attributed |
aligned metadata/authored-normal boundary carrier |
stl-io, obj-io, ply-io, amf-io |
triangle interchange |
gltf-io, vrml-io, dxf-io |
scene and CAD interchange |
svg-io, gerber-io |
curve interchange |
offset |
regularized planar offsets |
truetype-text, hershey-text, image-io |
text and raster curve construction |
metaballs, sdf |
sampled implicit geometry |
bevymesh |
Bevy conversion |
wasm |
WebAssembly/browser wrappers |
dispatch-trace |
computation-path evidence for tests and benchmarks |
A minimal curve-and-solid build is:
[dependencies]
csgrs = { version = "0.23.0", default-features = false, features = ["curve", "stl-io"] }For WebAssembly:
cargo install wasm-pack
wasm-pack build --release --target bundler --out-dir pkg -- --features wasm- Topology-sensitive decisions use
Realand the Hyper predicate stack. - Boolean inputs must meet Hypermesh's documented finite, closed, oriented
solid preconditions. Keep structured
try_*errors. - Sampled circles, text, raster contours, implicit fields, and graphics buffers remain explicit approximations at caller-chosen resolutions.
CurveRegion2,CurveString2, andCurvePath2have different meanings; CSGRS does not hide them behind a combined 2D facade.TriangleMeshis authoritative geometry.AttributedMeshstores only aligned boundary attributes.- Bevy, WebAssembly, scalar adapters, FFI, and serialization are application boundaries; their finite projections never become topology truth.
The reproducible harness, CSV schema, dependency policy, and interpretation rules are in benchmarks/README.md. Retained measurements and optimization notes are in PERFORMANCE.md.
The serialized suite compares CSGRS, CGAL EPECK, and tight-tolerance
OpenCascade across 66 shared construction, transform, Boolean, analysis, and
I/O workloads. That includes deterministic 8,020-triangle concave-labyrinth
and 36,096-triangle level-3 Sierpiński-foam meshes. Rust comparisons add
boolmesh, manifold-rust, and tri-mesh where their representations
overlap.
Set YEAHRIGHT_BENCH=1 for benchmark runs that should download the optional
public-domain YeahRight corpus into target/benchmark-fixtures/yeahright.
The examples directory covers primitives, native curve extrusion, Booleans,
transforms, hulls, Minkowski sums, metadata, adapters, and interchange. The
Nuxt WebAssembly demo is under
examples/wasm-nuxt4.
Community integrations:
These are the authoritative mathematical and interchange references for the crate:
- Nicholas M. Patrikalakis, Takashi Maekawa, and Wonjoon Cho, Shape Interrogation for Computer Aided Design and Manufacturing, MIT Press. The curve/surface, intersection, robustness, and offset chapters motivate the separation of representation, interrogation, and presentation.
- Jonathan Richard Shewchuk, “Adaptive Precision Floating-Point Arithmetic and Fast Robust Geometric Predicates”, Discrete & Computational Geometry 18, 1997, pp. 305–363. This is the primary reference for sign-correct adaptive predicates.
- Chee K. Yap, “Towards Exact Geometric Computation”, Computational Geometry 7, 1997, pp. 3–23. This informs the separation between exact decisions and explicit approximation boundaries.
- Ucamco, Gerber Layer Format specifications, the normative source for the Gerber adapter.
- W3C, Scalable Vector Graphics (SVG) 2, the normative source format adapted through Hypercurve.
- Khronos Group, glTF 2.0 specification, the normative scene and buffer format for the glTF adapter.
CSGRS began as a Rust translation of Evan Wallace's CSG.js, released under the MIT license. That lineage and copyright notice remain in LICENSE.
The crate builds on work by the Hyperreal, Hyperlattice, Hyperlimit,
Hypercurve, Hypertri, Hypermesh, Hyperphysics, and Hypersdf contributors and
the upstream format/rendering crates named in Cargo.toml.
The compiled single-stroke font catalog was created by Dr. A. V. Hershey at the U.S. National Bureau of Standards. Its source distribution format was created by James Hurt of Cognition, Inc.; the integrated Rust representation is not the U.S. NTIS distribution format.
Run the release checks before submitting changes:
cargo fmt --all -- --check
cargo test --all-features
cargo clippy --all-features --all-targets -- -D warnings
RUSTDOCFLAGS="-D warnings" cargo doc --all-features --no-deps
cargo check --all-features --benchesDiscussion is available in the CSGRS Discord community. CSGRS is available under the MIT License.








































































