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: sameeragarwal@google.com (Sameer Agarwal) |
| 30 | |
| 31 | #include <cmath> |
| 32 | #include "ceres/autodiff_local_parameterization.h" |
| 33 | #include "ceres/local_parameterization.h" |
| 34 | #include "ceres/rotation.h" |
| 35 | #include "gtest/gtest.h" |
| 36 | |
| 37 | namespace ceres { |
| 38 | namespace internal { |
| 39 | |
| 40 | struct IdentityPlus { |
| 41 | template <typename T> |
| 42 | bool operator()(const T* x, const T* delta, T* x_plus_delta) const { |
| 43 | for (int i = 0; i < 3; ++i) { |
| 44 | x_plus_delta[i] = x[i] + delta[i]; |
| 45 | } |
| 46 | return true; |
| 47 | } |
| 48 | }; |
| 49 | |
| 50 | TEST(AutoDiffLocalParameterizationTest, IdentityParameterization) { |
| 51 | AutoDiffLocalParameterization<IdentityPlus, 3, 3> |
| 52 | parameterization; |
| 53 | |
| 54 | double x[3] = {1.0, 2.0, 3.0}; |
| 55 | double delta[3] = {0.0, 1.0, 2.0}; |
| 56 | double x_plus_delta[3] = {0.0, 0.0, 0.0}; |
| 57 | parameterization.Plus(x, delta, x_plus_delta); |
| 58 | |
| 59 | EXPECT_EQ(x_plus_delta[0], 1.0); |
| 60 | EXPECT_EQ(x_plus_delta[1], 3.0); |
| 61 | EXPECT_EQ(x_plus_delta[2], 5.0); |
| 62 | |
| 63 | double jacobian[9]; |
| 64 | parameterization.ComputeJacobian(x, jacobian); |
| 65 | int k = 0; |
| 66 | for (int i = 0; i < 3; ++i) { |
| 67 | for (int j = 0; j < 3; ++j, ++k) { |
| 68 | EXPECT_EQ(jacobian[k], (i == j) ? 1.0 : 0.0); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | struct ScaledPlus { |
| 74 | explicit ScaledPlus(const double &scale_factor) |
| 75 | : scale_factor_(scale_factor) |
| 76 | {} |
| 77 | |
| 78 | template <typename T> |
| 79 | bool operator()(const T* x, const T* delta, T* x_plus_delta) const { |
| 80 | for (int i = 0; i < 3; ++i) { |
| 81 | x_plus_delta[i] = x[i] + T(scale_factor_) * delta[i]; |
| 82 | } |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | const double scale_factor_; |
| 87 | }; |
| 88 | |
| 89 | TEST(AutoDiffLocalParameterizationTest, ScaledParameterization) { |
| 90 | const double kTolerance = 1e-14; |
| 91 | |
| 92 | AutoDiffLocalParameterization<ScaledPlus, 3, 3> |
| 93 | parameterization(new ScaledPlus(1.2345)); |
| 94 | |
| 95 | double x[3] = {1.0, 2.0, 3.0}; |
| 96 | double delta[3] = {0.0, 1.0, 2.0}; |
| 97 | double x_plus_delta[3] = {0.0, 0.0, 0.0}; |
| 98 | parameterization.Plus(x, delta, x_plus_delta); |
| 99 | |
| 100 | EXPECT_NEAR(x_plus_delta[0], 1.0, kTolerance); |
| 101 | EXPECT_NEAR(x_plus_delta[1], 3.2345, kTolerance); |
| 102 | EXPECT_NEAR(x_plus_delta[2], 5.469, kTolerance); |
| 103 | |
| 104 | double jacobian[9]; |
| 105 | parameterization.ComputeJacobian(x, jacobian); |
| 106 | int k = 0; |
| 107 | for (int i = 0; i < 3; ++i) { |
| 108 | for (int j = 0; j < 3; ++j, ++k) { |
| 109 | EXPECT_NEAR(jacobian[k], (i == j) ? 1.2345 : 0.0, kTolerance); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | struct QuaternionPlus { |
| 115 | template<typename T> |
| 116 | bool operator()(const T* x, const T* delta, T* x_plus_delta) const { |
| 117 | const T squared_norm_delta = |
| 118 | delta[0] * delta[0] + delta[1] * delta[1] + delta[2] * delta[2]; |
| 119 | |
| 120 | T q_delta[4]; |
| 121 | if (squared_norm_delta > T(0.0)) { |
| 122 | T norm_delta = sqrt(squared_norm_delta); |
| 123 | const T sin_delta_by_delta = sin(norm_delta) / norm_delta; |
| 124 | q_delta[0] = cos(norm_delta); |
| 125 | q_delta[1] = sin_delta_by_delta * delta[0]; |
| 126 | q_delta[2] = sin_delta_by_delta * delta[1]; |
| 127 | q_delta[3] = sin_delta_by_delta * delta[2]; |
| 128 | } else { |
| 129 | // We do not just use q_delta = [1,0,0,0] here because that is a |
| 130 | // constant and when used for automatic differentiation will |
| 131 | // lead to a zero derivative. Instead we take a first order |
| 132 | // approximation and evaluate it at zero. |
| 133 | q_delta[0] = T(1.0); |
| 134 | q_delta[1] = delta[0]; |
| 135 | q_delta[2] = delta[1]; |
| 136 | q_delta[3] = delta[2]; |
| 137 | } |
| 138 | |
| 139 | QuaternionProduct(q_delta, x, x_plus_delta); |
| 140 | return true; |
| 141 | } |
| 142 | }; |
| 143 | |
| 144 | void QuaternionParameterizationTestHelper(const double* x, |
| 145 | const double* delta) { |
| 146 | const double kTolerance = 1e-14; |
| 147 | double x_plus_delta_ref[4] = {0.0, 0.0, 0.0, 0.0}; |
| 148 | double jacobian_ref[12]; |
| 149 | |
| 150 | |
| 151 | QuaternionParameterization ref_parameterization; |
| 152 | ref_parameterization.Plus(x, delta, x_plus_delta_ref); |
| 153 | ref_parameterization.ComputeJacobian(x, jacobian_ref); |
| 154 | |
| 155 | double x_plus_delta[4] = {0.0, 0.0, 0.0, 0.0}; |
| 156 | double jacobian[12]; |
| 157 | AutoDiffLocalParameterization<QuaternionPlus, 4, 3> parameterization; |
| 158 | parameterization.Plus(x, delta, x_plus_delta); |
| 159 | parameterization.ComputeJacobian(x, jacobian); |
| 160 | |
| 161 | for (int i = 0; i < 4; ++i) { |
| 162 | EXPECT_NEAR(x_plus_delta[i], x_plus_delta_ref[i], kTolerance); |
| 163 | } |
| 164 | |
| 165 | const double x_plus_delta_norm = |
| 166 | sqrt(x_plus_delta[0] * x_plus_delta[0] + |
| 167 | x_plus_delta[1] * x_plus_delta[1] + |
| 168 | x_plus_delta[2] * x_plus_delta[2] + |
| 169 | x_plus_delta[3] * x_plus_delta[3]); |
| 170 | |
| 171 | EXPECT_NEAR(x_plus_delta_norm, 1.0, kTolerance); |
| 172 | |
| 173 | for (int i = 0; i < 12; ++i) { |
| 174 | EXPECT_TRUE(std::isfinite(jacobian[i])); |
| 175 | EXPECT_NEAR(jacobian[i], jacobian_ref[i], kTolerance) |
| 176 | << "Jacobian mismatch: i = " << i |
| 177 | << "\n Expected \n" << ConstMatrixRef(jacobian_ref, 4, 3) |
| 178 | << "\n Actual \n" << ConstMatrixRef(jacobian, 4, 3); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | TEST(AutoDiffLocalParameterization, QuaternionParameterizationZeroTest) { |
| 183 | double x[4] = {0.5, 0.5, 0.5, 0.5}; |
| 184 | double delta[3] = {0.0, 0.0, 0.0}; |
| 185 | QuaternionParameterizationTestHelper(x, delta); |
| 186 | } |
| 187 | |
| 188 | |
| 189 | TEST(AutoDiffLocalParameterization, QuaternionParameterizationNearZeroTest) { |
| 190 | double x[4] = {0.52, 0.25, 0.15, 0.45}; |
| 191 | double norm_x = sqrt(x[0] * x[0] + |
| 192 | x[1] * x[1] + |
| 193 | x[2] * x[2] + |
| 194 | x[3] * x[3]); |
| 195 | for (int i = 0; i < 4; ++i) { |
| 196 | x[i] = x[i] / norm_x; |
| 197 | } |
| 198 | |
| 199 | double delta[3] = {0.24, 0.15, 0.10}; |
| 200 | for (int i = 0; i < 3; ++i) { |
| 201 | delta[i] = delta[i] * 1e-14; |
| 202 | } |
| 203 | |
| 204 | QuaternionParameterizationTestHelper(x, delta); |
| 205 | } |
| 206 | |
| 207 | TEST(AutoDiffLocalParameterization, QuaternionParameterizationNonZeroTest) { |
| 208 | double x[4] = {0.52, 0.25, 0.15, 0.45}; |
| 209 | double norm_x = sqrt(x[0] * x[0] + |
| 210 | x[1] * x[1] + |
| 211 | x[2] * x[2] + |
| 212 | x[3] * x[3]); |
| 213 | |
| 214 | for (int i = 0; i < 4; ++i) { |
| 215 | x[i] = x[i] / norm_x; |
| 216 | } |
| 217 | |
| 218 | double delta[3] = {0.24, 0.15, 0.10}; |
| 219 | QuaternionParameterizationTestHelper(x, delta); |
| 220 | } |
| 221 | |
| 222 | } // namespace internal |
| 223 | } // namespace ceres |