gtsam
Loading...
Searching...
No Matches
Scenario.h
Go to the documentation of this file.
1/* ----------------------------------------------------------------------------
2
3 * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4 * Atlanta, Georgia 30332-0415
5 * All Rights Reserved
6 * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7
8 * See LICENSE for the license information
9
10 * -------------------------------------------------------------------------- */
11
17
18#pragma once
21#include <gtsam/geometry/Gal3.h>
22#include <gtsam/base/Lie.h>
23#include <gtsam/dllexport.h>
24
25#include <algorithm>
26#include <cmath>
27#include <iterator>
28#include <map>
29#include <stdexcept>
30#include <string>
31
32namespace gtsam {
33
35class GTSAM_EXPORT Scenario {
36 public:
38 virtual ~Scenario() {}
39
40 // Quantities a Scenario needs to specify:
41
42 virtual Pose3 pose(double t) const = 0;
43 virtual Vector3 omega_b(double t) const = 0;
44 virtual Vector3 velocity_n(double t) const = 0;
45 virtual Vector3 acceleration_n(double t) const = 0;
46
47 // Derived quantities:
48
49 Rot3 rotation(double t) const;
50 NavState navState(double t) const;
51 Gal3 gal3(double t) const;
52
53 Vector3 velocity_b(double t) const;
54
55 Vector3 acceleration_b(double t) const;
56};
57
65class GTSAM_EXPORT ConstantTwistScenario : public Scenario {
66 public:
68 ConstantTwistScenario(const Vector3& w, const Vector3& v,
69 const Pose3& nTb0 = Pose3())
70 : twist_((Vector6() << w, v).finished()), a_b_(w.cross(v)), nTb0_(nTb0) {}
71
72 Pose3 pose(double t) const override;
73 Vector3 omega_b(double t) const override;
74 Vector3 velocity_n(double t) const override;
75 Vector3 acceleration_n(double t) const override;
76
77 private:
78 const Vector6 twist_;
79 const Vector3 a_b_; // constant centripetal acceleration in body = w_b * v_b
80 const Pose3 nTb0_;
81};
82
84class GTSAM_EXPORT AcceleratingScenario : public Scenario {
85 public:
88 AcceleratingScenario(const Rot3& nRb, const Point3& p0, const Vector3& v0,
89 const Vector3& a_n,
90 const Vector3& omega_b = Vector3::Zero())
91 : nRb_(nRb), p0_(p0), v0_(v0), a_n_(a_n), omega_b_(omega_b) {}
92
93 Pose3 pose(double t) const override;
94 Vector3 omega_b(double t) const override;
95 Vector3 velocity_n(double t) const override;
96 Vector3 acceleration_n(double t) const override;
97
98 private:
99 const Rot3 nRb_;
100 const Vector3 p0_, v0_, a_n_, omega_b_;
101};
102
113class GTSAM_EXPORT DiscreteScenario : public Scenario {
114 public:
122 DiscreteScenario(const std::map<double, Pose3>& poses,
123 const std::map<double, Vector3>& angularVelocities_b,
124 const std::map<double, Vector3>& velocities_n,
125 const std::map<double, Vector3>& accelerations_n)
126 : poses_(poses),
127 angularVelocities_b_(angularVelocities_b),
128 velocities_n_(velocities_n),
129 accelerations_n_(accelerations_n) {
130 if (poses_.empty() || angularVelocities_b_.empty() ||
131 velocities_n_.empty() || accelerations_n_.empty()) {
132 throw std::invalid_argument(
133 "Input maps for DiscreteScenario cannot be empty.");
134 }
135
136 // Since std::map is sorted, the first element has the smallest key
137 // and the last element has the largest key. We can access the last
138 // element's key efficiently using rbegin().
139 double min_t = poses_.begin()->first;
140 double max_t = poses_.rbegin()->first;
141
142 min_t = std::min(min_t, angularVelocities_b_.begin()->first);
143 max_t = std::max(max_t, angularVelocities_b_.rbegin()->first);
144
145 min_t = std::min(min_t, velocities_n_.begin()->first);
146 max_t = std::max(max_t, velocities_n_.rbegin()->first);
147
148 min_t = std::min(min_t, accelerations_n_.begin()->first);
149 max_t = std::max(max_t, accelerations_n_.rbegin()->first);
150
151 // Calculate and assign the duration.
152 t_ = max_t - min_t;
153 }
154
170 static DiscreteScenario FromCSV(const std::string& csv_filepath);
171
174 Pose3 pose(double t) const override;
175 Vector3 omega_b(double t) const override;
176 Vector3 velocity_n(double t) const override;
177 Vector3 acceleration_n(double t) const override;
179
181 double duration() const;
182
183 private:
185 std::map<double, Pose3> poses_;
187 std::map<double, Vector3> angularVelocities_b_;
189 std::map<double, Vector3> velocities_n_;
191 std::map<double, Vector3> accelerations_n_;
193 double t_;
194
207 template <typename T>
208 T interpolate(const std::map<double, T>& values, double t) const {
209 // Find the first element with a timestamp >= t
210 auto it2 = values.lower_bound(t);
211
212 // If t is before or at the very first measurement, return the first value.
213 if (it2 == values.begin()) {
214 return it2->second;
215 }
216
217 // If t is after the very last measurement, return the last value.
218 if (it2 == values.end()) {
219 return values.rbegin()->second;
220 }
221
222 // Standard case: t is between it1 and it2.
223 auto it1 = std::prev(it2);
224
225 const double t1 = it1->first;
226 const T& value1 = it1->second;
227 const double t2 = it2->first;
228 const T& value2 = it2->second;
229
230 const double dt = t2 - t1;
231 // Avoid division by zero if timestamps are identical
232 if (std::abs(dt) < 1e-9) {
233 return value1;
234 }
235
236 // Calculate interpolation fraction
237 const double alpha = (t - t1) / dt;
238
239 // Use GTSAM's manifold interpolation
240 return gtsam::interpolate<T>(value1, value2, alpha);
241 }
242};
243
244} // namespace gtsam
Base class and basic functions for Lie types.
3D Galilean Group SGal(3) state (attitude, position, velocity, time)
Navigation state composing of attitude, position, and velocity.
Global functions in a separate testing namespace.
Definition chartTesting.h:28
T interpolate(const T &X, const T &Y, double t, typename MakeOptionalJacobian< T, T >::type Hx={}, typename MakeOptionalJacobian< T, T >::type Hy={}, typename MakeOptionalJacobian< T, double >::type Ht={})
Linear interpolation between X and Y by coefficient t.
Definition Lie.h:415
Vector3 Point3
As of GTSAM 4, in order to make GTSAM more lean, it is now possible to just typedef Point3 to Vector3...
Definition Point3.h:38
Point3 cross(const Point3 &p, const Point3 &q, OptionalJacobian< 3, 3 > H1, OptionalJacobian< 3, 3 > H2)
cross product
Definition Point3.cpp:66
Represents an element of the 3D Galilean group SGal(3).
Definition Gal3.h:39
A 3D pose (R,t) : (Rot3,Point3).
Definition Pose3.h:42
Rot3 is a 3D rotation represented as a rotation matrix if the preprocessor symbol GTSAM_USE_QUATERNIO...
Definition Rot3.h:65
Navigation state: Pose (rotation, translation) + velocity Following Barrau20icra, this class belongs ...
Definition NavState.h:45
Simple trajectory simulator.
Definition Scenario.h:35
virtual Pose3 pose(double t) const =0
pose at time t
virtual Vector3 acceleration_n(double t) const =0
acceleration in nav frame
virtual Vector3 velocity_n(double t) const =0
velocity at time t, in nav frame
virtual ~Scenario()
virtual destructor
Definition Scenario.h:38
virtual Vector3 omega_b(double t) const =0
angular velocity in body frame
ConstantTwistScenario(const Vector3 &w, const Vector3 &v, const Pose3 &nTb0=Pose3())
Construct scenario with constant twist [w,v].
Definition Scenario.h:68
Vector3 omega_b(double t) const override
angular velocity in body frame
Definition Scenario.cpp:72
AcceleratingScenario(const Rot3 &nRb, const Point3 &p0, const Vector3 &v0, const Vector3 &a_n, const Vector3 &omega_b=Vector3::Zero())
Construct scenario with constant acceleration in navigation frame and optional angular velocity in bo...
Definition Scenario.h:88
A scenario defined by discrete ground-truth measurements over time.
Definition Scenario.h:113
DiscreteScenario(const std::map< double, Pose3 > &poses, const std::map< double, Vector3 > &angularVelocities_b, const std::map< double, Vector3 > &velocities_n, const std::map< double, Vector3 > &accelerations_n)
Constructor.
Definition Scenario.h:122