add +/- operations for Rect_
MODIFY
src/hub/manual/core/rect.rs
src/hub/manual/core/rect.rs
+use std::ops::{Add, AddAssign, Sub, SubAssign};
+
+use crate::core::{Point_, Size_, ValidPointType, ValidSizeType};
+
valid_types!(ValidRectType, i32, f32, f64);
#[repr(C)]
@@ -20,3 +24,99 @@ impl<T: ValidRectType + Default> Default for Rect_<T> {
Self { x: Default::default(), y: Default::default(), width: Default::default(), height: Default::default() }
}
}
+
+impl<P, R> Add<Point_<P>> for Rect_<R>
+ where
+ P: ValidPointType,
+ R: ValidRectType + AddAssign<P>
+{
+ type Output = Rect_<R>;
+
+ fn add(mut self, rhs: Point_<P>) -> Self::Output {
+ self += rhs;
+ self
+ }
+}
+
+impl<P, R> Sub<Point_<P>> for Rect_<R>
+ where
+ P: ValidPointType,
+ R: ValidRectType + SubAssign<P>
+{
+ type Output = Rect_<R>;
+
+ fn sub(mut self, rhs: Point_<P>) -> Self::Output {
+ self -= rhs;
+ self
+ }
+}
+
+impl<S, R> Add<Size_<S>> for Rect_<R>
+ where
+ S: ValidSizeType,
+ R: ValidRectType + AddAssign<S>
+{
+ type Output = Rect_<R>;
+
+ fn add(mut self, rhs: Size_<S>) -> Self::Output {
+ self += rhs;
+ self
+ }
+}
+
+impl<S, R> Sub<Size_<S>> for Rect_<R>
+ where
+ S: ValidSizeType,
+ R: ValidRectType + SubAssign<S>
+{
+ type Output = Rect_<R>;
+
+ fn sub(mut self, rhs: Size_<S>) -> Self::Output {
+ self -= rhs;
+ self
+ }
+}
+
+impl<P, R> AddAssign<Point_<P>> for Rect_<R>
+ where
+ P: ValidPointType,
+ R: ValidRectType + AddAssign<P>
+{
+ fn add_assign(&mut self, rhs: Point_<P>) {
+ self.x += rhs.x;
+ self.y += rhs.y;
+ }
+}
+
+impl<P, R> SubAssign<Point_<P>> for Rect_<R>
+ where
+ P: ValidPointType,
+ R: ValidRectType + SubAssign<P>
+{
+ fn sub_assign(&mut self, rhs: Point_<P>) {
+ self.x -= rhs.x;
+ self.y -= rhs.y;
+ }
+}
+
+impl<S, R> AddAssign<Size_<S>> for Rect_<R>
+ where
+ S: ValidSizeType,
+ R: ValidRectType + AddAssign<S>
+{
+ fn add_assign(&mut self, rhs: Size_<S>) {
+ self.width += rhs.width;
+ self.height += rhs.height;
+ }
+}
+
+impl<S, R> SubAssign<Size_<S>> for Rect_<R>
+ where
+ S: ValidSizeType,
+ R: ValidRectType + SubAssign<S>
+{
+ fn sub_assign(&mut self, rhs: Size_<S>) {
+ self.width -= rhs.width;
+ self.height -= rhs.height;
+ }
+}

ADD
tests/rect.rs
+use opencv::core;
+
+#[test]
+fn rect_add() {
+ let src = core::Rect::new(0, 0, 100, 100);
+
+ // Point
+ let res = core::Rect::new(50, 50, 100, 100);
+ {
+ let src = src.clone();
+ let out = src + core::Point::new(50, 50);
+ assert_eq!(out, res);
+ }
+
+ {
+ let mut out = src.clone();
+ out += core::Point::new(50, 50);
+ assert_eq!(out, res);
+ }
+
+ // Size
+ let res = core::Rect::new(0, 0, 200, 200);
+ {
+ let src = src.clone();
+ let out = src + core::Size::new(100, 100);
+ assert_eq!(out, res);
+ }
+
+ {
+ let mut out = src.clone();
+ out += core::Size::new(100, 100);
+ assert_eq!(out, res);
+ }
+}
+
+#[test]
+fn rect_sub() {
+ let src = core::Rect::new(50, 50, 100, 100);
+
+ // Point
+ let res = core::Rect::new(25, 25, 100, 100);
+ {
+ let src = src.clone();
+ let out = src - core::Point::new(25, 25);
+ assert_eq!(out, res);
+ }
+
+ {
+ let mut out = src.clone();
+ out -= core::Point::new(25, 25);
+ assert_eq!(out, res);
+ }
+
+ // Size
+ let res = core::Rect::new(50, 50, 75, 75);
+ {
+ let src = src.clone();
+ let out = src - core::Size::new(25, 25);
+ assert_eq!(out, res);
+ }
+
+ {
+ let mut out = src.clone();
+ out -= core::Size::new(25, 25);
+ assert_eq!(out, res);
+ }
+}

add +/- operations for Rect_