geomath 0.2.4

Stack-allocated maths for geometry, simulation and computer graphics
Documentation
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
//!
//! A point encapsulates position, speed and trajectory.
//!
//! It can be seen as a moving point where we store both the kinematic state of the point
//! and it's trajectory.
//!
//! ## Purpose
//! The main goal of the abstraction is to provide a handler for solving ODE of second order
//! such as the 2nd law of Newton.
//! It also provide some affine geometry features.
//!
//! ## Conventions
//! * The trajectory needs to be updated by the calling code
//! * A vector 2N is seen as an upper part containing the position and a lower part containing a speed
//! * Operations between points changes only position and speed
//! * Operations between points of size N and vectors of size N changes only position
//! * Operations between points of size N and vectors of size 2N changes position and speed
//! * Metric space operations between points are perform between their positions
//! * Equality between points checks at both positions and speed
//!
//! **Note :** The trajectory is never updated, you have to call `update_trajectory` method to
//! add the current position value to the trajectory.
//!
use std::fmt;
use std::fmt::Debug;
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};

use crate::prelude::*;
use crate::trajectory;
use crate::trajectory::Trajectory;
use crate::vector::{self, *};

/// 2D point
pub type Point2 = Point<Vector2>;
/// 3D point
pub type Point3 = Point<Vector3>;
/// 4D point
pub type Point4 = Point<Vector4>;

/// constant points
pub mod consts {
    use crate::vector;
    use super::*;

    /// 2D zero point
    pub const ZEROS_2: Point2 = Point2 {
        position: vector::consts::ZEROS_2,
        speed: vector::consts::ZEROS_2,
        trajectory: trajectory::consts::ZEROS_2,
    };

    /// 3D zero point
    pub const ZEROS_3: Point3 = Point3 {
        position: vector::consts::ZEROS_3,
        speed: vector::consts::ZEROS_3,
        trajectory: trajectory::consts::ZEROS_3,
    };

    /// 4D zero point
    pub const ZEROS_4: Point4 = Point4 {
        position: vector::consts::ZEROS_4,
        speed: vector::consts::ZEROS_4,
        trajectory: trajectory::consts::ZEROS_4,
    };
}

/// Generic structure and documentation for points
#[derive(Copy, Clone)]
pub struct Point<T> {
    pub position: T,
    pub speed: T,
    pub trajectory: Trajectory<T>,
}

impl From<Vector2> for Point2 {
    fn from(vector: Vector2) -> Self {
        Point::new(vector, vector::consts::ZEROS_2)
    }
}

impl From<Vector3> for Point3 {
    fn from(vector: Vector3) -> Self {
        Point::new(vector, vector::consts::ZEROS_3)
    }
}

impl From<Vector4> for Point2 {
    fn from(vector: Vector4) -> Self {
        Point::new(vector.upper(), vector.lower())
    }
}

impl From<Vector6> for Point3 {
    fn from(vector: Vector6) -> Self {
        Point3::new(vector.upper(), vector.lower())
    }
}

impl<T> Point<T> where
    T: Copy + Clone + AddAssign<T> + SubAssign<T> {
    /// Construct a point with given position and speed vectors
    //noinspection RsTypeCheck
    #[inline]
    pub fn new(position: T, speed: T) -> Point<T> {
        Point {
            position,
            speed,
            trajectory: Trajectory::from(position),
        }
    }

    /// Updates trajectory
    ///
    /// The value of `position` is pushed into the trajectory.
    #[inline]
    pub fn update_trajectory(&mut self) -> &mut Self {
        self.trajectory.push(&self.position);
        self
    }

    /// Change the origin
    ///
    /// This method also changes the origin of the trajectory.
    #[inline]
    pub fn reset_origin(&mut self, origin: &Self, old_origin: &Self) -> &mut Self {
        self.position += old_origin.position;
        self.speed += old_origin.speed;
        self.trajectory += old_origin.trajectory;
        self.position -= origin.position;
        self.speed -= origin.speed;
        self.trajectory -= origin.trajectory;
        self
    }
}

impl<T> Initializer for Point<T> where
    T: Initializer + Copy + Clone + AddAssign<T> + SubAssign<T> {
    #[inline]
    fn zeros() -> Self {
        Point::new(T::ones(), T::ones())
    }

    #[inline]
    fn ones() -> Self {
        Point::new(T::ones(), T::ones())
    }
}

impl<T> Reset<Point<T>> for Point<T> where
    T: Reset<T> + Copy + Clone + AddAssign<T> + SubAssign<T> {
    fn reset0(&mut self) -> &mut Self {
        self.position.reset0();
        self.speed.reset0();
        self
    }

    fn reset1(&mut self) -> &mut Self {
        self.position.reset1();
        self.speed.reset1();
        self
    }

    fn reset(&mut self, val: &Point<T>) -> &mut Self {
        self.position = val.position;
        self.speed = val.speed;
        self
    }
}

impl<T> Debug for Point<T> where
    T: Debug + Copy + Clone + AddAssign<T> + SubAssign<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        write!(
            f,
            "position: {:?}\nspeed: {:?}\n",
            self.position,
            self.speed,
        )
    }
}

impl Array<[f64; 4]> for Point2 {
    #[inline]
    fn to_array(&self) -> [f64; 4] {
        [self.position.x, self.position.y, self.speed.x, self.speed.y]
    }

    #[inline]
    fn set_array(&mut self, array: &[f64; 4]) -> &mut Self {
        self.position.x = array[0];
        self.position.y = array[1];
        self.speed.x = array[2];
        self.speed.y = array[3];
        self
    }
}

impl Array<[f64; 6]> for Point3 {
    #[inline]
    fn to_array(&self) -> [f64; 6] {
        [self.position.x, self.position.y, self.position.z, self.speed.x, self.speed.y, self.speed.z]
    }

    #[inline]
    fn set_array(&mut self, array: &[f64; 6]) -> &mut Self {
        self.position.x = array[0];
        self.position.y = array[1];
        self.position.z = array[2];
        self.speed.x = array[3];
        self.speed.y = array[4];
        self.speed.z = array[5];
        self
    }
}

impl Vector<Vector4> for Point2 {
    #[inline]
    fn to_vector(&self) -> Vector4 {
        Vector4::concat(&self.position, &self.speed)
    }

    #[inline]
    fn set_vector(&mut self, vector: &Vector4) -> &mut Self {
        self.position.x = vector.x;
        self.position.y = vector.y;
        self.speed.x = vector.z;
        self.speed.y = vector.w;
        self
    }
}

impl Vector<Vector6> for Point3 {
    #[inline]
    fn to_vector(&self) -> Vector6 {
        Vector6::concat(&self.position, &self.speed)
    }

    #[inline]
    fn set_vector(&mut self, vector: &Vector6) -> &mut Self {
        self.position.x = vector.x;
        self.position.y = vector.y;
        self.position.z = vector.z;
        self.speed.x = vector.u;
        self.speed.y = vector.v;
        self.speed.z = vector.w;
        self
    }
}

impl<T> Metric for Point<T> where
    T: Metric + Copy + Clone + AddAssign<T> + SubAssign<T> {
    #[inline]
    fn dot(&self, other: &Self) -> f64 {
        self.position.dot(&other.position)
    }

    #[inline]
    fn distance2(&self, other: &Self) -> f64 {
        self.position.distance2(&other.position)
    }

    #[inline]
    fn distance(&self, other: &Self) -> f64 {
        self.position.distance(&other.position)
    }

    #[inline]
    fn magnitude2(&self) -> f64 {
        self.position.magnitude2()
    }

    #[inline]
    fn magnitude(&self) -> f64 {
        self.position.magnitude()
    }

    fn set_normalized(&mut self) -> &mut Self {
        self.position.set_normalized();
        self
    }
}

impl<T> PartialEq for Point<T> where
    T: PartialEq<T> + Copy + Clone + AddAssign<T> + SubAssign<T> {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.position == other.position && self.speed == other.speed
    }

    #[inline]
    fn ne(&self, other: &Self) -> bool {
        self.position != other.position || self.speed != other.speed
    }
}

impl<T> Add<T> for Point<T> where
    T: AddAssign<T> + Copy + Clone + SubAssign<T> {
    type Output = Point<T>;

    #[inline]
    fn add(self, rhs: T) -> Self::Output {
        let mut ret = self;
        ret += rhs;
        ret
    }
}

impl<T> Add<Point<T>> for Point<T> where
    T: AddAssign<T> + Copy + Clone + SubAssign<T> {
    type Output = Point<T>;

    #[inline]
    fn add(self, rhs: Point<T>) -> Self::Output {
        let mut ret = self;
        ret += rhs;
        ret
    }
}

impl Add<Vector4> for Point2 {
    type Output = Point2;

    #[inline]
    fn add(self, rhs: Vector4) -> Self::Output {
        let mut ret = self;
        ret += rhs;
        ret
    }
}

impl Add<Vector6> for Point3 {
    type Output = Point3;

    #[inline]
    fn add(self, rhs: Vector6) -> Self::Output {
        let mut ret = self;
        ret += rhs;
        ret
    }
}

impl<T> AddAssign<T> for Point<T> where
    T: AddAssign<T> + Copy + Clone + SubAssign<T> {
    #[inline]
    fn add_assign(&mut self, rhs: T) {
        self.position += rhs;
    }
}

impl<T> AddAssign<Point<T>> for Point<T> where
    T: AddAssign<T> + Copy + Clone + SubAssign<T> {
    #[inline]
    fn add_assign(&mut self, rhs: Point<T>) {
        self.position += rhs.position;
        self.speed += rhs.speed;
    }
}

impl AddAssign<Vector4> for Point2 {
    #[inline]
    fn add_assign(&mut self, rhs: Vector4) {
        self.position.x += rhs.x;
        self.position.y += rhs.y;
        self.speed.x += rhs.z;
        self.speed.y += rhs.w;
    }
}

impl AddAssign<Vector6> for Point3 {
    #[inline]
    fn add_assign(&mut self, rhs: Vector6) {
        self.position.x += rhs.x;
        self.position.y += rhs.y;
        self.position.z += rhs.z;
        self.speed.x += rhs.u;
        self.speed.y += rhs.v;
        self.speed.z += rhs.w;
    }
}

impl<T> Sub<T> for Point<T> where
    T: SubAssign<T> + Copy + Clone + AddAssign<T> {
    type Output = Point<T>;

    #[inline]
    fn sub(self, rhs: T) -> Self::Output {
        let mut ret = self;
        ret -= rhs;
        ret
    }
}

impl<T> Sub<Point<T>> for Point<T> where
    T: SubAssign<T> + Copy + Clone + AddAssign<T> {
    type Output = Point<T>;

    #[inline]
    fn sub(self, rhs: Point<T>) -> Self::Output {
        let mut ret = self;
        ret -= rhs;
        ret
    }
}

impl Sub<Vector4> for Point2 {
    type Output = Point2;

    #[inline]
    fn sub(self, rhs: Vector4) -> Self::Output {
        let mut ret = self;
        ret -= rhs;
        ret
    }
}

impl Sub<Vector6> for Point3 {
    type Output = Point3;

    #[inline]
    fn sub(self, rhs: Vector6) -> Self::Output {
        let mut ret = self;
        ret -= rhs;
        ret
    }
}

impl<T> SubAssign<T> for Point<T> where
    T: SubAssign<T> + Copy + Clone + AddAssign<T> {
    #[inline]
    fn sub_assign(&mut self, rhs: T) {
        self.position -= rhs;
    }
}

impl<T> SubAssign<Point<T>> for Point<T> where
    T: SubAssign<T> + Copy + Clone + AddAssign<T> {
    #[inline]
    fn sub_assign(&mut self, rhs: Point<T>) {
        self.position -= rhs.position;
        self.speed -= rhs.speed;
    }
}

impl SubAssign<Vector4> for Point2 {
    #[inline]
    fn sub_assign(&mut self, rhs: Vector4) {
        self.position.x -= rhs.x;
        self.position.y -= rhs.y;
        self.speed.x -= rhs.z;
        self.speed.y -= rhs.w;
    }
}

impl SubAssign<Vector6> for Point3 {
    #[inline]
    fn sub_assign(&mut self, rhs: Vector6) {
        self.position.x -= rhs.x;
        self.position.y -= rhs.y;
        self.position.z -= rhs.z;
        self.speed.x -= rhs.u;
        self.speed.y -= rhs.v;
        self.speed.z -= rhs.w;
    }
}

impl<T> Mul<f64> for Point<T> where
    T: MulAssign<f64> + Copy + Clone + AddAssign<T> + SubAssign<T> {
    type Output = Point<T>;

    #[inline]
    fn mul(self, rhs: f64) -> Self::Output {
        let mut output = self;
        output *= rhs;
        output
    }
}

impl<T> MulAssign<f64> for Point<T> where
    T: MulAssign<f64> + Copy + Clone + AddAssign<T> + SubAssign<T> {
    #[inline]
    fn mul_assign(&mut self, rhs: f64) {
        self.position *= rhs;
        self.speed *= rhs;
    }
}

impl<T> Div<f64> for Point<T> where
    T: DivAssign<f64> + Copy + Clone + AddAssign<T> + SubAssign<T> {
    type Output = Point<T>;

    #[inline]
    fn div(self, rhs: f64) -> Self::Output {
        let mut output = self;
        output /= rhs;
        output
    }
}

impl<T> DivAssign<f64> for Point<T> where
    T: DivAssign<f64> + Copy + Clone + AddAssign<T> + SubAssign<T> {
    #[inline]
    fn div_assign(&mut self, rhs: f64) {
        self.position /= rhs;
        self.speed /= rhs;
    }
}