13 #include <boost/foreach.hpp>
14 #include <boost/make_shared.hpp>
32 typedef Eigen::Matrix<double, 2, 3> Matrix23;
49 const Matrix3& P,
const Vector& b) :
58 keys_.push_back(it.first);
67 inline std::vector<KeyMatrix2D>& Fblocks() {
71 inline Matrix3& PointCovariance() {
89 void print(
const std::string& s =
"",
90 const KeyFormatter& keyFormatter = DefaultKeyFormatter)
const {
91 std::cout <<
" ImplicitSchurFactor " << std::endl;
94 std::cout <<
" E_ \n" <<
E_ << std::endl;
95 std::cout <<
" b_ \n" <<
b_.transpose() << std::endl;
100 if (!dynamic_cast<const ImplicitSchurFactor*>(&lf))
113 throw std::runtime_error(
114 "ImplicitSchurFactor::augmentedJacobian non implemented");
117 virtual std::pair<Matrix, Vector>
jacobian()
const {
118 throw std::runtime_error(
"ImplicitSchurFactor::jacobian non implemented");
119 return std::make_pair(Matrix(), Vector());
122 throw std::runtime_error(
123 "ImplicitSchurFactor::augmentedInformation non implemented");
127 throw std::runtime_error(
128 "ImplicitSchurFactor::information non implemented");
137 for (
size_t pos = 0; pos <
size(); ++pos) {
143 Eigen::Matrix<double, D, 3> FtE = Fj.transpose()
144 *
E_.block<2, 3>(2 * pos, 0);
146 Eigen::Matrix<double, D, 1> dj;
147 for (
size_t k = 0; k < D; ++k) {
149 dj(k) = Fj.col(k).squaredNorm();
166 typedef Eigen::Matrix<double, D, 1> DVector;
167 typedef Eigen::Map<DVector> DMap;
169 for (
size_t pos = 0; pos <
size(); ++pos) {
175 Eigen::Matrix<double, D, 3> FtE = Fj.transpose()
176 *
E_.block<2, 3>(2 * pos, 0);
179 for (
size_t k = 0; k < D; ++k) {
180 dj(k) = Fj.col(k).squaredNorm();
184 DMap(d + D * j) += dj;
190 std::map<Key, Matrix> blocks;
192 for (
size_t pos = 0; pos <
size(); ++pos) {
201 const Matrix23& Ej =
E_.block<2, 3>(2 * pos, 0);
202 blocks[j] = Fj.transpose() * (Fj - Ej *
PointCovariance_ * Ej.transpose() * Fj);
214 return boost::make_shared<ImplicitSchurFactor<D> >(
Fblocks_,
216 throw std::runtime_error(
"ImplicitSchurFactor::clone non implemented");
223 return boost::make_shared<ImplicitSchurFactor<D> >(
Fblocks_,
225 throw std::runtime_error(
"ImplicitSchurFactor::negate non implemented");
230 void multiplyHessianAdd(
const Matrix& F,
const Matrix& E,
231 const Matrix& PointCovariance,
double alpha,
const Vector& x, Vector& y) {
233 Vector d1 = E.transpose() *
e1;
234 Vector d2 = PointCovariance * d1;
236 Vector e3 = alpha * (e1 - e2);
237 y += F.transpose() * e3;
240 typedef std::vector<Vector2> Error2s;
250 for (
size_t k = 0; k <
size(); k++)
251 d1 +=
E_.block < 2, 3 > (2 * k, 0).transpose() * (e1[k] - 2 *
b_.segment < 2 > (k * 2));
257 for (
size_t k = 0; k <
size(); k++)
258 e2[k] = e1[k] - 2 *
b_.segment < 2 > (k * 2) -
E_.block < 2, 3 > (2 * k, 0) * d2;
277 for (
size_t k = 0; k <
size(); ++k)
282 for (
size_t k = 0; k <
size(); ++k)
283 result +=
dot(e1[k], e2[k]);
285 double f =
b_.squaredNorm();
286 return 0.5 * (result + f);
299 for (
size_t k = 0; k <
size(); ++k)
304 for (
size_t k = 0; k <
size(); ++k)
305 result +=
dot(e2[k], e2[k]);
318 for (
size_t k = 0; k <
size(); k++)
319 d1 +=
E_.block < 2, 3 > (2 * k, 0).transpose() * e1[k];
325 for (
size_t k = 0; k <
size(); k++)
326 e2[k] = e1[k] -
E_.block < 2, 3 > (2 * k, 0) * d2;
330 mutable Error2s
e1, e2;
339 typedef Eigen::Matrix<double, D, 1> DVector;
340 typedef Eigen::Map<DVector> DMap;
341 typedef Eigen::Map<const DVector> ConstDMap;
351 e1[k++] = it.second * ConstDMap(x + D * key);
360 DMap(y + D * key) += it.second.transpose() * alpha * e2[k++];
365 std::vector<size_t>
keys)
const {
380 for (
size_t k = 0; k <
size(); ++k)
386 for (
size_t k = 0; k <
size(); ++k) {
388 static const Vector
empty;
389 std::pair<VectorValues::iterator, bool> it = y.
tryInsert(key, empty);
390 Vector& yi = it.first->second;
393 yi = Vector::Zero(
Fblocks_[k].second.cols());
394 yi +=
Fblocks_[k].second.transpose() * alpha * e2[k];
405 static const Vector
empty;
407 std::pair<VectorValues::iterator, bool> it = y.
tryInsert(key, empty);
408 Vector& yi = it.first->second;
420 for (
size_t k = 0; k <
size(); k++)
421 e1[k] =
b_.segment < 2 > (2 * k);
426 for (
size_t k = 0; k <
size(); ++k) {
441 typedef Eigen::Matrix<double, D, 1> DVector;
442 typedef Eigen::Map<DVector> DMap;
447 for (
size_t k = 0; k <
size(); k++)
448 e1[k] =
b_.segment < 2 > (2 * k);
451 for (
size_t k = 0; k <
size(); ++k) {
453 DMap(d + D * j) += -
Fblocks_[k].second.transpose() * e2[k];
Vector b_
2m-dimensional RHS vector
Definition: ImplicitSchurFactor.h:39
void hessianDiagonal(double *d) const
add the contribution of this factor to the diagonal of the hessian d(output) = d(input) + deltaHessia...
Definition: ImplicitSchurFactor.h:163
virtual std::pair< Matrix, Vector > jacobian() const
Return the dense Jacobian and right-hand-side , with the noise models baked into A and b...
Definition: ImplicitSchurFactor.h:117
Vector & at(Key j)
Read/write access to the vector value with key j, throws std::out_of_range if j does not exist...
Definition: VectorValues.h:147
boost::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition: ImplicitSchurFactor.h:27
virtual Matrix augmentedInformation() const
Return the augmented information matrix represented by this GaussianFactor.
Definition: ImplicitSchurFactor.h:121
double dot(const V1 &a, const V2 &b)
Dot product.
Definition: Vector.h:259
void projectError(const Error2s &e1, Error2s &e2) const
Calculate corrected error Q*e = (I - E*P*E')*e.
Definition: ImplicitSchurFactor.h:313
std::vector< KeyMatrix2D > Fblocks_
All 2*D F blocks (one for each camera)
Definition: ImplicitSchurFactor.h:36
virtual VectorValues hessianDiagonal() const
Return the diagonal of the Hessian for this factor.
Definition: ImplicitSchurFactor.h:133
ImplicitSchurFactor(const std::vector< KeyMatrix2D > &Fblocks, const Matrix &E, const Matrix3 &P, const Vector &b)
Construct from blcoks of F, E, inv(E'*E), and RHS vector b.
Definition: ImplicitSchurFactor.h:48
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
print
Definition: ImplicitSchurFactor.h:89
void initKeys()
initialize keys from Fblocks
Definition: ImplicitSchurFactor.h:55
Matrix E_
The 2m*3 E Jacobian with respect to the point.
Definition: ImplicitSchurFactor.h:38
std::pair< Key, Matrix2D > KeyMatrix2D
named F block
Definition: ImplicitSchurFactor.h:34
Eigen::Matrix< double, D, D > MatrixDD
camera hessian
Definition: ImplicitSchurFactor.h:33
ImplicitSchurFactor.
Definition: ImplicitSchurFactor.h:23
const Matrix3 & getPointCovariance() const
Get matrix P.
Definition: ImplicitSchurFactor.h:84
ImplicitSchurFactor This
Typedef to this class.
Definition: ImplicitSchurFactor.h:26
FastVector< Key > keys_
The keys involved in this factor.
Definition: Factor.h:69
bool equals(const GaussianFactor &lf, double tol) const
equals
Definition: ImplicitSchurFactor.h:99
const FastVector< Key > & keys() const
Access the factor's involved variable keys.
Definition: Factor.h:115
Error2s e1
Scratch space for multiplyHessianAdd.
Definition: ImplicitSchurFactor.h:330
void print(const std::string &s="Factor", const KeyFormatter &formatter=DefaultKeyFormatter) const
print
Definition: Factor.cpp:30
virtual double error(const VectorValues &x) const
Print for testable.
Definition: ImplicitSchurFactor.h:270
void multiplyHessianAdd(double alpha, const double *x, double *y) const
double* Hessian-vector multiply, i.e.
Definition: ImplicitSchurFactor.h:336
void multiplyHessianAdd(double alpha, const VectorValues &x, VectorValues &y) const
Hessian-vector multiply, i.e.
Definition: ImplicitSchurFactor.h:372
virtual std::map< Key, Matrix > hessianBlockDiagonal() const
Return the block diagonal of the Hessian for this factor.
Definition: ImplicitSchurFactor.h:189
Eigen::Matrix< double, 2, D > Matrix2D
type of an F block
Definition: ImplicitSchurFactor.h:31
virtual DenseIndex getDim(const_iterator variable) const
Degrees of freedom of camera.
Definition: ImplicitSchurFactor.h:108
virtual bool empty() const
Test whether the factor is empty.
Definition: ImplicitSchurFactor.h:218
size_t Key
Integer nonlinear key type.
Definition: types.h:59
ImplicitSchurFactor()
Constructor.
Definition: ImplicitSchurFactor.h:44
ptrdiff_t DenseIndex
The index type for Eigen objects.
Definition: types.h:74
size_t size() const
Definition: Factor.h:126
boost::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition: GaussianFactor.h:39
void gradientAtZero(double *d) const
Calculate gradient, which is -F'Q*b, see paper - RAW MEMORY ACCESS.
Definition: ImplicitSchurFactor.h:438
virtual Matrix augmentedJacobian() const
Return a dense Jacobian matrix, augmented with b with the noise models baked into A and b...
Definition: ImplicitSchurFactor.h:112
This class represents a collection of vector-valued variables associated each with a unique integer i...
Definition: VectorValues.h:89
void multiplyHessianDummy(double alpha, const VectorValues &x, VectorValues &y) const
Dummy version to measure overhead of key access.
Definition: ImplicitSchurFactor.h:401
virtual ~ImplicitSchurFactor()
Destructor.
Definition: ImplicitSchurFactor.h:62
virtual GaussianFactor::shared_ptr clone() const
Clone a factor (make a deep copy)
Definition: ImplicitSchurFactor.h:213
iterator insert(Key j, const Vector &value)
Insert a vector value with key j.
Definition: VectorValues.h:183
VectorValues gradientAtZero() const
Calculate gradient, which is -F'Q*b, see paper.
Definition: ImplicitSchurFactor.h:416
void projectError2(const Error2s &e1, Error2s &e2) const
Calculate corrected error Q*(e-2*b) = (I - E*P*E')*(e-2*b)
Definition: ImplicitSchurFactor.h:245
void multiplyHessianAdd(double alpha, const double *x, double *y, std::vector< size_t > keys) const
y += alpha * A'*A*x
Definition: ImplicitSchurFactor.h:364
virtual GaussianFactor::shared_ptr negate() const
Construct the corresponding anti-factor to negate information stored stored in this factor...
Definition: ImplicitSchurFactor.h:222
std::pair< iterator, bool > tryInsert(Key j, const Vector &value)
insert that mimics the STL map insert - if the value already exists, the map is not modified and an i...
Definition: VectorValues.h:210
boost::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: types.h:62
Linear Factor Graph where all factors are Gaussians.
FastVector< Key >::const_iterator const_iterator
Const iterator over keys.
Definition: Factor.h:64
Matrix3 PointCovariance_
the 3*3 matrix P = inv(E'E) (2*2 if degenerate)
Definition: ImplicitSchurFactor.h:37
virtual Matrix information() const
Return the non-augmented information matrix represented by this GaussianFactor.
Definition: ImplicitSchurFactor.h:126
An abstract virtual base class for JacobianFactor and HessianFactor.
Definition: GaussianFactor.h:35