| 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 <gmock/gmock.h> |
| 28 | #include <gtest/gtest.h> |
| 29 | |
| 30 | #include <algorithm> |
| 31 | #include <memory> |
| 32 | #include <numeric> |
| 33 | #include <unordered_set> |
| 34 | |
| 35 | #include "allocate_optimizer.h" |
| 36 | #include "g2o/core/eigen_types.h" |
| 37 | #include "g2o/core/factory.h" |
| 38 | #include "g2o/core/hyper_dijkstra.h" |
| 39 | #include "g2o/core/hyper_graph.h" |
| 40 | #include "g2o/core/jacobian_workspace.h" |
| 41 | #include "g2o/core/optimizable_graph.h" |
| 42 | #include "g2o/core/optimization_algorithm_property.h" |
| 43 | #include "g2o/core/sparse_optimizer.h" |
| 44 | #include "g2o/stuff/string_tools.h" |
| 45 | #include "g2o/types/slam2d/types_slam2d.h" |
| 46 | |
| 47 | G2O_USE_TYPE_GROUP(slam2d); |
| 48 | |
| 49 | namespace { |
| 50 | class JacobianWorkspaceTestAdapter : public g2o::JacobianWorkspace { |
| 51 | public: |
| 52 | [[nodiscard]] WorkspaceVector& workspace() { return _workspace; } |
| 53 | [[nodiscard]] int maxNumVertices() const { return _maxNumVertices; } |
| 54 | [[nodiscard]] int maxDimension() const { return _maxDimension; } |
| 55 | }; |
| 56 | } // namespace |
| 57 | |
| 58 | TEST(General, BinaryEdgeConstructor) { |
| 59 | g2o::EdgeSE2 e2; |
| 60 | ASSERT_EQ(nullptr, e2.vertices()[0]); |
| 61 | ASSERT_EQ(nullptr, e2.vertices()[1]); |
| 62 | } |
| 63 | |
| 64 | TEST(General, GraphAddVertex) { |
| 65 | g2o::SparseOptimizer* optimizer = g2o::internal::createOptimizerForTests(); |
| 66 | |
| 67 | g2o::VertexSE2* v1 = new g2o::VertexSE2(); |
| 68 | v1->setId(0); |
| 69 | ASSERT_TRUE(optimizer->addVertex(v1)); |
| 70 | ASSERT_EQ(optimizer, v1->graph()); |
| 71 | ASSERT_EQ(size_t(1), optimizer->vertices().size()); |
| 72 | ASSERT_FALSE(optimizer->addVertex(v1)); |
| 73 | ASSERT_EQ(size_t(1), optimizer->vertices().size()); |
| 74 | |
| 75 | auto* v1_from_optimizer = optimizer->vertex(0); |
| 76 | ASSERT_EQ(v1, v1_from_optimizer); |
| 77 | |
| 78 | { |
| 79 | g2o::VertexSE2* v2 = new g2o::VertexSE2(); |
| 80 | v2->setId(0); |
| 81 | ASSERT_FALSE(optimizer->addVertex(v2)); |
| 82 | ASSERT_EQ(size_t(1), optimizer->vertices().size()); |
| 83 | ASSERT_EQ(nullptr, v2->graph()); |
| 84 | delete v2; |
| 85 | } |
| 86 | |
| 87 | const bool removed = optimizer->removeVertex(v1, true); |
| 88 | ASSERT_TRUE(removed); |
| 89 | ASSERT_TRUE(optimizer->vertices().empty()); |
| 90 | |
| 91 | delete optimizer; |
| 92 | } |
| 93 | |
| 94 | TEST(General, GraphAddEdge) { |
| 95 | g2o::SparseOptimizer* optimizer = g2o::internal::createOptimizerForTests(); |
| 96 | |
| 97 | g2o::VertexSE2* v1 = new g2o::VertexSE2(); |
| 98 | v1->setId(0); |
| 99 | g2o::VertexSE2* v2 = new g2o::VertexSE2(); |
| 100 | v2->setId(1); |
| 101 | |
| 102 | ASSERT_TRUE(optimizer->addVertex(v1)); |
| 103 | ASSERT_TRUE(optimizer->addVertex(v2)); |
| 104 | |
| 105 | g2o::EdgeSE2* e1 = new g2o::EdgeSE2(); |
| 106 | e1->setVertex(0, v1); |
| 107 | e1->setVertex(1, v2); |
| 108 | ASSERT_TRUE(optimizer->addEdge(e1)); |
| 109 | ASSERT_EQ(optimizer, e1->graph()); |
| 110 | ASSERT_FALSE(optimizer->addEdge(e1)); |
| 111 | ASSERT_EQ(size_t(1), optimizer->edges().size()); |
| 112 | ASSERT_EQ(size_t(1), v1->edges().size()); |
| 113 | ASSERT_EQ(size_t(1), v1->edges().size()); |
| 114 | ASSERT_EQ(size_t(1), v1->edges().count(e1)); |
| 115 | ASSERT_EQ(size_t(1), v2->edges().count(e1)); |
| 116 | |
| 117 | g2o::EdgeSE2* e2 = new g2o::EdgeSE2(); |
| 118 | ASSERT_FALSE(optimizer->addEdge(e2)) |
| 119 | << "Adding edge with unset vertices was possible"; |
| 120 | ASSERT_EQ(size_t(1), optimizer->edges().size()); |
| 121 | ASSERT_EQ(nullptr, e2->graph()); |
| 122 | |
| 123 | g2o::EdgeSE2* e3 = new g2o::EdgeSE2(); |
| 124 | e3->setVertex(0, v1); |
| 125 | e3->setVertex(1, v1); |
| 126 | ASSERT_FALSE(optimizer->addEdge(e3)) |
| 127 | << "Adding binary edge with same vertices was possible"; |
| 128 | ASSERT_EQ(size_t(1), optimizer->edges().size()); |
| 129 | |
| 130 | const bool removed = optimizer->removeEdge(e1); |
| 131 | ASSERT_TRUE(removed); |
| 132 | ASSERT_TRUE(optimizer->edges().empty()); |
| 133 | |
| 134 | delete e2; |
| 135 | delete e3; |
| 136 | delete optimizer; |
| 137 | } |
| 138 | |
| 139 | TEST(General, GraphChangeId) { |
| 140 | std::unique_ptr<g2o::SparseOptimizer> optimizer( |
| 141 | g2o::internal::createOptimizerForTests()); |
| 142 | |
| 143 | g2o::VertexSE2* v1 = new g2o::VertexSE2(); |
| 144 | v1->setId(0); |
| 145 | g2o::VertexSE2 v2; |
| 146 | v2.setId(1); |
| 147 | optimizer->addVertex(v1); |
| 148 | |
| 149 | EXPECT_EQ(v1->id(), 0); |
| 150 | const bool changed_id = optimizer->changeId(v1, 42); |
| 151 | EXPECT_TRUE(changed_id); |
| 152 | EXPECT_EQ(v1->id(), 42); |
| 153 | |
| 154 | EXPECT_EQ(v2.id(), 1); |
| 155 | const bool not_changed = optimizer->changeId(&v2, 17); |
| 156 | EXPECT_FALSE(not_changed); |
| 157 | EXPECT_EQ(v2.id(), 1); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Fixture to test saving and loading of a graph. |
| 162 | * Here, we will have a simple graph with N nodes and N edges. |
| 163 | * We use for saving and loading. |
| 164 | */ |
| 165 | class GeneralGraphOperations : public ::testing::Test { |
| 166 | protected: |
| 167 | void SetUp() override { |
| 168 | optimizer.reset(g2o::internal::createOptimizerForTests()); |
| 169 | |
| 170 | // Add vertices |
| 171 | for (size_t i = 0; i < numVertices; ++i) { |
| 172 | g2o::VertexSE2* v = new g2o::VertexSE2; |
| 173 | v->setEstimate(g2o::SE2()); |
| 174 | v->setId(i); |
| 175 | v->setFixed(i == 0); // fix the first vertex |
| 176 | optimizer->addVertex(v); |
| 177 | } |
| 178 | |
| 179 | // Add edges |
| 180 | for (size_t i = 0; i < numVertices; ++i) { |
| 181 | g2o::EdgeSE2* e1 = new g2o::EdgeSE2(); |
| 182 | e1->vertices()[0] = optimizer->vertex((i + 0) % numVertices); |
| 183 | e1->vertices()[1] = optimizer->vertex((i + 1) % numVertices); |
| 184 | e1->setMeasurement(g2o::SE2(1., 0., 0.)); |
| 185 | e1->setInformation(g2o::EdgeSE2::InformationType::Identity()); |
| 186 | optimizer->addEdge(e1); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | // void TearDown() override {} |
| 191 | |
| 192 | //! recover the vertex IDs from a output generated by OptimizableGraph::save() |
| 193 | static std::vector<int> parseVertexIds(std::istream& is) { |
| 194 | std::vector<int> result; |
| 195 | std::stringstream currentLine; |
| 196 | while (g2o::readLine(is, currentLine) >= 0) { |
| 197 | std::string token; |
| 198 | currentLine >> token; |
| 199 | if (g2o::strStartsWith(token, "VERTEX_")) { |
| 200 | int id; |
| 201 | currentLine >> id; |
| 202 | result.push_back(id); |
| 203 | } |
| 204 | } |
| 205 | return result; |
| 206 | } |
| 207 | |
| 208 | //! recover the fixed IDs from a output generated by OptimizableGraph::save() |
| 209 | static std::vector<int> parseFixedIds(std::istream& is) { |
| 210 | std::vector<int> result; |
| 211 | std::stringstream currentLine; |
| 212 | while (g2o::readLine(is, currentLine) >= 0) { |
| 213 | std::string token; |
| 214 | currentLine >> token; |
| 215 | if (token == "FIX") { |
| 216 | int id; |
| 217 | while (currentLine >> id) result.push_back(id); |
| 218 | } |
| 219 | } |
| 220 | return result; |
| 221 | } |
| 222 | |
| 223 | //! recover the vertec IDs of the edges from a output generated by |
| 224 | //! OptimizableGraph::save() |
| 225 | static std::vector<std::pair<int, int>> parseEdgeIds(std::istream& is) { |
| 226 | std::vector<std::pair<int, int>> result; |
| 227 | std::stringstream currentLine; |
| 228 | while (g2o::readLine(is, currentLine) >= 0) { |
| 229 | std::string token; |
| 230 | currentLine >> token; |
| 231 | if (g2o::strStartsWith(token, "EDGE_")) { |
| 232 | int id1, id2; |
| 233 | currentLine >> id1 >> id2; |
| 234 | result.emplace_back(std::make_pair(id1, id2)); |
| 235 | } |
| 236 | } |
| 237 | return result; |
| 238 | } |
| 239 | |
| 240 | //! helper method to restart reading at the start of a stream |
| 241 | static void resetStream(std::istream& is) { |
| 242 | is.clear(); |
| 243 | is.seekg(0, std::ios::beg); |
| 244 | } |
| 245 | |
| 246 | //! returns the expected Vertex IDs of the fixture |
| 247 | std::vector<int> expectedIds() const { |
| 248 | std::vector<int> result(numVertices); |
| 249 | std::iota(result.begin(), result.end(), 0); |
| 250 | return result; |
| 251 | } |
| 252 | |
| 253 | //! returns the expected Vertex IDs of the edges of the fixture |
| 254 | std::vector<std::pair<int, int>> expectedEdgeIds() const { |
| 255 | std::vector<std::pair<int, int>> result; |
| 256 | for (size_t i = 0; i < numVertices; ++i) |
| 257 | result.emplace_back((i + 0) % numVertices, (i + 1) % numVertices); |
| 258 | return result; |
| 259 | } |
| 260 | |
| 261 | std::map<int, g2o::Vector3> vertexEstimates() const { |
| 262 | std::map<int, g2o::Vector3> result; |
| 263 | for (const auto& idV : optimizer->vertices()) { |
| 264 | g2o::VertexSE2* v = dynamic_cast<g2o::VertexSE2*>(idV.second); |
| 265 | result[idV.first] = v->estimate().toVector(); |
| 266 | } |
| 267 | return result; |
| 268 | } |
| 269 | |
| 270 | //! returns the expected Vertex IDs of the fixture |
| 271 | std::vector<int> fixedIds() const { |
| 272 | std::vector<int> result; |
| 273 | for (const auto& idV : optimizer->vertices()) { |
| 274 | g2o::OptimizableGraph::Vertex* v = |
| 275 | dynamic_cast<g2o::OptimizableGraph::Vertex*>(idV.second); |
| 276 | if (v->fixed()) result.push_back(v->id()); |
| 277 | } |
| 278 | return result; |
| 279 | } |
| 280 | |
| 281 | std::unique_ptr<g2o::SparseOptimizer> optimizer; |
| 282 | size_t numVertices = 3; |
| 283 | }; |
| 284 | |
| 285 | TEST_F(GeneralGraphOperations, SavingGraph) { |
| 286 | std::stringstream graphData; |
| 287 | optimizer->save(graphData); |
| 288 | |
| 289 | // Analyze the written graph data |
| 290 | resetStream(graphData); |
| 291 | auto vertexIds = parseVertexIds(graphData); |
| 292 | resetStream(graphData); |
| 293 | auto fixedIds = parseFixedIds(graphData); |
| 294 | resetStream(graphData); |
| 295 | auto edgeIds = parseEdgeIds(graphData); |
| 296 | |
| 297 | EXPECT_THAT(vertexIds, testing::ElementsAreArray(expectedIds())); |
| 298 | EXPECT_THAT(fixedIds, testing::ElementsAre(0)); |
| 299 | EXPECT_THAT(edgeIds, testing::ElementsAreArray(expectedEdgeIds())); |
| 300 | } |
| 301 | |
| 302 | namespace internal { |
| 303 | using KeyIntVector = std::vector<decltype(testing::Key(42))>; |
| 304 | static KeyIntVector VectorIntToKeys(const std::vector<int>& keys) { |
| 305 | KeyIntVector matchers; |
| 306 | for (const auto& val : keys) matchers.push_back(testing::Key(val)); |
| 307 | return matchers; |
| 308 | } |
| 309 | } // namespace internal |
| 310 | |
| 311 | TEST_F(GeneralGraphOperations, LoadingGraph) { |
| 312 | std::stringstream graphData; |
| 313 | optimizer->save(graphData); |
| 314 | |
| 315 | // clear the graph and make sure it is actually empty |
| 316 | optimizer->clear(); |
| 317 | ASSERT_THAT(optimizer->vertices(), testing::SizeIs(0)); |
| 318 | ASSERT_THAT(optimizer->edges(), testing::SizeIs(0)); |
| 319 | |
| 320 | // load the graph and test the loaded vertices and edges |
| 321 | optimizer->load(graphData); |
| 322 | ASSERT_THAT(optimizer->vertices(), testing::SizeIs(numVertices)); |
| 323 | ASSERT_THAT(optimizer->edges(), testing::SizeIs(numVertices)); |
| 324 | ASSERT_THAT(optimizer->dimensions(), testing::ElementsAre(3)); |
| 325 | |
| 326 | ASSERT_THAT(optimizer->vertices(), |
| 327 | testing::UnorderedElementsAreArray( |
| 328 | internal::VectorIntToKeys(expectedIds()))); |
| 329 | ASSERT_THAT(optimizer->edges(), |
| 330 | testing::Each(testing::Property( |
| 331 | &g2o::OptimizableGraph::Edge::vertices, testing::SizeIs(2)))); |
| 332 | ASSERT_THAT( |
| 333 | optimizer->edges(), |
| 334 | testing::Each(testing::ResultOf( |
| 335 | [](g2o::HyperGraph::Edge* e) { |
| 336 | return static_cast<g2o::OptimizableGraph::Edge*>(e)->graph(); |
| 337 | }, |
| 338 | testing::Eq(static_cast<g2o::OptimizableGraph*>(optimizer.get()))))); |
| 339 | ASSERT_THAT(optimizer->edges(), testing::Each(testing::ResultOf( |
| 340 | [](g2o::HyperGraph::Edge* e) { |
| 341 | return std::make_pair( |
| 342 | e->vertex(0)->id(), |
| 343 | e->vertex(1)->id()); |
| 344 | }, |
| 345 | testing::AnyOfArray(expectedEdgeIds())))); |
| 346 | } |
| 347 | |
| 348 | TEST_F(GeneralGraphOperations, SaveSubsetVertices) { |
| 349 | g2o::OptimizableGraph::VertexSet verticesToSave{optimizer->vertex(0), |
| 350 | optimizer->vertex(1)}; |
| 351 | |
| 352 | std::stringstream graphData; |
| 353 | optimizer->saveSubset(graphData, verticesToSave); |
| 354 | optimizer->clear(); |
| 355 | |
| 356 | optimizer->load(graphData); |
| 357 | EXPECT_THAT(optimizer->vertices(), testing::SizeIs(2)); |
| 358 | EXPECT_THAT(optimizer->edges(), testing::SizeIs(1)); |
| 359 | EXPECT_THAT(optimizer->vertices(), testing::UnorderedElementsAreArray( |
| 360 | internal::VectorIntToKeys({0, 1}))); |
| 361 | } |
| 362 | |
| 363 | TEST_F(GeneralGraphOperations, SaveSubsetEdges) { |
| 364 | g2o::OptimizableGraph::EdgeSet edgesToSave; |
| 365 | std::copy_if(optimizer->edges().begin(), optimizer->edges().end(), |
| 366 | std::inserter(edgesToSave, edgesToSave.end()), |
| 367 | [](const g2o::HyperGraph::Edge* e) { |
| 368 | return e->vertices().size() == 2 && e->vertex(0)->id() == 1 && |
| 369 | e->vertex(1)->id() == 2; |
| 370 | }); |
| 371 | |
| 372 | ASSERT_THAT(edgesToSave, testing::SizeIs(1)); |
| 373 | |
| 374 | std::stringstream graphData; |
| 375 | optimizer->saveSubset(graphData, edgesToSave); |
| 376 | optimizer->clear(); |
| 377 | |
| 378 | optimizer->load(graphData); |
| 379 | EXPECT_THAT(optimizer->vertices(), testing::SizeIs(2)); |
| 380 | EXPECT_THAT(optimizer->edges(), testing::SizeIs(1)); |
| 381 | EXPECT_THAT(optimizer->vertices(), testing::UnorderedElementsAreArray( |
| 382 | internal::VectorIntToKeys({1, 2}))); |
| 383 | } |
| 384 | |
| 385 | TEST_F(GeneralGraphOperations, PushPopActiveVertices) { |
| 386 | optimizer->initializeOptimization(); |
| 387 | std::map<int, g2o::Vector3> originalEstimates = vertexEstimates(); |
| 388 | |
| 389 | // push and apply some transformation |
| 390 | optimizer->push(); |
| 391 | for (const auto& idV : optimizer->vertices()) { |
| 392 | g2o::VertexSE2* v = dynamic_cast<g2o::VertexSE2*>(idV.second); |
| 393 | v->setEstimate(v->estimate() * g2o::SE2(idV.first, 0, 0)); |
| 394 | ASSERT_THAT(v->stackSize(), testing::Eq(1)); |
| 395 | } |
| 396 | // the estimates should differ now |
| 397 | std::map<int, g2o::Vector3> changedEstimates = vertexEstimates(); |
| 398 | ASSERT_THAT(originalEstimates, testing::Ne(changedEstimates)); |
| 399 | // recover |
| 400 | optimizer->pop(); |
| 401 | std::map<int, g2o::Vector3> recoveredEstimates = vertexEstimates(); |
| 402 | ASSERT_THAT(originalEstimates, testing::Eq(recoveredEstimates)); |
| 403 | for (const auto& idV : optimizer->vertices()) { |
| 404 | g2o::VertexSE2* v = dynamic_cast<g2o::VertexSE2*>(idV.second); |
| 405 | ASSERT_THAT(v->stackSize(), testing::Eq(0)); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | TEST_F(GeneralGraphOperations, PushDiscardActiveVertices) { |
| 410 | optimizer->initializeOptimization(); |
| 411 | std::map<int, g2o::Vector3> originalEstimates = vertexEstimates(); |
| 412 | |
| 413 | // push and apply some transformation |
| 414 | optimizer->push(); |
| 415 | for (const auto& idV : optimizer->vertices()) { |
| 416 | g2o::VertexSE2* v = dynamic_cast<g2o::VertexSE2*>(idV.second); |
| 417 | v->setEstimate(v->estimate() * g2o::SE2(idV.first, 0, 0)); |
| 418 | ASSERT_THAT(v->stackSize(), testing::Eq(1)); |
| 419 | } |
| 420 | // the estimates should differ now |
| 421 | std::map<int, g2o::Vector3> changedEstimates = vertexEstimates(); |
| 422 | ASSERT_THAT(originalEstimates, testing::Ne(changedEstimates)); |
| 423 | // recover |
| 424 | optimizer->discardTop(); |
| 425 | std::map<int, g2o::Vector3> recoveredEstimates = vertexEstimates(); |
| 426 | ASSERT_THAT(originalEstimates, testing::Ne(recoveredEstimates)); |
| 427 | ASSERT_THAT(changedEstimates, testing::Eq(recoveredEstimates)); |
| 428 | for (const auto& idV : optimizer->vertices()) { |
| 429 | g2o::VertexSE2* v = dynamic_cast<g2o::VertexSE2*>(idV.second); |
| 430 | ASSERT_THAT(v->stackSize(), testing::Eq(0)); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | TEST_F(GeneralGraphOperations, PushPopOptimizableGraph) { |
| 435 | std::map<int, g2o::Vector3> originalEstimates = vertexEstimates(); |
| 436 | |
| 437 | // push and apply some transformation |
| 438 | optimizer->OptimizableGraph::push(); |
| 439 | for (const auto& idV : optimizer->vertices()) { |
| 440 | g2o::VertexSE2* v = dynamic_cast<g2o::VertexSE2*>(idV.second); |
| 441 | v->setEstimate(v->estimate() * g2o::SE2(idV.first * 2, 0, 0)); |
| 442 | ASSERT_THAT(v->stackSize(), testing::Eq(1)); |
| 443 | } |
| 444 | // the estimates should differ now |
| 445 | std::map<int, g2o::Vector3> changedEstimates = vertexEstimates(); |
| 446 | ASSERT_THAT(originalEstimates, testing::Ne(changedEstimates)); |
| 447 | // recover |
| 448 | optimizer->OptimizableGraph::pop(); |
| 449 | std::map<int, g2o::Vector3> recoveredEstimates = vertexEstimates(); |
| 450 | ASSERT_THAT(originalEstimates, testing::Eq(recoveredEstimates)); |
| 451 | for (const auto& idV : optimizer->vertices()) { |
| 452 | g2o::VertexSE2* v = dynamic_cast<g2o::VertexSE2*>(idV.second); |
| 453 | ASSERT_THAT(v->stackSize(), testing::Eq(0)); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | TEST_F(GeneralGraphOperations, PushDiscardOptimizableGraph) { |
| 458 | optimizer->initializeOptimization(); |
| 459 | std::map<int, g2o::Vector3> originalEstimates = vertexEstimates(); |
| 460 | |
| 461 | // push and apply some transformation |
| 462 | optimizer->OptimizableGraph::push(); |
| 463 | for (const auto& idV : optimizer->vertices()) { |
| 464 | g2o::VertexSE2* v = dynamic_cast<g2o::VertexSE2*>(idV.second); |
| 465 | v->setEstimate(v->estimate() * g2o::SE2(idV.first * 3, 0, 0)); |
| 466 | ASSERT_THAT(v->stackSize(), testing::Eq(1)); |
| 467 | } |
| 468 | // the estimates should differ now |
| 469 | std::map<int, g2o::Vector3> changedEstimates = vertexEstimates(); |
| 470 | ASSERT_THAT(originalEstimates, testing::Ne(changedEstimates)); |
| 471 | // recover |
| 472 | optimizer->OptimizableGraph::discardTop(); |
| 473 | std::map<int, g2o::Vector3> recoveredEstimates = vertexEstimates(); |
| 474 | ASSERT_THAT(originalEstimates, testing::Ne(recoveredEstimates)); |
| 475 | ASSERT_THAT(changedEstimates, testing::Eq(recoveredEstimates)); |
| 476 | for (const auto& idV : optimizer->vertices()) { |
| 477 | g2o::VertexSE2* v = dynamic_cast<g2o::VertexSE2*>(idV.second); |
| 478 | ASSERT_THAT(v->stackSize(), testing::Eq(0)); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | TEST_F(GeneralGraphOperations, PushPopDiscardSubset) { |
| 483 | g2o::HyperGraph::VertexSet pushingSet = {optimizer->vertex(0), |
| 484 | optimizer->vertex(2)}; |
| 485 | g2o::HyperGraph::VertexSet notPushedSet; |
| 486 | for (const auto& idV : optimizer->vertices()) { |
| 487 | if (pushingSet.count(idV.second) == 0) notPushedSet.insert(idV.second); |
| 488 | } |
| 489 | |
| 490 | for (int operation = 0; operation < 2; ++operation) { |
| 491 | // push |
| 492 | optimizer->push(pushingSet); |
| 493 | for (const auto& elem : pushingSet) { |
| 494 | g2o::OptimizableGraph::Vertex* v = |
| 495 | dynamic_cast<g2o::OptimizableGraph::Vertex*>(elem); |
| 496 | ASSERT_THAT(v->stackSize(), testing::Eq(1)); |
| 497 | } |
| 498 | for (const auto& elem : notPushedSet) { |
| 499 | g2o::OptimizableGraph::Vertex* v = |
| 500 | dynamic_cast<g2o::OptimizableGraph::Vertex*>(elem); |
| 501 | ASSERT_THAT(v->stackSize(), testing::Eq(0)); |
| 502 | } |
| 503 | // pop or discard |
| 504 | if (operation == 0) |
| 505 | optimizer->pop(pushingSet); |
| 506 | else |
| 507 | optimizer->discardTop(pushingSet); |
| 508 | for (const auto& idV : optimizer->vertices()) { |
| 509 | g2o::VertexSE2* v = dynamic_cast<g2o::VertexSE2*>(idV.second); |
| 510 | ASSERT_THAT(v->stackSize(), testing::Eq(0)); |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | TEST_F(GeneralGraphOperations, FixSubset) { |
| 516 | EXPECT_THAT(fixedIds(), |
| 517 | testing::ElementsAre(0)); // just vertex with ID zero is fixed |
| 518 | |
| 519 | g2o::HyperGraph::VertexSet fixingSet; |
| 520 | fixingSet.insert(optimizer->vertex(1)); |
| 521 | |
| 522 | optimizer->setFixed(fixingSet, true); |
| 523 | EXPECT_THAT(fixedIds(), testing::WhenSorted(testing::ElementsAre(0, 1))); |
| 524 | |
| 525 | optimizer->setFixed(fixingSet, false); |
| 526 | EXPECT_THAT(fixedIds(), testing::ElementsAre(0)); |
| 527 | } |
| 528 | |
| 529 | TEST_F(GeneralGraphOperations, TrivialInit) { |
| 530 | optimizer->initializeOptimization(); |
| 531 | |
| 532 | optimizer->computeActiveErrors(); |
| 533 | ASSERT_THAT(optimizer->activeVertices(), testing::SizeIs(numVertices)); |
| 534 | ASSERT_THAT(optimizer->activeChi2(), testing::Gt(0.)); |
| 535 | ASSERT_TRUE(optimizer->verifyInformationMatrices()); |
| 536 | |
| 537 | ASSERT_THAT( |
| 538 | optimizer->activeVertices(), |
| 539 | testing::Each(testing::ResultOf( |
| 540 | [this](g2o::OptimizableGraph::Vertex* v) { |
| 541 | return optimizer->findActiveVertex(v); |
| 542 | }, |
| 543 | testing::Not(testing::Eq(optimizer->activeVertices().end()))))); |
| 544 | |
| 545 | std::map<int, g2o::Vector3> estimates = vertexEstimates(); |
| 546 | for (size_t i = 1; i < numVertices; ++i) |
| 547 | ASSERT_DOUBLE_EQ(0, estimates[i].norm()); |
| 548 | optimizer->computeInitialGuess(); |
| 549 | estimates = vertexEstimates(); |
| 550 | for (size_t i = 1; i < numVertices; ++i) ASSERT_LT(0, estimates[i].norm()); |
| 551 | } |
| 552 | |
| 553 | TEST_F(GeneralGraphOperations, EdgeInit) { |
| 554 | g2o::OptimizableGraph::Edge* e = |
| 555 | dynamic_cast<g2o::OptimizableGraph::Edge*>(*optimizer->edges().begin()); |
| 556 | g2o::OptimizableGraph::Edge* e2 = dynamic_cast<g2o::OptimizableGraph::Edge*>( |
| 557 | *std::next(optimizer->edges().begin())); |
| 558 | g2o::HyperGraph::EdgeSet eset = {e}; |
| 559 | |
| 560 | optimizer->initializeOptimization(eset); |
| 561 | |
| 562 | EXPECT_THAT(optimizer->activeVertices(), testing::SizeIs(2)); |
| 563 | EXPECT_THAT(optimizer->activeEdges(), testing::SizeIs(1)); |
| 564 | EXPECT_THAT(optimizer->findActiveEdge(e), |
| 565 | testing::Not(testing::Eq(optimizer->activeEdges().end()))); |
| 566 | EXPECT_THAT(optimizer->findActiveEdge(e2), |
| 567 | testing::Eq(optimizer->activeEdges().end())); |
| 568 | } |
| 569 | |
| 570 | TEST_F(GeneralGraphOperations, Gauge) { |
| 571 | optimizer->initializeOptimization(); |
| 572 | |
| 573 | EXPECT_THAT(optimizer->gaugeFreedom(), testing::IsFalse()); |
| 574 | EXPECT_THAT(optimizer->findGauge(), testing::NotNull()); |
| 575 | |
| 576 | optimizer->vertex(0)->setFixed(false); |
| 577 | EXPECT_THAT(optimizer->gaugeFreedom(), testing::IsTrue()); |
| 578 | EXPECT_THAT(optimizer->findGauge(), testing::NotNull()); |
| 579 | } |
| 580 | |
| 581 | TEST_F(GeneralGraphOperations, Dimensions) { |
| 582 | optimizer->initializeOptimization(); |
| 583 | |
| 584 | EXPECT_EQ(optimizer->maxDimension(), 3); |
| 585 | EXPECT_THAT(optimizer->dimensions(), testing::ElementsAre(3)); |
| 586 | |
| 587 | g2o::VertexPointXY* point = new g2o::VertexPointXY; |
| 588 | point->setId(numVertices + 1); |
| 589 | optimizer->addVertex(point); |
| 590 | |
| 591 | EXPECT_EQ(optimizer->maxDimension(), 3); |
| 592 | EXPECT_THAT(optimizer->dimensions(), testing::ElementsAre(2, 3)); |
| 593 | } |
| 594 | |
| 595 | TEST_F(GeneralGraphOperations, VerifyInformationMatrices) { |
| 596 | optimizer->initializeOptimization(); |
| 597 | |
| 598 | ASSERT_TRUE(optimizer->verifyInformationMatrices()); |
| 599 | |
| 600 | g2o::EdgeSE2* e1 = new g2o::EdgeSE2(); |
| 601 | e1->vertices()[0] = optimizer->vertex(0); |
| 602 | e1->vertices()[1] = optimizer->vertex(1); |
| 603 | e1->setMeasurement(g2o::SE2(2., 0., 0.)); |
| 604 | e1->setInformation(-1 * g2o::EdgeSE2::InformationType::Identity()); |
| 605 | optimizer->addEdge(e1); |
| 606 | |
| 607 | g2o::EdgeSE2* e2 = new g2o::EdgeSE2(); |
| 608 | e2->vertices()[0] = optimizer->vertex(1); |
| 609 | e2->vertices()[1] = optimizer->vertex(2); |
| 610 | e2->setMeasurement(g2o::SE2(1., 2., 0.)); |
| 611 | g2o::EdgeSE2::InformationType infoMat = |
| 612 | g2o::EdgeSE2::InformationType::Identity(); |
| 613 | infoMat(1, 2) = 1; |
| 614 | e2->setInformation(infoMat); |
| 615 | optimizer->addEdge(e2); |
| 616 | |
| 617 | ASSERT_FALSE(optimizer->verifyInformationMatrices(true)); |
| 618 | } |
| 619 | |
| 620 | TEST_F(GeneralGraphOperations, SolverSuitable) { |
| 621 | g2o::OptimizationAlgorithmProperty solverPropertyVar; |
| 622 | solverPropertyVar.requiresMarginalize = false; |
| 623 | solverPropertyVar.poseDim = -1; |
| 624 | solverPropertyVar.landmarkDim = -1; |
| 625 | |
| 626 | g2o::OptimizationAlgorithmProperty solverPropertyFix32; |
| 627 | solverPropertyFix32.requiresMarginalize = true; |
| 628 | solverPropertyFix32.poseDim = 3; |
| 629 | solverPropertyFix32.landmarkDim = 2; |
| 630 | |
| 631 | g2o::OptimizationAlgorithmProperty solverPropertyFix63; |
| 632 | solverPropertyFix63.requiresMarginalize = true; |
| 633 | solverPropertyFix63.poseDim = 6; |
| 634 | solverPropertyFix63.landmarkDim = 3; |
| 635 | |
| 636 | std::set<int> vertexDims = {2, 3}; |
| 637 | std::set<int> vertexDimsNoMatch = {1, 5}; |
| 638 | |
| 639 | EXPECT_TRUE(optimizer->isSolverSuitable(solverPropertyVar)); |
| 640 | EXPECT_TRUE(optimizer->isSolverSuitable(solverPropertyVar, vertexDims)); |
| 641 | EXPECT_TRUE( |
| 642 | optimizer->isSolverSuitable(solverPropertyVar, vertexDimsNoMatch)); |
| 643 | |
| 644 | EXPECT_TRUE(optimizer->isSolverSuitable(solverPropertyFix32)); |
| 645 | EXPECT_TRUE(optimizer->isSolverSuitable(solverPropertyFix32, vertexDims)); |
| 646 | EXPECT_FALSE( |
| 647 | optimizer->isSolverSuitable(solverPropertyFix32, vertexDimsNoMatch)); |
| 648 | |
| 649 | EXPECT_FALSE(optimizer->isSolverSuitable(solverPropertyFix63)); |
| 650 | EXPECT_FALSE(optimizer->isSolverSuitable(solverPropertyFix63, vertexDims)); |
| 651 | EXPECT_FALSE( |
| 652 | optimizer->isSolverSuitable(solverPropertyFix63, vertexDimsNoMatch)); |
| 653 | } |
| 654 | |
| 655 | TEST_F(GeneralGraphOperations, JacWorkspace) { |
| 656 | JacobianWorkspaceTestAdapter workspace; |
| 657 | ASSERT_THAT(workspace.maxDimension(), testing::Le(0)); |
| 658 | ASSERT_THAT(workspace.maxNumVertices(), testing::Le(0)); |
| 659 | |
| 660 | auto* root = optimizer->vertex(0); |
| 661 | workspace.updateSize(*root->edges().begin(), true); |
| 662 | EXPECT_THAT(workspace.maxDimension(), testing::Eq(9)); |
| 663 | EXPECT_THAT(workspace.maxNumVertices(), testing::Eq(2)); |
| 664 | EXPECT_THAT(workspace.workspace(), testing::IsEmpty()); |
| 665 | |
| 666 | workspace.updateSize(5, 23, true); |
| 667 | EXPECT_THAT(workspace.maxDimension(), testing::Eq(23)); |
| 668 | EXPECT_THAT(workspace.maxNumVertices(), testing::Eq(5)); |
| 669 | EXPECT_THAT(workspace.workspace(), testing::IsEmpty()); |
| 670 | |
| 671 | workspace.updateSize(*optimizer, true); |
| 672 | EXPECT_THAT(workspace.maxDimension(), testing::Eq(9)); |
| 673 | EXPECT_THAT(workspace.maxNumVertices(), testing::Eq(2)); |
| 674 | EXPECT_THAT(workspace.workspace(), testing::IsEmpty()); |
| 675 | |
| 676 | bool allocated = workspace.allocate(); |
| 677 | EXPECT_THAT(allocated, testing::IsTrue()); |
| 678 | ASSERT_THAT(workspace.workspace(), testing::SizeIs(2)); |
| 679 | EXPECT_THAT(workspace.workspace(), |
| 680 | testing::Each(testing::SizeIs(workspace.maxDimension()))); |
| 681 | |
| 682 | for (int i = 0; i < 2; ++i) { |
| 683 | EXPECT_THAT(workspace.workspaceForVertex(i), testing::NotNull()); |
| 684 | } |
| 685 | |
| 686 | for (auto& wp : workspace.workspace()) wp.array() = 1; |
| 687 | workspace.setZero(); |
| 688 | EXPECT_THAT( |
| 689 | workspace.workspace(), |
| 690 | testing::Each(testing::ResultOf( |
| 691 | [](const g2o::VectorX vec) { return vec.isApproxToConstant(0); }, |
| 692 | testing::IsTrue()))); |
| 693 | } |
| 694 | |
| 695 | namespace { |
| 696 | |
| 697 | class TreeVisitor : public g2o::HyperDijkstra::TreeAction { |
| 698 | public: |
| 699 | double perform(g2o::HyperGraph::Vertex* v, |
| 700 | g2o::HyperGraph::Vertex* /*vParent*/, |
| 701 | g2o::HyperGraph::Edge* /*e*/) override { |
| 702 | visited_ids_.insert(v->id()); |
| 703 | return 1.; |
| 704 | } |
| 705 | |
| 706 | const std::unordered_set<int>& visitedIds() const { return visited_ids_; } |
| 707 | |
| 708 | protected: |
| 709 | std::unordered_set<int> visited_ids_; |
| 710 | }; |
| 711 | |
| 712 | std::vector<int> range_helper(int range) { |
| 713 | std::vector<int> result(range); |
| 714 | std::iota(result.begin(), result.end(), 0); |
| 715 | return result; |
| 716 | } |
| 717 | } // namespace |
| 718 | |
| 719 | TEST_F(GeneralGraphOperations, HyperDijkstraVisitor) { |
| 720 | g2o::UniformCostFunction uniformCost; |
| 721 | g2o::HyperDijkstra hyperDijkstra(optimizer.get()); |
| 722 | hyperDijkstra.shortestPaths(optimizer->vertex(0), &uniformCost); |
| 723 | |
| 724 | g2o::HyperDijkstra::computeTree(hyperDijkstra.adjacencyMap()); |
| 725 | TreeVisitor treeVisitor; |
| 726 | g2o::HyperDijkstra::visitAdjacencyMap(hyperDijkstra.adjacencyMap(), |
| 727 | &treeVisitor); |
| 728 | EXPECT_THAT(treeVisitor.visitedIds(), testing::SizeIs(numVertices)); |
| 729 | EXPECT_THAT(treeVisitor.visitedIds(), |
| 730 | testing::UnorderedElementsAreArray(range_helper(numVertices))); |
| 731 | } |