Skip to content

Commit e93fa52

Browse files
authored
Merge pull request #751 from joriskleiber/consistent-parameters-geometric-types
Fix: Consistent by-value and by-ref parameters for geometric types
2 parents aecab4a + 56a4a35 commit e93fa52

File tree

11 files changed

+153
-152
lines changed

11 files changed

+153
-152
lines changed

godot-core/src/builtin/aabb.rs

+46-46
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl Aabb {
4545

4646
/// Returns an AABB with the same geometry, with most-negative corner as `position` and non-negative `size`.
4747
#[inline]
48-
pub fn abs(&self) -> Self {
48+
pub fn abs(self) -> Self {
4949
Aabb {
5050
position: self.position + self.size.coord_min(Vector3::ZERO),
5151
size: self.size.abs(),
@@ -54,7 +54,7 @@ impl Aabb {
5454

5555
/// Whether `self` covers at least the entire area of `b` (and possibly more).
5656
#[inline]
57-
pub fn encloses(&self, b: Aabb) -> bool {
57+
pub fn encloses(self, b: Aabb) -> bool {
5858
let end = self.end();
5959
let b_end = b.end();
6060

@@ -71,16 +71,16 @@ impl Aabb {
7171
/// # Panics
7272
/// If `self.size` is negative.
7373
#[inline]
74-
pub fn expand(&self, to: Vector3) -> Self {
75-
self.merge(&Aabb::new(to, Vector3::ZERO))
74+
pub fn expand(self, to: Vector3) -> Self {
75+
self.merge(Aabb::new(to, Vector3::ZERO))
7676
}
7777

7878
/// Returns a larger AABB that contains this AABB and `b`.
7979
///
8080
/// # Panics
8181
/// If either `self.size` or `b.size` is negative.
8282
#[inline]
83-
pub fn merge(&self, b: &Aabb) -> Self {
83+
pub fn merge(self, b: Aabb) -> Self {
8484
self.assert_nonnegative();
8585
b.assert_nonnegative();
8686

@@ -95,21 +95,21 @@ impl Aabb {
9595
/// # Panics
9696
/// If `self.size` is negative.
9797
#[inline]
98-
pub fn volume(&self) -> real {
98+
pub fn volume(self) -> real {
9999
self.assert_nonnegative();
100100
self.size.x * self.size.y * self.size.z
101101
}
102102

103103
/// Returns the center of the AABB, which is equal to `position + (size / 2)`.
104104
#[inline]
105-
pub fn center(&self) -> Vector3 {
105+
pub fn center(self) -> Vector3 {
106106
self.position + (self.size / 2.0)
107107
}
108108

109109
/// Returns a copy of the AABB grown by the specified `amount` on all sides.
110110
#[inline]
111111
#[must_use]
112-
pub fn grow(&self, amount: real) -> Self {
112+
pub fn grow(self, amount: real) -> Self {
113113
let position = self.position - Vector3::new(amount, amount, amount);
114114
let size = self.size + Vector3::new(amount, amount, amount) * 2.0;
115115

@@ -122,7 +122,7 @@ impl Aabb {
122122
/// # Panics
123123
/// If `self.size` is negative.
124124
#[inline]
125-
pub fn has_point(&self, point: Vector3) -> bool {
125+
pub fn has_point(self, point: Vector3) -> bool {
126126
self.assert_nonnegative();
127127

128128
let point = point - self.position;
@@ -135,13 +135,13 @@ impl Aabb {
135135

136136
/// Returns `true` if the AABB has area, and `false` if the AABB is linear, empty, or has a negative size. See also `Aabb.area()`.
137137
#[inline]
138-
pub fn has_area(&self) -> bool {
138+
pub fn has_area(self) -> bool {
139139
((self.size.x > 0.0) as u8 + (self.size.y > 0.0) as u8 + (self.size.z > 0.0) as u8) >= 2
140140
}
141141

142142
/// Returns true if the AABB has a volume, and false if the AABB is flat, linear, empty, or has a negative size.
143143
#[inline]
144-
pub fn has_volume(&self) -> bool {
144+
pub fn has_volume(self) -> bool {
145145
self.size.x > 0.0 && self.size.y > 0.0 && self.size.z > 0.0
146146
}
147147

@@ -150,14 +150,14 @@ impl Aabb {
150150
/// # Panics
151151
/// If `self.size` is negative.
152152
#[inline]
153-
pub fn intersection(&self, b: &Aabb) -> Option<Self> {
153+
pub fn intersection(self, b: Aabb) -> Option<Self> {
154154
self.assert_nonnegative();
155155

156156
if !self.intersects(b) {
157157
return None;
158158
}
159159

160-
let mut rect = *b;
160+
let mut rect = b;
161161
rect.position = rect.position.coord_max(self.position);
162162

163163
let end = self.end();
@@ -169,13 +169,13 @@ impl Aabb {
169169

170170
/// Returns `true` if this AABB is finite, by calling `@GlobalScope.is_finite` on each component.
171171
#[inline]
172-
pub fn is_finite(&self) -> bool {
172+
pub fn is_finite(self) -> bool {
173173
self.position.is_finite() && self.size.is_finite()
174174
}
175175

176176
/// The end of the `Aabb` calculated as `position + size`.
177177
#[inline]
178-
pub fn end(&self) -> Vector3 {
178+
pub fn end(self) -> Vector3 {
179179
self.position + self.size
180180
}
181181

@@ -189,7 +189,7 @@ impl Aabb {
189189

190190
/// Returns the normalized longest axis of the AABB.
191191
#[inline]
192-
pub fn longest_axis(&self) -> Option<Vector3> {
192+
pub fn longest_axis(self) -> Option<Vector3> {
193193
self.longest_axis_index().map(|axis| match axis {
194194
Vector3Axis::X => Vector3::RIGHT,
195195
Vector3Axis::Y => Vector3::UP,
@@ -199,19 +199,19 @@ impl Aabb {
199199

200200
/// Returns the index of the longest axis of the AABB (according to Vector3's AXIS_* constants).
201201
#[inline]
202-
pub fn longest_axis_index(&self) -> Option<Vector3Axis> {
202+
pub fn longest_axis_index(self) -> Option<Vector3Axis> {
203203
self.size.max_axis()
204204
}
205205

206206
/// Returns the scalar length of the longest axis of the AABB.
207207
#[inline]
208-
pub fn longest_axis_size(&self) -> real {
208+
pub fn longest_axis_size(self) -> real {
209209
self.size.x.max(self.size.y.max(self.size.z))
210210
}
211211

212212
/// Returns the normalized shortest axis of the AABB.
213213
#[inline]
214-
pub fn shortest_axis(&self) -> Option<Vector3> {
214+
pub fn shortest_axis(self) -> Option<Vector3> {
215215
self.shortest_axis_index().map(|axis| match axis {
216216
Vector3Axis::X => Vector3::RIGHT,
217217
Vector3Axis::Y => Vector3::UP,
@@ -221,19 +221,19 @@ impl Aabb {
221221

222222
/// Returns the index of the shortest axis of the AABB (according to Vector3::AXIS* enum).
223223
#[inline]
224-
pub fn shortest_axis_index(&self) -> Option<Vector3Axis> {
224+
pub fn shortest_axis_index(self) -> Option<Vector3Axis> {
225225
self.size.min_axis()
226226
}
227227

228228
/// Returns the scalar length of the shortest axis of the AABB.
229229
#[inline]
230-
pub fn shortest_axis_size(&self) -> real {
230+
pub fn shortest_axis_size(self) -> real {
231231
self.size.x.min(self.size.y.min(self.size.z))
232232
}
233233

234234
/// Returns the support point in a given direction. This is useful for collision detection algorithms.
235235
#[inline]
236-
pub fn support(&self, dir: Vector3) -> Vector3 {
236+
pub fn support(self, dir: Vector3) -> Vector3 {
237237
let half_extents = self.size * 0.5;
238238
let relative_center_point = self.position + half_extents;
239239

@@ -253,7 +253,7 @@ impl Aabb {
253253
///
254254
/// _Godot equivalent: `AABB.intersects(AABB b, bool include_borders = true)`_
255255
#[inline]
256-
pub fn intersects(&self, b: &Aabb) -> bool {
256+
pub fn intersects(self, b: Aabb) -> bool {
257257
let end = self.end();
258258
let end_b = b.end();
259259

@@ -271,7 +271,7 @@ impl Aabb {
271271
///
272272
/// _Godot equivalent: `AABB.intersects(AABB b, bool include_borders = false)`_
273273
#[inline]
274-
pub fn intersects_exclude_borders(&self, &b: &Aabb) -> bool {
274+
pub fn intersects_exclude_borders(self, b: Aabb) -> bool {
275275
let end = self.end();
276276
let end_b = b.end();
277277

@@ -285,7 +285,7 @@ impl Aabb {
285285

286286
/// Returns `true` if the AABB is on both sides of a plane.
287287
#[inline]
288-
pub fn intersects_plane(&self, plane: &Plane) -> bool {
288+
pub fn intersects_plane(self, plane: Plane) -> bool {
289289
// The set of the edges of the AABB.
290290
let points = [
291291
self.position,
@@ -318,7 +318,7 @@ impl Aabb {
318318
/// # Panics
319319
/// If `self.size` is negative.
320320
#[inline]
321-
pub fn intersects_ray(&self, from: Vector3, dir: Vector3) -> bool {
321+
pub fn intersects_ray(self, from: Vector3, dir: Vector3) -> bool {
322322
self.assert_nonnegative();
323323

324324
let tmin = (self.position - from) / dir;
@@ -338,7 +338,7 @@ impl Aabb {
338338
/// # Panics
339339
/// If `self.size` is negative.
340340
#[inline]
341-
pub fn intersects_segment(&self, from: Vector3, to: Vector3) -> bool {
341+
pub fn intersects_segment(self, from: Vector3, to: Vector3) -> bool {
342342
self.assert_nonnegative();
343343

344344
let segment_dir = to - from;
@@ -371,7 +371,7 @@ impl Aabb {
371371
///
372372
/// Most functions will fail to give a correct result if the size is negative.
373373
#[inline]
374-
pub fn assert_nonnegative(&self) {
374+
pub fn assert_nonnegative(self) {
375375
assert!(
376376
self.size.x >= 0.0 && self.size.y >= 0.0 && self.size.z >= 0.0,
377377
"size {:?} is negative",
@@ -467,27 +467,27 @@ mod test {
467467
};
468468

469469
// Check for intersection including border
470-
assert!(aabb1.intersects(&aabb2));
471-
assert!(aabb2.intersects(&aabb1));
470+
assert!(aabb1.intersects(aabb2));
471+
assert!(aabb2.intersects(aabb1));
472472

473473
// Check for non-intersection including border
474-
assert!(!aabb1.intersects(&aabb3));
475-
assert!(!aabb3.intersects(&aabb1));
474+
assert!(!aabb1.intersects(aabb3));
475+
assert!(!aabb3.intersects(aabb1));
476476

477477
// Check for intersection excluding border
478-
assert!(aabb1.intersects_exclude_borders(&aabb2));
479-
assert!(aabb2.intersects_exclude_borders(&aabb1));
478+
assert!(aabb1.intersects_exclude_borders(aabb2));
479+
assert!(aabb2.intersects_exclude_borders(aabb1));
480480

481481
// Check for non-intersection excluding border
482-
assert!(!aabb1.intersects_exclude_borders(&aabb3));
483-
assert!(!aabb3.intersects_exclude_borders(&aabb1));
482+
assert!(!aabb1.intersects_exclude_borders(aabb3));
483+
assert!(!aabb3.intersects_exclude_borders(aabb1));
484484

485485
// Check for non-intersection excluding border
486-
assert!(!aabb1.intersects_exclude_borders(&aabb4));
487-
assert!(!aabb4.intersects_exclude_borders(&aabb1));
486+
assert!(!aabb1.intersects_exclude_borders(aabb4));
487+
assert!(!aabb4.intersects_exclude_borders(aabb1));
488488

489489
// Check for intersection with same AABB including border
490-
assert!(aabb1.intersects(&aabb1));
490+
assert!(aabb1.intersects(aabb1));
491491
}
492492

493493
#[test]
@@ -515,17 +515,17 @@ mod test {
515515

516516
// Test cases
517517
assert_eq!(
518-
aabb1.intersection(&aabb2),
518+
aabb1.intersection(aabb2),
519519
Some(Aabb {
520520
position: Vector3::new(1.0, 1.0, 1.0),
521521
size: Vector3::new(1.0, 1.0, 1.0),
522522
})
523523
);
524524

525-
assert_eq!(aabb1.intersection(&aabb3), None);
525+
assert_eq!(aabb1.intersection(aabb3), None);
526526

527527
assert_eq!(
528-
aabb1.intersection(&aabb4),
528+
aabb1.intersection(aabb4),
529529
Some(Aabb {
530530
position: Vector3::new(0.0, 0.0, 0.0),
531531
size: Vector3::new(0.0, 0.0, 0.0),
@@ -620,10 +620,10 @@ mod test {
620620
};
621621

622622
// Test cases
623-
assert!(aabb.intersects_plane(&plane_inside));
624-
assert!(!aabb.intersects_plane(&plane_outside));
625-
assert!(aabb.intersects_plane(&plane_intersect));
626-
assert!(!aabb.intersects_plane(&plane_parallel));
623+
assert!(aabb.intersects_plane(plane_inside));
624+
assert!(!aabb.intersects_plane(plane_outside));
625+
assert!(aabb.intersects_plane(plane_intersect));
626+
assert!(!aabb.intersects_plane(plane_parallel));
627627
}
628628

629629
#[test]

godot-core/src/builtin/basis.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,15 @@ impl Basis {
162162
}
163163

164164
/// Creates a `[Vector3; 3]` with the columns of the `Basis`.
165-
pub fn to_cols(self) -> [Vector3; 3] {
165+
pub fn to_cols(&self) -> [Vector3; 3] {
166166
self.transposed().rows
167167
}
168168

169169
/// Creates a [`Quaternion`] representing the same rotation as this basis.
170170
///
171171
/// _Godot equivalent: `Basis.get_rotation_quaternion()`_
172172
#[doc(alias = "get_rotation_quaternion")]
173-
pub fn to_quat(self) -> Quaternion {
173+
pub fn to_quat(&self) -> Quaternion {
174174
RQuat::from_mat3(&self.orthonormalized().to_glam()).to_front()
175175
}
176176

@@ -211,7 +211,7 @@ impl Basis {
211211
/// The order of the angles are given by `order`.
212212
///
213213
/// _Godot equivalent: `Basis.get_euler()`_
214-
pub fn to_euler(self, order: EulerOrder) -> Vector3 {
214+
pub fn to_euler(&self, order: EulerOrder) -> Vector3 {
215215
use glam::swizzles::Vec3Swizzles as _;
216216

217217
let col_a = self.col_a().to_glam();
@@ -346,23 +346,23 @@ impl Basis {
346346
///
347347
/// _Godot equivalent: `Basis.scaled()`_
348348
#[must_use]
349-
pub fn scaled(self, scale: Vector3) -> Self {
350-
Self::from_diagonal(scale.x, scale.y, scale.z) * self
349+
pub fn scaled(&self, scale: Vector3) -> Self {
350+
Self::from_diagonal(scale.x, scale.y, scale.z) * (*self)
351351
}
352352

353353
/// Returns the inverse of the matrix.
354354
///
355355
/// _Godot equivalent: `Basis.inverse()`_
356356
#[must_use]
357-
pub fn inverse(self) -> Basis {
357+
pub fn inverse(&self) -> Basis {
358358
self.glam(|mat| mat.inverse())
359359
}
360360

361361
/// Returns the transposed version of the matrix.
362362
///
363363
/// _Godot equivalent: `Basis.transposed()`_
364364
#[must_use]
365-
pub fn transposed(self) -> Self {
365+
pub fn transposed(&self) -> Self {
366366
Self::from_cols(self.rows[0], self.rows[1], self.rows[2])
367367
}
368368

@@ -376,7 +376,7 @@ impl Basis {
376376
///
377377
/// _Godot equivalent: `Basis.orthonormalized()`_
378378
#[must_use]
379-
pub fn orthonormalized(self) -> Self {
379+
pub fn orthonormalized(&self) -> Self {
380380
assert!(
381381
!self.determinant().is_zero_approx(),
382382
"Determinant should not be zero."
@@ -401,16 +401,16 @@ impl Basis {
401401
///
402402
/// _Godot equivalent: `Basis.rotated()`_
403403
#[must_use]
404-
pub fn rotated(self, axis: Vector3, angle: real) -> Self {
405-
Self::from_axis_angle(axis, angle) * self
404+
pub fn rotated(&self, axis: Vector3, angle: real) -> Self {
405+
Self::from_axis_angle(axis, angle) * (*self)
406406
}
407407

408408
/// Assuming that the matrix is a proper rotation matrix, slerp performs
409409
/// a spherical-linear interpolation with another rotation matrix.
410410
///
411411
/// _Godot equivalent: `Basis.slerp()`_
412412
#[must_use]
413-
pub fn slerp(self, other: Self, weight: real) -> Self {
413+
pub fn slerp(&self, other: &Self, weight: real) -> Self {
414414
let from = self.to_quat();
415415
let to = other.to_quat();
416416

0 commit comments

Comments
 (0)