Skip to content

Commit

Permalink
- Add FloatExt trait
Browse files Browse the repository at this point in the history
- Add `impl_float_vector_component_fns` macro for float-vectors
  • Loading branch information
lilizoey committed Jul 3, 2023
1 parent 565bf10 commit 7ca49a3
Show file tree
Hide file tree
Showing 13 changed files with 506 additions and 452 deletions.
16 changes: 10 additions & 6 deletions godot-core/src/builtin/basis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use godot_ffi as sys;
use sys::{ffi_methods, GodotFfi};

use crate::builtin::math::{is_equal_approx, lerp, ApproxEq, GlamConv, GlamType, CMP_EPSILON};
use crate::builtin::math::{ApproxEq, FloatExt, GlamConv, GlamType};
use crate::builtin::real_consts::FRAC_PI_2;
use crate::builtin::{real, Quaternion, RMat3, RQuat, RVec2, RVec3, Vector3};

Expand Down Expand Up @@ -271,9 +271,9 @@ impl Basis {
}

fn is_between_neg1_1(f: real) -> Ordering {
if f >= (1.0 - CMP_EPSILON) {
if f >= (1.0 - real::CMP_EPSILON) {
Ordering::Greater
} else if f <= -(1.0 - CMP_EPSILON) {
} else if f <= -(1.0 - real::CMP_EPSILON) {
Ordering::Less
} else {
Ordering::Equal
Expand Down Expand Up @@ -364,15 +364,19 @@ impl Basis {
Self::from_cols(self.rows[0], self.rows[1], self.rows[2])
}

/// Returns the orthonormalized version of the matrix (useful to call from
/// ⚠️ Returns the orthonormalized version of the matrix (useful to call from
/// time to time to avoid rounding error for orthogonal matrices). This
/// performs a Gram-Schmidt orthonormalization on the basis of the matrix.
///
/// # Panics
///
/// If the determinant of the matrix is 0.
///
/// _Godot equivalent: `Basis.orthonormalized()`_
#[must_use]
pub fn orthonormalized(self) -> Self {
assert!(
!is_equal_approx(self.determinant(), 0.0),
!self.determinant().is_zero_approx(),
"Determinant should not be zero."
);

Expand Down Expand Up @@ -411,7 +415,7 @@ impl Basis {
let mut result = Self::from_quat(from.slerp(to, weight));

for i in 0..3 {
result.rows[i] *= lerp(self.rows[i].length(), other.rows[i].length(), weight);
result.rows[i] *= self.rows[i].length().lerp(other.rows[i].length(), weight);
}

result
Expand Down
8 changes: 0 additions & 8 deletions godot-core/src/builtin/math/approx_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

use crate::builtin::real;

// TODO(bromeon): test this against Godot's own is_equal_approx() implementation for equality-comparable built-in types (excl Callable/Rid/...)

/// Approximate equality-comparison of geometric types.
Expand All @@ -19,12 +17,6 @@ pub trait ApproxEq: PartialEq {
fn approx_eq(&self, other: &Self) -> bool;
}

impl ApproxEq for real {
fn approx_eq(&self, other: &Self) -> bool {
crate::builtin::math::is_equal_approx(*self, *other)
}
}

/// Asserts that two values are approximately equal
///
/// For comparison, this uses `ApproxEq::approx_eq` by default, or the provided `fn = ...` function.
Expand Down
Loading

0 comments on commit 7ca49a3

Please sign in to comment.