Skip to content

Commit

Permalink
Added glam_assert! to check preconditions.
Browse files Browse the repository at this point in the history
Is enabled by default in debug and can be enabled in other profiles
using the `glam-assert` feature or the `debug-assertions` property in
`Cargo.toml`. This means glam assertions can be enabed independently of
debug assertions.

Switched current debug_assert usage to use glam_assert.

Made matrix inverse determinant != 0 asserts consistent between matrix
types.
  • Loading branch information
bitshifter committed Jul 5, 2019
1 parent bc15fde commit 8c86ebf
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 17 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ default = ["std"]
# approx is needed for unit tests, rand is needed for benchmarking
std = ["approx", "rand"]

# enable additional glam checks
glam-assert = []

# this is primarily for testing the fallback implementation
scalar-math = []

Expand Down
2 changes: 1 addition & 1 deletion src/f32/mat2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl Mat2 {
// TODO: SSE2
let (a, b, c, d) = self.0.into();
let det = a * d - b * c;
debug_assert!(det != 0.0);
glam_assert!(det != 0.0);
let tmp = Vec4::new(1.0, -1.0, -1.0, 1.0) / det;
Self(Vec4::new(d, b, c, a) * tmp)
}
Expand Down
6 changes: 4 additions & 2 deletions src/f32/mat3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn mat3(x_axis: Vec3, y_axis: Vec3, z_axis: Vec3) -> Mat3 {

#[inline]
fn quat_to_axes(rotation: Quat) -> (Vec3, Vec3, Vec3) {
debug_assert!(rotation.is_normalized());
glam_assert!(rotation.is_normalized());
let (x, y, z, w) = rotation.into();
let x2 = x + x;
let y2 = y + y;
Expand Down Expand Up @@ -225,7 +225,9 @@ impl Mat3 {
let tmp0 = self.y_axis.cross(self.z_axis);
let tmp1 = self.z_axis.cross(self.x_axis);
let tmp2 = self.x_axis.cross(self.y_axis);
let inv_det = Vec3::splat(1.0 / self.z_axis().dot(tmp2));
let det = self.z_axis().dot(tmp2);
glam_assert!(det != 0.0);
let inv_det = Vec3::splat(1.0 / det);
Mat3::new(tmp0 * inv_det, tmp1 * inv_det, tmp2 * inv_det).transpose()
}

Expand Down
20 changes: 11 additions & 9 deletions src/f32/mat4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn mat4(x_axis: Vec4, y_axis: Vec4, z_axis: Vec4, w_axis: Vec4) -> Mat4 {

#[inline]
fn quat_to_axes(rotation: Quat) -> (Vec4, Vec4, Vec4) {
debug_assert!(rotation.is_normalized());
glam_assert!(rotation.is_normalized());
let (x, y, z, w) = rotation.into();
let x2 = x + x;
let y2 = y + y;
Expand Down Expand Up @@ -373,6 +373,8 @@ impl Mat4 {
let dot0 = self.x_axis * col0;
let dot1 = dot0.x() + dot0.y() + dot0.z() + dot0.w();

glam_assert!(dot1 != 0.0);

let rcp_det = 1.0 / dot1;
inverse * rcp_det
}
Expand Down Expand Up @@ -424,10 +426,10 @@ impl Mat4 {

// #[inline]
// pub fn perspective_fov_lh(fovy: Angle, aspect: f32, nearz: f32, farz: f32) -> Self {
// debug_assert!(nearz > 0.0 && farz > 0.0);
// debug_assert!(fovy != Angle::from_radians(0.0));
// debug_assert!(aspect != 0.0);
// debug_assert!(farz != nearz);
// glam_assert!(nearz > 0.0 && farz > 0.0);
// glam_assert!(fovy != Angle::from_radians(0.0));
// glam_assert!(aspect != 0.0);
// glam_assert!(farz != nearz);

// let (sin_fov, cos_fov) = (0.5 * fovy).sin_cos();
// let height = cos_fov / sin_fov;
Expand All @@ -444,10 +446,10 @@ impl Mat4 {

// #[inline]
// pub fn perspective_fov_rh(fovy: Angle, aspect: f32, nearz: f32, farz: f32) -> Self {
// debug_assert!(nearz > 0.0 && farz > 0.0);
// debug_assert!(fovy != Angle::from_radians(0.0));
// debug_assert!(aspect != 0.0);
// debug_assert!(farz != nearz);
// glam_assert!(nearz > 0.0 && farz > 0.0);
// glam_assert!(fovy != Angle::from_radians(0.0));
// glam_assert!(aspect != 0.0);
// glam_assert!(farz != nearz);

// let (sin_fov, cos_fov) = (0.5 * fovy).sin_cos();
// let height = cos_fov / sin_fov;
Expand Down
5 changes: 2 additions & 3 deletions src/f32/quat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl Quat {
/// Create quaterion for a normalized rotation axis and angle.
/// The axis must be normalized.
pub fn from_axis_angle(axis: Vec3, angle: Angle) -> Self {
debug_assert!((axis.length_squared() - 1.0).abs() < 0.01);
glam_assert!(axis.is_normalized());
let (s, c) = (angle * 0.5).sin_cos();
(axis * s).extend(c).into()
}
Expand Down Expand Up @@ -177,8 +177,7 @@ impl Quat {

#[inline]
pub fn is_normalized(self) -> bool {
const THRESHOLD: f32 = 0.00001;
(self.length_squared() - 1.0).abs() < THRESHOLD
is_normalized!(self)
}

#[inline]
Expand Down
4 changes: 2 additions & 2 deletions src/f32/quat_sse2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Quat {
let q = unsafe { Self(_mm_set_ps(w, z, y, x)) };
// might be better as a warning - if this asserts during deserialization it's really
// obtuse.
// debug_assert!(q.is_normalized());
// glam_assert!(q.is_normalized());
q
}

Expand All @@ -57,7 +57,7 @@ impl Quat {
pub fn from_slice_unaligned(slice: &[f32]) -> Self {
assert!(slice.len() >= 4);
let q = unsafe { Self(_mm_loadu_ps(slice.as_ptr())) };
debug_assert!(q.is_normalized());
glam_assert!(q.is_normalized());
q
}

Expand Down
5 changes: 5 additions & 0 deletions src/f32/vec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ impl Vec2 {
self + ((rhs - self) * s)
}

#[inline]
pub fn is_normalized(self) -> bool {
is_normalized!(self)
}

#[inline]
pub(crate) fn mul_add(self, a: Self, b: Self) -> Self {
Self((self.0 * a.0) + b.0, (self.1 * a.1) + b.1)
Expand Down
5 changes: 5 additions & 0 deletions src/f32/vec3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ impl Vec3 {
pub fn lerp(self, rhs: Self, s: f32) -> Self {
self + ((rhs - self) * s)
}

#[inline]
pub fn is_normalized(self) -> bool {
is_normalized!(self)
}
}

impl AsRef<[f32; 3]> for Vec3 {
Expand Down
5 changes: 5 additions & 0 deletions src/f32/vec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ impl Vec4 {
pub fn lerp(self, rhs: Self, s: f32) -> Self {
self + ((rhs - self) * s)
}

#[inline]
pub fn is_normalized(self) -> bool {
is_normalized!(self)
}
}

impl AsRef<[f32; 4]> for Vec4 {
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ and benchmarks.
*/
#![doc(html_root_url = "https://docs.rs/glam/0.7.1")]

#[macro_use]
mod macros;

pub mod f32;

pub use self::f32::{
Expand Down
18 changes: 18 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

#[cfg(any(debug_assertions, feature = "glam-assert"))]
macro_rules! glam_assert {
($($arg:tt)*) => ( assert!($($arg)*); )
}
#[cfg(not(any(debug_assertions, feature = "glam-assert")))]
macro_rules! glam_assert {
($($arg:tt)*) => ()
}

macro_rules! is_normalized {
($self:expr, threshold => $threshold:expr) => {
($self.length_squared() - 1.0).abs() < $threshold
};
($self:expr) => {
is_normalized!($self, threshold => 0.00001)
};
}

0 comments on commit 8c86ebf

Please sign in to comment.