Trajectory.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <map>
4 
5 #include "Track.h"
6 
7 
8 namespace armarx::trajectory
9 {
10 
11  /**
12  * @brief This class is used to update entities based on trajectory
13  * defined by keyframes.
14  *
15  * A Trajectory is a collection of tracks, where each track represents a
16  * timeline of a single value. A track is composed of keyframes, where
17  * each keyframe represents a value at a specific time on the timeline.
18  *
19  * @see Track
20  */
21  class Trajectory
22  {
23  public:
24 
25  /// Constructor.
26  Trajectory();
27 
28 
29  /// Clear the trajectory of all tracks.
30  void clear();
31 
32 
33  /**
34  * @brief Add track with the given ID (and no update function).
35  * If there is already a track with the given ID, it is overwritten.
36  */
37  VariantTrack& addTrack(const TrackID& id);
38 
39  /**
40  * @brief Add track with the given ID and update function.
41  * If there is already a track with the given ID, it is overwritten.
42  */
43  VariantTrack& addTrack(const TrackID& id, const VariantTrack::UpdateFunc& updateFunc);
44 
45 
46  /// Add a keyframe to the specified track.
47  /// @throw `error::NoTrackWithID` if there is not track with the given ID
48  void addKeyframe(const TrackID& id, const VariantKeyframe& keyframe);
49 
50  /// Add a keyframe to the specified track.
51  /// @throw `error::NoTrackWithID` if there is not track with the given ID
52  void addKeyframe(const TrackID& id, float time, const VariantValue& value);
53 
54 
55  /**
56  * @brief Update all tracks for the given time.
57  * @param ignoreEmptyTracks If true, empty tracks are ignored.
58  * @throws `error::EmptyTrack` If `ignoreEmptyTracks` is false and a track is empty.
59  */
60  void update(float time, bool ignoreEmptyTracks = false);
61 
62 
63  /// Get the track with the given ID.
64  /// @throw `error::NoTrackWithID` if there is not track with the given ID
65  VariantTrack& operator[](const TrackID& id);
66 
67  /// Get the track with the given ID.
68  /// @throw `error::NoTrackWithID` if there is not track with the given ID
69  const VariantTrack& operator[](const TrackID& id) const;
70 
71  friend std::ostream& operator<<(std::ostream& os, const Trajectory& trajectory);
72 
73 
74  private:
75 
76  /// The tracks.
77  std::map<TrackID, VariantTrack> tracks;
78 
79  };
80 
81 
82  std::ostream& operator<<(std::ostream& os, const Trajectory& trajectory);
83 
84 
85  // UPDATE FUNCTION HELPERS
86 
87  /// Get an update function for value assignments.
88  template <typename ValueT>
90  {
91  return [&v](VariantValue value)
92  {
93  v = std::get<ValueT>(value);
94  };
95  }
96 
97  /// Wrap the function in a Track::UpdateFunc.
98  VariantTrack::UpdateFunc toUpdateFunc(std::function<void(float)> func);
99  /// Wrap the function in a Track::UpdateFunc.
100  VariantTrack::UpdateFunc toUpdateFunc(std::function<void(const Eigen::MatrixXf&)> func);
101  /// Wrap the function in a Track::UpdateFunc.
102  VariantTrack::UpdateFunc toUpdateFunc(std::function<void(const Eigen::Quaternionf&)> func);
103 
104 }
105 
armarx::trajectory::VariantValue
std::variant< float, Eigen::MatrixXf, Eigen::Quaternionf > VariantValue
Variant for trajectory values.
Definition: VariantValue.h:13
armarx::trajectory::Keyframe
A keyframe, representing a value at a given time.
Definition: Track.h:14
armarx::trajectory::Track
A track represents the timeline of a single value, identified by a track ID.
Definition: Track.h:34
armarx::trajectory::Trajectory::operator[]
VariantTrack & operator[](const TrackID &id)
Get the track with the given ID.
Definition: Trajectory.cpp:43
armarx::trajectory::TrackID
std::string TrackID
ID of tracks.
Definition: VariantValue.h:16
armarx::trajectory::Trajectory::update
void update(float time, bool ignoreEmptyTracks=false)
Update all tracks for the given time.
Definition: Trajectory.cpp:35
armarx::trajectory::toUpdateFunc
VariantTrack::UpdateFunc toUpdateFunc(std::function< void(float)> func)
Wrap the function in a Track::UpdateFunc.
Definition: Trajectory.cpp:81
armarx::trajectory::Track::UpdateFunc
std::function< void(ValueT)> UpdateFunc
The update function type.
Definition: Track.h:38
armarx::trajectory::Trajectory::addTrack
VariantTrack & addTrack(const TrackID &id)
Add track with the given ID (and no update function).
Definition: Trajectory.cpp:15
armarx::trajectory::Trajectory::operator<<
friend std::ostream & operator<<(std::ostream &os, const Trajectory &trajectory)
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::trajectory::updateValue
VariantTrack::UpdateFunc updateValue(ValueT &v)
Get an update function for value assignments.
Definition: Trajectory.h:89
Eigen::Quaternionf
Quaternion< float, 0 > Quaternionf
Definition: EigenForwardDeclarations.h:61
armarx::trajectory::Trajectory
This class is used to update entities based on trajectory defined by keyframes.
Definition: Trajectory.h:21
armarx::trajectory
Definition: exceptions.cpp:3
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
armarx::trajectory::operator<<
std::ostream & operator<<(std::ostream &os, const Track< ValueT > &track)
Definition: Track.h:274
armarx::trajectory::Trajectory::clear
void clear()
Clear the trajectory of all tracks.
Definition: Trajectory.cpp:10
armarx::trajectory::Trajectory::addKeyframe
void addKeyframe(const TrackID &id, const VariantKeyframe &keyframe)
Add a keyframe to the specified track.
Definition: Trajectory.cpp:25
Track.h
armarx::trajectory::Trajectory::Trajectory
Trajectory()
Constructor.
Definition: Trajectory.cpp:7