Skip to content

Commit dfdcc4c

Browse files
committed
Rect2, Aabb: pass self by value (they are Copy; also consistency with Quat, Plane etc.)
1 parent b02b6ce commit dfdcc4c

File tree

2 files changed

+41
-43
lines changed

2 files changed

+41
-43
lines changed

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl Aabb {
2828

2929
/// Ending corner. This is calculated as `position + size`.
3030
#[inline]
31-
pub fn end(&self) -> Vector3 {
31+
pub fn end(self) -> Vector3 {
3232
self.position + self.size
3333
}
3434

@@ -41,7 +41,7 @@ impl Aabb {
4141
/// Returns an `Aabb` with equivalent position and area, modified so that the most-negative
4242
/// corner is the origin and the size is positive.
4343
#[inline]
44-
pub fn abs(&self) -> Self {
44+
pub fn abs(self) -> Self {
4545
let position = self.position + Vector3::gd(self.size.glam().min(glam::Vec3A::ZERO));
4646
let size = self.size.abs();
4747

@@ -54,7 +54,7 @@ impl Aabb {
5454
///
5555
/// [get_area]: https://docs.godotengine.org/en/stable/classes/class_aabb.html#class-aabb-method-get-area
5656
#[inline]
57-
pub fn get_volume(&self) -> f32 {
57+
pub fn get_volume(self) -> f32 {
5858
self.size.x * self.size.y * self.size.z
5959
}
6060

@@ -68,13 +68,13 @@ impl Aabb {
6868
///
6969
/// [`has_no_area`]: https://docs.godotengine.org/en/stable/classes/class_aabb.html#class-aabb-method-has-no-area
7070
#[inline]
71-
pub fn has_no_volume(&self) -> bool {
71+
pub fn has_no_volume(self) -> bool {
7272
self.size.x <= 0.0 || self.size.y <= 0.0 || self.size.z <= 0.0
7373
}
7474

7575
/// Returns true if the bounding box is empty or all of its dimensions are negative.
7676
#[inline]
77-
pub fn has_no_surface(&self) -> bool {
77+
pub fn has_no_surface(self) -> bool {
7878
self.size.x <= 0.0 && self.size.y <= 0.0 && self.size.z <= 0.0
7979
}
8080

@@ -84,7 +84,7 @@ impl Aabb {
8484
/// Note: This method is not reliable for bounding boxes with a negative size. Use
8585
/// [`abs`][Self::abs] to get a positive sized equivalent box to check for contained points.
8686
#[inline]
87-
pub fn contains_point(&self, point: Vector3) -> bool {
87+
pub fn contains_point(self, point: Vector3) -> bool {
8888
let point = point - self.position;
8989

9090
point.abs() == point
@@ -96,15 +96,15 @@ impl Aabb {
9696
/// Returns true if this bounding box and `b` are approximately equal, by calling
9797
/// [`is_equal_approx`](Vector3::is_equal_approx) on each component.
9898
#[inline]
99-
pub fn is_equal_approx(&self, b: Self) -> bool {
99+
pub fn is_equal_approx(self, b: Self) -> bool {
100100
self.position.is_equal_approx(b.position) && self.size.is_equal_approx(b.size)
101101
}
102102

103103
/// Gets the position of the 8 endpoints of the bounding box in space.
104104
///
105105
/// The index returns an arbitrary point, but all points are guaranteed to be unique.
106106
#[inline]
107-
pub fn get_endpoint(&self, index: usize) -> Option<Vector3> {
107+
pub fn get_endpoint(self, index: usize) -> Option<Vector3> {
108108
match index {
109109
0 => Some(self.position),
110110
1 => Some(self.position + Vector3::new(0.0, 0.0, self.size.z)),
@@ -120,42 +120,42 @@ impl Aabb {
120120

121121
/// Returns the normalized longest axis of the bounding box.
122122
#[inline]
123-
pub fn get_longest_axis(&self) -> Vector3 {
123+
pub fn get_longest_axis(self) -> Vector3 {
124124
self.size.max_axis().to_unit_vector()
125125
}
126126

127127
/// Returns the index of the longest axis of the bounding box.
128128
///
129129
/// If multiple axes have the same length, then the first in order X, Y, Z is returned.
130130
#[inline]
131-
pub fn get_longest_axis_index(&self) -> Axis {
131+
pub fn get_longest_axis_index(self) -> Axis {
132132
self.size.max_axis()
133133
}
134134

135135
/// Returns the scalar length of the longest axis of the bounding box.
136136
#[inline]
137-
pub fn get_longest_axis_size(&self) -> f32 {
137+
pub fn get_longest_axis_size(self) -> f32 {
138138
let Vector3 { x, y, z } = self.size;
139139
x.max(y).max(z)
140140
}
141141

142142
/// Returns the normalized shortest axis of the bounding box.
143143
#[inline]
144-
pub fn get_shortest_axis(&self) -> Vector3 {
144+
pub fn get_shortest_axis(self) -> Vector3 {
145145
self.size.min_axis().to_unit_vector()
146146
}
147147

148148
/// Returns the index of the shortest axis of the bounding box.
149149
///
150150
/// If multiple axes have the same length, then the first in order X, Y, Z is returned.
151151
#[inline]
152-
pub fn get_shortest_axis_index(&self) -> Axis {
152+
pub fn get_shortest_axis_index(self) -> Axis {
153153
self.size.min_axis()
154154
}
155155

156156
/// Returns the scalar length of the shortest axis of the bounding box.
157157
#[inline]
158-
pub fn get_shortest_axis_size(&self) -> f32 {
158+
pub fn get_shortest_axis_size(self) -> f32 {
159159
let Vector3 { x, y, z } = self.size;
160160
x.min(y).min(z)
161161
}
@@ -173,7 +173,7 @@ impl Aabb {
173173
/// [1]: https://ncollide.org/geometric_representations/#support-mappings
174174
/// [2]: https://www.toptal.com/game/video-game-physics-part-ii-collision-detection-for-solid-objects
175175
#[inline]
176-
pub fn get_support(&self, dir: Vector3) -> Vector3 {
176+
pub fn get_support(self, dir: Vector3) -> Vector3 {
177177
self.position
178178
+ Vector3::new(
179179
if dir.x > 0.0 { 0.0 } else { self.size.x },
@@ -187,7 +187,7 @@ impl Aabb {
187187
/// It is possible to specify a negative amount to shrink the AABB (note that this can invert the AABB).
188188
#[inline]
189189
#[must_use]
190-
pub fn grow(&self, by: f32) -> Self {
190+
pub fn grow(self, by: f32) -> Self {
191191
let position = self.position - Vector3::new(by, by, by);
192192
let size = self.size + Vector3::new(by, by, by) * 2.0;
193193

@@ -198,7 +198,7 @@ impl Aabb {
198198
///
199199
/// This **excludes** borders; if the intersection has no volume, `false` is returned.
200200
#[inline]
201-
pub fn intersects(&self, b: Self) -> bool {
201+
pub fn intersects(self, b: Self) -> bool {
202202
self.position.x < b.position.x + b.size.x
203203
&& self.position.x + self.size.x > b.position.x
204204
&& self.position.y < b.position.y + b.size.y
@@ -209,7 +209,7 @@ impl Aabb {
209209

210210
/// Returns true if the bounding box is on both sides of a plane.
211211
#[inline]
212-
pub fn intersects_plane(&self, plane: Plane) -> bool {
212+
pub fn intersects_plane(self, plane: Plane) -> bool {
213213
let mut corners = [Vector3::ZERO; 8];
214214
for (i, corner) in corners.iter_mut().enumerate() {
215215
*corner = self.get_endpoint(i).unwrap();
@@ -230,7 +230,7 @@ impl Aabb {
230230

231231
/// Returns true if the bounding box intersects the line segment between `from` and `to`.
232232
#[inline]
233-
pub fn intersects_segment(&self, from: Vector3, to: Vector3) -> bool {
233+
pub fn intersects_segment(self, from: Vector3, to: Vector3) -> bool {
234234
let mut min: f32 = 0.0;
235235
let mut max: f32 = 1.0;
236236

@@ -283,7 +283,7 @@ impl Aabb {
283283
/// This **excludes** borders; if the intersection has no volume, `None` is returned.
284284
#[inline]
285285
#[must_use]
286-
pub fn intersection(&self, b: Self) -> Option<Self> {
286+
pub fn intersection(self, b: Self) -> Option<Self> {
287287
if !self.intersects(b) {
288288
return None;
289289
}
@@ -306,7 +306,7 @@ impl Aabb {
306306
/// Returns a larger bounding box that contains both this `Aabb` and `b`.
307307
#[inline]
308308
#[must_use]
309-
pub fn merge(&self, b: Self) -> Self {
309+
pub fn merge(self, b: Self) -> Self {
310310
let position = Vector3::new(
311311
self.position.x.min(b.position.x),
312312
self.position.y.min(b.position.y),

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

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl Rect2 {
3535

3636
/// Ending corner. This is calculated as `position + size`.
3737
#[inline]
38-
pub fn end(&self) -> Vector2 {
38+
pub fn end(self) -> Vector2 {
3939
self.position + self.size
4040
}
4141

@@ -48,7 +48,7 @@ impl Rect2 {
4848
/// Returns a rectangle with equivalent position and area, modified so that the top-left corner
4949
/// is the origin and `width` and `height` are positive.
5050
#[inline]
51-
pub fn abs(&self) -> Self {
51+
pub fn abs(self) -> Self {
5252
let position = self.position + Vector2::new(self.size.x.min(0.0), self.size.y.min(0.0));
5353
let size = self.size.abs();
5454

@@ -57,7 +57,7 @@ impl Rect2 {
5757

5858
/// Returns the area of the rectangle. See also [`has_no_area`][Self::has_no_area].
5959
#[inline]
60-
pub fn get_area(&self) -> f32 {
60+
pub fn get_area(self) -> f32 {
6161
self.size.x * self.size.y
6262
}
6363

@@ -80,7 +80,7 @@ impl Rect2 {
8080
/// # }
8181
/// ```
8282
#[inline]
83-
pub fn has_no_area(&self) -> bool {
83+
pub fn has_no_area(self) -> bool {
8484
self.size.x <= 0.0 || self.size.y <= 0.0
8585
}
8686

@@ -90,7 +90,7 @@ impl Rect2 {
9090
/// Note: This method is not reliable for `Rect2` with a negative size. Use [`abs`][Self::abs]
9191
/// to get a positive sized equivalent rectangle to check for contained points.
9292
#[inline]
93-
pub fn contains_point(&self, point: Vector2) -> bool {
93+
pub fn contains_point(self, point: Vector2) -> bool {
9494
let point = point - self.position;
9595

9696
point.abs() == point && point.x < self.size.x && point.y < self.size.y
@@ -99,7 +99,7 @@ impl Rect2 {
9999
/// Returns true if this rectangle and `b` are approximately equal, by calling
100100
/// [`is_equal_approx`](Vector2::is_equal_approx) on each component.
101101
#[inline]
102-
pub fn is_equal_approx(&self, b: Self) -> bool {
102+
pub fn is_equal_approx(self, b: Self) -> bool {
103103
self.position.is_equal_approx(b.position) && self.size.is_equal_approx(b.size)
104104
}
105105

@@ -111,7 +111,7 @@ impl Rect2 {
111111
/// Note: This method is not reliable for `Rect2` with a negative size. Use [`abs`][Self::abs]
112112
/// to get a positive sized equivalent rectangle to check for intersections.
113113
#[inline]
114-
pub fn intersects(&self, b: Self) -> bool {
114+
pub fn intersects(self, b: Self) -> bool {
115115
self.position.x < b.position.x + b.size.x
116116
&& self.position.x + self.size.x > b.position.x
117117
&& self.position.y < b.position.y + b.size.y
@@ -126,7 +126,7 @@ impl Rect2 {
126126
/// Note: This method is not reliable for `Rect2` with a negative size. Use [`abs`][Self::abs]
127127
/// to get a positive sized equivalent rectangle to check for intersections.
128128
#[inline]
129-
pub fn intersects_including_borders(&self, b: Self) -> bool {
129+
pub fn intersects_including_borders(self, b: Self) -> bool {
130130
self.position.x <= b.position.x + b.size.x
131131
&& self.position.x + self.size.x >= b.position.x
132132
&& self.position.y <= b.position.y + b.size.y
@@ -137,7 +137,7 @@ impl Rect2 {
137137
///
138138
/// This is true when `self` covers all the area of `b`, and possibly (but not necessarily) more.
139139
#[inline]
140-
pub fn encloses(&self, b: Self) -> bool {
140+
pub fn encloses(self, b: Self) -> bool {
141141
b.position.x >= self.position.x
142142
&& b.position.y >= self.position.y
143143
&& b.position.x + b.size.x <= self.position.x + self.size.x
@@ -153,7 +153,7 @@ impl Rect2 {
153153
/// to get a positive sized equivalent rectangle for clipping.
154154
#[inline]
155155
#[must_use]
156-
pub fn intersection(&self, b: Self) -> Option<Self> {
156+
pub fn intersection(self, b: Self) -> Option<Self> {
157157
if !self.intersects(b) {
158158
return None;
159159
}
@@ -177,7 +177,7 @@ impl Rect2 {
177177
/// to get a positive sized equivalent rectangle for merging.
178178
#[inline]
179179
#[must_use]
180-
pub fn merge(&self, b: Self) -> Self {
180+
pub fn merge(self, b: Self) -> Self {
181181
let position = Vector2::new(
182182
self.position.x.min(b.position.x),
183183
self.position.y.min(b.position.y),
@@ -214,14 +214,14 @@ impl Rect2 {
214214
/// ```
215215
#[inline]
216216
#[must_use]
217-
pub fn expand(&self, to: Vector2) -> Self {
217+
pub fn expand(self, to: Vector2) -> Self {
218218
self.merge(Self::new(to, Vector2::ZERO))
219219
}
220220

221221
/// Returns a copy of this rectangle grown by a given amount of units on all the sides.
222222
#[inline]
223223
#[must_use]
224-
pub fn grow(&self, by: f32) -> Self {
224+
pub fn grow(self, by: f32) -> Self {
225225
let position = self.position - Vector2::new(by, by);
226226
let size = self.size + Vector2::new(by, by) * 2.0;
227227

@@ -232,22 +232,20 @@ impl Rect2 {
232232
/// individually.
233233
#[inline]
234234
#[must_use]
235-
pub fn grow_individual(&self, left: f32, top: f32, right: f32, bottom: f32) -> Self {
236-
let mut rect = *self;
235+
pub fn grow_individual(mut self, left: f32, top: f32, right: f32, bottom: f32) -> Self {
236+
self.position.x -= left;
237+
self.position.y -= top;
238+
self.size.x += left + right;
239+
self.size.y += top + bottom;
237240

238-
rect.position.x -= left;
239-
rect.position.y -= top;
240-
rect.size.x += left + right;
241-
rect.size.y += top + bottom;
242-
243-
rect
241+
self
244242
}
245243

246244
/// Returns a copy of this rectangle grown by a given amount of units towards the [`Margin`]
247245
/// direction.
248246
#[inline]
249247
#[must_use]
250-
pub fn grow_margin(&self, margin: Margin, amount: f32) -> Self {
248+
pub fn grow_margin(self, margin: Margin, amount: f32) -> Self {
251249
let left = if margin == Margin::Left { amount } else { 0.0 };
252250
let top = if margin == Margin::Top { amount } else { 0.0 };
253251
let right = if margin == Margin::Right { amount } else { 0.0 };

0 commit comments

Comments
 (0)