gtsam 4.1.1
gtsam
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
19#pragma once
20
23
24namespace gtsam {
43template <class CAMERA>
45 : public SmartProjectionFactor<CAMERA> {
46 private:
49 typedef typename CAMERA::CalibrationType CALIBRATION;
50
51 protected:
54 std::vector<std::pair<Key, Key>> world_P_body_key_pairs_;
55
58 std::vector<double> alphas_;
59
62 boost::shared_ptr<typename Base::Cameras> cameraRig_;
63
66 FastVector<size_t> cameraIds_;
67
68 public:
69 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
70
71 typedef CAMERA Camera;
73
75 typedef boost::shared_ptr<This> shared_ptr;
76
77 static const int DimBlock =
78 12;
80 static const int DimPose = 6;
81 static const int ZDim = 2;
82 typedef Eigen::Matrix<double, ZDim, DimBlock>
83 MatrixZD; // F blocks (derivatives wrt block of 2 poses)
84 typedef std::vector<MatrixZD, Eigen::aligned_allocator<MatrixZD>>
85 FBlocks; // vector of F blocks
86
89
98 const SharedNoiseModel& sharedNoiseModel,
99 const boost::shared_ptr<Cameras>& cameraRig,
101 : Base(sharedNoiseModel, params), cameraRig_(cameraRig) {
102 // throw exception if configuration is not supported by this factor
103 if (Base::params_.degeneracyMode != gtsam::ZERO_ON_DEGENERACY)
104 throw std::runtime_error(
105 "SmartProjectionRigFactor: "
106 "degeneracyMode must be set to ZERO_ON_DEGENERACY");
107 if (Base::params_.linearizationMode != gtsam::HESSIAN)
108 throw std::runtime_error(
109 "SmartProjectionRigFactor: "
110 "linearizationMode must be set to HESSIAN");
111 }
112
115
128 void add(const Point2& measured, const Key& world_P_body_key1,
129 const Key& world_P_body_key2, const double& alpha,
130 const size_t& cameraId = 0) {
131 // store measurements in base class
132 this->measured_.push_back(measured);
133
134 // store the pair of keys for each measurement, in the same order
135 world_P_body_key_pairs_.push_back(
136 std::make_pair(world_P_body_key1, world_P_body_key2));
137
138 // also store keys in the keys_ vector: these keys are assumed to be
139 // unique, so we avoid duplicates here
140 if (std::find(this->keys_.begin(), this->keys_.end(), world_P_body_key1) ==
141 this->keys_.end())
142 this->keys_.push_back(world_P_body_key1); // add only unique keys
143 if (std::find(this->keys_.begin(), this->keys_.end(), world_P_body_key2) ==
144 this->keys_.end())
145 this->keys_.push_back(world_P_body_key2); // add only unique keys
146
147 // store interpolation factor
148 alphas_.push_back(alpha);
149
150 // store id of the camera taking the measurement
151 cameraIds_.push_back(cameraId);
152 }
153
167 void add(const Point2Vector& measurements,
168 const std::vector<std::pair<Key, Key>>& world_P_body_key_pairs,
169 const std::vector<double>& alphas,
170 const FastVector<size_t>& cameraIds = FastVector<size_t>()) {
171 if (world_P_body_key_pairs.size() != measurements.size() ||
172 world_P_body_key_pairs.size() != alphas.size() ||
173 (world_P_body_key_pairs.size() != cameraIds.size() &&
174 cameraIds.size() != 0)) { // cameraIds.size()=0 is default
175 throw std::runtime_error(
176 "SmartProjectionPoseFactorRollingShutter: "
177 "trying to add inconsistent inputs");
178 }
179 if (cameraIds.size() == 0 && cameraRig_->size() > 1) {
180 throw std::runtime_error(
181 "SmartProjectionPoseFactorRollingShutter: "
182 "camera rig includes multiple camera "
183 "but add did not input cameraIds");
184 }
185 for (size_t i = 0; i < measurements.size(); i++) {
186 add(measurements[i], world_P_body_key_pairs[i].first,
187 world_P_body_key_pairs[i].second, alphas[i],
188 cameraIds.size() == 0 ? 0
189 : cameraIds[i]); // use 0 as default if
190 // cameraIds was not specified
191 }
192 }
193
196 const std::vector<std::pair<Key, Key>>& world_P_body_key_pairs() const {
198 }
199
201 const std::vector<double>& alphas() const { return alphas_; }
202
204 const boost::shared_ptr<Cameras>& cameraRig() const { return cameraRig_; }
205
207 const FastVector<size_t>& cameraIds() const { return cameraIds_; }
208
214 void print(
215 const std::string& s = "",
216 const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override {
217 std::cout << s << "SmartProjectionPoseFactorRollingShutter: \n ";
218 for (size_t i = 0; i < cameraIds_.size(); i++) {
219 std::cout << "-- Measurement nr " << i << std::endl;
220 std::cout << " pose1 key: "
221 << keyFormatter(world_P_body_key_pairs_[i].first) << std::endl;
222 std::cout << " pose2 key: "
223 << keyFormatter(world_P_body_key_pairs_[i].second) << std::endl;
224 std::cout << " alpha: " << alphas_[i] << std::endl;
225 std::cout << "cameraId: " << cameraIds_[i] << std::endl;
226 (*cameraRig_)[cameraIds_[i]].print("camera in rig:\n");
227 }
228 Base::print("", keyFormatter);
229 }
230
232 bool equals(const NonlinearFactor& p, double tol = 1e-9) const override {
235 &p);
236
237 double keyPairsEqual = true;
238 if (this->world_P_body_key_pairs_.size() ==
239 e->world_P_body_key_pairs().size()) {
240 for (size_t k = 0; k < this->world_P_body_key_pairs_.size(); k++) {
241 const Key key1own = world_P_body_key_pairs_[k].first;
242 const Key key1e = e->world_P_body_key_pairs()[k].first;
243 const Key key2own = world_P_body_key_pairs_[k].second;
244 const Key key2e = e->world_P_body_key_pairs()[k].second;
245 if (!(key1own == key1e) || !(key2own == key2e)) {
246 keyPairsEqual = false;
247 break;
248 }
249 }
250 } else {
251 keyPairsEqual = false;
252 }
253
254 return e && Base::equals(p, tol) && alphas_ == e->alphas() &&
255 keyPairsEqual && cameraRig_->equals(*(e->cameraRig())) &&
256 std::equal(cameraIds_.begin(), cameraIds_.end(),
257 e->cameraIds().begin());
258 }
259
266 typename Base::Cameras cameras(const Values& values) const override {
267 typename Base::Cameras cameras;
268 for (size_t i = 0; i < this->measured_.size();
269 i++) { // for each measurement
270 const Pose3& w_P_body1 =
271 values.at<Pose3>(world_P_body_key_pairs_[i].first);
272 const Pose3& w_P_body2 =
273 values.at<Pose3>(world_P_body_key_pairs_[i].second);
274 double interpolationFactor = alphas_[i];
275 const Pose3& w_P_body =
276 interpolate<Pose3>(w_P_body1, w_P_body2, interpolationFactor);
277 const typename Base::Camera& camera_i = (*cameraRig_)[cameraIds_[i]];
278 const Pose3& body_P_cam = camera_i.pose();
279 const Pose3& w_P_cam = w_P_body.compose(body_P_cam);
280 cameras.emplace_back(w_P_cam,
281 make_shared<typename CAMERA::CalibrationType>(
282 camera_i.calibration()));
283 }
284 return cameras;
285 }
286
290 double error(const Values& values) const override {
291 if (this->active(values)) {
292 return this->totalReprojectionError(this->cameras(values));
293 } else { // else of active flag
294 return 0.0;
295 }
296 }
297
307 void computeJacobiansWithTriangulatedPoint(FBlocks& Fs, Matrix& E, Vector& b,
308 const Values& values) const {
309 if (!this->result_) {
310 throw("computeJacobiansWithTriangulatedPoint");
311 } else { // valid result: compute jacobians
312 size_t numViews = this->measured_.size();
313 E = Matrix::Zero(2 * numViews,
314 3); // a Point2 for each view (point jacobian)
315 b = Vector::Zero(2 * numViews); // a Point2 for each view
316 // intermediate Jacobians
317 Eigen::Matrix<double, ZDim, DimPose> dProject_dPoseCam;
318 Eigen::Matrix<double, DimPose, DimPose> dInterpPose_dPoseBody1,
319 dInterpPose_dPoseBody2, dPoseCam_dInterpPose;
320 Eigen::Matrix<double, ZDim, 3> Ei;
321
322 for (size_t i = 0; i < numViews; i++) { // for each camera/measurement
323 auto w_P_body1 = values.at<Pose3>(world_P_body_key_pairs_[i].first);
324 auto w_P_body2 = values.at<Pose3>(world_P_body_key_pairs_[i].second);
325 double interpolationFactor = alphas_[i];
326 // get interpolated pose:
327 auto w_P_body =
328 interpolate<Pose3>(w_P_body1, w_P_body2, interpolationFactor,
329 dInterpPose_dPoseBody1, dInterpPose_dPoseBody2);
330 const typename Base::Camera& camera_i = (*cameraRig_)[cameraIds_[i]];
331 auto body_P_cam = camera_i.pose();
332 auto w_P_cam = w_P_body.compose(body_P_cam, dPoseCam_dInterpPose);
333 PinholeCamera<CALIBRATION> camera(w_P_cam, camera_i.calibration());
334
335 // get jacobians and error vector for current measurement
336 Point2 reprojectionError_i =
337 Point2(camera.project(*this->result_, dProject_dPoseCam, Ei) -
338 this->measured_.at(i));
339 Eigen::Matrix<double, ZDim, DimBlock> J; // 2 x 12
340 J.block(0, 0, ZDim, 6) =
341 dProject_dPoseCam * dPoseCam_dInterpPose *
342 dInterpPose_dPoseBody1; // (2x6) * (6x6) * (6x6)
343 J.block(0, 6, ZDim, 6) =
344 dProject_dPoseCam * dPoseCam_dInterpPose *
345 dInterpPose_dPoseBody2; // (2x6) * (6x6) * (6x6)
346
347 // fit into the output structures
348 Fs.push_back(J);
349 size_t row = 2 * i;
350 b.segment<ZDim>(row) = -reprojectionError_i;
351 E.block<ZDim, 3>(row, 0) = Ei;
352 }
353 }
354 }
355
357 boost::shared_ptr<RegularHessianFactor<DimPose>> createHessianFactor(
358 const Values& values, const double& lambda = 0.0,
359 bool diagonalDamping = false) const {
360 // we may have multiple observation sharing the same keys (due to the
361 // rolling shutter interpolation), hence the number of unique keys may be
362 // smaller than 2 * nrMeasurements
363 size_t nrUniqueKeys =
364 this->keys_
365 .size(); // note: by construction, keys_ only contains unique keys
366
367 typename Base::Cameras cameras = this->cameras(values);
368
369 // Create structures for Hessian Factors
370 KeyVector js;
371 std::vector<Matrix> Gs(nrUniqueKeys * (nrUniqueKeys + 1) / 2);
372 std::vector<Vector> gs(nrUniqueKeys);
373
374 if (this->measured_.size() !=
375 cameras.size()) // 1 observation per interpolated camera
376 throw std::runtime_error(
377 "SmartProjectionPoseFactorRollingShutter: "
378 "measured_.size() inconsistent with input");
379
380 // triangulate 3D point at given linearization point
381 this->triangulateSafe(cameras);
382
383 if (!this->result_) { // failed: return "empty/zero" Hessian
384 if (this->params_.degeneracyMode == ZERO_ON_DEGENERACY) {
385 for (Matrix& m : Gs) m = Matrix::Zero(DimPose, DimPose);
386 for (Vector& v : gs) v = Vector::Zero(DimPose);
387 return boost::make_shared<RegularHessianFactor<DimPose>>(this->keys_,
388 Gs, gs, 0.0);
389 } else {
390 throw std::runtime_error(
391 "SmartProjectionPoseFactorRollingShutter: "
392 "only supported degeneracy mode is ZERO_ON_DEGENERACY");
393 }
394 }
395 // compute Jacobian given triangulated 3D Point
396 FBlocks Fs;
397 Matrix E;
398 Vector b;
399 this->computeJacobiansWithTriangulatedPoint(Fs, E, b, values);
400
401 // Whiten using noise model
402 this->noiseModel_->WhitenSystem(E, b);
403 for (size_t i = 0; i < Fs.size(); i++)
404 Fs[i] = this->noiseModel_->Whiten(Fs[i]);
405
406 Matrix3 P = Base::Cameras::PointCov(E, lambda, diagonalDamping);
407
408 // Collect all the key pairs: these are the keys that correspond to the
409 // blocks in Fs (on which we apply the Schur Complement)
410 KeyVector nonuniqueKeys;
411 for (size_t i = 0; i < world_P_body_key_pairs_.size(); i++) {
412 nonuniqueKeys.push_back(world_P_body_key_pairs_.at(i).first);
413 nonuniqueKeys.push_back(world_P_body_key_pairs_.at(i).second);
414 }
415
416 // Build augmented Hessian (with last row/column being the information
417 // vector) Note: we need to get the augumented hessian wrt the unique keys
418 // in key_
419 SymmetricBlockMatrix augmentedHessianUniqueKeys =
420 Base::Cameras::template SchurComplementAndRearrangeBlocks<3, 12, 6>(
421 Fs, E, P, b, nonuniqueKeys, this->keys_);
422
423 return boost::make_shared<RegularHessianFactor<DimPose>>(
424 this->keys_, augmentedHessianUniqueKeys);
425 }
426
434 boost::shared_ptr<GaussianFactor> linearizeDamped(
435 const Values& values, const double& lambda = 0.0) const {
436 // depending on flag set on construction we may linearize to different
437 // linear factors
438 switch (this->params_.linearizationMode) {
439 case HESSIAN:
440 return this->createHessianFactor(values, lambda);
441 default:
442 throw std::runtime_error(
443 "SmartProjectionPoseFactorRollingShutter: "
444 "unknown linearization mode");
445 }
446 }
447
449 boost::shared_ptr<GaussianFactor> linearize(
450 const Values& values) const override {
451 return this->linearizeDamped(values);
452 }
453
454 private:
457 template <class ARCHIVE>
458 void serialize(ARCHIVE& ar, const unsigned int /*version*/) {
459 ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
460 }
461};
462// end of class declaration
463
465template <class CAMERA>
467 : public Testable<SmartProjectionPoseFactorRollingShutter<CAMERA>> {};
468
469} // namespace gtsam
Base class to create smart factors on poses or cameras.
Smart factor on cameras (pose + calibration)
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:86
const MATRIX::ConstRowXpr row(const MATRIX &A, size_t j)
Extracts a row view from a matrix that avoids a copy.
Definition: Matrix.h:225
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
noiseModel::Base::shared_ptr SharedNoiseModel
Note, deliberately not in noiseModel namespace.
Definition: NoiseModel.h:736
bool equal(const T &obj1, const T &obj2, double tol)
Call equal on the object.
Definition: Testable.h:84
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:69
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
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
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:35
Definition: PinholeCamera.h:33
Point2 project(const Point3 &pw, OptionalJacobian< 2, 6 > Dpose=boost::none, OptionalJacobian< 2, 3 > Dpoint=boost::none, OptionalJacobian< 2, DimK > Dcal=boost::none) const
project a 3D point from world coordinates into the image
Definition: PinholePose.h:118
Definition: Pose3.h:37
This is the base class for all factor types.
Definition: Factor.h:56
KeyVector keys_
The keys involved in this factor.
Definition: Factor.h:73
Nonlinear factor base class.
Definition: NonlinearFactor.h:43
virtual bool active(const Values &) const
Checks whether a factor should be used based on a set of values.
Definition: NonlinearFactor.h:106
A non-templated config holding any types of Manifold-group elements.
Definition: Values.h:63
const ValueType at(Key j) const
Retrieve a variable by key j.
Definition: Values-inl.h:346
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
LinearizationMode linearizationMode
How to linearize the factor.
Definition: SmartFactorParams.h:44
DegeneracyMode degeneracyMode
How to linearize the factor.
Definition: SmartFactorParams.h:45
SmartProjectionFactor: triangulates point and keeps an estimate of it around.
Definition: SmartProjectionFactor.h:45
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
Definition: SmartProjectionPoseFactorRollingShutter.h:45
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:307
double error(const Values &values) const override
error calculates the error of the factor.
Definition: SmartProjectionPoseFactorRollingShutter.h:290
void add(const Point2Vector &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:167
Base::Cameras cameras(const Values &values) const override
Collect all cameras involved in this factor.
Definition: SmartProjectionPoseFactorRollingShutter.h:266
static const int DimBlock
size of the variable stacking 2 poses from which the observation pose is interpolated
Definition: SmartProjectionPoseFactorRollingShutter.h:77
static const int DimPose
Pose3 dimension.
Definition: SmartProjectionPoseFactorRollingShutter.h:80
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:357
std::vector< double > alphas_
interpolation factor (one for each observation) to interpolate between pair of consecutive poses
Definition: SmartProjectionPoseFactorRollingShutter.h:58
const FastVector< size_t > & cameraIds() const
return the calibration object
Definition: SmartProjectionPoseFactorRollingShutter.h:207
const std::vector< double > & alphas() const
return the interpolation factors alphas
Definition: SmartProjectionPoseFactorRollingShutter.h:201
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
print
Definition: SmartProjectionPoseFactorRollingShutter.h:214
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:434
SmartProjectionPoseFactorRollingShutter(const SharedNoiseModel &sharedNoiseModel, const boost::shared_ptr< Cameras > &cameraRig, const SmartProjectionParams &params=SmartProjectionParams())
Constructor.
Definition: SmartProjectionPoseFactorRollingShutter.h:97
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:196
FastVector< size_t > cameraIds_
vector of camera Ids (one for each observation, in the same order), identifying which camera took the...
Definition: SmartProjectionPoseFactorRollingShutter.h:66
boost::shared_ptr< typename Base::Cameras > cameraRig_
one or more cameras taking observations (fixed poses wrt body + fixed intrinsics)
Definition: SmartProjectionPoseFactorRollingShutter.h:62
boost::shared_ptr< This > shared_ptr
shorthand for a smart pointer to a factor
Definition: SmartProjectionPoseFactorRollingShutter.h:75
const boost::shared_ptr< Cameras > & cameraRig() const
return the calibration object
Definition: SmartProjectionPoseFactorRollingShutter.h:204
void add(const Point2 &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:128
bool equals(const NonlinearFactor &p, double tol=1e-9) const override
equals
Definition: SmartProjectionPoseFactorRollingShutter.h:232
friend class boost::serialization::access
Serialization function.
Definition: SmartProjectionPoseFactorRollingShutter.h:456
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:54
static const int ZDim
Measurement dimension (Point2)
Definition: SmartProjectionPoseFactorRollingShutter.h:81
boost::shared_ptr< GaussianFactor > linearize(const Values &values) const override
linearize
Definition: SmartProjectionPoseFactorRollingShutter.h:449
SmartProjectionPoseFactorRollingShutter()
Default constructor, only for serialization.
Definition: SmartProjectionPoseFactorRollingShutter.h:88
~SmartProjectionPoseFactorRollingShutter() override=default
Virtual destructor.