Vectors, quaternions, and matrices for general purposes, and computer graphics.
Vector, matrix, and quaternion data structures and operations. Uses f32
or f64
based types. SoA SIMD support.
Fundamental types:
- Vec3
- Vec4
- Quaternion
- Mat3
- Mat4
- Vec3x8,
- Vec3x16,
- Quaternionx8,
- (etc)
Example use cases:
- Computer graphics
- Biomechanics
- Robotics and unmanned aerial vehicles
- Structural chemistry and biochemistry
- Cosmology modeling
- Various scientific and engineering applications
- Aircraft attitude systems and autopilots
Vector and Quaternion types are copy.
For Compatibility with no_std targets, e.g. embedded, disable default features, and enable the no_std
feature. This omits SIMD,
std::fmt::Display
implementations, and enables num_traits's libm
capabilities
for certain operations. lin_alg = { version = "^1.1.3", default-features = false, features = ["no_std"] }
For computer-graphics functionality (e.g. specialty matrix constructors, and [de]serialization to byte arrays for passing to and from GPUs), use the computer_graphics
feature. For bincode binary encoding and decoding, use the encode
feature.
For information on practical quaternion operations: Quaternions: A practical guide.
The From
trait is implemented for most types, for converting between f32
and f64
variants using the into()
syntax.
SIMD
Includes SIMD constructs with structure of array (SoA) layout, for Vec and Quaternion types. For example: Vec3x8
, f64::Vec3x4
, Vec4x8
, and Quaternionx8
.
These allow you to perform parallel operations on vectors and quaternions (e.g. 4, 8, or 16 at a time) for a similar computation cost as a single one.
They are currently configured with 256-bit wide (AVX) values, performing operations on 8 f32
types, or 4 f64
types. See the examples below for details.
This library exposes an f32x8
SIMD type that wraps __m256
with appropriate constructors, operator overloads etc, and similar. It's used
internally by the SIMD Vec and Quaternion types. This,
and the Vec3x8
, Quaternionx8
etc APIs, mimic the nightly core::simd library. They're used internally by our SIMD vector and quaternion types.
It also includes f64x4
. We are waiting to add 512-bit wide types until their operations are in stable rust. Hopefully soon!
We use these custom SIMD types vice core::simd
so this library will work on stable rust. We'll remove these when core::simd
is stable.
This lib also includes pack
and unpack
utility functions for converting slices of f32
, Vec3
, Quaternion
etc between
SIMD and normal (scalar) values. This handles padding the last chunk, since input data may not align evenly in chunks of 4, 8, or 16.
These include pack_vec3
, unpack_quaternion
, pack_float
etc.
It also includes a generic pack_slice
for packing Copy
type items into arrays, generally for use with SIMD types.
Note: This approach is the opposite of the array of structures (AoS) approach to SIMD used by the glam
and cgmath
libraries.
CUDA (GPU)
This library includes two helper functions for use with the cudarc
library; these are to allocate Vec3
and Quaternion
types. (f32 and f64). They perform host-to-device copies. This is intended to simply application code. To enable this, select
the cuda
feature in Cargo.toml
A note on performance
For performance-sensitive operations, depending on the details of your computation and hardware, you may wish to use a mix of GPU (CUDA, or graphics shaders), parallelization via threads (e.g. Rayon), and SIMD operations. This library aims to assist in these operations, and leaves details to the application.
Examples
See the official documentation (Linked above) for details. Below is a brief, impractical syntax overview:
use TAU;
use ;
If using for computer graphics, this functionality may be helpful:
let a = new;
let bytes = a.to_bytes; // Send this to the GPU. `Quaternion` and `MatN` have similar methods.
let model_mat = new_translation
* self.orientation.to_matrix
* new_scaler_partial;
let proj_mat = new_perspective_lh;
let view_mat = self.orientation.inverse.to_matrix * new_translation;
// Example of rolling a camera around the forward axis:
let fwd = orientation.rotate_vec;
let rotation = from_axis_angle;
orientation = rotation * orientation;
A practical geometry example:
/// Calculate the dihedral angle between 4 positions (3 bonds).
/// The `bonds` are one atom's position, substracted from the next. Order matters.
A SIMD example of vector operations:
use f32:;
// Non-SIMD Vec3s we'll start with.
let vec_a = new;
let vec_b = new;
// An example where we copy the same Vec3 into all 8 slots. In most practical uses,
// each slot will contain a different value.
let a = from_array;
let b = from_array;
// Perform vector addition on 8 Vec3s at once.
let c = a + b;
// Create a [Vec3; 8].
let d = a.cross.to_array;
// Create a `f32x8`, then convert to an array.
let dot_result = a.dot.to_array;
let e = vec_a * 3.;
let f = vec_a * from_array;
let g = vec_a * splat
A SIMD example of rotating vectors.
use TAU;
use ;
let rot_init = ;
let rotation = from_array;
// This could be 8 separate values.
let vec = from_array;
let result = rotation.rotate_vec.to_array;
let sqrt_2_div_2 = 2_f32.sqrt/2.;
let angled = new;
assert!;
assert!;
assert!;
assert!;
assert!;
assert!;
assert!;
assert!;
An example function using SIMD for a practical use, integrating Vec3x8s
with SIMD types directly.
use ;
// ...
This example shows a few different SIMD operations. It mixes Vec3s, floating point types, and arrays of non-numerical variables, synchronized.
Another example of SIMD packing syntax. It works similarly for f32/f64, and quaternions. (From our unit tests):