Trajectory.cpp
Go to the documentation of this file.
1 #include "Trajectory.h"
2 
3 
4 using namespace armarx::trajectory;
5 
6 
8 {}
9 
11 {
12  tracks.clear();
13 }
14 
16 {
17  return tracks.emplace(id, VariantTrack {id}).first->second;
18 }
19 
21 {
22  return tracks.emplace(id, VariantTrack {id, updateFunc}).first->second;
23 }
24 
25 void Trajectory::addKeyframe(const TrackID& id, const VariantKeyframe& keyframe)
26 {
27  (*this)[id].addKeyframe(keyframe);
28 }
29 
30 void Trajectory::addKeyframe(const TrackID& id, float time, const VariantValue& value)
31 {
32  addKeyframe(id, VariantKeyframe {time, value});
33 }
34 
35 void Trajectory::update(float time, bool ignoreEmptyTracks)
36 {
37  for (auto& it : tracks)
38  {
39  it.second.update(time, ignoreEmptyTracks);
40  }
41 }
42 
44 {
45  try
46  {
47  return tracks.at(id);
48  }
49  catch (const std::out_of_range&)
50  {
51  throw error::NoTrackWithID(id);
52  }
53 }
54 
56 {
57  try
58  {
59  return tracks.at(id);
60  }
61  catch (const std::out_of_range&)
62  {
63  throw error::NoTrackWithID(id);
64  }
65 }
66 
67 std::ostream& armarx::trajectory::operator<<(std::ostream& os, const Trajectory& trajectory)
68 {
69  os << "Trajectory with " << trajectory.tracks.size() << " tracks: ";
70  for (const auto& [name, track] : trajectory.tracks)
71  {
72  os << "\n - " << track;
73  }
74  return os;
75 }
76 
77 
78 namespace armarx
79 {
80 
81  auto trajectory::toUpdateFunc(std::function<void (float)> func) -> VariantTrack::UpdateFunc
82  {
83  return [func](VariantValue value)
84  {
85  func(std::get<float>(value));
86  };
87  }
88 
89  auto trajectory::toUpdateFunc(std::function<void (const Eigen::MatrixXf&)> func) -> VariantTrack::UpdateFunc
90  {
91  return [func](VariantValue value)
92  {
93  func(std::get<Eigen::MatrixXf>(value));
94  };
95  }
96 
97  auto trajectory::toUpdateFunc(std::function<void (const Eigen::Quaternionf&)> func) -> VariantTrack::UpdateFunc
98  {
99  return [func](VariantValue value)
100  {
101  func(std::get<Eigen::Quaternionf>(value));
102  };
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
Trajectory.h
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
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::trajectory::Trajectory
This class is used to update entities based on trajectory defined by keyframes.
Definition: Trajectory.h:21
armarx::trajectory::error::NoTrackWithID
Definition: exceptions.h:25
armarx::trajectory
Definition: exceptions.cpp:3
armarx::Quaternion< float, 0 >
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
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::trajectory::Trajectory::addKeyframe
void addKeyframe(const TrackID &id, const VariantKeyframe &keyframe)
Add a keyframe to the specified track.
Definition: Trajectory.cpp:25
armarx::trajectory::Trajectory::Trajectory
Trajectory()
Constructor.
Definition: Trajectory.cpp:7