1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
use num_traits::{NumCast, NumOps, ToPrimitive, Zero};
use crate::{
core::{Point_, VecN},
opencv_type_simple_generic,
};
#[repr(C)]
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd)]
pub struct Point3_<T> {
pub x: T,
pub y: T,
pub z: T,
}
impl<T> Point3_<T> {
#[inline]
pub fn new(x: T, y: T, z: T) -> Self {
Self { x, y, z }
}
#[inline]
pub fn from_vec3(vec: VecN<T, 3>) -> Self {
let [x, y, z] = vec.0;
Self::new(x, y, z)
}
#[inline]
pub fn from_point(pt: Point_<T>) -> Self
where
T: Zero,
{
Self::new(pt.x, pt.y, T::zero())
}
#[inline]
pub fn cross(self, pt: Self) -> Self
where
T: NumOps + Copy,
{
Self::new(
self.y * pt.z - self.z * pt.y,
self.z * pt.x - self.x * pt.z,
self.x * pt.y - self.y * pt.x,
)
}
#[inline]
pub fn dot(self, pt: Self) -> T
where
T: NumOps,
{
self.x * pt.x + self.y * pt.y + self.z * pt.z
}
#[inline]
pub fn ddot(self, pt: Self) -> f64
where
f64: From<T>,
{
let self_x: f64 = From::from(self.x);
let self_y: f64 = From::from(self.y);
let self_z: f64 = From::from(self.z);
let pt_x: f64 = From::from(pt.x);
let pt_y: f64 = From::from(pt.y);
let pt_z: f64 = From::from(pt.z);
self_x * pt_x + self_y * pt_y + self_z * pt_z
}
#[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);
let self_z: f64 = From::from(self.z);
(self_x.powi(2) + self_y.powi(2) + self_z.powi(2)).sqrt()
}
#[inline]
pub fn to<D: NumCast>(self) -> Option<Point3_<D>>
where
T: ToPrimitive,
{
Some(Point3_::new(D::from(self.x)?, D::from(self.y)?, D::from(self.z)?))
}
#[inline]
pub fn to_vec3(self) -> VecN<T, 3> {
VecN::<_, 3>::from([self.x, self.y, self.z])
}
}
impl<T> From<(T, T, T)> for Point3_<T> {
#[inline]
fn from(s: (T, T, T)) -> Self {
Self::new(s.0, s.1, s.2)
}
}
impl<T> From<VecN<T, 3>> for Point3_<T> {
#[inline]
fn from(s: VecN<T, 3>) -> Self {
Self::from_vec3(s)
}
}
impl<T: Zero> From<Point_<T>> for Point3_<T> {
#[inline]
fn from(s: Point_<T>) -> Self {
Self::from_point(s)
}
}
impl<T> Add for Point3_<T>
where
Self: AddAssign,
{
type Output = Self;
fn add(mut self, rhs: Self) -> Self::Output {
self += rhs;
self
}
}
impl<T> Sub for Point3_<T>
where
Self: SubAssign,
{
type Output = Self;
fn sub(mut self, rhs: Self) -> Self::Output {
self -= rhs;
self
}
}
impl<T> Mul<T> for Point3_<T>
where
Self: MulAssign<T>,
{
type Output = Self;
fn mul(mut self, rhs: T) -> Self::Output {
self *= rhs;
self
}
}
impl<T> Div<T> for Point3_<T>
where
Self: DivAssign<T>,
{
type Output = Self;
fn div(mut self, rhs: T) -> Self::Output {
self /= rhs;
self
}
}
impl<T: AddAssign> AddAssign for Point3_<T> {
fn add_assign(&mut self, rhs: Point3_<T>) {
self.x += rhs.x;
self.y += rhs.y;
self.z += rhs.z;
}
}
impl<T: SubAssign> SubAssign for Point3_<T> {
fn sub_assign(&mut self, rhs: Point3_<T>) {
self.x -= rhs.x;
self.y -= rhs.y;
self.z -= rhs.z;
}
}
impl<T: MulAssign + Copy> MulAssign<T> for Point3_<T> {
fn mul_assign(&mut self, rhs: T) {
self.x *= rhs;
self.y *= rhs;
self.z *= rhs;
}
}
impl<T: DivAssign + Copy> DivAssign<T> for Point3_<T> {
fn div_assign(&mut self, rhs: T) {
self.x /= rhs;
self.y /= rhs;
self.z /= rhs;
}
}
opencv_type_simple_generic! { Point3_<Copy> }