| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 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 |
|
| 53 |
|
| 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 |
} |
| 106 |
|
| 107 |
#endif |