testFactorTesting.cpp
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  * -------------------------------1------------------------------------------- */
11 
19 #include <gtsam/geometry/Pose3.h>
22 #include <gtsam/slam/expressions.h>
23 
25 
26 using namespace gtsam;
27 
28 /* ************************************************************************* */
29 Vector3 bodyVelocity(const Pose3& w_t_b,
30  const Vector3& vec_w,
31  OptionalJacobian<3, 6> Hpose = {},
32  OptionalJacobian<3, 3> Hvel = {}) {
33  Matrix36 Hrot__pose;
34  Rot3 w_R_b = w_t_b.rotation(Hrot__pose);
35  Matrix33 Hvel__rot;
36  Vector3 vec_b = w_R_b.unrotate(vec_w, Hvel__rot, Hvel);
37  if (Hpose) {
38  *Hpose = Hvel__rot * Hrot__pose;
39  }
40  return vec_b;
41 }
42 
43 // Functor used to create an expression for the measured wheel speed scaled
44 // by the scale factor.
46  public:
47  explicit ScaledVelocityFunctor(double measured_wheel_speed)
48  : measured_velocity_(measured_wheel_speed, 0, 0) {}
49 
50  // Computes the scaled measured velocity vector from the measured wheel speed
51  // and velocity scale factor. Also computes the corresponding jacobian
52  // (w.r.t. the velocity scale).
53  Vector3 operator()(double vscale,
54  OptionalJacobian<3, 1> H = {}) const {
55  // The velocity scale factor value we are optimizing for is centered around
56  // 0, so we need to add 1 to it before scaling the velocity.
57  const Vector3 scaled_velocity = (vscale + 1.0) * measured_velocity_;
58  if (H) {
59  *H = measured_velocity_;
60  }
61  return scaled_velocity;
62  }
63 
64  private:
66 };
67 
68 /* ************************************************************************* */
69 TEST(ExpressionTesting, Issue16) {
70  const double tol = 1e-4;
71  const double numerical_step = 1e-3;
72 
73  // Note: name of keys matters: if we use 'p' instead of 'x' then this no
74  // longer repros the problem from issue 16. This is because the order of
75  // evaluation in linearizeNumerically depends on the key values. To repro
76  // we want to first evaluate the jacobian for the scale, then velocity,
77  // then pose.
78  const auto pose_key = Symbol('x', 1);
79  const auto vel_key = Symbol('v', 1);
80  const auto scale_key = Symbol('s', 1);
81 
82  Values values;
83  values.insert<Pose3>(pose_key, Pose3());
84  values.insert<Vector3>(vel_key, Vector3(1, 0, 0));
85  values.insert<double>(scale_key, 0);
86 
87  const Vector3_ body_vel(&bodyVelocity,
88  Pose3_(pose_key),
89  Vector3_(vel_key));
90  const Vector3_ scaled_measured_vel(ScaledVelocityFunctor(1),
91  Double_(scale_key));
92  const auto err_expr = body_vel - scaled_measured_vel;
93 
94  const auto err = err_expr.value(values);
95  EXPECT_LONGS_EQUAL(3, err.size());
98  "ScaleAndCompare", err_expr, values, numerical_step, tol));
99 }
100 
101 /* ************************************************************************* */
102 int main() {
103  TestResult tr;
104  return TestRegistry::runAllTests(tr);
105 }
106 /* ************************************************************************* */
107 
TestRegistry::runAllTests
static int runAllTests(TestResult &result)
Definition: TestRegistry.cpp:27
H
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set set pointsize set encoding default set nopolar set noparametric set set set set surface set nocontour set clabel set mapping cartesian set nohidden3d set cntrparam order set cntrparam linear set cntrparam levels auto set cntrparam points set size set set xzeroaxis lt lw set x2zeroaxis lt lw set yzeroaxis lt lw set y2zeroaxis lt lw set tics in set ticslevel set tics set mxtics default set mytics default set mx2tics default set my2tics default set xtics border mirror norotate autofreq set ytics border mirror norotate autofreq set ztics border nomirror norotate autofreq set nox2tics set noy2tics set timestamp bottom norotate set rrange[*:*] noreverse nowriteback set trange[*:*] noreverse nowriteback set urange[*:*] noreverse nowriteback set vrange[*:*] noreverse nowriteback set xlabel matrix size set x2label set timefmt d m y n H
Definition: gnuplot_common_settings.hh:74
gtsam::Expression::value
T value(const Values &values, std::vector< Matrix > *H=nullptr) const
Return value and optional derivatives, reverse AD version Notes: this is not terribly efficient,...
Definition: Expression-inl.h:142
gtsam::Double_
Expression< double > Double_
Definition: nonlinear/expressions.h:28
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
EXPECT_LONGS_EQUAL
#define EXPECT_LONGS_EQUAL(expected, actual)
Definition: Test.h:154
EXPECT
#define EXPECT(condition)
Definition: Test.h:150
gtsam::Z_3x1
static const Eigen::MatrixBase< Vector3 >::ConstantReturnType Z_3x1
Definition: Vector.h:46
TestHarness.h
different_sigmas::values
HybridValues values
Definition: testHybridBayesNet.cpp:245
gtsam::Vector3
Eigen::Vector3d Vector3
Definition: Vector.h:43
ScaledVelocityFunctor::operator()
Vector3 operator()(double vscale, OptionalJacobian< 3, 1 > H={}) const
Definition: testFactorTesting.cpp:53
gtsam::Rot3::unrotate
Point3 unrotate(const Point3 &p, OptionalJacobian< 3, 3 > H1={}, OptionalJacobian< 3, 3 > H2={}) const
rotate point from world to rotated frame
Definition: Rot3.cpp:135
ScaledVelocityFunctor::measured_velocity_
Vector3 measured_velocity_
Definition: testFactorTesting.cpp:65
gtsam::internal::testExpressionJacobians
bool testExpressionJacobians(const std::string &name_, const gtsam::Expression< T > &expression, const gtsam::Values &values, double nd_step, double tolerance)
Definition: expressionTesting.h:31
gtsam::Expression
Definition: Expression.h:47
expressions.h
Common expressions, both linear and non-linear.
gtsam::Rot3
Rot3 is a 3D rotation represented as a rotation matrix if the preprocessor symbol GTSAM_USE_QUATERNIO...
Definition: Rot3.h:58
gtsam::Pose3
Definition: Pose3.h:37
bodyVelocity
Vector3 bodyVelocity(const Pose3 &w_t_b, const Vector3 &vec_w, OptionalJacobian< 3, 6 > Hpose={}, OptionalJacobian< 3, 3 > Hvel={})
Definition: testFactorTesting.cpp:29
gtsam::HybridValues::insert
void insert(Key j, const Vector &value)
Definition: HybridValues.cpp:85
TestResult
Definition: TestResult.h:26
expressionTesting.h
Test harness methods for expressions.
gtsam::Pose3::rotation
const Rot3 & rotation(OptionalJacobian< 3, 6 > Hself={}) const
get rotation
Definition: Pose3.cpp:315
ScaledVelocityFunctor
Definition: testFactorTesting.cpp:45
gtsam
traits
Definition: SFMdata.h:40
gtsam::TEST
TEST(SmartFactorBase, Pinhole)
Definition: testSmartFactorBase.cpp:38
gtsam::OptionalJacobian
Definition: OptionalJacobian.h:38
gtsam::Values
Definition: Values.h:65
expressions.h
Common expressions for solving geometry/slam/sfm problems.
ScaledVelocityFunctor::ScaledVelocityFunctor
ScaledVelocityFunctor(double measured_wheel_speed)
Definition: testFactorTesting.cpp:47
gtsam::assert_equal
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:40
gtsam::Pose3_
Expression< Pose3 > Pose3_
Definition: slam/expressions.h:39
gtsam::tol
const G double tol
Definition: Group.h:79
gtsam::Vector3_
Expression< Vector3 > Vector3_
Definition: nonlinear/expressions.h:31
Pose3.h
3D Pose
gtsam::Symbol
Definition: inference/Symbol.h:37
main
int main()
Definition: testFactorTesting.cpp:102


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:07:26