Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame^] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
| 2 | // Copyright 2015 Google Inc. All rights reserved. |
| 3 | // http://ceres-solver.org/ |
| 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: keir@google.com (Keir Mierle) |
| 30 | // |
| 31 | // Utility functions useful for testing. |
| 32 | |
| 33 | #include "ceres/test_util.h" |
| 34 | |
| 35 | #include <algorithm> |
| 36 | #include <cmath> |
| 37 | #include "ceres/file.h" |
| 38 | #include "ceres/stringprintf.h" |
| 39 | #include "ceres/types.h" |
| 40 | #include "gflags/gflags.h" |
| 41 | #include "glog/logging.h" |
| 42 | #include "gtest/gtest.h" |
| 43 | |
| 44 | DECLARE_string(test_srcdir); |
| 45 | |
| 46 | // This macro is used to inject additional path information specific |
| 47 | // to the build system. |
| 48 | |
| 49 | #ifndef CERES_TEST_SRCDIR_SUFFIX |
| 50 | #define CERES_TEST_SRCDIR_SUFFIX "" |
| 51 | #endif |
| 52 | |
| 53 | namespace ceres { |
| 54 | namespace internal { |
| 55 | |
| 56 | bool ExpectClose(double x, double y, double max_abs_relative_difference) { |
| 57 | double absolute_difference = fabs(x - y); |
| 58 | double relative_difference = absolute_difference / std::max(fabs(x), fabs(y)); |
| 59 | if (x == 0 || y == 0) { |
| 60 | // If x or y is exactly zero, then relative difference doesn't have any |
| 61 | // meaning. Take the absolute difference instead. |
| 62 | relative_difference = absolute_difference; |
| 63 | } |
| 64 | if (relative_difference > max_abs_relative_difference) { |
| 65 | VLOG(1) << StringPrintf("x=%17g y=%17g abs=%17g rel=%17g", |
| 66 | x, y, absolute_difference, relative_difference); |
| 67 | } |
| 68 | |
| 69 | EXPECT_NEAR(relative_difference, 0.0, max_abs_relative_difference); |
| 70 | return relative_difference <= max_abs_relative_difference; |
| 71 | } |
| 72 | |
| 73 | void ExpectArraysCloseUptoScale(int n, |
| 74 | const double* p, |
| 75 | const double* q, |
| 76 | double tol) { |
| 77 | CHECK_GT(n, 0); |
| 78 | CHECK(p); |
| 79 | CHECK(q); |
| 80 | |
| 81 | double p_max = 0; |
| 82 | double q_max = 0; |
| 83 | int p_i = 0; |
| 84 | int q_i = 0; |
| 85 | |
| 86 | for (int i = 0; i < n; ++i) { |
| 87 | if (std::abs(p[i]) > p_max) { |
| 88 | p_max = std::abs(p[i]); |
| 89 | p_i = i; |
| 90 | } |
| 91 | if (std::abs(q[i]) > q_max) { |
| 92 | q_max = std::abs(q[i]); |
| 93 | q_i = i; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // If both arrays are all zeros, they are equal up to scale, but |
| 98 | // for testing purposes, that's more likely to be an error than |
| 99 | // a desired result. |
| 100 | CHECK_NE(p_max, 0.0); |
| 101 | CHECK_NE(q_max, 0.0); |
| 102 | |
| 103 | for (int i = 0; i < n; ++i) { |
| 104 | double p_norm = p[i] / p[p_i]; |
| 105 | double q_norm = q[i] / q[q_i]; |
| 106 | |
| 107 | EXPECT_NEAR(p_norm, q_norm, tol) << "i=" << i; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | void ExpectArraysClose(int n, |
| 112 | const double* p, |
| 113 | const double* q, |
| 114 | double tol) { |
| 115 | CHECK_GT(n, 0); |
| 116 | CHECK(p); |
| 117 | CHECK(q); |
| 118 | |
| 119 | for (int i = 0; i < n; ++i) { |
| 120 | EXPECT_TRUE(ExpectClose(p[i], q[i], tol)) |
| 121 | << "p[" << i << "]" << p[i] << " " |
| 122 | << "q[" << i << "]" << q[i] << " " |
| 123 | << "tol: " << tol; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | std::string TestFileAbsolutePath(const std::string& filename) { |
| 128 | return JoinPath(FLAGS_test_srcdir + CERES_TEST_SRCDIR_SUFFIX, |
| 129 | filename); |
| 130 | } |
| 131 | |
| 132 | std::string ToString(const Solver::Options& options) { |
| 133 | return StringPrintf( |
| 134 | "(%s, %s, %s, %s, %d)", |
| 135 | LinearSolverTypeToString(options.linear_solver_type), |
| 136 | SparseLinearAlgebraLibraryTypeToString( |
| 137 | options.sparse_linear_algebra_library_type), |
| 138 | options.linear_solver_ordering? "USER": "AUTOMATIC", |
| 139 | PreconditionerTypeToString(options.preconditioner_type), |
| 140 | options.num_threads); |
| 141 | } |
| 142 | |
| 143 | } // namespace internal |
| 144 | } // namespace ceres |