| 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 | #include <sstream> |
| 28 | |
| 29 | #include "g2o/types/slam3d/edge_pointxyz.h" |
| 30 | #include "g2o/types/slam3d/edge_se3.h" |
| 31 | #include "g2o/types/slam3d/edge_se3_offset.h" |
| 32 | #include "g2o/types/slam3d/edge_se3_pointxyz.h" |
| 33 | #include "g2o/types/slam3d/edge_se3_pointxyz_depth.h" |
| 34 | #include "g2o/types/slam3d/edge_se3_pointxyz_disparity.h" |
| 35 | #include "g2o/types/slam3d/edge_se3_prior.h" |
| 36 | #include "g2o/types/slam3d/edge_se3_xyzprior.h" |
| 37 | #include "g2o/types/slam3d/edge_xyz_prior.h" |
| 38 | #include "g2o/types/slam3d/parameter_stereo_camera.h" |
| 39 | #include "g2o/types/slam3d/vertex_pointxyz.h" |
| 40 | #include "g2o/types/slam3d/vertex_se3.h" |
| 41 | #include "gtest/gtest.h" |
| 42 | #include "unit_test/test_helper/io.h" |
| 43 | #include "unit_test/test_helper/random_state.h" |
| 44 | |
| 45 | using namespace std; |
| 46 | using namespace g2o; |
| 47 | |
| 48 | class IoSlam3dParam : public ::testing::Test { |
| 49 | protected: |
| 50 | void SetUp() override { |
| 51 | graph.reset(new g2o::OptimizableGraph); |
| 52 | |
| 53 | // setting up parameters for tests |
| 54 | paramOffset = new ParameterSE3Offset(); |
| 55 | paramOffset->setId(paramId++); |
| 56 | graph->addParameter(paramOffset); |
| 57 | |
| 58 | paramCamera = new ParameterCamera; |
| 59 | paramCamera->setId(paramId++); |
| 60 | graph->addParameter(paramCamera); |
| 61 | |
| 62 | // setting up some vertices |
| 63 | for (int i = 0; i < 2; ++i) { |
| 64 | VertexSE3* p = new VertexSE3; |
| 65 | p->setId(numVertices++); |
| 66 | graph->addVertex(p); |
| 67 | poses.at(i) = p; |
| 68 | } |
| 69 | point = new VertexPointXYZ; |
| 70 | point->setId(numVertices++); |
| 71 | graph->addVertex(point); |
| 72 | } |
| 73 | |
| 74 | bool preparePosePoseEdge(OptimizableGraph::Edge* e) { |
| 75 | e->setParameterId(0, paramOffset->id()); |
| 76 | for (size_t i = 0; i < e->vertices().size(); ++i) |
| 77 | e->setVertex(i, poses.at(i)); |
| 78 | return graph->addEdge(e); |
| 79 | } |
| 80 | |
| 81 | bool preparePosePointEdge(OptimizableGraph::Edge* e) { |
| 82 | e->setParameterId(0, paramOffset->id()); |
| 83 | e->setVertex(0, poses.at(0)); |
| 84 | e->setVertex(1, point); |
| 85 | return graph->addEdge(e); |
| 86 | } |
| 87 | |
| 88 | bool prepareCamPointEdge(OptimizableGraph::Edge* e) { |
| 89 | e->setParameterId(0, paramCamera->id()); |
| 90 | e->setVertex(0, poses.at(0)); |
| 91 | e->setVertex(1, point); |
| 92 | return graph->addEdge(e); |
| 93 | } |
| 94 | |
| 95 | std::shared_ptr<g2o::OptimizableGraph> graph; |
| 96 | VertexPointXYZ* point = nullptr; |
| 97 | ParameterSE3Offset* paramOffset = nullptr; |
| 98 | ParameterCamera* paramCamera = nullptr; |
| 99 | std::vector<VertexSE3*> poses = {nullptr, nullptr}; |
| 100 | int numVertices = 0; |
| 101 | int paramId = 42; |
| 102 | }; |
| 103 | |
| 104 | /* |
| 105 | * VERTEX Tests |
| 106 | */ |
| 107 | TEST(IoSlam3d, ReadWriteVertexSE3) { |
| 108 | readWriteVectorBasedVertex<VertexSE3, internal::RandomIsometry3>(); |
| 109 | } |
| 110 | |
| 111 | TEST(IoSlam3d, ReadWriteVertexPointXYZ) { |
| 112 | readWriteVectorBasedVertex<VertexPointXYZ>(); |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * EDGE Tests |
| 117 | */ |
| 118 | TEST(IoSlam3d, ReadWriteEdgeSE3) { |
| 119 | readWriteVectorBasedEdge<EdgeSE3, internal::RandomIsometry3>(); |
| 120 | } |
| 121 | |
| 122 | TEST(IoSlam3d, ReadWriteEdgePointXYZ) { |
| 123 | readWriteVectorBasedEdge<EdgePointXYZ>(); |
| 124 | } |
| 125 | |
| 126 | TEST(IoSlam3d, ReadWriteEdgeXYZPrior) { |
| 127 | readWriteVectorBasedEdge<EdgeXYZPrior>(); |
| 128 | } |
| 129 | |
| 130 | TEST_F(IoSlam3dParam, ReadWriteEdgeSE3Offset) { |
| 131 | // additional graph structures |
| 132 | ParameterSE3Offset* paramOffset2 = new ParameterSE3Offset(); |
| 133 | paramOffset2->setId(paramId++); |
| 134 | graph->addParameter(paramOffset2); |
| 135 | |
| 136 | // setting up the edge for output |
| 137 | EdgeSE3Offset* outputEdge = new EdgeSE3Offset(); |
| 138 | outputEdge->setParameterId(1, paramOffset2->id()); |
| 139 | ASSERT_TRUE(preparePosePoseEdge(outputEdge)); |
| 140 | |
| 141 | // Test IO |
| 142 | readWriteVectorBasedEdge<EdgeSE3Offset, internal::RandomIsometry3>( |
| 143 | outputEdge); |
| 144 | } |
| 145 | |
| 146 | TEST_F(IoSlam3dParam, ReadWriteEdgeSE3PointXYZ) { |
| 147 | EdgeSE3PointXYZ* outputEdge = new EdgeSE3PointXYZ(); |
| 148 | ASSERT_TRUE(preparePosePointEdge(outputEdge)); |
| 149 | readWriteVectorBasedEdge<EdgeSE3PointXYZ>(outputEdge); |
| 150 | } |
| 151 | |
| 152 | TEST_F(IoSlam3dParam, ReadWriteEdgeSE3Prior) { |
| 153 | EdgeSE3Prior* outputEdge = new EdgeSE3Prior(); |
| 154 | ASSERT_TRUE(preparePosePoseEdge(outputEdge)); |
| 155 | readWriteVectorBasedEdge<EdgeSE3Prior, internal::RandomIsometry3>(outputEdge); |
| 156 | } |
| 157 | |
| 158 | TEST_F(IoSlam3dParam, ReadWriteEdgeSE3PointXYZDepth) { |
| 159 | EdgeSE3PointXYZDepth* outputEdge = new EdgeSE3PointXYZDepth(); |
| 160 | ASSERT_TRUE(prepareCamPointEdge(outputEdge)); |
| 161 | readWriteVectorBasedEdge<EdgeSE3PointXYZDepth>(outputEdge); |
| 162 | } |
| 163 | |
| 164 | TEST_F(IoSlam3dParam, ReadWriteEdgeSE3PointXYZDisparity) { |
| 165 | EdgeSE3PointXYZDisparity* outputEdge = new EdgeSE3PointXYZDisparity(); |
| 166 | ASSERT_TRUE(prepareCamPointEdge(outputEdge)); |
| 167 | readWriteVectorBasedEdge<EdgeSE3PointXYZDisparity>(outputEdge); |
| 168 | } |
| 169 | |
| 170 | TEST_F(IoSlam3dParam, ReadWriteEdgeSE3XYZPrior) { |
| 171 | EdgeSE3XYZPrior* outputEdge = new EdgeSE3XYZPrior(); |
| 172 | ASSERT_TRUE(preparePosePoseEdge(outputEdge)); |
| 173 | readWriteVectorBasedEdge<EdgeSE3XYZPrior>(outputEdge); |
| 174 | } |
| 175 | |
| 176 | // Parameter tests |
| 177 | TEST(IoSlam3d, ReadWriteParameterCamera) { |
| 178 | ParameterCamera outputParam; |
| 179 | outputParam.setOffset(internal::RandomIsometry3::create()); |
| 180 | outputParam.setKcam(1, 2, 3, 4); // just some test values for read/write |
| 181 | ParameterCamera inputParam; |
| 182 | readWriteGraphElement(outputParam, &inputParam); |
| 183 | ASSERT_TRUE(internal::RandomIsometry3::isApprox(outputParam.offset(), |
| 184 | inputParam.offset())); |
| 185 | ASSERT_TRUE(outputParam.Kcam().isApprox(inputParam.Kcam(), 1e-5)); |
| 186 | } |
| 187 | |
| 188 | TEST(IoSlam3d, ReadWriteParameterStereoCamera) { |
| 189 | ParameterStereoCamera outputParam; |
| 190 | outputParam.setOffset(internal::RandomIsometry3::create()); |
| 191 | outputParam.setBaseline(0.1); |
| 192 | outputParam.setKcam(1, 2, 3, 4); // just some test values for read/write |
| 193 | ParameterStereoCamera inputParam; |
| 194 | readWriteGraphElement(outputParam, &inputParam); |
| 195 | ASSERT_DOUBLE_EQ(outputParam.baseline(), inputParam.baseline()); |
| 196 | ASSERT_TRUE(internal::RandomIsometry3::isApprox(outputParam.offset(), |
| 197 | inputParam.offset())); |
| 198 | ASSERT_TRUE(outputParam.Kcam().isApprox(inputParam.Kcam(), 1e-5)); |
| 199 | } |
| 200 | |
| 201 | TEST(IoSlam3d, ReadWriteParameterSE3Offset) { |
| 202 | ParameterSE3Offset outputParam; |
| 203 | outputParam.setOffset(internal::RandomIsometry3::create()); |
| 204 | ParameterSE3Offset inputParam; |
| 205 | readWriteGraphElement(outputParam, &inputParam); |
| 206 | ASSERT_TRUE(internal::RandomIsometry3::isApprox(outputParam.offset(), |
| 207 | inputParam.offset())); |
| 208 | } |