ezcgmath/lib.rs
1//! # ezcgmath
2//!
3//! An extremely simplified linear algebra API, for use with computer graphics.
4//!
5//! Math should be fun, and easy. This crate aims to simplify the usage of math in
6//! computer graphics applications for both the novice, and the expert. This is achieved
7//! by providing a super-straightforward API with good documentation, and zero abstraction on the
8//! main types.
9//!
10//! ## Implementation Details
11//! The `Scalar` type is fixed to f32. This was chosen due to its straightforward compatibility with graphics APIs.
12//! It is up to the user to create their own abstractions if any limits are hit due to this.
13//!
14//! The coordinate system is fixed to being left handed, with Y as up. If you wish to convert to a different
15//! coordinate system you may achieve this by creating a reflection matrix and applying it at the appropriate point.
16//!
17//! Transformations in this API are applied in a row-major manor. As a reminder, this means that
18//! transformations are applied in the order they are specified.
19//! For example, if you wanted to scale, _then_ rotate, and _then_ translate a position vector,
20//! you would write these transformations in "reading order":
21//!
22//! ```
23//! use ezcgmath::prelude::*;
24//! use ezcgmath::approx::*;
25//!
26//! let position_vector = Vector3::new(5.0, 0.0, 0.0);
27//! let scale_matrix = Matrix4x4::from_nonuniform_scale(&Vector3::new(2.0, 1.0, 1.0));
28//! let rotation_matrix = Quaternion::from_axis_angle(&Vector3::new(0.0, 1.0, 0.0), Degrees(90.0));
29//! let translation_matrix = Matrix4x4::from_translation(&Vector3::new(0.0, 0.0, -10.0));
30//! let transformed_vector = position_vector * scale_matrix * rotation_matrix * translation_matrix;
31//! ```
32//!
33//! ## Disclaimer
34//! ezcgmath is still very much a work in progress. If there are holes you'd like filling,
35//! please feel free to open an issue on GitHub so we can start a conversation on it. If you'd like to
36//! contribute, that's great! Please read CONTRIBUTING.md for some guidelines on the process.
37
38#[macro_use]
39mod macros;
40
41/// The primitive type used for all math in this crate.
42pub type Scalar = f32;
43
44/// An angle in Radians. Can be converted from Degrees easily with `Radians::from(degrees)`
45#[derive(Debug, Copy, Clone, PartialEq)]
46pub struct Radians(pub Scalar);
47impl_approx!(Radians, 0);
48
49impl From<Scalar> for Radians {
50 fn from(radians: Scalar) -> Self {
51 Self(radians)
52 }
53}
54
55impl From<Degrees> for Radians {
56 fn from(degrees: Degrees) -> Self {
57 Self(degrees.0 * std::f32::consts::PI / 180.0)
58 }
59}
60
61/// An angle in Degrees. Can be converted from Radians easily with `Degrees::from(radians)`
62#[derive(Debug, Copy, Clone, PartialEq)]
63pub struct Degrees(pub Scalar);
64impl_approx!(Degrees, 0);
65
66impl From<Scalar> for Degrees {
67 fn from(degrees: Scalar) -> Self {
68 Self(degrees)
69 }
70}
71
72impl From<Radians> for Degrees {
73 fn from(radians: Radians) -> Self {
74 Self(radians.0 * 180.0 / std::f32::consts::PI)
75 }
76}
77
78/// approx crate re-export, useful for asserts on vector/matrix types.
79pub mod approx;
80
81/// Contains Matrix types and operations
82pub mod matrix;
83
84/// Contains the Quaternion type
85pub mod quaternion;
86
87/// Contains Vector types and operations
88pub mod vector;
89
90/// The most common types you will use in this library, re-exported under a single module.
91pub mod prelude {
92 pub use crate::{Degrees, Radians};
93 pub use crate::matrix::Matrix4x4;
94 pub use crate::quaternion::Quaternion;
95 pub use crate::vector::Vector3;
96}