10ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Ceres Solver - A fast non-linear least squares minimizer 20ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Copyright 2010, 2011, 2012 Google Inc. All rights reserved. 30ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// http://code.google.com/p/ceres-solver/ 40ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 50ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Redistribution and use in source and binary forms, with or without 60ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// modification, are permitted provided that the following conditions are met: 70ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 80ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Redistributions of source code must retain the above copyright notice, 90ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// this list of conditions and the following disclaimer. 100ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Redistributions in binary form must reproduce the above copyright notice, 110ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// this list of conditions and the following disclaimer in the documentation 120ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// and/or other materials provided with the distribution. 130ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Neither the name of Google Inc. nor the names of its contributors may be 140ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// used to endorse or promote products derived from this software without 150ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// specific prior written permission. 160ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 170ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 180ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 190ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 200ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 210ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 220ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 230ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 240ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 250ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 260ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 270ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// POSSIBILITY OF SUCH DAMAGE. 280ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 290ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Author: sameeragarwal@google.com (Sameer Agarwal) 300ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 310ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/local_parameterization.h" 320ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 330ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/internal/eigen.h" 340ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/rotation.h" 350ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "glog/logging.h" 360ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 370ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongnamespace ceres { 380ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 390ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus KongIdentityParameterization::IdentityParameterization(const int size) 400ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong : size_(size) { 410ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong CHECK_GT(size, 0); 420ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong} 430ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 440ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongbool IdentityParameterization::Plus(const double* x, 450ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong const double* delta, 460ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong double* x_plus_delta) const { 470ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong VectorRef(x_plus_delta, size_) = 480ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong ConstVectorRef(x, size_) + ConstVectorRef(delta, size_); 490ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong return true; 500ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong} 510ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 520ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongbool IdentityParameterization::ComputeJacobian(const double* x, 530ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong double* jacobian) const { 540ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong MatrixRef(jacobian, size_, size_) = Matrix::Identity(size_, size_); 550ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong return true; 560ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong} 570ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 580ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus KongSubsetParameterization::SubsetParameterization( 590ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong int size, 600ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong const vector<int>& constant_parameters) 610ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong : local_size_(size - constant_parameters.size()), 620ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong constancy_mask_(size, 0) { 630ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong CHECK_GT(constant_parameters.size(), 0) 640ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong << "The set of constant parameters should contain at least " 650ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong << "one element. If you do not wish to hold any parameters " 660ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong << "constant, then do not use a SubsetParameterization"; 670ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 680ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong vector<int> constant = constant_parameters; 690ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong sort(constant.begin(), constant.end()); 700ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong CHECK(unique(constant.begin(), constant.end()) == constant.end()) 710ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong << "The set of constant parameters cannot contain duplicates"; 720ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong CHECK_LT(constant_parameters.size(), size) 730ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong << "Number of parameters held constant should be less " 740ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong << "than the size of the parameter block. If you wish " 750ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong << "to hold the entire parameter block constant, then a " 760ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong << "efficient way is to directly mark it as constant " 770ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong << "instead of using a LocalParameterization to do so."; 780ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong CHECK_GE(*min_element(constant.begin(), constant.end()), 0); 790ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong CHECK_LT(*max_element(constant.begin(), constant.end()), size); 800ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 810ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong for (int i = 0; i < constant_parameters.size(); ++i) { 820ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong constancy_mask_[constant_parameters[i]] = 1; 830ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong } 840ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong} 850ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 860ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongbool SubsetParameterization::Plus(const double* x, 870ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong const double* delta, 880ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong double* x_plus_delta) const { 890ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong for (int i = 0, j = 0; i < constancy_mask_.size(); ++i) { 900ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong if (constancy_mask_[i]) { 910ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong x_plus_delta[i] = x[i]; 920ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong } else { 930ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong x_plus_delta[i] = x[i] + delta[j++]; 940ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong } 950ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong } 960ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong return true; 970ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong} 980ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 990ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongbool SubsetParameterization::ComputeJacobian(const double* x, 1000ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong double* jacobian) const { 1010ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong MatrixRef m(jacobian, constancy_mask_.size(), local_size_); 1020ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong m.setZero(); 1030ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong for (int i = 0, j = 0; i < constancy_mask_.size(); ++i) { 1040ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong if (!constancy_mask_[i]) { 1050ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong m(i, j++) = 1.0; 1060ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong } 1070ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong } 1080ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong return true; 1090ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong} 1100ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 1110ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongbool QuaternionParameterization::Plus(const double* x, 1120ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong const double* delta, 1130ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong double* x_plus_delta) const { 1140ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong const double norm_delta = 1150ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong sqrt(delta[0] * delta[0] + delta[1] * delta[1] + delta[2] * delta[2]); 1160ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong if (norm_delta > 0.0) { 1170ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong const double sin_delta_by_delta = (sin(norm_delta) / norm_delta); 1180ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong double q_delta[4]; 1190ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong q_delta[0] = cos(norm_delta); 1200ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong q_delta[1] = sin_delta_by_delta * delta[0]; 1210ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong q_delta[2] = sin_delta_by_delta * delta[1]; 1220ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong q_delta[3] = sin_delta_by_delta * delta[2]; 1230ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong QuaternionProduct(q_delta, x, x_plus_delta); 1240ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong } else { 1250ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong for (int i = 0; i < 4; ++i) { 1260ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong x_plus_delta[i] = x[i]; 1270ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong } 1280ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong } 1290ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong return true; 1300ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong} 1310ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 1320ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongbool QuaternionParameterization::ComputeJacobian(const double* x, 1330ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong double* jacobian) const { 1340ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong jacobian[0] = -x[1]; jacobian[1] = -x[2]; jacobian[2] = -x[3]; // NOLINT 1350ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong jacobian[3] = x[0]; jacobian[4] = x[3]; jacobian[5] = -x[2]; // NOLINT 1360ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong jacobian[6] = -x[3]; jacobian[7] = x[0]; jacobian[8] = x[1]; // NOLINT 1370ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong jacobian[9] = x[2]; jacobian[10] = -x[1]; jacobian[11] = x[0]; // NOLINT 1380ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong return true; 1390ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong} 1400ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 1410ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong} // namespace ceres 142