37template <
class CAMERA>
52 mutable std::vector<Pose3, Eigen::aligned_allocator<Pose3>>
67 using SharedHessianFactor = std::shared_ptr<HessianFactorType>;
68 using SharedJacobianFactor = std::shared_ptr<JacobianFactorType>;
84 : Base(sharedNoiseModel),
97 const std::string& s =
"",
99 std::cout << s <<
"SmartProjectionFactor\n";
100 std::cout <<
"linearizationMode: " << params_.linearizationMode
102 std::cout <<
"triangulationParameters:\n"
103 << params_.triangulation << std::endl;
104 std::cout <<
"result:\n" <<
result_ << std::endl;
110 const This* e =
dynamic_cast<const This*
>(&p);
131 bool retriangulate =
false;
137 retriangulate =
true;
140 if (!retriangulate) {
141 for (
size_t i = 0; i <
cameras.size(); i++) {
143 params_.retriangulationThreshold)) {
155 for (
size_t i = 0; i < m; i++)
160 return retriangulate;
173 return TriangulationResult::Degenerate();
178 params_.triangulation);
195 const Cameras&
cameras,
const double _lambda = 0.0,
196 bool diagonalDamping =
false)
const {
197 size_t numKeys = this->
keys_.size();
200 std::vector<Matrix> Gs(numKeys * (numKeys + 1) / 2);
201 std::vector<Vector> gs(numKeys);
204 throw std::runtime_error(
205 "SmartProjectionHessianFactor: this->measured_"
206 ".size() inconsistent with input");
210 if (params_.degeneracyMode == ZERO_ON_DEGENERACY && !
result_) {
213 for (Vector& v : gs) v = Vector::Zero(
Base::Dim);
214 return std::make_shared<RegularHessianFactor<Base::Dim>>(this->
keys_, Gs,
219 typename Base::FBlocks Fs;
231 return std::make_shared<RegularHessianFactor<Base::Dim>>(this->
keys_,
236 std::shared_ptr<RegularImplicitSchurFactor<CAMERA>>
238 double _lambda)
const {
243 return std::shared_ptr<RegularImplicitSchurFactor<CAMERA>>();
248 const Cameras&
cameras,
double _lambda)
const {
253 return std::make_shared<JacobianFactorQ<Base::Dim, 2>>(this->
keys_);
258 const Values& values,
double _lambda)
const {
264 const Cameras&
cameras,
double _lambda)
const {
269 return std::make_shared<JacobianFactorSVD<Base::Dim, 2>>(this->
keys_);
274 const Values& values,
double _lambda = 0.0)
const {
279 virtual std::shared_ptr<RegularImplicitSchurFactor<CAMERA>>
281 return createRegularImplicitSchurFactor(this->
cameras(values), _lambda);
286 const Values& values,
double _lambda = 0.0)
const {
297 const Cameras&
cameras,
const double _lambda = 0.0)
const {
300 switch (params_.linearizationMode) {
304 return createRegularImplicitSchurFactor(
cameras, _lambda);
310 throw std::runtime_error(
"SmartFactorlinearize: unknown mode");
321 const Values& values,
const double _lambda = 0.0)
const {
330 const Values& values)
const override {
343 return nonDegenerate;
359 Matrix& E, Vector& b,
360 const Cameras&
cameras)
const {
364 Unit3 backProjected =
375 Vector& b,
const Values& values)
const {
379 return nonDegenerate;
384 Matrix& Enull, Vector& b,
385 const Values& values)
const {
390 return nonDegenerate;
400 return Vector::Zero(
cameras.size() * 2);
411 const Cameras&
cameras, std::optional<Point3> externalPoint = {})
const {
422 Unit3 backprojected =
432 if (this->
active(values)) {
464#if GTSAM_ENABLE_BOOST_SERIALIZATION
466 friend class boost::serialization::access;
467 template <
class ARCHIVE>
468 void serialize(ARCHIVE& ar,
const unsigned int ) {
469 ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
470 ar& BOOST_SERIALIZATION_NVP(params_);
471 ar& BOOST_SERIALIZATION_NVP(
result_);
478template <
class CAMERA>
480 :
public Testable<SmartProjectionFactorBase<CAMERA>> {};
Functions for triangulation.
Base class to create smart factors on poses or cameras.
Collect common parameters for SmartProjection and SmartStereoProjection factors.
Global functions in a separate testing namespace.
Definition chartTesting.h:28
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition Key.cpp:30
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition Key.h:91
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition Key.h:35
TriangulationResult triangulateSafe(const CameraSet< CAMERA > &cameras, const typename CAMERA::MeasurementVector &measured, const TriangulationParameters ¶ms)
triangulateSafe: extensive checking of the outcome
Definition triangulation.h:706
noiseModel::Base::shared_ptr SharedNoiseModel
Aliases.
Definition NoiseModel.h:846
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition Group.h:37
This class stores a dense matrix and allows it to be accessed as a collection of blocks.
Definition SymmetricBlockMatrix.h:80
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:152
A set of cameras, all with their own calibration.
Definition CameraSet.h:37
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)
Do Schur complement, given Jacobian as Fs,E,P, return SymmetricBlockMatrix G = F' * F - F' * E * P * ...
Definition CameraSet.h:175
TriangulationResult is an optional point, along with the reasons why it is invalid.
Definition triangulation.h:646
Represents a 3D point on a unit sphere.
Definition Unit3.h:44
KeyVector keys_
The keys involved in this factor.
Definition Factor.h:88
A HessianFactor where all variables have the same dimension D.
Definition RegularHessianFactor.h:57
Nonlinear factor base class.
Definition NonlinearFactor.h:70
virtual bool active(const Values &c) const
Checks whether a factor should be used based on a set of values.
Definition NonlinearFactor.h:143
A non-templated config holding any types of Manifold-group elements.
Definition Values.h:65
JacobianFactor for Schur complement that uses Q noise model.
Definition JacobianFactorQ.h:27
void computeJacobians(FBlocks &Fs, Matrix &E, Vector &b, const Cameras &cameras, const POINT &point) const
Compute F, E, and b (called below in both vanilla and SVD versions), where F is a vector of derivativ...
Definition SmartFactorBase.h:318
std::shared_ptr< JacobianFactor > createJacobianSVDFactor(const Cameras &cameras, const Point3 &point, double lambda=0.0) const
Return Jacobians as JacobianFactorSVD.
Definition SmartFactorBase.h:419
std::shared_ptr< RegularImplicitSchurFactor< CAMERA > > createRegularImplicitSchurFactor(const Cameras &cameras, const Point3 &point, double lambda=0.0, bool diagonalDamping=false) const
Return Jacobians as RegularImplicitSchurFactor with raw access.
Definition SmartFactorBase.h:389
static const int Dim
Camera dimension.
Definition SmartFactorBase.h:61
std::shared_ptr< JacobianFactorQ< Dim, ZDim > > createJacobianQFactor(const Cameras &cameras, const Point3 &point, double lambda=0.0, bool diagonalDamping=false) const
Return Jacobians as JacobianFactorQ.
Definition SmartFactorBase.h:402
virtual Cameras cameras(const Values &values) const
Collect all cameras: important that in key order.
Definition SmartFactorBase.h:165
Vector unwhitenedError(const Cameras &cameras, const POINT &point, typename Cameras::FBlocks *Fs=nullptr, Matrix *E=nullptr) const
Compute reprojection errors [h(x)-z] = [cameras.project(p)-z] and derivatives.
Definition SmartFactorBase.h:211
double totalReprojectionError(const Cameras &cameras, const POINT &point) const
Calculate the error of the factor.
Definition SmartFactorBase.h:300
void computeJacobiansSVD(FBlocks &Fs, Matrix &Enull, Vector &b, const Cameras &cameras, const POINT &point) const
SVD version that produces smaller Jacobian matrices by doing an SVD decomposition on E,...
Definition SmartFactorBase.h:333
ZVector measured_
Measurements for each of the m views.
Definition SmartFactorBase.h:80
SmartFactorBase()
Default Constructor, for serialization.
Definition SmartFactorBase.h:96
void whitenJacobians(FBlocks &F, Matrix &E, Vector &b) const
Whiten the Jacobians computed by computeJacobians using noiseModel_.
Definition SmartFactorBase.h:380
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
print
Definition SmartFactorBase.h:178
CameraSet< CAMERA > Cameras
The CameraSet data structure is used to refer to a set of cameras.
Definition SmartFactorBase.h:93
bool equals(const NonlinearFactor &p, double tol=1e-9) const override
equals
Definition SmartFactorBase.h:191
Definition SmartFactorParams.h:42
LinearizationMode linearizationMode
How to linearize the factor.
Definition SmartFactorParams.h:44
DegeneracyMode degeneracyMode
How to linearize the factor.
Definition SmartFactorParams.h:45
Common base for monocular smart projection factors.
Definition SmartProjectionFactorBase.h:38
double totalReprojectionError(const Cameras &cameras, std::optional< Point3 > externalPoint={}) const
Calculate the error of the factor.
Definition SmartProjectionFactorBase.h:410
std::shared_ptr< JacobianFactor > createJacobianSVDFactor(const Cameras &cameras, double _lambda) const
Different (faster) way to compute a JacobianFactorSVD factor.
Definition SmartProjectionFactorBase.h:263
bool triangulateForLinearize(const Cameras &cameras) const
Possibly re-triangulate before calculating Jacobians.
Definition SmartProjectionFactorBase.h:188
bool isOutlier() const
return the outlier state
Definition SmartProjectionFactorBase.h:458
std::shared_ptr< GaussianFactor > linearize(const Values &values) const override
linearize
Definition SmartProjectionFactorBase.h:329
std::shared_ptr< JacobianFactorQ< Base::Dim, 2 > > createJacobianQFactor(const Cameras &cameras, double _lambda) const
Create JacobianFactorQ factor.
Definition SmartProjectionFactorBase.h:247
bool decideIfTriangulate(const Cameras &cameras) const
Check if the new linearization point is the same as the one used for previous triangulation.
Definition SmartProjectionFactorBase.h:122
bool equals(const NonlinearFactor &p, double tol=1e-9) const override
equals
Definition SmartProjectionFactorBase.h:109
std::shared_ptr< RegularHessianFactor< Base::Dim > > createHessianFactor(const Cameras &cameras, const double _lambda=0.0, bool diagonalDamping=false) const
Create a Hessianfactor that is an approximation of error(p).
Definition SmartProjectionFactorBase.h:194
RegularHessianFactor< Base::Dim > HessianFactorType
Exact linear-factor types produced by this camera specialization.
Definition SmartProjectionFactorBase.h:65
~SmartProjectionFactorBase() override
Virtual destructor.
Definition SmartProjectionFactorBase.h:89
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
print
Definition SmartProjectionFactorBase.h:96
std::shared_ptr< GaussianFactor > linearizeDamped(const Cameras &cameras, const double _lambda=0.0) const
Linearize to Gaussian Factor.
Definition SmartProjectionFactorBase.h:296
CAMERA Camera
Shorthand for a set of cameras.
Definition SmartProjectionFactorBase.h:61
std::shared_ptr< This > shared_ptr
Shorthand for a smart pointer to a factor.
Definition SmartProjectionFactorBase.h:58
bool triangulateAndComputeE(Matrix &E, const Values &values) const
Triangulate and compute derivative of error with respect to point.
Definition SmartProjectionFactorBase.h:350
SmartProjectionFactorBase()
Default constructor, only for serialization.
Definition SmartProjectionFactorBase.h:73
virtual std::shared_ptr< RegularHessianFactor< Base::Dim > > linearizeToHessian(const Values &values, double _lambda=0.0) const
Linearize to a Hessianfactor.
Definition SmartProjectionFactorBase.h:273
TriangulationResult triangulateSafe(const Cameras &cameras) const
Call gtsam::triangulateSafe iff we need to re-triangulate.
Definition SmartProjectionFactorBase.h:169
SmartProjectionFactorBase(const SharedNoiseModel &sharedNoiseModel, const SmartProjectionParams ¶ms=SmartProjectionParams())
Constructor.
Definition SmartProjectionFactorBase.h:81
bool triangulateAndComputeJacobians(typename Base::FBlocks &Fs, Matrix &E, Vector &b, const Values &values) const
Version that takes values, and creates the point.
Definition SmartProjectionFactorBase.h:374
std::shared_ptr< JacobianFactorQ< Base::Dim, 2 > > createJacobianQFactor(const Values &values, double _lambda) const
Create JacobianFactorQ factor, takes values.
Definition SmartProjectionFactorBase.h:257
std::shared_ptr< GaussianFactor > linearizeDamped(const Values &values, const double _lambda=0.0) const
Linearize to Gaussian Factor.
Definition SmartProjectionFactorBase.h:320
virtual std::shared_ptr< RegularImplicitSchurFactor< CAMERA > > linearizeToImplicit(const Values &values, double _lambda=0.0) const
Linearize to an Implicit Schur factor.
Definition SmartProjectionFactorBase.h:280
std::vector< Pose3, Eigen::aligned_allocator< Pose3 > > cameraPosesTriangulation_
Definition SmartProjectionFactorBase.h:53
bool isPointBehindCamera() const
return the cheirality status flag
Definition SmartProjectionFactorBase.h:455
bool triangulateAndComputeE(Matrix &E, const Cameras &cameras) const
Triangulate and compute derivative of error with respect to point.
Definition SmartProjectionFactorBase.h:338
bool isFarPoint() const
return the farPoint state
Definition SmartProjectionFactorBase.h:461
Vector reprojectionErrorAfterTriangulation(const Values &values) const
Calculate vector of re-projection errors, before applying noise model.
Definition SmartProjectionFactorBase.h:394
bool isDegenerate() const
return the degenerate state
Definition SmartProjectionFactorBase.h:452
virtual std::shared_ptr< JacobianFactorQ< Base::Dim, 2 > > linearizeToJacobian(const Values &values, double _lambda=0.0) const
Linearize to a JacobianfactorQ.
Definition SmartProjectionFactorBase.h:285
TriangulationResult result_
Definition SmartProjectionFactorBase.h:51
bool isValid() const
Is result valid?
Definition SmartProjectionFactorBase.h:449
TriangulationResult point() const
return the landmark
Definition SmartProjectionFactorBase.h:440
bool triangulateAndComputeJacobiansSVD(typename Base::FBlocks &Fs, Matrix &Enull, Vector &b, const Values &values) const
takes values
Definition SmartProjectionFactorBase.h:383
TriangulationResult point(const Values &values) const
COMPUTE the landmark.
Definition SmartProjectionFactorBase.h:443
double error(const Values &values) const override
Calculate total reprojection error.
Definition SmartProjectionFactorBase.h:431
void computeJacobiansWithTriangulatedPoint(typename Base::FBlocks &Fs, 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 SmartProjectionFactorBase.h:358