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