Berkeley SfM
distance_metric.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, The Regents of the University of California (Regents).
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following
14  * disclaimer in the documentation and/or other materials provided
15  * with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Please contact the author(s) of this library if you have any questions.
34  * Authors: Erik Nelson ( eanelson@eecs.berkeley.edu )
35  * David Fridovich-Keil ( dfk@eecs.berkeley.edu )
36  */
37 
38 #include "distance_metric.h"
39 
40 namespace bsfm {
41 
43  static DistanceMetric instance;
44  return instance;
45 }
46 
47 void DistanceMetric::SetMetric(const Metric& metric) {
48  metric_ = metric;
49 }
50 
51 void DistanceMetric::SetMaximumDistance(double maximum_distance) {
52  maximum_distance_ = maximum_distance;
53 }
54 
55 double DistanceMetric::Max() const {
56  return maximum_distance_;
57 }
58 
59 double DistanceMetric::operator()(const Descriptor& descriptor1,
60  const Descriptor& descriptor2) {
61  double distance = 0.0;
62  switch (metric_) {
63  case SCALED_L2:
64  distance = GetScaledL2Distance(descriptor1, descriptor2);
65  break;
66  case HAMMING:
67  distance = GetHammingDistance(descriptor1, descriptor2);
68  break;
69  // No default to catch incompatible types at compile time.
70  }
71  return distance;
72 }
73 
75  std::vector<Descriptor>& descriptors) const {
76  bool normalized = false;
77  switch (metric_) {
78  case SCALED_L2:
79  NormalizeDescriptors(descriptors);
80  normalized = true;
81  break;
82  case HAMMING:
83  break;
84  // No default to catch incompatible types at compile time.
85  }
86 
87  return normalized;
88 }
89 
90 // Hidden constructor.
92  : maximum_distance_(std::numeric_limits<double>::max()) {
93  // Set metric to the default argument of SetMetric(), defined in the header.
94  SetMetric();
95 }
96 
98  const Descriptor& descriptor1, const Descriptor& descriptor2) const {
99  CHECK_EQ(descriptor1.size(), descriptor2.size());
100  return 1.0 - descriptor1.dot(descriptor2);
101 }
102 
104  const Descriptor& descriptor2) const {
105  CHECK_EQ(descriptor1.size(), descriptor2.size());
106  int sum = 0;
107  for (size_t ii = 0; ii < descriptor1.size(); ++ii) {
108  unsigned char d1 = static_cast<unsigned char>(descriptor1(ii));
109  unsigned char d2 = static_cast<unsigned char>(descriptor2(ii));
110  sum += d1 ^ d2;
111  }
112  return static_cast<double>(sum);
113 }
114 
116  std::vector<Descriptor>& descriptors) const {
117  for (auto& descriptor : descriptors) {
118  descriptor.normalize();
119  }
120 }
121 
122 } //\namespace bsfm
STL namespace.
double GetHammingDistance(const Descriptor &descriptor1, const Descriptor &descriptor2) const
static DistanceMetric & Instance()
void SetMaximumDistance(double maximum_distance)
double operator()(const Descriptor &descriptor1, const Descriptor &descriptor2)
double GetScaledL2Distance(const Descriptor &descriptor1, const Descriptor &descriptor2) const
void SetMetric(const Metric &metric=Metric::SCALED_L2)
Definition: camera.cpp:50
bool MaybeNormalizeDescriptors(std::vector< Descriptor > &descriptors) const
void NormalizeDescriptors(std::vector< Descriptor > &descriptors) const
::Eigen::Matrix< double, Eigen::Dynamic, 1 > Descriptor
Definition: types.h:83