microcad_core/
lib.rs

1// Copyright © 2024-2025 The µcad authors <info@ucad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4//! µcad core
5
6mod boolean_op;
7
8pub mod bounds;
9pub mod color;
10pub mod core_error;
11pub mod geo2d;
12#[cfg(feature = "geo3d")]
13pub mod geo3d;
14pub mod length;
15pub mod render;
16pub mod traits;
17pub mod triangle;
18
19/// Primitive integer type.
20pub type Integer = i64;
21/// Primitive floating point type.
22pub type Scalar = f64;
23/// 2D vector type.
24pub type Vec2 = cgmath::Vector2<Scalar>;
25/// 3D vector type.
26pub type Vec3 = cgmath::Vector3<Scalar>;
27/// 4D vector type.
28pub type Vec4 = cgmath::Vector4<Scalar>;
29/// 2D matrix type.
30pub type Mat2 = cgmath::Matrix2<Scalar>;
31/// 3D matrix type.
32pub type Mat3 = cgmath::Matrix3<Scalar>;
33/// 4D matrix type.
34pub type Mat4 = cgmath::Matrix4<Scalar>;
35/// Primitive angle type in radians.
36pub type Angle = cgmath::Rad<Scalar>;
37/// Length type.
38pub use length::Length;
39
40/// Constants.
41pub mod consts {
42    pub use std::f64::consts::PI;
43    pub use std::f64::consts::TAU;
44}
45
46pub use boolean_op::BooleanOp;
47pub use bounds::*;
48pub use color::*;
49pub use core_error::*;
50pub use geo2d::*;
51pub use geo3d::*;
52pub use render::*;
53pub use triangle::*;
54
55/// Convert a Matrix4 to Matrix3.
56pub fn mat4_to_mat3(m: &Mat4) -> Mat3 {
57    Mat3::from_cols(m.x.truncate_n(2), m.y.truncate_n(2), m.w.truncate_n(2))
58}
59
60/// Convert a Matrix3 to Matrix4.
61pub fn mat3_to_mat4(m: &Mat3) -> Mat4 {
62    Mat4::new(
63        m.x.x, m.x.y, 0.0, m.x.z, // First column: X basis + X translation
64        m.y.x, m.y.y, 0.0, m.y.z, // Second column: Y basis + Y translation
65        0.0, 0.0, 1.0, 0.0, // Z axis: identity (no change)
66        0.0, 0.0, 0.0, 1.0, // Homogeneous row
67    )
68}