| Crates.io | convexhull3d |
| lib.rs | convexhull3d |
| version | 0.5.0 |
| created_at | 2025-11-20 09:00:48.599312+00 |
| updated_at | 2025-11-20 09:00:48.599312+00 |
| description | 3D Convex Hull and Computational Geometry library |
| homepage | |
| repository | https://github.com/pierreaubert/sotf |
| max_upload_size | |
| id | 1941695 |
| size | 79,135 |
A Rust implementation of the Quickhull algorithm for computing convex hulls in 3D space. Based on the convhull_3d C library and inspired by the MATLAB Computational Geometry Toolbox.
use convexhull3d::{ConvexHull3D, Vertex};
// Define vertices
let vertices = vec![
Vertex::new(0.0, 0.0, 0.0),
Vertex::new(1.0, 0.0, 0.0),
Vertex::new(0.0, 1.0, 0.0),
Vertex::new(0.0, 0.0, 1.0),
];
// Build convex hull
let hull = ConvexHull3D::build(&vertices).unwrap();
// Get properties
println!("Faces: {}", hull.num_faces());
println!("Volume: {}", hull.volume());
println!("Surface Area: {}", hull.surface_area());
use convexhull3d::{ConvexHull3D, export_obj, export_html, testdata};
// Generate test data
let vertices = testdata::icosahedron_vertices();
// Compute hull
let hull = ConvexHull3D::build(&vertices).unwrap();
// Export to OBJ
export_obj(&hull, "icosahedron.obj").unwrap();
// Export to interactive HTML
export_html(&hull, "icosahedron.html", "Icosahedron Hull").unwrap();
use convexhull3d::testdata;
// Random points on a sphere
let sphere_points = testdata::random_sphere_points(1000, 1.0);
// Fibonacci sphere (uniform distribution)
let uniform_sphere = testdata::fibonacci_sphere_points(500, 1.0);
// T-Design sphere approximations
let tdesign_180 = testdata::tdesign_180_sphere();
let tdesign_840 = testdata::tdesign_840_sphere();
let tdesign_5100 = testdata::tdesign_5100_sphere();
// Platonic solids
let cube = testdata::cube_vertices(2.0);
let octahedron = testdata::octahedron_vertices();
let icosahedron = testdata::icosahedron_vertices();
The implementation uses the Quickhull algorithm for 3D convex hull computation:
The library has been tested against the same test cases as the C++ convhull_3d library:
| Test Case | Input Vertices | Output Faces | Time |
|---|---|---|---|
| Tetrahedron | 4 | 4 | < 1ms |
| Cube | 8 | 12-14 | < 1ms |
| Octahedron | 6 | 8-12 | < 1ms |
| Icosahedron | 12 | 20-32 | < 1ms |
| Random Sphere (936 pts) | 936 | ~14,678 | ~170ms |
| T-Design 180 | 180 | ~1,092 | ~10ms |
| T-Design 840 | 840 | ~24,898 | ~2,500ms |
Note: Face counts may vary slightly due to numerical precision and triangulation choices.
# Run all tests
cargo test --package convexhull3d
# Run specific test
cargo test --package convexhull3d test_tetrahedron
# Run with output
cargo test --package convexhull3d -- --nocapture
Tests automatically generate:
target/convexhull_test_output/*.objtarget/convexhull_test_output/*.htmlOpen the HTML files in a browser to see interactive 3D visualizations using Three.js.
This Rust implementation provides similar functionality to the convhull_3d C library:
| Feature | C Implementation | Rust Implementation |
|---|---|---|
| Algorithm | Quickhull | Quickhull |
| 3D Convex Hull | ✓ | ✓ |
| OBJ Export | ✓ | ✓ |
| MATLAB Export | ✓ | - |
| HTML Visualization | - | ✓ |
| Memory Safety | Manual | Automatic |
Vertex: 3D point with x, y, z coordinatesFace: Triangle defined by 3 vertex indicesConvexHull3D: Result of 3D convex hull computationConvexHull3D::build(&[Vertex]): Build 3D convex hull from verticesexport_obj(&ConvexHull3D, path): Export to OBJ formatexport_html(&ConvexHull3D, path, title): Export to interactive HTMLtestdata::random_sphere_points(n, radius): Random sphere distributiontestdata::fibonacci_sphere_points(n, radius): Uniform sphere distributiontestdata::tdesign_*_sphere(): T-Design approximationstestdata::*_vertices(): Platonic solids and other shapesGPL-3.0-or-later (matching the parent project)
This crate is part of the SotF project.