~ubuntu-branches/ubuntu/raring/ceres-solver/raring

« back to all changes in this revision

Viewing changes to internal/ceres/local_parameterization_test.cc

  • Committer: Package Import Robot
  • Author(s): Koichi Akabe
  • Date: 2012-06-04 07:15:43 UTC
  • Revision ID: package-import@ubuntu.com-20120604071543-zx6uthupvmtqn3k2
Tags: upstream-1.1.1
ImportĀ upstreamĀ versionĀ 1.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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/
 
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
 
 
38
namespace ceres {
 
39
namespace internal {
 
40
 
 
41
TEST(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
 
 
64
TEST(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
 
 
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};
 
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.
 
122
struct 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
 
 
152
void QuaternionParameterizationTestHelper(const double* x,
 
153
                                          const double* delta,
 
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);
 
158
 
 
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);
 
164
  }
 
165
 
 
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]);
 
171
 
 
172
  EXPECT_NEAR(x_plus_delta_norm, 1.0, kTolerance);
 
173
 
 
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 };
 
178
 
 
179
  // Autodiff jacobian at delta_x = 0.
 
180
  internal::AutoDiff<QuaternionPlus, double, 4, 3>::Differentiate(
 
181
      QuaternionPlus(), parameters, 4, x_plus_delta, jacobian_array);
 
182
 
 
183
  double jacobian[12];
 
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);
 
191
  }
 
192
}
 
193
 
 
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);
 
199
}
 
200
 
 
201
 
 
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] +
 
205
                       x[1] * x[1] +
 
206
                       x[2] * x[2] +
 
207
                       x[3] * x[3]);
 
208
  for (int i = 0; i < 4; ++i) {
 
209
    x[i] = x[i] / norm_x;
 
210
  }
 
211
 
 
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;
 
215
  }
 
216
 
 
217
  double q_delta[4];
 
218
  q_delta[0] = 1.0;
 
219
  q_delta[1] = delta[0];
 
220
  q_delta[2] = delta[1];
 
221
  q_delta[3] = delta[2];
 
222
 
 
223
  QuaternionParameterizationTestHelper(x, delta, q_delta);
 
224
}
 
225
 
 
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] +
 
229
                       x[1] * x[1] +
 
230
                       x[2] * x[2] +
 
231
                       x[3] * x[3]);
 
232
 
 
233
  for (int i = 0; i < 4; ++i) {
 
234
    x[i] = x[i] / norm_x;
 
235
  }
 
236
 
 
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]);
 
241
  double q_delta[4];
 
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];
 
246
 
 
247
  QuaternionParameterizationTestHelper(x, delta, q_delta);
 
248
}
 
249
 
 
250
 
 
251
}  // namespace internal
 
252
}  // namespace ceres