testSerializationSam.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  * -------------------------------------------------------------------------- */
11 
22 #include <gtsam/geometry/Pose2.h>
23 #include <gtsam/geometry/Pose3.h>
28 #include <gtsam/sam/RangeFactor.h>
29 
30 using namespace std;
31 using namespace gtsam;
32 
33 namespace {
34 Key poseKey(1);
35 Key pointKey(2);
36 constexpr double rangeMmeasurement(10.0);
37 } // namespace
38 
39 /* ************************************************************************* */
40 // Export Noisemodels
41 // See http://www.boost.org/doc/libs/1_32_0/libs/serialization/doc/special.html
44 
45 /* ************************************************************************* */
46 TEST(SerializationSam, BearingFactor2D) {
47  using BearingFactor2D = BearingFactor<Pose2, Point2>;
48  double measurement2D(10.0);
49  static SharedNoiseModel model2D(noiseModel::Isotropic::Sigma(1, 0.5));
50  BearingFactor2D factor2D(poseKey, pointKey, measurement2D, model2D);
51  EXPECT(serializationTestHelpers::equalsObj(factor2D));
52  EXPECT(serializationTestHelpers::equalsXML(factor2D));
53  EXPECT(serializationTestHelpers::equalsBinary(factor2D));
54 }
55 
56 /* ************************************************************************* */
57 TEST(SerializationSam, BearingFactor3D) {
58  using BearingFactor3D = BearingFactor<Pose3, Point3>;
59  Unit3 measurement3D =
60  Pose3().bearing(Point3(1, 0, 0)); // has to match values!
61  static SharedNoiseModel model3D(noiseModel::Isotropic::Sigma(2, 0.5));
62  BearingFactor3D factor3D(poseKey, pointKey, measurement3D, model3D);
63  EXPECT(serializationTestHelpers::equalsObj(factor3D));
64  EXPECT(serializationTestHelpers::equalsXML(factor3D));
65  EXPECT(serializationTestHelpers::equalsBinary(factor3D));
66 }
67 
68 /* ************************************************************************* */
69 namespace {
70 static SharedNoiseModel rangeNoiseModel(noiseModel::Unit::Create(1));
71 }
72 
73 TEST(SerializationSam, RangeFactor2D) {
75  RangeFactor2D factor2D(poseKey, pointKey, rangeMmeasurement, rangeNoiseModel);
76  EXPECT(serializationTestHelpers::equalsObj(factor2D));
77  EXPECT(serializationTestHelpers::equalsXML(factor2D));
78  EXPECT(serializationTestHelpers::equalsBinary(factor2D));
79 }
80 
81 /* ************************************************************************* */
82 TEST(SerializationSam, RangeFactor3D) {
84  RangeFactor3D factor3D(poseKey, pointKey, rangeMmeasurement, rangeNoiseModel);
85  EXPECT(serializationTestHelpers::equalsObj(factor3D));
86  EXPECT(serializationTestHelpers::equalsXML(factor3D));
87  EXPECT(serializationTestHelpers::equalsBinary(factor3D));
88 }
89 
90 /* ************************************************************************* */
91 TEST(RangeFactor, EqualsAfterDeserializing) {
92  // Check that the same results are obtained after deserializing:
93  Pose3 body_P_sensor_3D(Rot3::RzRyRx(-M_PI_2, 0.0, -M_PI_2),
94  Point3(0.25, -0.10, 1.0));
96  poseKey, pointKey, rangeMmeasurement, rangeNoiseModel, body_P_sensor_3D),
97  factor3D_2;
98 
99  // check with Equal() trait:
100  gtsam::serializationTestHelpers::roundtripXML(factor3D_1, factor3D_2);
101  CHECK(assert_equal(factor3D_1, factor3D_2));
102 
103  const Pose3 pose(Rot3::RzRyRx(0.2, -0.3, 1.75), Point3(1.0, 2.0, -3.0));
104  const Point3 point(-2.0, 11.0, 1.0);
105  const Values values = {{poseKey, genericValue(pose)},
106  {pointKey, genericValue(point)}};
107 
108  const Vector error_1 = factor3D_1.unwhitenedError(values);
109  const Vector error_2 = factor3D_2.unwhitenedError(values);
110  CHECK(assert_equal(error_1, error_2));
111 }
112 
113 /* ************************************************************************* */
114 TEST(BearingRangeFactor, Serialization2D) {
116  static SharedNoiseModel model2D(noiseModel::Isotropic::Sigma(2, 0.5));
117  BearingRangeFactor2D factor2D(poseKey, pointKey, 1, 2, model2D);
118 
119  EXPECT(serializationTestHelpers::equalsObj(factor2D));
120  EXPECT(serializationTestHelpers::equalsXML(factor2D));
121  EXPECT(serializationTestHelpers::equalsBinary(factor2D));
122 }
123 
124 /* ************************************************************************* */
125 TEST(BearingRangeFactor, Serialization3D) {
127  static SharedNoiseModel model3D(noiseModel::Isotropic::Sigma(3, 0.5));
129  Pose3().bearing(Point3(1, 0, 0)), 1, model3D);
130  EXPECT(serializationTestHelpers::equalsObj(factor3D));
131  EXPECT(serializationTestHelpers::equalsXML(factor3D));
132  EXPECT(serializationTestHelpers::equalsBinary(factor3D));
133 }
134 
135 /* ************************************************************************* */
136 int main() {
137  TestResult tr;
138  return TestRegistry::runAllTests(tr);
139 }
140 /* ************************************************************************* */
const gtsam::Key poseKey
#define CHECK(condition)
Definition: Test.h:108
static int runAllTests(TestResult &result)
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:40
leaf::MyValues values
Definition: BFloat16.h:88
Evaluate derivatives of a nonlinear factor numerically.
const gtsam::Key pointKey
TEST(SerializationSam, BearingFactor2D)
Represents a 3D point on a unit sphere.
Definition: Unit3.h:42
Eigen::VectorXd Vector
Definition: Vector.h:38
Test harness methods for expressions.
#define EXPECT(condition)
Definition: Test.h:150
Unit3 bearing(const Point3 &point, OptionalJacobian< 2, 6 > Hself={}, OptionalJacobian< 2, 3 > Hpoint={}) const
Definition: Pose3.cpp:425
Vector unwhitenedError(const Values &x, OptionalMatrixVecType H=nullptr) const override
traits
Definition: chartTesting.h:28
GenericValue< T > genericValue(const T &v)
Definition: GenericValue.h:211
static const Pose3 pose(Rot3(Vector3(1, -1, -1).asDiagonal()), Point3(0, 0, 0.5))
BOOST_CLASS_EXPORT(gtsam::noiseModel::Isotropic)
int main()
Vector3 Point3
Definition: Point3.h:38
2D Pose
3D Pose
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:102
noiseModel::Base::shared_ptr SharedNoiseModel
Definition: NoiseModel.h:741


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:39:29