mapf 0.3.0

Base traits and utilities for multi-agent planning
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
/*
 * Copyright (C) 2022 Open Source Robotics Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
*/

use super::{timed::TimeCmp, Duration, InterpError, Motion, TimePoint, Waypoint};
use cached::{Cached, UnboundCache};
use sorted_vec::{FindOrInsert, SortedSet};
use std::cell::RefCell;
use std::rc::Rc;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FindWaypoint {
    /// The requested time is exactly on the waypoint at this index
    Exact(usize),

    /// The requested time is approaching the waypoint of this index. This is
    /// always greater than 0. If the time point was less than the initial time
    /// of the trajectory then BeforeStart will be returned instead.
    Approaching(usize),

    /// The requested time is before the start of the trajectory
    BeforeStart,

    /// The requested time is after the trajectory is finished
    AfterFinish,
}

#[derive(Clone, Copy, PartialEq, Eq)]
pub enum RemovalError {
    /// The specified index or range is outside the index bounds of the
    /// trajectory.
    OutOfBounds,

    /// The requested removal would leave the trajectory with fewer than 2
    /// waypoints.
    Depleting,
}

#[derive(Clone, Copy, PartialEq, Eq)]
pub enum MutateError {
    /// The requested index is out of the bounds of the trajectory.
    OutOfBounds,

    /// The time of the waypoint was shifted too high or too low, which would
    /// cause its order within the trajectory to change. Its time value has been
    /// changed back to its original value.
    InvalidTimeChange,
}

#[derive(Clone, PartialEq, Eq)]
pub struct Trajectory<W: Waypoint> {
    waypoints: SortedSet<TimeCmp<W>>,
    indefinite_initial_time: bool,
    indefinite_finish_time: bool,
}

impl<W: Waypoint> Trajectory<W> {
    /// Create a new trajectory, starting with the given endpoints. If the
    /// endpoints have the same time value then this will return an Err.
    pub fn new(start: W, finish: W) -> Result<Self, ()> {
        if start.time() == finish.time() {
            return Result::Err(());
        }

        let mut result = Self {
            waypoints: SortedSet::new(),
            indefinite_initial_time: false,
            indefinite_finish_time: false,
        };

        result.waypoints.push(TimeCmp(start));
        result.waypoints.push(TimeCmp(finish));
        return Result::Ok(result);
    }

    pub fn with_indefinite_initial_time(mut self, value: bool) -> Self {
        self.indefinite_initial_time = value;
        self
    }

    pub fn set_indefinite_initial_time(&mut self, value: bool) {
        self.indefinite_initial_time = value;
    }

    pub fn has_indefinite_initial_time(&self) -> bool {
        self.indefinite_initial_time
    }

    pub fn with_indefinite_finish_time(mut self, value: bool) -> Self {
        self.indefinite_finish_time = value;
        self
    }

    pub fn set_indefinite_finish_time(&mut self, value: bool) {
        self.indefinite_finish_time = value;
    }

    pub fn has_indefinite_finish_time(&self) -> bool {
        self.indefinite_finish_time
    }

    /// Create a new trajectory that holds at a waypoint until a certain time.
    /// If the finish time is equal to the start time, then this will return an
    /// Err.
    pub fn hold(from: W, until: TimePoint) -> Result<Self, ()> {
        if from.time() == until {
            return Result::Err(());
        }

        let mut finish = from.clone();
        finish.set_time(until);
        return Self::new(from, finish);
    }

    /// Drains elements out of the given iterator type and constructs a
    /// Trajectory with them. If the final number of elements that would be in
    /// the trajectory is less than 2, then this function returns an Err.
    pub fn from_iter<I: std::iter::IntoIterator<Item = W>>(iter: I) -> Result<Self, ()> {
        let mut result = Self {
            waypoints: SortedSet::new(),
            indefinite_initial_time: false,
            indefinite_finish_time: false,
        };
        for element in iter {
            result.waypoints.push(TimeCmp(element));
        }

        if result.waypoints.len() < 2 {
            return Result::Err(());
        } else {
            return Result::Ok(result);
        }
    }

    /// Attemtps to insert the waypoint into this trajectory. If a waypoint
    /// already exists with the same time value as the waypoint, then this
    /// returns an error with the index of the existing waypoint.
    pub fn insert(&mut self, waypoint: W) -> Result<usize, usize> {
        match self.waypoints.find_or_push(TimeCmp(waypoint)) {
            FindOrInsert::Found(index) => Result::Err(index),
            FindOrInsert::Inserted(index) => Result::Ok(index),
        }
    }

    /// Inserts the waypoint into this trajectory, or if the trajectory already
    /// has a waypoint at an equivalent time, then the value of this waypoint
    /// gets assigned to the old one.
    pub fn insert_or_assign(&mut self, waypoint: W) -> usize {
        return self.waypoints.replace(TimeCmp(waypoint)).0;
    }

    /// Attempt to remove the waypoint at the specified index. If the erasure
    /// would leave less than 2 waypoints in the trajectory, then this function
    /// has no effect and instead returns an Err.
    pub fn remove_index(&mut self, index: usize) -> Result<(), RemovalError> {
        if self.waypoints.len() <= 2 {
            return Result::Err(RemovalError::Depleting);
        }

        if index >= self.waypoints.len() {
            return Result::Err(RemovalError::OutOfBounds);
        }

        self.waypoints.remove_index(index);
        return Result::Ok(());
    }

    /// Find the segment of the trajectory that matches this point in time.
    pub fn find(&self, time: &TimePoint) -> FindWaypoint {
        match self
            .waypoints
            .binary_search_by(|x| x.partial_cmp(time).unwrap())
        {
            Result::Ok(index) => return FindWaypoint::Exact(index),
            Result::Err(index) => {
                if index == 0 {
                    return FindWaypoint::BeforeStart;
                } else if index == self.waypoints.len() {
                    return FindWaypoint::AfterFinish;
                } else {
                    return FindWaypoint::Approaching(index);
                }
            }
        }
    }

    /// Simultaneously adjust the times of all the waypoints in the trajectory
    pub fn adjust_times(&mut self, by: Duration) {
        // SAFETY: Uniformly changing the times of all the waypoints inside the
        // trajectory cannot change their relative ordering.
        unsafe {
            let vec = self.waypoints.get_unchecked_mut_vec();
            for element in vec.iter_mut() {
                let new_time = element.0.time() + by;
                element.0.set_time(new_time);
            }
        }
    }

    /// Get the waypoint at the requested index if it is available, otherwise
    /// get None.
    pub fn get(&self, index: usize) -> Option<&W> {
        // TODO(@mxgrey): Investigate how SliceIndex can be used here. The TimeCmp
        // wrapper complicates this.
        return self.waypoints.get(index).map(|x| &x.0);
    }

    /// Get the time duration of the trajectory. If the start or finish are
    pub fn duration(&self) -> Option<Duration> {
        if let (Some(initial), Some(finish)) = (self.initial_time(), self.finish_time()) {
            Some(finish - initial)
        } else {
            None
        }
    }

    pub fn motion_duration(&self) -> Duration {
        self.finish_motion().time() - self.initial_motion().time()
    }

    /// Trajectories always have at least two values, so we can always get the
    /// first waypoint.
    pub fn initial_motion(&self) -> &W {
        &self.waypoints.first().unwrap().0
    }

    pub fn finish_motion(&self) -> &W {
        &self.waypoints.last().unwrap().0
    }

    /// Get the time that the trajectory starts.
    pub fn initial_time(&self) -> Option<TimePoint> {
        if self.indefinite_initial_time {
            None
        } else {
            Some(self.initial_motion_time())
        }
    }

    pub fn initial_motion_time(&self) -> TimePoint {
        self.initial_motion().time()
    }

    /// Get the time that the trajectory finishes.
    pub fn finish_time(&self) -> Option<TimePoint> {
        if self.indefinite_finish_time {
            None
        } else {
            Some(self.finish_motion_time())
        }
    }

    pub fn finish_motion_time(&self) -> TimePoint {
        self.finish_motion().time()
    }

    /// Make changes to the waypoint at a specified index. If a change is made
    /// to the waypoint's time that would cause its order within the vector to
    /// change, then its time value will be reverted back to the original and
    /// an error will be returned.
    pub fn mutate_waypoint<F: FnOnce(&mut W)>(
        &mut self,
        index: usize,
        f: F,
    ) -> Result<(), MutateError> {
        if index >= self.waypoints.len() {
            return Result::Err(MutateError::OutOfBounds);
        }

        let mut lower_bound_opt = Option::None;
        let mut upper_bound_opt = Option::None;

        // SAFETY: All accesses of the vec are done within the checked bounds,
        // and we ensure that changes to the time value of the waypoint will
        // keep it within its current location in the trajectory.
        unsafe {
            let vec = self.waypoints.get_unchecked_mut_vec();
            if index > 0 {
                lower_bound_opt = Some(vec.get_unchecked(index - 1).0.time().clone());
            }

            if index < vec.len() - 1 {
                upper_bound_opt = Some(vec.get_unchecked(index + 1).0.time().clone());
            }

            let wp = vec.get_unchecked_mut(index);
            let original_time = wp.0.time().clone();

            f(&mut wp.0);

            if let Some(lower_bound) = lower_bound_opt {
                if wp.0.time() <= lower_bound {
                    wp.0.set_time(original_time);
                    return Result::Err(MutateError::InvalidTimeChange);
                }
            }

            if let Some(upper_bound) = upper_bound_opt {
                if wp.0.time() >= upper_bound {
                    wp.0.set_time(original_time);
                    return Result::Err(MutateError::InvalidTimeChange);
                }
            }
        }

        return Result::Ok(());
    }

    /// Get the number of Waypoint elements in the trajectory.
    pub fn len(&self) -> usize {
        return self.waypoints.len();
    }

    /// Reserve space in the trajectory for `additional` more Waypoints.
    /// This will not change the length of the trajectory, but it can improve
    /// performance by preventing redundant memory allocations if the trajectory
    /// needs to grow much further.
    pub fn reserve(&mut self, additional: usize) {
        self.waypoints.reserve(additional);
    }

    /// Returns the number of Waypoints that the trajectory can hold without
    /// reallocating.
    pub fn capacity(&self) -> usize {
        return self.waypoints.capacity();
    }

    /// Get a motion for this trajectory
    pub fn motion<'a>(&'a self) -> TrajectoryMotion<'a, W> {
        return TrajectoryMotion {
            trajectory: self,
            motion_cache: RefCell::new(UnboundCache::new()),
        };
    }

    /// Iterate through this trajectory.
    ///
    /// Note: We return a slice whose item is wrapped in the TimeCmp newtype.
    /// This makes getting the actual waypoint out of the iterator slightly less
    /// convenient because you will need to dereference it, but it allows us to
    /// provide all the functionality of a slice without any custom
    /// implementations and without any unsafe blocks.
    pub fn iter(&self) -> TrajectoryIter<'_, W> {
        TrajectoryIter::new(self, self.initial_motion_time(), None)
    }

    /// Iterate through this trajectory, starting at the requested time.
    ///
    /// If the requested time is in between two waypoints, the iterator will
    /// begin with the waypoint that comes immediately before the requested
    /// time.
    ///
    /// If the requested time is before the time of the trajectory's initial
    /// waypoint and the trajectory has an indefinite initial time, then the
    /// iterator will begin with a waypoint that is a clone of the initial
    /// waypoint but with the input `time` as its time value.
    ///
    /// If the trajectory has a definite initial time which comes after the
    /// requested time, then the iterator will begin with the first waypoint
    /// in the trajectory.
    pub fn iter_from(&self, time: TimePoint) -> TrajectoryIter<'_, W> {
        TrajectoryIter::new(self, time, None)
    }

    pub fn iter_range(&self, from_time: TimePoint, to_time: TimePoint) -> TrajectoryIter<'_, W> {
        TrajectoryIter::new(self, from_time, Some(to_time))
    }
}

impl<W: Waypoint> std::fmt::Debug for Trajectory<W> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.debug_struct("Trajectory")
            .field("indefinite_initial_time", &self.indefinite_initial_time)
            .field("indefinite_finish_time", &self.indefinite_finish_time)
            .field("waypoints", &DebugWaypoints(&self.waypoints))
            .finish()
    }
}

struct DebugWaypoints<'a, W: Waypoint>(&'a SortedSet<TimeCmp<W>>);
impl<'a, W: Waypoint> std::fmt::Debug for DebugWaypoints<'a, W> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut builder = f.debug_list();
        for wp in self.0.iter() {
            builder.entry(&wp.0);
        }
        builder.finish()
    }
}

impl<W: Waypoint> std::ops::Deref for Trajectory<W> {
    type Target = [TimeCmp<W>];

    fn deref(&self) -> &Self::Target {
        &self.waypoints
    }
}

pub struct TrajectoryMotion<'a, W: Waypoint> {
    trajectory: &'a Trajectory<W>,
    // TODO(@mxgrey): We could probably use references with explicit lifetimes
    // instead of Rc.
    motion_cache: RefCell<UnboundCache<usize, Rc<W::Motion>>>,
}

impl<'a, W: Waypoint> TrajectoryMotion<'a, W> {
    fn find_motion_segment(&self, time: &TimePoint) -> Result<MotionSegment<W>, InterpError> {
        match self.trajectory.find(time) {
            FindWaypoint::Exact(index) => {
                if index == 0 {
                    return Ok(MotionSegment::Interp(self.get_motion_segment(index + 1)));
                } else {
                    return Ok(MotionSegment::Interp(self.get_motion_segment(index)));
                }
            }
            FindWaypoint::Approaching(index) => {
                return Ok(MotionSegment::Interp(self.get_motion_segment(index)));
            }
            FindWaypoint::BeforeStart => {
                if self.trajectory.has_indefinite_initial_time() {
                    return Ok(MotionSegment::Holding(
                        self.trajectory.initial_motion().position(),
                    ));
                }
            }
            FindWaypoint::AfterFinish => {
                if self.trajectory.has_indefinite_finish_time() {
                    return Ok(MotionSegment::Holding(
                        self.trajectory.finish_motion().position(),
                    ));
                }
            }
        }

        Err(InterpError::OutOfBounds {
            range: [
                self.trajectory.initial_motion_time(),
                self.trajectory.finish_motion_time(),
            ],
            request: *time,
        })
    }

    fn get_motion_segment(&self, index: usize) -> Rc<W::Motion> {
        return self
            .motion_cache
            .borrow_mut()
            .cache_get_or_set_with(index, || {
                // SAFETY: This should only be called by find_motion_segment
                // which should only call this function with an index greater
                // than zero and less than the length of the underlying vec.
                unsafe {
                    let wp0 = self.trajectory.get_unchecked(index - 1);
                    let wp1 = self.trajectory.get_unchecked(index);
                    return Rc::new(wp0.interpolate(wp1));
                }
            })
            .clone();
    }
}

enum MotionSegment<W: Waypoint> {
    Interp(Rc<W::Motion>),
    Holding(W::Position),
}

impl<'a, W: Waypoint> Motion<W::Position, W::Velocity> for TrajectoryMotion<'a, W> {
    fn compute_position(&self, time: &TimePoint) -> Result<W::Position, InterpError> {
        match self.find_motion_segment(time)? {
            MotionSegment::Interp(motion) => motion.compute_position(time),
            MotionSegment::Holding(p) => Ok(p),
        }
    }

    fn compute_velocity(&self, time: &TimePoint) -> Result<W::Velocity, InterpError> {
        match self.find_motion_segment(time)? {
            MotionSegment::Interp(motion) => motion.compute_velocity(time),
            MotionSegment::Holding(_) => Ok(W::zero_velocity()),
        }
    }
}

pub struct TrajectoryIter<'a, W: Waypoint> {
    trajectory: &'a Trajectory<W>,
    next_element: TrajectoryIterNext,
    begin: TimePoint,
    until: Option<TimePoint>,
}

impl<'a, W: Waypoint> TrajectoryIter<'a, W> {
    pub fn pairs(self) -> TrajectoryIterPairs<'a, W> {
        TrajectoryIterPairs {
            base: self,
            previous: None,
        }
    }

    fn new(trajectory: &'a Trajectory<W>, begin: TimePoint, until: Option<TimePoint>) -> Self {
        let next_element = match trajectory.find(&begin) {
            FindWaypoint::BeforeStart => {
                if trajectory.indefinite_initial_time {
                    TrajectoryIterNext::PreInitial(begin)
                } else {
                    TrajectoryIterNext::Index(0)
                }
            }
            FindWaypoint::Exact(index) => TrajectoryIterNext::Index(index),
            FindWaypoint::Approaching(index) => TrajectoryIterNext::Index(index - 1),
            FindWaypoint::AfterFinish => TrajectoryIterNext::StartPostFinish(begin),
        };

        Self {
            trajectory,
            next_element,
            begin,
            until,
        }
    }
}

enum TrajectoryIterNext {
    PreInitial(TimePoint),
    Index(usize),
    StartPostFinish(TimePoint),
    ReachedPostFinish(TimePoint),
    Depleted,
}

impl<'a, W: Waypoint> Iterator for TrajectoryIter<'a, W> {
    type Item = W;

    fn next(&mut self) -> Option<W> {
        match self.next_element {
            TrajectoryIterNext::PreInitial(t) => {
                if let Some(t_f) = self.until {
                    if t_f < t {
                        self.next_element = TrajectoryIterNext::Depleted;
                        return None;
                    }
                }

                let mut wp = self.trajectory.initial_motion().clone();
                wp.set_time(t);
                self.next_element = TrajectoryIterNext::Index(0);
                Some(wp)
            }
            TrajectoryIterNext::Index(index) => {
                let wp = self.trajectory.get(index).map(|wp| wp.clone());
                if let (Some(t_f), Some(wp)) = (self.until, &wp) {
                    if t_f <= wp.time() {
                        // We only include the first element that exceeds the
                        // finish time.
                        self.next_element = TrajectoryIterNext::Depleted;
                        if wp.time() < self.begin {
                            // This element comes before the begin time.
                            // This is not supposed to happen, but let's handle
                            // it gracefully anyway.
                            return None;
                        }
                        return Some(wp.clone());
                    }
                }

                self.next_element = if index + 1 < self.trajectory.len() {
                    TrajectoryIterNext::Index(index + 1)
                } else {
                    if let Some(t_f) = self.until {
                        TrajectoryIterNext::ReachedPostFinish(t_f)
                    } else {
                        TrajectoryIterNext::Depleted
                    }
                };

                wp
            }
            TrajectoryIterNext::StartPostFinish(t) => {
                if !self.trajectory.has_indefinite_finish_time() {
                    self.next_element = TrajectoryIterNext::Depleted;
                    return None;
                }

                let mut wp = self.trajectory.finish_motion().clone();
                wp.set_time(t);
                if let Some(t_f) = self.until {
                    self.next_element = TrajectoryIterNext::ReachedPostFinish(t_f);
                } else {
                    self.next_element = TrajectoryIterNext::Depleted;
                }
                return Some(wp);
            }
            TrajectoryIterNext::ReachedPostFinish(t) => {
                if !self.trajectory.has_indefinite_finish_time() {
                    self.next_element = TrajectoryIterNext::Depleted;
                    return None;
                }

                let mut wp = self.trajectory.finish_motion().clone();
                wp.set_time(t);
                self.next_element = TrajectoryIterNext::Depleted;
                return Some(wp);
            }
            TrajectoryIterNext::Depleted => None,
        }
    }
}

// TODO(@mxgrey): Consider how to make this more general so that it can work on
// N-sized windows. E.g. use a circular buffer array of a fixed size to store
// N previous waypoints.
pub struct TrajectoryIterPairs<'a, W: Waypoint> {
    base: TrajectoryIter<'a, W>,
    previous: Option<W>,
}

impl<'a, W: Waypoint> Iterator for TrajectoryIterPairs<'a, W> {
    type Item = [W; 2];

    fn next(&mut self) -> Option<[W; 2]> {
        let previous = match &self.previous {
            Some(wp) => wp.clone(),
            None => match self.base.next() {
                Some(wp) => wp,
                None => return None,
            },
        };

        match self.base.next() {
            Some(wp) => {
                self.previous = Some(wp.clone());
                Some([previous, wp])
            }
            None => None,
        }
    }
}

// #[cfg(test)]
// mod tests {
//     use super::*;
//     use crate::motion::se2;
//     use crate::motion::se2::timed_position::Waypoint as WaypointSE2;
//     use crate::motion::Motion;
//     use approx::assert_relative_eq;

//     #[test]
//     fn test_valid_motion() {
//         let t0 = time_point::TimePoint::new(0);
//         let mut trajectory = se2::LinearTrajectory::new(
//             WaypointSE2::new(t0, 0.0, 0.0, 0.0),
//             WaypointSE2::new(
//                 t0 + time_point::Duration::from_secs(2),
//                 1.0,
//                 0.0,
//                 90f64.to_radians(),
//             ),
//         )
//         .expect("Trajectory failed to be created");

//         let insertion = trajectory.insert(WaypointSE2::new(
//             t0 + time_point::Duration::from_secs(3),
//             1.0,
//             1.0,
//             180f64.to_radians(),
//         ));
//         assert_eq!(insertion.ok(), Some(2));

//         let insertion = trajectory.insert(WaypointSE2::new(
//             t0 + time_point::Duration::from_secs(1),
//             0.0,
//             1.0,
//             -45f64.to_radians(),
//         ));
//         assert_eq!(insertion.ok(), Some(1));

//         assert_eq!(trajectory.len(), 4);

//         let motion = trajectory.motion();
//         let p = motion
//             .compute_position(&(t0 + time_point::Duration::from_secs_f64(0.5)))
//             .expect("Failed to calculate position");
//         assert_relative_eq!(p.translation.vector[0], 0.0);
//         assert_relative_eq!(p.translation.vector[1], 0.5);
//         assert_relative_eq!(p.rotation.angle(), (-45f64 / 2.0).to_radians());

//         let v = motion
//             .compute_velocity(&(t0 + time_point::Duration::from_secs_f64(0.6788612)))
//             .expect("Failed to calculate velocity");
//         assert_relative_eq!(v.translational[0], 0.0);
//         assert_relative_eq!(v.translational[1], 1.0);
//         assert_relative_eq!(v.rotational, -45f64.to_radians());

//         let p = motion
//             .compute_position(&(t0 + time_point::Duration::from_secs(3)))
//             .expect("Failed to calculate position");
//         assert_relative_eq!(p.translation.vector[0], 1.0);
//         assert_relative_eq!(p.translation.vector[1], 1.0);
//         assert_relative_eq!(p.rotation.angle(), 180f64.to_radians());

//         let v = motion
//             .compute_velocity(&(t0 + time_point::Duration::from_secs(3)))
//             .expect("Failed to compute velocity");
//         assert_relative_eq!(v.translational[0], 0.0);
//         assert_relative_eq!(v.translational[1], 1.0);
//         assert_relative_eq!(v.rotational, 90f64.to_radians());

//         let p = motion
//             .compute_position(&t0)
//             .expect("Failed to compute velocity");
//         assert_relative_eq!(p.translation.vector[0], 0.0);
//         assert_relative_eq!(p.translation.vector[1], 0.0);
//         assert_relative_eq!(p.rotation.angle(), 0.0);

//         let v = motion
//             .compute_velocity(&t0)
//             .expect("Failed to calculate velocity");
//         assert_relative_eq!(v.translational[0], 0.0);
//         assert_relative_eq!(v.translational[1], 1.0);
//         assert_relative_eq!(v.rotational, -45f64.to_radians());

//         let err = motion.compute_position(&(t0 - time_point::Duration::new(1)));
//         assert_eq!(err, Err(InterpError::OutOfBounds));

//         let err = motion.compute_velocity(&(t0 - time_point::Duration::new(1)));
//         assert_eq!(err, Err(InterpError::OutOfBounds));
//     }
// }