30#include <gtsam/base/concepts.h>
42#if GTSAM_ENABLE_BOOST_SERIALIZATION
43#include <boost/serialization/nvp.hpp>
52namespace serialization {
64template <
class CAMERA,
class LANDMARK>
67 typename traits<typename CAMERA::Measurement>::TangentVector, CAMERA,
69 GTSAM_CONCEPT_MANIFOLD_TYPE(CAMERA)
70 GTSAM_CONCEPT_MANIFOLD_TYPE(LANDMARK)
72 using Measurement =
typename CAMERA::Measurement;
74 static const int DimC = FixedDimension<CAMERA>::value;
75 static const int DimL = FixedDimension<LANDMARK>::value;
77 typedef Eigen::Matrix<double, ZDim, DimC> JacobianC;
78 typedef Eigen::Matrix<double, ZDim, DimL> JacobianL;
92 typedef std::shared_ptr<This> shared_ptr;
102 Key cameraKey,
Key landmarkKey)
108 typename M = Measurement,
109 typename std::enable_if<std::is_same<M, Point2>::value,
int>::type = 0>
113 typename M = Measurement,
114 typename std::enable_if<std::is_same<M, Point2>::value,
int>::type = 0>
120 gtsam::NonlinearFactor::shared_ptr
clone()
const override {
121 return std::static_pointer_cast<gtsam::NonlinearFactor>(
122 gtsam::NonlinearFactor::shared_ptr(
new This(*
this)));
131 const std::string& s =
"SFMFactor",
141 const This* e =
dynamic_cast<const This*
>(&p);
147 ErrorVector
evaluateError(
const CAMERA& camera,
const LANDMARK& point,
151 const Measurement predicted = camera.project2(point, H1, H2);
158 const ErrorVector
error =
160 if (H1) *H1 = Hlocal * *H1;
161 if (H2) *H2 = Hlocal * *H2;
167 if (H1) *H1 = JacobianC::Zero();
168 if (H2) *H2 = JacobianL::Zero();
170 return Vector::Zero(ZDim);
178 ErrorVector
evaluateError(
const CAMERA& camera,
const LANDMARK& point,
179 Eigen::Ref<Matrix> H1,
180 Eigen::Ref<Matrix> H2)
const {
182 const Measurement predicted = camera.project2(point, H1, H2);
188 const ErrorVector
error =
191 const JacobianC Dcamera = Hlocal * H1;
192 const JacobianL Dlandmark = Hlocal * H2;
201 return ErrorVector::Zero();
206 std::shared_ptr<GaussianFactor>
linearize(
207 const Values& values)
const override {
208 if (!this->
active(values))
return std::shared_ptr<JacobianFactor>();
210 const Key key1 = this->key1(), key2 = this->key2();
214 values.
at<CAMERA>(key1), values.
at<LANDMARK>(key2), Dcamera, Dlandmark);
218 throw std::invalid_argument(
219 "NoiseModelFactor: NoiseModel has dimension " +
220 std::to_string(
noiseModel->dim()) +
" instead of " +
221 std::to_string(ZDim) +
".");
225 Matrix dynamicCamera = Dcamera, dynamicLandmark = Dlandmark;
227 noiseModel->WhitenSystem(dynamicCamera, dynamicLandmark, dynamicB);
228 Dcamera = dynamicCamera;
229 Dlandmark = dynamicLandmark;
233 SharedDiagonal linearModel;
235 const auto constrained =
236 std::static_pointer_cast<noiseModel::Constrained>(
noiseModel);
237 linearModel = constrained->unit();
240 return std::make_shared<FixedJacobianFactor<ZDim, DimC, DimL>>(
241 KeyVector{key1, key2}, std::vector<Matrix>{Dcamera, Dlandmark}, b,
249#if GTSAM_ENABLE_BOOST_SERIALIZATION
251 friend class boost::serialization::access;
252 template <
class Archive>
253 void serialize(Archive& ar,
const unsigned int ) {
255 ar& boost::serialization::make_nvp(
256 "NoiseModelFactor2", boost::serialization::base_object<Base>(*
this));
262template <
class CAMERA,
class LANDMARK>
264 :
Testable<GeneralSFMFactor<CAMERA, LANDMARK>> {};
271template <
class CALIBRATION>
274 GTSAM_CONCEPT_MANIFOLD_TYPE(CALIBRATION)
275 static const int DimK = FixedDimension<CALIBRATION>::value;
287 typedef std::shared_ptr<This> shared_ptr;
298 Key poseKey,
Key landmarkKey,
Key calibKey)
305 gtsam::NonlinearFactor::shared_ptr
clone()
const override {
306 return std::static_pointer_cast<gtsam::NonlinearFactor>(
307 gtsam::NonlinearFactor::shared_ptr(
new This(*
this)));
316 const std::string& s =
"SFMFactor2",
326 const This* e =
dynamic_cast<const This*
>(&p);
337 Camera camera(pose3, calib);
340 if (H1) *H1 = Matrix::Zero(2, 6);
341 if (H2) *H2 = Matrix::Zero(2, 3);
342 if (H3) *H3 = Matrix::Zero(2, DimK);
343 std::cout << e.what() <<
": Landmark "
354#if GTSAM_ENABLE_BOOST_SERIALIZATION
356 friend class boost::serialization::access;
357 template <
class Archive>
358 void serialize(Archive& ar,
const unsigned int ) {
360 ar& boost::serialization::make_nvp(
361 "NoiseModelFactor3", boost::serialization::base_object<Base>(*
this));
367template <
class CALIBRATION>
369 :
Testable<GeneralSFMFactor2<CALIBRATION>> {};
Typedefs for easier changing of types.
typedef and functions to augment Eigen's MatrixXd
Macros for Vector constants to avoid excessive template instantiation.
Access to matrices via blocks of pre-defined sizes.
Concept check for values that can be used in unit tests.
Base class and basic functions for Manifold types.
typedef and functions to augment Eigen's VectorXd
Base class for all pinhole cameras.
3D Pose manifold SO(3) x R^3 and group SE(3)
Arbitrary-arity Jacobian factor with compile-time block dimensions.
Base class for noise model factors with N variables.
Non-linear factor base classes.
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
Matrix * OptionalMatrixType
This typedef will be used everywhere boost::optional<Matrix&> reference was used previously.
Definition NonlinearFactor.h:57
Vector3 Point3
As of GTSAM 4, in order to make GTSAM more lean, it is now possible to just typedef Point3 to Vector3...
Definition Point3.h:38
Vector2 Point2
As of GTSAM 4, in order to make GTSAM more lean, it is now possible to just typedef Point2 to Vector2...
Definition Point2.h:32
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
noiseModel::Base::shared_ptr SharedNoiseModel
Aliases.
Definition NoiseModel.h:846
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:43
All noise models live in the noiseModel namespace.
Definition LossFunctions.cpp:33
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition Group.h:37
Detect whether a traits type provides Local with Jacobians.
Definition Manifold.h:145
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:152
tag to assert a type is a vector space
Definition VectorSpace.h:21
Definition CalibratedCamera.h:35
A pinhole camera class that has a Pose3 and a Calibration.
Definition PinholeCamera.h:34
Point2 project(const Point3 &pw, OptionalJacobian< 2, 6 > Dpose={}, OptionalJacobian< 2, 3 > Dpoint={}, OptionalJacobian< 2, DimK > Dcal={}) const
project a 3D point from world coordinates into the image
Definition PinholePose.h:112
A 3D pose (R,t) : (Rot3,Point3).
Definition Pose3.h:42
virtual void print(const std::string &s="Factor", const KeyFormatter &formatter=DefaultKeyFormatter) const
print
Definition Factor.cpp:29
bool equals(const This &other, double tol=1e-9) const
check equality
Definition Factor.cpp:42
NoiseModelFactorT()
Definition NoiseModelFactorN.h:257
virtual ErrorVector evaluateError(const ValueTypes &... x, OptionalMatrixTypeT< ValueTypes >... H) const=0
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
double error(const Values &c) const override
Calculate the error of the factor.
Definition NonlinearFactor.cpp:146
A non-templated config holding any types of Manifold-group elements.
Definition Values.h:65
const ValueType at(Key j) const
Retrieve a variable by key j.
Definition Values-inl.h:260
Non-linear factor for a constraint derived from a 2D measurement.
Definition GeneralSFMFactor.h:68
const Measurement measured() const
Definition GeneralSFMFactor.h:245
Measurement measured_
Definition GeneralSFMFactor.h:81
gtsam::NonlinearFactor::shared_ptr clone() const override
Definition GeneralSFMFactor.h:119
bool equals(const NonlinearFactor &p, double tol=1e-9) const override
equals
Definition GeneralSFMFactor.h:139
GeneralSFMFactor()
default constructor
Definition GeneralSFMFactor.h:105
GeneralSFMFactor< CAMERA, LANDMARK > This
typedef for this object
Definition GeneralSFMFactor.h:84
NoiseModelFactorT< ErrorVector, CAMERA, LANDMARK > Base
typedef for the base class
Definition GeneralSFMFactor.h:86
ErrorVector evaluateError(const CAMERA &camera, const LANDMARK &point, OptionalMatrixType H1, OptionalMatrixType H2) const override
h(x)-z
Definition GeneralSFMFactor.h:146
~GeneralSFMFactor() override
destructor
Definition GeneralSFMFactor.h:116
std::shared_ptr< GaussianFactor > linearize(const Values &values) const override
Linearize using fixed-size Jacobians.
Definition GeneralSFMFactor.h:205
void print(const std::string &s="SFMFactor", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
print
Definition GeneralSFMFactor.h:129
GeneralSFMFactor(const Measurement &measured, const SharedNoiseModel &model, Key cameraKey, Key landmarkKey)
Constructor.
Definition GeneralSFMFactor.h:101
Non-linear factor for a constraint derived from a 2D measurement.
Definition GeneralSFMFactor.h:273
Vector2 evaluateError(const Pose3 &pose3, const Point3 &point, const CALIBRATION &calib, OptionalMatrixType H1, OptionalMatrixType H2, OptionalMatrixType H3) const override
h(x)-z
Definition GeneralSFMFactor.h:332
NoiseModelFactorT< Vector2, Pose3, Point3, CALIBRATION > Base
typedef for the base class
Definition GeneralSFMFactor.h:284
GeneralSFMFactor2()
default constructor
Definition GeneralSFMFactor.h:300
~GeneralSFMFactor2() override
destructor
Definition GeneralSFMFactor.h:302
gtsam::NonlinearFactor::shared_ptr clone() const override
Definition GeneralSFMFactor.h:305
void print(const std::string &s="SFMFactor2", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
print
Definition GeneralSFMFactor.h:315
Point2 measured_
Definition GeneralSFMFactor.h:278
const Point2 measured() const
Definition GeneralSFMFactor.h:351
PinholeCamera< CALIBRATION > Camera
typedef for camera type
Definition GeneralSFMFactor.h:282
GeneralSFMFactor2(const Point2 &measured, const SharedNoiseModel &model, Key poseKey, Key landmarkKey, Key calibKey)
Constructor.
Definition GeneralSFMFactor.h:297
bool equals(const NonlinearFactor &p, double tol=1e-9) const override
equals
Definition GeneralSFMFactor.h:325