ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
visibility_pyramid.cc
Go to the documentation of this file.
1 // Copyright (c) 2018, ETH Zurich and UNC Chapel Hill.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 //
14 // * Neither the name of ETH Zurich and UNC Chapel Hill nor the names of
15 // its contributors may be used to endorse or promote products derived
16 // from this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 // POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Johannes L. Schoenberger (jsch-at-demuc-dot-de)
31 
33 #include "util/logging.h"
34 #include "util/math.h"
35 
36 namespace colmap {
37 
39 
40 VisibilityPyramid::VisibilityPyramid(const size_t num_levels,
41  const size_t width, const size_t height)
42  : width_(width), height_(height), score_(0), max_score_(0) {
43  pyramid_.resize(num_levels);
44  for (size_t level = 0; level < num_levels; ++level) {
45  const size_t level_plus_one = level + 1;
46  const int dim = 1 << level_plus_one;
47  pyramid_[level].setZero(dim, dim);
48  max_score_ += dim * dim * dim * dim;
49  }
50 }
51 
52 void VisibilityPyramid::SetPoint(const double x, const double y) {
53  CHECK_GT(pyramid_.size(), 0);
54 
55  size_t cx = 0;
56  size_t cy = 0;
57  CellForPoint(x, y, &cx, &cy);
58 
59  for (int i = static_cast<int>(pyramid_.size() - 1); i >= 0; --i) {
60  auto& level = pyramid_[i];
61 
62  level(cy, cx) += 1;
63  if (level(cy, cx) == 1) {
64  score_ += level.size();
65  }
66 
67  cx = cx >> 1;
68  cy = cy >> 1;
69  }
70 
71  CHECK_LE(score_, max_score_);
72 }
73 
74 void VisibilityPyramid::ResetPoint(const double x, const double y) {
75  CHECK_GT(pyramid_.size(), 0);
76 
77  size_t cx = 0;
78  size_t cy = 0;
79  CellForPoint(x, y, &cx, &cy);
80 
81  for (int i = static_cast<int>(pyramid_.size() - 1); i >= 0; --i) {
82  auto& level = pyramid_[i];
83 
84  level(cy, cx) -= 1;
85  if (level(cy, cx) == 0) {
86  score_ -= level.size();
87  }
88 
89  cx = cx >> 1;
90  cy = cy >> 1;
91  }
92 
93  CHECK_LE(score_, max_score_);
94 }
95 
96 void VisibilityPyramid::CellForPoint(const double x, const double y, size_t* cx,
97  size_t* cy) const {
98  CHECK_GT(width_, 0);
99  CHECK_GT(height_, 0);
100  const int max_dim = 1 << pyramid_.size();
101  *cx = Clip<size_t>(static_cast<size_t>(max_dim * x / width_), 0,
102  static_cast<size_t>(max_dim - 1));
103  *cy = Clip<size_t>(static_cast<size_t>(max_dim * y / height_), 0,
104  static_cast<size_t>(max_dim - 1));
105 }
106 
107 } // namespace colmap
int width
int height
void ResetPoint(const double x, const double y)
void SetPoint(const double x, const double y)
normal_z y
normal_z x