### Matrix Multiplication Examples Source: https://docs.rs/vek/latest/src/vek/mat.rs.html?search= Examples demonstrating matrix multiplication for row-major matrices and mixed row-major/column-major operations. ```rust use vek::mat::row_major::Mat4; let m = Mat4::::new( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5 ); let r = Mat4::::new( 26, 32, 18, 24, 82, 104, 66, 88, 38, 56, 74, 92, 54, 68, 42, 56 ); assert_eq!(m * m, r); assert_eq!(m, m * Mat4::::identity()); assert_eq!(m, Mat4::::identity() * m); ``` ```rust use vek::mat::row_major::Mat4 as Rows4; use vek::mat::column_major::Mat4 as Cols4; let m = Rows4::::new( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5 ); let b = Cols4::from(m); let r = Cols4::::new( 26, 32, 18, 24, 82, 104, 66, 88, 38, 56, 74, 92, 54, 68, 42, 56 ); assert_eq!(m * b, r); assert_eq!(m * Cols4::::identity(), m.into()); assert_eq!(Rows4::::identity() * b, m.into()); ``` -------------------------------- ### Transform to Mat4 conversion example Source: https://docs.rs/vek/latest/src/vek/transform.rs.html Demonstrates converting a Transform into a Mat4 matrix. This example shows how to construct a matrix from position, orientation, and scale components. ```rust let (p, rz, s) = (Vec3::unit_x(), 3.0_f32, 5.0_f32); let a = Mat4::scaling_3d(s).rotated_z(rz).translated_3d(p); let b = Mat4::from(Transform { position: p, orientation: Quaternion::rotation_z(rz), scale: Vec3::broadcast(s), }); assert_relative_eq!(a, b); ``` -------------------------------- ### Create Matrix from Basis Vectors (Contrived Example) Source: https://docs.rs/vek/latest/src/vek/mat.rs.html A more complex example demonstrating the creation of a transformation matrix from a rotated and translated basis. Verifies the matrix correctly maps points. ```rust # extern crate vek; # #[macro_use] extern crate approx; # use vek::{Mat4, Vec3}; # fn main() { // Sanity test let origin = Vec3::new(1_f32, 2., 3.); let r = Mat4::rotation_3d(3., Vec3::new(2_f32, 1., 3.)); let i = r.mul_direction(Vec3::unit_x()); let j = r.mul_direction(Vec3::unit_y()); let k = r.mul_direction(Vec3::unit_z()); let m = Mat4::local_to_basis(origin, i, j, k); assert_relative_eq!(origin, m.mul_point(Vec3::zero())); assert_relative_eq!(origin+i, m.mul_point(Vec3::unit_x())); assert_relative_eq!(origin+j, m.mul_point(Vec3::unit_y())); assert_relative_eq!(origin+k, m.mul_point(Vec3::unit_z())); # } ``` -------------------------------- ### Diagonal Extraction Examples Source: https://docs.rs/vek/latest/src/vek/mat.rs.html Demonstrates extracting the diagonal elements of a matrix into a vector. Includes examples for a zero matrix, an identity matrix, and a matrix with custom diagonal values. ```rust assert_eq!(Mat4::::zero().diagonal(), Vec4::zero()); assert_eq!(Mat4::::identity().diagonal(), Vec4::one()); let mut m = Mat4::zero(); m[(0, 0)] = 1; m[(1, 1)] = 2; m[(2, 2)] = 3; m[(3, 3)] = 4; assert_eq!(m.diagonal(), Vec4::new(1, 2, 3, 4)); assert_eq!(m.diagonal(), Vec4::iota() + 1); ``` -------------------------------- ### Constructor: new Source: https://docs.rs/vek/latest/vek/geom/repr_c/struct.Ray.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Creates a new Ray instance. ```APIDOC ## impl> Ray ### pub fn new(origin: Vec3, direction: Vec3) -> Self Creates a `Ray` from a starting point and direction. This doesn’t check if `direction` is normalized, because either you know it is, or it isn’t and maybe it doesn’t matter for your use case. ``` -------------------------------- ### Iterate Over a Slice in Rust Source: https://docs.rs/vek/latest/vek/vec/repr_c/extent2/struct.Extent2.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Use `iter()` to get an iterator that yields immutable references to slice elements from start to end. ```rust let x = &[1, 2, 4]; let mut iterator = x.iter(); assert_eq!(iterator.next(), Some(&1)); assert_eq!(iterator.next(), Some(&2)); assert_eq!(iterator.next(), Some(&4)); assert_eq!(iterator.next(), None); ``` -------------------------------- ### Constructor: new Source: https://docs.rs/vek/latest/vek/geom/repr_c/struct.Ray.html?search= Creates a new Ray instance. ```APIDOC ## `new` - `impl> Ray` ### Description Creates a `Ray` from a starting point and direction. This doesn’t check if `direction` is normalized, because either you know it is, or it isn’t and maybe it doesn’t matter for your use case. ### Method `pub fn new(origin: Vec3, direction: Vec3) -> Self` ``` -------------------------------- ### Slice Pointer Access Source: https://docs.rs/vek/latest/vek/vec/repr_c/rgb/struct.Rgb.html?search=std%3A%3Avec Provides methods to get mutable pointers to the start and end of a slice, useful for interfacing with C-style APIs. ```APIDOC ## GET /websites/rs_vek/as_mut_ptr_range ### Description Returns the two unsafe mutable pointers spanning the slice. The returned range is half-open, meaning the end pointer points one past the last element. This is useful for interacting with foreign interfaces that use two pointers to refer to a range of elements. ### Method GET ### Endpoint /websites/rs_vek/as_mut_ptr_range ### Parameters #### Path Parameters None #### Query Parameters None ### Response #### Success Response (200) - **ptr_range** (*Range<*mut T*>*) - A range containing the start and end mutable pointers. #### Response Example ```json { "ptr_range": { "start": "0x...", "end": "0x..." } } ``` ``` -------------------------------- ### Get Multiplicative Identity for Mat2 Source: https://docs.rs/vek/latest/vek/mat/repr_c/row_major/mat2/struct.Mat2.html?search=std%3A%3Avec Returns the multiplicative identity element (1) for `Mat2`. This is useful for initializing matrices or as a starting point for operations. ```rust impl> One for Mat2 Source§ #### fn one() -> Self Returns the multiplicative identity element of `Self`, `1`. Read more ``` -------------------------------- ### OpenGL and Initialization Source: https://docs.rs/vek/latest/vek/mat/repr_c/column_major/mat4/struct.Mat4.html?search=u32+-%3E+bool Methods for OpenGL integration and matrix creation. ```APIDOC ## gl_should_transpose ### Description Gets the transpose parameter to pass to OpenGL glUniformMatrix*() functions. ## new ### Description Creates a new 4x4 matrix from 16 individual elements in a layout-agnostic way. ``` -------------------------------- ### Initialize and Convert Mat4 Source: https://docs.rs/vek/latest/vek/mat/repr_c/row_major/mat4/struct.Mat4.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Examples for creating identity matrices, performing type casting, and broadcasting diagonal values. ```rust assert_eq!(Mat4::::default(), Mat4::::identity()); ``` ```rust let m = Mat4::::identity(); let m: Mat4 = m.numcast().unwrap(); assert_eq!(m, Mat4::identity()); ``` ```rust assert_eq!(Mat4::broadcast_diagonal(0), Mat4::zero()); assert_eq!(Mat4::broadcast_diagonal(1), Mat4::identity()); assert_eq!(Mat4::broadcast_diagonal(2), Mat4::new( 2,0,0,0, 0,2,0,0, 0,0,2,0, 0,0,0,2, )); ``` -------------------------------- ### Get Pointer Range of Slice with as_ptr_range Source: https://docs.rs/vek/latest/vek/vec/repr_c/extent2/struct.Extent2.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Retrieve the start and end raw pointers of a slice using `as_ptr_range`. The end pointer is one past the last element. Useful for C-style interfaces. ```rust let a = [1, 2, 3]; let x = &a[1] as *const _; let y = &5 as *const _; assert!(a.as_ptr_range().contains(&x)); assert!(!a.as_ptr_range().contains(&y)); ``` -------------------------------- ### Get Element Offset for Unaligned Element Source: https://docs.rs/vek/latest/vek/vec/repr_c/extent2/struct.Extent2.html?search=u32+-%3E+bool Illustrates `element_offset` returning `None` when an element reference does not point to the start of an element within the slice. This occurs with flattened slices where references might be unaligned. Requires nightly Rust. ```rust #![feature(substr_range)] let arr: &[[u32; 2]] = &[[0, 1], [2, 3]]; let flat_arr: &[u32] = arr.as_flattened(); let ok_elm: &[u32; 2] = flat_arr[0..2].try_into().unwrap(); let weird_elm: &[u32; 2] = flat_arr[1..3].try_into().unwrap(); assert_eq!(ok_elm, &[0, 1]); assert_eq!(weird_elm, &[1, 2]); assert_eq!(arr.element_offset(ok_elm), Some(0)); // Points to element 0 assert_eq!(arr.element_offset(weird_elm), None); // Points between element 0 and 1 ``` -------------------------------- ### Wrap Example: Integer Wrapping Source: https://docs.rs/vek/latest/src/vek/ops.rs.html?search= Demonstrates the `wrapped` method for i32, showing how negative numbers wrap around a positive bound. ```rust assert_eq!((-5_i32).wrapped(3), 1); assert_eq!((-4_i32).wrapped(3), 2); assert_eq!((-3_i32).wrapped(3), 0); assert_eq!((-2_i32).wrapped(3), 1); assert_eq!((-1_i32).wrapped(3), 2); assert_eq!(0_i32.wrapped(3), 0); assert_eq!(1_i32.wrapped(3), 1); assert_eq!(2_i32.wrapped(3), 2); assert_eq!(3_i32.wrapped(3), 0); assert_eq!(4_i32.wrapped(3), 1); assert_eq!(5_i32.wrapped(3), 2); ``` -------------------------------- ### Get Element Offset in Slice Source: https://docs.rs/vek/latest/vek/vec/repr_c/extent2/struct.Extent2.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Uses the nightly-only `element_offset` method to find the index of an element reference within a slice. This method relies on pointer arithmetic and does not compare elements. It returns `None` if the element reference is not aligned with the start of an element. ```rust #![feature(substr_range)] let nums: &[u32] = &[1, 7, 1, 1]; let num = &nums[2]; assert_eq!(num, &1); assert_eq!(nums.element_offset(num), Some(2)); ``` ```rust #![feature(substr_range)] let arr: &[[u32; 2]] = &[[0, 1], [2, 3]]; let flat_arr: &[u32] = arr.as_flattened(); let ok_elm: &[u32; 2] = flat_arr[0..2].try_into().unwrap(); let weird_elm: &[u32; 2] = flat_arr[1..3].try_into().unwrap(); assert_eq!(ok_elm, &[0, 1]); assert_eq!(weird_elm, &[1, 2]); assert_eq!(arr.element_offset(ok_elm), Some(0)); // Points to element 0 assert_eq!(arr.element_offset(weird_elm), None); // Points between element 0 and 1 ``` -------------------------------- ### Example Usage of 3D Rotation Matrices Source: https://docs.rs/vek/latest/src/vek/mat.rs.html?search= Demonstrates creating and applying various 3D rotations using `Mat4::rotation_z`, `Mat4::rotation_x`, `Mat4::rotation_y`, and `Mat4::rotation_3d`. Includes assertions to verify rotation results. ```rust # extern crate vek; # #[macro_use] extern crate approx; # use vek::{Mat4, Vec4}; # use std::f32::consts::PI; # # fn main() { let v = Vec4::unit_x(); let m = Mat4::rotation_z(PI); assert_relative_eq!(m * v, -v); let m = Mat4::rotation_z(PI * 0.5); assert_relative_eq!(m * v, Vec4::unit_y()); let m = Mat4::rotation_z(PI * 1.5); assert_relative_eq!(m * v, -Vec4::unit_y()); let angles = 32; for i in 0..angles { let theta = PI * 2. * (i as f32) / (angles as f32); // See what rotating unit vectors do for most angles between 0 and 2*PI. // It's helpful to picture this as a right-handed coordinate system. let v = Vec4::unit_y(); let m = Mat4::rotation_x(theta); assert_relative_eq!(m * v, Vec4::new(0., theta.cos(), theta.sin(), 0.)); let v = Vec4::unit_z(); let m = Mat4::rotation_y(theta); assert_relative_eq!(m * v, Vec4::new(theta.sin(), 0., theta.cos(), 0.)); let v = Vec4::unit_x(); let m = Mat4::rotation_z(theta); assert_relative_eq!(m * v, Vec4::new(theta.cos(), theta.sin(), 0., 0.)); assert_relative_eq!(Mat4::rotation_x(theta), Mat4::rotation_3d(theta, Vec4::unit_x())); assert_relative_eq!(Mat4::rotation_y(theta), Mat4::rotation_3d(theta, Vec4::unit_y())); assert_relative_eq!(Mat4::rotation_z(theta), Mat4::rotation_3d(theta, Vec4::unit_z())); } # } ``` -------------------------------- ### Get Element Offset in Slice Source: https://docs.rs/vek/latest/vek/vec/repr_c/extent2/struct.Extent2.html?search=u32+-%3E+bool Shows how to find the index of an element within a slice using `element_offset`. This method uses pointer arithmetic and does not compare elements. Returns `None` if the element reference is not aligned with the start of an element. Requires nightly Rust. ```rust #![feature(substr_range)] let nums: &[u32] = &[1, 7, 1, 1]; let num = &nums[2]; assert_eq!(num, &1); assert_eq!(nums.element_offset(num), Some(2)); ``` -------------------------------- ### Mat3 Rotation Examples Source: https://docs.rs/vek/latest/src/vek/mat.rs.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Demonstrates how to apply rotations to vectors using Mat3. Requires `vek` and `approx` crates. ```rust # extern crate vek; # #[macro_use] extern crate approx; # use vek::{Vec3, Mat3}; # fn main() { let theta = std::f32::consts::PI / 2.0; let v = Vec3::unit_y(); let m = Mat3::rotation_x(theta); assert_relative_eq!(m * v, Vec3::new(0., theta.cos(), theta.sin())); let v = Vec3::unit_z(); let m = Mat3::rotation_y(theta); assert_relative_eq!(m * v, Vec3::new(theta.sin(), 0., theta.cos())); let v = Vec3::unit_x(); let m = Mat3::rotation_z(theta); assert_relative_eq!(m * v, Vec3::new(theta.cos(), theta.sin(), 0.)); # } ``` -------------------------------- ### Invert RGB Example Source: https://docs.rs/vek/latest/src/vek/vec.rs.html Example usage of the inverted_rgb method. ```rust # use vek::Rgba; let opaque_orange = Rgba::new(255_u8, 128, 0, 255_u8); assert_eq!(opaque_orange.inverted_rgb(), Rgba::new(0, 127, 255, 255)); assert_eq!(Rgba::::black().inverted_rgb(), Rgba::white()); assert_eq!(Rgba::::white().inverted_rgb(), Rgba::black()); ``` -------------------------------- ### get Source: https://docs.rs/vek/latest/vek/vec/repr_c/extent3/struct.Extent3.html?search= Safely gets an element or subslice by index or range. ```APIDOC ## pub fn get(&self, index: I) -> Option<&>::Output> ### Description Returns a reference to an element or subslice depending on the type of index. If given a position, returns a reference to the element at that position or `None` if out of bounds. If given a range, returns the subslice corresponding to that range, or `None` if out of bounds. ### Method GET ### Endpoint N/A (Method on slice) ### Parameters #### Path Parameters - **index** (I: SliceIndex<[T]>) - Required - The index or range to access. ### Request Example ```rust let v = [10, 40, 30]; assert_eq!(Some(&40), v.get(1)); assert_eq!(Some(&[10, 40][..]), v.get(0..2)); assert_eq!(None, v.get(3)); assert_eq!(None, v.get(0..4)); ``` ### Response #### Success Response (200) - **Option<&>::Output>** - A reference to the element or subslice, or None if the index is out of bounds. ``` -------------------------------- ### Transition Construction Methods Source: https://docs.rs/vek/latest/vek/transition/struct.Transition.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Methods for initializing a Transition instance. ```rust pub fn with_mapper(start: T, end: T, progress_mapper: F) -> Self where Progress: Zero, ``` ```rust pub fn with_mapper_and_progress( start: T, end: T, progress_mapper: F, progress: Progress, ) -> Self ``` ```rust pub fn new(start: T, end: T) -> Self where Progress: Zero, ``` ```rust pub fn with_progress(start: T, end: T, progress: Progress) -> Self ``` -------------------------------- ### Get TypeId of Any Source: https://docs.rs/vek/latest/vek/geom/repr_c/struct.Aabb.html?search=u32+-%3E+bool Gets the TypeId of a value. Applicable to any type T that is 'static. ```rust fn type_id(&self) -> TypeId ``` -------------------------------- ### IsBetween Trait Example Source: https://docs.rs/vek/latest/vek/ops/trait.IsBetween.html?search= Example demonstrating the usage of the `is_between` method for `i32`. ```APIDOC ```rust use vek::ops::IsBetween; assert!(5_i32 .is_between(5, 10)); assert!(7_i32 .is_between(5, 10)); assert!(10_i32.is_between(5, 10)); assert!(!(4_i32 .is_between(5, 10))); assert!(!(11_i32.is_between(5, 10))); ``` ``` -------------------------------- ### Transition Creation Methods Source: https://docs.rs/vek/latest/vek/transition/struct.Transition.html?search=u32+-%3E+bool Methods for creating new Transition instances. ```APIDOC ## Transition Creation Methods ### `with_mapper(start: T, end: T, progress_mapper: F) -> Self` Creates a new `Transition` from `start` and `end` values and `progress_mapper`, setting `progress` to zero. Requires `Progress: Zero`. ### `with_mapper_and_progress(start: T, end: T, progress_mapper: F, progress: Progress) -> Self` Creates a new `Transition` from `start`, `end`, `progress_mapper` and `progress` values. ### `new(start: T, end: T) -> Self` Creates a new `LinearTransition` from `start` and `end` values, setting `progress` to zero. This method is available when `F` is `IdentityProgressMapper` and requires `Progress: Zero`. ### `with_progress(start: T, end: T, progress: Progress) -> Self` Creates a new `LinearTransition` from `start`, `end` and `progress` values. This method is available when `F` is `IdentityProgressMapper`. ``` -------------------------------- ### Example Matrix Multiplication Usage Source: https://docs.rs/vek/latest/src/vek/mat.rs.html?search= Example usage of matrix multiplication with vectors. ```rust use vek::mat::column_major::Mat4; use vek::vec::Vec4; let m = Mat4::new( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5 ``` -------------------------------- ### OpenGL and Initialization Source: https://docs.rs/vek/latest/vek/mat/repr_c/row_major/mat4/struct.Mat4.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Methods for OpenGL matrix transposition and matrix creation. ```APIDOC ## OpenGL and Initialization ### Description Utilities for OpenGL integration and matrix construction. ### Methods - `gl_should_transpose(&self) -> bool` - `new(m00: T, ..., m33: T) -> Self` ``` -------------------------------- ### Scalar Broadcasting Usage Examples Source: https://docs.rs/vek/latest/src/vek/vec.rs.html?search=std%3A%3Avec Examples demonstrating how scalars are broadcasted to vectors via the From trait. ```rust # use vek::{Mat4, Vec3, Vec4}; assert_eq!(Vec4::min(4, 5), Vec4::broadcast(4)); assert_eq!(Vec4::max(4, 5), Vec4::broadcast(5)); assert_eq!(Vec4::from(4), Vec4::broadcast(4)); assert_eq!(Vec4::from(4).mul_add(4, 5), Vec4::broadcast(21)); // scaling_3d() logically accepts a Vec3... let _ = Mat4::::scaling_3d(Vec3::broadcast(5.0)); // ... but there you go; quick uniform scale, thanks to Into ! let _ = Mat4::scaling_3d(5_f32); ``` ```rust # use vek::Mat4; // This creates a matrix that translates to (5,5,5), but it's probably not what you meant. // Hopefully the `_3d` suffix would help you catch this. let _ = Mat4::translation_3d(5_f32); // translation_3d() takes V: Into because it allows it to accept // Vec2, Vec3 and Vec4, and also with both repr(C) and repr(simd) layouts. ``` -------------------------------- ### Transition Creation Methods Source: https://docs.rs/vek/latest/src/vek/transition.rs.html Provides methods for creating `Transition` instances, including `with_mapper` (setting progress to zero) and `with_mapper_and_progress` (setting initial progress). It also includes `into_range` to convert back to a `Range`. ```rust impl Transition { /// Creates a new `Transition` from `start` and `end` values and `progress_mapper`, setting `progress` to zero. pub fn with_mapper(start: T, end: T, progress_mapper: F) -> Self where Progress: Zero { Self { start, end, progress_mapper, progress: Zero::zero() } } /// Creates a new `Transition` from `start`, `end`, `progress_mapper` and `progress` values. pub fn with_mapper_and_progress(start: T, end: T, progress_mapper: F, progress: Progress) -> Self { Self { start, end, progress_mapper, progress } } /// Converts this into a `Range`, dropping the `progress` and `progress_mapper` values. pub fn into_range(self) -> Range { let Self { start, end, .. } = self; Range { start, end } } } ``` -------------------------------- ### get Source: https://docs.rs/vek/latest/vek/vec/repr_c/extent2/struct.Extent2.html?search=u32+-%3E+bool Safely gets a reference to an element or subslice by index or range. Returns None if out of bounds. ```APIDOC ## pub fn get(&self, index: I) -> Option<&>::Output> ### Description Returns a reference to an element or subslice depending on the type of index. If given a position, returns a reference to the element at that position or `None` if out of bounds. If given a range, returns the subslice corresponding to that range, or `None` if out of bounds. ### Method GET ### Endpoint N/A (Method on slice) ### Parameters #### Query Parameters - **index** (I) - Required - The index or range to access. ### Examples ```rust let v = [10, 40, 30]; assert_eq!(Some(&40), v.get(1)); assert_eq!(Some(&[10, 40][..]), v.get(0..2)); assert_eq!(None, v.get(3)); assert_eq!(None, v.get(0..4)); ``` ``` -------------------------------- ### Create a basis_to_local transformation matrix Source: https://docs.rs/vek/latest/src/vek/mat.rs.html?search= Demonstrates building a matrix to transform points from canonical space to a local basis, including verification that it is the inverse of local_to_basis. ```rust # extern crate vek; # #[macro_use] extern crate approx; # use vek::{Mat4, Vec3}; # fn main() { let origin = Vec3::new(1_f32, 2., 3.); let i = Vec3::unit_z(); let j = Vec3::unit_y(); let k = Vec3::unit_x(); let m = Mat4::basis_to_local(origin, i, j, k); assert_relative_eq!(m.mul_point(origin), Vec3::zero()); assert_relative_eq!(m.mul_point(origin+i), Vec3::unit_x()); assert_relative_eq!(m.mul_point(origin+j), Vec3::unit_y()); assert_relative_eq!(m.mul_point(origin+k), Vec3::unit_z()); // `local_to_basis` and `basis_to_local` undo each other let a = Mat4::::basis_to_local(origin, i, j, k); let b = Mat4::::local_to_basis(origin, i, j, k); assert_relative_eq!(a*b, Mat4::identity()); assert_relative_eq!(b*a, Mat4::identity()); # } ``` ```rust # extern crate vek; # #[macro_use] extern crate approx; # use vek::{Mat4, Vec3}; # fn main() { let origin = Vec3::new(1_f32, 2., 3.); let r = Mat4::rotation_3d(3., Vec3::new(2_f32, 1., 3.)); let i = r.mul_direction(Vec3::unit_x()); let j = r.mul_direction(Vec3::unit_y()); let k = r.mul_direction(Vec3::unit_z()); let m = Mat4::basis_to_local(origin, i, j, k); assert_relative_eq!(m.mul_point(origin), Vec3::zero(), epsilon = 0.000001); assert_relative_eq!(m.mul_point(origin+i), Vec3::unit_x(), epsilon = 0.000001); assert_relative_eq!(m.mul_point(origin+j), Vec3::unit_y(), epsilon = 0.000001); assert_relative_eq!(m.mul_point(origin+k), Vec3::unit_z(), epsilon = 0.000001); # } ``` -------------------------------- ### Lerp Usage Examples Source: https://docs.rs/vek/latest/vek/ops/trait.Lerp.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Examples demonstrating how to use the Lerp trait with primitive types and custom structures. ```APIDOC ```rust use vek::ops::Lerp; // Example with primitive types let a = Lerp::lerp(0, 10, 0.5_f32); let b = Lerp::lerp(&0, &10, 0.5_f32); let c = i32::lerp(0, 10, 0.5_f32); let d = <&i32>::lerp(&0, &10, 0.5_f32); assert_eq!(a, b); assert_eq!(a, c); assert_eq!(a, d); // Example with a custom struct (GameState) /// A data-heavy structure that represents a current game state. /// It's neither Copy and nor even Clone! struct GameState { pub camera_position: Vec3, // ... obviously a lot of other members following ... } // We can select the Progress type. I chose f64; the default is f32. impl<'a> Lerp for &'a GameState { type Output = GameState; fn lerp_unclamped(a: Self, b: Self, t: f64) -> GameState { GameState { camera_position: Lerp::lerp(a.camera_position, b.camera_position, t as f32), // ... etc for all relevant members... } } } let a = GameState { camera_position: Vec3::zero() }; let b = GameState { camera_position: Vec3::unit_x() }; let c = Lerp::lerp(&a, &b, 0.5); // Hurray! We've got an interpolated state without consuming the two previous ones. ``` ``` -------------------------------- ### Matrix Initialization and Utility Methods Source: https://docs.rs/vek/latest/src/vek/mat.rs.html?search=u32+-%3E+bool Methods for creating identity matrices, zero matrices, and broadcasting diagonal values. ```APIDOC ## Matrix Initialization ### Description Methods to initialize matrices with specific patterns like identity, zero, or diagonal values. ### Methods - `identity()`: Returns the identity matrix. - `zero()`: Returns a matrix with all elements set to zero. - `broadcast_diagonal(val: T)`: Initializes a matrix with the diagonal set to `val` and others to zero. - `with_diagonal(d: $Vec)`: Initializes a matrix using a vector for the diagonal. ### Parameters - **val** (T) - Required - The value to set on the diagonal. - **d** ($Vec) - Required - The vector containing diagonal elements. ``` -------------------------------- ### get Source: https://docs.rs/vek/latest/vek/vec/repr_c/extent2/struct.Extent2.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Safely gets a reference to an element or subslice by index or range. Returns None if the index is out of bounds. ```APIDOC ## pub fn get(&self, index: I) -> Option<&>::Output> ### Description Returns a reference to an element or subslice depending on the type of index. If given a position, returns a reference to the element at that position or `None` if out of bounds. If given a range, returns the subslice corresponding to that range, or `None` if out of bounds. ### Method GET ### Endpoint N/A (Method on slice) ### Parameters #### Path Parameters - **index** (I: SliceIndex<[T]>) - Required - The index or range to access. ### Request Example ```rust let v = [10, 40, 30]; assert_eq!(Some(&40), v.get(1)); assert_eq!(Some(&[10, 40][..]), v.get(0..2)); assert_eq!(None, v.get(3)); assert_eq!(None, v.get(0..4)); ``` ### Response #### Success Response (200) - **`Option<&>::Output>`**: A reference to the element or subslice, or None if the index is out of bounds. ``` -------------------------------- ### vek::transition Module Overview Source: https://docs.rs/vek/latest/vek/transition/index.html?search=std%3A%3Avec Overview of the structs and traits available in the vek::transition module for handling value transitions. ```APIDOC ## Structs ### IdentityProgressMapper A pass-through functor that returns progress values directly as LERP factors. ### ProgressMapperFn A function pointer container that can map a progress value to a LERP factor. ### Transition A convenience structure for storing a progression from one value to another. ## Traits ### ProgressMapper A functor that maps a progress value to a LERP factor. ## Type Aliases ### LinearTransition A convenience structure for storing a linear progression from one value to another. ``` -------------------------------- ### Mat2 Get OpenGL Transpose Flag Source: https://docs.rs/vek/latest/vek/mat/repr_c/row_major/mat2/struct.Mat2.html Gets the transpose parameter for OpenGL `glUniformMatrix*()` functions as a boolean. ```APIDOC ## gl_should_transpose() -> bool ### Description Gets the `transpose` parameter to pass to OpenGL `glUniformMatrix*()` functions. ### Method `pub fn gl_should_transpose(&self) -> bool` ### Return Value A plain `bool` which you may directly cast to a `GLboolean`. ### Notes This takes `&self` to prevent surprises when changing the type of matrix you plan to send. ``` -------------------------------- ### Disk Creation Methods Source: https://docs.rs/vek/latest/vek/geom/repr_c/struct.Disk.html?search=std%3A%3Avec Methods for creating new Disk instances with specific radii. ```APIDOC ## Disk Creation ### `new(center: Vec2

, radius: E) -> Self` Creates a new Disk from `center` and `radius`. ### `unit(center: Vec2

) -> Self` Creates a new Disk from `center` and a `radius` equal to one. ### `point(center: Vec2

) -> Self` Creates a new Disk from `center` and a `radius` equal to zero. ``` -------------------------------- ### Mat3 Constructors and Static Methods Source: https://docs.rs/vek/latest/vek/mat/repr_c/row_major/mat3/struct.Mat3.html?search= Methods for creating Mat3 instances, including identity, zero, and diagonal matrices. ```APIDOC ### Static Methods * `identity() -> Self` - Returns the identity matrix. Requires `T: Zero + One`. * `zero() -> Self` - Returns a matrix with all elements set to zero. Requires `T: Zero`. * `broadcast_diagonal(val: T) -> Self` - Creates a matrix with diagonal elements set to `val` and others to zero. Requires `T: Zero + Copy`. * `with_diagonal(d: Vec3) -> Self` - Creates a matrix with diagonal elements from `d` and others to zero. Requires `T: Zero + Copy`. ``` -------------------------------- ### Get Row Pointer Source: https://docs.rs/vek/latest/vek/mat/repr_c/row_major/mat2/struct.Mat2.html Gets a const pointer to the matrix's elements, assuming row-major packing. ```APIDOC ## as_row_ptr() -> *const T ### Description Gets a const pointer to this matrix’s elements. ### Method `pub fn as_row_ptr(&self) -> *const T` ### Panics Panics if the matrix’s elements are not tightly packed in memory, which may be the case for matrices in the `repr_simd` module. You may check this with the `is_packed()` method. ### Response Example ```rust // Returns a raw const pointer to the first element of the matrix. // Example: 0x7ffc12345678 as *const u32 ``` ``` -------------------------------- ### Create a local_to_basis transformation matrix Source: https://docs.rs/vek/latest/src/vek/mat.rs.html?search= Demonstrates building a matrix to transform points from a local basis to canonical space. ```rust # extern crate vek; # #[macro_use] extern crate approx; # use vek::{Mat4, Vec3}; # fn main() { let origin = Vec3::new(1_f32, 2., 3.); let i = Vec3::unit_z(); let j = Vec3::unit_y(); let k = Vec3::unit_x(); let m = Mat4::local_to_basis(origin, i, j, k); assert_relative_eq!(origin, m.mul_point(Vec3::zero())); assert_relative_eq!(origin+i, m.mul_point(Vec3::unit_x())); assert_relative_eq!(origin+j, m.mul_point(Vec3::unit_y())); assert_relative_eq!(origin+k, m.mul_point(Vec3::unit_z())); // `local_to_basis` and `basis_to_local` undo each other let a = Mat4::::local_to_basis(origin, i, j, k); let b = Mat4::::basis_to_local(origin, i, j, k); assert_relative_eq!(a*b, Mat4::identity()); assert_relative_eq!(b*a, Mat4::identity()); # } ``` -------------------------------- ### Get Mutable Row Pointer Source: https://docs.rs/vek/latest/vek/mat/repr_c/row_major/mat2/struct.Mat2.html Gets a mutable pointer to the matrix's elements, assuming row-major packing. ```APIDOC ## as_mut_row_ptr() -> *mut T ### Description Gets a mut pointer to this matrix’s elements. ### Method `pub fn as_mut_row_ptr(&mut self) -> *mut T` ### Panics Panics if the matrix’s elements are not tightly packed in memory, which may be the case for matrices in the `repr_simd` module. You may check this with the `is_packed()` method. ### Response Example ```rust // Returns a raw mutable pointer to the first element of the matrix. // Example: 0x7ffc12345678 as *mut u32 ``` ``` -------------------------------- ### Ray::new Source: https://docs.rs/vek/latest/vek/geom/repr_c/struct.Ray.html?search=std%3A%3Avec Creates a new Ray instance from a starting point and a direction vector. ```APIDOC ## fn new(origin: Vec3, direction: Vec3) -> Self ### Description Creates a `Ray` from a starting point and direction. Note that methods expect the direction vector to be normalized. ### Parameters #### Request Body - **origin** (Vec3) - Required - The ray’s starting point. - **direction** (Vec3) - Required - The ray’s direction vector. ``` -------------------------------- ### Get Mutable Column Pointer Source: https://docs.rs/vek/latest/vek/mat/repr_c/column_major/mat3/struct.Mat3.html Gets a mutable pointer to the matrix's elements, assuming column-major packing. ```APIDOC ## pub fn as_mut_col_ptr(&mut self) -> *mut T ### Description Gets a mut pointer to this matrix’s elements. ### Method `as_mut_col_ptr` ### Panics Panics if the matrix’s elements are not tightly packed in memory, which may be the case for matrices in the `repr_simd` module. You may check this with the `is_packed()` method. ### Response Example ```rust // Returns a *mut T pointer to the matrix elements. ``` ``` -------------------------------- ### Get Matrix Row Pointer Source: https://docs.rs/vek/latest/vek/mat/repr_c/row_major/mat3/struct.Mat3.html?search= Gets a const pointer to this matrix’s elements. Panics if elements are not tightly packed. ```APIDOC ## GET /api/matrix/as_row_ptr ### Description Gets a const pointer to this matrix’s elements. Panics if elements are not tightly packed. ### Method GET ### Endpoint /api/matrix/as_row_ptr ### Panics Panics if the matrix’s elements are not tightly packed in memory, which may be the case for matrices in the `repr_simd` module. You may check this with the `is_packed()` method. ### Response #### Success Response (200) - **pointer** (*const T) - A const pointer to the matrix elements. #### Response Example ```json { "pointer": "0x1234567890abcdef" } ``` ``` -------------------------------- ### Disk Constructors Source: https://docs.rs/vek/latest/vek/geom/repr_c/struct.Disk.html?search=u32+-%3E+bool Provides methods for creating new Disk instances. ```APIDOC ## impl Disk ### pub fn new(center: Vec2

, radius: E) -> Self Creates a new Disk from `center` and `radius`. ### pub fn unit(center: Vec2

) -> Self Creates a new Disk from `center` and a `radius` equal to one. ### pub fn point(center: Vec2

) -> Self Creates a new Disk from `center` and a `radius` equal to zero. ``` -------------------------------- ### Get Disk Bounding Rectangle Source: https://docs.rs/vek/latest/vek/geom/repr_c/struct.Disk.html?search= Gets the bounding rectangle for this shape. Requires specific trait bounds on P and E. ```rust pub fn rect(self) -> Rect where P: Sub + From + Copy, E: Copy + Add, ``` -------------------------------- ### 3D Rotation Examples Source: https://docs.rs/vek/latest/src/vek/mat.rs.html Demonstrates the usage of `rotation_z`, `rotation_x`, `rotation_y`, and `rotation_3d` functions with assertions to verify rotations of unit vectors. ```rust # extern crate vek; # #[macro_use] extern crate approx; # use vek::{Mat4, Vec4}; use std::f32::consts::PI; # fn main() { let v = Vec4::unit_x(); let m = Mat4::rotation_z(PI); assert_relative_eq!(m * v, -v); let m = Mat4::rotation_z(PI * 0.5); assert_relative_eq!(m * v, Vec4::unit_y()); let m = Mat4::rotation_z(PI * 1.5); assert_relative_eq!(m * v, -Vec4::unit_y()); let angles = 32; for i in 0..angles { let theta = PI * 2. * (i as f32) / (angles as f32); // See what rotating unit vectors do for most angles between 0 and 2*PI. // It's helpful to picture this as a right-handed coordinate system. let v = Vec4::unit_y(); let m = Mat4::rotation_x(theta); assert_relative_eq!(m * v, Vec4::new(0., theta.cos(), theta.sin(), 0.)); let v = Vec4::unit_z(); let m = Mat4::rotation_y(theta); assert_relative_eq!(m * v, Vec4::new(theta.sin(), 0., theta.cos(), 0.)); let v = Vec4::unit_x(); let m = Mat4::rotation_z(theta); assert_relative_eq!(m * v, Vec4::new(theta.cos(), theta.sin(), 0., 0.)); assert_relative_eq!(Mat4::rotation_x(theta), Mat4::rotation_3d(theta, Vec4::unit_x())); assert_relative_eq!(Mat4::rotation_y(theta), Mat4::rotation_3d(theta, Vec4::unit_y())); assert_relative_eq!(Mat4::rotation_z(theta), Mat4::rotation_3d(theta, Vec4::unit_z())); } # } ``` -------------------------------- ### vek::transition Module Overview Source: https://docs.rs/vek/latest/vek/transition/index.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Overview of the structs and traits available in the vek::transition module for handling value interpolation. ```APIDOC ## Module: vek::transition ### Description Convenience structures for representing a transition from one value to another, useful for animation and custom interpolation. ### Structs - **IdentityProgressMapper**: A pass-through functor that returns progress values directly as LERP factors. - **ProgressMapperFn**: A function pointer container that can map a progress value to a LERP factor. - **Transition**: A convenience structure for storing a progression from one value to another. ### Traits - **ProgressMapper**: A functor that maps a progress value to a LERP factor. ### Type Aliases - **LinearTransition**: A convenience structure for storing a linear progression from one value to another. ``` -------------------------------- ### Get Disk Diameter Source: https://docs.rs/vek/latest/vek/geom/repr_c/struct.Disk.html?search= Gets the value of twice the radius. Requires the radius type to support Copy and Add operations. ```rust pub fn diameter(self) -> E where E: Copy + Add, ``` -------------------------------- ### Convert Mat3 to Quaternion Example Source: https://docs.rs/vek/latest/src/vek/mat.rs.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Demonstrates converting a 3x3 rotation matrix back into a quaternion. Requires `vek` and `approx` crates. ```rust # extern crate vek; # #[macro_use] extern crate approx; # use vek::{Mat3, Quaternion, Vec3}; # fn main() { let (angle, axis) = (3_f32, Vec3::new(1_f32, 3., 7.)); let a = Quaternion::rotation_3d(angle, axis); let b = Quaternion::from(Mat3::rotation_3d(angle, axis)); assert_relative_eq!(a, b); # } ``` -------------------------------- ### Get Transition State (Borrowed, Clamped) Source: https://docs.rs/vek/latest/vek/transition/type.LinearTransition.html?search= Gets a reference to the transition’s current state, clamping progress to [0;1]. ```rust pub fn current<'a>(&'a self) -> T where &'a T: Lerp, Progress: Copy + Clamp + Zero + One, Gets the transition’s current state, clamping progress to [0;1]. ```