point.rs
3.58 KB
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
//
// Basic geometric things...
//
// Georg Hopp <georg@steffers.org>
//
// Copyright © 2019 Georg Hopp
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
use std::fmt::Debug;
use std::ops::{Add, Div, Mul, Neg, Sub};
use crate::math::transform::{TMatrix, Transformable};
use crate::math::trigonometry::Trig;
use crate::math::vector::Vector;
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct Point<T>(pub Vector<T>, T)
where T: Add + Sub + Neg + Mul + Div + PartialEq + Copy + Trig;
impl<T> Point<T>
where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
+ Mul<Output = T> + Div<Output = T>
+ PartialEq + Trig + Copy + From<i32> {
pub fn new(x :T, y :T, z :T) -> Self {
Self(Vector(x, y, z), 1.into())
}
}
impl<T> Add for Point<T>
where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
+ Mul<Output = T> + Div<Output = T>
+ PartialEq + Trig + Copy {
type Output = Self;
fn add(self, other :Self) -> Self {
let Point(v1, w1) = self;
let Point(v2, w2) = other;
Self(v1 + v2, w1 + w2)
}
}
impl<T> Neg for Point<T>
where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
+ Mul<Output = T> + Div<Output = T>
+ PartialEq + Trig + Copy {
type Output = Self;
fn neg(self) -> Self {
let Point(v, w) = self;
Self(-v, -w)
}
}
impl<T> Sub for Point<T>
where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
+ Mul<Output = T> + Div<Output = T>
+ PartialEq + Trig + Copy {
type Output = Self;
fn sub(self, other :Self) -> Self {
self + -other
}
}
impl<T> Mul for Point<T>
where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
+ Mul<Output = T> + Div<Output = T>
+ PartialEq + Trig + Copy + From<i32> {
type Output = Self;
fn mul(self, other :Self) -> Self {
let a :Vector<T> = self.into();
let b :Vector<T> = other.into();
Point(a * b, 1.into())
}
}
impl<T> From<Vector<T>> for Point<T>
where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
+ Mul<Output = T> + Div<Output = T>
+ PartialEq + Trig + Copy + From<i32> {
fn from(v :Vector<T>) -> Self {
Point(v, 1.into())
}
}
impl<T> Into<Vector<T>> for Point<T>
where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
+ Mul<Output = T> + Div<Output = T>
+ PartialEq + Trig + Copy + From<i32> {
fn into(self) -> Vector<T> {
let Point(v, w) = self;
if w == 0.into() {
v
} else {
v.mul(&w.recip())
}
}
}
impl<T> Transformable<T> for Point<T>
where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
+ Mul<Output = T> + Div<Output = T>
+ PartialEq + Debug + Trig + Copy + From<i32> {
fn transform(&self, m :&TMatrix<T>) -> Self {
let Point(v, w) = *self;
let (v, w) = m.apply(&v, w);
if w == 0.into() {
v.into()
} else {
v.mul(&w.recip()).into()
}
}
}