1 // 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 31 #include <algorithm> 32 #include <cctype> 33 #include <string> 34 #include "ceres/types.h" 35 #include "glog/logging.h" 36 37 namespace ceres { 38 39 #define CASESTR(x) case x: return #x 40 41 #define STRENUM(x) if (value == #x) { *type = x; return true;} 42 UpperCase(string * input)43 void UpperCase(string* input) { 44 std::transform(input->begin(), input->end(), input->begin(), ::toupper); 45 } 46 LinearSolverTypeToString(LinearSolverType solver_type)47 const char* LinearSolverTypeToString(LinearSolverType solver_type) { 48 switch (solver_type) { 49 CASESTR(DENSE_NORMAL_CHOLESKY); 50 CASESTR(DENSE_QR); 51 CASESTR(SPARSE_NORMAL_CHOLESKY); 52 CASESTR(DENSE_SCHUR); 53 CASESTR(SPARSE_SCHUR); 54 CASESTR(ITERATIVE_SCHUR); 55 CASESTR(CGNR); 56 default: 57 return "UNKNOWN"; 58 } 59 } 60 StringToLinearSolverType(string value,LinearSolverType * type)61 bool StringToLinearSolverType(string value, LinearSolverType* type) { 62 UpperCase(&value); 63 STRENUM(DENSE_NORMAL_CHOLESKY); 64 STRENUM(DENSE_QR); 65 STRENUM(SPARSE_NORMAL_CHOLESKY); 66 STRENUM(DENSE_SCHUR); 67 STRENUM(SPARSE_SCHUR); 68 STRENUM(ITERATIVE_SCHUR); 69 STRENUM(CGNR); 70 return false; 71 } 72 PreconditionerTypeToString(PreconditionerType preconditioner_type)73 const char* PreconditionerTypeToString( 74 PreconditionerType preconditioner_type) { 75 switch (preconditioner_type) { 76 CASESTR(IDENTITY); 77 CASESTR(JACOBI); 78 CASESTR(SCHUR_JACOBI); 79 CASESTR(CLUSTER_JACOBI); 80 CASESTR(CLUSTER_TRIDIAGONAL); 81 default: 82 return "UNKNOWN"; 83 } 84 } 85 StringToPreconditionerType(string value,PreconditionerType * type)86 bool StringToPreconditionerType(string value, PreconditionerType* type) { 87 UpperCase(&value); 88 STRENUM(IDENTITY); 89 STRENUM(JACOBI); 90 STRENUM(SCHUR_JACOBI); 91 STRENUM(CLUSTER_JACOBI); 92 STRENUM(CLUSTER_TRIDIAGONAL); 93 return false; 94 } 95 SparseLinearAlgebraLibraryTypeToString(SparseLinearAlgebraLibraryType sparse_linear_algebra_library_type)96 const char* SparseLinearAlgebraLibraryTypeToString( 97 SparseLinearAlgebraLibraryType sparse_linear_algebra_library_type) { 98 switch (sparse_linear_algebra_library_type) { 99 CASESTR(SUITE_SPARSE); 100 CASESTR(CX_SPARSE); 101 default: 102 return "UNKNOWN"; 103 } 104 } 105 106 StringToSparseLinearAlgebraLibraryType(string value,SparseLinearAlgebraLibraryType * type)107 bool StringToSparseLinearAlgebraLibraryType( 108 string value, 109 SparseLinearAlgebraLibraryType* type) { 110 UpperCase(&value); 111 STRENUM(SUITE_SPARSE); 112 STRENUM(CX_SPARSE); 113 return false; 114 } 115 TrustRegionStrategyTypeToString(TrustRegionStrategyType trust_region_strategy_type)116 const char* TrustRegionStrategyTypeToString( 117 TrustRegionStrategyType trust_region_strategy_type) { 118 switch (trust_region_strategy_type) { 119 CASESTR(LEVENBERG_MARQUARDT); 120 CASESTR(DOGLEG); 121 default: 122 return "UNKNOWN"; 123 } 124 } 125 StringToTrustRegionStrategyType(string value,TrustRegionStrategyType * type)126 bool StringToTrustRegionStrategyType(string value, 127 TrustRegionStrategyType* type) { 128 UpperCase(&value); 129 STRENUM(LEVENBERG_MARQUARDT); 130 STRENUM(DOGLEG); 131 return false; 132 } 133 DoglegTypeToString(DoglegType dogleg_type)134 const char* DoglegTypeToString(DoglegType dogleg_type) { 135 switch (dogleg_type) { 136 CASESTR(TRADITIONAL_DOGLEG); 137 CASESTR(SUBSPACE_DOGLEG); 138 default: 139 return "UNKNOWN"; 140 } 141 } 142 StringToDoglegType(string value,DoglegType * type)143 bool StringToDoglegType(string value, DoglegType* type) { 144 UpperCase(&value); 145 STRENUM(TRADITIONAL_DOGLEG); 146 STRENUM(SUBSPACE_DOGLEG); 147 return false; 148 } 149 SolverTerminationTypeToString(SolverTerminationType termination_type)150 const char* SolverTerminationTypeToString( 151 SolverTerminationType termination_type) { 152 switch (termination_type) { 153 CASESTR(NO_CONVERGENCE); 154 CASESTR(FUNCTION_TOLERANCE); 155 CASESTR(GRADIENT_TOLERANCE); 156 CASESTR(PARAMETER_TOLERANCE); 157 CASESTR(NUMERICAL_FAILURE); 158 CASESTR(USER_ABORT); 159 CASESTR(USER_SUCCESS); 160 CASESTR(DID_NOT_RUN); 161 default: 162 return "UNKNOWN"; 163 } 164 } 165 LinearSolverTerminationTypeToString(LinearSolverTerminationType termination_type)166 const char* LinearSolverTerminationTypeToString( 167 LinearSolverTerminationType termination_type) { 168 switch (termination_type) { 169 CASESTR(TOLERANCE); 170 CASESTR(MAX_ITERATIONS); 171 CASESTR(STAGNATION); 172 CASESTR(FAILURE); 173 default: 174 return "UNKNOWN"; 175 } 176 } 177 178 #undef CASESTR 179 #undef STRENUM 180 IsSchurType(LinearSolverType type)181 bool IsSchurType(LinearSolverType type) { 182 return ((type == SPARSE_SCHUR) || 183 (type == DENSE_SCHUR) || 184 (type == ITERATIVE_SCHUR)); 185 } 186 IsSparseLinearAlgebraLibraryTypeAvailable(SparseLinearAlgebraLibraryType type)187 bool IsSparseLinearAlgebraLibraryTypeAvailable( 188 SparseLinearAlgebraLibraryType type) { 189 if (type == SUITE_SPARSE) { 190 #ifdef CERES_NO_SUITESPARSE 191 return false; 192 #else 193 return true; 194 #endif 195 } 196 197 if (type == CX_SPARSE) { 198 #ifdef CERES_NO_CXSPARSE 199 return false; 200 #else 201 return true; 202 #endif 203 } 204 205 LOG(WARNING) << "Unknown sparse linear algebra library " << type; 206 return false; 207 } 208 209 } // namespace ceres 210