gfxmath-vec3
A simple 3D math library to compute 3 dimensional vectors.
Usage
Vec3
Vec3 uses the standard { x, y, z } coordinate system. We can obtain the coefficients for each axis by calling its member.
The structure of Vec3 is as follows:
The creation of a Vec3 objection can be done via multiple ways.
use Vec3;
// Standard construction
let v = Vec3 ;
// Using `new` function
let v = new;
// Using tuples
let v: = .into;
// Using macros
use vec3;
let v = vec3!;
assert_eq!;
assert_eq!;
assert_eq!;
Operators
Common mathematical operators are implemented for Vec3.
+
,+=
-
,-=
*
,*=
/
,/=
// Reminder: Rust automatically infers floats to `f64` and integers to `i64`.
let v1 = new;
let v2 = new;
let res = v1 + v2;
assert_eq!;
assert_eq!;
assert_eq!;
Cross Product
use Cross;
use Vec3;
let a = new;
let b = all;
let res = a.cross;
assert_eq!;
assert_eq!;
assert_eq!;
Dot Product
use ;
let a = new;
let b = new;
let res = a.dot;
assert_eq!;
Normalize
Produce a unit vector.
use Norm;
use Vec3;
let a = new;
let an = a.norm.unwrap;
assert_eq!;
assert_eq!;
assert_eq!;
Known Limitations
Left-Hand Primitives
One caveat is that operators where the Left-Hand side is a primitive has limited support. This is due to restrictions for trait implementations. impl <T> Add<Vec3<T>> for T
is illegal within Rust syntax (due to the trailing T
) because the implementation must be for a known type for non-local traits and types. Since Add
is from the core
package and T
(the type to be implemented for) is not derived from a local trait, this is not possible.
At the time of this writing, primitives that work on the Left-Hand Side for common operators are f32
, f64
, i32
, and i64
.
// Works
let lhs: f32 = 4.0;
let rhs = new;
let res = lhs + rhs;
// NOT SUPPORTED!!
let lhs: u32 = 4;
let rhs = new;
let res = lhs + rhs;
Hash Implementation
Currently, Hash implementations are limited for the following types:
f32
f64
i32
i64
u32
u64
Showcase
License
Apache 2.0