blob: 1533884b9b26fdba4344789e1284e0335e600178 [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>
32#include "gtest/gtest.h"
33#include "ceres/internal/autodiff.h"
34#include "ceres/internal/eigen.h"
35#include "ceres/local_parameterization.h"
36#include "ceres/rotation.h"
37
38namespace ceres {
39namespace internal {
40
41TEST(IdentityParameterization, EverythingTest) {
42 IdentityParameterization parameterization(3);
43 EXPECT_EQ(parameterization.GlobalSize(), 3);
44 EXPECT_EQ(parameterization.LocalSize(), 3);
45
46 double x[3] = {1.0, 2.0, 3.0};
47 double delta[3] = {0.0, 1.0, 2.0};
48 double x_plus_delta[3] = {0.0, 0.0, 0.0};
49 parameterization.Plus(x, delta, x_plus_delta);
50 EXPECT_EQ(x_plus_delta[0], 1.0);
51 EXPECT_EQ(x_plus_delta[1], 3.0);
52 EXPECT_EQ(x_plus_delta[2], 5.0);
53
54 double jacobian[9];
55 parameterization.ComputeJacobian(x, jacobian);
56 int k = 0;
57 for (int i = 0; i < 3; ++i) {
58 for (int j = 0; j < 3; ++j, ++k) {
59 EXPECT_EQ(jacobian[k], (i == j) ? 1.0 : 0.0);
60 }
61 }
62}
63
64TEST(SubsetParameterization, DeathTests) {
65 vector<int> constant_parameters;
66 EXPECT_DEATH(SubsetParameterization parameterization(1, constant_parameters),
67 "at least");
68
69 constant_parameters.push_back(0);
70 EXPECT_DEATH(SubsetParameterization parameterization(1, constant_parameters),
71 "Number of parameters");
72
73 constant_parameters.push_back(1);
74 EXPECT_DEATH(SubsetParameterization parameterization(2, constant_parameters),
75 "Number of parameters");
76
77 constant_parameters.push_back(1);
78 EXPECT_DEATH(SubsetParameterization parameterization(2, constant_parameters),
79 "duplicates");
80}
81
82TEST(SubsetParameterization, NormalFunctionTest) {
83 double x[4] = {1.0, 2.0, 3.0, 4.0};
84 for (int i = 0; i < 4; ++i) {
85 vector<int> constant_parameters;
86 constant_parameters.push_back(i);
87 SubsetParameterization parameterization(4, constant_parameters);
88 double delta[3] = {1.0, 2.0, 3.0};
89 double x_plus_delta[4] = {0.0, 0.0, 0.0};
90
91 parameterization.Plus(x, delta, x_plus_delta);
92 int k = 0;
93 for (int j = 0; j < 4; ++j) {
94 if (j == i) {
95 EXPECT_EQ(x_plus_delta[j], x[j]);
96 } else {
97 EXPECT_EQ(x_plus_delta[j], x[j] + delta[k++]);
98 }
99 }
100
101 double jacobian[4 * 3];
102 parameterization.ComputeJacobian(x, jacobian);
103 int delta_cursor = 0;
104 int jacobian_cursor = 0;
105 for (int j = 0; j < 4; ++j) {
106 if (j != i) {
107 for (int k = 0; k < 3; ++k, jacobian_cursor++) {
108 EXPECT_EQ(jacobian[jacobian_cursor], delta_cursor == k ? 1.0 : 0.0);
109 }
110 ++delta_cursor;
111 } else {
112 for (int k = 0; k < 3; ++k, jacobian_cursor++) {
113 EXPECT_EQ(jacobian[jacobian_cursor], 0.0);
114 }
115 }
116 }
117 };
118}
119
120// Functor needed to implement automatically differentiated Plus for
121// quaternions.
122struct QuaternionPlus {
123 template<typename T>
124 bool operator()(const T* x, const T* delta, T* x_plus_delta) const {
125 const T squared_norm_delta =
126 delta[0] * delta[0] + delta[1] * delta[1] + delta[2] * delta[2];
127
128 T q_delta[4];
129 if (squared_norm_delta > T(0.0)) {
130 T norm_delta = sqrt(squared_norm_delta);
131 const T sin_delta_by_delta = sin(norm_delta) / norm_delta;
132 q_delta[0] = cos(norm_delta);
133 q_delta[1] = sin_delta_by_delta * delta[0];
134 q_delta[2] = sin_delta_by_delta * delta[1];
135 q_delta[3] = sin_delta_by_delta * delta[2];
136 } else {
137 // We do not just use q_delta = [1,0,0,0] here because that is a
138 // constant and when used for automatic differentiation will
139 // lead to a zero derivative. Instead we take a first order
140 // approximation and evaluate it at zero.
141 q_delta[0] = T(1.0);
142 q_delta[1] = delta[0];
143 q_delta[2] = delta[1];
144 q_delta[3] = delta[2];
145 }
146
147 QuaternionProduct(q_delta, x, x_plus_delta);
148 return true;
149 }
150};
151
152void QuaternionParameterizationTestHelper(const double* x,
153 const double* delta,
154 const double* q_delta) {
155 double x_plus_delta_ref[4] = {0.0, 0.0, 0.0, 0.0};
156 QuaternionProduct(q_delta, x, x_plus_delta_ref);
157
158 double x_plus_delta[4] = {0.0, 0.0, 0.0, 0.0};
159 QuaternionParameterization param;
160 param.Plus(x, delta, x_plus_delta);
161 for (int i = 0; i < 4; ++i) {
162 EXPECT_EQ(x_plus_delta[i], x_plus_delta_ref[i]);
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, 1e-12);
172
173 double jacobian_ref[12];
174 double zero_delta[3] = {0.0, 0.0, 0.0};
175 const double* parameters[2] = {x, zero_delta};
176 double* jacobian_array[2] = { NULL, jacobian_ref };
177
178 // Autodiff jacobian at delta_x = 0.
179 internal::AutoDiff<QuaternionPlus, double, 4, 4, 3>::Differentiate(
180 QuaternionPlus(), parameters, x_plus_delta, jacobian_array);
181
182 double jacobian[12];
183 param.ComputeJacobian(x, jacobian);
184 for (int i = 0; i < 12; ++i) {
185 EXPECT_TRUE(isfinite(jacobian[i]));
186 EXPECT_NEAR(jacobian[i], jacobian_ref[i], 1e-12)
187 << "Jacobian mismatch: i = " << i
188 << "\n Expected \n" << ConstMatrixRef(jacobian_ref, 4, 3)
189 << "\n Actual \n" << ConstMatrixRef(jacobian, 4, 3);
190 }
191}
192
193TEST(QuaternionParameterization, ZeroTest) {
194 double x[4] = {0.5, 0.5, 0.5, 0.5};
195 double delta[3] = {0.0, 0.0, 0.0};
196 double q_delta[4] = {1.0, 0.0, 0.0, 0.0};
197 QuaternionParameterizationTestHelper(x, delta, q_delta);
198}
199
200
201TEST(QuaternionParameterization, NearZeroTest) {
202 double x[4] = {0.52, 0.25, 0.15, 0.45};
203 double norm_x = sqrt(x[0] * x[0] +
204 x[1] * x[1] +
205 x[2] * x[2] +
206 x[3] * x[3]);
207 for (int i = 0; i < 4; ++i) {
208 x[i] = x[i] / norm_x;
209 }
210
211 double delta[3] = {0.24, 0.15, 0.10};
212 for (int i = 0; i < 3; ++i) {
213 delta[i] = delta[i] * 1e-14;
214 }
215
216 double q_delta[4];
217 q_delta[0] = 1.0;
218 q_delta[1] = delta[0];
219 q_delta[2] = delta[1];
220 q_delta[3] = delta[2];
221
222 QuaternionParameterizationTestHelper(x, delta, q_delta);
223}
224
225TEST(QuaternionParameterization, AwayFromZeroTest) {
226 double x[4] = {0.52, 0.25, 0.15, 0.45};
227 double norm_x = sqrt(x[0] * x[0] +
228 x[1] * x[1] +
229 x[2] * x[2] +
230 x[3] * x[3]);
231
232 for (int i = 0; i < 4; ++i) {
233 x[i] = x[i] / norm_x;
234 }
235
236 double delta[3] = {0.24, 0.15, 0.10};
237 const double delta_norm = sqrt(delta[0] * delta[0] +
238 delta[1] * delta[1] +
239 delta[2] * delta[2]);
240 double q_delta[4];
241 q_delta[0] = cos(delta_norm);
242 q_delta[1] = sin(delta_norm) / delta_norm * delta[0];
243 q_delta[2] = sin(delta_norm) / delta_norm * delta[1];
244 q_delta[3] = sin(delta_norm) / delta_norm * delta[2];
245
246 QuaternionParameterizationTestHelper(x, delta, q_delta);
247}
248
249
250} // namespace internal
251} // namespace ceres