38 template<
class CAMERA>
56 typedef std::vector<MatrixZD, Eigen::aligned_allocator<MatrixZD> >
FBlocks;
82 :
GaussianFactor(keys), FBlocks_(Fs), PointCovariance_(P), E_(E), b_(b) {}
88 const FBlocks&
Fs()
const {
107 std::cout <<
" RegularImplicitSchurFactor " << std::endl;
110 std::cout <<
"Fblock:\n" << FBlocks_[
pos] << std::endl;
112 std::cout <<
"PointCovariance:\n" << PointCovariance_ << std::endl;
113 std::cout <<
"E:\n" << E_ << std::endl;
114 std::cout <<
"b:\n" << b_.transpose() << std::endl;
119 const This*
f =
dynamic_cast<const This*
>(&lf);
122 for (
size_t k = 0; k < FBlocks_.size(); ++k) {
140 throw std::runtime_error(
141 "RegularImplicitSchurFactor::updateHessian non implemented");
144 throw std::runtime_error(
145 "RegularImplicitSchurFactor::augmentedJacobian non implemented");
148 std::pair<Matrix, Vector>
jacobian()
const override {
149 throw std::runtime_error(
150 "RegularImplicitSchurFactor::jacobian non implemented");
165 int m = this->
keys_.size();
167 return augmented.block(0, 0, M, M);
176 for (
size_t k = 0; k <
size(); ++k) {
181 const MatrixZD& Fj = FBlocks_[k];
183 * E_.block<
ZDim, 3>(ZDim * k, 0);
186 for (
int k = 0; k <
D; ++k) {
188 dj(k) = Fj.col(k).squaredNorm();
191 dj(k) -= FtE.row(k) * PointCovariance_ * FtE.row(k).transpose();
196 result.first->second += dj;
216 const MatrixZD& Fj = FBlocks_[
pos];
218 * E_.block<
ZDim, 3>(ZDim *
pos, 0);
221 for (
int k = 0; k <
D; ++k) {
222 dj(k) = Fj.col(k).squaredNorm();
224 dj(k) -= FtE.row(k) * PointCovariance_ * FtE.row(k).transpose();
226 DMap(d + D * j) += dj;
232 std::map<Key, Matrix> blocks;
237 const MatrixZD& Fj = FBlocks_[
pos];
243 const Matrix23& Ej = E_.block<
ZDim, 3>(ZDim *
pos, 0);
244 blocks[
j] = Fj.transpose()
245 * (Fj - Ej * PointCovariance_ * Ej.transpose() * Fj);
257 return std::make_shared<RegularImplicitSchurFactor<CAMERA> >(
keys_,
259 throw std::runtime_error(
260 "RegularImplicitSchurFactor::clone non implemented");
264 return std::make_shared<RegularImplicitSchurFactor<CAMERA> >(
keys_,
266 throw std::runtime_error(
267 "RegularImplicitSchurFactor::negate non implemented");
279 y += F.transpose() * e3;
282 typedef std::vector<Vector2, Eigen::aligned_allocator<Vector2>>
Error2s;
292 for (
size_t k = 0; k <
size(); k++)
293 d1 += E_.block<ZDim, 3>(ZDim * k, 0).transpose()
294 * (e1[k] - ZDim * b_.segment<ZDim>(k *
ZDim));
300 for (
size_t k = 0; k <
size(); k++)
301 e2[k] = e1[k] - ZDim * b_.segment<ZDim>(k * ZDim)
302 - E_.block<
ZDim, 3>(ZDim * k, 0) * d2;
321 for (
size_t k = 0; k <
size(); ++k)
326 for (
size_t k = 0; k <
size(); ++k)
329 double f = b_.squaredNorm();
330 return 0.5 * (result +
f);
343 for (
size_t k = 0; k <
size(); ++k)
344 e1[k] = FBlocks_[k] * x.
at(
keys_[k]) - b_.segment<ZDim>(k *
ZDim);
348 for (
size_t k = 0; k <
size(); ++k)
362 for (
size_t k = 0; k <
size(); k++)
363 d1 += E_.block<ZDim, 3>(ZDim * k, 0).transpose() * e1[k];
369 for (
size_t k = 0; k <
size(); k++)
370 e2[k] = e1[k] - E_.block<ZDim, 3>(ZDim * k, 0) *
d2;
392 for (
size_t k = 0; k <
size(); ++k) {
394 e1[k] = FBlocks_[k] * ConstDMap(x + D * key);
400 for (
size_t k = 0; k <
size(); ++k) {
402 DMap(y + D * key) += FBlocks_[k].transpose() * alpha * e2[k];
407 std::vector<size_t>
keys)
const {
421 for (
size_t k = 0; k <
size(); ++k)
422 e1[k] = FBlocks_[k] * x.
at(
keys_[k]);
427 for (
size_t k = 0; k <
size(); ++k) {
430 std::pair<VectorValues::iterator, bool> it = y.
tryInsert(key, empty);
431 Vector& yi = it.first->second;
434 yi = Vector::Zero(FBlocks_[k].
cols());
435 yi += FBlocks_[k].transpose() * alpha * e2[k];
445 for (
size_t k = 0; k <
size(); ++k) {
448 std::pair<VectorValues::iterator, bool> it = y.
tryInsert(key, empty);
449 Vector& yi = it.first->second;
461 for (
size_t k = 0; k <
size(); k++)
462 e1[k] = b_.segment<ZDim>(ZDim * k);
467 for (
size_t k = 0; k <
size(); ++k) {
469 g.
insert(key, -FBlocks_[k].transpose() * e2[k]);
488 for (
size_t k = 0; k <
size(); k++)
489 e1[k] = b_.segment<ZDim>(ZDim * k);
492 for (
size_t k = 0; k <
size(); ++k) {
494 DMap(d + D * j) += -FBlocks_[k].transpose() * e2[k];
500 throw std::runtime_error(
501 "gradient for RegularImplicitSchurFactor is not implemented yet");
507 template<
class CAMERA>
510 template<
class CAMERA>
515 RegularImplicitSchurFactor<CAMERA> > {
const gtsam::Symbol key('X', 0)
const Matrix & getPointCovariance() const
static const int D
Camera dimension.
bool empty() const
Whether the factor is empty (involves zero variables).
double errorJF(const VectorValues &x) const
void multiplyHessianAdd(double alpha, const double *x, double *y) const
double* Hessian-vector multiply, i.e. y += F'alpha(I - E*P*E')*F*x RAW memory access! Assumes keys st...
Matrix< RealScalar, Dynamic, Dynamic > M
void multiplyHessianAdd(double alpha, const double *x, double *y, std::vector< size_t > keys) const
void updateHessian(const KeyVector &keys, SymmetricBlockMatrix *info) const override
double dot(const V1 &a, const V2 &b)
virtual void print(const std::string &s="Factor", const KeyFormatter &formatter=DefaultKeyFormatter) const
print
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
print
A matrix or vector expression mapping an existing array of data.
static void multiplyHessianAdd(const Matrix &F, const Matrix &E, const Matrix &PointCovariance, double alpha, const Vector &x, Vector &y)
Base class to create smart factors on poses or cameras.
GaussianFactor::shared_ptr negate() const override
void hessianDiagonal(double *d) const override
add the contribution of this factor to the diagonal of the hessian d(output) = d(input) + deltaHessia...
GaussianFactor::shared_ptr clone() const override
KeyVector keys_
The keys involved in this factor.
iterator insert(const std::pair< Key, Vector > &key_value)
void hessianDiagonalAdd(VectorValues &d) const override
Add the diagonal of the Hessian for this factor to existing VectorValues.
void multiplyHessianDummy(double alpha, const VectorValues &x, VectorValues &y) const
Dummy version to measure overhead of key access.
FBlocks FBlocks_
All ZDim*D F blocks (one for each camera)
A set of cameras, all with their own calibration.
static SymmetricBlockMatrix SchurComplement(const std::vector< Eigen::Matrix< double, ZDim, ND >, Eigen::aligned_allocator< Eigen::Matrix< double, ZDim, ND >>> &Fs, const Matrix &E, const Eigen::Matrix< double, N, N > &P, const Vector &b)
static const KeyFormatter DefaultKeyFormatter
ptrdiff_t DenseIndex
The index type for Eigen objects.
void g(const string &key, int i)
std::pair< iterator, bool > tryInsert(Key j, const Vector &value)
void multiplyHessianAdd(double alpha, const VectorValues &x, VectorValues &y) const override
Hessian-vector multiply, i.e. y += F'alpha(I - E*P*E')*F*x.
double error(const VectorValues &x) const override
Error2s e1
Scratch space for multiplyHessianAdd.
std::pair< VectorValues::iterator, bool > emplace(Key j, Args &&... args)
RegularImplicitSchurFactor This
Typedef to this class.
RegularImplicitSchurFactor(const KeyVector &keys, const FBlocks &Fs, const Matrix &E, const Matrix &P, const Vector &b)
Construct from blocks of F, E, inv(E'*E), and RHS vector b.
RegularImplicitSchurFactor()
Constructor.
Eigen::Matrix< double, D, D > MatrixDD
camera Hessian
void projectError2(const Error2s &e1, Error2s &e2) const
Calculate corrected error Q*(e-ZDim*b) = (I - E*P*E')*(e-ZDim*b)
VectorValues hessianDiagonal() const
Return the diagonal of the Hessian for this factor.
const Matrix E_
The 2m*3 E Jacobian with respect to the point.
std::shared_ptr< This > shared_ptr
shared_ptr to this class
Eigen::SelfAdjointView< constBlock, Eigen::Upper > selfadjointView(DenseIndex I, DenseIndex J) const
Return the square sub-matrix that contains blocks(i:j, i:j).
const FBlocks & Fs() const
Eigen::Matrix< double, ZDim, D > MatrixZD
type of an F block
std::vector< Vector2, Eigen::aligned_allocator< Vector2 > > Error2s
GenericMeasurement< Point2, Point2 > Measurement
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
void projectError(const Error2s &e1, Error2s &e2) const
Calculate corrected error Q*e = (I - E*P*E')*e.
const Matrix PointCovariance_
the 3*3 matrix P = inv(E'E) (2*2 if degenerate)
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
std::shared_ptr< This > shared_ptr
shared_ptr to this class
bool equal_with_abs_tol(const Eigen::DenseBase< MATRIX > &A, const Eigen::DenseBase< MATRIX > &B, double tol=1e-9)
void gradientAtZero(double *d) const override
Matrix augmentedJacobian() const override
~RegularImplicitSchurFactor() override
Destructor.
Vector gradient(Key key, const VectorValues &x) const override
Gradient wrt a key at any values.
VectorValues gradientAtZero() const override
Matrix information() const override
Compute full information matrix
static const int ZDim
Measurement dimension.
std::vector< MatrixZD, Eigen::aligned_allocator< MatrixZD > > FBlocks
std::map< Key, Matrix > hessianBlockDiagonal() const override
Return the block diagonal of the Hessian for this factor.
const KeyVector & keys() const
Access the factor's involved variable keys.
bool equals(const GaussianFactor &lf, double tol) const override
equals
KeyVector::const_iterator const_iterator
Const iterator over keys.
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
The matrix class, also used for vectors and row-vectors.
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
const Vector b_
2m-dimensional RHS vector
DenseIndex getDim(const_iterator variable) const override
Degrees of freedom of camera.
std::uint64_t Key
Integer nonlinear key type.
Matrix augmentedInformation() const override
Compute full augmented information matrix
std::pair< Matrix, Vector > jacobian() const override