blob: 55c765b1b345862c9ed2b4b18684539e00888a9c [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
31#include <cmath>
Keir Mierle58ede272012-06-24 17:23:57 -070032#include "ceres/fpclassify.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070033#include "ceres/internal/autodiff.h"
34#include "ceres/internal/eigen.h"
35#include "ceres/local_parameterization.h"
36#include "ceres/rotation.h"
Sameer Agarwal0beab862012-08-13 15:12:01 -070037#include "gtest/gtest.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070038
39namespace ceres {
40namespace internal {
41
42TEST(IdentityParameterization, EverythingTest) {
43 IdentityParameterization parameterization(3);
44 EXPECT_EQ(parameterization.GlobalSize(), 3);
45 EXPECT_EQ(parameterization.LocalSize(), 3);
46
47 double x[3] = {1.0, 2.0, 3.0};
48 double delta[3] = {0.0, 1.0, 2.0};
49 double x_plus_delta[3] = {0.0, 0.0, 0.0};
50 parameterization.Plus(x, delta, x_plus_delta);
51 EXPECT_EQ(x_plus_delta[0], 1.0);
52 EXPECT_EQ(x_plus_delta[1], 3.0);
53 EXPECT_EQ(x_plus_delta[2], 5.0);
54
55 double jacobian[9];
56 parameterization.ComputeJacobian(x, jacobian);
57 int k = 0;
58 for (int i = 0; i < 3; ++i) {
59 for (int j = 0; j < 3; ++j, ++k) {
60 EXPECT_EQ(jacobian[k], (i == j) ? 1.0 : 0.0);
61 }
62 }
63}
64
Keir Mierleefe7ac62012-06-24 22:25:28 -070065// Death tests are not working on Windows yet.
66// TODO(keir): Figure out how to enable these.
67#ifndef _WIN32
68
Keir Mierle8ebb0732012-04-30 23:09:08 -070069TEST(SubsetParameterization, DeathTests) {
70 vector<int> constant_parameters;
71 EXPECT_DEATH(SubsetParameterization parameterization(1, constant_parameters),
72 "at least");
73
74 constant_parameters.push_back(0);
75 EXPECT_DEATH(SubsetParameterization parameterization(1, constant_parameters),
76 "Number of parameters");
77
78 constant_parameters.push_back(1);
79 EXPECT_DEATH(SubsetParameterization parameterization(2, constant_parameters),
80 "Number of parameters");
81
82 constant_parameters.push_back(1);
83 EXPECT_DEATH(SubsetParameterization parameterization(2, constant_parameters),
84 "duplicates");
85}
86
Keir Mierleefe7ac62012-06-24 22:25:28 -070087#endif // _WIN32
88
Keir Mierle8ebb0732012-04-30 23:09:08 -070089TEST(SubsetParameterization, NormalFunctionTest) {
90 double x[4] = {1.0, 2.0, 3.0, 4.0};
91 for (int i = 0; i < 4; ++i) {
92 vector<int> constant_parameters;
93 constant_parameters.push_back(i);
94 SubsetParameterization parameterization(4, constant_parameters);
95 double delta[3] = {1.0, 2.0, 3.0};
96 double x_plus_delta[4] = {0.0, 0.0, 0.0};
97
98 parameterization.Plus(x, delta, x_plus_delta);
99 int k = 0;
100 for (int j = 0; j < 4; ++j) {
101 if (j == i) {
102 EXPECT_EQ(x_plus_delta[j], x[j]);
103 } else {
104 EXPECT_EQ(x_plus_delta[j], x[j] + delta[k++]);
105 }
106 }
107
108 double jacobian[4 * 3];
109 parameterization.ComputeJacobian(x, jacobian);
110 int delta_cursor = 0;
111 int jacobian_cursor = 0;
112 for (int j = 0; j < 4; ++j) {
113 if (j != i) {
114 for (int k = 0; k < 3; ++k, jacobian_cursor++) {
115 EXPECT_EQ(jacobian[jacobian_cursor], delta_cursor == k ? 1.0 : 0.0);
116 }
117 ++delta_cursor;
118 } else {
119 for (int k = 0; k < 3; ++k, jacobian_cursor++) {
120 EXPECT_EQ(jacobian[jacobian_cursor], 0.0);
121 }
122 }
123 }
124 };
125}
126
127// Functor needed to implement automatically differentiated Plus for
128// quaternions.
129struct QuaternionPlus {
130 template<typename T>
131 bool operator()(const T* x, const T* delta, T* x_plus_delta) const {
132 const T squared_norm_delta =
133 delta[0] * delta[0] + delta[1] * delta[1] + delta[2] * delta[2];
134
135 T q_delta[4];
136 if (squared_norm_delta > T(0.0)) {
137 T norm_delta = sqrt(squared_norm_delta);
138 const T sin_delta_by_delta = sin(norm_delta) / norm_delta;
139 q_delta[0] = cos(norm_delta);
140 q_delta[1] = sin_delta_by_delta * delta[0];
141 q_delta[2] = sin_delta_by_delta * delta[1];
142 q_delta[3] = sin_delta_by_delta * delta[2];
143 } else {
144 // We do not just use q_delta = [1,0,0,0] here because that is a
145 // constant and when used for automatic differentiation will
146 // lead to a zero derivative. Instead we take a first order
147 // approximation and evaluate it at zero.
148 q_delta[0] = T(1.0);
149 q_delta[1] = delta[0];
150 q_delta[2] = delta[1];
151 q_delta[3] = delta[2];
152 }
153
154 QuaternionProduct(q_delta, x, x_plus_delta);
155 return true;
156 }
157};
158
159void QuaternionParameterizationTestHelper(const double* x,
160 const double* delta,
161 const double* q_delta) {
Sameer Agarwalb01f1982012-05-10 12:18:39 -0700162 const double kTolerance = 1e-14;
Keir Mierle8ebb0732012-04-30 23:09:08 -0700163 double x_plus_delta_ref[4] = {0.0, 0.0, 0.0, 0.0};
164 QuaternionProduct(q_delta, x, x_plus_delta_ref);
165
166 double x_plus_delta[4] = {0.0, 0.0, 0.0, 0.0};
167 QuaternionParameterization param;
168 param.Plus(x, delta, x_plus_delta);
169 for (int i = 0; i < 4; ++i) {
Sameer Agarwalb01f1982012-05-10 12:18:39 -0700170 EXPECT_NEAR(x_plus_delta[i], x_plus_delta_ref[i], kTolerance);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700171 }
172
173 const double x_plus_delta_norm =
174 sqrt(x_plus_delta[0] * x_plus_delta[0] +
175 x_plus_delta[1] * x_plus_delta[1] +
176 x_plus_delta[2] * x_plus_delta[2] +
177 x_plus_delta[3] * x_plus_delta[3]);
178
Sameer Agarwalb01f1982012-05-10 12:18:39 -0700179 EXPECT_NEAR(x_plus_delta_norm, 1.0, kTolerance);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700180
181 double jacobian_ref[12];
182 double zero_delta[3] = {0.0, 0.0, 0.0};
183 const double* parameters[2] = {x, zero_delta};
184 double* jacobian_array[2] = { NULL, jacobian_ref };
185
186 // Autodiff jacobian at delta_x = 0.
Keir Mierlefdeb5772012-05-09 07:38:07 -0700187 internal::AutoDiff<QuaternionPlus, double, 4, 3>::Differentiate(
188 QuaternionPlus(), parameters, 4, x_plus_delta, jacobian_array);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700189
190 double jacobian[12];
191 param.ComputeJacobian(x, jacobian);
192 for (int i = 0; i < 12; ++i) {
Keir Mierle58ede272012-06-24 17:23:57 -0700193 EXPECT_TRUE(IsFinite(jacobian[i]));
Sameer Agarwalb01f1982012-05-10 12:18:39 -0700194 EXPECT_NEAR(jacobian[i], jacobian_ref[i], kTolerance)
Keir Mierle8ebb0732012-04-30 23:09:08 -0700195 << "Jacobian mismatch: i = " << i
196 << "\n Expected \n" << ConstMatrixRef(jacobian_ref, 4, 3)
197 << "\n Actual \n" << ConstMatrixRef(jacobian, 4, 3);
198 }
199}
200
201TEST(QuaternionParameterization, ZeroTest) {
202 double x[4] = {0.5, 0.5, 0.5, 0.5};
203 double delta[3] = {0.0, 0.0, 0.0};
204 double q_delta[4] = {1.0, 0.0, 0.0, 0.0};
205 QuaternionParameterizationTestHelper(x, delta, q_delta);
206}
207
208
209TEST(QuaternionParameterization, NearZeroTest) {
210 double x[4] = {0.52, 0.25, 0.15, 0.45};
211 double norm_x = sqrt(x[0] * x[0] +
212 x[1] * x[1] +
213 x[2] * x[2] +
214 x[3] * x[3]);
215 for (int i = 0; i < 4; ++i) {
216 x[i] = x[i] / norm_x;
217 }
218
219 double delta[3] = {0.24, 0.15, 0.10};
220 for (int i = 0; i < 3; ++i) {
221 delta[i] = delta[i] * 1e-14;
222 }
223
224 double q_delta[4];
225 q_delta[0] = 1.0;
226 q_delta[1] = delta[0];
227 q_delta[2] = delta[1];
228 q_delta[3] = delta[2];
229
230 QuaternionParameterizationTestHelper(x, delta, q_delta);
231}
232
233TEST(QuaternionParameterization, AwayFromZeroTest) {
234 double x[4] = {0.52, 0.25, 0.15, 0.45};
235 double norm_x = sqrt(x[0] * x[0] +
236 x[1] * x[1] +
237 x[2] * x[2] +
238 x[3] * x[3]);
239
240 for (int i = 0; i < 4; ++i) {
241 x[i] = x[i] / norm_x;
242 }
243
244 double delta[3] = {0.24, 0.15, 0.10};
245 const double delta_norm = sqrt(delta[0] * delta[0] +
246 delta[1] * delta[1] +
247 delta[2] * delta[2]);
248 double q_delta[4];
249 q_delta[0] = cos(delta_norm);
250 q_delta[1] = sin(delta_norm) / delta_norm * delta[0];
251 q_delta[2] = sin(delta_norm) / delta_norm * delta[1];
252 q_delta[3] = sin(delta_norm) / delta_norm * delta[2];
253
254 QuaternionParameterizationTestHelper(x, delta, q_delta);
255}
256
257
258} // namespace internal
259} // namespace ceres