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/
5
// Redistribution and use in source and binary forms, with or without
6
// modification, are permitted provided that the following conditions are met:
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.
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.
29
// Author: sameeragarwal@google.com (Sameer Agarwal)
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"
41
TEST(IdentityParameterization, EverythingTest) {
42
IdentityParameterization parameterization(3);
43
EXPECT_EQ(parameterization.GlobalSize(), 3);
44
EXPECT_EQ(parameterization.LocalSize(), 3);
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);
55
parameterization.ComputeJacobian(x, jacobian);
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);
64
TEST(SubsetParameterization, DeathTests) {
65
vector<int> constant_parameters;
66
EXPECT_DEATH(SubsetParameterization parameterization(1, constant_parameters),
69
constant_parameters.push_back(0);
70
EXPECT_DEATH(SubsetParameterization parameterization(1, constant_parameters),
71
"Number of parameters");
73
constant_parameters.push_back(1);
74
EXPECT_DEATH(SubsetParameterization parameterization(2, constant_parameters),
75
"Number of parameters");
77
constant_parameters.push_back(1);
78
EXPECT_DEATH(SubsetParameterization parameterization(2, constant_parameters),
82
TEST(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};
91
parameterization.Plus(x, delta, x_plus_delta);
93
for (int j = 0; j < 4; ++j) {
95
EXPECT_EQ(x_plus_delta[j], x[j]);
97
EXPECT_EQ(x_plus_delta[j], x[j] + delta[k++]);
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) {
107
for (int k = 0; k < 3; ++k, jacobian_cursor++) {
108
EXPECT_EQ(jacobian[jacobian_cursor], delta_cursor == k ? 1.0 : 0.0);
112
for (int k = 0; k < 3; ++k, jacobian_cursor++) {
113
EXPECT_EQ(jacobian[jacobian_cursor], 0.0);
120
// Functor needed to implement automatically differentiated Plus for
122
struct QuaternionPlus {
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];
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];
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.
142
q_delta[1] = delta[0];
143
q_delta[2] = delta[1];
144
q_delta[3] = delta[2];
147
QuaternionProduct(q_delta, x, x_plus_delta);
152
void QuaternionParameterizationTestHelper(const double* x,
154
const double* q_delta) {
155
const double kTolerance = 1e-14;
156
double x_plus_delta_ref[4] = {0.0, 0.0, 0.0, 0.0};
157
QuaternionProduct(q_delta, x, x_plus_delta_ref);
159
double x_plus_delta[4] = {0.0, 0.0, 0.0, 0.0};
160
QuaternionParameterization param;
161
param.Plus(x, delta, x_plus_delta);
162
for (int i = 0; i < 4; ++i) {
163
EXPECT_NEAR(x_plus_delta[i], x_plus_delta_ref[i], kTolerance);
166
const double x_plus_delta_norm =
167
sqrt(x_plus_delta[0] * x_plus_delta[0] +
168
x_plus_delta[1] * x_plus_delta[1] +
169
x_plus_delta[2] * x_plus_delta[2] +
170
x_plus_delta[3] * x_plus_delta[3]);
172
EXPECT_NEAR(x_plus_delta_norm, 1.0, kTolerance);
174
double jacobian_ref[12];
175
double zero_delta[3] = {0.0, 0.0, 0.0};
176
const double* parameters[2] = {x, zero_delta};
177
double* jacobian_array[2] = { NULL, jacobian_ref };
179
// Autodiff jacobian at delta_x = 0.
180
internal::AutoDiff<QuaternionPlus, double, 4, 3>::Differentiate(
181
QuaternionPlus(), parameters, 4, x_plus_delta, jacobian_array);
184
param.ComputeJacobian(x, jacobian);
185
for (int i = 0; i < 12; ++i) {
186
EXPECT_TRUE(isfinite(jacobian[i]));
187
EXPECT_NEAR(jacobian[i], jacobian_ref[i], kTolerance)
188
<< "Jacobian mismatch: i = " << i
189
<< "\n Expected \n" << ConstMatrixRef(jacobian_ref, 4, 3)
190
<< "\n Actual \n" << ConstMatrixRef(jacobian, 4, 3);
194
TEST(QuaternionParameterization, ZeroTest) {
195
double x[4] = {0.5, 0.5, 0.5, 0.5};
196
double delta[3] = {0.0, 0.0, 0.0};
197
double q_delta[4] = {1.0, 0.0, 0.0, 0.0};
198
QuaternionParameterizationTestHelper(x, delta, q_delta);
202
TEST(QuaternionParameterization, NearZeroTest) {
203
double x[4] = {0.52, 0.25, 0.15, 0.45};
204
double norm_x = sqrt(x[0] * x[0] +
208
for (int i = 0; i < 4; ++i) {
209
x[i] = x[i] / norm_x;
212
double delta[3] = {0.24, 0.15, 0.10};
213
for (int i = 0; i < 3; ++i) {
214
delta[i] = delta[i] * 1e-14;
219
q_delta[1] = delta[0];
220
q_delta[2] = delta[1];
221
q_delta[3] = delta[2];
223
QuaternionParameterizationTestHelper(x, delta, q_delta);
226
TEST(QuaternionParameterization, AwayFromZeroTest) {
227
double x[4] = {0.52, 0.25, 0.15, 0.45};
228
double norm_x = sqrt(x[0] * x[0] +
233
for (int i = 0; i < 4; ++i) {
234
x[i] = x[i] / norm_x;
237
double delta[3] = {0.24, 0.15, 0.10};
238
const double delta_norm = sqrt(delta[0] * delta[0] +
239
delta[1] * delta[1] +
240
delta[2] * delta[2]);
242
q_delta[0] = cos(delta_norm);
243
q_delta[1] = sin(delta_norm) / delta_norm * delta[0];
244
q_delta[2] = sin(delta_norm) / delta_norm * delta[1];
245
q_delta[3] = sin(delta_norm) / delta_norm * delta[2];
247
QuaternionParameterizationTestHelper(x, delta, q_delta);
251
} // namespace internal