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