25 #include <gtsam/inference/Symbol.h> 28 #include <boost/optional.hpp> 29 #include <boost/make_shared.hpp> 36 HESSIAN, IMPLICIT_SCHUR, JACOBIAN_Q, JACOBIAN_SVD
41 IGNORE_DEGENERACY, ZERO_ON_DEGENERACY, HANDLE_INFINITY
66 DegeneracyMode degMode = IGNORE_DEGENERACY,
bool throwCheirality =
false,
67 bool verboseCheirality =
false) :
68 linearizationMode(linMode), degeneracyMode(degMode), retriangulationThreshold(
69 1e-5), throwCheirality(throwCheirality), verboseCheirality(
76 void print(
const std::string& str)
const {
77 std::cout <<
"linearizationMode: " << linearizationMode <<
"\n";
78 std::cout <<
" degeneracyMode: " << degeneracyMode <<
"\n";
79 std::cout << triangulation << std::endl;
83 return linearizationMode;
86 return degeneracyMode;
91 bool getVerboseCheirality()
const {
92 return verboseCheirality;
94 bool getThrowCheirality()
const {
95 return throwCheirality;
98 linearizationMode = linMode;
101 degeneracyMode = degMode;
103 void setRankTolerance(
double rankTol) {
106 void setEnableEPI(
bool enableEPI) {
109 void setLandmarkDistanceThreshold(
double landmarkDistanceThreshold) {
112 void setDynamicOutlierRejectionThreshold(
double dynOutRejectionThreshold) {
119 friend class boost::serialization::access;
120 template<
class ARCHIVE>
121 void serialize(ARCHIVE & ar,
const unsigned int version) {
122 ar & BOOST_SERIALIZATION_NVP(linearizationMode);
123 ar & BOOST_SERIALIZATION_NVP(degeneracyMode);
124 ar & BOOST_SERIALIZATION_NVP(triangulation);
125 ar & BOOST_SERIALIZATION_NVP(retriangulationThreshold);
126 ar & BOOST_SERIALIZATION_NVP(throwCheirality);
127 ar & BOOST_SERIALIZATION_NVP(verboseCheirality);
140 template<
class CAMERA>
182 const boost::optional<Pose3> body_P_sensor = boost::none,
184 Base(sharedNoiseModel, body_P_sensor), params_(params),
198 DefaultKeyFormatter)
const {
199 std::cout << s <<
"SmartProjectionFactor\n";
202 std::cout <<
"triangulationParameters:\n" << params_.triangulation
204 std::cout <<
"result:\n" << result_ << std::endl;
210 const This *e =
dynamic_cast<const This*
>(&p);
212 && Base::equals(p, tol);
221 size_t m = cameras.size();
223 bool retriangulate =
false;
226 if (cameraPosesTriangulation_.empty()
227 || cameras.size() != cameraPosesTriangulation_.size())
228 retriangulate =
true;
230 if (!retriangulate) {
231 for (
size_t i = 0; i < cameras.size(); i++) {
232 if (!cameras[i].pose().
equals(cameraPosesTriangulation_[i],
234 retriangulate =
true;
241 cameraPosesTriangulation_.clear();
242 cameraPosesTriangulation_.reserve(m);
243 for (
size_t i = 0; i < m; i++)
245 cameraPosesTriangulation_.push_back(cameras[i].pose());
248 return retriangulate;
254 size_t m = cameras.size();
256 return TriangulationResult::Degenerate();
258 bool retriangulate = decideIfTriangulate(cameras);
261 params_.triangulation);
268 return bool(result_);
273 const Cameras& cameras,
const double lambda = 0.0,
bool diagonalDamping =
276 size_t numKeys = this->keys_.size();
279 std::vector<Matrix> Gs(numKeys * (numKeys + 1) / 2);
280 std::vector<Vector> gs(numKeys);
282 if (this->measured_.size() != cameras.size()) {
284 <<
"SmartProjectionHessianFactor: this->measured_.size() inconsistent with input" 294 m = Matrix::Zero(Base::Dim, Base::Dim);
296 v = Vector::Zero(Base::Dim);
297 return boost::make_shared<RegularHessianFactor<Base::Dim> >(this->keys_,
302 std::vector<typename Base::MatrixZD> Fblocks;
305 computeJacobiansWithTriangulatedPoint(Fblocks, E, b, cameras);
308 Base::whitenJacobians(Fblocks, E, b);
312 Cameras::SchurComplement(Fblocks, E, b, lambda, diagonalDamping);
314 return boost::make_shared<RegularHessianFactor<Base::Dim> >(this->keys_,
319 boost::shared_ptr<RegularImplicitSchurFactor<CAMERA> > createRegularImplicitSchurFactor(
320 const Cameras& cameras,
double lambda)
const {
321 if (triangulateForLinearize(cameras))
322 return Base::createRegularImplicitSchurFactor(cameras, *result_, lambda);
325 return boost::shared_ptr<RegularImplicitSchurFactor<CAMERA> >();
330 const Cameras& cameras,
double lambda)
const {
331 if (triangulateForLinearize(cameras))
332 return Base::createJacobianQFactor(cameras, *result_, lambda);
335 return boost::make_shared<JacobianFactorQ<Base::Dim, 2> >(this->keys_);
340 const Values& values,
double lambda)
const {
341 return createJacobianQFactor(this->cameras(values), lambda);
346 const Cameras& cameras,
double lambda)
const {
347 if (triangulateForLinearize(cameras))
348 return Base::createJacobianSVDFactor(cameras, *result_, lambda);
351 return boost::make_shared<JacobianFactorSVD<Base::Dim, 2> >(this->keys_);
356 const Values& values,
double lambda = 0.0)
const {
357 return createHessianFactor(this->cameras(values), lambda);
362 const Values& values,
double lambda = 0.0)
const {
363 return createRegularImplicitSchurFactor(this->cameras(values), lambda);
368 const Values& values,
double lambda = 0.0)
const {
369 return createJacobianQFactor(this->cameras(values), lambda);
378 const double lambda = 0.0)
const {
382 return createHessianFactor(cameras, lambda);
384 return createRegularImplicitSchurFactor(cameras, lambda);
386 return createJacobianSVDFactor(cameras, lambda);
388 return createJacobianQFactor(cameras, lambda);
390 throw std::runtime_error(
"SmartFactorlinearize: unknown mode");
400 const double lambda = 0.0)
const {
402 Cameras cameras = this->cameras(values);
403 return linearizeDamped(cameras, lambda);
408 const Values& values)
const {
409 return linearizeDamped(values);
417 bool nonDegenerate = triangulateForLinearize(cameras);
419 cameras.
project2(*result_, boost::none, E);
420 return nonDegenerate;
428 Cameras cameras = this->cameras(values);
429 return triangulateAndComputeE(E, cameras);
436 std::vector<typename Base::MatrixZD>& Fblocks, Matrix& E, Vector& b,
437 const Cameras& cameras)
const {
442 Unit3 backProjected = cameras[0].backprojectPointAtInfinity(
443 this->measured_.at(0));
444 Base::computeJacobians(Fblocks, E, b, cameras, backProjected);
447 Base::computeJacobians(Fblocks, E, b, cameras, *result_);
453 std::vector<typename Base::MatrixZD>& Fblocks, Matrix& E, Vector& b,
454 const Values& values)
const {
455 Cameras cameras = this->cameras(values);
456 bool nonDegenerate = triangulateForLinearize(cameras);
458 computeJacobiansWithTriangulatedPoint(Fblocks, E, b, cameras);
459 return nonDegenerate;
464 std::vector<typename Base::MatrixZD>& Fblocks, Matrix& Enull, Vector& b,
465 const Values& values)
const {
466 Cameras cameras = this->cameras(values);
467 bool nonDegenerate = triangulateForLinearize(cameras);
469 Base::computeJacobiansSVD(Fblocks, Enull, b, cameras, *result_);
470 return nonDegenerate;
475 Cameras cameras = this->cameras(values);
476 bool nonDegenerate = triangulateForLinearize(cameras);
478 return Base::unwhitenedError(cameras, *result_);
480 return Vector::Zero(cameras.size() * 2);
490 boost::optional<Point3> externalPoint = boost::none)
const {
499 return Base::totalReprojectionError(cameras, *result_);
502 Unit3 backprojected = cameras.front().backprojectPointAtInfinity(
503 this->measured_.at(0));
504 return Base::totalReprojectionError(cameras, backprojected);
512 if (this->active(values)) {
513 return totalReprojectionError(Base::cameras(values));
526 Cameras cameras = this->cameras(values);
537 return result_.degenerate();
542 return result_.behindCamera();
548 friend class boost::serialization::access;
549 template<
class ARCHIVE>
550 void serialize(ARCHIVE & ar,
const unsigned int version) {
551 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
552 ar & BOOST_SERIALIZATION_NVP(params_);
553 ar & BOOST_SERIALIZATION_NVP(result_);
554 ar & BOOST_SERIALIZATION_NVP(cameraPosesTriangulation_);
560 template<
class CAMERA>
562 SmartProjectionFactor<CAMERA> > {
double dynamicOutlierRejectionThreshold
If this is nonnegative the we will check if the average reprojection error is smaller than this thres...
Definition: triangulation.h:338
virtual bool equals(const NonlinearFactor &p, double tol=1e-9) const
equals
Definition: SmartProjectionFactor.h:209
TriangulationResult is an optional point, along with the reasons why it is invalid.
Definition: triangulation.h:384
TriangulationResult point(const Values &values) const
COMPUTE the landmark.
Definition: SmartProjectionFactor.h:525
bool triangulateForLinearize(const Cameras &cameras) const
triangulate
Definition: SmartProjectionFactor.h:266
Nonlinear factor base class.
Definition: NonlinearFactor.h:52
SmartProjectionFactor: triangulates point and keeps an estimate of it around.
Definition: SmartProjectionFactor.h:141
TriangulationResult result_
result from triangulateSafe
Definition: SmartProjectionFactor.h:159
Represents a 3D point on a unit sphere.
Definition: Unit3.h:42
boost::shared_ptr< RegularHessianFactor< Base::Dim > > createHessianFactor(const Cameras &cameras, const double lambda=0.0, bool diagonalDamping=false) const
linearize returns a Hessianfactor that is an approximation of error(p)
Definition: SmartProjectionFactor.h:272
This is the base class for all factor types.
Definition: Factor.h:51
double rankTolerance
threshold to decide whether triangulation is result.degenerate
Definition: triangulation.h:324
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition: Matrix.cpp:140
bool isValid() const
Is result valid?
Definition: SmartProjectionFactor.h:531
bool triangulateAndComputeJacobians(std::vector< typename Base::MatrixZD > &Fblocks, Matrix &E, Vector &b, const Values &values) const
Version that takes values, and creates the point.
Definition: SmartProjectionFactor.h:452
noiseModel::Base::shared_ptr SharedNoiseModel
Note, deliberately not in noiseModel namespace.
Definition: NoiseModel.h:1072
virtual boost::shared_ptr< RegularHessianFactor< Base::Dim > > linearizeToHessian(const Values &values, double lambda=0.0) const
linearize to a Hessianfactor
Definition: SmartProjectionFactor.h:355
DegeneracyMode
How to manage degeneracy.
Definition: SmartProjectionFactor.h:40
virtual double error(const Values &values) const
Calculate total reprojection error.
Definition: SmartProjectionFactor.h:511
bool throwCheirality
If true, re-throws Cheirality exceptions (default: false)
Definition: SmartProjectionFactor.h:60
bool verboseCheirality
If true, prints text for Cheirality exceptions (default: false)
Definition: SmartProjectionFactor.h:61
std::vector< Pose3 > cameraPosesTriangulation_
current triangulation poses
Definition: SmartProjectionFactor.h:160
boost::shared_ptr< GaussianFactor > linearizeDamped(const Values &values, const double lambda=0.0) const
Linearize to Gaussian Factor.
Definition: SmartProjectionFactor.h:399
SmartProjectionFactor()
Default constructor, only for serialization.
Definition: SmartProjectionFactor.h:174
Base class for smart factors This base class has no internal point, but it has a measurement, noise model and an optional sensor pose.
Definition: SmartFactorBase.h:47
A non-templated config holding any types of Manifold-group elements.
Definition: Values.h:70
Definition: SymmetricBlockMatrix.h:51
boost::shared_ptr< JacobianFactor > createJacobianSVDFactor(const Cameras &cameras, double lambda) const
different (faster) way to compute Jacobian factor
Definition: SmartProjectionFactor.h:345
bool enableEPI
if set to true, will refine triangulation using LM
Definition: triangulation.h:325
double retriangulationThreshold
threshold to decide whether to re-triangulate
Definition: SmartProjectionFactor.h:55
A helper that implements the traits interface for GTSAM types.
Definition: Testable.h:150
Base class to create smart factors on poses or cameras.
LinearizationMode linearizationMode
How to linearize the factor.
Definition: SmartProjectionFactor.h:49
LinearizationMode
Linearization mode: what factor to linearize to.
Definition: SmartProjectionFactor.h:35
Definition: triangulation.h:322
Functions for triangulation.
boost::shared_ptr< JacobianFactorQ< Base::Dim, 2 > > createJacobianQFactor(const Values &values, double lambda) const
Create a factor, takes values.
Definition: SmartProjectionFactor.h:339
virtual ~SmartProjectionFactor()
Virtual destructor.
Definition: SmartProjectionFactor.h:189
boost::shared_ptr< GaussianFactor > linearizeDamped(const Cameras &cameras, const double lambda=0.0) const
Linearize to Gaussian Factor.
Definition: SmartProjectionFactor.h:377
double landmarkDistanceThreshold
if the landmark is triangulated at distance larger than this, result is flagged as degenerate...
Definition: triangulation.h:331
TriangulationResult triangulateSafe(const std::vector< CAMERA > &cameras, const std::vector< Point2 > &measured, const TriangulationParameters ¶ms)
triangulateSafe: extensive checking of the outcome
Definition: triangulation.h:440
bool isPointBehindCamera() const
return the cheirality status flag
Definition: SmartProjectionFactor.h:541
bool triangulateAndComputeJacobiansSVD(std::vector< typename Base::MatrixZD > &Fblocks, Matrix &Enull, Vector &b, const Values &values) const
takes values
Definition: SmartProjectionFactor.h:463
virtual boost::shared_ptr< GaussianFactor > linearize(const Values &values) const
linearize
Definition: SmartProjectionFactor.h:407
A set of cameras, all with their own calibration.
Definition: CameraSet.h:34
boost::shared_ptr< JacobianFactorQ< Base::Dim, 2 > > createJacobianQFactor(const Cameras &cameras, double lambda) const
create factor
Definition: SmartProjectionFactor.h:329
bool decideIfTriangulate(const Cameras &cameras) const
Check if the new linearization point is the same as the one used for previous triangulation.
Definition: SmartProjectionFactor.h:216
bool isDegenerate() const
return the degenerate state
Definition: SmartProjectionFactor.h:536
CameraSet< CAMERA > Cameras
shorthand for a set of cameras
Definition: SmartProjectionFactor.h:169
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
print
Definition: SmartProjectionFactor.h:197
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
bool equals(const CameraSet &p, double tol=1e-9) const
equals
Definition: CameraSet.h:82
double totalReprojectionError(const Cameras &cameras, boost::optional< Point3 > externalPoint=boost::none) const
Calculate the error of the factor.
Definition: SmartProjectionFactor.h:489
utility functions for loading datasets
TriangulationResult triangulateSafe(const Cameras &cameras) const
triangulateSafe
Definition: SmartProjectionFactor.h:252
boost::shared_ptr< This > shared_ptr
shorthand for a smart pointer to a factor
Definition: SmartProjectionFactor.h:166
TriangulationResult point() const
return the landmark
Definition: SmartProjectionFactor.h:520
SmartProjectionFactor(const SharedNoiseModel &sharedNoiseModel, const boost::optional< Pose3 > body_P_sensor=boost::none, const SmartProjectionParams ¶ms=SmartProjectionParams())
Constructor.
Definition: SmartProjectionFactor.h:181
std::vector< Z > project2(const POINT &point, boost::optional< FBlocks & > Fs=boost::none, boost::optional< Matrix & > E=boost::none) const
Project a point (possibly Unit3 at infinity), with derivatives Note that F is a sparse block-diagonal...
Definition: CameraSet.h:101
Definition: SmartProjectionFactor.h:47
virtual boost::shared_ptr< JacobianFactorQ< Base::Dim, 2 > > linearizeToJacobian(const Values &values, double lambda=0.0) const
linearize to a JacobianfactorQ
Definition: SmartProjectionFactor.h:367
void computeJacobiansWithTriangulatedPoint(std::vector< typename Base::MatrixZD > &Fblocks, Matrix &E, Vector &b, const Cameras &cameras) const
Compute F, E only (called below in both vanilla and SVD versions) Assumes the point has been computed...
Definition: SmartProjectionFactor.h:435
Vector reprojectionErrorAfterTriangulation(const Values &values) const
Calculate vector of re-projection errors, before applying noise model.
Definition: SmartProjectionFactor.h:474
bool triangulateAndComputeE(Matrix &E, const Values &values) const
Triangulate and compute derivative of error with respect to point.
Definition: SmartProjectionFactor.h:427
bool triangulateAndComputeE(Matrix &E, const Cameras &cameras) const
Triangulate and compute derivative of error with respect to point.
Definition: SmartProjectionFactor.h:416
virtual boost::shared_ptr< RegularImplicitSchurFactor< CAMERA > > linearizeToImplicit(const Values &values, double lambda=0.0) const
linearize to an Implicit Schur factor
Definition: SmartProjectionFactor.h:361
DegeneracyMode degeneracyMode
How to linearize the factor.
Definition: SmartProjectionFactor.h:50
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
boost::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: Key.h:33