### Installing Cargo Xtask for Build Tests in Rust Source: https://github.com/timschmidt/csgrs/blob/main/readme.md This command demonstrates how to install `cargo xtask`, a utility used for managing project-specific tasks. It is a prerequisite for running the custom build tests included in the repository, streamlining the development workflow by providing a dedicated command-line interface for project operations. ```rust cargo install xtask ``` -------------------------------- ### Creating a Rapier Rigid Body and Collider from CSG in Rust Source: https://github.com/timschmidt/csgrs/blob/main/readme.md This example demonstrates how to construct and insert a Rapier rigid body and its corresponding collider from a `csgrs::csg::CSG` object. The `to_rigid_body()` method simplifies the process, requiring `RigidBodySet`, `ColliderSet`, translation, rotation (axis-angle), and density to define the physical properties of the object within a Rapier physics simulation. ```rust use nalgebra::Vector3; use csgrs::float_types::rapier3d::prelude::*; // re-exported for f32/f64 support; use csgrs::float_types::FRAC_PI_2; use csgrs::csg::CSG; let mut rb_set = RigidBodySet::new(); let mut co_set = ColliderSet::new(); let axis_angle = Vector3::z() * FRAC_PI_2; // 90° around Z let rb_handle = csg_obj.to_rigid_body( &mut rb_set, &mut co_set, Vector3::new(0.0, 0.0, 0.0), // translation axis_angle, // axis-angle 1.0, // density ); ``` -------------------------------- ### Creating a Custom Polyhedron (Pyramid) in Rust Source: https://github.com/timschmidt/csgrs/blob/main/readme.md This example demonstrates how to construct a custom polyhedron, specifically a pyramid, using `CSG::polyhedron`. It defines the vertices as `points` and the faces as `faces` (indices into the `points` array), then passes them to the function to generate the 3D shape. ```Rust let points = &[ [0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [1.0, 1.0, 0.0], [0.0, 1.0, 0.0], [0.5, 0.5, 1.0] ]; let faces = vec![ vec![0, 1, 2, 3], // base rectangle vec![0, 1, 4], // triangular side vec![1, 2, 4], vec![2, 3, 4], vec![3, 0, 4] ]; let pyramid = CSG::polyhedron(points, &faces, None); ``` -------------------------------- ### Checking CSG Object Manifold Property in Rust Source: https://github.com/timschmidt/csgrs/blob/main/readme.md This example shows how to determine if a `csgrs::csg::CSG` object is manifold using the `is_manifold()` method. This function triangulates the CSG and verifies that each edge is used exactly twice, which is a critical property for valid 3D models in many applications, ensuring the geometry is 'watertight' and well-formed. ```rust if (csg_obj.is_manifold()){ println!("CSG is manifold!"); } else { println!("Not manifold."); } ``` -------------------------------- ### Initializing a New Rust Project with csgrs Source: https://github.com/timschmidt/csgrs/blob/main/readme.md This command sequence initializes a new Rust project named 'my_cad_project', navigates into its directory, and adds the 'csgrs' crate as a dependency. This is the standard way to set up a new project using Cargo, Rust's package manager, before writing any application code. ```Shell cargo new my_cad_project cd my_cad_project cargo add csgrs ``` -------------------------------- ### Building csgrs Project for WebAssembly (WASM) Source: https://github.com/timschmidt/csgrs/blob/main/readme.md This shell command compiles the `csgrs` project for the WebAssembly (WASM) target. It enables the 'wasm' feature, specifies the `wasm32-unknown-unknown` target for a generic WASM output, and builds in release mode for optimized performance. This is essential for deploying `csgrs` functionality in web environments. ```Shell cargo build --features="wasm" --target=wasm32-unknown-unknown --release ``` -------------------------------- ### Creating a Unit Cube in Rust Source: https://github.com/timschmidt/csgrs/blob/main/readme.md This snippet demonstrates how to create a unit cube (1.0x1.0x1.0) at the origin using the `CSG::cube` function. It takes width, length, height, and an optional metadata parameter, setting all dimensions to 1.0 and metadata to None. ```Rust let cube = CSG::cube(1.0, 1.0, 1.0, None); ``` -------------------------------- ### Creating Basic 2D Shapes and Extruding Text (Rust) Source: https://github.com/timschmidt/csgrs/blob/main/readme.md This snippet demonstrates the creation of fundamental 2D geometric primitives such as squares and circles using the `CSG` library. It also illustrates how to load font data, generate 2D text, and subsequently extrude the 2D text into a 3D object by specifying a height. ```Rust let square = CSG::square(1.0, 1.0, None); // 1×1 at origin let rect = CSG::square(2.0, 4.0, None); let circle = CSG::circle(1.0, 32, None); // radius=1, 32 segments let circle2 = CSG::circle(2.0, 64, None); let font_data = include_bytes!("../fonts/MyFont.ttf"); let csg_text = CSG::text("Hello!", font_data, 20.0, None); // Then extrude the text to make it 3D: let text_3d = csg_text.extrude(1.0); ``` -------------------------------- ### Creating a Sphere in Rust Source: https://github.com/timschmidt/csgrs/blob/main/readme.md This snippet shows how to construct a sphere with a radius of 2.0 using `CSG::sphere`. It specifies 32 segments and 16 stacks for its tessellation, and no metadata, resulting in a detailed spherical mesh. ```Rust let sphere = CSG::sphere(2.0, 32, 16, None); ``` -------------------------------- ### Exporting and Importing DXF Files with CSG in Rust Source: https://github.com/timschmidt/csgrs/blob/main/readme.md This snippet illustrates the process of exporting a CSG object to a DXF file and importing a DXF file to create a CSG object. It uses `to_dxf` for export and `CSG::from_dxf` for import, providing functionality for interoperability with CAD software that supports the DXF format. ```Rust // Export DXF let dxf_bytes = csg_obj.to_dxf()?; std::fs::write("output.dxf", dxf_bytes)?; // Import DXF let dxf_data = std::fs::read("some_file.dxf")?; let csg_dxf = CSG::from_dxf(&dxf_data)?; ``` -------------------------------- ### Exporting and Importing STL Files with CSG in Rust Source: https://github.com/timschmidt/csgrs/blob/main/readme.md This code demonstrates how to export CSG shapes to both ASCII and binary STL formats, and how to import an existing STL file into a CSG object. It utilizes `to_stl_ascii` and `to_stl_binary` methods for export, and `CSG::from_stl` for import, showcasing common file I/O operations for 3D models. ```Rust // Save to ASCII STL let stl_text = csg_union.to_stl_ascii("union_solid"); std::fs::write("union_ascii.stl", stl_text).unwrap(); // Save to binary STL let stl_bytes = csg_union.to_stl_binary("union_solid").unwrap(); std::fs::write("union_bin.stl", stl_bytes).unwrap(); // Load from an STL file on disk let file_data = std::fs::read("some_file.stl")?; let imported_csg = CSG::from_stl(&file_data)?; ``` -------------------------------- ### Creating a Cylinder in Rust Source: https://github.com/timschmidt/csgrs/blob/main/readme.md This snippet illustrates the creation of a cylinder using `CSG::cylinder`. The cylinder has a radius of 1.0, a height of 2.0, and is tessellated with 16 segments, with no additional metadata. ```Rust let cyl = CSG::cylinder(1.0, 2.0, 16, None); ``` -------------------------------- ### Converting CSG to Parry TriMesh in Rust Source: https://github.com/timschmidt/csgrs/blob/main/readme.md This code illustrates how to transform a `csgrs::csg::CSG` object into a Parry `TriMesh` wrapped in a `SharedShape`. The `to_trimesh()` method facilitates integration with physics engines or other libraries that utilize Parry's geometry types, providing a collision-ready representation of the CSG object. ```rust use csgrs::csg::CSG; use csgrs::float_types::rapier3d::prelude::*; // re-exported for f32/f64 support let trimesh_shape = csg_obj.to_trimesh(); // SharedShape with a TriMesh ``` -------------------------------- ### Running All Build Tests with Cargo Xtask in Rust Source: https://github.com/timschmidt/csgrs/blob/main/readme.md This command executes all defined build tests within the project using `cargo xtask`. It's part of the repository's testing suite, allowing developers to verify various feature flag combinations and ensure the library builds correctly under different configurations, which is crucial for maintaining code quality and compatibility. ```rust cargo xtask test-all ``` -------------------------------- ### Calculating Mass Properties of CSG Object in Rust Source: https://github.com/timschmidt/csgrs/blob/main/readme.md This snippet demonstrates how to calculate the mass, center of mass (COM), and inertia local frame for a `csgrs::csg::CSG` object. The `mass_properties()` method takes a density value and returns these physical characteristics, which are crucial for accurate physics simulations or structural analysis. ```rust let density = 1.0; let (mass, com, inertia_frame) = csg_obj.mass_properties(density); println!("Mass: {}", mass); println!("Center of Mass: {:?}", com); println!("Inertia local frame: {:?}", inertia_frame); ``` -------------------------------- ### Creating CSG from SDF in Rust Source: https://github.com/timschmidt/csgrs/blob/main/readme.md This snippet demonstrates how to create a CSG (Constructive Solid Geometry) shape from a Signed Distance Function (SDF) in Rust. It defines an SDF for a sphere, then uses `CSG::from_sdf` with specified resolution, bounding box, and iso-value to generate the CSG representation. The `iso_value` typically represents the surface where the SDF is zero. ```Rust let my_sdf = |p: &Point3| p.coords.norm() - 1.5; let resolution = (60, 60, 60); let min_pt = Point3::new(-2.0, -2.0, -2.0); let max_pt = Point3::new( 2.0, 2.0, 2.0); let iso_value = 0.0; // Typically zero for SDF-based surfaces let csg_shape = CSG::from_sdf(my_sdf, resolution, min_pt, max_pt, iso_value, None); ``` -------------------------------- ### Generating a Trapezoid with CSG (Rust) Source: https://github.com/timschmidt/csgrs/blob/main/readme.md Constructs a 2D trapezoid. Parameters include `top_width`, `bottom_width`, `height`, and `top_offset` to precisely define the shape's dimensions and alignment. An optional `metadata` parameter allows for attaching custom data. ```Rust CSG::trapezoid(top_width: Real, bottom_width: Real, height: Real, top_offset: Real, metadata: Option) ``` -------------------------------- ### Generating a Circle with CSG (Rust) Source: https://github.com/timschmidt/csgrs/blob/main/readme.md Constructs a 2D circle. The `radius` parameter specifies the size of the circle, and `segments` determines the number of facets used to approximate the curve, affecting its smoothness. An optional `metadata` parameter can be included. ```Rust CSG::circle(radius: Real, segments: usize, metadata: Option) ``` -------------------------------- ### Performing CSG Difference Operation and Exporting STL in Rust Source: https://github.com/timschmidt/csgrs/blob/main/readme.md This Rust code demonstrates how to perform a Constructive Solid Geometry (CSG) difference operation between a cube and a sphere using the `csgrs` library. It aliases the generic CSG type, creates two basic 3D shapes, calculates their difference, and then exports the resulting geometry to an ASCII STL file. The `std::fs::write` function is used to save the generated STL data to a file. ```Rust // Alias the library’s generic CSG type with empty metadata: type CSG = csgrs::csg::CSG<()>; // Create two shapes: let cube = CSG::cube(2.0, 2.0, 2.0, None); // 2×2×2 cube at origin, no metadata let sphere = CSG::sphere(1.0, 16, 8, None); // sphere of radius=1 at origin, no metadata // Difference one from the other: let difference_result = cube.difference(&sphere); // Write the result as an ASCII STL: let stl = difference_result.to_stl_ascii("cube_minus_sphere"); std::fs::write("cube_sphere_difference.stl", stl).unwrap(); ``` -------------------------------- ### Generating a Star with CSG (Rust) Source: https://github.com/timschmidt/csgrs/blob/main/readme.md Generates a 2D star shape. `num_points` specifies the number of points, `outer_radius` defines the radius to the outer tips, and `inner_radius` defines the radius to the inner vertices. An optional `metadata` parameter can be included. ```Rust CSG::star(num ``` -------------------------------- ### Managing Metadata with CSG Polygons in Rust Source: https://github.com/timschmidt/csgrs/blob/main/readme.md This snippet illustrates how to define and associate custom metadata with individual polygons within a `csgrs::CSG` object. It demonstrates creating a custom `MyMetadata` struct, attaching it to a `Polygon`, and then retrieving and mutating this metadata, enabling flexible storage of additional per-polygon information like color or labels. ```rust use csgrs::polygon::Polygon; use csgrs::vertex::Vertex; use nalgebra::{Point3, Vector3}; #[derive(Clone)] struct MyMetadata { color: (u8, u8, u8), label: String, } type CSG = csgrs::CSG; // For a single polygon: let mut poly = Polygon::new( vec![ Vertex::new(Point3::origin(), Vector3::z()), Vertex::new(Point3::new(1.0, 0.0, 0.0), Vector3::z()), Vertex::new(Point3::new(0.0, 1.0, 0.0), Vector3::z()), ], Some(MyMetadata { color: (255, 0, 0), label: "Triangle".into(), }), ); // Retrieve metadata if let Some(data) = poly.metadata() { println!("This polygon is labeled {}", data.label); } // Mutate metadata if let Some(data_mut) = poly.metadata_mut() { data_mut.label.push_str("_extended"); } ``` -------------------------------- ### Generating a Polygon with CSG (Rust) Source: https://github.com/timschmidt/csgrs/blob/main/readme.md Generates a 2D polygon from a sequence of vertices. The `&[[x1,y1],[x2,y2],...]` parameter is an array of 2D points defining the polygon's corners in order. An optional `metadata` parameter allows for attaching custom data. ```Rust CSG::polygon(&[[x1,y1],[x2,y2],...], metadata: Option) ``` -------------------------------- ### Generating Metaballs CSG in Rust Source: https://github.com/timschmidt/csgrs/blob/main/readme.md This snippet illustrates the creation of a 3D shape from metaballs using `CSG::from_metaballs`. It defines two `MetaBall` instances, specifies the resolution of the generated mesh, an iso-value for the surface, and padding around the balls to control the meshing process. ```Rust use csgrs::csg::MetaBall; let balls = vec![ MetaBall::new(Point3::origin(), 1.0), MetaBall::new(Point3::new(1.5, 0.0, 0.0), 1.0) ]; let resolution = (60, 60, 60); let iso_value = 1.0; let padding = 1.0; let metaball_csg = CSG::from_metaballs( &balls, resolution, iso_value, padding, None ); ``` -------------------------------- ### Creating a Bevy Mesh from CSG Object in Rust Source: https://github.com/timschmidt/csgrs/blob/main/readme.md This snippet demonstrates how to convert a `csgrs::csg::CSG` object into a Bevy `Mesh`. It utilizes the `to_bevy_mesh()` method, which is essential for rendering CSG-generated geometry within the Bevy game engine. The resulting `bevy_mesh` can then be used in Bevy scenes. ```rust use csgrs::csg::CSG; use bevy::{prelude::*, render::render_asset::RenderAssetUsages, render::mesh::{Indices, PrimitiveTopology}}; let bevy_mesh = csg_obj.to_bevy_mesh(); ``` -------------------------------- ### Generating a Regular N-gon with CSG (Rust) Source: https://github.com/timschmidt/csgrs/blob/main/readme.md Generates a regular N-sided polygon. The `sides` parameter specifies the number of equal sides, and `radius` defines the distance from the center to each vertex. An optional `metadata` parameter can be included for custom data. ```Rust CSG::regular_ngon(sides: usize, radius: Real, metadata: Option) ``` -------------------------------- ### Applying Transformations to CSG Shapes in Rust Source: https://github.com/timschmidt/csgrs/blob/main/readme.md This snippet demonstrates various affine transformations that can be applied to CSG objects, including translation, rotation, scaling, and mirroring. It shows both direct coordinate translation and vector-based translation, along with defining planes for mirroring operations. These methods return new transformed `CSG` instances without modifying the original. ```Rust use nalgebra::Vector3; use csgrs::plane::Plane; let moved = cube.translate(3.0, 0.0, 0.0); let moved2 = cube.translate_vector(Vector3::new(3.0, 0.0, 0.0)); let rotated = sphere.rotate(0.0, 45.0, 90.0); let scaled = cylinder.scale(2.0, 1.0, 1.0); let plane_x = Plane { normal: Vector3::x(), w: 0.0 }; // x=0 plane let plane_y = Plane { normal: Vector3::y(), w: 0.0 }; // y=0 plane let plane_z = Plane { normal: Vector3::z(), w: 0.0 }; // z=0 plane let mirrored = cube.mirror(plane_x); ``` -------------------------------- ### Performing 3D Extrusions and Revolves (Rust) Source: https://github.com/timschmidt/csgrs/blob/main/readme.md This snippet showcases advanced 3D modeling operations within the `CSG` library. It includes simple extrusion of a 2D shape along the Z-axis, rotation-based extrusion to create revolved solids, and the more complex operation of extruding between two distinct 2D polygons to form a lofted 3D object. ```Rust let square = CSG::square(2.0, 2.0, None); let prism = square.extrude(5.0); let revolve_shape = square.rotate_extrude(360.0, 16); let polygon_bottom = CSG::circle(2.0, 64, None); let polygon_top = polygon_bottom.translate(0.0, 0.0, 5.0); let lofted = CSG::extrude_between(&polygon_bottom.polygons[0], &polygon_top.polygons[0], false); ``` -------------------------------- ### Generating an Ellipse with CSG (Rust) Source: https://github.com/timschmidt/csgrs/blob/main/readme.md Constructs a 2D ellipse. The `width` and `height` parameters define the major and minor axes of the ellipse, while `segments` controls the number of linear segments used for approximation. An optional `metadata` parameter can be provided. ```Rust CSG::ellipse(width: Real, height: Real, segments: usize, metadata: Option) ``` -------------------------------- ### Generating a Rounded Rectangle with CSG (Rust) Source: https://github.com/timschmidt/csgrs/blob/main/readme.md Creates a 2D rectangle with rounded corners. It takes `width` and `height` for the overall dimensions, `corner_radius` to specify the curvature, and `corner_segments` for the smoothness of the rounded sections. An optional `metadata` parameter can be used. ```Rust CSG::rounded_rectangle(width: Real, height: Real, corner_radius: Real, corner_segments: usize, metadata: Option) ``` -------------------------------- ### Generating a Square with CSG (Rust) Source: https://github.com/timschmidt/csgrs/blob/main/readme.md Creates a 2D square shape. This function requires `width` and `length` parameters to define the dimensions of the square. An optional `metadata` parameter can be provided for associating additional data with the generated shape. ```Rust CSG::square(width: Real, length: Real, metadata: Option) ``` -------------------------------- ### Generating a Right Triangle with CSG (Rust) Source: https://github.com/timschmidt/csgrs/blob/main/readme.md Creates a 2D right triangle. This function requires `width` and `height` parameters to define the lengths of the two perpendicular sides. An optional `metadata` parameter can be provided for additional data. ```Rust CSG::right_triangle(width: Real, height: Real, metadata: Option) ``` -------------------------------- ### Generating Hershey Text as CSG in Rust Source: https://github.com/timschmidt/csgrs/blob/main/readme.md This code demonstrates how to generate 2D text as a CSG shape using Hershey fonts. It loads font data using `include_bytes!` and then creates a `CSG` object from the specified text, font, and height. This is useful for engraving or creating 3D text from single-stroke fonts. ```Rust let font_data = include_bytes!("../fonts/myfont.jhf"); let csg_text = CSG::from_hershey("Hello!", font_data, 20.0, None); ``` -------------------------------- ### Performing Boolean Operations on CSG Shapes in Rust Source: https://github.com/timschmidt/csgrs/blob/main/readme.md This code illustrates the fundamental boolean operations (union, difference, intersection) available for CSG shapes in Rust. Each operation takes two existing `CSG` objects and returns a new `CSG` object representing the combined or subtracted geometry. These operations are crucial for constructing complex shapes from simpler primitives. ```Rust let union_result = cube.union(&sphere); let difference_result = cube.difference(&sphere); let intersection_result = cylinder.intersection(&sphere); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.