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