Skip to content

Commit 6cb37a8

Browse files
authored
Merge pull request #1035 from godot-rust/qol/basis-method-names
Rename `Basis` + `Quaternion` methods, closer to Godot
2 parents a2d4861 + 1e25834 commit 6cb37a8

File tree

6 files changed

+87
-33
lines changed

6 files changed

+87
-33
lines changed

godot-core/src/builtin/basis.rs

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,15 @@ impl Basis {
124124
/// Create a `Basis` from a `Quaternion`.
125125
///
126126
/// _Godot equivalent: `Basis(Quaternion from)`_
127-
pub fn from_quat(quat: Quaternion) -> Self {
127+
pub fn from_quaternion(quat: Quaternion) -> Self {
128128
RMat3::from_quat(quat.to_glam()).to_front()
129129
}
130130

131+
#[deprecated = "Renamed to `from_quaternion()`"]
132+
pub fn from_quat(quat: Quaternion) -> Self {
133+
Self::from_quaternion(quat)
134+
}
135+
131136
/// Create a `Basis` from three angles `a`, `b`, and `c` interpreted
132137
/// as Euler angles according to the given `EulerOrder`.
133138
///
@@ -162,23 +167,20 @@ impl Basis {
162167
/// (implies +X is right).
163168
///
164169
/// _Godot equivalent: `Basis.looking_at()`_
165-
pub fn new_looking_at(target: Vector3, up: Vector3, use_model_front: bool) -> Self {
170+
pub fn looking_at(target: Vector3, up: Vector3, use_model_front: bool) -> Self {
166171
super::inner::InnerBasis::looking_at(target, up, use_model_front)
167172
}
168173

174+
#[deprecated = "Renamed to `looking_at()`"]
175+
pub fn new_looking_at(target: Vector3, up: Vector3, use_model_front: bool) -> Self {
176+
Self::looking_at(target, up, use_model_front)
177+
}
178+
169179
/// Creates a `[Vector3; 3]` with the columns of the `Basis`.
170180
pub fn to_cols(&self) -> [Vector3; 3] {
171181
self.transposed().rows
172182
}
173183

174-
/// Creates a [`Quaternion`] representing the same rotation as this basis.
175-
///
176-
/// _Godot equivalent: `Basis.get_rotation_quaternion()`_
177-
#[doc(alias = "get_rotation_quaternion")]
178-
pub fn to_quat(&self) -> Quaternion {
179-
RQuat::from_mat3(&self.orthonormalized().to_glam()).to_front()
180-
}
181-
182184
const fn to_rows_array(self) -> [real; 9] {
183185
let [Vector3 {
184186
x: ax,
@@ -196,11 +198,24 @@ impl Basis {
196198
[ax, bx, cx, ay, by, cy, az, bz, cz]
197199
}
198200

201+
/// Creates a [`Quaternion`] representing the same rotation as this basis.
202+
///
203+
/// _Godot equivalent: `Basis.get_rotation_quaternion()`_
204+
#[doc(alias = "get_rotation_quaternion")]
205+
pub fn get_quaternion(&self) -> Quaternion {
206+
RQuat::from_mat3(&self.orthonormalized().to_glam()).to_front()
207+
}
208+
209+
#[deprecated = "Renamed to `get_quaternion()`"]
210+
pub fn to_quat(&self) -> Quaternion {
211+
self.get_quaternion()
212+
}
213+
199214
/// Returns the scale of the matrix.
200215
///
201216
/// _Godot equivalent: `Basis.get_scale()`_
202217
#[must_use]
203-
pub fn scale(&self) -> Vector3 {
218+
pub fn get_scale(&self) -> Vector3 {
204219
let det = self.determinant();
205220
let det_sign = if det < 0.0 { -1.0 } else { 1.0 };
206221

@@ -211,12 +226,24 @@ impl Basis {
211226
) * det_sign
212227
}
213228

229+
#[deprecated = "Renamed to `get_scale()`"]
230+
pub fn scale(&self) -> Vector3 {
231+
self.get_scale()
232+
}
233+
234+
/// Returns the rotation of the matrix in euler angles, with the order `YXZ`.
235+
///
236+
/// See [`get_euler_with()`](Self::get_euler_with) for custom angle orders.
237+
pub fn get_euler(&self) -> Vector3 {
238+
self.get_euler_with(EulerOrder::YXZ)
239+
}
240+
214241
/// Returns the rotation of the matrix in euler angles.
215242
///
216-
/// The order of the angles are given by `order`.
243+
/// The order of the angles are given by `order`. To use the default order `YXZ`, see [`get_euler()`](Self::get_euler).
217244
///
218245
/// _Godot equivalent: `Basis.get_euler()`_
219-
pub fn to_euler(&self, order: EulerOrder) -> Vector3 {
246+
pub fn get_euler_with(&self, order: EulerOrder) -> Vector3 {
220247
use glam::swizzles::Vec3Swizzles as _;
221248

222249
let col_a = self.col_a().to_glam();
@@ -276,6 +303,11 @@ impl Basis {
276303
.to_front()
277304
}
278305

306+
#[deprecated = "Renamed to `get_euler()` + `get_euler_with()`"]
307+
pub fn to_euler(&self, order: EulerOrder) -> Vector3 {
308+
self.get_euler_with(order)
309+
}
310+
279311
fn is_between_neg1_1(f: real) -> Ordering {
280312
if f >= (1.0 - real::CMP_EPSILON) {
281313
Ordering::Greater
@@ -416,10 +448,10 @@ impl Basis {
416448
/// _Godot equivalent: `Basis.slerp()`_
417449
#[must_use]
418450
pub fn slerp(&self, other: &Self, weight: real) -> Self {
419-
let from = self.to_quat();
420-
let to = other.to_quat();
451+
let from = self.get_quaternion();
452+
let to = other.get_quaternion();
421453

422-
let mut result = Self::from_quat(from.slerp(to, weight));
454+
let mut result = Self::from_quaternion(from.slerp(to, weight));
423455

424456
for i in 0..3 {
425457
result.rows[i] *= self.rows[i].length().lerp(other.rows[i].length(), weight);
@@ -649,7 +681,7 @@ mod test {
649681
let to_rotation: Basis = Basis::from_euler(rot_order, original_euler);
650682

651683
// Euler from rotation
652-
let euler_from_rotation: Vector3 = to_rotation.to_euler(rot_order);
684+
let euler_from_rotation: Vector3 = to_rotation.get_euler_with(rot_order);
653685
let rotation_from_computed_euler: Basis = Basis::from_euler(rot_order, euler_from_rotation);
654686

655687
let res: Basis = to_rotation.inverse() * rotation_from_computed_euler;
@@ -670,7 +702,7 @@ mod test {
670702
);
671703

672704
// Double check `to_rotation` decomposing with XYZ rotation order.
673-
let euler_xyz_from_rotation: Vector3 = to_rotation.to_euler(EulerOrder::XYZ);
705+
let euler_xyz_from_rotation: Vector3 = to_rotation.get_euler_with(EulerOrder::XYZ);
674706
let rotation_from_xyz_computed_euler: Basis =
675707
Basis::from_euler(EulerOrder::XYZ, euler_xyz_from_rotation);
676708

godot-core/src/builtin/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
//! ways to achieve the same has downsides: users wonder if a subtle difference exists, or if all options are in fact identical.
5353
//! It's unclear which one is the "preferred" option. Recognizing other people's code becomes harder, because there tend to be dialects.
5454
//! 2. It's often a purely stylistic choice, without functional benefits. Someone may want to write `(1, 2).into()` instead of
55-
//! `Vector2::new(1, 2)`. This is not strong enough of a reason -- if brevity is of concern, a function `vec2(1, 2)` does the job better.
55+
//! `Vector2i::new(1, 2)`. This is not strong enough of a reason -- if brevity is of concern, a function `vec2i(1, 2)` does the job better.
5656
//! 3. `From` is less explicit than a named conversion function. If you see `string.to_variant()` or `color.to_hsv()`, you immediately
5757
//! know the target type. `string.into()` and `color.into()` lose that aspect. Even with `(1, 2).into()`, you'd first have to check whether
5858
//! `From` is only converting the tuple, or if it _also_ provides an `i32`-to-`f32` cast, thus resulting in `Vector2` instead of `Vector2i`.
@@ -62,8 +62,8 @@
6262
//! Temporarily commenting out such non-local code breaks the declaration line, too. To make matters worse, turbofish `.into::<Type>()` isn't
6363
//! possible either.
6464
//! 5. Rust itself [requires](https://doc.rust-lang.org/std/convert/trait.From.html#when-to-implement-from) that `From` conversions are
65-
//! infallible, lossless, value-preserving and obvious. This rules out a lot of scenarios such as `Basis::to_quaternion()` (which only maintains
66-
//! the rotation part, not scale) or `Color::try_to_hsv()` (which is fallible and lossy).
65+
//! infallible, lossless, value-preserving and obvious. This rules out a lot of scenarios such as `DynGd::to_gd()` (which only maintains
66+
//! the class part, not trait) or `Color::try_to_hsv()` (which is fallible and lossy).
6767
//!
6868
//! One main reason to support `From` is to allow generic programming, in particular `impl Into<T>` parameters. This is also the reason
6969
//! why the string types have historically implemented the trait. But this became less relevant with the advent of

godot-core/src/builtin/quaternion.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl Quaternion {
107107
}
108108
}
109109

110-
#[deprecated = "Moved to `Quaternion::exp()`"]
110+
#[deprecated = "Renamed to `Quaternion::exp()`"]
111111
pub fn to_exp(self) -> Self {
112112
self.exp()
113113
}
@@ -147,9 +147,25 @@ impl Quaternion {
147147
}
148148
}
149149

150-
#[doc(alias = "get_euler")]
150+
/// Returns the rotation of the matrix in euler angles, with the order `YXZ`.
151+
///
152+
/// See [`get_euler_with()`](Self::get_euler_with) for custom angle orders.
153+
pub fn get_euler(self) -> Vector3 {
154+
self.get_euler_with(EulerOrder::YXZ)
155+
}
156+
157+
/// Returns the rotation of the matrix in euler angles.
158+
///
159+
/// The order of the angles are given by `order`. To use the default order `YXZ`, see [`get_euler()`](Self::get_euler).
160+
///
161+
/// _Godot equivalent: `Quaternion.get_euler()`_
162+
pub fn get_euler_with(self, order: EulerOrder) -> Vector3 {
163+
Basis::from_quaternion(self).get_euler_with(order)
164+
}
165+
166+
#[deprecated = "Renamed to `get_euler()` + `get_euler_with()`"]
151167
pub fn to_euler(self, order: EulerOrder) -> Vector3 {
152-
Basis::from_quat(self).to_euler(order)
168+
self.get_euler_with(order)
153169
}
154170

155171
pub fn inverse(self) -> Self {

godot-core/src/builtin/transform3d.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,16 +137,16 @@ impl Transform3D {
137137
/// a given weight (on the range of 0.0 to 1.0).
138138
#[must_use]
139139
pub fn interpolate_with(&self, other: &Self, weight: real) -> Self {
140-
let src_scale = self.basis.scale();
141-
let src_rot = self.basis.to_quat().normalized();
140+
let src_scale = self.basis.get_scale();
141+
let src_rot = self.basis.get_quaternion().normalized();
142142
let src_loc = self.origin;
143143

144-
let dst_scale = other.basis.scale();
145-
let dst_rot = other.basis.to_quat().normalized();
144+
let dst_scale = other.basis.get_scale();
145+
let dst_rot = other.basis.get_quaternion().normalized();
146146
let dst_loc = other.origin;
147147

148148
let mut basis = Basis::from_scale(src_scale.lerp(dst_scale, weight));
149-
basis = Basis::from_quat(src_rot.slerp(dst_rot, weight)) * basis;
149+
basis = Basis::from_quaternion(src_rot.slerp(dst_rot, weight)) * basis;
150150

151151
Self {
152152
basis,
@@ -163,7 +163,7 @@ impl Transform3D {
163163
#[must_use]
164164
pub fn looking_at(&self, target: Vector3, up: Vector3, use_model_front: bool) -> Self {
165165
Self {
166-
basis: Basis::new_looking_at(target - self.origin, up, use_model_front),
166+
basis: Basis::looking_at(target - self.origin, up, use_model_front),
167167
origin: self.origin,
168168
}
169169
}

godot-core/src/obj/guards.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ pub struct DynGdRef<'a, D: ?Sized> {
105105
}
106106

107107
impl<'a, D: ?Sized> DynGdRef<'a, D> {
108+
#[doc(hidden)]
108109
pub fn from_guard<T: AsDyn<D>>(guard: GdRef<'a, T>) -> Self {
109110
let obj = &*guard;
110111
let dyn_obj = obj.dyn_upcast();
@@ -147,6 +148,7 @@ pub struct DynGdMut<'a, D: ?Sized> {
147148
}
148149

149150
impl<'a, D: ?Sized> DynGdMut<'a, D> {
151+
#[doc(hidden)]
150152
pub fn from_guard<T: AsDyn<D>>(mut guard: GdMut<'a, T>) -> Self {
151153
let obj = &mut *guard;
152154
let dyn_obj = obj.dyn_upcast_mut();

itest/rust/src/builtin_tests/geometry/basis_test.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,19 @@ fn basis_equiv() {
144144
assert_eq_approx!(real::from_f64(inner), outer, "function: {name}\n");
145145
}
146146

147-
assert_eq_approx!(inner.get_scale(), outer.scale(), "function: get_scale\n");
147+
assert_eq_approx!(
148+
inner.get_scale(),
149+
outer.get_scale(),
150+
"function: get_scale\n"
151+
);
148152
assert_eq_approx!(
149153
inner.get_euler(EulerOrder::XYZ as i64),
150-
outer.to_euler(EulerOrder::XYZ),
154+
outer.get_euler_with(EulerOrder::XYZ),
151155
"function: get_euler\n"
152156
);
153157
assert_eq_approx!(
154158
inner.get_rotation_quaternion(),
155-
outer.to_quat(),
159+
outer.get_quaternion(),
156160
"function: get_rotation_quaternion\n"
157161
)
158162
}

0 commit comments

Comments
 (0)