23#include <gtsam_unstable/dllexport.h>
44template <
class CAMERA>
50 typedef typename CAMERA::CalibrationType CALIBRATION;
51 typedef typename CAMERA::Measurement MEASUREMENT;
52 typedef typename CAMERA::MeasurementVector MEASUREMENTS;
72 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
79 typedef Eigen::Matrix<double, ZDim, DimBlock>
81 typedef std::vector<MatrixZD, Eigen::aligned_allocator<MatrixZD>>
84 typedef CAMERA Camera;
102 const boost::shared_ptr<Cameras>&
cameraRig,
106 if (Base::params_.degeneracyMode != gtsam::ZERO_ON_DEGENERACY)
107 throw std::runtime_error(
108 "SmartProjectionRigFactor: "
109 "degeneracyMode must be set to ZERO_ON_DEGENERACY");
110 if (Base::params_.linearizationMode != gtsam::HESSIAN)
111 throw std::runtime_error(
112 "SmartProjectionRigFactor: "
113 "linearizationMode must be set to HESSIAN");
132 const Key& world_P_body_key2,
const double& alpha,
133 const size_t& cameraId = 0) {
139 std::make_pair(world_P_body_key1, world_P_body_key2));
143 if (std::find(this->
keys_.begin(), this->keys_.end(), world_P_body_key1) ==
145 this->
keys_.push_back(world_P_body_key1);
146 if (std::find(this->
keys_.begin(), this->keys_.end(), world_P_body_key2) ==
148 this->
keys_.push_back(world_P_body_key2);
170 void add(
const MEASUREMENTS& measurements,
172 const std::vector<double>&
alphas,
178 throw std::runtime_error(
179 "SmartProjectionPoseFactorRollingShutter: "
180 "trying to add inconsistent inputs");
183 throw std::runtime_error(
184 "SmartProjectionPoseFactorRollingShutter: "
185 "camera rig includes multiple camera "
186 "but add did not input cameraIds");
188 for (
size_t i = 0; i < measurements.size(); i++) {
218 const std::string& s =
"",
219 const KeyFormatter& keyFormatter = DefaultKeyFormatter)
const override {
220 std::cout << s <<
"SmartProjectionPoseFactorRollingShutter: \n ";
221 for (
size_t i = 0; i <
cameraIds_.size(); i++) {
222 std::cout <<
"-- Measurement nr " << i << std::endl;
223 std::cout <<
" pose1 key: "
225 std::cout <<
" pose2 key: "
227 std::cout <<
" alpha: " <<
alphas_[i] << std::endl;
228 std::cout <<
"cameraId: " <<
cameraIds_[i] << std::endl;
229 (*cameraRig_)[
cameraIds_[i]].print(
"camera in rig:\n");
240 double keyPairsEqual =
true;
241 if (this->world_P_body_key_pairs_.size() ==
243 for (
size_t k = 0; k < this->world_P_body_key_pairs_.size(); k++) {
248 if (!(key1own == key1e) || !(key2own == key2e)) {
249 keyPairsEqual =
false;
254 keyPairsEqual =
false;
270 typename Base::Cameras
cameras;
271 for (
size_t i = 0; i < this->
measured_.size();
273 const Pose3& w_P_body1 =
275 const Pose3& w_P_body2 =
277 double interpolationFactor =
alphas_[i];
278 const Pose3& w_P_body =
281 const Pose3& body_P_cam = camera_i.pose();
282 const Pose3& w_P_cam = w_P_body.compose(body_P_cam);
285 camera_i.calibration()));
294 if (this->
active(values)) {
311 const Values& values)
const {
313 throw(
"computeJacobiansWithTriangulatedPoint");
315 size_t numViews = this->
measured_.size();
316 E = Matrix::Zero(2 * numViews,
318 b = Vector::Zero(2 * numViews);
320 Eigen::Matrix<double, ZDim, DimPose> dProject_dPoseCam;
321 Eigen::Matrix<double, DimPose, DimPose> dInterpPose_dPoseBody1,
322 dInterpPose_dPoseBody2, dPoseCam_dInterpPose;
323 Eigen::Matrix<double, ZDim, 3> Ei;
325 for (
size_t i = 0; i < numViews; i++) {
328 double interpolationFactor =
alphas_[i];
332 dInterpPose_dPoseBody1, dInterpPose_dPoseBody2);
334 auto body_P_cam = camera_i.pose();
335 auto w_P_cam = w_P_body.compose(body_P_cam, dPoseCam_dInterpPose);
338 camera_i.calibration()));
341 Point2 reprojectionError_i = camera.reprojectionError(
343 Eigen::Matrix<double, ZDim, DimBlock> J;
344 J.block(0, 0,
ZDim, 6) =
345 dProject_dPoseCam * dPoseCam_dInterpPose *
346 dInterpPose_dPoseBody1;
347 J.block(0, 6,
ZDim, 6) =
348 dProject_dPoseCam * dPoseCam_dInterpPose *
349 dInterpPose_dPoseBody2;
354 b.segment<
ZDim>(
row) = -reprojectionError_i;
362 const Values& values,
const double& lambda = 0.0,
363 bool diagonalDamping =
false)
const {
367 size_t nrUniqueKeys =
371 typename Base::Cameras
cameras = this->cameras(values);
375 std::vector<Matrix> Gs(nrUniqueKeys * (nrUniqueKeys + 1) / 2);
376 std::vector<Vector> gs(nrUniqueKeys);
380 throw std::runtime_error(
381 "SmartProjectionPoseFactorRollingShutter: "
382 "measured_.size() inconsistent with input");
388 if (this->params_.degeneracyMode == ZERO_ON_DEGENERACY) {
390 for (Vector& v : gs) v = Vector::Zero(
DimPose);
391 return boost::make_shared<RegularHessianFactor<DimPose>>(this->
keys_,
394 throw std::runtime_error(
395 "SmartProjectionPoseFactorRollingShutter: "
396 "only supported degeneracy mode is ZERO_ON_DEGENERACY");
407 for (
size_t i = 0; i < Fs.size(); i++)
424 Base::Cameras::template SchurComplementAndRearrangeBlocks<3, 12, 6>(
425 Fs, E, P, b, nonuniqueKeys, this->
keys_);
427 return boost::make_shared<RegularHessianFactor<DimPose>>(
428 this->
keys_, augmentedHessianUniqueKeys);
439 const Values& values,
const double& lambda = 0.0)
const {
442 switch (this->params_.linearizationMode) {
446 throw std::runtime_error(
447 "SmartProjectionPoseFactorRollingShutter: "
448 "unknown linearization mode");
454 const Values& values)
const override {
460 friend class boost::serialization::access;
461 template <
class ARCHIVE>
462 void serialize(ARCHIVE& ar,
const unsigned int ) {
463 ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
469template <
class CAMERA>
471 :
public Testable<SmartProjectionPoseFactorRollingShutter<CAMERA>> {};
Base class to create smart factors on poses or cameras.
Smart factor on cameras (pose + calibration).
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
FastVector is a type alias to a std::vector with a custom memory allocator.
Definition FastVector.h:33
Global functions in a separate testing namespace.
Definition chartTesting.h:28
const MATRIX::ConstRowXpr row(const MATRIX &A, size_t j)
Extracts a row view from a matrix that avoids a copy.
Definition Matrix.h:222
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition Key.h:86
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:27
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:724
gtsam::enable_if_t< needs_eigen_aligned_allocator< T >::value, boost::shared_ptr< T > > make_shared(Args &&... args)
Add our own make_shared as a layer of wrapping on boost::make_shared This solves the problem with the...
Definition make_shared.h:57
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:100
T interpolate(const T &X, const T &Y, double t, typename MakeOptionalJacobian< T, T >::type Hx=boost::none, typename MakeOptionalJacobian< T, T >::type Hy=boost::none)
Linear interpolation between X and Y by coefficient t.
Definition Lie.h:327
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition concepts.h:30
This class stores a dense matrix and allows it to be accessed as a collection of blocks.
Definition SymmetricBlockMatrix.h:52
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:151
A set of cameras, all with their own calibration.
Definition CameraSet.h:36
static Matrix PointCov(const Matrix &E, const double lambda=0.0, bool diagonalDamping=false)
Computes Point Covariance P, with lambda parameter, dynamic version.
Definition CameraSet.h:331
A 3D pose (R,t) : (Rot3,Point3).
Definition Pose3.h:37
KeyVector keys_
The keys involved in this factor.
Definition Factor.h:85
Nonlinear factor base class.
Definition NonlinearFactor.h:42
virtual bool active(const Values &) const
Checks whether a factor should be used based on a set of values.
Definition NonlinearFactor.h:118
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:361
SharedIsotropic noiseModel_
As of Feb 22, 2015, the noise model is the same for all measurements and is isotropic.
Definition SmartFactorBase.h:72
ZVector measured_
Measurements for each of the m views.
Definition SmartFactorBase.h:79
const ZVector & measured() const
Return the 2D measurements (ZDim, in general).
Definition SmartFactorBase.h:159
Definition SmartFactorParams.h:42
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
print
Definition SmartProjectionFactor.h:103
TriangulationResult result_
result from triangulateSafe
Definition SmartProjectionFactor.h:63
TriangulationResult triangulateSafe(const Cameras &cameras) const
Call gtsam::triangulateSafe iff we need to re-triangulate.
Definition SmartProjectionFactor.h:174
double totalReprojectionError(const Cameras &cameras, boost::optional< Point3 > externalPoint=boost::none) const
Calculate the error of the factor.
Definition SmartProjectionFactor.h:411
bool equals(const NonlinearFactor &p, double tol=1e-9) const override
equals
Definition SmartProjectionFactor.h:115
CAMERA Camera
shorthand for a set of cameras
Definition SmartProjectionFactor.h:74
SmartProjectionFactor()
Default constructor, only for serialization.
Definition SmartProjectionFactor.h:80
If you are using the factor, please cite: L.
Definition SmartProjectionPoseFactorRollingShutter.h:46
void add(const MEASUREMENT &measured, const Key &world_P_body_key1, const Key &world_P_body_key2, const double &alpha, const size_t &cameraId=0)
add a new measurement, with 2 pose keys, interpolation factor, and cameraId
Definition SmartProjectionPoseFactorRollingShutter.h:131
void computeJacobiansWithTriangulatedPoint(FBlocks &Fs, Matrix &E, Vector &b, const Values &values) const
Compute jacobian F, E and error vector at a given linearization point.
Definition SmartProjectionPoseFactorRollingShutter.h:310
double error(const Values &values) const override
error calculates the error of the factor.
Definition SmartProjectionPoseFactorRollingShutter.h:293
Base::Cameras cameras(const Values &values) const override
Collect all cameras involved in this factor.
Definition SmartProjectionPoseFactorRollingShutter.h:269
static const int DimPose
Pose3 dimension.
Definition SmartProjectionPoseFactorRollingShutter.h:77
boost::shared_ptr< RegularHessianFactor< DimPose > > createHessianFactor(const Values &values, const double &lambda=0.0, bool diagonalDamping=false) const
linearize and return a Hessianfactor that is an approximation of error(p)
Definition SmartProjectionPoseFactorRollingShutter.h:361
std::vector< double > alphas_
interpolation factor (one for each observation) to interpolate between pair of consecutive poses
Definition SmartProjectionPoseFactorRollingShutter.h:61
const FastVector< size_t > & cameraIds() const
return the calibration object
Definition SmartProjectionPoseFactorRollingShutter.h:210
const std::vector< double > & alphas() const
return the interpolation factors alphas
Definition SmartProjectionPoseFactorRollingShutter.h:204
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
print
Definition SmartProjectionPoseFactorRollingShutter.h:217
boost::shared_ptr< GaussianFactor > linearizeDamped(const Values &values, const double &lambda=0.0) const
Linearize to Gaussian Factor (possibly adding a damping factor Lambda for LM).
Definition SmartProjectionPoseFactorRollingShutter.h:438
SmartProjectionPoseFactorRollingShutter(const SharedNoiseModel &sharedNoiseModel, const boost::shared_ptr< Cameras > &cameraRig, const SmartProjectionParams ¶ms=SmartProjectionParams())
Constructor.
Definition SmartProjectionPoseFactorRollingShutter.h:100
const std::vector< std::pair< Key, Key > > & world_P_body_key_pairs() const
return (for each observation) the keys of the pair of poses from which we interpolate
Definition SmartProjectionPoseFactorRollingShutter.h:199
FastVector< size_t > cameraIds_
vector of camera Ids (one for each observation, in the same order), identifying which camera took the...
Definition SmartProjectionPoseFactorRollingShutter.h:69
boost::shared_ptr< typename Base::Cameras > cameraRig_
one or more cameras taking observations (fixed poses wrt body + fixed intrinsics)
Definition SmartProjectionPoseFactorRollingShutter.h:65
boost::shared_ptr< This > shared_ptr
shorthand for a smart pointer to a factor
Definition SmartProjectionPoseFactorRollingShutter.h:88
const boost::shared_ptr< Cameras > & cameraRig() const
return the calibration object
Definition SmartProjectionPoseFactorRollingShutter.h:207
bool equals(const NonlinearFactor &p, double tol=1e-9) const override
equals
Definition SmartProjectionPoseFactorRollingShutter.h:235
static EIGEN_MAKE_ALIGNED_OPERATOR_NEW const int DimBlock
size of the variable stacking 2 poses from which the observation pose is interpolated
Definition SmartProjectionPoseFactorRollingShutter.h:74
std::vector< std::pair< Key, Key > > world_P_body_key_pairs_
The keys of the pose of the body (with respect to an external world frame): two consecutive poses for...
Definition SmartProjectionPoseFactorRollingShutter.h:57
static const int ZDim
Measurement dimension (Point2).
Definition SmartProjectionPoseFactorRollingShutter.h:78
void add(const MEASUREMENTS &measurements, const std::vector< std::pair< Key, Key > > &world_P_body_key_pairs, const std::vector< double > &alphas, const FastVector< size_t > &cameraIds=FastVector< size_t >())
Variant of the previous "add" function in which we include multiple measurements.
Definition SmartProjectionPoseFactorRollingShutter.h:170
boost::shared_ptr< GaussianFactor > linearize(const Values &values) const override
linearize
Definition SmartProjectionPoseFactorRollingShutter.h:453
SmartProjectionPoseFactorRollingShutter()
Default constructor, only for serialization.
Definition SmartProjectionPoseFactorRollingShutter.h:91
~SmartProjectionPoseFactorRollingShutter() override=default
Virtual destructor.