Skip to content

Commit f664d5d

Browse files
aswanth-07sarlinpe
andauthored
Clamp visibility coordinates before unsigned conversion (colmap#4943)
`VisibilityPyramid::CellForPoint` converts normalized coordinates to `size_t` before clamping. A point left of or above the image can therefore occupy the opposite edge, increasing the visibility score instead of sharing the nearest boundary cell. Cropped undistortion can retain observations outside the output image bounds. I clamp both coordinates as doubles before conversion. The same helper handles insertion and removal, and existing nonnegative coordinate bins and score weights stay unchanged. No API, saved model or allocation changes. The native regression fails on pristine main: a left-boundary point and its negative-coordinate counterpart produce score 10920 instead of 5460. All three native visibility tests and all 28 independent cell-oracle cases pass after the fix. All 4,800 nonnegative update scores match pristine exactly. MSVC Release, clang-format and whitespace checks pass. Local Clang-Tidy reports the same nine pre-existing source/header diagnostics on base and patch. --------- Co-authored-by: Paul-Edouard Sarlin <[email protected]>
1 parent e8f3522 commit f664d5d

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

‎src/colmap/scene/visibility_pyramid.cc‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#include "colmap/scene/visibility_pyramid.h"
44

5-
#include "colmap/math/math.h"
65
#include "colmap/util/logging.h"
76

87
#include <algorithm>
@@ -75,8 +74,8 @@ void VisibilityPyramid::CellForPoint(const double x,
7574
THROW_CHECK_GT(width_, 0);
7675
THROW_CHECK_GT(height_, 0);
7776
const int max_dim = 1 << pyramid_.size();
78-
*cx = std::clamp<size_t>(max_dim * x / width_, 0, max_dim - 1);
79-
*cy = std::clamp<size_t>(max_dim * y / height_, 0, max_dim - 1);
77+
*cx = std::clamp<double>(max_dim * x / width_, 0, max_dim - 1);
78+
*cy = std::clamp<double>(max_dim * y / height_, 0, max_dim - 1);
8079
}
8180

8281
} // namespace colmap

‎src/colmap/scene/visibility_pyramid_test.cc‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include "colmap/scene/visibility_pyramid.h"
44

5+
#include <algorithm>
6+
57
#include <gtest/gtest.h>
68

79
namespace colmap {
@@ -60,5 +62,23 @@ TEST(VisibilityPyramid, Score) {
6062
}
6163
}
6264

65+
TEST(VisibilityPyramid, ClampOutsideImage) {
66+
const std::vector<Eigen::Vector2d> points = {
67+
{-42, 200}, {320, -45}, {-42, -45}, {682, 200}, {320, 525}};
68+
for (const auto& point : points) {
69+
const double x = std::clamp(point.x(), 0.0, 639.0);
70+
const double y = std::clamp(point.y(), 0.0, 479.0);
71+
VisibilityPyramid pyramid(6, 640, 480);
72+
pyramid.SetPoint(x, y);
73+
const size_t score = pyramid.Score();
74+
pyramid.SetPoint(point.x(), point.y());
75+
EXPECT_EQ(pyramid.Score(), score) << point.transpose();
76+
pyramid.ResetPoint(point.x(), point.y());
77+
EXPECT_EQ(pyramid.Score(), score);
78+
pyramid.ResetPoint(x, y);
79+
EXPECT_EQ(pyramid.Score(), 0);
80+
}
81+
}
82+
6383
} // namespace
6484
} // namespace colmap

0 commit comments

Comments
 (0)