blob: eeae74e3f95d46811ab12ecfce42adfd241c5c50 [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 <glog/logging.h>
32#include "ceres/internal/eigen.h"
33#include "ceres/local_parameterization.h"
34#include "ceres/rotation.h"
35
36namespace ceres {
37
38IdentityParameterization::IdentityParameterization(const int size)
39 : size_(size) {
40 CHECK_GT(size, 0);
41}
42
43bool IdentityParameterization::Plus(const double* x,
44 const double* delta,
45 double* x_plus_delta) const {
46 VectorRef(x_plus_delta, size_) =
47 ConstVectorRef(x, size_) + ConstVectorRef(delta, size_);
48 return true;
49}
50
51bool IdentityParameterization::ComputeJacobian(const double* x,
52 double* jacobian) const {
53 MatrixRef(jacobian, size_, size_) = Matrix::Identity(size_, size_);
54 return true;
55}
56
57SubsetParameterization::SubsetParameterization(
58 int size,
59 const vector<int>& constant_parameters)
60 : local_size_(size - constant_parameters.size()),
61 constancy_mask_(size, 0) {
62 CHECK_GT(constant_parameters.size(), 0)
63 << "The set of constant parameters should contain at least "
64 << "one element. If you do not wish to hold any parameters "
65 << "constant, then do not use a SubsetParameterization";
66
67 vector<int> constant = constant_parameters;
68 sort(constant.begin(), constant.end());
69 CHECK(unique(constant.begin(), constant.end()) == constant.end())
70 << "The set of constant parameters cannot contain duplicates";
71 CHECK_LT(constant_parameters.size(), size)
72 << "Number of parameters held constant should be less "
73 << "than the size of the parameter block. If you wish "
74 << "to hold the entire parameter block constant, then a "
75 << "efficient way is to directly mark it as constant "
76 << "instead of using a LocalParameterization to do so.";
77 CHECK_GE(*min_element(constant.begin(), constant.end()), 0);
78 CHECK_LT(*max_element(constant.begin(), constant.end()), size);
79
80 for (int i = 0; i < constant_parameters.size(); ++i) {
81 constancy_mask_[constant_parameters[i]] = 1;
82 }
83}
84
85bool SubsetParameterization::Plus(const double* x,
86 const double* delta,
87 double* x_plus_delta) const {
88 for (int i = 0, j = 0; i < constancy_mask_.size(); ++i) {
89 if (constancy_mask_[i]) {
90 x_plus_delta[i] = x[i];
91 } else {
92 x_plus_delta[i] = x[i] + delta[j++];
93 }
94 }
95 return true;
96}
97
98bool SubsetParameterization::ComputeJacobian(const double* x,
99 double* jacobian) const {
100 MatrixRef m(jacobian, constancy_mask_.size(), local_size_);
101 m.setZero();
102 for (int i = 0, j = 0; i < constancy_mask_.size(); ++i) {
103 if (!constancy_mask_[i]) {
104 m(i, j++) = 1.0;
105 }
106 }
107 return true;
108}
109
110bool QuaternionParameterization::Plus(const double* x,
111 const double* delta,
112 double* x_plus_delta) const {
113 const double norm_delta =
114 sqrt(delta[0] * delta[0] + delta[1] * delta[1] + delta[2] * delta[2]);
115 if (norm_delta > 0.0) {
116 const double sin_delta_by_delta = (sin(norm_delta) / norm_delta);
117 double q_delta[4];
118 q_delta[0] = cos(norm_delta);
119 q_delta[1] = sin_delta_by_delta * delta[0];
120 q_delta[2] = sin_delta_by_delta * delta[1];
121 q_delta[3] = sin_delta_by_delta * delta[2];
122 QuaternionProduct(q_delta, x, x_plus_delta);
123 } else {
124 for (int i = 0; i < 4; ++i) {
125 x_plus_delta[i] = x[i];
126 }
127 }
128 return true;
129}
130
131bool QuaternionParameterization::ComputeJacobian(const double* x,
132 double* jacobian) const {
133 jacobian[0] = -x[1]; jacobian[1] = -x[2]; jacobian[2] = -x[3]; // NOLINT
134 jacobian[3] = x[0]; jacobian[4] = x[3]; jacobian[5] = -x[2]; // NOLINT
135 jacobian[6] = -x[3]; jacobian[7] = x[0]; jacobian[8] = x[1]; // NOLINT
136 jacobian[9] = x[2]; jacobian[10] = -x[1]; jacobian[11] = x[0]; // NOLINT
137 return true;
138}
139
140} // namespace ceres