test_velocities.cpp
Go to the documentation of this file.
1 #include <iostream>
2 
3 #include "tests.hpp"
4 
5 #include <sophus/velocities.hpp>
6 
7 namespace Sophus {
8 namespace experimental {
9 
10 template <class Scalar>
12  bool passed = true;
13  std::vector<SE3<Scalar>, Eigen::aligned_allocator<SE3<Scalar>>> bar_Ts_baz;
14 
15  for (size_t i = 0; i < 10; ++i) {
16  bar_Ts_baz.push_back(SE3<Scalar>::rotX(i * 0.001) *
17  SE3<Scalar>::rotY(i * 0.001) *
18  SE3<Scalar>::transX(0.01 * i));
19  }
20 
21  SE3<Scalar> foo_T_bar =
23 
24  std::vector<SE3<Scalar>, Eigen::aligned_allocator<SE3<Scalar>>> foo_Ts_baz;
25  for (auto const& bar_T_baz : bar_Ts_baz) {
26  foo_Ts_baz.push_back(foo_T_bar * bar_T_baz);
27  }
28 
29  auto gen_linear_vels =
30  [](std::vector<SE3<Scalar>, Eigen::aligned_allocator<SE3<Scalar>>> const&
31  a_Ts_b) {
32  std::vector<Vector3<Scalar>, Eigen::aligned_allocator<Vector3<Scalar>>>
33  linearVels_a;
34  for (size_t i = 0; i < a_Ts_b.size() - 1; ++i) {
35  linearVels_a.push_back(a_Ts_b[i + 1].translation() -
36  a_Ts_b[i].translation());
37  }
38  return linearVels_a;
39  };
40 
41  // linear velocities in frame bar
42  std::vector<Vector3<Scalar>, Eigen::aligned_allocator<Vector3<Scalar>>>
43  linearVels_bar = gen_linear_vels(bar_Ts_baz);
44  // linear velocities in frame foo
45  std::vector<Vector3<Scalar>, Eigen::aligned_allocator<Vector3<Scalar>>>
46  linearVels_foo = gen_linear_vels(foo_Ts_baz);
47 
48  for (size_t i = 0; i < linearVels_bar.size(); ++i) {
49  SOPHUS_TEST_APPROX(passed, linearVels_foo[i],
50  transformVelocity(foo_T_bar, linearVels_bar[i]),
52  }
53  return passed;
54 }
55 
56 template <class Scalar>
58  bool passed = true;
59 
60  SE3<Scalar> foo_T_bar =
62 
63  // One parameter subgroup of SE3, motion through space given time t.
64  auto bar_T_baz = [](Scalar t) -> SE3<Scalar> {
65  return SE3<Scalar>::rotX(t * Scalar(0.01)) *
66  SE3<Scalar>::rotY(t * Scalar(0.0001)) *
67  SE3<Scalar>::transX(t * Scalar(0.0001));
68  };
69 
70  std::vector<Scalar> ts = {Scalar(0), Scalar(0.3), Scalar(1)};
71 
72  Scalar h = Constants<Scalar>::epsilon();
73  for (Scalar t : ts) {
74  // finite difference approximiation of instantanious velocity in frame bar
75  Vector3<Scalar> rotVel_in_frame_bar =
76  finiteDifferenceRotationalVelocity<Scalar>(bar_T_baz, t, h);
77 
78  // finite difference approximiation of instantanious velocity in frame foo
79  Vector3<Scalar> rotVel_in_frame_foo =
80  finiteDifferenceRotationalVelocity<Scalar>(
81  [&foo_T_bar, bar_T_baz](Scalar t) -> SE3<Scalar> {
82  return foo_T_bar * bar_T_baz(t);
83  },
84  t, h);
85 
86  Vector3<Scalar> rotVel_in_frame_bar2 =
87  transformVelocity(foo_T_bar.inverse(), rotVel_in_frame_foo);
89  passed, rotVel_in_frame_bar, rotVel_in_frame_bar2,
90  // not too tight threshold, because of finit difference approximation
91  std::sqrt(Constants<Scalar>::epsilon()));
92 
93  // The rotational velocities rotVel_in_frame_foo and rotVel_in_frame_bar
94  // should not be equal since they are in different frames (foo != bar).
95  SOPHUS_TEST_NOT_APPROX(passed, rotVel_in_frame_foo, rotVel_in_frame_bar,
96  Scalar(1e-3));
97 
98  // Expect same result when using adjoint instead since:
99  // vee(bar_R_foo * hat(vel_foo) * bar_R_foo^T = bar_R_foo 8 vel_foo.
101  passed, transformVelocity(foo_T_bar.inverse(), rotVel_in_frame_foo),
102  SO3<Scalar>::vee(foo_T_bar.so3().inverse().matrix() *
103  SO3<Scalar>::hat(rotVel_in_frame_foo) *
104  foo_T_bar.so3().matrix()),
106  }
107  return passed;
108 }
109 
111  using std::cerr;
112  using std::endl;
113 
114  cerr << "Test Velocities" << endl << endl;
115  cerr << "Double tests: " << endl;
116  bool passed = tests_linear_velocities<double>();
117  passed &= tests_rotational_velocities<double>();
118  processTestResult(passed);
119 
120  cerr << "Float tests: " << endl;
121  passed = tests_linear_velocities<float>();
122  passed &= tests_rotational_velocities<float>();
123  processTestResult(passed);
124 
125  return 0;
126 }
127 } // namespace experimental
128 } // namespace Sophus
129 
Sophus::SE3::rotY
static SOPHUS_FUNC SE3 rotY(Scalar const &y)
Definition: se3.hpp:893
Sophus::SE3::so3
SOPHUS_FUNC SO3Member & so3()
Definition: se3.hpp:527
Sophus::experimental::transformVelocity
Vector3< Scalar > transformVelocity(SO3< Scalar > const &foo_R_bar, Vector3< Scalar > const &vel_bar)
Definition: velocities.hpp:18
SOPHUS_TEST_APPROX
#define SOPHUS_TEST_APPROX(passed, left, right, thr,...)
Definition: test_macros.hpp:96
Sophus::SO3
SO3 using default storage; derived from SO3Base.
Definition: so3.hpp:19
SOPHUS_TEST_NOT_APPROX
#define SOPHUS_TEST_NOT_APPROX(passed, left, right, thr,...)
Definition: test_macros.hpp:113
Sophus::experimental::test_velocities
int test_velocities()
Definition: test_velocities.cpp:110
Sophus::Vector3
Vector< Scalar, 3, Options > Vector3
Definition: types.hpp:21
Sophus::experimental::tests_rotational_velocities
bool tests_rotational_velocities()
Definition: test_velocities.cpp:57
Sophus::experimental::tests_linear_velocities
bool tests_linear_velocities()
Definition: test_velocities.cpp:11
Sophus::SE3::rotZ
static SOPHUS_FUNC SE3 rotZ(Scalar const &z)
Definition: se3.hpp:899
Sophus::Constants::epsilon
static SOPHUS_FUNC Scalar epsilon()
Definition: common.hpp:147
Sophus
Definition: average.hpp:17
Sophus::processTestResult
void processTestResult(bool passed)
Definition: test_macros.hpp:38
Sophus::SE3
SE3 using default storage; derived from SE3Base.
Definition: se3.hpp:11
Sophus::SE3::transX
static SOPHUS_FUNC SE3 transX(Scalar const &x)
Definition: se3.hpp:928
Sophus::Constants
Definition: common.hpp:146
tests.hpp
main
int main()
Definition: test_velocities.cpp:130
Sophus::SE3::rotX
static SOPHUS_FUNC SE3 rotX(Scalar const &x)
Definition: se3.hpp:887
Sophus::SE3::transY
static SOPHUS_FUNC SE3 transY(Scalar const &y)
Definition: se3.hpp:934
velocities.hpp


sophus
Author(s): Hauke Strasdat
autogenerated on Wed Mar 2 2022 01:01:48