Skip to content

Commit

Permalink
Removed the dependency on the approx crate.
Browse files Browse the repository at this point in the history
This was primiarily used for unit tests. It has been replaced by a
simple `assert_approx_eq` macro and a `FloatCompare` trait defined in
the test `support` module which is implemented for all `glam` types.

Each `glam` type has had an `abs_diff_eq` method added which returns
true if the absolute difference of each element is less than or equal to
a given `max_abs_diff` value.

This is mostly sufficient for the purpose of `glam`'s tests.

Fixes #6.
  • Loading branch information
bitshifter committed Oct 14, 2019
1 parent c54b102 commit 0f8b169
Show file tree
Hide file tree
Showing 26 changed files with 414 additions and 475 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ rust:
# cache: cargo

env:
- CARGO_FEATURES="approx mint rand serde"
- CARGO_FEATURES="approx mint rand serde scalar-math"
- CARGO_FEATURES="mint rand serde"
- CARGO_FEATURES="mint rand serde scalar-math"

matrix:
allow_failures:
Expand Down
5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ maintenance = { status = "experimental" }

[features]
default = ["std"]

# approx is needed for unit tests
std = ["approx"]
std = []

# enable additional glam checks
glam-assert = []
Expand All @@ -32,7 +30,6 @@ scalar-math = []
transform-types = []

[dependencies]
approx = { version = "0.3", optional = true, default-features = false }
mint = { version = "0.5", optional = true, default-features = false }
rand = { version = "0.7", optional = true, default-features = false }
serde = { version = "1.0", optional = true, features = ["derive"] }
Expand Down
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ one element it is more efficient to convert from tuples or arrays:
let (x, y, z) = v.into();
```

### Default features
### Optional features

* `approx` - implementations of the `AbsDiffEq` and `UlpsEq` traits for all
`glam` types. This is primarily used for unit testing.
* `mint` - for interoperating with other 3D math libraries
* `rand` - implementations of `Distribution` trait for all `glam` types. This
is primarily used for unit testing.
Expand Down Expand Up @@ -94,7 +92,7 @@ performance.

* Only single precision floating point (`f32`) arithmetic is supported
* No traits or generics for simplicity of implementation and usage
* All dependencies are optional (e.g. approx, rand and serde)
* All dependencies are optional (e.g. `mint`, `rand` and `serde`)
* Follows the [Rust API Guidelines] where possible
* Aiming for 100% test [coverage][coveralls.io]
* Common functionality is benchmarked using [Criterion.rs]
Expand Down
280 changes: 0 additions & 280 deletions src/f32/glam_approx.rs

This file was deleted.

14 changes: 14 additions & 0 deletions src/f32/mat2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,20 @@ impl Mat2 {
let s = Vec4::splat(rhs);
Mat2(self.0 * s)
}

/// Returns true if the absolute difference of all elements between `self`
/// and `rhs` is less than or equal to `max_abs_diff`.
///
/// This can be used to compare if two `Mat2`'s contain similar elements. It
/// works best when comparing with a known value. The `max_abs_diff` that
/// should be used used depends on the values being compared against.
///
/// For more on floating point comparisons see
/// https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
#[inline]
pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f32) -> bool {
self.0.abs_diff_eq(rhs.0, max_abs_diff)
}
}

#[cfg(feature = "rand")]
Expand Down
16 changes: 16 additions & 0 deletions src/f32/mat3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,22 @@ impl Mat3 {
// TODO: optimise
self.mul_vec3(rhs.extend(0.0)).truncate()
}

/// Returns true if the absolute difference of all elements between `self`
/// and `rhs` is less than or equal to `max_abs_diff`.
///
/// This can be used to compare if two `Mat3`'s contain similar elements. It
/// works best when comparing with a known value. The `max_abs_diff` that
/// should be used used depends on the values being compared against.
///
/// For more on floating point comparisons see
/// https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
#[inline]
pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f32) -> bool {
self.x_axis.abs_diff_eq(rhs.x_axis, max_abs_diff)
&& self.y_axis.abs_diff_eq(rhs.y_axis, max_abs_diff)
&& self.z_axis.abs_diff_eq(rhs.z_axis, max_abs_diff)
}
}

#[cfg(feature = "rand")]
Expand Down
17 changes: 17 additions & 0 deletions src/f32/mat4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,23 @@ impl Mat4 {
// rhs w = 0
res
}

/// Returns true if the absolute difference of all elements between `self`
/// and `rhs` is less than or equal to `max_abs_diff`.
///
/// This can be used to compare if two `Mat4`'s contain similar elements. It
/// works best when comparing with a known value. The `max_abs_diff` that
/// should be used used depends on the values being compared against.
///
/// For more on floating point comparisons see
/// https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
#[inline]
pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f32) -> bool {
self.x_axis.abs_diff_eq(rhs.x_axis, max_abs_diff)
&& self.y_axis.abs_diff_eq(rhs.y_axis, max_abs_diff)
&& self.z_axis.abs_diff_eq(rhs.z_axis, max_abs_diff)
&& self.w_axis.abs_diff_eq(rhs.w_axis, max_abs_diff)
}
}

#[cfg(feature = "rand")]
Expand Down
Loading

0 comments on commit 0f8b169

Please sign in to comment.