blob: 5f3455fdd412a0cf520d8f68e65883914ca75ae1 [file] [log] [blame]
Keir Mierle8ebb0732012-04-30 23:09:08 -07001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2010, 2011, 2012 Google Inc. All rights reserved.
3// http://code.google.com/p/ceres-solver/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7//
8// * Redistributions of source code must retain the above copyright notice,
9// this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13// * Neither the name of Google Inc. nor the names of its contributors may be
14// used to endorse or promote products derived from this software without
15// specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27// POSSIBILITY OF SUCH DAMAGE.
28//
29// Author: sameeragarwal@google.com (Sameer Agarwal)
30
Sameer Agarwalcbae8562012-09-02 13:50:43 -070031#include <algorithm>
Keir Mierle27dd0d32012-10-15 13:52:36 -070032#include <cctype>
Keir Mierle8ebb0732012-04-30 23:09:08 -070033#include <string>
34#include "ceres/types.h"
Sameer Agarwalcbae8562012-09-02 13:50:43 -070035#include "glog/logging.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070036
37namespace ceres {
38
39#define CASESTR(x) case x: return #x
Sameer Agarwalcbae8562012-09-02 13:50:43 -070040#define STRENUM(x) if (value == #x) { *type = x; return true;}
41
Sergey Sharybinb53c9662013-02-25 01:14:26 +060042static void UpperCase(string* input) {
Sameer Agarwalcbae8562012-09-02 13:50:43 -070043 std::transform(input->begin(), input->end(), input->begin(), ::toupper);
44}
45
Sameer Agarwal1afd4982012-11-29 10:28:11 -080046const char* LinearSolverTypeToString(LinearSolverType type) {
47 switch (type) {
Sameer Agarwalb9f15a52012-08-18 13:06:19 -070048 CASESTR(DENSE_NORMAL_CHOLESKY);
Keir Mierle8ebb0732012-04-30 23:09:08 -070049 CASESTR(DENSE_QR);
Sameer Agarwalb9f15a52012-08-18 13:06:19 -070050 CASESTR(SPARSE_NORMAL_CHOLESKY);
Keir Mierle8ebb0732012-04-30 23:09:08 -070051 CASESTR(DENSE_SCHUR);
52 CASESTR(SPARSE_SCHUR);
53 CASESTR(ITERATIVE_SCHUR);
Keir Mierlee2a6cdc2012-05-07 06:39:56 -070054 CASESTR(CGNR);
Keir Mierle8ebb0732012-04-30 23:09:08 -070055 default:
56 return "UNKNOWN";
57 }
58}
59
Sameer Agarwalcbae8562012-09-02 13:50:43 -070060bool StringToLinearSolverType(string value, LinearSolverType* type) {
61 UpperCase(&value);
62 STRENUM(DENSE_NORMAL_CHOLESKY);
63 STRENUM(DENSE_QR);
64 STRENUM(SPARSE_NORMAL_CHOLESKY);
65 STRENUM(DENSE_SCHUR);
66 STRENUM(SPARSE_SCHUR);
67 STRENUM(ITERATIVE_SCHUR);
68 STRENUM(CGNR);
69 return false;
70}
71
Sameer Agarwal1afd4982012-11-29 10:28:11 -080072const char* PreconditionerTypeToString(PreconditionerType type) {
73 switch (type) {
Keir Mierle8ebb0732012-04-30 23:09:08 -070074 CASESTR(IDENTITY);
75 CASESTR(JACOBI);
76 CASESTR(SCHUR_JACOBI);
77 CASESTR(CLUSTER_JACOBI);
78 CASESTR(CLUSTER_TRIDIAGONAL);
79 default:
80 return "UNKNOWN";
81 }
82}
83
Sameer Agarwalcbae8562012-09-02 13:50:43 -070084bool StringToPreconditionerType(string value, PreconditionerType* type) {
85 UpperCase(&value);
86 STRENUM(IDENTITY);
87 STRENUM(JACOBI);
88 STRENUM(SCHUR_JACOBI);
89 STRENUM(CLUSTER_JACOBI);
90 STRENUM(CLUSTER_TRIDIAGONAL);
91 return false;
92}
93
Sameer Agarwalb0518732012-05-29 00:27:57 -070094const char* SparseLinearAlgebraLibraryTypeToString(
Sameer Agarwal1afd4982012-11-29 10:28:11 -080095 SparseLinearAlgebraLibraryType type) {
96 switch (type) {
Sameer Agarwalb0518732012-05-29 00:27:57 -070097 CASESTR(SUITE_SPARSE);
98 CASESTR(CX_SPARSE);
99 default:
100 return "UNKNOWN";
101 }
102}
103
Sameer Agarwalcbae8562012-09-02 13:50:43 -0700104bool StringToSparseLinearAlgebraLibraryType(
105 string value,
106 SparseLinearAlgebraLibraryType* type) {
107 UpperCase(&value);
108 STRENUM(SUITE_SPARSE);
109 STRENUM(CX_SPARSE);
110 return false;
111}
112
Sameer Agarwal367b65e2013-08-09 10:35:37 -0700113const char* DenseLinearAlgebraLibraryTypeToString(
114 DenseLinearAlgebraLibraryType type) {
115 switch (type) {
116 CASESTR(EIGEN);
117 CASESTR(LAPACK);
118 default:
119 return "UNKNOWN";
120 }
121}
122
123bool StringToDenseLinearAlgebraLibraryType(
124 string value,
125 DenseLinearAlgebraLibraryType* type) {
126 UpperCase(&value);
127 STRENUM(EIGEN);
128 STRENUM(LAPACK);
129 return false;
130}
131
Sameer Agarwal1afd4982012-11-29 10:28:11 -0800132const char* TrustRegionStrategyTypeToString(TrustRegionStrategyType type) {
133 switch (type) {
Sameer Agarwalcbae8562012-09-02 13:50:43 -0700134 CASESTR(LEVENBERG_MARQUARDT);
135 CASESTR(DOGLEG);
136 default:
137 return "UNKNOWN";
138 }
139}
140
141bool StringToTrustRegionStrategyType(string value,
142 TrustRegionStrategyType* type) {
143 UpperCase(&value);
144 STRENUM(LEVENBERG_MARQUARDT);
145 STRENUM(DOGLEG);
146 return false;
147}
148
Sameer Agarwal1afd4982012-11-29 10:28:11 -0800149const char* DoglegTypeToString(DoglegType type) {
150 switch (type) {
Sameer Agarwalcbae8562012-09-02 13:50:43 -0700151 CASESTR(TRADITIONAL_DOGLEG);
152 CASESTR(SUBSPACE_DOGLEG);
153 default:
154 return "UNKNOWN";
155 }
156}
157
158bool StringToDoglegType(string value, DoglegType* type) {
159 UpperCase(&value);
160 STRENUM(TRADITIONAL_DOGLEG);
161 STRENUM(SUBSPACE_DOGLEG);
162 return false;
163}
164
Sameer Agarwal1afd4982012-11-29 10:28:11 -0800165const char* MinimizerTypeToString(MinimizerType type) {
166 switch (type) {
167 CASESTR(TRUST_REGION);
168 CASESTR(LINE_SEARCH);
169 default:
170 return "UNKNOWN";
171 }
172}
173
174bool StringToMinimizerType(string value, MinimizerType* type) {
175 UpperCase(&value);
176 STRENUM(TRUST_REGION);
177 STRENUM(LINE_SEARCH);
178 return false;
179}
180
181const char* LineSearchDirectionTypeToString(LineSearchDirectionType type) {
182 switch (type) {
183 CASESTR(STEEPEST_DESCENT);
184 CASESTR(NONLINEAR_CONJUGATE_GRADIENT);
185 CASESTR(LBFGS);
Alex Stewart9aa0e3c2013-07-05 20:22:37 +0100186 CASESTR(BFGS);
Sameer Agarwal1afd4982012-11-29 10:28:11 -0800187 default:
188 return "UNKNOWN";
189 }
190}
191
192bool StringToLineSearchDirectionType(string value,
193 LineSearchDirectionType* type) {
194 UpperCase(&value);
195 STRENUM(STEEPEST_DESCENT);
196 STRENUM(NONLINEAR_CONJUGATE_GRADIENT);
197 STRENUM(LBFGS);
Alex Stewart9aa0e3c2013-07-05 20:22:37 +0100198 STRENUM(BFGS);
Sameer Agarwal1afd4982012-11-29 10:28:11 -0800199 return false;
200}
201
202const char* LineSearchTypeToString(LineSearchType type) {
203 switch (type) {
204 CASESTR(ARMIJO);
Alex Stewart9aa0e3c2013-07-05 20:22:37 +0100205 CASESTR(WOLFE);
Sameer Agarwal1afd4982012-11-29 10:28:11 -0800206 default:
207 return "UNKNOWN";
208 }
209}
210
211bool StringToLineSearchType(string value, LineSearchType* type) {
212 UpperCase(&value);
213 STRENUM(ARMIJO);
Alex Stewart9aa0e3c2013-07-05 20:22:37 +0100214 STRENUM(WOLFE);
Sameer Agarwal1afd4982012-11-29 10:28:11 -0800215 return false;
216}
217
Sameer Agarwal09244012013-06-30 14:33:23 -0700218const char* LineSearchInterpolationTypeToString(
219 LineSearchInterpolationType type) {
220 switch (type) {
221 CASESTR(BISECTION);
222 CASESTR(QUADRATIC);
223 CASESTR(CUBIC);
224 default:
225 return "UNKNOWN";
226 }
227}
228
229bool StringToLineSearchInterpolationType(
230 string value,
231 LineSearchInterpolationType* type) {
232 UpperCase(&value);
233 STRENUM(BISECTION);
234 STRENUM(QUADRATIC);
235 STRENUM(CUBIC);
236 return false;
237}
238
Sameer Agarwal1afd4982012-11-29 10:28:11 -0800239const char* NonlinearConjugateGradientTypeToString(
240 NonlinearConjugateGradientType type) {
241 switch (type) {
242 CASESTR(FLETCHER_REEVES);
243 CASESTR(POLAK_RIBIRERE);
244 CASESTR(HESTENES_STIEFEL);
245 default:
246 return "UNKNOWN";
247 }
248}
249
250bool StringToNonlinearConjugateGradientType(
251 string value,
252 NonlinearConjugateGradientType* type) {
253 UpperCase(&value);
254 STRENUM(FLETCHER_REEVES);
255 STRENUM(POLAK_RIBIRERE);
256 STRENUM(HESTENES_STIEFEL);
257 return false;
258}
259
Sameer Agarwal5a974712013-06-07 22:38:30 -0700260const char* CovarianceAlgorithmTypeToString(
261 CovarianceAlgorithmType type) {
262 switch (type) {
263 CASESTR(DENSE_SVD);
264 CASESTR(SPARSE_CHOLESKY);
265 CASESTR(SPARSE_QR);
266 default:
267 return "UNKNOWN";
268 }
269}
270
271bool StringToCovarianceAlgorithmType(
272 string value,
273 CovarianceAlgorithmType* type) {
274 UpperCase(&value);
275 STRENUM(DENSE_SVD);
276 STRENUM(SPARSE_CHOLESKY);
277 STRENUM(SPARSE_QR);
278 return false;
279}
280
Sameer Agarwalf06b9fa2013-10-27 21:38:13 -0700281const char* VisibilityClusteringTypeToString(
282 VisibilityClusteringType type) {
283 switch (type) {
284 CASESTR(CANONICAL_VIEWS);
285 CASESTR(SINGLE_LINKAGE);
286 default:
287 return "UNKNOWN";
288 }
289}
290
291bool StringToVisibilityClusteringType(
292 string value,
293 VisibilityClusteringType* type) {
294 UpperCase(&value);
295 STRENUM(CANONICAL_VIEWS);
296 STRENUM(SINGLE_LINKAGE);
297 return false;
298}
299
Sameer Agarwal1afd4982012-11-29 10:28:11 -0800300const char* SolverTerminationTypeToString(SolverTerminationType type) {
301 switch (type) {
Keir Mierle8ebb0732012-04-30 23:09:08 -0700302 CASESTR(NO_CONVERGENCE);
303 CASESTR(FUNCTION_TOLERANCE);
304 CASESTR(GRADIENT_TOLERANCE);
305 CASESTR(PARAMETER_TOLERANCE);
306 CASESTR(NUMERICAL_FAILURE);
307 CASESTR(USER_ABORT);
308 CASESTR(USER_SUCCESS);
309 CASESTR(DID_NOT_RUN);
310 default:
311 return "UNKNOWN";
312 }
313}
314
Keir Mierle8ebb0732012-04-30 23:09:08 -0700315#undef CASESTR
Sameer Agarwalcbae8562012-09-02 13:50:43 -0700316#undef STRENUM
Keir Mierle8ebb0732012-04-30 23:09:08 -0700317
318bool IsSchurType(LinearSolverType type) {
319 return ((type == SPARSE_SCHUR) ||
320 (type == DENSE_SCHUR) ||
321 (type == ITERATIVE_SCHUR));
322}
323
Sameer Agarwal14ee7952012-09-06 11:05:32 -0700324bool IsSparseLinearAlgebraLibraryTypeAvailable(
325 SparseLinearAlgebraLibraryType type) {
326 if (type == SUITE_SPARSE) {
327#ifdef CERES_NO_SUITESPARSE
328 return false;
329#else
330 return true;
331#endif
332 }
333
334 if (type == CX_SPARSE) {
335#ifdef CERES_NO_CXSPARSE
336 return false;
337#else
338 return true;
339#endif
340 }
341
342 LOG(WARNING) << "Unknown sparse linear algebra library " << type;
343 return false;
344}
345
Sameer Agarwal367b65e2013-08-09 10:35:37 -0700346bool IsDenseLinearAlgebraLibraryTypeAvailable(
347 DenseLinearAlgebraLibraryType type) {
348 if (type == EIGEN) {
349 return true;
350 }
351 if (type == LAPACK) {
352#ifdef CERES_NO_LAPACK
353 return false;
354#else
355 return true;
356#endif
357 }
358
359 LOG(WARNING) << "Unknown dense linear algebra library " << type;
360 return false;
361}
362
Keir Mierle8ebb0732012-04-30 23:09:08 -0700363} // namespace ceres