| 
									
										
										
										
											2013-06-13 03:30:20 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @file testSerialization.cpp | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @brief Tests for serialization global functions using boost.serialization | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @date Jun 12, 2013 | 
					
						
							|  |  |  |  * @author Alex Cunningham | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <CppUnitLite/TestHarness.h>
 | 
					
						
							| 
									
										
										
										
											2013-08-12 02:41:08 +08:00
										 |  |  | #include <iostream>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-13 03:30:20 +08:00
										 |  |  | #include <gtsam/slam/serialization.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <gtsam/geometry/Pose2.h>
 | 
					
						
							|  |  |  | #include <gtsam/geometry/Pose3.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <gtsam/slam/PriorFactor.h>
 | 
					
						
							|  |  |  | #include <gtsam/slam/BetweenFactor.h>
 | 
					
						
							| 
									
										
										
										
											2013-06-13 22:29:31 +08:00
										 |  |  | #include <gtsam/slam/BearingRangeFactor.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | #include <fstream>
 | 
					
						
							|  |  |  | #include <sstream>
 | 
					
						
							|  |  |  | #include <boost/assign/std/vector.hpp>
 | 
					
						
							|  |  |  | #include <boost/filesystem.hpp>
 | 
					
						
							| 
									
										
										
										
											2013-06-13 03:30:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							| 
									
										
										
										
											2013-06-13 22:29:31 +08:00
										 |  |  | using namespace boost::assign; | 
					
						
							|  |  |  | namespace fs = boost::filesystem; | 
					
						
							|  |  |  | #ifdef TOPSRCDIR
 | 
					
						
							|  |  |  | static string topdir = TOPSRCDIR; | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | static string topdir = "TOPSRCDIR_NOT_CONFIGURED"; // If TOPSRCDIR is not defined, we error
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2013-06-13 03:30:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | Values exampleValues() { | 
					
						
							|  |  |  |   Values result; | 
					
						
							|  |  |  |   result.insert(234, gtsam::Rot2::fromAngle(0.1)); | 
					
						
							|  |  |  |   result.insert(123, gtsam::Point2(1.0, 2.0)); | 
					
						
							|  |  |  |   result.insert(254, gtsam::Pose2(1.0, 2.0, 0.3)); | 
					
						
							|  |  |  |   result.insert(678, gtsam::Rot3::Rx(0.1)); | 
					
						
							|  |  |  |   result.insert(498, gtsam::Point3(1.0, 2.0, 3.0)); | 
					
						
							|  |  |  |   result.insert(345, gtsam::Pose3(Rot3::Rx(0.1), Point3(1.0, 2.0, 3.0))); | 
					
						
							|  |  |  |   return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | NonlinearFactorGraph exampleGraph() { | 
					
						
							|  |  |  |   NonlinearFactorGraph graph; | 
					
						
							|  |  |  |   graph.add(PriorFactor<Pose2>(234, Pose2(1.0, 2.0, 0.3), noiseModel::Diagonal::Sigmas(ones(3)))); | 
					
						
							|  |  |  |   graph.add(BetweenFactor<Pose2>(234, 567, Pose2(1.0, 2.0, 0.3), noiseModel::Diagonal::Sigmas(ones(3)))); | 
					
						
							| 
									
										
										
										
											2013-06-13 22:29:31 +08:00
										 |  |  |   graph.add(BearingRangeFactor<Pose2,Point2>(234, 567, Rot2::fromAngle(0.3), 2.0, noiseModel::Diagonal::Sigmas(ones(2)))); | 
					
						
							| 
									
										
										
										
											2013-06-13 03:30:20 +08:00
										 |  |  |   return graph; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST( testSerialization, text_graph_serialization ) { | 
					
						
							|  |  |  |   NonlinearFactorGraph graph = exampleGraph(); | 
					
						
							|  |  |  |   string serialized = serializeGraph(graph); | 
					
						
							| 
									
										
										
										
											2013-06-13 03:30:21 +08:00
										 |  |  |   NonlinearFactorGraph actGraph = *deserializeGraph(serialized); | 
					
						
							| 
									
										
										
										
											2013-06-13 03:30:20 +08:00
										 |  |  |   EXPECT(assert_equal(graph, actGraph)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST( testSerialization, xml_graph_serialization ) { | 
					
						
							|  |  |  |   NonlinearFactorGraph graph = exampleGraph(); | 
					
						
							|  |  |  |   string serialized = serializeGraphXML(graph, "graph1"); | 
					
						
							| 
									
										
										
										
											2013-06-13 03:30:21 +08:00
										 |  |  |   NonlinearFactorGraph actGraph = *deserializeGraphXML(serialized, "graph1"); | 
					
						
							| 
									
										
										
										
											2013-06-13 03:30:20 +08:00
										 |  |  |   EXPECT(assert_equal(graph, actGraph)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST( testSerialization, text_values_serialization ) { | 
					
						
							|  |  |  |   Values values = exampleValues(); | 
					
						
							|  |  |  |   string serialized = serializeValues(values); | 
					
						
							| 
									
										
										
										
											2013-06-13 03:30:21 +08:00
										 |  |  |   Values actValues = *deserializeValues(serialized); | 
					
						
							| 
									
										
										
										
											2013-06-13 03:30:20 +08:00
										 |  |  |   EXPECT(assert_equal(values, actValues)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST( testSerialization, xml_values_serialization ) { | 
					
						
							|  |  |  |   Values values = exampleValues(); | 
					
						
							|  |  |  |   string serialized = serializeValuesXML(values, "values1"); | 
					
						
							| 
									
										
										
										
											2013-06-13 03:30:21 +08:00
										 |  |  |   Values actValues = *deserializeValuesXML(serialized, "values1"); | 
					
						
							| 
									
										
										
										
											2013-06-13 03:30:20 +08:00
										 |  |  |   EXPECT(assert_equal(values, actValues, 1e-5)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-13 22:29:31 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST( testSerialization, serialization_file ) { | 
					
						
							|  |  |  |   // Create files in folder in build folder
 | 
					
						
							|  |  |  |   fs::remove_all("actual"); | 
					
						
							|  |  |  |   fs::create_directory("actual"); | 
					
						
							|  |  |  |   string path = "actual/"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   NonlinearFactorGraph graph = exampleGraph(); | 
					
						
							|  |  |  |   Values values = exampleValues(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Serialize objects using each configuration
 | 
					
						
							|  |  |  |   EXPECT(serializeGraphToFile(graph, path + "graph.dat")); | 
					
						
							|  |  |  |   EXPECT(serializeGraphToXMLFile(graph, path + "graph.xml", "graph1")); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   EXPECT(serializeValuesToFile(values, path + "values.dat")); | 
					
						
							|  |  |  |   EXPECT(serializeValuesToXMLFile(values, path + "values.xml", "values1")); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Deserialize
 | 
					
						
							| 
									
										
										
										
											2013-06-13 22:46:53 +08:00
										 |  |  |   NonlinearFactorGraph actGraph = *deserializeGraphFromFile(path + "graph.dat"); | 
					
						
							|  |  |  |   NonlinearFactorGraph actGraphXML = *deserializeGraphFromXMLFile(path + "graph.xml", "graph1"); | 
					
						
							| 
									
										
										
										
											2013-06-13 22:29:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-13 22:46:53 +08:00
										 |  |  |   Values actValues = *deserializeValuesFromFile(path + "values.dat"); | 
					
						
							|  |  |  |   Values actValuesXML = *deserializeValuesFromXMLFile(path + "values.xml", "values1"); | 
					
						
							| 
									
										
										
										
											2013-06-13 22:29:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Verify
 | 
					
						
							|  |  |  |   EXPECT(assert_equal(graph, actGraph)); | 
					
						
							|  |  |  |   EXPECT(assert_equal(graph, actGraphXML)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   EXPECT(assert_equal(values, actValues)); | 
					
						
							|  |  |  |   EXPECT(assert_equal(values, actValuesXML)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-13 03:30:20 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | int main() { TestResult tr; return TestRegistry::runAllTests(tr); } | 
					
						
							|  |  |  | /* ************************************************************************* */ |