pub struct CSG<S: Clone> {
pub polygons: Vec<Polygon<S>>,
}Expand description
The main CSG solid structure. Contains a list of polygons.
Fields§
§polygons: Vec<Polygon<S>>Implementations§
Source§impl<S: Clone> CSG<S>
impl<S: Clone> CSG<S>
Sourcepub fn from_polygons(polygons: Vec<Polygon<S>>) -> Self
pub fn from_polygons(polygons: Vec<Polygon<S>>) -> Self
Build a CSG from an existing polygon list
Sourcepub fn to_polygons(&self) -> &[Polygon<S>]
pub fn to_polygons(&self) -> &[Polygon<S>]
Return the internal polygons
Sourcepub fn cube(options: Option<(&[f64; 3], &[f64; 3])>) -> CSG<S>
pub fn cube(options: Option<(&[f64; 3], &[f64; 3])>) -> CSG<S>
Construct an axis-aligned cube, with optional center and radius
Sourcepub fn sphere(options: Option<(&[f64; 3], f64, usize, usize)>) -> CSG<S>
pub fn sphere(options: Option<(&[f64; 3], f64, usize, usize)>) -> CSG<S>
Construct a sphere with optional center, radius, slices, stacks
Sourcepub fn cylinder(options: Option<(&[f64; 3], &[f64; 3], f64, usize)>) -> CSG<S>
pub fn cylinder(options: Option<(&[f64; 3], &[f64; 3], f64, usize)>) -> CSG<S>
Construct a cylinder with optional start, end, radius, slices
Sourcepub fn polyhedron(points: &[[f64; 3]], faces: &[Vec<usize>]) -> CSG<S>
pub fn polyhedron(points: &[[f64; 3]], faces: &[Vec<usize>]) -> CSG<S>
Creates a CSG polyhedron from raw vertex data (points) and face indices.
§Parameters
points: a slice of[x,y,z]coordinates.faces: each element is a list of indices intopoints, describing one face. Each face must have at least 3 indices.
§Example
let pts = &[
[0.0, 0.0, 0.0], // point0
[1.0, 0.0, 0.0], // point1
[1.0, 1.0, 0.0], // point2
[0.0, 1.0, 0.0], // point3
[0.5, 0.5, 1.0], // point4 - top
];
// Two faces: bottom square [0,1,2,3], and a pyramid side [0,1,4]
let fcs = vec![
vec![0, 1, 2, 3],
vec![0, 1, 4],
vec![1, 2, 4],
vec![2, 3, 4],
vec![3, 0, 4],
];
let csg_poly = CSG::polyhedron(pts, &fcs);Sourcepub fn transform(&self, mat: &Matrix4<f64>) -> CSG<S>
pub fn transform(&self, mat: &Matrix4<f64>) -> CSG<S>
Transform all vertices in this CSG by a given 4×4 matrix.
pub fn translate(&self, v: Vector3<f64>) -> CSG<S>
pub fn rotate(&self, x_deg: f64, y_deg: f64, z_deg: f64) -> CSG<S>
pub fn scale(&self, sx: f64, sy: f64, sz: f64) -> CSG<S>
Sourcepub fn convex_hull(&self) -> CSG<S>
pub fn convex_hull(&self) -> CSG<S>
Compute the convex hull of all vertices in this CSG.
Sourcepub fn minkowski_sum(&self, other: &CSG<S>) -> CSG<S>
pub fn minkowski_sum(&self, other: &CSG<S>) -> CSG<S>
Compute the Minkowski sum: self ⊕ other
Naive approach: Take every vertex in self, add it to every vertex in other,
then compute the convex hull of all resulting points.
Sourcepub fn subdivide_triangles(&self, levels: u32) -> CSG<S>
pub fn subdivide_triangles(&self, levels: u32) -> CSG<S>
Subdivide all polygons in this CSG ‘levels’ times, returning a new CSG. This results in a triangular mesh with more detail.
Sourcepub fn renormalize(&mut self)
pub fn renormalize(&mut self)
Renormalize all polygons in this CSG by re-computing each polygon’s plane and assigning that plane’s normal to all vertices.
Sourcepub fn ray_intersections(
&self,
origin: &Point3<f64>,
direction: &Vector3<f64>,
) -> Vec<(Point3<f64>, f64)>
pub fn ray_intersections( &self, origin: &Point3<f64>, direction: &Vector3<f64>, ) -> Vec<(Point3<f64>, f64)>
Casts a ray defined by origin + t * direction against all triangles
of this CSG and returns a list of (intersection_point, distance),
sorted by ascending distance.
§Parameters
origin: The ray’s start point.direction: The ray’s direction vector.
§Returns
A Vec of (Point3<f64>, f64) where:
Point3<f64>is the intersection coordinate in 3D,f64is the distance (the ray parameter t) fromorigin.
Sourcepub fn square(params: Option<([f64; 2], bool)>) -> CSG<S>
pub fn square(params: Option<([f64; 2], bool)>) -> CSG<S>
Creates a 2D square in the XY plane.
§Parameters
size: the width and height of the square (default [1.0, 1.0])center: iftrue, center the square about (0,0); otherwise bottom-left is at (0,0).
§Example
let sq = CSG::square(None); // or with custom params: let sq2 = CSG::square(Some(([2.0, 3.0], true)));
Sourcepub fn polygon_2d(points: &[[f64; 2]]) -> CSG<S>
pub fn polygon_2d(points: &[[f64; 2]]) -> CSG<S>
Creates a 2D polygon in the XY plane from a list of [x, y] points.
§Parameters
points: a sequence of 2D points (e.g.[[0.0,0.0], [1.0,0.0], [0.5,1.0]]) describing the polygon boundary in order.
Note: This simple version ignores ‘paths’ and holes. For more complex polygons, we’ll have to handle multiple paths, winding order, holes, etc.
§Example
let pts = vec![[0.0, 0.0], [2.0, 0.0], [1.0, 1.5]]; let poly2d = CSG::polygon_2d(&pts);
Sourcepub fn extrude(&self, height: f64) -> CSG<S>
pub fn extrude(&self, height: f64) -> CSG<S>
Linearly extrude this (2D) shape in the +Z direction by height.
This is similar to OpenSCAD’s linear_extrude(height=...) assuming
the base 2D shape is in the XY plane with a +Z normal.
- If your shape is centered around Z=0, the resulting extrusion
will go from Z=0 to Z=
height. - The top polygons will be created at Z=
height. - The bottom polygons remain at Z=0 (the original).
- Side polygons will be formed around the perimeter.
Sourcepub fn rotate_extrude(&self, angle_degs: f64, segments: usize) -> CSG<S>
pub fn rotate_extrude(&self, angle_degs: f64, segments: usize) -> CSG<S>
Rotate-extrude (revolve) this 2D shape around the Z-axis from 0..angle_degs.
segmentsdetermines how many steps to sample around the axis.- For a full revolve, pass
angle_degs = 360.0.
This is similar to OpenSCAD’s rotate_extrude(angle=..., segments=...).
Sourcepub fn bounding_box(&self) -> Aabb
pub fn bounding_box(&self) -> Aabb
Returns a parry3d::bounding_volume::Aabb.
Sourcepub fn grow(&self, distance: f64) -> CSG<S>
pub fn grow(&self, distance: f64) -> CSG<S>
Approximate growing (outward offset) of the shape by a given distance (3D). This method unions translated copies of the shape along a sphere.
Sourcepub fn shrink(&self, distance: f64) -> CSG<S>
pub fn shrink(&self, distance: f64) -> CSG<S>
Approximate shrinking (inward offset) of the shape by a given distance (3D). This method unions translated copies of the complement of the shape along a sphere, then inverts the result.
Sourcepub fn grow_2d(&self, distance: f64) -> CSG<S>
pub fn grow_2d(&self, distance: f64) -> CSG<S>
Approximate 2D growing (outward offset) of the shape by a given distance.
Sourcepub fn shrink_2d(&self, distance: f64) -> CSG<S>
pub fn shrink_2d(&self, distance: f64) -> CSG<S>
Approximate 2D shrinking (inward offset) of the shape by a given distance.
Sourcepub fn text_mesh(text_str: &str, font_data: &[u8], size: Option<f64>) -> CSG<S>
pub fn text_mesh(text_str: &str, font_data: &[u8], size: Option<f64>) -> CSG<S>
Creates 2D text in the XY plane using the meshtext crate to generate glyph meshes.
text_str: the text to renderfont_data: TTF font file bytes (e.g.include_bytes!("../assets/FiraMono-Regular.ttf"))size: optional scaling factor (e.g., a rough “font size”).
Note: This is a basic example that:
- does not handle kerning or multi-line text,
- simply advances the cursor by each glyph’s width,
- places all characters along the X axis.
Sourcepub fn to_trimesh(&self) -> SharedShape
pub fn to_trimesh(&self) -> SharedShape
Convert the polygons in this CSG to a Parry TriMesh. Useful for collision detection or physics simulations.
Sourcepub fn mass_properties(
&self,
density: f64,
) -> (f64, Point3<f64>, Unit<Quaternion<f64>>)
pub fn mass_properties( &self, density: f64, ) -> (f64, Point3<f64>, Unit<Quaternion<f64>>)
Approximate mass properties using Rapier.
Sourcepub fn to_rigid_body(
&self,
rb_set: &mut RigidBodySet,
co_set: &mut ColliderSet,
translation: Vector3<f64>,
rotation: Vector3<f64>,
density: f64,
) -> RigidBodyHandle
pub fn to_rigid_body( &self, rb_set: &mut RigidBodySet, co_set: &mut ColliderSet, translation: Vector3<f64>, rotation: Vector3<f64>, density: f64, ) -> RigidBodyHandle
Create a Rapier rigid body + collider from this CSG, using
an axis-angle rotation in 3D (the vector’s length is the
rotation in radians, and its direction is the axis).
Sourcepub fn to_stl(&self, name: &str) -> String
pub fn to_stl(&self, name: &str) -> String
Convert this CSG to an ASCII STL string with the given name.
let csg = CSG::cube(None);
let stl_text = csg.to_stl("my_solid");
println!("{}", stl_text);Trait Implementations§
Auto Trait Implementations§
impl<S> Freeze for CSG<S>
impl<S> RefUnwindSafe for CSG<S>where
S: RefUnwindSafe,
impl<S> Send for CSG<S>where
S: Send,
impl<S> Sync for CSG<S>where
S: Sync,
impl<S> Unpin for CSG<S>where
S: Unpin,
impl<S> UnwindSafe for CSG<S>where
S: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.