Skip to content

Commit 79e1850

Browse files
committed
PR adjustments; rename/deprecate Basis.inverted() -> inverse()
1 parent 3436ed7 commit 79e1850

File tree

2 files changed

+55
-29
lines changed

2 files changed

+55
-29
lines changed

gdnative-core/src/core_types/geom/basis.rs

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::core_types::{IsEqualApprox, Quat, Vector3};
2-
use core::ops::Mul;
2+
use crate::globalscope::lerp;
33
use glam::Mat3;
4+
use std::ops::Mul;
45

56
/// A 3x3 matrix, typically used as an orthogonal basis for [`Transform`][crate::core_types::Transform].
67
///
@@ -241,37 +242,31 @@ impl Basis {
241242
///
242243
/// If the determinant of `self` is zero.
243244
#[inline]
244-
pub fn inverted(&self) -> Self {
245+
pub fn inverse(&self) -> Self {
245246
let mut copy = *self;
246247
copy.invert();
247248
copy
248249
}
249250

251+
#[inline]
252+
#[deprecated = "Use `inverse` instead."]
253+
pub fn inverted(&self) -> Self {
254+
self.inverse()
255+
}
256+
250257
/// Returns linear interpolation on a sphere between two basis by weight amount (on the range of 0.0 to 1.0).
251258
#[inline]
252259
pub fn slerp(&self, other: &Basis, weight: f32) -> Self {
253-
#[inline]
254-
fn lerp_f(from: f32, to: f32, weight: f32) -> f32 {
255-
from + (to - from) * weight
256-
}
257260
let from = self.to_quat();
258261
let to = other.to_quat();
259262
let mut result = Basis::from_quat(from.slerp(to, weight));
260-
result.elements[0] *= lerp_f(
261-
self.elements[0].length(),
262-
other.elements[0].length(),
263-
weight,
264-
);
265-
result.elements[1] *= lerp_f(
266-
self.elements[1].length(),
267-
other.elements[1].length(),
268-
weight,
269-
);
270-
result.elements[2] *= lerp_f(
271-
self.elements[2].length(),
272-
other.elements[2].length(),
273-
weight,
274-
);
263+
264+
for i in 0..3 {
265+
result.elements[i] *= lerp(
266+
self.elements[i].length()..=other.elements[i].length(),
267+
weight,
268+
);
269+
}
275270

276271
result
277272
}
@@ -350,7 +345,7 @@ impl Basis {
350345
}
351346

352347
#[inline]
353-
pub fn orthogonalize(&mut self) {
348+
fn orthogonalize(&mut self) {
354349
let scale = self.scale();
355350
self.orthonormalize();
356351
self.scale_local(scale);
@@ -855,6 +850,6 @@ mod tests {
855850
Vector3::new(-0.165055, 0.94041, -0.297299),
856851
Vector3::new(0.98324, 0.180557, 0.025257),
857852
);
858-
assert!(expected.is_equal_approx(&b.inverted()));
853+
assert!(expected.is_equal_approx(&b.inverse()));
859854
}
860855
}

gdnative-core/src/core_types/geom/transform.rs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@ use std::ops::Mul;
22

33
use crate::core_types::{Basis, Vector3};
44

5+
// Note regarding naming of interpolation: there are 3 namings in Godot.
6+
// * `lerp` + `slerp` for simple types
7+
// * `linear_interpolate` for vectors and colors
8+
// (now renamed to [`lerp`](https://docs.godotengine.org/en/latest/classes/class_vector3.html?highlight=vector3#class-vector3-method-lerp) + `slerp`)
9+
// * `Vector3` also has `cubic_interpolate` and `bezier_interpolate`, which might explain the origins
10+
// * `interpolate_with` for transforms; in Godot 4 also
11+
// [`sphere_interpolate_with`](https://docs.godotengine.org/en/latest/classes/class_transform3d.html#class-transform3d-method-sphere-interpolate-with)
12+
//
13+
// We currently also have `Transform2D::interpolate_with()`.
14+
// In an ideal world, all those would be called `lerp` and `slerp`.
15+
516
/// Affine 3D transform (3x4 matrix).
617
///
718
/// Used for 3D linear transformations. Uses a basis + origin representation.
@@ -89,7 +100,7 @@ impl Transform {
89100
/// Returns this transform, with its origin moved by a certain `translation`
90101
#[deprecated = "`translated` is not relative to the transform's coordinate system \
91102
and thus inconsistent with GDScript. Please use translated_global() instead. \
92-
This method will be renamed to translated_local in gdnative 0.11."]
103+
This method will be renamed to translated_local in gdnative 0.12."]
93104
#[inline]
94105
pub fn translated(&self, translation: Vector3) -> Self {
95106
Self {
@@ -119,17 +130,18 @@ impl Transform {
119130
/// affine_inverse for transforms with scaling).
120131
#[inline]
121132
pub fn inverse(&self) -> Self {
133+
let basis = self.basis.transposed();
122134
Transform {
123-
basis: self.basis.transposed(),
124-
origin: self.basis.xform(-self.origin),
135+
basis,
136+
origin: basis.xform(-self.origin),
125137
}
126138
}
127139

128140
/// Returns the inverse of the transform, under the assumption that the
129141
/// transformation is composed of rotation, scaling and translation.
130142
#[inline]
131143
pub fn affine_inverse(&self) -> Self {
132-
let basis_inv = self.basis.inverted();
144+
let basis_inv = self.basis.inverse();
133145
let origin_inv = basis_inv.xform(-self.origin);
134146

135147
Self {
@@ -150,12 +162,14 @@ impl Transform {
150162
} * (*self)
151163
}
152164

165+
/*
153166
/// Returns the rotated transform around the given axis by the given angle (in radians),
154167
/// using matrix multiplication. The axis must be a normalized vector.
155168
#[inline]
156169
fn rotate(&mut self, axis: Vector3, phi: f32) {
157170
*self = self.rotated(axis, phi);
158171
}
172+
*/
159173

160174
/// Returns a copy of the transform rotated such that its -Z axis points
161175
/// towards the target position.
@@ -189,7 +203,7 @@ impl Transform {
189203
/// In-place translates the transform by the given offset, relative to
190204
/// the transform's basis vectors.
191205
#[inline]
192-
fn translate_withbasis(&mut self, translation: Vector3) {
206+
fn translate_global(&mut self, translation: Vector3) {
193207
// Note: Godot source uses origin + basis dot translation,
194208
// but self.translate() uses only origin + translation
195209
self.origin.x += self.basis.elements[0].dot(translation);
@@ -204,7 +218,7 @@ impl Transform {
204218
#[inline]
205219
pub fn translated_global(&self, translation: Vector3) -> Self {
206220
let mut copy = *self;
207-
copy.translate_withbasis(translation);
221+
copy.translate_global(translation);
208222
copy
209223
}
210224

@@ -356,6 +370,23 @@ mod tests {
356370
assert!(expected.is_equal_approx(&t))
357371
}
358372

373+
/*
374+
#[test]
375+
fn inverse_is_sane() {
376+
let t = test_inputs().0.inverse();
377+
let expected = Transform::from_basis_origin(
378+
// Fix values
379+
Vector3::new(0.309725, -0.66022015, 3.9329607),
380+
Vector3::new(-0.57629496, 1.8808193, 0.3611141),
381+
Vector3::new(-0.47722515, -0.14864945, 0.012628445),
382+
Vector3::new(-0.7398631, 0.0425314, 0.03682696),
383+
);
384+
385+
println!("TF: {t:?}");
386+
assert!(expected.is_equal_approx(&t))
387+
}
388+
*/
389+
359390
#[test]
360391
fn orthonormalization_is_sane() {
361392
// Godot reports:

0 commit comments

Comments
 (0)