Debian Codegraph @

unit_test/test_helper/io.h

raw symbols SCIP
1 // g2o - General Graph Optimization
2 // Copyright (C) 2014 R. Kuemmerle, G. Grisetti, W. Burgard
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 #ifndef TESTHELPER_IO_H
28 #define TESTHELPER_IO_H
29
30 #include <Eigen/Core>
31 #include <sstream>
32
33 #include "gtest/gtest.h"
34
35 namespace g2o {
36
37 template <typename T>
38 void readWriteGraphElement(const T& output, T* input) {
39 std::stringstream data;
40 EXPECT_TRUE(output.write(data));
41 EXPECT_TRUE(input->read(data));
42 }
43
44 template <typename Derived>
45 void randomizeInformationMatrix(Eigen::MatrixBase<Derived>& m) {
46 m = Derived::Random(m.rows(), m.cols());
47 m = m * m.transpose();
48 m += m.rows() * Derived::Identity();
49 }
50
51 /**
52 * Small wrapper to allow passing optional pointers
53 * TODO when we switch to C++17 use std::optional instead
54 */
55 template <typename T>
56 struct OptionalPtr {
57 OptionalPtr() : ptr(new T), own(true) {}
58 OptionalPtr(T* p) : ptr(p), own(false) {}
59 T* ptr;
60 bool own;
61 };
62
63 template <typename T>
64 struct RandomEstimate {
65 static typename T::EstimateType create() { return T::EstimateType::Random(); }
66 static bool isApprox(const typename T::EstimateType& a,
67 const typename T::EstimateType& b) {
68 return a.isApprox(b, 1e-5);
69 }
70 };
71
72 template <typename T>
73 struct RandomMeasurement {
74 static typename T::Measurement create() { return T::Measurement::Random(); }
75 static bool isApprox(const typename T::Measurement& a,
76 const typename T::Measurement& b) {
77 return a.isApprox(b, 1e-5);
78 }
79 };
80
81 template <typename T, typename RandomEstimateFunctor = RandomEstimate<T>>
82 void readWriteVectorBasedVertex(
83 OptionalPtr<T> outputVertex = OptionalPtr<T>()) {
84 outputVertex.ptr->setEstimate(RandomEstimateFunctor::create());
85 T inputVertex;
86 readWriteGraphElement(*outputVertex.ptr, &inputVertex);
87 ASSERT_TRUE(RandomEstimateFunctor::isApprox(outputVertex.ptr->estimate(),
88 inputVertex.estimate()));
89 if (outputVertex.own) delete outputVertex.ptr;
90 }
91
92 template <typename T, typename RandomMeasurementFunctor = RandomMeasurement<T>>
93 void readWriteVectorBasedEdge(OptionalPtr<T> outputEdge = OptionalPtr<T>()) {
94 outputEdge.ptr->setMeasurement(RandomMeasurementFunctor::create());
95 randomizeInformationMatrix(outputEdge.ptr->information());
96 T inputEdge;
97 readWriteGraphElement(*outputEdge.ptr, &inputEdge);
98 ASSERT_TRUE(RandomMeasurementFunctor::isApprox(outputEdge.ptr->measurement(),
99 inputEdge.measurement()));
100 ASSERT_TRUE(
101 outputEdge.ptr->information().isApprox(inputEdge.information(), 1e-5));
102 if (outputEdge.own) delete outputEdge.ptr;
103 }
104
105 } // namespace g2o
106
107 #endif