gtsam 4.2
gtsam
Loading...
Searching...
No Matches
SmartProjectionPoseFactorRollingShutter.h
Go to the documentation of this file.
1/* ----------------------------------------------------------------------------
2
3 * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4 * Atlanta, Georgia 30332-0415
5 * All Rights Reserved
6 * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7
8 * See LICENSE for the license information
9
10 * -------------------------------------------------------------------------- */
11
18
19#pragma once
20
23#include <gtsam_unstable/dllexport.h>
24
25namespace gtsam {
36
44template <class CAMERA>
45class GTSAM_UNSTABLE_EXPORT SmartProjectionPoseFactorRollingShutter
46 : public SmartProjectionFactor<CAMERA> {
47 private:
50 typedef typename CAMERA::CalibrationType CALIBRATION;
51 typedef typename CAMERA::Measurement MEASUREMENT;
52 typedef typename CAMERA::MeasurementVector MEASUREMENTS;
53
54 protected:
57 std::vector<std::pair<Key, Key>> world_P_body_key_pairs_;
58
61 std::vector<double> alphas_;
62
65 boost::shared_ptr<typename Base::Cameras> cameraRig_;
66
70
71 public:
72 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
73
74 static const int DimBlock =
75 12;
77 static const int DimPose = 6;
78 static const int ZDim = 2;
79 typedef Eigen::Matrix<double, ZDim, DimBlock>
80 MatrixZD; // F blocks (derivatives wrt block of 2 poses)
81 typedef std::vector<MatrixZD, Eigen::aligned_allocator<MatrixZD>>
82 FBlocks; // vector of F blocks
83
84 typedef CAMERA Camera;
85 typedef CameraSet<CAMERA> Cameras;
86
88 typedef boost::shared_ptr<This> shared_ptr;
89
92
101 const SharedNoiseModel& sharedNoiseModel,
102 const boost::shared_ptr<Cameras>& cameraRig,
104 : Base(sharedNoiseModel, params), cameraRig_(cameraRig) {
105 // throw exception if configuration is not supported by this factor
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");
114 }
115
118
131 void add(const MEASUREMENT& measured, const Key& world_P_body_key1,
132 const Key& world_P_body_key2, const double& alpha,
133 const size_t& cameraId = 0) {
134 // store measurements in base class
135 this->measured_.push_back(measured);
136
137 // store the pair of keys for each measurement, in the same order
138 world_P_body_key_pairs_.push_back(
139 std::make_pair(world_P_body_key1, world_P_body_key2));
140
141 // also store keys in the keys_ vector: these keys are assumed to be
142 // unique, so we avoid duplicates here
143 if (std::find(this->keys_.begin(), this->keys_.end(), world_P_body_key1) ==
144 this->keys_.end())
145 this->keys_.push_back(world_P_body_key1); // add only unique keys
146 if (std::find(this->keys_.begin(), this->keys_.end(), world_P_body_key2) ==
147 this->keys_.end())
148 this->keys_.push_back(world_P_body_key2); // add only unique keys
149
150 // store interpolation factor
151 alphas_.push_back(alpha);
152
153 // store id of the camera taking the measurement
154 cameraIds_.push_back(cameraId);
155 }
156
170 void add(const MEASUREMENTS& measurements,
171 const std::vector<std::pair<Key, Key>>& world_P_body_key_pairs,
172 const std::vector<double>& alphas,
174 if (world_P_body_key_pairs.size() != measurements.size() ||
175 world_P_body_key_pairs.size() != alphas.size() ||
176 (world_P_body_key_pairs.size() != cameraIds.size() &&
177 cameraIds.size() != 0)) { // cameraIds.size()=0 is default
178 throw std::runtime_error(
179 "SmartProjectionPoseFactorRollingShutter: "
180 "trying to add inconsistent inputs");
181 }
182 if (cameraIds.size() == 0 && cameraRig_->size() > 1) {
183 throw std::runtime_error(
184 "SmartProjectionPoseFactorRollingShutter: "
185 "camera rig includes multiple camera "
186 "but add did not input cameraIds");
187 }
188 for (size_t i = 0; i < measurements.size(); i++) {
189 add(measurements[i], world_P_body_key_pairs[i].first,
190 world_P_body_key_pairs[i].second, alphas[i],
191 cameraIds.size() == 0 ? 0
192 : cameraIds[i]); // use 0 as default if
193 // cameraIds was not specified
194 }
195 }
196
199 const std::vector<std::pair<Key, Key>>& world_P_body_key_pairs() const {
201 }
202
204 const std::vector<double>& alphas() const { return alphas_; }
205
207 const boost::shared_ptr<Cameras>& cameraRig() const { return cameraRig_; }
208
210 const FastVector<size_t>& cameraIds() const { return cameraIds_; }
211
217 void print(
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: "
224 << keyFormatter(world_P_body_key_pairs_[i].first) << std::endl;
225 std::cout << " pose2 key: "
226 << keyFormatter(world_P_body_key_pairs_[i].second) << std::endl;
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");
230 }
231 Base::print("", keyFormatter);
232 }
233
235 bool equals(const NonlinearFactor& p, double tol = 1e-9) const override {
238 &p);
239
240 double keyPairsEqual = true;
241 if (this->world_P_body_key_pairs_.size() ==
242 e->world_P_body_key_pairs().size()) {
243 for (size_t k = 0; k < this->world_P_body_key_pairs_.size(); k++) {
244 const Key key1own = world_P_body_key_pairs_[k].first;
245 const Key key1e = e->world_P_body_key_pairs()[k].first;
246 const Key key2own = world_P_body_key_pairs_[k].second;
247 const Key key2e = e->world_P_body_key_pairs()[k].second;
248 if (!(key1own == key1e) || !(key2own == key2e)) {
249 keyPairsEqual = false;
250 break;
251 }
252 }
253 } else {
254 keyPairsEqual = false;
255 }
256
257 return e && Base::equals(p, tol) && alphas_ == e->alphas() &&
258 keyPairsEqual && cameraRig_->equals(*(e->cameraRig())) &&
259 std::equal(cameraIds_.begin(), cameraIds_.end(),
260 e->cameraIds().begin());
261 }
262
269 typename Base::Cameras cameras(const Values& values) const override {
270 typename Base::Cameras cameras;
271 for (size_t i = 0; i < this->measured_.size();
272 i++) { // for each measurement
273 const Pose3& w_P_body1 =
274 values.at<Pose3>(world_P_body_key_pairs_[i].first);
275 const Pose3& w_P_body2 =
276 values.at<Pose3>(world_P_body_key_pairs_[i].second);
277 double interpolationFactor = alphas_[i];
278 const Pose3& w_P_body =
279 interpolate<Pose3>(w_P_body1, w_P_body2, interpolationFactor);
280 const typename Base::Camera& camera_i = (*cameraRig_)[cameraIds_[i]];
281 const Pose3& body_P_cam = camera_i.pose();
282 const Pose3& w_P_cam = w_P_body.compose(body_P_cam);
283 cameras.emplace_back(w_P_cam,
285 camera_i.calibration()));
286 }
287 return cameras;
288 }
289
293 double error(const Values& values) const override {
294 if (this->active(values)) {
295 return this->totalReprojectionError(this->cameras(values));
296 } else { // else of active flag
297 return 0.0;
298 }
299 }
300
310 void computeJacobiansWithTriangulatedPoint(FBlocks& Fs, Matrix& E, Vector& b,
311 const Values& values) const {
312 if (!this->result_) {
313 throw("computeJacobiansWithTriangulatedPoint");
314 } else { // valid result: compute jacobians
315 size_t numViews = this->measured_.size();
316 E = Matrix::Zero(2 * numViews,
317 3); // a Point2 for each view (point jacobian)
318 b = Vector::Zero(2 * numViews); // a Point2 for each view
319 // intermediate Jacobians
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;
324
325 for (size_t i = 0; i < numViews; i++) { // for each camera/measurement
326 auto w_P_body1 = values.at<Pose3>(world_P_body_key_pairs_[i].first);
327 auto w_P_body2 = values.at<Pose3>(world_P_body_key_pairs_[i].second);
328 double interpolationFactor = alphas_[i];
329 // get interpolated pose:
330 auto w_P_body =
331 interpolate<Pose3>(w_P_body1, w_P_body2, interpolationFactor,
332 dInterpPose_dPoseBody1, dInterpPose_dPoseBody2);
333 const typename Base::Camera& camera_i = (*cameraRig_)[cameraIds_[i]];
334 auto body_P_cam = camera_i.pose();
335 auto w_P_cam = w_P_body.compose(body_P_cam, dPoseCam_dInterpPose);
336 typename Base::Camera camera(
338 camera_i.calibration()));
339
340 // get jacobians and error vector for current measurement
341 Point2 reprojectionError_i = camera.reprojectionError(
342 *this->result_, this->measured_.at(i), dProject_dPoseCam, Ei);
343 Eigen::Matrix<double, ZDim, DimBlock> J; // 2 x 12
344 J.block(0, 0, ZDim, 6) =
345 dProject_dPoseCam * dPoseCam_dInterpPose *
346 dInterpPose_dPoseBody1; // (2x6) * (6x6) * (6x6)
347 J.block(0, 6, ZDim, 6) =
348 dProject_dPoseCam * dPoseCam_dInterpPose *
349 dInterpPose_dPoseBody2; // (2x6) * (6x6) * (6x6)
350
351 // fit into the output structures
352 Fs.push_back(J);
353 size_t row = 2 * i;
354 b.segment<ZDim>(row) = -reprojectionError_i;
355 E.block<ZDim, 3>(row, 0) = Ei;
356 }
357 }
358 }
359
361 boost::shared_ptr<RegularHessianFactor<DimPose>> createHessianFactor(
362 const Values& values, const double& lambda = 0.0,
363 bool diagonalDamping = false) const {
364 // we may have multiple observation sharing the same keys (due to the
365 // rolling shutter interpolation), hence the number of unique keys may be
366 // smaller than 2 * nrMeasurements
367 size_t nrUniqueKeys =
368 this->keys_
369 .size(); // note: by construction, keys_ only contains unique keys
370
371 typename Base::Cameras cameras = this->cameras(values);
372
373 // Create structures for Hessian Factors
374 KeyVector js;
375 std::vector<Matrix> Gs(nrUniqueKeys * (nrUniqueKeys + 1) / 2);
376 std::vector<Vector> gs(nrUniqueKeys);
377
378 if (this->measured_.size() !=
379 cameras.size()) // 1 observation per interpolated camera
380 throw std::runtime_error(
381 "SmartProjectionPoseFactorRollingShutter: "
382 "measured_.size() inconsistent with input");
383
384 // triangulate 3D point at given linearization point
386
387 if (!this->result_) { // failed: return "empty/zero" Hessian
388 if (this->params_.degeneracyMode == ZERO_ON_DEGENERACY) {
389 for (Matrix& m : Gs) m = Matrix::Zero(DimPose, DimPose);
390 for (Vector& v : gs) v = Vector::Zero(DimPose);
391 return boost::make_shared<RegularHessianFactor<DimPose>>(this->keys_,
392 Gs, gs, 0.0);
393 } else {
394 throw std::runtime_error(
395 "SmartProjectionPoseFactorRollingShutter: "
396 "only supported degeneracy mode is ZERO_ON_DEGENERACY");
397 }
398 }
399 // compute Jacobian given triangulated 3D Point
400 FBlocks Fs;
401 Matrix E;
402 Vector b;
403 this->computeJacobiansWithTriangulatedPoint(Fs, E, b, values);
404
405 // Whiten using noise model
406 this->noiseModel_->WhitenSystem(E, b);
407 for (size_t i = 0; i < Fs.size(); i++)
408 Fs[i] = this->noiseModel_->Whiten(Fs[i]);
409
410 Matrix3 P = Cameras::PointCov(E, lambda, diagonalDamping);
411
412 // Collect all the key pairs: these are the keys that correspond to the
413 // blocks in Fs (on which we apply the Schur Complement)
414 KeyVector nonuniqueKeys;
415 for (size_t i = 0; i < world_P_body_key_pairs_.size(); i++) {
416 nonuniqueKeys.push_back(world_P_body_key_pairs_.at(i).first);
417 nonuniqueKeys.push_back(world_P_body_key_pairs_.at(i).second);
418 }
419
420 // Build augmented Hessian (with last row/column being the information
421 // vector) Note: we need to get the augumented hessian wrt the unique keys
422 // in key_
423 SymmetricBlockMatrix augmentedHessianUniqueKeys =
424 Base::Cameras::template SchurComplementAndRearrangeBlocks<3, 12, 6>(
425 Fs, E, P, b, nonuniqueKeys, this->keys_);
426
427 return boost::make_shared<RegularHessianFactor<DimPose>>(
428 this->keys_, augmentedHessianUniqueKeys);
429 }
430
438 boost::shared_ptr<GaussianFactor> linearizeDamped(
439 const Values& values, const double& lambda = 0.0) const {
440 // depending on flag set on construction we may linearize to different
441 // linear factors
442 switch (this->params_.linearizationMode) {
443 case HESSIAN:
444 return this->createHessianFactor(values, lambda);
445 default:
446 throw std::runtime_error(
447 "SmartProjectionPoseFactorRollingShutter: "
448 "unknown linearization mode");
449 }
450 }
451
453 boost::shared_ptr<GaussianFactor> linearize(
454 const Values& values) const override {
455 return this->linearizeDamped(values);
456 }
457
458 private:
460 friend class boost::serialization::access;
461 template <class ARCHIVE>
462 void serialize(ARCHIVE& ar, const unsigned int /*version*/) {
463 ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
464 }
465};
466// end of class declaration
467
469template <class CAMERA>
471 : public Testable<SmartProjectionPoseFactorRollingShutter<CAMERA>> {};
472
473} // namespace gtsam
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 &params=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.