Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
uses: Swatinem/rust-cache@v2

- name: Run Check
run: cargo check --features "parallel stl-io svg-io dxf-io truetype-text hershey-text image-io"
run: cargo check --features "parallel stl-io svg-io dxf-io truetype-text hershey-text image-io" --all-targets

test:
name: Test Suite
Expand Down
15 changes: 15 additions & 0 deletions .typos.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[files]
extend-exclude = [".git", "stl/**", "hershey-fonts/*"]

[default]
locale = "en"

[default.extend-words]
# allow list / custom corrections
CSG = "CSG"
csg = "csg"
cgs = "CSG"
CGS = "CSG"

iy = "iy"
toi = "toi"
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ rapier3d = { version = "0.24.0", optional = true }
parry3d-f64 = { version = "0.19.0", optional = true }
parry3d = { version = "0.19.0", optional = true }

# bevy mesh convertion
# bevy mesh conversion
bevy_mesh = { version = "0.16", optional = true }
bevy_asset = { version = "0.16", optional = true }
wgpu-types = { version = "*", optional = true, default-features = false } # this will use the version that bevy_mesh does
Expand Down
23 changes: 23 additions & 0 deletions examples/A_right_triangle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//! Scene A: Demonstrate a right_triangle(width=2, height=1)

use csgrs::CSG;
use nalgebra::{Point3, Vector3};
use std::fs;

fn main() {
let tri_2d = CSG::right_triangle(2.0, 1.0, None);
// A tiny arrow pointing from the right-angle corner outward:
let arrow = CSG::arrow(
Point3::new(0.0, 0.0, 0.1), // at corner
Vector3::new(0.5, 0.0, 0.0),
8,
true,
None::<()>,
)
.scale(0.05, 0.05, 0.05);
let scene = tri_2d.extrude(0.1).union(&arrow);
let _ = fs::write(
"stl/scene_right_triangle.stl",
scene.to_stl_ascii("scene_right_triangle"),
);
}
15 changes: 15 additions & 0 deletions examples/B_extrude_vector.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//! Scene B: Demonstrate extrude_vector(direction)

use csgrs::CSG;
use nalgebra::Vector3;
use std::fs;

fn main() {
let circle2d = CSG::<()>::circle(1.0, 32, None);
// extrude along an arbitrary vector
let extruded_along_vec = circle2d.extrude_vector(Vector3::new(0.0, 0.0, 2.0));
let _ = fs::write(
"stl/scene_extrude_vector.stl",
extruded_along_vec.to_stl_ascii("scene_extrude_vector"),
);
}
19 changes: 19 additions & 0 deletions examples/E_center.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//! Scene E: Demonstrate center() (moves shape so bounding box is centered on the origin)

use csgrs::CSG;
use std::fs;

fn main() {
let off_center_circle = CSG::<()>::circle(1.0, 32, None)
.translate(5.0, 2.0, 0.0)
.extrude(0.1);
let centered_circle = off_center_circle.center();
let _ = fs::write(
"stl/scene_circle_off_center.stl",
off_center_circle.to_stl_ascii("scene_circle_off_center"),
);
let _ = fs::write(
"stl/scene_circle_centered.stl",
centered_circle.to_stl_ascii("scene_circle_centered"),
);
}
17 changes: 17 additions & 0 deletions examples/F_float.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//! Scene F: Demonstrate float() (moves shape so bottom is at z=0)

use csgrs::CSG;
use std::fs;

fn main() {
let sphere_for_float = CSG::<()>::sphere(1.0, 16, 8, None).translate(0.0, 0.0, -1.5);
let floated = sphere_for_float.float();
let _ = fs::write(
"stl/scene_sphere_before_float.stl",
sphere_for_float.to_stl_ascii("scene_sphere_before_float"),
);
let _ = fs::write(
"stl/scene_sphere_floated.stl",
floated.to_stl_ascii("scene_sphere_floated"),
);
}
16 changes: 16 additions & 0 deletions examples/G_inverse.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//! Scene G: Demonstrate inverse() (flips inside/outside)

use csgrs::CSG;
use std::fs;

fn main() {
let sphere = CSG::<()>::sphere(1.0, 16, 8, None);

// Hard to visualize in STL, but let's do it anyway
let inv_sphere = sphere.inverse();
#[cfg(feature = "stl-io")]
let _ = fs::write(
"stl/scene_inverse_sphere.stl",
inv_sphere.to_stl_binary("scene_inverse_sphere").unwrap(),
);
}
15 changes: 15 additions & 0 deletions examples/H_tessellate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//! Scene H: Demonstrate tessellate() (forces triangulation)

use csgrs::CSG;
use std::fs;

fn main() {
let sphere = CSG::<()>::sphere(1.0, 16, 8, None);

let tri_sphere = sphere.tessellate();
#[cfg(feature = "stl-io")]
let _ = fs::write(
"stl/scene_tessellate_sphere.stl",
tri_sphere.to_stl_binary("scene_tessellate_sphere").unwrap(),
);
}
18 changes: 18 additions & 0 deletions examples/I_slice.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//! Scene I: Demonstrate slice(plane) – slice a cube at z=0

use csgrs::{CSG, Plane};
use nalgebra::Vector3;
use std::fs;

fn main() {
let cube = CSG::<()>::cube(2.0, 2.0, 2.0, None);

let plane_z = Plane::from_normal(Vector3::z(), 0.5);
let sliced_polygons = cube.slice(plane_z);
let _ = fs::write("stl/scene_sliced_cube.stl", cube.to_stl_ascii("sliced_cube"));
// Save cross-section as well
let _ = fs::write(
"stl/scene_sliced_cube_section.stl",
sliced_polygons.to_stl_ascii("sliced_cube_section"),
);
}
17 changes: 17 additions & 0 deletions examples/J_vertices.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//! Scene J: Demonstrate re-computing vertices() or printing them

use csgrs::CSG;
use std::fs;

fn main() {
let circle_extruded = CSG::<()>::circle(1.0, 32, None).extrude(0.5);
let verts = circle_extruded.vertices();
println!("Scene J circle_extruded has {} vertices", verts.len());
// We'll still save an STL so there's a visual
let _ = fs::write(
"stl/scene_j_circle_extruded.stl",
circle_extruded
.to_stl_binary("scene_j_circle_extruded")
.unwrap(),
);
}
13 changes: 13 additions & 0 deletions examples/K_reuleaux_polygon.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//! Scene K: Demonstrate reuleaux_polygon with a typical triangle shape
//! (already used sides=4 in main examples, so let's do sides=3 here)

use csgrs::CSG;
use std::fs;

fn main() {
let reuleaux_tri = CSG::<()>::reuleaux(3, 2.0, 16, None).extrude(0.1);
let _ = fs::write(
"stl/scene_reuleaux_triangle.stl",
reuleaux_tri.to_stl_ascii("scene_reuleaux_triangle"),
);
}
13 changes: 13 additions & 0 deletions examples/L_rotate_extrude.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//! Scene L: Demonstrate rotate_extrude (360 deg) on a square

use csgrs::CSG;
use std::fs;

fn main() {
let small_square = CSG::<()>::square(1.0, 1.0, None).translate(2.0, 0.0, 0.0);
let revolve = small_square.rotate_extrude(360.0, 24);
let _ = fs::write(
"stl/scene_square_revolve_360.stl",
revolve.to_stl_ascii("scene_square_revolve_360"),
);
}
17 changes: 17 additions & 0 deletions examples/M_mirror.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//! Scene M: Demonstrate “mirror” across a Y=0 plane

use csgrs::{CSG, Plane};
use nalgebra::Vector3;
use std::fs;

fn main() {
let plane_y = Plane::from_normal(Vector3::y(), 0.0);
let shape = CSG::<()>::square(2.0, 1.0, None)
.translate(1.0, 1.0, 0.0)
.extrude(0.1);
let mirrored = shape.mirror(plane_y);
let _ = fs::write(
"stl/scene_square_mirrored_y.stl",
mirrored.to_stl_ascii("scene_square_mirrored_y"),
);
}
15 changes: 15 additions & 0 deletions examples/N_scale.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//! Scene N: Demonstrate scale()

use csgrs::csg::CSG;
use std::fs;

fn main() {
let sphere = CSG::<()>::sphere(1.0, 16, 8, None);

let scaled = sphere.scale(1.0, 2.0, 0.5);
#[cfg(feature = "stl-io")]
let _ = fs::write(
"stl/scene_scaled_sphere.stl",
scaled.to_stl_binary("scene_scaled_sphere").unwrap(),
);
}
18 changes: 18 additions & 0 deletions examples/O_transform.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//! Scene O: Demonstrate transform() with an arbitrary affine matrix

use csgrs::csg::CSG;
use std::fs;

fn main() {
use nalgebra::{Matrix4, Translation3};
let xlate = Translation3::new(2.0, 0.0, 1.0).to_homogeneous();
// Scale matrix
let scale_mat = Matrix4::new_scaling(0.5);
// Combine
let transform_mat = xlate * scale_mat;
let shape = CSG::<()>::cube(1.0, 1.0, 1.0, None).transform(&transform_mat);
let _ = fs::write(
"stl/scene_transform_cube.stl",
shape.to_stl_ascii("scene_transform_cube"),
);
}
14 changes: 14 additions & 0 deletions examples/P_offset.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//! Scene P: Demonstrate offset(distance)

use csgrs::CSG;
use std::fs;

fn main() {
let poly_2d = CSG::<()>::polygon(&[[0.0, 0.0], [2.0, 0.0], [1.0, 1.5]], None);
let grown = poly_2d.offset(0.2);
let scene = grown.extrude(0.1);
let _ = fs::write(
"stl/scene_offset_grown.stl",
scene.to_stl_ascii("scene_offset_grown"),
);
}
31 changes: 31 additions & 0 deletions examples/airfoil.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//! This example demos creating airfoils with naca2412 and naca0015 profiles

use csgrs::CSG;
use std::{fs, path::Path};

const PATH: &str = "stl/airfoil";

fn main() {
// Ensure the folder exists
let _ = fs::create_dir_all(PATH);

// 2-D profile for NACA 2412, 1 m chord, 100 pts / surface
let naca2412 = CSG::airfoil("2412", 1.0, 100, None);
write_example(&naca2412, "naca2412");

// quick solid wing rib 5 mm thick
let rib = naca2412.extrude(0.005);
write_example(&rib, "naca2412_extruded");

// symmetric foil for a centerboard
let naca0015 = CSG::airfoil("0015", 0.3, 80, None)
.extrude_vector(nalgebra::Vector3::new(0.0, 0.0, 1.2));
write_example(&naca0015, "naca0015");
}

fn write_example(shape: &CSG, name: &str) {
let _ = fs::write(
Path::new(PATH).join(name).with_extension("stl"),
shape.to_stl_binary(name).unwrap(),
);
}
34 changes: 34 additions & 0 deletions examples/arrow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//! This example demos creating an arrow and an inverse of the same arrow

use csgrs::CSG;
use nalgebra::{Point3, Vector3};
use std::{fs, path::Path};

const PATH: &str = "stl/arrow";

fn main() {
// Ensure the folder exists
let _ = fs::create_dir_all(PATH);

// start point
let start = Point3::new(1.0, 1.0, 1.0);
// Arrow direction vector, the arrow’s length is the norm of the direction vector.
let direction = Vector3::new(10.0, 5.0, 20.0);

// number of segments for the cylindrical shaft and head
let segments = 16;

// Create the arrow. We pass `None` for metadata.
let arrow = CSG::arrow(start, direction, segments, true, None::<()>);
write_example(&arrow, "arrow");

let arrow_reversed = CSG::arrow(start, direction, segments, false, None::<()>);
write_example(&arrow_reversed, "arrow_reversed");
}

fn write_example(shape: &CSG, name: &str) {
let _ = fs::write(
Path::new(PATH).join(name).with_extension("stl"),
shape.to_stl_binary(name).unwrap(),
);
}
Loading
Loading