ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
utils.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <array>
11 #include <cmath>
12 
13 namespace colmap {
14 namespace retrieval {
15 
16 struct ImageScore {
17  int image_id = -1;
18  float score = 0.0f;
19 };
20 
21 // Implements the weighting function used to derive a voting weight from the
22 // Hamming distance of two binary signatures. See Eqn. 4 in
23 // Arandjelovic, Zisserman. DisLocation: Scalable descriptor distinctiveness for
24 // location recognition. ACCV 2014.
25 // The template is the length of the Hamming embedding vectors.
26 // This class is based on an original implementation by Torsten Sattler.
27 template <int N, int kSigma = 16>
29 public:
30  static const size_t kMaxHammingDistance =
31  static_cast<size_t>(1.5f * kSigma);
32 
34  // Fills the look-up table.
35  const float sigma_squared = kSigma * kSigma;
36  for (int n = 0; n <= N; ++n) {
37  const float hamming_dist = static_cast<float>(n);
38  if (hamming_dist <= kMaxHammingDistance) {
39  look_up_table_.at(n) =
40  std::exp(-hamming_dist * hamming_dist / sigma_squared);
41  } else {
42  look_up_table_.at(n) = 0.0f;
43  }
44  }
45  }
46 
47  // Returns the weight for Hamming distance h and standard deviation sigma.
48  // Does not perform a range check when performing the look-up.
49  inline float operator()(const size_t hamming_dist) const {
50  return look_up_table_.at(hamming_dist);
51  }
52 
53 private:
54  // In order to avoid wasting computations, we once compute a look-up table
55  // storing all function values for all possible values of the standard
56  // deviation \sigma. This is implemented as a (N + 1) vector.
57  std::array<float, N + 1> look_up_table_;
58 };
59 
60 } // namespace retrieval
61 } // namespace colmap
static const size_t kMaxHammingDistance
Definition: utils.h:30
float operator()(const size_t hamming_dist) const
Definition: utils.h:49