add more Point_ methods
MODIFY
src/manual/core/point.rs
src/manual/core/point.rs
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
-use crate::manual::core::size::ValidSizeType;
+use num::{NumCast, ToPrimitive};
+
+use crate::core::{Rect_, Size_, ValidRectType, ValidSizeType};
valid_types!(ValidPointType, i32, i64, f32, f64);
#[repr(C)]
#[derive(Copy, Clone, Debug, PartialEq)]
-/// [docs.opencv.org](https://docs.opencv.org/3.4.6/db/d4e/classcv_1_1Point__.html)
+/// [docs.opencv.org](https://docs.opencv.org/master/db/d4e/classcv_1_1Point__.html)
pub struct Point_<T: ValidPointType> {
pub x: T,
pub y: T,
}
impl<T: ValidPointType> Point_<T> {
+ #[inline]
pub fn new(x: T, y: T) -> Self {
Self { x, y }
}
+
+ #[inline]
+ pub fn from_size(sz: Size_<T>) -> Self where T: ValidSizeType {
+ Self { x: sz.width, y: sz.height }
}
-impl<T: ValidPointType + Mul<Output=T> + Add<Output=T>> Point_<T>
- where
- f64: From<T>
-{
- pub fn norm(&self) -> f64 {
- f64::from(self.x * self.x + self.y * self.y).sqrt()
+ #[inline]
+ pub fn cross(self, pt: Point_<T>) -> f64 where f64: From<T> {
+ let self_x: f64 = From::from(self.x);
+ let self_y: f64 = From::from(self.y);
+ let pt_x: f64 = From::from(pt.x);
+ let pt_y: f64 = From::from(pt.y);
+ self_x * pt_y - self_y * pt_x
}
+
+ #[inline]
+ pub fn dot(self, pt: Point_<T>) -> T where T: Mul<Output=T> + Add<Output=T> {
+ self.x * pt.x + self.y * pt.y
}
-impl<T: ValidPointType + ValidSizeType> Point_<T> {
- pub fn from_size(sz: super::Size_<T>) -> Self {
- Self { x: sz.width, y: sz.height }
+ #[inline]
+ pub fn ddot(self, pt: Point_<T>) -> f64 where f64: From<T> {
+ let self_x: f64 = From::from(self.x);
+ let self_y: f64 = From::from(self.y);
+ let pt_x: f64 = From::from(pt.x);
+ let pt_y: f64 = From::from(pt.y);
+ self_x * pt_x + self_y * pt_y
+ }
+
+ #[inline]
+ pub fn inside(self, rect: Rect_<T>) -> bool where T: ValidRectType + Add<Output=T> + PartialOrd {
+ rect.contains(self)
+ }
+
+ #[inline]
+ pub fn norm(self) -> f64 where f64: From<T> {
+ let self_x: f64 = From::from(self.x);
+ let self_y: f64 = From::from(self.y);
+ (self_x.powi(2) + self_y.powi(2)).sqrt()
+ }
+
+ #[inline]
+ pub fn to<D: ValidPointType + NumCast>(self) -> Option<Point_<D>> where T: ToPrimitive {
+ Some(Point_ { x: D::from(self.x)?, y: D::from(self.y)? })
}
}

MODIFY
tests/point.rs
tests/point.rs
-use opencv::core::Point;
+use opencv::core::{Point, Point2d, Point2f, Point2l, Size2l, Point2i};
#[test]
fn point_add() {
@@ -38,18 +38,18 @@ fn point_sub() {
#[test]
fn point_mul() {
- let src = Point::new(50, 50);
+ let src = Point2f::new(50., 50.);
- let res = Point::new(100, 100);
+ let res = Point2f::new(100., 100.);
{
let src = src.clone();
- let out = src * 2;
+ let out = src * 2.;
assert_eq!(out, res);
}
{
let mut out = src.clone();
- out *= 2;
+ out *= 2.;
assert_eq!(out, res);
}
}
@@ -72,8 +72,31 @@ fn point_div() {
}
}
+#[test]
+fn point_constructor() {
+ let r = Point2l::from_size(Size2l::new(1, 2));
+ assert_eq!(1, r.x);
+ assert_eq!(2, r.y);
+}
+
#[test]
fn point_methods() {
let pt = Point::new(2, 2);
assert_eq!(2.8284271247461903, pt.norm());
+
+ assert_eq!(0., pt.cross(Point::new(2, 2)));
+ assert_eq!(0., pt.cross(Point::new(-4, -4)));
+ assert_eq!(16., pt.cross(Point::new(-4, 4)));
+
+ assert_eq!(8, pt.dot(Point::new(2, 2)));
+ assert_eq!(8., pt.ddot(Point::new(2, 2)));
+}
+
+#[test]
+fn point_conv() {
+ let ptf = Point2d::new(1.2, 2.3);
+ assert_eq!(Point2i::new(1, 2), ptf.to::<i32>().unwrap());
+ assert_eq!(Point2f::new(1.2, 2.3), ptf.to::<f32>().unwrap());
+ let pti = Point2i::new(1, 2);
+ assert_eq!(Point2f::new(1., 2.), pti.to::<f32>().unwrap());
}

add more Point_ methods