mirror of
https://github.com/ceres-solver/ceres-solver.git
synced 2026-08-29 08:34:37 +08:00
Add line local parameterization.
This CL adds a local parameterization for a n-dimensional line, which is represented as an origin point and a direction. The line direction is updated in the same way as a homogeneous vector and the origin point is updated perpendicular to the line direction. Change-Id: I733f395e5cc4250abf9778c26fe0a5ae1de6b624
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
// Ceres Solver - A fast non-linear least squares minimizer
|
||||
// Copyright 2015 Google Inc. All rights reserved.
|
||||
// http://code.google.com/p/ceres-solver/
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above copyright notice,
|
||||
// this list of conditions and the following disclaimer in the documentation
|
||||
// and/or other materials provided with the distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its contributors may be
|
||||
// used to endorse or promote products derived from this software without
|
||||
// specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
// POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// Author: vitus@google.com (Michael Vitus)
|
||||
|
||||
#ifndef CERES_PUBLIC_INTERNAL_HOUSEHOLDER_VECTOR_H_
|
||||
#define CERES_PUBLIC_INTERNAL_HOUSEHOLDER_VECTOR_H_
|
||||
|
||||
#include "Eigen/Core"
|
||||
#include "glog/logging.h"
|
||||
|
||||
namespace ceres {
|
||||
namespace internal {
|
||||
|
||||
// Algorithm 5.1.1 from 'Matrix Computations' by Golub et al. (Johns Hopkins
|
||||
// Studies in Mathematical Sciences) but using the nth element of the input
|
||||
// vector as pivot instead of first. This computes the vector v with v(n) = 1
|
||||
// and beta such that H = I - beta * v * v^T is orthogonal and
|
||||
// H * x = ||x||_2 * e_n.
|
||||
template <typename Derived, typename Scalar, int N>
|
||||
void ComputeHouseholderVector(const Eigen::DenseBase<Derived>& x,
|
||||
Eigen::Matrix<Scalar, N, 1>* v,
|
||||
Scalar* beta) {
|
||||
CHECK(beta != nullptr);
|
||||
CHECK(v != nullptr);
|
||||
CHECK_GT(x.rows(), 1);
|
||||
CHECK_EQ(x.rows(), v->rows());
|
||||
|
||||
Scalar sigma = x.head(x.rows() - 1).squaredNorm();
|
||||
*v = x;
|
||||
(*v)(v->rows() - 1) = Scalar(1.0);
|
||||
|
||||
*beta = Scalar(0.0);
|
||||
const Scalar& x_pivot = x(x.rows() - 1);
|
||||
|
||||
if (sigma <= Scalar(std::numeric_limits<double>::epsilon())) {
|
||||
if (x_pivot < Scalar(0.0)) {
|
||||
*beta = Scalar(2.0);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const Scalar mu = sqrt(x_pivot * x_pivot + sigma);
|
||||
Scalar v_pivot = Scalar(1.0);
|
||||
|
||||
if (x_pivot <= Scalar(0.0)) {
|
||||
v_pivot = x_pivot - mu;
|
||||
} else {
|
||||
v_pivot = -sigma / (x_pivot + mu);
|
||||
}
|
||||
|
||||
*beta = Scalar(2.0) * v_pivot * v_pivot / (sigma + v_pivot * v_pivot);
|
||||
|
||||
v->head(v->rows() - 1) /= v_pivot;
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
} // namespace ceres
|
||||
|
||||
#endif // CERES_PUBLIC_INTERNAL_HOUSEHOLDER_VECTOR_H_
|
||||
@@ -0,0 +1,172 @@
|
||||
// Ceres Solver - A fast non-linear least squares minimizer
|
||||
// Copyright 2020 Google Inc. All rights reserved.
|
||||
// http://ceres-solver.org/
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above copyright notice,
|
||||
// this list of conditions and the following disclaimer in the documentation
|
||||
// and/or other materials provided with the distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its contributors may be
|
||||
// used to endorse or promote products derived from this software without
|
||||
// specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
// POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// Author: jodebo_beck@gmx.de (Johannes Beck)
|
||||
//
|
||||
|
||||
#ifndef CERES_PUBLIC_INTERNAL_LINE_PARAMETERIZATION_H_
|
||||
#define CERES_PUBLIC_INTERNAL_LINE_PARAMETERIZATION_H_
|
||||
|
||||
#include "householder_vector.h"
|
||||
|
||||
namespace ceres {
|
||||
|
||||
template <int AmbientSpaceDimension>
|
||||
bool LineParameterization<AmbientSpaceDimension>::Plus(
|
||||
const double* x_ptr,
|
||||
const double* delta_ptr,
|
||||
double* x_plus_delta_ptr) const {
|
||||
// We seek a box plus operator of the form
|
||||
//
|
||||
// [o*, d*] = Plus([o, d], [delta_o, delta_d])
|
||||
//
|
||||
// where o is the origin point, d is the direction vector, delta_o is
|
||||
// the delta of the origin point and delta_d the delta of the direction and
|
||||
// o* and d* is the updated origin point and direction.
|
||||
//
|
||||
// We separate the Plus operator into the origin point and directional part
|
||||
// d* = Plus_d(d, delta_d)
|
||||
// o* = Plus_o(o, d, delta_o)
|
||||
//
|
||||
// The direction update function Plus_d is the same as for the homogeneous vector
|
||||
// parameterization:
|
||||
//
|
||||
// d* = H_{v(d)} [0.5 sinc(0.5 |delta_d|) delta_d, cos(0.5 |delta_d|)]^T
|
||||
//
|
||||
// where H is the householder matrix
|
||||
// H_{v} = I - (2 / |v|^2) v v^T
|
||||
// and
|
||||
// v(d) = d - sign(d_n) |d| e_n.
|
||||
//
|
||||
// The origin point update function Plus_o is defined as
|
||||
//
|
||||
// o* = o + H_{v(d)} [0.5 delta_o, 0]^T.
|
||||
|
||||
static constexpr int kDim = AmbientSpaceDimension;
|
||||
using AmbientVector = Eigen::Matrix<double, kDim, 1>;
|
||||
using AmbientVectorRef = Eigen::Map<Eigen::Matrix<double, kDim, 1>>;
|
||||
using ConstAmbientVectorRef = Eigen::Map<const Eigen::Matrix<double, kDim, 1>>;
|
||||
using ConstTangentVectorRef =
|
||||
Eigen::Map<const Eigen::Matrix<double, kDim - 1, 1>>;
|
||||
|
||||
|
||||
ConstAmbientVectorRef o(x_ptr);
|
||||
ConstAmbientVectorRef d(x_ptr + kDim);
|
||||
|
||||
ConstTangentVectorRef delta_o(delta_ptr);
|
||||
ConstTangentVectorRef delta_d(delta_ptr + kDim - 1);
|
||||
AmbientVectorRef o_plus_delta(x_plus_delta_ptr);
|
||||
AmbientVectorRef d_plus_delta(x_plus_delta_ptr + kDim);
|
||||
|
||||
const double norm_delta_d = delta_d.norm();
|
||||
|
||||
o_plus_delta = o;
|
||||
|
||||
// Shortcut for zero delta direction.
|
||||
if (norm_delta_d == 0.0) {
|
||||
d_plus_delta = d;
|
||||
|
||||
if (delta_o.isZero(0.0)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate the householder transformation which is needed for f_d and f_o.
|
||||
AmbientVector v;
|
||||
double beta;
|
||||
internal::ComputeHouseholderVector(d, &v, &beta);
|
||||
|
||||
if (norm_delta_d != 0.0) {
|
||||
// Map the delta from the minimum representation to the over parameterized
|
||||
// homogeneous vector. See section A6.9.2 on page 624 of Hartley & Zisserman
|
||||
// (2nd Edition) for a detailed description. Note there is a typo on Page
|
||||
// 625, line 4 so check the book errata.
|
||||
const double norm_delta_div_2 = 0.5 * norm_delta_d;
|
||||
const double sin_delta_by_delta =
|
||||
std::sin(norm_delta_div_2) / norm_delta_div_2;
|
||||
|
||||
// Apply the delta update to remain on the unit sphere. See section A6.9.3
|
||||
// on page 625 of Hartley & Zisserman (2nd Edition) for a detailed
|
||||
// description.
|
||||
AmbientVector y;
|
||||
y.template head<kDim - 1>() = 0.5 * sin_delta_by_delta * delta_d;
|
||||
y[kDim - 1] = std::cos(norm_delta_div_2);
|
||||
|
||||
d_plus_delta = d.norm() * (y - v * (beta * (v.transpose() * y)));
|
||||
}
|
||||
|
||||
// The null space is in the direction of the line, so the tangent space is
|
||||
// perpendicular to the line direction. This is achieved by using the
|
||||
// householder matrix of the direction and allow only movements
|
||||
// perpendicular to e_n.
|
||||
//
|
||||
// The factor of 0.5 is used to be consistent with the line direction
|
||||
// update.
|
||||
AmbientVector y;
|
||||
y << 0.5 * delta_o, 0;
|
||||
o_plus_delta += y - v * (beta * (v.transpose() * y));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template <int AmbientSpaceDimension>
|
||||
bool LineParameterization<AmbientSpaceDimension>::ComputeJacobian(
|
||||
const double* x_ptr, double* jacobian_ptr) const {
|
||||
static constexpr int kDim = AmbientSpaceDimension;
|
||||
using AmbientVector = Eigen::Matrix<double, kDim, 1>;
|
||||
using ConstAmbientVectorRef = Eigen::Map<const Eigen::Matrix<double, kDim, 1>>;
|
||||
using MatrixRef = Eigen::Map<
|
||||
Eigen::Matrix<double, 2 * kDim, 2 * (kDim - 1), Eigen::RowMajor>>;
|
||||
|
||||
ConstAmbientVectorRef d(x_ptr + kDim);
|
||||
MatrixRef jacobian(jacobian_ptr);
|
||||
|
||||
// Clear the Jacobian as only half of the matrix is not zero.
|
||||
jacobian.setZero();
|
||||
|
||||
AmbientVector v;
|
||||
double beta;
|
||||
internal::ComputeHouseholderVector(d, &v, &beta);
|
||||
|
||||
// The Jacobian is equal to J = 0.5 * H.leftCols(kDim - 1) where H is
|
||||
// the Householder matrix (H = I - beta * v * v') for the origin point. For
|
||||
// the line direction part the Jacobian is scaled by the norm of the
|
||||
// direction.
|
||||
for (int i = 0; i < kDim - 1; ++i) {
|
||||
jacobian.block(0, i, kDim, 1) = -0.5 * beta * v(i) * v;
|
||||
jacobian.col(i)(i) += 0.5;
|
||||
}
|
||||
|
||||
jacobian.template block<kDim, kDim - 1>(kDim, kDim - 1) =
|
||||
jacobian.template block<kDim, kDim - 1>(0, 0) * d.norm();
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace ceres
|
||||
|
||||
#endif // CERES_PUBLIC_INTERNAL_LINE_PARAMETERIZATION_H_
|
||||
@@ -262,6 +262,33 @@ class CERES_EXPORT HomogeneousVectorParameterization
|
||||
const int size_;
|
||||
};
|
||||
|
||||
// This provides a parameterization for lines, where the line is
|
||||
// over-parameterized by an origin point and a direction vector. So the
|
||||
// parameter vector size needs to be two times the ambient space dimension,
|
||||
// where the first half is interpreted as the origin point and the second half
|
||||
// as the direction.
|
||||
//
|
||||
// The plus operator for the line direction is the same as for the
|
||||
// HomogeneousVectorParameterization. The update of the origin point is
|
||||
// perpendicular to the line direction before the update.
|
||||
//
|
||||
// This local parameterization is a special case of the affine Grassmannian
|
||||
// manifold (see https://en.wikipedia.org/wiki/Affine_Grassmannian_(manifold))
|
||||
// for the case Graff_1(R^n).
|
||||
template <int AmbientSpaceDimension>
|
||||
class CERES_EXPORT LineParameterization : public LocalParameterization {
|
||||
public:
|
||||
static_assert(AmbientSpaceDimension >= 2,
|
||||
"The ambient space must be at least 2");
|
||||
|
||||
bool Plus(const double* x,
|
||||
const double* delta,
|
||||
double* x_plus_delta) const override;
|
||||
bool ComputeJacobian(const double* x, double* jacobian) const override;
|
||||
int GlobalSize() const override { return 2 * AmbientSpaceDimension; }
|
||||
int LocalSize() const override { return 2 * (AmbientSpaceDimension - 1); }
|
||||
};
|
||||
|
||||
// Construct a local parameterization by taking the Cartesian product
|
||||
// of a number of other local parameterizations. This is useful, when
|
||||
// a parameter block is the cartesian product of two or more
|
||||
@@ -328,5 +355,7 @@ class CERES_EXPORT ProductParameterization : public LocalParameterization {
|
||||
} // namespace ceres
|
||||
|
||||
#include "ceres/internal/reenable_warnings.h"
|
||||
#include "ceres/internal/line_parameterization.h"
|
||||
|
||||
#endif // CERES_PUBLIC_LOCAL_PARAMETERIZATION_H_
|
||||
|
||||
|
||||
Reference in New Issue
Block a user