ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
camera_models.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 
32 #include "base/camera_models.h"
33 
34 #include <unordered_map>
35 
36 #include <boost/algorithm/string.hpp>
37 
38 namespace colmap {
39 
40 // Initialize params_info, focal_length_idxs, principal_point_idxs,
41 // extra_params_idxs
42 #define CAMERA_MODEL_CASE(CameraModel) \
43  const int CameraModel::model_id = InitializeModelId(); \
44  const std::string CameraModel::model_name = \
45  CameraModel::InitializeModelName(); \
46  const size_t CameraModel::num_params = InitializeNumParams(); \
47  const std::string CameraModel::params_info = \
48  CameraModel::InitializeParamsInfo(); \
49  const std::vector<size_t> CameraModel::focal_length_idxs = \
50  CameraModel::InitializeFocalLengthIdxs(); \
51  const std::vector<size_t> CameraModel::principal_point_idxs = \
52  CameraModel::InitializePrincipalPointIdxs(); \
53  const std::vector<size_t> CameraModel::extra_params_idxs = \
54  CameraModel::InitializeExtraParamsIdxs();
55 
57 
58 #undef CAMERA_MODEL_CASE
59 
60 std::unordered_map<std::string, int> InitialzeCameraModelNameToId() {
61  std::unordered_map<std::string, int> camera_model_name_to_id;
62 
63 #define CAMERA_MODEL_CASE(CameraModel) \
64  camera_model_name_to_id.emplace(CameraModel::model_name, \
65  CameraModel::model_id);
66 
68 
69 #undef CAMERA_MODEL_CASE
70 
71  return camera_model_name_to_id;
72 }
73 
74 std::unordered_map<int, std::string> InitialzeCameraModelIdToName() {
75  std::unordered_map<int, std::string> camera_model_id_to_name;
76 
77 #define CAMERA_MODEL_CASE(CameraModel) \
78  camera_model_id_to_name.emplace(CameraModel::model_id, \
79  CameraModel::model_name);
80 
82 
83 #undef CAMERA_MODEL_CASE
84 
85  return camera_model_id_to_name;
86 }
87 
88 static const std::unordered_map<std::string, int> CAMERA_MODEL_NAME_TO_ID =
90 
91 static const std::unordered_map<int, std::string> CAMERA_MODEL_ID_TO_NAME =
93 
94 bool ExistsCameraModelWithName(const std::string& model_name) {
95  return CAMERA_MODEL_NAME_TO_ID.count(model_name) > 0;
96 }
97 
98 bool ExistsCameraModelWithId(const int model_id) {
99  return CAMERA_MODEL_ID_TO_NAME.count(model_id) > 0;
100 }
101 
102 int CameraModelNameToId(const std::string& model_name) {
103  const auto it = CAMERA_MODEL_NAME_TO_ID.find(model_name);
104  if (it == CAMERA_MODEL_NAME_TO_ID.end()) {
105  return kInvalidCameraModelId;
106  } else {
107  return it->second;
108  }
109 }
110 
111 std::string CameraModelIdToName(const int model_id) {
112  const auto it = CAMERA_MODEL_ID_TO_NAME.find(model_id);
113  if (it == CAMERA_MODEL_ID_TO_NAME.end()) {
114  return "";
115  } else {
116  return it->second;
117  }
118 }
119 
120 std::vector<double> CameraModelInitializeParams(const int model_id,
121  const double focal_length,
122  const size_t width,
123  const size_t height) {
124  // Assuming that image measurements are within [0, dim], i.e. that the
125  // upper left corner is the (0, 0) coordinate (rather than the center of
126  // the upper left pixel). This complies with the default SiftGPU convention.
127  switch (model_id) {
128 #define CAMERA_MODEL_CASE(CameraModel) \
129  case CameraModel::kModelId: \
130  return CameraModel::InitializeParams(focal_length, width, height); \
131  break;
132 
134 
135 #undef CAMERA_MODEL_CASE
136  }
137 }
138 
139 std::string CameraModelParamsInfo(const int model_id) {
140  switch (model_id) {
141 #define CAMERA_MODEL_CASE(CameraModel) \
142  case CameraModel::kModelId: \
143  return CameraModel::params_info; \
144  break;
145 
147 
148 #undef CAMERA_MODEL_CASE
149  }
150 
151  return "Camera model does not exist";
152 }
153 
154 static const std::vector<size_t> EMPTY_IDXS;
155 
156 const std::vector<size_t>& CameraModelFocalLengthIdxs(const int model_id) {
157  switch (model_id) {
158 #define CAMERA_MODEL_CASE(CameraModel) \
159  case CameraModel::kModelId: \
160  return CameraModel::focal_length_idxs; \
161  break;
162 
164 
165 #undef CAMERA_MODEL_CASE
166  }
167 
168  return EMPTY_IDXS;
169 }
170 
171 const std::vector<size_t>& CameraModelPrincipalPointIdxs(const int model_id) {
172  switch (model_id) {
173 #define CAMERA_MODEL_CASE(CameraModel) \
174  case CameraModel::kModelId: \
175  return CameraModel::principal_point_idxs; \
176  break;
177 
179 
180 #undef CAMERA_MODEL_CASE
181  }
182 
183  return EMPTY_IDXS;
184 }
185 
186 const std::vector<size_t>& CameraModelExtraParamsIdxs(const int model_id) {
187  switch (model_id) {
188 #define CAMERA_MODEL_CASE(CameraModel) \
189  case CameraModel::kModelId: \
190  return CameraModel::extra_params_idxs; \
191  break;
192 
194 
195 #undef CAMERA_MODEL_CASE
196  }
197 
198  return EMPTY_IDXS;
199 }
200 
201 size_t CameraModelNumParams(const int model_id) {
202  switch (model_id) {
203 #define CAMERA_MODEL_CASE(CameraModel) \
204  case CameraModel::kModelId: \
205  return CameraModel::num_params;
206 
208 
209 #undef CAMERA_MODEL_CASE
210  }
211 
212  return 0;
213 }
214 
215 bool CameraModelVerifyParams(const int model_id,
216  const std::vector<double>& params) {
217  switch (model_id) {
218 #define CAMERA_MODEL_CASE(CameraModel) \
219  case CameraModel::kModelId: \
220  if (params.size() == CameraModel::num_params) { \
221  return true; \
222  } \
223  break;
224 
226 
227 #undef CAMERA_MODEL_CASE
228  }
229 
230  return false;
231 }
232 
233 bool CameraModelHasBogusParams(const int model_id,
234  const std::vector<double>& params,
235  const size_t width, const size_t height,
236  const double min_focal_length_ratio,
237  const double max_focal_length_ratio,
238  const double max_extra_param) {
239  switch (model_id) {
240 #define CAMERA_MODEL_CASE(CameraModel) \
241  case CameraModel::kModelId: \
242  return CameraModel::HasBogusParams( \
243  params, width, height, min_focal_length_ratio, max_focal_length_ratio, \
244  max_extra_param); \
245  break;
246 
248 
249 #undef CAMERA_MODEL_CASE
250  }
251 
252  return false;
253 }
254 
255 } // namespace colmap
int width
int height
#define CAMERA_MODEL_CASES
Definition: camera_models.h:95
#define CAMERA_MODEL_SWITCH_CASES
const std::vector< size_t > & CameraModelFocalLengthIdxs(const int model_id)
static const int kInvalidCameraModelId
Definition: camera_models.h:55
bool ExistsCameraModelWithId(const int model_id)
static const std::unordered_map< std::string, int > CAMERA_MODEL_NAME_TO_ID
static const std::vector< size_t > EMPTY_IDXS
bool ExistsCameraModelWithName(const std::string &model_name)
bool CameraModelHasBogusParams(const int model_id, const std::vector< double > &params, const size_t width, const size_t height, const double min_focal_length_ratio, const double max_focal_length_ratio, const double max_extra_param)
static const std::unordered_map< int, std::string > CAMERA_MODEL_ID_TO_NAME
size_t CameraModelNumParams(const int model_id)
bool CameraModelVerifyParams(const int model_id, const std::vector< double > &params)
const std::vector< size_t > & CameraModelExtraParamsIdxs(const int model_id)
std::string CameraModelIdToName(const int model_id)
const std::vector< size_t > & CameraModelPrincipalPointIdxs(const int model_id)
std::unordered_map< int, std::string > InitialzeCameraModelIdToName()
CAMERA_MODEL_CASES std::unordered_map< std::string, int > InitialzeCameraModelNameToId()
std::string CameraModelParamsInfo(const int model_id)
std::vector< double > CameraModelInitializeParams(const int model_id, const double focal_length, const size_t width, const size_t height)
int CameraModelNameToId(const std::string &model_name)