Skip to content

Commit d896761

Browse files
SfM: Update visibility on feature transitions [R14-F7] (CPU perf)
## Summary Update the visibility pyramid when a feature gains its first triangulated correspondence or loses its last. Additional correspondences change support but leave the visible feature set unchanged, so repeating every pyramid-level update does unnecessary work. ## Changes - Put SetPoint and ResetPoint beside the existing zero/one visibility-count transitions, preserving occupied-cell scores for colliding features. - Extend the existing visibility-score and AddImage tests to cover repeated support, collisions, partial/final removal, underflow and rebuilding the manager. ## Background The repeated updates are inherited from fe8f82a, "Initial commit" (2016), in src/base/image.cc. The ObservationManager extraction, eaebe3c, "Add ObservationManager (colmap#2575)", retained them. ## Results Fixed public Add64/Delete32/Delete32 bookkeeping on a 16-image, 64-track, 120-pair fixture: | Platform | Before | After | | --- | ---: | ---: | | Linux/WSL | 1.395 ms | 0.640 ms | | Windows | 2.776 ms | 1.438 ms | Each value averages four fresh process means per arm, ABBAABBA, with two warmup and ten measured batches per process. Canonical inputs and outputs match within each platform. Whole-command Linux observations, including setup/oracles/serialization: median CPU 0.495 -> 0.443 s, median per-process peak RSS 414.50 -> 414.35 MiB, mean instructions 3.879 -> 3.782 billion. Windows median CPU 2.234 -> 2.211 s and median per-process peak working set 14.54 -> 14.33 MiB have overlapping process ranges. These broader counters describe the probe, not isolated pyramid operations. ## Validation - Current composition: all 18 maintained manager cases and two mapper lifecycle cases passed, without skips. Earlier matched arms passed the same selected counts on Linux and Windows. - clang-tidy-18 completed on the owner and maintained test with no displayed source diagnostics; generated/header warnings were suppressed. Changed-line formatting passed with the real source filename. - Incremental build: three translation units and two test links, reusing 46 production objects and 13 archives. Source, selected-link and provider postflight checks completed using the retained outputs. AI disclosure: Totally vibe coded
1 parent d186f12 commit d896761

2 files changed

Lines changed: 50 additions & 6 deletions

File tree

‎src/colmap/sfm/observation_manager.cc‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,9 @@ void ObservationManager::IncrementCorrespondenceHasPoint3D(
168168
stats.num_correspondences_have_point3D[point2D_idx] += 1;
169169
if (stats.num_correspondences_have_point3D[point2D_idx] == 1) {
170170
stats.num_visible_points3D += 1;
171+
stats.point3D_visibility_pyramid.SetPoint(point2D.xy(0), point2D.xy(1));
171172
}
172173

173-
stats.point3D_visibility_pyramid.SetPoint(point2D.xy(0), point2D.xy(1));
174-
175174
THROW_CHECK_LE(stats.num_visible_points3D, stats.num_observations);
176175
}
177176

@@ -187,10 +186,9 @@ void ObservationManager::DecrementCorrespondenceHasPoint3D(
187186
stats.num_correspondences_have_point3D[point2D_idx] -= 1;
188187
if (stats.num_correspondences_have_point3D[point2D_idx] == 0) {
189188
stats.num_visible_points3D -= 1;
189+
stats.point3D_visibility_pyramid.ResetPoint(point2D.xy(0), point2D.xy(1));
190190
}
191191

192-
stats.point3D_visibility_pyramid.ResetPoint(point2D.xy(0), point2D.xy(1));
193-
194192
THROW_CHECK_LE(stats.num_visible_points3D, stats.num_observations);
195193
}
196194

‎src/colmap/sfm/observation_manager_test.cc‎

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
#include "colmap/math/random_eigen.h"
66
#include "colmap/util/hash_containers.h"
77

8-
#include <memory>
9-
108
#include <limits>
9+
#include <memory>
10+
#include <stdexcept>
1111

1212
#include <gtest/gtest.h>
1313

@@ -674,6 +674,8 @@ TEST(ObservationManager, Point3DVisibilityScore) {
674674
points2D.emplace_back(i, j);
675675
}
676676
}
677+
// Distinct features can occupy the same cell at every pyramid level.
678+
points2D.back() = points2D[1];
677679
image.SetPoints2D(points2D);
678680
reconstruction.AddImage(image);
679681
image.SetImageId(kImageId2);
@@ -726,6 +728,38 @@ TEST(ObservationManager, Point3DVisibilityScore) {
726728
obs_manager.IncrementCorrespondenceHasPoint3D(kImageId1, 2);
727729
EXPECT_EQ(obs_manager.Point3DVisibilityScore(kImageId1),
728730
2 * scores.sum() + 2 * scores.bottomRows(scores.size() - 1).sum());
731+
732+
const size_t score = obs_manager.Point3DVisibilityScore(kImageId1);
733+
obs_manager.IncrementCorrespondenceHasPoint3D(kImageId1, 15);
734+
EXPECT_EQ(obs_manager.Point3DVisibilityScore(kImageId1), score);
735+
EXPECT_EQ(obs_manager.NumVisiblePoints3D(kImageId1), 5);
736+
737+
// Partial removals preserve visibility, including shared coarse cells.
738+
for (const point2D_t point2D_idx : {1, 4, 1}) {
739+
obs_manager.DecrementCorrespondenceHasPoint3D(kImageId1, point2D_idx);
740+
EXPECT_EQ(obs_manager.Point3DVisibilityScore(kImageId1), score);
741+
EXPECT_EQ(obs_manager.NumVisiblePoints3D(kImageId1), 5);
742+
}
743+
// Removing one feature leaves its cell occupied by the colliding feature.
744+
obs_manager.DecrementCorrespondenceHasPoint3D(kImageId1, 1);
745+
EXPECT_EQ(obs_manager.Point3DVisibilityScore(kImageId1), score);
746+
EXPECT_EQ(obs_manager.NumVisiblePoints3D(kImageId1), 4);
747+
obs_manager.DecrementCorrespondenceHasPoint3D(kImageId1, 15);
748+
EXPECT_EQ(obs_manager.Point3DVisibilityScore(kImageId1),
749+
2 * scores.sum() + scores.bottomRows(scores.size() - 1).sum());
750+
EXPECT_EQ(obs_manager.NumVisiblePoints3D(kImageId1), 3);
751+
752+
for (const point2D_t point2D_idx : {4, 5}) {
753+
obs_manager.DecrementCorrespondenceHasPoint3D(kImageId1, point2D_idx);
754+
}
755+
EXPECT_EQ(obs_manager.Point3DVisibilityScore(kImageId1), scores.sum());
756+
obs_manager.DecrementCorrespondenceHasPoint3D(kImageId1, 2);
757+
EXPECT_EQ(obs_manager.Point3DVisibilityScore(kImageId1), 0);
758+
EXPECT_EQ(obs_manager.NumVisiblePoints3D(kImageId1), 0);
759+
EXPECT_THROW(obs_manager.DecrementCorrespondenceHasPoint3D(kImageId1, 2),
760+
std::invalid_argument);
761+
EXPECT_EQ(obs_manager.Point3DVisibilityScore(kImageId1), 0);
762+
EXPECT_EQ(obs_manager.NumVisiblePoints3D(kImageId1), 0);
729763
}
730764

731765
TEST(ObservationManager, AddImage) {
@@ -786,6 +820,8 @@ TEST(ObservationManager, AddImage) {
786820
EXPECT_EQ(obs_manager.NumVisiblePoints3D(3), 1);
787821
EXPECT_EQ(obs_manager.NumObservations(3), 2);
788822
EXPECT_EQ(obs_manager.NumCorrespondences(3), 3);
823+
const size_t visibility_score = obs_manager.Point3DVisibilityScore(3);
824+
EXPECT_GT(visibility_score, 0);
789825

790826
// Verify AddImage refreshed image 1's cached stats (was 2 obs, now 3).
791827
EXPECT_EQ(obs_manager.NumObservations(1), 3);
@@ -808,6 +844,16 @@ TEST(ObservationManager, AddImage) {
808844

809845
EXPECT_NO_THROW(obs_manager.DeRegisterFrame(1));
810846
EXPECT_EQ(obs_manager.NumVisibleCorrespondences(3), 1);
847+
EXPECT_EQ(obs_manager.NumVisiblePoints3D(3), 1);
848+
EXPECT_EQ(obs_manager.Point3DVisibilityScore(3), visibility_score);
849+
{
850+
ObservationManager rebuilt(reconstruction, graph);
851+
EXPECT_EQ(rebuilt.NumVisiblePoints3D(3), 1);
852+
EXPECT_EQ(rebuilt.Point3DVisibilityScore(3), visibility_score);
853+
}
854+
obs_manager.DeletePoint3D(point3D_id);
855+
EXPECT_EQ(obs_manager.NumVisiblePoints3D(3), 0);
856+
EXPECT_EQ(obs_manager.Point3DVisibilityScore(3), 0);
811857
}
812858

813859
TEST(ObservationManager, AddImageVisibilityAcrossNeighborRegistration) {

0 commit comments

Comments
 (0)