Skip to content

Commit 7965b60

Browse files
Fabian Schenkmeta-codesync[bot]
authored andcommitted
hoist/const in depthmap hot loops
Differential Revision: D108284659 fbshipit-source-id: 996da16d52b6d4487ca75fc9b70c2d133654a9b6
1 parent 1e04718 commit 7965b60

1 file changed

Lines changed: 23 additions & 20 deletions

File tree

opensfm/src/dense/src/depthmap.cc

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@ void DepthmapEstimator::SetMinPatchSD(float sd) {
184184
void DepthmapEstimator::ComputeBruteForce(DepthmapEstimatorResult* result) {
185185
AssignMatrices(result);
186186

187-
int hpz = (patch_size_ - 1) / 2;
187+
const int hpz = (patch_size_ - 1) / 2;
188+
const cv::Vec3f normal(0, 0, -1);
188189
for (int i = hpz; i < result->depth.rows - hpz; ++i) {
189190
for (int j = hpz; j < result->depth.cols - hpz; ++j) {
190191
for (int d = 0; d < num_depth_planes_; ++d) {
@@ -195,7 +196,6 @@ void DepthmapEstimator::ComputeBruteForce(DepthmapEstimatorResult* result) {
195196
depth = 1 / (1 / min_depth_ + d * (1 / max_depth_ - 1 / min_depth_) /
196197
(num_depth_planes_ - 1));
197198
}
198-
cv::Vec3f normal(0, 0, -1);
199199
cv::Vec3f plane = PlaneFromDepthAndNormal(j, i, Ks_[0], depth, normal);
200200
CheckPlaneCandidate(result, i, j, plane);
201201
}
@@ -274,11 +274,12 @@ void DepthmapEstimator::ComputeIgnoreMask(DepthmapEstimatorResult* result) {
274274

275275
float DepthmapEstimator::PatchVariance(int i, int j) {
276276
float* patch = patch_variance_buffer_.data();
277-
int hpz = (patch_size_ - 1) / 2;
277+
const int hpz = (patch_size_ - 1) / 2;
278+
const cv::Mat& image0 = images_[0];
278279
int counter = 0;
279280
for (int u = -hpz; u <= hpz; ++u) {
280281
for (int v = -hpz; v <= hpz; ++v) {
281-
patch[counter++] = images_[0].at<unsigned char>(i + u, j + v);
282+
patch[counter++] = image0.at<unsigned char>(i + u, j + v);
282283
}
283284
}
284285
return Variance(patch, patch_size_ * patch_size_);
@@ -425,35 +426,37 @@ void DepthmapEstimator::ComputePlaneScore(int i, int j, const cv::Vec3f& plane,
425426
float DepthmapEstimator::ComputePlaneImageScore(int i, int j,
426427
const cv::Vec3f& plane,
427428
int other) const {
428-
cv::Matx33f H = PlaneInducedHomographyBaked(Kinvs_[0], Qs_[other], as_[other],
429-
Ks_[other], plane);
430-
int hpz = (patch_size_ - 1) / 2;
429+
const cv::Matx33f H = PlaneInducedHomographyBaked(
430+
Kinvs_[0], Qs_[other], as_[other], Ks_[other], plane);
431+
const int hpz = (patch_size_ - 1) / 2;
431432

432-
float u = H(0, 0) * j + H(0, 1) * i + H(0, 2);
433-
float v = H(1, 0) * j + H(1, 1) * i + H(1, 2);
434-
float w = H(2, 0) * j + H(2, 1) * i + H(2, 2);
433+
const float u = H(0, 0) * j + H(0, 1) * i + H(0, 2);
434+
const float v = H(1, 0) * j + H(1, 1) * i + H(1, 2);
435+
const float w = H(2, 0) * j + H(2, 1) * i + H(2, 2);
435436

436437
if (w == 0.0) {
437438
return -1.0f;
438439
}
439440

440-
float dfdx_x = (H(0, 0) * w - H(2, 0) * u) / (w * w);
441-
float dfdx_y = (H(1, 0) * w - H(2, 0) * v) / (w * w);
442-
float dfdy_x = (H(0, 1) * w - H(2, 1) * u) / (w * w);
443-
float dfdy_y = (H(1, 1) * w - H(2, 1) * v) / (w * w);
441+
const float dfdx_x = (H(0, 0) * w - H(2, 0) * u) / (w * w);
442+
const float dfdx_y = (H(1, 0) * w - H(2, 0) * v) / (w * w);
443+
const float dfdy_x = (H(0, 1) * w - H(2, 1) * u) / (w * w);
444+
const float dfdy_y = (H(1, 1) * w - H(2, 1) * v) / (w * w);
444445

445-
float Hx0 = u / w;
446-
float Hy0 = v / w;
446+
const float Hx0 = u / w;
447+
const float Hy0 = v / w;
447448

448-
float im1_center = images_[0].at<unsigned char>(i, j);
449+
const cv::Mat& image0 = images_[0];
450+
const cv::Mat& image_other = images_[other];
451+
const float im1_center = image0.at<unsigned char>(i, j);
449452

450453
NCCEstimator ncc;
451454
for (int dy = -hpz; dy <= hpz; ++dy) {
452455
for (int dx = -hpz; dx <= hpz; ++dx) {
453-
float im1 = images_[0].at<unsigned char>(i + dy, j + dx);
456+
float im1 = image0.at<unsigned char>(i + dy, j + dx);
454457
float x2 = Hx0 + dfdx_x * dx + dfdy_x * dy;
455458
float y2 = Hy0 + dfdx_y * dx + dfdy_y * dy;
456-
float im2 = LinearInterpolation<unsigned char>(images_[other], y2, x2);
459+
float im2 = LinearInterpolation<unsigned char>(image_other, y2, x2);
457460
float weight = BilateralWeight(im1 - im1_center, dx, dy);
458461
ncc.Push(im1, im2, weight);
459462
}
@@ -563,7 +566,7 @@ void DepthmapPruner::Prune(std::vector<float>* merged_points,
563566
std::vector<float>* merged_normals,
564567
std::vector<unsigned char>* merged_colors,
565568
std::vector<unsigned char>* merged_labels) {
566-
cv::Matx33f Rinv = Rs_[0].t();
569+
const cv::Matx33f Rinv = Rs_[0].t();
567570
for (int i = 0; i < depths_[0].rows; ++i) {
568571
for (int j = 0; j < depths_[0].cols; ++j) {
569572
float depth = depths_[0].at<float>(i, j);

0 commit comments

Comments
 (0)