testSerialization.cpp
Go to the documentation of this file.
1 
11 
12 #include <gtsam/geometry/Pose2.h>
13 #include <gtsam/geometry/Pose3.h>
14 
17 
19 
20 #include <boost/filesystem.hpp>
21 
22 #include <iostream>
23 #include <cstdlib>
24 #include <fstream>
25 #include <sstream>
26 
27 using namespace std;
28 using namespace gtsam;
29 namespace fs = boost::filesystem;
30 #ifdef TOPSRCDIR
31 static string topdir = TOPSRCDIR;
32 #else
33 static string topdir = "TOPSRCDIR_NOT_CONFIGURED"; // If TOPSRCDIR is not defined, we error
34 #endif
35 
37  Values result;
39  result.insert(123, gtsam::Point2(1.0, 2.0));
40  result.insert(254, gtsam::Pose2(1.0, 2.0, 0.3));
41  result.insert(678, gtsam::Rot3::Rx(0.1));
42  result.insert(498, gtsam::Point3(1.0, 2.0, 3.0));
43  result.insert(345, gtsam::Pose3(Rot3::Rx(0.1), Point3(1.0, 2.0, 3.0)));
44  return result;
45 }
46 
49  graph.addPrior(234, Pose2(1.0, 2.0, 0.3), noiseModel::Diagonal::Sigmas(Vector::Ones(3)));
50  graph.add(BetweenFactor<Pose2>(234, 567, Pose2(1.0, 2.0, 0.3), noiseModel::Diagonal::Sigmas(Vector::Ones(3))));
51  graph.add(BearingRangeFactor<Pose2,Point2>(234, 567, Rot2::fromAngle(0.3), 2.0, noiseModel::Diagonal::Sigmas(Vector::Ones(2))));
52  return graph;
53 }
54 
55 /* ************************************************************************* */
56 TEST( testSerialization, text_graph_serialization ) {
58  string serialized = serializeGraph(graph);
59  NonlinearFactorGraph actGraph = *deserializeGraph(serialized);
60  EXPECT(assert_equal(graph, actGraph));
61 }
62 
63 /* ************************************************************************* */
64 TEST( testSerialization, xml_graph_serialization ) {
66  string serialized = serializeGraphXML(graph, "graph1");
67  NonlinearFactorGraph actGraph = *deserializeGraphXML(serialized, "graph1");
68  EXPECT(assert_equal(graph, actGraph));
69 }
70 
71 /* ************************************************************************* */
72 TEST( testSerialization, text_values_serialization ) {
74  string serialized = serializeValues(values);
75  Values actValues = *deserializeValues(serialized);
76  EXPECT(assert_equal(values, actValues));
77 }
78 
79 /* ************************************************************************* */
80 TEST( testSerialization, xml_values_serialization ) {
82  string serialized = serializeValuesXML(values, "values1");
83  Values actValues = *deserializeValuesXML(serialized, "values1");
84  EXPECT(assert_equal(values, actValues, 1e-5));
85 }
86 
87 /* ************************************************************************* */
88 TEST( testSerialization, serialization_file ) {
89  // Create files in folder in build folder
90  fs::remove_all("actual");
91  fs::create_directory("actual");
92  string path = "actual/";
93 
96 
97  // Serialize objects using each configuration
98  EXPECT(serializeGraphToFile(graph, path + "graph.dat"));
99  EXPECT(serializeGraphToXMLFile(graph, path + "graph.xml", "graph1"));
100 
101  EXPECT(serializeValuesToFile(values, path + "values.dat"));
102  EXPECT(serializeValuesToXMLFile(values, path + "values.xml", "values1"));
103 
104  // Deserialize
105  NonlinearFactorGraph actGraph = *deserializeGraphFromFile(path + "graph.dat");
106  NonlinearFactorGraph actGraphXML = *deserializeGraphFromXMLFile(path + "graph.xml", "graph1");
107 
108  Values actValues = *deserializeValuesFromFile(path + "values.dat");
109  Values actValuesXML = *deserializeValuesFromXMLFile(path + "values.xml", "values1");
110 
111  // Verify
112  EXPECT(assert_equal(graph, actGraph));
113  EXPECT(assert_equal(graph, actGraphXML));
114 
115  EXPECT(assert_equal(values, actValues));
116  EXPECT(assert_equal(values, actValuesXML));
117 }
118 
119 /* ************************************************************************* */
120 int main() { TestResult tr; return TestRegistry::runAllTests(tr); }
121 /* ************************************************************************* */
TestRegistry::runAllTests
static int runAllTests(TestResult &result)
Definition: TestRegistry.cpp:27
Pose2.h
2D Pose
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
EXPECT
#define EXPECT(condition)
Definition: Test.h:150
TestHarness.h
serialization.h
Global functions for performing serialization, designed for use with matlab.
exampleValues
Values exampleValues()
Definition: testSerialization.cpp:36
different_sigmas::values
HybridValues values
Definition: testHybridBayesNet.cpp:245
TEST
TEST(testSerialization, text_graph_serialization)
Definition: testSerialization.cpp:56
result
Values result
Definition: OdometryOptimize.cpp:8
gtsam::Rot2::fromAngle
static Rot2 fromAngle(double theta)
Named constructor from angle in radians.
Definition: Rot2.h:61
gtsam::serializeGraph
std::string serializeGraph(const NonlinearFactorGraph &graph)
Definition: serialization.cpp:179
BetweenFactor.h
gtsam::BearingRangeFactor
Definition: sam/BearingRangeFactor.h:34
gtsam::Pose3
Definition: Pose3.h:37
gtsam::deserializeGraphXML
NonlinearFactorGraph::shared_ptr deserializeGraphXML(const std::string &serialized_graph, const std::string &name="graph")
Definition: serialization.cpp:196
gtsam::deserializeGraphFromFile
NonlinearFactorGraph::shared_ptr deserializeGraphFromFile(const std::string &fname)
Definition: serialization.cpp:251
gtsam::NonlinearFactorGraph
Definition: NonlinearFactorGraph.h:55
gtsam::NonlinearFactorGraph::addPrior
void addPrior(Key key, const T &prior, const SharedNoiseModel &model=nullptr)
Definition: NonlinearFactorGraph.h:199
gtsam::serializeValuesXML
std::string serializeValuesXML(const Values &values, const std::string &name="values")
Definition: serialization.cpp:216
exampleGraph
NonlinearFactorGraph exampleGraph()
Definition: testSerialization.cpp:47
gtsam::Point2
Vector2 Point2
Definition: Point2.h:32
main
int main()
Definition: testSerialization.cpp:120
TestResult
Definition: TestResult.h:26
gtsam::deserializeValues
Values::shared_ptr deserializeValues(const std::string &serialized_values)
Definition: serialization.cpp:209
matlab_wrap.path
path
Definition: matlab_wrap.py:66
gtsam::deserializeValuesFromFile
Values::shared_ptr deserializeValuesFromFile(const std::string &fname)
Definition: serialization.cpp:268
gtsam::deserializeGraph
NonlinearFactorGraph::shared_ptr deserializeGraph(const std::string &serialized_graph)
Definition: serialization.cpp:184
gtsam::Rot3::Rx
static Rot3 Rx(double t)
Rotation around X axis as in http://en.wikipedia.org/wiki/Rotation_matrix, counterclockwise when look...
Definition: Rot3M.cpp:56
gtsam
traits
Definition: SFMdata.h:40
gtsam::deserializeGraphFromXMLFile
NonlinearFactorGraph::shared_ptr deserializeGraphFromXMLFile(const std::string &fname, const std::string &name="graph")
Definition: serialization.cpp:259
gtsam::Values
Definition: Values.h:65
topdir
static string topdir
Definition: testSerialization.cpp:33
std
Definition: BFloat16.h:88
gtsam::Values::insert
void insert(Key j, const Value &val)
Definition: Values.cpp:156
gtsam::assert_equal
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:41
gtsam::Point3
Vector3 Point3
Definition: Point3.h:38
gtsam::serializeGraphToXMLFile
bool serializeGraphToXMLFile(const NonlinearFactorGraph &graph, const std::string &fname, const std::string &name="graph")
Definition: serialization.cpp:234
gtsam::serializeGraphXML
std::string serializeGraphXML(const NonlinearFactorGraph &graph, const std::string &name="graph")
Definition: serialization.cpp:191
gtsam::serializeGraphToFile
bool serializeGraphToFile(const NonlinearFactorGraph &graph, const std::string &fname)
Definition: serialization.cpp:229
gtsam::FactorGraph::add
IsDerived< DERIVEDFACTOR > add(std::shared_ptr< DERIVEDFACTOR > factor)
add is a synonym for push_back.
Definition: FactorGraph.h:171
graph
NonlinearFactorGraph graph
Definition: doc/Code/OdometryExample.cpp:2
gtsam::deserializeValuesXML
Values::shared_ptr deserializeValuesXML(const std::string &serialized_values, const std::string &name="values")
Definition: serialization.cpp:221
gtsam::deserializeValuesFromXMLFile
Values::shared_ptr deserializeValuesFromXMLFile(const std::string &fname, const std::string &name="values")
Definition: serialization.cpp:276
Pose3.h
3D Pose manifold SO(3) x R^3 and group SE(3)
gtsam::serializeValues
std::string serializeValues(const Values &values)
Definition: serialization.cpp:204
gtsam::Pose2
Definition: Pose2.h:39
BearingRangeFactor.h
a single factor contains both the bearing and the range to prevent handle to pair bearing and range f...
gtsam::serializeValuesToFile
bool serializeValuesToFile(const Values &values, const std::string &fname)
Definition: serialization.cpp:240
gtsam::BetweenFactor
Definition: BetweenFactor.h:40
gtsam::serializeValuesToXMLFile
bool serializeValuesToXMLFile(const Values &values, const std::string &fname, const std::string &name="values")
Definition: serialization.cpp:245


gtsam
Author(s):
autogenerated on Sun Dec 22 2024 04:17:12