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
#![cfg_attr(feature = "strict", deny(warnings))]
#![deny(missing_docs)]
#[macro_use] extern crate serde_derive;
#[macro_use] extern crate clapme;
#[derive(Clone, Copy, Serialize, Deserialize, Debug, ClapMe)]
pub struct Vector3d<T> {
pub x: T,
pub y: T,
pub z: T,
}
impl<T> Vector3d<T> {
pub fn new(x: T, y: T, z: T) -> Vector3d<T> {
Vector3d { x: x, y: y, z: z }
}
pub fn dot<U: Mul<T, Output=X>, X: Add<Output=X>>(self, rhs: Vector3d<U>) -> X {
rhs.x*self.x + rhs.y*self.y + rhs.z*self.z
}
}
use std::ops::Add;
impl<T: Add<T, Output = T>> Add<Vector3d<T>> for Vector3d<T> {
type Output = Vector3d<T>;
fn add(self, rhs: Vector3d<T>) -> Self::Output {
Vector3d::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
}
}
use std::ops::Sub;
impl<T: Sub<T, Output = T>> Sub<Vector3d<T>> for Vector3d<T> {
type Output = Vector3d<T>;
fn sub(self, rhs: Vector3d<T>) -> Self::Output {
Vector3d::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
}
}
use std::ops::Neg;
impl<T: Neg<Output = T>> Neg for Vector3d<T> {
type Output = Vector3d<T>;
fn neg(self) -> Self::Output {
Vector3d::new(-self.x, -self.y, -self.z)
}
}
use std::ops::Mul;
impl<S: Clone, X, T: Mul<S, Output=X>> Mul<S> for Vector3d<T> {
type Output = Vector3d<X>;
fn mul(self, rhs: S) -> Self::Output {
Vector3d::new(self.x * rhs.clone(), self.y * rhs.clone(), self.z * rhs)
}
}
use std::ops::Div;
impl<S: Clone, X, T: Div<S, Output=X>> Div<S> for Vector3d<T> {
type Output = Vector3d<X>;
fn div(self, rhs: S) -> Self::Output {
Vector3d::new(self.x / rhs.clone(), self.y / rhs.clone(), self.z / rhs)
}
}
impl<T: Clone> Vector3d<T> {
pub fn cross<U: Clone + Mul<T, Output=X>,
X: Add<Output=X> + Sub<Output=X>>(self, rhs: Vector3d<U>) -> Vector3d<X> {
Vector3d::new(rhs.z.clone()*self.y.clone() - rhs.y.clone()*self.z.clone(),
rhs.x.clone()*self.z.clone() - rhs.z*self.x.clone(),
rhs.y*self.x - rhs.x.clone()*self.y.clone())
}
}
impl<T: Clone + Mul<T, Output=X>, X: Add<Output=X>> Vector3d<T> {
pub fn norm2(self) -> X {
self.clone().dot(self)
}
}
use std::ops::Index;
impl<T> Index<usize> for Vector3d<T> {
type Output = T;
fn index<'a>(&'a self, index: usize) -> &'a T {
match index {
0 => &self.x,
1 => &self.y,
2 => &self.z,
_ => panic!("Invalid index"),
}
}
}
use std::ops::IndexMut;
impl<T> IndexMut<usize> for Vector3d<T> {
fn index_mut<'a>(&'a mut self, index: usize) -> &'a mut T {
match index {
0 => &mut self.x,
1 => &mut self.y,
2 => &mut self.z,
_ => panic!("Invalid index"),
}
}
}
use std::fmt;
impl<T: fmt::Display> fmt::Display for Vector3d<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "({}, {}, {})", self.x, self.y, self.z)
}
}