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 
32 #include "feature/types.h"
33 
34 #include "util/logging.h"
35 
36 namespace colmap {
37 
FeatureKeypoint()38 FeatureKeypoint::FeatureKeypoint()
39     : FeatureKeypoint(0, 0) {}
40 
FeatureKeypoint(const float x,const float y)41 FeatureKeypoint::FeatureKeypoint(const float x, const float y)
42     : FeatureKeypoint(x, y, 1, 0, 0, 1) {}
43 
FeatureKeypoint(const float x_,const float y_,const float scale,const float orientation)44 FeatureKeypoint::FeatureKeypoint(const float x_, const float y_,
45                                  const float scale, const float orientation)
46     : x(x_), y(y_) {
47   CHECK_GE(scale, 0.0);
48   const float scale_cos_orientation = scale * std::cos(orientation);
49   const float scale_sin_orientation = scale * std::sin(orientation);
50   a11 = scale_cos_orientation;
51   a12 = -scale_sin_orientation;
52   a21 = scale_sin_orientation;
53   a22 = scale_cos_orientation;
54 }
55 
FeatureKeypoint(const float x_,const float y_,const float a11_,const float a12_,const float a21_,const float a22_)56 FeatureKeypoint::FeatureKeypoint(const float x_, const float y_,
57                                  const float a11_, const float a12_,
58                                  const float a21_, const float a22_)
59     : x(x_), y(y_), a11(a11_), a12(a12_), a21(a21_), a22(a22_) {}
60 
FromParameters(const float x,const float y,const float scale_x,const float scale_y,const float orientation,const float shear)61 FeatureKeypoint FeatureKeypoint::FromParameters(const float x, const float y,
62                                                 const float scale_x,
63                                                 const float scale_y,
64                                                 const float orientation,
65                                                 const float shear) {
66   return FeatureKeypoint(x, y, scale_x * std::cos(orientation),
67                          -scale_y * std::sin(orientation + shear),
68                          scale_x * std::sin(orientation),
69                          scale_y * std::cos(orientation + shear));
70 }
71 
Rescale(const float scale)72 void FeatureKeypoint::Rescale(const float scale) {
73   Rescale(scale, scale);
74 }
75 
Rescale(const float scale_x,const float scale_y)76 void FeatureKeypoint::Rescale(const float scale_x, const float scale_y) {
77   CHECK_GT(scale_x, 0);
78   CHECK_GT(scale_y, 0);
79   x *= scale_x;
80   y *= scale_y;
81   a11 *= scale_x;
82   a12 *= scale_y;
83   a21 *= scale_x;
84   a22 *= scale_y;
85 }
86 
ComputeScale() const87 float FeatureKeypoint::ComputeScale() const {
88   return (ComputeScaleX() + ComputeScaleY()) / 2.0f;
89 }
90 
ComputeScaleX() const91 float FeatureKeypoint::ComputeScaleX() const {
92   return std::sqrt(a11 * a11 + a21 * a21);
93 }
94 
ComputeScaleY() const95 float FeatureKeypoint::ComputeScaleY() const {
96   return std::sqrt(a12 * a12 + a22 * a22);
97 }
98 
ComputeOrientation() const99 float FeatureKeypoint::ComputeOrientation() const {
100   return std::atan2(a21, a11);
101 }
102 
ComputeShear() const103 float FeatureKeypoint::ComputeShear() const {
104   return std::atan2(-a12, a22) - ComputeOrientation();
105 }
106 
107 }  // namespace colmap
108