csgrs offers an intuitive approach to Constructive Solid Geometry on 3D meshes using BSP trees in Rust. Handle complex Boolean operations like union, subtraction, and intersection efficiently on 3D models. Ideal for developers needing precise control over 3D model transformations.
csgrs is a Rust library designed for performing Constructive Solid Geometry (CSG) operations on 3D meshes using Binary Space Partitioning (BSP) trees. CSG is a powerful modeling technique that applies Boolean operations such as union, intersection, and difference to combine 3D shapes, allowing for the creation of complex models from simpler components. This library provides an an implementation that effectively handles edge cases, particularly those involving overlapping coplanar polygons.
Key Features
- Boolean Operations on Meshes: Perform efficient operations such as unions, intersections, and subtractions on 3D geometric shapes.
- BSP Tree Utilization: Leverages BSP trees to manage and compute geometry, enhancing performance and accuracy.
- Comprehensive Edge Case Handling: Correctly manages cases involving overlapping polygons to ensure robust geometry processing.
Basic Usage Example
The library offers simple functions to create common geometric shapes and combine them using Boolean operations:
Construct a 2D shape:
let square = csgrs::square(None); let square2 = csgrs::square(Some(([2.0, 3.0], true))); let circle = csgrs::circle(None); let circle2 = csgrs::circle(Some((2.0, 64))); let points = vec![[0.0, 0.0], [2.0, 0.0], [1.0, 1.5]]; let polygon2d = csgrs::polygon_2d(&points);
Construct a 3D shape:
let cube = csgrs::cube(None); let cube2 = csgrs::cube(Some([0.0, 0.0, 0.0], [1.0, 1.0, 1.0])); // center, radius let sphere = csgrs::sphere(None); let sphere2 = csgrs::sphere(Some([0.0, 0.0, 0.0], 1.0, 16, 8)); // center, radius, slices, stacks let cylinder = csgrs::cylinder(None); let cylinder2 = csgrs::cylinder(Some([0.0, -1.0, 0.0], [0.0, 1.0, 0.0], 1.0, 16)); // start, end, radius, slices
Combine shapes:
let union_result = cube.union(&sphere); let subtraction_result = cube.subtract(&sphere); let intersection_result = cylinder.intersect(&sphere);
Extract polygons:
let polygons = union_result.to_polygons(); println!("Polygon count = {}", polygons.len());
Translate:
let translation_result = cube.translate(Vector3::new(3.0, 2.0, 1.0));
Rotate:
let rotation_result = cube.rotate(15.0, 45.0, 0.0);
Scale:
let scale_result = cube.scale(2.0, 1.0, 3.0);
Mirror:
let mirror_result = cube.mirror(Axis::Y);
Convex hull:
let hull = cube.convex_hull();
Minkowski sum:
let rounded_cube = cube.minkowski_sum(&sphere);
Extrude a 2D shape:
let square = csgrs::square(Some(([2.0, 2.0], true))); let prism = square.extrude(5.0);
Export an STL:
let stl_data = union_result.to_stl("cube_minus_sphere"); let filename = "output.stl"; let mut file = File::create(filename).expect("Failed to create file"); file.write_all(stl_data.as_bytes()).expect("Failed to write STL");
No comments yet.
Sign in to be the first to comment.