na/
traits.rs

1use nalgebra as na;
2use na::*;
3use na::storage::Storage;
4
5
6pub trait JoinVec<T, U>{
7    type Output;
8    fn join(self, u: U) -> Self::Output;
9}
10
11impl<T: super::BaseNum> JoinVec<T,T> for T{
12    type Output = Vector2<T>;
13    #[inline]
14    fn join(self, v: T) -> Vector2<T>{
15        Vector2::new(self, v)
16    }
17}
18
19impl<T: super::BaseNum, S: Storage<T,U2,U1>> JoinVec<T,Vector<T, na::U2, S>> for T{
20    type Output = Vector3<T>;
21    #[inline]
22    fn join(self, v: Vector<T, na::U2, S>) -> Vector3<T>{
23        Vector3::new(self, v[0].clone(), v[1].clone())
24    }
25}
26
27impl<T: super::BaseNum, S: Storage<T,U3,U1>> JoinVec<T,Vector<T, na::U3, S>> for T{
28    type Output = Vector4<T>;
29    #[inline]
30    fn join(self, v: Vector<T, na::U3, S>) -> Vector4<T>{
31        Vector4::new(self, v[0].clone(), v[1].clone(), v[2].clone())
32    }
33}
34
35impl<'a, T, S> JoinVec<T,T> for Matrix<T,na::U1,na::U1,S>
36    where T: super::BaseNum,
37          S: Storage<T, na::U1, na::U1>
38{
39    type Output = Vector2<T>;
40    #[inline]
41    fn join(self, v: T) -> Vector2<T>{
42        Vector2::new(self[0].clone(), v)
43    }
44}
45
46
47
48
49
50impl<'a, T, S> JoinVec<T,T> for Matrix<T,na::U2,na::U1,S>
51    where T: super::BaseNum,
52          S: Storage<T,na::U2,na::U1>
53{
54    type Output = Vector3<T>;
55    #[inline]
56    fn join(self, v: T) -> Vector3<T>{
57        Vector3::new(self[0].clone(), self[1].clone(), v)
58    }
59}
60
61impl<'a, T, S> JoinVec<T,Vector<T, na::U2, S>> for Vector<T, na::U2, S>
62    where T: super::BaseNum,
63          S: Storage<T, na::U2, na::U1>,
64{
65    type Output = Vector4<T>;
66    #[inline]
67    fn join(self, v: Vector<T, na::U2, S>) -> Vector4<T>{
68        Vector4::new(self[0].clone(), self[1].clone(), v[0].clone(), v[1].clone())
69    }
70}
71
72
73
74
75
76impl<'a, T, S> JoinVec<T,T> for Vector<T,na::U3,S>
77    where T: super::BaseNum,
78          S: Storage<T,na::U3,na::U1>
79{
80    type Output = Vector4<T>;
81    #[inline]
82    fn join(self, v: T) -> Vector4<T>{
83        Vector4::new(self[0].clone(), self[1].clone(), self[2].clone(), v)
84    }
85}
86
87impl<'a, T, S1, S2> JoinVec<T,Vector<T,na::U2,S2>> for Vector<T,na::U1,S1>
88    where T: super::BaseNum,
89          S1: Storage<T,na::U1,na::U1>,
90          S2: Storage<T,na::U2,na::U1>,
91{
92    type Output = Vector3<T>;
93    #[inline]
94    fn join(self, v: Vector<T,na::U2,S2>) -> Vector3<T>{
95        Vector3::new(self[0].clone(), v[0].clone(), v[1].clone())
96    }
97}
98
99impl<'a, T, S1, S3> JoinVec<T,Vector<T,na::U3,S3>> for Matrix<T,na::U1,na::U1,S1>
100    where T: super::BaseNum,
101          S1: Storage<T,na::U1,na::U1>,
102          S3: Storage<T,na::U3,na::U1>,
103{
104    type Output = Vector4<T>;
105    #[inline]
106    fn join(self, v: Vector<T,na::U3,S3>) -> Vector4<T>{
107        Vector4::new(self[0].clone(), v[0].clone(), v[1].clone(), v[2].clone())
108    }
109}
110
111pub trait IntoVec<V>{
112    fn into_vec(self) -> V;
113}
114
115impl<V> IntoVec<V> for V{
116    #[inline]
117    fn into_vec(self) -> V{
118        self
119    }
120}
121
122impl<T:Scalar> IntoVec<Vector2<T>> for T{
123    #[inline]
124    fn into_vec(self) -> Vector2<T>{
125        Vector2::new(self.clone(), self)
126    }
127}
128
129impl<T:Scalar> IntoVec<Vector2<T>> for [T;2]{
130    #[inline]
131    fn into_vec(self) -> Vector2<T>{
132        let [x,y]: [T;2] = self;
133        Vector2::new(x, y)
134    }
135}
136
137impl<'a, T:Scalar> IntoVec<Vector2<T>> for &'a[T]{
138    #[inline]
139    fn into_vec(self) -> Vector2<T>{
140        Vector2::new(self[0].clone(), self[1].clone())
141    }
142}
143
144impl<T:Scalar> IntoVec<Vector3<T>> for T{
145    #[inline]
146    fn into_vec(self) -> Vector3<T>{
147        Vector3::new(self.clone(), self.clone(), self)
148    }
149}
150
151impl<T:Scalar> IntoVec<Vector3<T>> for [T;3]{
152    #[inline]
153    fn into_vec(self) -> Vector3<T>{
154        let [x,y,z]: [T;3] = self;
155        Vector3::new(x, y, z)
156    }
157}
158
159impl<'a, T:Scalar> IntoVec<Vector3<T>> for &'a[T]{
160    #[inline]
161    fn into_vec(self) -> Vector3<T>{
162        Vector3::new(self[0].clone(), self[1].clone(), self[2].clone())
163    }
164}
165
166impl<T:Scalar> IntoVec<Vector4<T>> for T{
167    #[inline]
168    fn into_vec(self) -> Vector4<T>{
169        Vector4::new(self.clone(), self.clone(), self.clone(), self)
170    }
171}
172
173impl<T:Scalar> IntoVec<Vector4<T>> for [T;4]{
174    #[inline]
175    fn into_vec(self) -> Vector4<T>{
176        let [x,y,z,w]: [T;4] = self.into();
177        Vector4::new(x,y,z,w)
178    }
179}
180
181impl<'a, T:Scalar> IntoVec<Vector4<T>> for &'a[T]{
182    #[inline]
183    fn into_vec(self) -> Vector4<T>{
184        Vector4::new(self[0].clone(), self[1].clone(), self[2].clone(), self[3].clone())
185    }
186}
187
188
189pub trait JoinPnt<T, U>{
190    type Output;
191    fn join(self, v: U) -> Self::Output;
192}
193
194impl<T: na::Scalar> JoinPnt<T,T> for T{
195    type Output = Point2<T>;
196    #[inline]
197    fn join(self, v: T) -> Point2<T>{
198        Point2::new(self, v)
199    }
200}
201
202impl<T: na::Scalar> JoinPnt<T,Point2<T>> for T{
203    type Output = Point3<T>;
204    #[inline]
205    fn join(self, v: Point2<T>) -> Point3<T>{
206        let [y,z]: [T;2] = v.coords.into();
207        Point3::new(self, y, z)
208    }
209}
210
211impl<T: na::Scalar> JoinPnt<T,T> for Point2<T>{
212    type Output = Point3<T>;
213    #[inline]
214    fn join(self, v: T) -> Point3<T>{
215        let [x,y]: [T;2] = self.coords.into();
216        Point3::new(x, y, v)
217    }
218}
219
220impl<T: na::Scalar> JoinPnt<T,Point3<T>> for T{
221    type Output = Point4<T>;
222    #[inline]
223    fn join(self, v: Point3<T>) -> Point4<T>{
224        let [y,z,w]: [T;3] = v.coords.into();
225        Point4::new(self, y, z, w)
226    }
227}
228
229impl<T: na::Scalar> JoinPnt<T,T> for Point3<T>{
230    type Output = Point4<T>;
231    #[inline]
232    fn join(self, v: T) -> Point4<T>{
233        let [x,y,z]: [T;3] = self.coords.into();
234        Point4::new(x, y, z, v)
235    }
236}
237
238impl<T: na::Scalar> JoinPnt<T,Point2<T>> for Point2<T>{
239    type Output = Point4<T>;
240    #[inline]
241    fn join(self, v: Point2<T>) -> Point4<T>{
242        let [x,y]: [T;2] = self.coords.into();
243        let [z,w]: [T;2] = v.coords.into();
244        Point4::new(x,y,z,w)
245    }
246}
247
248pub trait IntoPnt<V>{
249    fn into_pnt(self) -> V;
250}
251
252impl<V> IntoPnt<V> for V{
253    #[inline]
254    fn into_pnt(self) -> V{
255        self
256    }
257}
258
259impl<T:Scalar> IntoPnt<Point2<T>> for T{
260    #[inline]
261    fn into_pnt(self) -> Point2<T>{
262        Point2::new(self.clone(), self)
263    }
264}
265
266impl<T:Scalar> IntoPnt<Point2<T>> for [T;2]{
267    #[inline]
268    fn into_pnt(self) -> Point2<T>{
269        let [x,y] = self;
270        Point2::new(x, y)
271    }
272}
273
274impl<'a, T:Scalar> IntoPnt<Point2<T>> for &'a[T]{
275    #[inline]
276    fn into_pnt(self) -> Point2<T>{
277        Point2::new(self[0].clone(), self[1].clone())
278    }
279}
280
281impl<T:Scalar> IntoPnt<Point3<T>> for T{
282    #[inline]
283    fn into_pnt(self) -> Point3<T>{
284        Point3::new(self.clone(), self.clone(), self)
285    }
286}
287
288impl<T:Scalar> IntoPnt<Point3<T>> for [T;3]{
289    #[inline]
290    fn into_pnt(self) -> Point3<T>{
291        let [x,y,z] = self;
292        Point3::new(x,y,z)
293    }
294}
295
296impl<'a, T:Scalar> IntoPnt<Point3<T>> for &'a[T]{
297    #[inline]
298    fn into_pnt(self) -> Point3<T>{
299        Point3::new(self[0].clone(), self[1].clone(), self[2].clone())
300    }
301}
302
303impl<T:Scalar> IntoPnt<Point4<T>> for T{
304    #[inline]
305    fn into_pnt(self) -> Point4<T>{
306        Point4::new(self.clone(), self.clone(), self.clone(), self)
307    }
308}
309
310impl<T:Scalar> IntoPnt<Point4<T>> for [T;4]{
311    #[inline]
312    fn into_pnt(self) -> Point4<T>{
313        let [x,y,z,w] = self;
314        Point4::new(x,y,z,w)
315    }
316}
317
318impl<'a, T:Scalar> IntoPnt<Point4<T>> for &'a[T]{
319    #[inline]
320    fn into_pnt(self) -> Point4<T>{
321        Point4::new(self[0].clone(), self[1].clone(), self[2].clone(), self[3].clone())
322    }
323}