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
//!
//! A trajectory is a circular buffer containing vectors. It can be seen as the trajectory
//! of a moving point, in this case, the vectors are the position vectors of the point.
//!
//! **Note :** Use the method `push` to add a point to a trajectory.
//! ## Purpose
//! This abstraction aims represent only the last points of a complete trajectory.
//! It's main purpose is to allow drawing a trajectory consistently without allocating
//! new space each time a point is added.
//!
//! ## Conventions
//! * All the trajectories have the same fixed size
//! * Vector space operations can be performed between trajectories
//! * Translation can be performed with a vector using `+` and `-` operators
//! * Scaling can be performed using `*` and `/` operators
//! * When adding a point to a trajectory, the oldest point is erased
//!

use std::fmt;
use std::fmt::Debug;
use std::ops::{Add, AddAssign, Div, DivAssign, Index, IndexMut, Mul, MulAssign, Sub, SubAssign};

use crate::prelude::{Initializer, Reset};
use crate::vector::{Vector2, Vector3, Vector4};
use crate::trajectory::consts::*;

/// 2D trajectory
pub type Trajectory2 = Trajectory<Vector2>;

/// 3D trajectory
pub type Trajectory3 = Trajectory<Vector3>;

/// 4D trajectory
pub type Trajectory4 = Trajectory<Vector4>;


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


    /// Size of all the trajectories
    pub const TRAJECTORY_SIZE: usize = 256;

    /// 2D zero trajectory
    pub const ZEROS_2: Trajectory2 = Trajectory2 {
        positions: [vector::consts::ZEROS_2; TRAJECTORY_SIZE],
        index: 0,
    };

    /// 3D zero trajectory
    pub const ZEROS_3: Trajectory3 = Trajectory3 {
        positions: [vector::consts::ZEROS_3; TRAJECTORY_SIZE],
        index: 0,
    };

    /// 4D zero trajectory
    pub const ZEROS_4: Trajectory4 = Trajectory4 {
        positions: [vector::consts::ZEROS_4; TRAJECTORY_SIZE],
        index: 0,
    };
}

/// Generic structure and documentation for trajectories
#[derive(Copy, Clone)]
pub struct Trajectory<T> {
    positions: [T; TRAJECTORY_SIZE],
    index: usize,
}

impl<T> From<T> for Trajectory<T> where
    T: Copy + Clone
{
    fn from(position: T) -> Self {
        Trajectory::new([position; TRAJECTORY_SIZE], 0)
    }
}

impl<T> From<[T; TRAJECTORY_SIZE]> for Trajectory<T> where
    T: Copy + Clone {
    fn from(positions: [T; TRAJECTORY_SIZE]) -> Self {
        Trajectory::new(positions, 0)
    }
}

impl<T> Trajectory<T> where
    T: Copy + Clone {

    /// Construct a trajectory with given positions and index of the oldest position in the array
    #[inline]
    pub fn new(positions: [T; TRAJECTORY_SIZE], index: usize) -> Trajectory<T> {
        Trajectory { positions, index }
    }

    /// Add a vector to the trajectory
    #[inline]
    pub fn push(&mut self, position: &T) {
        self.positions[self.index] = *position;
        self.index = self.index_offset(1);
    }

    /// Get the positions vectors of the trajectory as array
    #[inline]
    pub fn positions(&self) -> &[T; TRAJECTORY_SIZE] { &self.positions }

    /// Get the position vector at index. The 0 index is the oldest position vector
    #[inline]
    pub fn position(&self, i: usize) -> &T {
        &self.positions[self.index_offset(i)]
    }

    /// Same as `position` but with a mutable reference
    #[inline]
    pub fn position_mut(&mut self, i: usize) -> &mut T {
        &mut self.positions[self.index_offset(i)]
    }

    /// Get the lastly added position vector
    pub fn last(&self) -> &T {
        &self.positions[self.index_offset(TRAJECTORY_SIZE - 1)]
    }

    /// Same as `last` but with a mutable reference
    pub fn last_mut(&mut self) -> &mut T {
        &mut self.positions[self.index_offset(TRAJECTORY_SIZE - 1)]
    }

    #[inline]
    fn index_offset(&self, i: usize) -> usize {
        (i + self.index) % TRAJECTORY_SIZE
    }
}

impl<T> Initializer for Trajectory<T> where
    T: Initializer + Copy + Clone {
    #[inline]
    fn zeros() -> Self {
        Trajectory::new([T::zeros(); TRAJECTORY_SIZE], 0)
    }

    #[inline]
    fn ones() -> Self {
        Trajectory::new([T::ones(); TRAJECTORY_SIZE], 0)
    }
}

impl<T> Reset<T> for Trajectory<T> where
    T: Reset<T> + Copy + Clone {
    #[inline]
    fn reset0(&mut self) -> &mut Self {
        for position in self.positions.iter_mut() {
            position.reset0();
        }
        self
    }

    #[inline]
    fn reset1(&mut self) -> &mut Self {
        for position in self.positions.iter_mut() {
            position.reset1();
        }
        self
    }

    #[inline]
    fn reset(&mut self, val: &T) -> &mut Self {
        for pos in self.positions.iter_mut() {
            pos.reset(val);
        }
        self
    }
}

impl<T> Debug for Trajectory<T> where
    T: Debug + Copy + Clone {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        let mut buffer = String::new();
        for i in TRAJECTORY_SIZE - 32..TRAJECTORY_SIZE {
            buffer += format!("\n{:?}", self.positions[self.index_offset(i)]).as_str();
        }
        write!(f, "{}", buffer)
    }
}

impl<T> Add<Trajectory<T>> for Trajectory<T> where
    T: AddAssign<T> + Copy + Clone {
    type Output = Trajectory<T>;
    #[inline]
    fn add(self, rhs: Trajectory<T>) -> Self::Output {
        let mut ret = self;
        ret += rhs;
        ret
    }
}

impl<T> Add<T> for Trajectory<T> where
    T: AddAssign<T> + Copy + Clone {
    type Output = Trajectory<T>;
    #[inline]
    fn add(self, rhs: T) -> Self::Output {
        let mut ret = self;
        ret += rhs;
        ret
    }
}

impl<T> AddAssign<Trajectory<T>> for Trajectory<T> where
    T: AddAssign<T> + Copy + Clone {
    #[inline]
    fn add_assign(&mut self, rhs: Trajectory<T>) {
        for i in 0..TRAJECTORY_SIZE {
            self.positions[self.index_offset(i)] += rhs.positions[rhs.index_offset(i)];
        }
    }
}

impl<T> AddAssign<T> for Trajectory<T> where
    T: AddAssign<T> + Copy + Clone {
    #[inline]
    fn add_assign(&mut self, rhs: T) {
        for i in 0..TRAJECTORY_SIZE {
            self.positions[i] += rhs;
        }
    }
}

impl<T> Sub<Trajectory<T>> for Trajectory<T> where
    T: SubAssign<T> + Copy + Clone {
    type Output = Trajectory<T>;
    #[inline]
    fn sub(self, rhs: Trajectory<T>) -> Self::Output {
        let mut ret = self;
        ret -= rhs;
        ret
    }
}

impl<T> Sub<T> for Trajectory<T> where
    T: SubAssign<T> + Copy + Clone {
    type Output = Trajectory<T>;
    #[inline]
    fn sub(self, rhs: T) -> Self::Output {
        let mut ret = self;
        ret -= rhs;
        ret
    }
}

impl<T> SubAssign<Trajectory<T>> for Trajectory<T> where
    T: SubAssign<T> + Copy + Clone {
    #[inline]
    fn sub_assign(&mut self, rhs: Trajectory<T>) {
        for i in 0..TRAJECTORY_SIZE {
            self.positions[self.index_offset(i)] -= rhs.positions[rhs.index_offset(i)];
        }
    }
}

impl<T> SubAssign<T> for Trajectory<T> where
    T: SubAssign<T> + Copy + Clone {
    #[inline]
    fn sub_assign(&mut self, rhs: T) {
        for i in 0..TRAJECTORY_SIZE {
            self.positions[i] -= rhs;
        }
    }
}

impl<T> Mul<f64> for Trajectory<T> where
    T: MulAssign<f64> + Copy + Clone {
    type Output = Trajectory<T>;
    #[inline]
    fn mul(self, rhs: f64) -> Self::Output {
        let mut ret = self;
        ret *= rhs;
        ret
    }
}

impl<T> MulAssign<f64> for Trajectory<T> where
    T: MulAssign<f64> + Copy + Clone {
    #[inline]
    fn mul_assign(&mut self, rhs: f64) {
        for i in 0..TRAJECTORY_SIZE {
            self.positions[i] *= rhs;
        }
    }
}

impl<T> Div<f64> for Trajectory<T> where
    T: DivAssign<f64> + Copy + Clone {
    type Output = Trajectory<T>;
    #[inline]
    fn div(self, rhs: f64) -> Self::Output {
        let mut ret = self;
        ret /= rhs;
        ret
    }
}

impl<T> DivAssign<f64> for Trajectory<T> where
    T: DivAssign<f64> + Copy + Clone {
    #[inline]
    fn div_assign(&mut self, rhs: f64) {
        for i in 0..TRAJECTORY_SIZE {
            self.positions[i] /= rhs;
        }
    }
}

impl<T> Index<usize> for Trajectory<T> where
    T: Copy + Clone {
    type Output = T;
    #[inline]
    fn index(&self, index: usize) -> &Self::Output {
        &self.positions[self.index_offset(index)]
    }
}

impl<T> IndexMut<usize> for Trajectory<T> where
    T: Copy + Clone {
    #[inline]
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        &mut self.positions[self.index_offset(index)]
    }
}