Skip to content

Commit c77ea89

Browse files
committed
Aabb, Rect2, Rect2i: rename intersection() -> intersect()
1 parent e2c95c3 commit c77ea89

File tree

5 files changed

+49
-34
lines changed

5 files changed

+49
-34
lines changed

godot-core/src/builtin/aabb.rs

+27-22
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,10 @@ impl Aabb {
174174

175175
/// Returns the intersection between two AABBs.
176176
///
177-
/// # Panics
177+
/// # Panics (Debug)
178178
/// If `self.size` is negative.
179179
#[inline]
180-
pub fn intersection(self, b: Aabb) -> Option<Self> {
180+
pub fn intersect(self, b: Aabb) -> Option<Self> {
181181
self.assert_nonnegative();
182182

183183
if !self.intersects(b) {
@@ -194,6 +194,11 @@ impl Aabb {
194194
Some(rect)
195195
}
196196

197+
#[deprecated = "Renamed to `intersect()`"]
198+
pub fn intersection(self, b: Aabb) -> Option<Self> {
199+
self.intersect(b)
200+
}
201+
197202
/// Returns `true` if this AABB is finite, by calling `@GlobalScope.is_finite` on each component.
198203
#[inline]
199204
pub fn is_finite(self) -> bool {
@@ -434,6 +439,7 @@ impl Aabb {
434439
///
435440
/// Most functions will fail to give a correct result if the size is negative.
436441
#[inline]
442+
/// TODO(v0.3): make private, change to debug_assert().
437443
pub fn assert_nonnegative(self) {
438444
assert!(
439445
self.size.x >= 0.0 && self.size.y >= 0.0 && self.size.z >= 0.0,
@@ -529,27 +535,27 @@ mod test {
529535
size: Vector3::new(1.0, 1.0, 1.0),
530536
};
531537

532-
// Check for intersection including border
538+
// Check for intersection including border.
533539
assert!(aabb1.intersects(aabb2));
534540
assert!(aabb2.intersects(aabb1));
535541

536-
// Check for non-intersection including border
542+
// Check for non-intersection including border.
537543
assert!(!aabb1.intersects(aabb3));
538544
assert!(!aabb3.intersects(aabb1));
539545

540-
// Check for intersection excluding border
546+
// Check for intersection excluding border.
541547
assert!(aabb1.intersects_exclude_borders(aabb2));
542548
assert!(aabb2.intersects_exclude_borders(aabb1));
543549

544-
// Check for non-intersection excluding border
550+
// Check for non-intersection excluding border.
545551
assert!(!aabb1.intersects_exclude_borders(aabb3));
546552
assert!(!aabb3.intersects_exclude_borders(aabb1));
547553

548-
// Check for non-intersection excluding border
554+
// Check for non-intersection excluding border.
549555
assert!(!aabb1.intersects_exclude_borders(aabb4));
550556
assert!(!aabb4.intersects_exclude_borders(aabb1));
551557

552-
// Check for intersection with same AABB including border
558+
// Check for intersection with same AABB including border.
553559
assert!(aabb1.intersects(aabb1));
554560
}
555561

@@ -576,19 +582,18 @@ mod test {
576582
size: Vector3::new(1.0, 1.0, 1.0),
577583
};
578584

579-
// Test cases
580585
assert_eq!(
581-
aabb1.intersection(aabb2),
586+
aabb1.intersect(aabb2),
582587
Some(Aabb {
583588
position: Vector3::new(1.0, 1.0, 1.0),
584589
size: Vector3::new(1.0, 1.0, 1.0),
585590
})
586591
);
587592

588-
assert_eq!(aabb1.intersection(aabb3), None);
593+
assert_eq!(aabb1.intersect(aabb3), None);
589594

590595
assert_eq!(
591-
aabb1.intersection(aabb4),
596+
aabb1.intersect(aabb4),
592597
Some(Aabb {
593598
position: Vector3::new(0.0, 0.0, 0.0),
594599
size: Vector3::new(0.0, 0.0, 0.0),
@@ -598,7 +603,7 @@ mod test {
598603

599604
#[test]
600605
fn test_intersects_ray() {
601-
// Test case 1: Ray intersects the AABB
606+
// Test case 1: Ray intersects the AABB.
602607
let aabb1 = Aabb {
603608
position: Vector3::new(0.0, 0.0, 0.0),
604609
size: Vector3::new(2.0, 2.0, 2.0),
@@ -608,7 +613,7 @@ mod test {
608613

609614
assert!(aabb1.intersects_ray(from1, dir1));
610615

611-
// Test case 2: Ray misses the AABB
616+
// Test case 2: Ray misses the AABB.
612617
let aabb2 = Aabb {
613618
position: Vector3::new(0.0, 0.0, 0.0),
614619
size: Vector3::new(2.0, 2.0, 2.0),
@@ -617,7 +622,7 @@ mod test {
617622
let dir2 = Vector3::new(0.0, 0.0, 1.0);
618623
assert!(!aabb2.intersects_ray(from2, dir2));
619624

620-
// Test case 3: Ray starts inside the AABB
625+
// Test case 3: Ray starts inside the AABB.
621626
let aabb3 = Aabb {
622627
position: Vector3::new(0.0, 0.0, 0.0),
623628
size: Vector3::new(2.0, 2.0, 2.0),
@@ -626,7 +631,7 @@ mod test {
626631
let dir3 = Vector3::new(0.0, 0.0, 1.0);
627632
assert!(aabb3.intersects_ray(from3, dir3));
628633

629-
// Test case 4: Ray direction parallel to AABB
634+
// Test case 4: Ray direction parallel to AABB.
630635
let aabb4 = Aabb {
631636
position: Vector3::new(0.0, 0.0, 0.0),
632637
size: Vector3::new(2.0, 2.0, 2.0),
@@ -635,7 +640,7 @@ mod test {
635640
let dir4 = Vector3::new(1.0, 0.0, 0.0);
636641
assert!(aabb4.intersects_ray(from4, dir4));
637642

638-
// Test case 5: Ray direction diagonal through the AABB
643+
// Test case 5: Ray direction diagonal through the AABB.
639644
let aabb5 = Aabb {
640645
position: Vector3::new(0.0, 0.0, 0.0),
641646
size: Vector3::new(2.0, 2.0, 2.0),
@@ -644,7 +649,7 @@ mod test {
644649
let dir5 = Vector3::new(1.0, 1.0, 1.0);
645650
assert!(aabb5.intersects_ray(from5, dir5));
646651

647-
// Test case 6: Ray origin on an AABB face
652+
// Test case 6: Ray origin on an AABB face.
648653
let aabb6 = Aabb {
649654
position: Vector3::new(0.0, 0.0, 0.0),
650655
size: Vector3::new(2.0, 2.0, 2.0),
@@ -745,10 +750,10 @@ mod test {
745750
position: Vector3::new(-1.5, 2.0, -2.5),
746751
size: Vector3::ONE,
747752
};
748-
let inter = aabb_big.intersection(aabb_small);
753+
let inter = aabb_big.intersect(aabb_small);
749754
assert!(
750755
inter.unwrap().approx_eq(&aabb_small),
751-
"intersection() with fully contained AABB should return the smaller AABB."
756+
"intersect() with fully contained AABB should return the smaller AABB."
752757
);
753758

754759
let aabb_small = Aabb {
@@ -759,7 +764,7 @@ mod test {
759764
position: Vector3::new(0.5, 2.0, -2.0),
760765
size: Vector3::new(1.0, 0.5, 1.0),
761766
};
762-
let inter = aabb_big.intersection(aabb_small);
767+
let inter = aabb_big.intersect(aabb_small);
763768
assert!(
764769
inter.unwrap().approx_eq(&expected),
765770
"intersect() with partially contained AABB (overflowing on Y axis) should match expected."
@@ -769,7 +774,7 @@ mod test {
769774
position: Vector3::new(10.0, -10.0, -10.0),
770775
size: Vector3::ONE,
771776
};
772-
let inter = aabb_big.intersection(aabb_small);
777+
let inter = aabb_big.intersect(aabb_small);
773778
assert!(
774779
inter.is_none(),
775780
"intersect() with non-contained AABB should return None."

godot-core/src/builtin/rect2.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ impl Rect2 {
198198

199199
/// Returns the intersection of this Rect2 and `b`. If the rectangles do not intersect, an empty Rect2 is returned.
200200
#[inline]
201-
pub fn intersection(self, b: Self) -> Option<Self> {
201+
pub fn intersect(self, b: Self) -> Option<Self> {
202202
if !self.intersects(b) {
203203
return None;
204204
}
@@ -213,6 +213,11 @@ impl Rect2 {
213213
Some(rect)
214214
}
215215

216+
#[deprecated = "Renamed to `intersect()`"]
217+
pub fn intersection(self, b: Rect2) -> Option<Self> {
218+
self.intersect(b)
219+
}
220+
216221
/// Checks whether two rectangles have at least one point in common.
217222
///
218223
/// Also returns `true` if the rects only touch each other (share a point/edge).

godot-core/src/builtin/rect2i.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ impl Rect2i {
223223
///
224224
/// Note that rectangles that only share a border do not intersect.
225225
#[inline]
226-
pub fn intersection(self, b: Self) -> Option<Self> {
226+
pub fn intersect(self, b: Self) -> Option<Self> {
227227
self.assert_nonnegative();
228228
b.assert_nonnegative();
229229

@@ -243,11 +243,16 @@ impl Rect2i {
243243
Some(Self::from_corners(new_pos, new_end))
244244
}
245245

246+
#[deprecated = "Renamed to `intersect()`"]
247+
pub fn intersection(self, b: Self) -> Option<Self> {
248+
self.intersect(b)
249+
}
250+
246251
/// Returns `true` if the `Rect2i` overlaps with `b` (i.e. they have at least one
247252
/// point in common)
248253
#[inline]
249254
pub fn intersects(self, b: Self) -> bool {
250-
self.intersection(b).is_some()
255+
self.intersect(b).is_some()
251256
}
252257

253258
/// Returns a larger `Rect2i` that contains this `Rect2i` and `b`.
@@ -554,19 +559,19 @@ mod test {
554559
let d = Rect2i::from_components(8, 8, 2, 3);
555560

556561
assert!(a.intersects(b));
557-
assert_eq!(a.intersection(b), Some(b));
562+
assert_eq!(a.intersect(b), Some(b));
558563
assert!(a.intersects(c));
559-
assert_eq!(a.intersection(c), Some(c));
564+
assert_eq!(a.intersect(c), Some(c));
560565
assert!(a.intersects(d));
561-
assert_eq!(a.intersection(d), Some(c));
566+
assert_eq!(a.intersect(d), Some(c));
562567

563568
assert!(!b.intersects(c));
564-
assert_eq!(b.intersection(c), None);
569+
assert_eq!(b.intersect(c), None);
565570
assert!(!b.intersects(d));
566-
assert_eq!(b.intersection(d), None);
571+
assert_eq!(b.intersect(d), None);
567572

568573
assert!(c.intersects(d));
569-
assert_eq!(c.intersection(d), Some(c));
574+
assert_eq!(c.intersect(d), Some(c));
570575
}
571576

572577
#[test]

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fn rect2_inner_equivalence() {
4848
inner_rect.intersects(other, false),
4949
);
5050
assert_eq!(
51-
rect.intersection(other).unwrap_or_default(),
51+
rect.intersect(other).unwrap_or_default(),
5252
inner_rect.intersection(other),
5353
);
5454
assert_eq_approx!(rect.merge(other), inner_rect.merge(other));

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ fn rect2i_equiv_unary() {
5050
evaluate_mappings("encloses", a.encloses(b), inner_a.encloses(b));
5151
evaluate_mappings("intersects", a.intersects(b), inner_a.intersects(b));
5252
evaluate_mappings(
53-
"intersection",
54-
a.intersection(b).unwrap_or_default(),
53+
"intersect",
54+
a.intersect(b).unwrap_or_default(),
5555
inner_a.intersection(b),
5656
);
5757
evaluate_mappings("merge", a.merge(b), inner_a.merge(b));

0 commit comments

Comments
 (0)