mesh_tools/compat.rs
1//! # Compatibility layer for math types
2//!
3//! This module re-exports types from the `mint` crate and provides constructor functions
4//! to maintain compatibility with the previous nalgebra-based API. Users can directly
5//! use these types without adding mint as a direct dependency.
6//!
7//! ## Usage
8//!
9//! ```rust
10//! use mesh_tools::compat::{Point3, Vector2, Vector3};
11//!
12//! // Creating points and vectors
13//! let position = mesh_tools::compat::point3::new(1.0, 2.0, 3.0);
14//! let uv = mesh_tools::compat::vector2::new(0.5, 0.5);
15//! let normal = mesh_tools::compat::vector3::new(0.0, 1.0, 0.0);
16//!
17//! // Accessing components directly
18//! let x = position.x;
19//! let y = position.y;
20//! let z = position.z;
21//!
22//! // Using the module-level functions
23//! use mesh_tools::compat::{point3_new, vector2_new, vector3_new};
24//! let position = point3_new(1.0, 2.0, 3.0);
25//!
26//! // Vector operations
27//! use mesh_tools::compat::{cross, normalize, dot};
28//! let a = vector3_new(1.0, 0.0, 0.0);
29//! let b = vector3_new(0.0, 1.0, 0.0);
30//! let cross_product = cross(a, b);
31//! let unit_vector = normalize(cross_product);
32//! let dot_product = dot(a, b);
33//! ```
34
35// Re-export mint types directly
36pub use mint::{Point3, Vector2, Vector3};
37
38/// Point3 creation and manipulation functions
39pub mod point3 {
40 use super::Point3;
41
42 /// Creates a new 3D point
43 pub fn new<T>(x: T, y: T, z: T) -> Point3<T> {
44 Point3 { x, y, z }
45 }
46}
47
48/// Vector2 creation and manipulation functions
49pub mod vector2 {
50 use super::Vector2;
51
52 /// Creates a new 2D vector
53 pub fn new<T>(x: T, y: T) -> Vector2<T> {
54 Vector2 { x, y }
55 }
56}
57
58/// Vector3 creation and manipulation functions
59pub mod vector3 {
60 use super::Vector3;
61
62 /// Creates a new 3D vector
63 pub fn new<T>(x: T, y: T, z: T) -> Vector3<T> {
64 Vector3 { x, y, z }
65 }
66
67 /// Computes the cross product of two 3D vectors
68 pub fn cross(a: Vector3<f32>, b: Vector3<f32>) -> Vector3<f32> {
69 Vector3 {
70 x: a.y * b.z - a.z * b.y,
71 y: a.z * b.x - a.x * b.z,
72 z: a.x * b.y - a.y * b.x,
73 }
74 }
75
76 /// Normalizes a 3D vector to unit length
77 pub fn normalize(v: Vector3<f32>) -> Vector3<f32> {
78 let length_squared = v.x * v.x + v.y * v.y + v.z * v.z;
79 if length_squared > 0.0 {
80 let length = length_squared.sqrt();
81 Vector3 {
82 x: v.x / length,
83 y: v.y / length,
84 z: v.z / length,
85 }
86 } else {
87 v
88 }
89 }
90
91 /// Computes the dot product of two 3D vectors
92 pub fn dot(a: Vector3<f32>, b: Vector3<f32>) -> f32 {
93 a.x * b.x + a.y * b.y + a.z * b.z
94 }
95}
96
97// Re-export common functions at the module level for easier access
98pub use point3::new as point3_new;
99pub use vector2::new as vector2_new;
100pub use vector3::new as vector3_new;
101pub use vector3::cross;
102pub use vector3::normalize;
103pub use vector3::dot;