Debian Codegraph @

debian/missing-sources/glam-0.15.2/src/lib.rs

raw symbols SCIP
1 /*!
2 # glam
3
4 `glam` is a simple and fast linear algebra library for games and graphics.
5
6 ## Features
7
8 * [`f32`](mod@f32) types
9 * vectors: [`Vec2`], [`Vec3`], [`Vec3A`] and [`Vec4`]
10 * square matrices: [`Mat2`], [`Mat3`], [`Mat3A`] and [`Mat4`]
11 * a quaternion type: [`Quat`]
12 * affine transformation types: [`Affine2`] and [`Affine3A`]
13 * [`f64`](mod@f64) types
14 * vectors: [`DVec2`], [`DVec3`] and [`DVec4`]
15 * square matrices: [`DMat2`], [`DMat3`] and [`DMat4`]
16 * a quaternion type: [`DQuat`]
17 * affine transformation types: [`DAffine2`] and [`DAffine3`]
18 * [`i32`](mod@i32) types
19 * vectors: [`IVec2`], [`IVec3`] and [`IVec4`]
20 * [`u32`](mod@u32) types
21 * vectors: [`UVec2`], [`UVec3`] and [`UVec4`]
22 * [`bool`](mod@bool) types
23 * vectors: [`BVec2`], [`BVec3`] and [`BVec4`]
24
25 ## SIMD
26
27 `glam` is built with SIMD in mind. Many `f32` types use 128-bit SIMD vector types for storage
28 and/or implementation. The use of SIMD generally enables better performance than using primitive
29 numeric types such as `f32`.
30
31 Some `glam` types use SIMD for storage meaning they are 16 byte aligned, these types include
32 `Mat2`, `Mat3A`, `Mat4`, `Quat`, `Vec3A`, `Vec4`, `Affine2` an `Affine3A`. Types
33 with an `A` suffix are a SIMD alternative to a scalar type, e.g. `Vec3` uses `f32` storage and
34 `Vec3A` uses SIMD storage.
35
36 When SIMD is not available on the target the types will maintain 16 byte alignment and internal
37 padding so that object sizes and layouts will not change between architectures. There are scalar
38 math fallback implementations exist when SIMD is not available. It is intended to add support for
39 other SIMD architectures once they appear in stable Rust.
40
41 Currently only SSE2 on x86/x86_64 is supported as this is what stable Rust supports.
42
43 ## Vec3A and Mat3A
44
45 `Vec3A` is a SIMD optimized version of the `Vec3` type, which due to 16 byte alignment results
46 in `Vec3A` containing 4 bytes of padding making it 16 bytes in size in total. `Mat3A` is composed
47 of three `Vec3A` columns.
48
49 | Type | `f32` bytes | Align bytes | Size bytes | Padding |
50 |:-----------|------------:|------------:|-----------:|--------:|
51 |[`Vec3`] | 12| 4| 12| 0|
52 |[`Vec3A`] | 12| 16| 16| 4|
53 |[`Mat3`] | 36| 4| 36| 0|
54 |[`Mat3A`] | 36| 16| 48| 12|
55
56 Despite this wasted space the SIMD implementations tend to outperform `f32` implementations in
57 [**mathbench**](https://github.com/bitshifter/mathbench-rs) benchmarks.
58
59 `glam` treats [`Vec3`] as the default 3D vector type and [`Vec3A`] a special case for optimization.
60 When methods need to return a 3D vector they will generally return [`Vec3`].
61
62 There are [`From`] trait implementations for converting from [`Vec4`] to a [`Vec3A`] and between
63 [`Vec3`] and [`Vec3A`] (and vice versa).
64
65 ```
66 use glam::{Vec3, Vec3A, Vec4};
67
68 let v4 = Vec4::new(1.0, 2.0, 3.0, 4.0);
69
70 // Convert from `Vec4` to `Vec3A`, this is a no-op if SIMD is supported.
71 let v3a = Vec3A::from(v4);
72 assert_eq!(Vec3A::new(1.0, 2.0, 3.0), v3a);
73
74 // Convert from `Vec3A` to `Vec3`.
75 let v3 = Vec3::from(v3a);
76 assert_eq!(Vec3::new(1.0, 2.0, 3.0), v3);
77
78 // Convert from `Vec3` to `Vec3A`.
79 let v3a = Vec3A::from(v3);
80 assert_eq!(Vec3A::new(1.0, 2.0, 3.0), v3a);
81 ```
82
83 ## Affine2 and Affine3A
84
85 `Affine2` and `Affine3A` are composed of a linear transform matrix and a vector translation. The
86 represent 2D and 3D affine transformations which are commonly used in games.
87
88 The table below shows the performance advantage of `Affine2` over `Mat3A` and `Mat3A` over `Mat3`.
89
90 | operation | `Mat3` | `Mat3A` | `Affine2` |
91 |--------------------|-------------|------------|------------|
92 | inverse | 11.4±0.09ns | 7.1±0.09ns | 5.4±0.06ns |
93 | mul self | 10.5±0.04ns | 5.2±0.05ns | 4.0±0.05ns |
94 | transform point2 | 2.7±0.02ns | 2.7±0.03ns | 2.8±0.04ns |
95 | transform vector2 | 2.6±0.01ns | 2.6±0.03ns | 2.3±0.02ns |
96
97 Performance is much closer between `Mat4` and `Affine3A` with the affine type being faster to
98 invert.
99
100 | operation | `Mat4` | `Affine3A` |
101 |--------------------|-------------|-------------|
102 | inverse | 15.9±0.11ns | 10.8±0.06ns |
103 | mul self | 7.3±0.05ns | 7.0±0.06ns |
104 | transform point3 | 3.6±0.02ns | 4.3±0.04ns |
105 | transform point3a | 3.0±0.02ns | 3.0±0.04ns |
106 | transform vector3 | 4.1±0.02ns | 3.9±0.04ns |
107 | transform vector3a | 2.8±0.02ns | 2.8±0.02ns |
108
109 Benchmarks were taken on an Intel Core i7-4710HQ.
110
111 ## Linear algebra conventions
112
113 `glam` interprets vectors as column matrices (also known as column vectors) meaning when
114 transforming a vector with a matrix the matrix goes on the left.
115
116 ```
117 use glam::{Mat3, Vec3};
118 let m = Mat3::IDENTITY;
119 let x = Vec3::X;
120 let v = m * x;
121 assert_eq!(v, x);
122 ```
123
124 Matrices are stored in memory in column-major order.
125
126 All angles are in radians. Rust provides the `f32::to_radians()` and `f64::to_radians()` methods to
127 convert from degrees.
128
129 ## Direct element access
130
131 Because some types may internally be implemented using SIMD types, direct access to vector elements
132 is supported by implementing the [`Deref`] and [`DerefMut`] traits.
133
134 ```
135 use glam::Vec3A;
136 let mut v = Vec3A::new(1.0, 2.0, 3.0);
137 assert_eq!(3.0, v.z);
138 v.z += 1.0;
139 assert_eq!(4.0, v.z);
140 ```
141
142 [`Deref`]: https://doc.rust-lang.org/std/ops/trait.Deref.html
143 [`DerefMut`]: https://doc.rust-lang.org/std/ops/trait.DerefMut.html
144
145 ## Vector swizzles
146
147 `glam` vector types have functions allowing elements of vectors to be reordered, this includes
148 creating a vector of a different size from the vectors elements.
149
150 The swizzle functions are implemented using traits to add them to each vector type. This is
151 primarily because there are a lot of swizzle functions which can obfuscate the other vector
152 functions in documentation and so on. The traits are [`Vec2Swizzles`], [`Vec3Swizzles`] and
153 [`Vec4Swizzles`].
154
155 Note that the [`Vec3Swizzles`] implementation for [`Vec3A`] will return a [`Vec3A`] for 3 element
156 swizzles, all other implementations will return [`Vec3`].
157
158 ```
159 use glam::{swizzles::*, Vec2, Vec3, Vec3A, Vec4};
160
161 let v = Vec4::new(1.0, 2.0, 3.0, 4.0);
162
163 // Reverse elements of `v`, if SIMD is supported this will use a vector shuffle.
164 let wzyx = v.wzyx();
165 assert_eq!(Vec4::new(4.0, 3.0, 2.0, 1.0), wzyx);
166
167 // Swizzle the yzw elements of `v` into a `Vec3`
168 let yzw = v.yzw();
169 assert_eq!(Vec3::new(2.0, 3.0, 4.0), yzw);
170
171 // To swizzle a `Vec4` into a `Vec3A` swizzle the `Vec4` first then convert to
172 // `Vec3A`. If SIMD is supported this will use a vector shuffle. The last
173 // element of the shuffled `Vec4` is ignored by the `Vec3A`.
174 let yzw = Vec3A::from(v.yzwx());
175 assert_eq!(Vec3A::new(2.0, 3.0, 4.0), yzw);
176
177 // You can swizzle from a `Vec4` to a `Vec2`
178 let xy = v.xy();
179 assert_eq!(Vec2::new(1.0, 2.0), xy);
180
181 // And back again
182 let yyxx = xy.yyxx();
183 assert_eq!(Vec4::new(2.0, 2.0, 1.0, 1.0), yyxx);
184 ```
185
186 ## SIMD and scalar consistency
187
188 `glam` types implement `serde` `Serialize` and `Deserialize` traits to ensure
189 that they will serialize and deserialize exactly the same whether or not
190 SIMD support is being used.
191
192 The SIMD versions implement the `core::fmt::Debug` and `core::fmt::Display`
193 traits so they print the same as the scalar version.
194
195 ```
196 use glam::Vec4;
197 let a = Vec4::new(1.0, 2.0, 3.0, 4.0);
198 assert_eq!(format!("{}", a), "[1, 2, 3, 4]");
199 ```
200
201 ## Feature gates
202
203 All `glam` dependencies are optional, however some are required for tests
204 and benchmarks.
205
206 * `std` - the default feature, has no dependencies.
207 * `rand` - used to generate random values. Used in benchmarks.
208 * `serde` - used for serialization and deserialization of types.
209 * `mint` - used for interoperating with other linear algebra libraries.
210 * `scalar-math` - disables SIMD support and uses native alignment for all types.
211 * `debug-glam-assert` - adds assertions in debug builds which check the validity of parameters
212 passed to `glam` to help catch runtime errors.
213 * `glam-assert` - adds assertions to all builds which check the validity of parameters passed to
214 `glam` to help catch runtime errors.
215
216 ## Minimum Supported Rust Version (MSRV)
217
218 The minimum supported Rust version is `1.45.0`.
219
220 */
221 #![doc(html_root_url = "https://docs.rs/glam/0.15.2")]
222 #![cfg_attr(not(feature = "std"), no_std)]
223 #![cfg_attr(target_arch = "spirv", feature(asm, register_attr, repr_simd))]
224 // This would require renaming a lot of stuff, disabling for now.
225 #![allow(clippy::upper_case_acronyms)]
226
227 #[macro_use]
228 mod macros;
229
230 #[macro_use]
231 mod vec;
232
233 #[doc(hidden)]
234 pub mod cast;
235
236 mod affine2;
237 mod affine3;
238 mod core;
239 mod euler;
240 mod features;
241 mod mat2;
242 mod mat3;
243 mod mat4;
244 mod quat;
245 mod vec2;
246 mod vec3;
247 mod vec4;
248 mod vec_mask;
249
250 #[cfg(feature = "spirv-std")]
251 mod spirv;
252
253 #[cfg(feature = "transform-types")]
254 mod transform;
255
256 #[doc(hidden)]
257 pub use self::core::storage::{XY, XYZ, XYZW};
258
259 /** `bool` vector mask types. */
260 pub mod bool {
261 pub use super::vec_mask::{BVec2, BVec3, BVec3A, BVec4, BVec4A};
262 }
263 pub use self::bool::*;
264
265 /** `f32` vector, quaternion and matrix types. */
266 pub mod f32 {
267 pub use super::affine2::Affine2;
268 pub use super::affine3::Affine3A;
269 pub use super::mat2::{mat2, Mat2};
270 pub use super::mat3::{mat3, mat3a, Mat3, Mat3A};
271 pub use super::mat4::{mat4, Mat4};
272 pub use super::quat::{quat, Quat};
273 pub use super::vec2::{vec2, Vec2};
274 pub use super::vec3::{vec3, vec3a, Vec3, Vec3A};
275 pub use super::vec4::{vec4, Vec4};
276
277 #[cfg(feature = "transform-types")]
278 #[allow(deprecated)]
279 pub use super::transform::{TransformRT, TransformSRT};
280 }
281 pub use self::f32::*;
282
283 /** `f64` vector, quaternion and matrix types. */
284 pub mod f64 {
285 pub use super::affine2::DAffine2;
286 pub use super::affine3::DAffine3;
287 pub use super::mat2::{dmat2, DMat2};
288 pub use super::mat3::{dmat3, DMat3};
289 pub use super::mat4::{dmat4, DMat4};
290 pub use super::quat::{dquat, DQuat};
291 pub use super::vec2::{dvec2, DVec2};
292 pub use super::vec3::{dvec3, DVec3};
293 pub use super::vec4::{dvec4, DVec4};
294 }
295 pub use self::f64::*;
296
297 /** `i32` vector types. */
298 pub mod i32 {
299 pub use super::vec2::{ivec2, IVec2};
300 pub use super::vec3::{ivec3, IVec3};
301 pub use super::vec4::{ivec4, IVec4};
302 }
303 pub use self::i32::*;
304
305 /** `u32` vector types. */
306 pub mod u32 {
307 pub use super::vec2::{uvec2, UVec2};
308 pub use super::vec3::{uvec3, UVec3};
309 pub use super::vec4::{uvec4, UVec4};
310 }
311 pub use self::u32::*;
312
313 /** Traits adding swizzle methods to all vector types. */
314 pub mod swizzles;
315
316 pub use self::swizzles::{Vec2Swizzles, Vec3Swizzles, Vec4Swizzles};
317
318 /** Rotation Helper */
319 pub use euler::EulerRot;