add intersect (&) and union (|) Rect_ operators
MODIFY
src/manual/core/rect.rs
src/manual/core/rect.rs
@@ -2,6 +2,7 @@ use std::{
fmt,
ops::{Add, AddAssign, Sub, SubAssign},
};
+use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign};
use num::{NumCast, ToPrimitive};
@@ -135,6 +136,24 @@ impl<S, R> Sub<Size_<S>> for Rect_<R>
}
}
+impl<T: ValidRectType> BitOr for Rect_<T> {
+ type Output = Rect_<T>;
+
+ fn bitor(mut self, rhs: Self) -> Self::Output {
+ self |= rhs;
+ self
+ }
+}
+
+impl<T: ValidRectType> BitAnd for Rect_<T> {
+ type Output = Rect_<T>;
+
+ fn bitand(mut self, rhs: Self) -> Self::Output {
+ self &= rhs;
+ self
+ }
+}
+
impl<P, R> AddAssign<Point_<P>> for Rect_<R>
where
P: ValidPointType,
@@ -179,6 +198,35 @@ impl<S, R> SubAssign<Size_<S>> for Rect_<R>
}
}
+impl<T: ValidRectType> BitOrAssign for Rect_<T> {
+ fn bitor_assign(&mut self, rhs: Self) {
+ if self.empty() {
+ *self = rhs;
+ } else if !rhs.empty() {
+ let x1 = partial_min(self.x, rhs.x);
+ let y1 = partial_min(self.y, rhs.y);
+ self.width = partial_max(self.x + self.width, rhs.x + rhs.width) - x1;
+ self.height = partial_max(self.y + self.height, rhs.y + rhs.height) - y1;
+ self.x = x1;
+ self.y = y1;
+ }
+ }
+}
+
+impl<T: ValidRectType> BitAndAssign for Rect_<T> {
+ fn bitand_assign(&mut self, rhs: Self) {
+ let x1 = partial_max(self.x, rhs.x);
+ let y1 = partial_max(self.y, rhs.y);
+ self.width = partial_min(self.x + self.width, rhs.x + rhs.width) - x1;
+ self.height = partial_min(self.y + self.height, rhs.y + rhs.height) - y1;
+ self.x = x1;
+ self.y = y1;
+ if self.empty() {
+ *self = Rect_::default();
+ }
+ }
+}
+
impl fmt::Debug for RotatedRect {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("RotatedRect")

MODIFY
tests/rect.rs
tests/rect.rs
-use opencv::core::{self, Point2d, Point2i, Rect2d, Rect2f, Rect2i, Size2d, Size2i};
+use opencv::core::{self, Point2d, Point2i, Rect, Rect2d, Rect2f, Rect2i, Size2d, Size2i};
#[test]
fn rect_add() {
- let src = core::Rect::new(0, 0, 100, 100);
+ let src = Rect::new(0, 0, 100, 100);
// Point
- let res = core::Rect::new(50, 50, 100, 100);
+ let res = Rect::new(50, 50, 100, 100);
{
let src = src.clone();
let out = src + core::Point::new(50, 50);
@@ -19,7 +19,7 @@ fn rect_add() {
}
// Size
- let res = core::Rect::new(0, 0, 200, 200);
+ let res = Rect::new(0, 0, 200, 200);
{
let src = src.clone();
let out = src + core::Size::new(100, 100);
@@ -35,10 +35,10 @@ fn rect_add() {
#[test]
fn rect_sub() {
- let src = core::Rect::new(50, 50, 100, 100);
+ let src = Rect::new(50, 50, 100, 100);
// Point
- let res = core::Rect::new(25, 25, 100, 100);
+ let res = Rect::new(25, 25, 100, 100);
{
let src = src.clone();
let out = src - core::Point::new(25, 25);
@@ -52,7 +52,7 @@ fn rect_sub() {
}
// Size
- let res = core::Rect::new(50, 50, 75, 75);
+ let res = Rect::new(50, 50, 75, 75);
{
let src = src.clone();
let out = src - core::Size::new(25, 25);
@@ -66,6 +66,44 @@ fn rect_sub() {
}
}
+#[test]
+fn rect_intersect() {
+ {
+ assert_eq!(Rect::new(50, 50, 100, 100), Rect::new(50, 50, 100, 100) & Rect::new(50, 50, 100, 100));
+ assert_eq!(Rect2d::new(149., 149., 1., 1.), Rect2d::new(50., 50., 100., 100.) & Rect2d::new(149., 149., 100., 100.));
+ assert_eq!(Rect2d::new(0., 0., 0., 0.), Rect2d::new(50., 50., 100., 100.) & Rect2d::new(150., 50., 100., 100.));
+ assert_eq!(Rect2f::new(0., 0., 0., 0.), Rect2f::new(50., 50., 100., 100.) & Rect2f::new(50., 150., 100., 100.));
+ assert_eq!(Rect2d::new(0., 0., 0., 0.), Rect2d::new(50., 50., 100., 100.) & Rect2d::new(-50., 50., 100., 100.));
+ assert_eq!(Rect::new(0, 0, 0, 0), Rect::new(50, 50, 100, 100) & Rect::new(50, -50, 100, 100));
+ assert_eq!(Rect2f::new(75., 75., 75., 75.), Rect2f::new(50., 50., 100., 100.) & Rect2f::new(75., 75., 100., 100.));
+ }
+
+ {
+ let mut r = Rect::new(50, 50, 100, 100);
+ r &= Rect::new(75, 75, 100, 10);
+ assert_eq!(Rect::new(75, 75, 75, 10), r);
+ }
+}
+
+#[test]
+fn rect_union() {
+ {
+ assert_eq!(Rect::new(50, 50, 100, 100), Rect::new(50, 50, 100, 100) | Rect::new(50, 50, 100, 100));
+ assert_eq!(Rect2d::new(50., 50., 199., 199.), Rect2d::new(50., 50., 100., 100.) | Rect2d::new(149., 149., 100., 100.));
+ assert_eq!(Rect2d::new(50., 50., 200., 100.), Rect2d::new(50., 50., 100., 100.) | Rect2d::new(150., 50., 100., 100.));
+ assert_eq!(Rect2f::new(50., 50., 100., 200.), Rect2f::new(50., 50., 100., 100.) | Rect2f::new(50., 150., 100., 100.));
+ assert_eq!(Rect2d::new(-50., 50., 200., 100.), Rect2d::new(50., 50., 100., 100.) | Rect2d::new(-50., 50., 100., 100.));
+ assert_eq!(Rect::new(50, -50, 100, 200), Rect::new(50, 50, 100, 100) | Rect::new(50, -50, 100, 100));
+ assert_eq!(Rect2f::new(50., 50., 125., 125.), Rect2f::new(50., 50., 100., 100.) | Rect2f::new(75., 75., 100., 100.));
+ }
+
+ {
+ let mut r = Rect::new(50, 50, 100, 100);
+ r |= Rect::new(75, 75, 100, 10);
+ assert_eq!(Rect::new(50, 50, 125, 100), r);
+ }
+}
+
#[test]
fn rect_constructor() {
let r = Rect2i::from_point_size(Point2i::new(1, 2), Size2i::new(3,4));

add intersect (&) and union (|) Rect_ operators