add more Rect_ methods
MODIFY
Cargo.toml
Cargo.toml
@@ -17,6 +17,7 @@ travis-ci = { repository = "twistedfall/opencv-rust", branch = "master" }
[dependencies]
lazy_static = "1.0"
libc = "0.2"
+num = "0.2"
slab = "0.4"
[build-dependencies]

MODIFY
src/hub/manual/core/rect.rs
src/hub/manual/core/rect.rs
-use std::ops::{Add, AddAssign, Sub, SubAssign};
+use std::ops::{Add, AddAssign, Mul, Sub, SubAssign};
+use num::Zero;
use crate::core::{Point_, Size_, ValidPointType, ValidSizeType};
@@ -19,6 +20,48 @@ impl<T: ValidRectType> Rect_<T> {
}
}
+impl<T: ValidRectType + ValidPointType> Rect_<T> {
+ #[inline]
+ pub fn tl(&self) -> Point_<T> {
+ Point_::new(self.x, self.y)
+ }
+}
+
+impl<T: ValidRectType + ValidPointType + Add<T, Output=T>> Rect_<T> {
+ #[inline]
+ pub fn br(&self) -> Point_<T> {
+ Point_::new(self.x + self.width, self.y + self.height)
+ }
+}
+
+impl<T: ValidRectType + ValidSizeType> Rect_<T> {
+ #[inline]
+ pub fn size(&self) -> Size_<T> {
+ Size_::new(self.width, self.height)
+ }
+}
+
+impl<T: ValidRectType + Mul<T, Output=T>> Rect_<T> {
+ #[inline]
+ pub fn area(&self) -> T {
+ self.width * self.height
+ }
+}
+
+impl<T: ValidRectType + Zero + PartialOrd> Rect_<T> {
+ #[inline]
+ pub fn empty(&self) -> bool {
+ self.width <= T::zero() || self.height <= T::zero()
+ }
+}
+
+impl<T: ValidRectType + ValidPointType + Add<T, Output=T> + PartialOrd> Rect_<T> {
+ #[inline]
+ pub fn contains(&self, pt: Point_<T>) -> bool {
+ self.x <= pt.x && pt.x < self.x + self.width && self.y <= pt.y && pt.y < self.y + self.height
+ }
+}
+
impl<T: ValidRectType + Default> Default for Rect_<T> {
fn default() -> Self {
Self { x: Default::default(), y: Default::default(), width: Default::default(), height: Default::default() }

MODIFY
tests/rect.rs
tests/rect.rs
use opencv::core;
+use opencv::core::{Rect2d, Point2d, Size2d};
#[test]
fn rect_add() {
@@ -65,3 +66,17 @@ fn rect_sub() {
assert_eq!(out, res);
}
}
+
+#[test]
+fn rect_methods() {
+ let mut rect = Rect2d::new(10., 20., 100., 200.);
+ assert!(!rect.empty());
+ assert_eq!(20000., rect.area());
+ assert_eq!(Point2d::new(10., 20.), rect.tl());
+ assert_eq!(Point2d::new(110., 220.), rect.br());
+ assert_eq!(Size2d::new(100., 200.), rect.size());
+ assert!(rect.contains(Point2d::new(20., 20.)));
+ assert!(!rect.contains(Point2d::new(120., 120.)));
+ rect.width = 0.;
+ assert!(rect.empty());
+}

add more Rect_ methods