|
4 | 4 |
|
5 | 5 | #include "colmap/math/random_eigen.h" |
6 | 6 | #include "colmap/scene/database_cache.h" |
| 7 | +#include "colmap/scene/database_sqlite.h" |
7 | 8 | #include "colmap/scene/synthetic.h" |
8 | 9 | #include "colmap/util/hash_containers.h" |
9 | 10 | #include "colmap/util/testing.h" |
@@ -433,6 +434,94 @@ TEST(PoseGraph, InvalidatePairsOutsideActiveImageIds) { |
433 | 434 | EXPECT_FALSE(pose_graph.IsValid(ImagePairToPairId(2, 3))); |
434 | 435 | } |
435 | 436 |
|
| 437 | +TEST(PoseGraph, Subgraph) { |
| 438 | + PoseGraph pose_graph; |
| 439 | + PoseGraph::Edge edge = SynthesizeEdge(/*num_matches=*/42); |
| 440 | + edge.cam2_from_cam1 = |
| 441 | + Rigid3d(RandomEigenQuaterniond(), RandomEigenVectord<3>()); |
| 442 | + edge.cam2_from_cam1_rotation_cov = 2.0f * Eigen::Matrix3f::Identity(); |
| 443 | + edge.prior = true; |
| 444 | + pose_graph.AddEdge(1, 2, edge); |
| 445 | + pose_graph.AddEdge(1, 3, SynthesizeEdge()); |
| 446 | + pose_graph.AddEdge(2, 3, SynthesizeEdge()); |
| 447 | + pose_graph.AddEdge(2, 4, SynthesizeEdge()); |
| 448 | + pose_graph.SetInvalidEdge(ImagePairToPairId(2, 4)); |
| 449 | + |
| 450 | + const PoseGraph subgraph = pose_graph.Subgraph({1, 2, 4}); |
| 451 | + EXPECT_EQ(subgraph.NumEdges(), 2); |
| 452 | + EXPECT_FALSE(subgraph.HasEdge(1, 3)); |
| 453 | + EXPECT_FALSE(subgraph.HasEdge(2, 3)); |
| 454 | + |
| 455 | + // Edges are copied with all their data and validity. |
| 456 | + const PoseGraph::Edge& subgraph_edge = |
| 457 | + subgraph.Edges().at(ImagePairToPairId(1, 2)); |
| 458 | + EXPECT_EQ( |
| 459 | + subgraph_edge.cam2_from_cam1.params, |
| 460 | + pose_graph.Edges().at(ImagePairToPairId(1, 2)).cam2_from_cam1.params); |
| 461 | + EXPECT_EQ(subgraph_edge.num_matches, 42); |
| 462 | + EXPECT_TRUE(subgraph_edge.prior); |
| 463 | + EXPECT_TRUE(subgraph_edge.valid); |
| 464 | + ASSERT_TRUE(subgraph_edge.cam2_from_cam1_rotation_cov.has_value()); |
| 465 | + EXPECT_EQ(*subgraph_edge.cam2_from_cam1_rotation_cov, |
| 466 | + *pose_graph.Edges() |
| 467 | + .at(ImagePairToPairId(1, 2)) |
| 468 | + .cam2_from_cam1_rotation_cov); |
| 469 | + EXPECT_TRUE(subgraph.HasEdge(2, 4)); |
| 470 | + EXPECT_FALSE(subgraph.IsValid(ImagePairToPairId(2, 4))); |
| 471 | + |
| 472 | + EXPECT_TRUE(pose_graph.Subgraph({}).Empty()); |
| 473 | +} |
| 474 | + |
| 475 | +// The subgraph of a component equals the pose graph loaded from the database |
| 476 | +// cache of that component, which GlobalPipeline relies on. |
| 477 | +TEST(PoseGraph, SubgraphMatchesComponentDatabaseCache) { |
| 478 | + auto database = Database::Open(kInMemorySqliteDatabasePath); |
| 479 | + SyntheticDatasetOptions synthetic_options; |
| 480 | + synthetic_options.num_rigs = 2; |
| 481 | + synthetic_options.num_cameras_per_rig = 2; |
| 482 | + synthetic_options.num_frames_per_rig = 3; |
| 483 | + synthetic_options.num_points3D = 50; |
| 484 | + synthetic_options.two_view_geometry_has_relative_pose = true; |
| 485 | + Reconstruction reconstruction; |
| 486 | + SynthesizeDataset(synthetic_options, &reconstruction, database.get()); |
| 487 | + |
| 488 | + DatabaseCache::Options cache_options; |
| 489 | + const auto cache = DatabaseCache::Create(*database, cache_options); |
| 490 | + PoseGraph pose_graph; |
| 491 | + pose_graph.Load(*cache->CorrespondenceGraph()); |
| 492 | + |
| 493 | + // Select the images of the reference cameras of some frames. The component |
| 494 | + // cache also includes the other images of their frames. |
| 495 | + DatabaseCache::Options component_options; |
| 496 | + for (const auto& [image_id, image] : reconstruction.Images()) { |
| 497 | + if (image.IsRefInFrame() && image.FrameId() % 2 == 0) { |
| 498 | + component_options.image_names.insert(image.Name()); |
| 499 | + } |
| 500 | + } |
| 501 | + ASSERT_FALSE(component_options.image_names.empty()); |
| 502 | + const auto component_cache = |
| 503 | + DatabaseCache::CreateFromCache(*cache, component_options); |
| 504 | + PoseGraph component_pose_graph; |
| 505 | + component_pose_graph.Load(*component_cache->CorrespondenceGraph()); |
| 506 | + |
| 507 | + FlatHashSet<image_t> component_image_ids; |
| 508 | + for (const auto& [image_id, _] : component_cache->Images()) { |
| 509 | + component_image_ids.insert(image_id); |
| 510 | + } |
| 511 | + ASSERT_GT(component_image_ids.size(), component_options.image_names.size()); |
| 512 | + const PoseGraph subgraph = pose_graph.Subgraph(component_image_ids); |
| 513 | + |
| 514 | + ASSERT_GT(subgraph.NumEdges(), 0); |
| 515 | + ASSERT_EQ(subgraph.NumEdges(), component_pose_graph.NumEdges()); |
| 516 | + for (const auto& [pair_id, edge] : component_pose_graph.Edges()) { |
| 517 | + ASSERT_TRUE(subgraph.Edges().count(pair_id)); |
| 518 | + const PoseGraph::Edge& subgraph_edge = subgraph.Edges().at(pair_id); |
| 519 | + EXPECT_EQ(subgraph_edge.cam2_from_cam1.params, edge.cam2_from_cam1.params); |
| 520 | + EXPECT_EQ(subgraph_edge.num_matches, edge.num_matches); |
| 521 | + EXPECT_EQ(subgraph_edge.valid, edge.valid); |
| 522 | + } |
| 523 | +} |
| 524 | + |
436 | 525 | TEST(PoseGraph, InvertEdgeCovariance) { |
437 | 526 | PoseGraph::Edge edge; |
438 | 527 | const Eigen::Quaterniond q21 = |
|
0 commit comments