| 1 | // g2o - General Graph Optimization |
| 2 | // Copyright (C) 2011 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 "g2o/core/base_fixed_sized_edge.h" |
| 28 | |
| 29 | #include "g2o/core/base_unary_edge.h" |
| 30 | #include "g2o/core/base_variable_sized_edge.h" |
| 31 | #include "g2o/core/eigen_types.h" |
| 32 | #include "g2o/core/robust_kernel_impl.h" |
| 33 | #include "g2o/types/slam2d/vertex_point_xy.h" |
| 34 | #include "g2o/types/slam2d/vertex_se2.h" |
| 35 | #include "gtest/gtest.h" |
| 36 | |
| 37 | class Edge3Constant |
| 38 | : public g2o::BaseFixedSizedEdge<2, g2o::Vector2, g2o::VertexSE2, |
| 39 | g2o::VertexSE2, g2o::VertexPointXY> { |
| 40 | public: |
| 41 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW; |
| 42 | Edge3Constant() |
| 43 | : g2o::BaseFixedSizedEdge<2, g2o::Vector2, g2o::VertexSE2, g2o::VertexSE2, |
| 44 | g2o::VertexPointXY>(){}; |
| 45 | void computeError() { |
| 46 | const auto a = static_cast<const g2o::VertexSE2*>(_vertices[0])->estimate(); |
| 47 | const auto b = static_cast<const g2o::VertexSE2*>(_vertices[1])->estimate(); |
| 48 | const auto c = |
| 49 | static_cast<const g2o::VertexPointXY*>(_vertices[2])->estimate(); |
| 50 | _error = (a * b * c - _measurement).eval(); |
| 51 | } |
| 52 | virtual bool read(std::istream&) { return false; }; |
| 53 | virtual bool write(std::ostream&) const { return false; }; |
| 54 | }; |
| 55 | |
| 56 | class Edge3Dynamic : public g2o::BaseVariableSizedEdge<2, g2o::Vector2> { |
| 57 | public: |
| 58 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW; |
| 59 | Edge3Dynamic() : g2o::BaseVariableSizedEdge<2, g2o::Vector2>() { resize(3); }; |
| 60 | void computeError() { |
| 61 | const auto a = static_cast<const g2o::VertexSE2*>(_vertices[0])->estimate(); |
| 62 | const auto b = static_cast<const g2o::VertexSE2*>(_vertices[1])->estimate(); |
| 63 | const auto c = |
| 64 | static_cast<const g2o::VertexPointXY*>(_vertices[2])->estimate(); |
| 65 | _error = (a * b * c - _measurement).eval(); |
| 66 | } |
| 67 | virtual bool read(std::istream&) { return false; }; |
| 68 | virtual bool write(std::ostream&) const { return false; }; |
| 69 | }; |
| 70 | |
| 71 | class VertexNotDefaultCtor : public g2o::BaseVertex<2, g2o::Vector2> { |
| 72 | public: |
| 73 | VertexNotDefaultCtor(int x, int y) { _estimate = g2o::Vector2(x, y); } |
| 74 | |
| 75 | virtual void oplusImpl(const double* update) { |
| 76 | _estimate[0] += update[0]; |
| 77 | _estimate[1] += update[1]; |
| 78 | } |
| 79 | |
| 80 | virtual void setToOriginImpl() { _estimate.setZero(); } |
| 81 | virtual bool read(std::istream& /*is*/) { return false; }; |
| 82 | virtual bool write(std::ostream& /*os*/) const { return false; }; |
| 83 | }; |
| 84 | |
| 85 | class EdgeUnaryCreateVertexTester |
| 86 | : public g2o::BaseUnaryEdge<2, g2o::Vector2, VertexNotDefaultCtor> { |
| 87 | public: |
| 88 | EdgeUnaryCreateVertexTester() = default; |
| 89 | |
| 90 | void computeError() { |
| 91 | const VertexNotDefaultCtor* v = |
| 92 | static_cast<const VertexNotDefaultCtor*>(_vertices[0]); |
| 93 | _error = v->estimate() - _measurement; |
| 94 | } |
| 95 | virtual bool read(std::istream& /*is*/) { return false; }; |
| 96 | virtual bool write(std::ostream& /*os*/) const { return false; }; |
| 97 | |
| 98 | virtual void setMeasurement(const g2o::Vector2& m) { _measurement = m; } |
| 99 | |
| 100 | virtual int measurementDimension() const { return 2; } |
| 101 | }; |
| 102 | |
| 103 | TEST(General, IndexToPairToIndex) { |
| 104 | using g2o::internal::index_to_pair; |
| 105 | using g2o::internal::pair_to_index; |
| 106 | using g2o::internal::TrivialPair; |
| 107 | |
| 108 | ASSERT_EQ(index_to_pair(0).first, 0); |
| 109 | ASSERT_EQ(index_to_pair(0).second, 1); |
| 110 | ASSERT_EQ(index_to_pair(1).first, 0); |
| 111 | ASSERT_EQ(index_to_pair(1).second, 2); |
| 112 | ASSERT_EQ(index_to_pair(2).first, 1); |
| 113 | ASSERT_EQ(index_to_pair(2).second, 2); |
| 114 | ASSERT_EQ(pair_to_index(0, 1), 0); |
| 115 | ASSERT_EQ(pair_to_index(0, 2), 1); |
| 116 | ASSERT_EQ(pair_to_index(1, 2), 2); |
| 117 | for (int j = 0; j < 32; ++j) |
| 118 | for (int i = 0; i < j; ++i) |
| 119 | ASSERT_EQ(index_to_pair(pair_to_index(i, j)), TrivialPair(i, j)); |
| 120 | for (int k = 0; k < 1024; ++k) |
| 121 | ASSERT_EQ(pair_to_index(index_to_pair(k).first, index_to_pair(k).second), |
| 122 | k); |
| 123 | |
| 124 | int k = 0; |
| 125 | for (int j = 0; j < 32; ++j) |
| 126 | for (int i = 0; i < j; ++i) { |
| 127 | ASSERT_EQ(pair_to_index(i, j), k); |
| 128 | ++k; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | TEST(General, ConstantEdgeConstructor) { |
| 133 | ASSERT_EQ(typeid(Edge3Dynamic::ErrorVector), |
| 134 | typeid(Edge3Constant::ErrorVector)); |
| 135 | ASSERT_EQ(typeid(Edge3Dynamic::InformationType), |
| 136 | typeid(Edge3Constant::InformationType)); |
| 137 | |
| 138 | Edge3Constant e_constant; |
| 139 | ASSERT_EQ(e_constant.vertices()[0], nullptr); |
| 140 | ASSERT_EQ(e_constant.vertices()[1], nullptr); |
| 141 | ASSERT_EQ(e_constant.vertices()[2], nullptr); |
| 142 | Edge3Dynamic e_dynamic; |
| 143 | ASSERT_EQ(e_dynamic.vertices()[0], e_constant.vertices()[0]); |
| 144 | ASSERT_EQ(e_dynamic.vertices()[1], e_constant.vertices()[1]); |
| 145 | ASSERT_EQ(e_dynamic.vertices()[2], e_constant.vertices()[2]); |
| 146 | } |
| 147 | |
| 148 | TEST(General, FixedEdgeCreateVertex) { |
| 149 | Edge3Constant e; |
| 150 | |
| 151 | auto* v1 = e.createVertex(0); |
| 152 | auto* v2 = e.createVertex(1); |
| 153 | auto* v3 = e.createVertex(2); |
| 154 | ASSERT_EQ(typeid(*v1), typeid(g2o::VertexSE2)); |
| 155 | ASSERT_EQ(typeid(*v2), typeid(g2o::VertexSE2)); |
| 156 | ASSERT_EQ(typeid(*v3), typeid(g2o::VertexPointXY)); |
| 157 | delete v3; |
| 158 | delete v2; |
| 159 | delete v1; |
| 160 | |
| 161 | ASSERT_EQ(nullptr, e.createVertex(-1)); |
| 162 | ASSERT_EQ(nullptr, e.createVertex(3)); |
| 163 | } |
| 164 | |
| 165 | TEST(General, FixedEdgeCreateVertexNonDefaultCtor) { |
| 166 | EdgeUnaryCreateVertexTester edge; |
| 167 | auto* vertex = edge.createVertex(0, 42, 23); |
| 168 | EXPECT_NE(vertex, nullptr); |
| 169 | ASSERT_EQ(typeid(*vertex), typeid(VertexNotDefaultCtor)); |
| 170 | auto* casted_vertex = dynamic_cast<VertexNotDefaultCtor*>(vertex); |
| 171 | ASSERT_NE(casted_vertex, nullptr); |
| 172 | EXPECT_TRUE(casted_vertex->estimate().isApprox(g2o::Vector2(42, 23))); |
| 173 | } |
| 174 | |
| 175 | template <typename EdgeType> |
| 176 | class EdgeTester { |
| 177 | public: |
| 178 | EdgeTester() { |
| 179 | edge.setMeasurement(g2o::Vector2{.3, .4}); |
| 180 | edge.setInformation(g2o::Matrix2::Identity()); |
| 181 | |
| 182 | v1.setId(0); |
| 183 | v1.setEstimate(g2o::SE2(.1, .2, .3)); |
| 184 | v2.setId(1); |
| 185 | v2.setEstimate(g2o::SE2(.3, .1, .2)); |
| 186 | v3.setId(2); |
| 187 | v3.setEstimate(g2o::Vector2(-.3, .5)); |
| 188 | edge.setVertex(0, &v1); |
| 189 | edge.setVertex(1, &v2); |
| 190 | edge.setVertex(2, &v3); |
| 191 | |
| 192 | jacobianWorkspace.updateSize(&edge); |
| 193 | jacobianWorkspace.allocate(); |
| 194 | |
| 195 | hessian01.setZero(); |
| 196 | hessian02.setZero(); |
| 197 | hessian12.setZero(); |
| 198 | hessian00.setZero(); |
| 199 | hessian11.setZero(); |
| 200 | hessian22.setZero(); |
| 201 | edge.mapHessianMemory(hessian01.data(), 0, 1, false); |
| 202 | edge.mapHessianMemory(hessian02.data(), 0, 2, false); |
| 203 | edge.mapHessianMemory(hessian12.data(), 1, 2, false); |
| 204 | v1.mapHessianMemory(hessian00.data()); |
| 205 | v2.mapHessianMemory(hessian11.data()); |
| 206 | v3.mapHessianMemory(hessian22.data()); |
| 207 | } |
| 208 | |
| 209 | EdgeType edge; |
| 210 | |
| 211 | g2o::VertexSE2 v1; |
| 212 | g2o::VertexSE2 v2; |
| 213 | g2o::VertexPointXY v3; |
| 214 | |
| 215 | g2o::JacobianWorkspace jacobianWorkspace; |
| 216 | |
| 217 | Eigen::Matrix<double, 3, 3> hessian01; |
| 218 | Eigen::Matrix<double, 3, 2> hessian02; |
| 219 | Eigen::Matrix<double, 3, 2> hessian12; |
| 220 | Eigen::Matrix<double, 3, 3> hessian00; |
| 221 | Eigen::Matrix<double, 3, 3> hessian11; |
| 222 | Eigen::Matrix<double, 2, 2> hessian22; |
| 223 | }; |
| 224 | |
| 225 | TEST(ConstantEdgeTest, ConstantEdge_allVerticesFixed) { |
| 226 | EdgeTester<Edge3Dynamic> dynamic; |
| 227 | EdgeTester<Edge3Constant> constant; |
| 228 | ASSERT_EQ(dynamic.edge.allVerticesFixed(), constant.edge.allVerticesFixed()); |
| 229 | ASSERT_FALSE(constant.edge.allVerticesFixed()); |
| 230 | dynamic.v1.setFixed(true); |
| 231 | dynamic.v2.setFixed(true); |
| 232 | dynamic.v3.setFixed(true); |
| 233 | constant.v1.setFixed(true); |
| 234 | constant.v2.setFixed(true); |
| 235 | constant.v3.setFixed(true); |
| 236 | ASSERT_EQ(dynamic.edge.allVerticesFixed(), constant.edge.allVerticesFixed()); |
| 237 | ASSERT_TRUE(constant.edge.allVerticesFixed()); |
| 238 | } |
| 239 | |
| 240 | TEST(ConstantEdgeTest, ConstantEdge_computeError) { |
| 241 | EdgeTester<Edge3Dynamic> dynamic; |
| 242 | EdgeTester<Edge3Constant> constant; |
| 243 | dynamic.edge.computeError(); |
| 244 | constant.edge.computeError(); |
| 245 | EXPECT_DOUBLE_EQ(0.0, (dynamic.edge.error() - constant.edge.error()).norm()); |
| 246 | } |
| 247 | |
| 248 | TEST(ConstantEdgeTest, ConstantEdge_linearizeOplus) { |
| 249 | EdgeTester<Edge3Dynamic> dynamic; |
| 250 | EdgeTester<Edge3Constant> constant; |
| 251 | dynamic.edge.computeError(); |
| 252 | constant.edge.computeError(); |
| 253 | dynamic.edge.linearizeOplus(dynamic.jacobianWorkspace); |
| 254 | constant.edge.linearizeOplus(constant.jacobianWorkspace); |
| 255 | EXPECT_DOUBLE_EQ(0.0, |
| 256 | (Eigen::Map<g2o::MatrixX>( |
| 257 | dynamic.jacobianWorkspace.workspaceForVertex(0), 2, 3) - |
| 258 | Eigen::Map<g2o::MatrixX>( |
| 259 | constant.jacobianWorkspace.workspaceForVertex(0), 2, 3)) |
| 260 | .norm()); |
| 261 | EXPECT_DOUBLE_EQ(0.0, |
| 262 | (Eigen::Map<g2o::MatrixX>( |
| 263 | dynamic.jacobianWorkspace.workspaceForVertex(1), 2, 3) - |
| 264 | Eigen::Map<g2o::MatrixX>( |
| 265 | constant.jacobianWorkspace.workspaceForVertex(1), 2, 3)) |
| 266 | .norm()); |
| 267 | EXPECT_DOUBLE_EQ(0.0, |
| 268 | (Eigen::Map<g2o::MatrixX>( |
| 269 | dynamic.jacobianWorkspace.workspaceForVertex(2), 2, 2) - |
| 270 | Eigen::Map<g2o::MatrixX>( |
| 271 | constant.jacobianWorkspace.workspaceForVertex(2), 2, 2)) |
| 272 | .norm()); |
| 273 | } |
| 274 | |
| 275 | TEST(ConstantEdgeTest, ConstantEdge_constructQuadraticForm) { |
| 276 | EdgeTester<Edge3Dynamic> dynamic; |
| 277 | EdgeTester<Edge3Constant> constant; |
| 278 | dynamic.edge.computeError(); |
| 279 | constant.edge.computeError(); |
| 280 | dynamic.edge.linearizeOplus(dynamic.jacobianWorkspace); |
| 281 | constant.edge.linearizeOplus(constant.jacobianWorkspace); |
| 282 | |
| 283 | dynamic.edge.constructQuadraticForm(); |
| 284 | constant.edge.constructQuadraticForm(); |
| 285 | |
| 286 | EXPECT_NEAR(0.0, (dynamic.hessian00 - constant.hessian00).norm(), 1e-7); |
| 287 | EXPECT_NEAR(0.0, (dynamic.hessian11 - constant.hessian11).norm(), 1e-7); |
| 288 | EXPECT_NEAR(0.0, (dynamic.hessian22 - constant.hessian22).norm(), 1e-7); |
| 289 | EXPECT_NEAR(0.0, (dynamic.hessian01 - constant.hessian01).norm(), 1e-7); |
| 290 | EXPECT_NEAR(0.0, (dynamic.hessian02 - constant.hessian02).norm(), 1e-7); |
| 291 | EXPECT_NEAR(0.0, (dynamic.hessian12 - constant.hessian12).norm(), 1e-7); |
| 292 | } |
| 293 | |
| 294 | TEST(ConstantEdgeTest, ConstantEdge_constructQuadraticForm_robust) { |
| 295 | EdgeTester<Edge3Dynamic> dynamic; |
| 296 | EdgeTester<Edge3Constant> constant; |
| 297 | |
| 298 | dynamic.edge.setMeasurement(g2o::Vector2{.3, 3.4}); |
| 299 | constant.edge.setMeasurement(g2o::Vector2{.3, 3.4}); |
| 300 | |
| 301 | // assuming that G2O_NO_IMPLICIT_OWNERSHIP_OF_OBJECTS is false |
| 302 | g2o::RobustKernelHuber* rk_dynamic = new g2o::RobustKernelHuber; |
| 303 | dynamic.edge.setRobustKernel(rk_dynamic); |
| 304 | dynamic.edge.computeError(); |
| 305 | dynamic.edge.linearizeOplus(dynamic.jacobianWorkspace); |
| 306 | dynamic.edge.constructQuadraticForm(); |
| 307 | |
| 308 | g2o::RobustKernelHuber* rk_constant = new g2o::RobustKernelHuber; |
| 309 | constant.edge.setRobustKernel(rk_constant); |
| 310 | constant.edge.computeError(); |
| 311 | constant.edge.linearizeOplus(constant.jacobianWorkspace); |
| 312 | constant.edge.constructQuadraticForm(); |
| 313 | EXPECT_NEAR(0, (dynamic.edge.error() - constant.edge.error()).norm(), 1e-7); |
| 314 | EXPECT_DOUBLE_EQ(0.0, |
| 315 | (Eigen::Map<g2o::MatrixX>( |
| 316 | dynamic.jacobianWorkspace.workspaceForVertex(0), 2, 3) - |
| 317 | Eigen::Map<g2o::MatrixX>( |
| 318 | constant.jacobianWorkspace.workspaceForVertex(0), 2, 3)) |
| 319 | .norm()); |
| 320 | EXPECT_NEAR(0.0, (dynamic.hessian00 - constant.hessian00).norm(), 1e-7); |
| 321 | EXPECT_NEAR(0.0, (dynamic.hessian11 - constant.hessian11).norm(), 1e-7); |
| 322 | EXPECT_NEAR(0.0, (dynamic.hessian22 - constant.hessian22).norm(), 1e-7); |
| 323 | EXPECT_NEAR(0.0, (dynamic.hessian01 - constant.hessian01).norm(), 1e-7); |
| 324 | EXPECT_NEAR(0.0, (dynamic.hessian02 - constant.hessian02).norm(), 1e-7); |
| 325 | EXPECT_NEAR(0.0, (dynamic.hessian12 - constant.hessian12).norm(), 1e-7); |
| 326 | } |
| 327 | |
| 328 | TEST(ConstantEdgeTest, ConstantEdge_constructQuadraticForm_rowMajor) { |
| 329 | EdgeTester<Edge3Dynamic> dynamic; |
| 330 | EdgeTester<Edge3Constant> constant; |
| 331 | dynamic.edge.mapHessianMemory(dynamic.hessian01.data(), 0, 1, true); |
| 332 | Eigen::Matrix<double, 2, 3> hessian20_dynamic; |
| 333 | Eigen::Matrix<double, 2, 3> hessian21_dynamic; |
| 334 | hessian20_dynamic.setZero(); |
| 335 | hessian21_dynamic.setZero(); |
| 336 | dynamic.edge.mapHessianMemory(hessian20_dynamic.data(), 0, 2, true); |
| 337 | dynamic.edge.mapHessianMemory(hessian21_dynamic.data(), 1, 2, true); |
| 338 | |
| 339 | constant.edge.mapHessianMemory(constant.hessian01.data(), 0, 1, true); |
| 340 | Eigen::Matrix<double, 2, 3> hessian20_constant; |
| 341 | Eigen::Matrix<double, 2, 3> hessian21_constant; |
| 342 | hessian20_constant.setZero(); |
| 343 | hessian21_constant.setZero(); |
| 344 | constant.edge.mapHessianMemory(hessian20_constant.data(), 0, 2, true); |
| 345 | constant.edge.mapHessianMemory(hessian21_constant.data(), 1, 2, true); |
| 346 | |
| 347 | dynamic.edge.computeError(); |
| 348 | constant.edge.computeError(); |
| 349 | dynamic.edge.linearizeOplus(dynamic.jacobianWorkspace); |
| 350 | constant.edge.linearizeOplus(constant.jacobianWorkspace); |
| 351 | |
| 352 | dynamic.edge.constructQuadraticForm(); |
| 353 | constant.edge.constructQuadraticForm(); |
| 354 | |
| 355 | EXPECT_NEAR(0.0, (dynamic.hessian00 - constant.hessian00).norm(), 1e-7); |
| 356 | EXPECT_NEAR(0.0, (dynamic.hessian11 - constant.hessian11).norm(), 1e-7); |
| 357 | EXPECT_NEAR(0.0, (dynamic.hessian22 - constant.hessian22).norm(), 1e-7); |
| 358 | EXPECT_NEAR(0.0, (dynamic.hessian01 - constant.hessian01).norm(), 1e-7); |
| 359 | EXPECT_NEAR(0.0, (hessian20_dynamic - hessian20_constant).norm(), 1e-7); |
| 360 | EXPECT_NEAR(0.0, (hessian21_dynamic - hessian21_constant).norm(), 1e-7); |
| 361 | |
| 362 | EdgeTester<Edge3Constant> constant_colMajor; |
| 363 | constant_colMajor.edge.computeError(); |
| 364 | constant_colMajor.edge.linearizeOplus(constant_colMajor.jacobianWorkspace); |
| 365 | constant_colMajor.edge.constructQuadraticForm(); |
| 366 | EXPECT_NEAR( |
| 367 | 0.0, |
| 368 | (constant_colMajor.hessian01 - constant.hessian01.transpose()).norm(), |
| 369 | 1e-7); |
| 370 | EXPECT_NEAR( |
| 371 | 0.0, |
| 372 | (constant_colMajor.hessian02 - hessian20_constant.transpose()).norm(), |
| 373 | 1e-7); |
| 374 | EXPECT_NEAR( |
| 375 | 0.0, |
| 376 | (constant_colMajor.hessian12 - hessian21_constant.transpose()).norm(), |
| 377 | 1e-7); |
| 378 | } |
| 379 | |
| 380 | TEST(ConstantEdgeTest, NotCopyAssignable) { |
| 381 | EXPECT_FALSE(std::is_copy_assignable<Edge3Constant>::value); |
| 382 | EXPECT_FALSE(std::is_copy_assignable<VertexNotDefaultCtor>::value); |
| 383 | } |