SmartProjectionRigFactor.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 
25 #pragma once
26 
28 
29 namespace gtsam {
75 template <class CAMERA>
77  private:
80  typedef typename CAMERA::CalibrationType CALIBRATION;
81  typedef typename CAMERA::Measurement MEASUREMENT;
82  typedef typename CAMERA::MeasurementVector MEASUREMENTS;
83 
84  static const int DimPose = 6;
85  static const int ZDim = 2;
86 
87  protected:
90 
92  std::shared_ptr<typename Base::Cameras> cameraRig_;
93 
97 
98  public:
100 
101  typedef CAMERA Camera;
103 
105  typedef std::shared_ptr<This> shared_ptr;
106 
109 
119  const SharedNoiseModel& sharedNoiseModel,
120  const std::shared_ptr<Cameras>& cameraRig,
122  : Base(sharedNoiseModel, params), cameraRig_(cameraRig) {
123  // throw exception if configuration is not supported by this factor
124  if (Base::params_.degeneracyMode != gtsam::ZERO_ON_DEGENERACY)
125  throw std::runtime_error(
126  "SmartProjectionRigFactor: "
127  "degeneracyMode must be set to ZERO_ON_DEGENERACY");
128  if (Base::params_.linearizationMode != gtsam::HESSIAN)
129  throw std::runtime_error(
130  "SmartProjectionRigFactor: "
131  "linearizationMode must be set to HESSIAN");
132  }
133 
144  void add(const MEASUREMENT& measured, const Key& poseKey,
145  const size_t& cameraId = 0) {
146  // store measurement and key
147  this->measured_.push_back(measured);
148  this->nonUniqueKeys_.push_back(poseKey);
149 
150  // also store keys in the keys_ vector: these keys are assumed to be
151  // unique, so we avoid duplicates here
152  if (std::find(this->keys_.begin(), this->keys_.end(), poseKey) ==
153  this->keys_.end())
154  this->keys_.push_back(poseKey); // add only unique keys
155 
156  // store id of the camera taking the measurement
157  cameraIds_.push_back(cameraId);
158  }
159 
170  void add(const MEASUREMENTS& measurements, const KeyVector& poseKeys,
172  if (poseKeys.size() != measurements.size() ||
173  (poseKeys.size() != cameraIds.size() && cameraIds.size() != 0)) {
174  throw std::runtime_error(
175  "SmartProjectionRigFactor: "
176  "trying to add inconsistent inputs");
177  }
178  if (cameraIds.size() == 0 && cameraRig_->size() > 1) {
179  throw std::runtime_error(
180  "SmartProjectionRigFactor: "
181  "camera rig includes multiple camera "
182  "but add did not input cameraIds");
183  }
184  for (size_t i = 0; i < measurements.size(); i++) {
185  add(measurements[i], poseKeys[i],
186  cameraIds.size() == 0 ? 0 : cameraIds[i]);
187  }
188  }
189 
192  const KeyVector& nonUniqueKeys() const { return nonUniqueKeys_; }
193 
195  const std::shared_ptr<Cameras>& cameraRig() const { return cameraRig_; }
196 
198  const FastVector<size_t>& cameraIds() const { return cameraIds_; }
199 
205  void print(
206  const std::string& s = "",
207  const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override {
208  std::cout << s << "SmartProjectionRigFactor: \n ";
209  for (size_t i = 0; i < nonUniqueKeys_.size(); i++) {
210  std::cout << "-- Measurement nr " << i << std::endl;
211  std::cout << "key: " << keyFormatter(nonUniqueKeys_[i]) << std::endl;
212  std::cout << "cameraId: " << cameraIds_[i] << std::endl;
213  (*cameraRig_)[cameraIds_[i]].print("camera in rig:\n");
214  }
215  Base::print("", keyFormatter);
216  }
217 
219  bool equals(const NonlinearFactor& p, double tol = 1e-9) const override {
220  const This* e = dynamic_cast<const This*>(&p);
221  return e && Base::equals(p, tol) && nonUniqueKeys_ == e->nonUniqueKeys() &&
222  cameraRig_->equals(*(e->cameraRig())) &&
223  std::equal(cameraIds_.begin(), cameraIds_.end(),
224  e->cameraIds().begin());
225  }
226 
233  typename Base::Cameras cameras(const Values& values) const override {
234  typename Base::Cameras cameras;
235  cameras.reserve(nonUniqueKeys_.size()); // preallocate
236  for (size_t i = 0; i < nonUniqueKeys_.size(); i++) {
237  const typename Base::Camera& camera_i = (*cameraRig_)[cameraIds_[i]];
238  const Pose3 world_P_sensor_i =
239  values.at<Pose3>(nonUniqueKeys_[i]) // = world_P_body
240  * camera_i.pose(); // = body_P_cam_i
241  cameras.emplace_back(world_P_sensor_i,
242  make_shared<typename CAMERA::CalibrationType>(
243  camera_i.calibration()));
244  }
245  return cameras;
246  }
247 
251  double error(const Values& values) const override {
252  if (this->active(values)) {
253  return this->totalReprojectionError(this->cameras(values));
254  } else { // else of active flag
255  return 0.0;
256  }
257  }
258 
269  Matrix& E, Vector& b,
270  const Cameras& cameras) const {
271  if (!this->result_) {
272  throw("computeJacobiansWithTriangulatedPoint");
273  } else { // valid result: compute jacobians
274  b = -cameras.reprojectionError(*this->result_, this->measured_, Fs, E);
275  for (size_t i = 0; i < Fs.size(); i++) {
276  const Pose3& body_P_sensor = (*cameraRig_)[cameraIds_[i]].pose();
277  const Pose3 world_P_body = cameras[i].pose() * body_P_sensor.inverse();
279  world_P_body.compose(body_P_sensor, H);
280  Fs.at(i) = Fs.at(i) * H;
281  }
282  }
283  }
284 
286  std::shared_ptr<RegularHessianFactor<DimPose> > createHessianFactor(
287  const Values& values, const double& lambda = 0.0,
288  bool diagonalDamping = false) const {
289  // we may have multiple observation sharing the same keys (e.g., 2 cameras
290  // measuring from the same body pose), hence the number of unique keys may
291  // be smaller than nrMeasurements
292  size_t nrUniqueKeys =
293  this->keys_
294  .size(); // note: by construction, keys_ only contains unique keys
295 
296  Cameras cameras = this->cameras(values);
297 
298  // Create structures for Hessian Factors
299  std::vector<size_t> js;
300  std::vector<Matrix> Gs(nrUniqueKeys * (nrUniqueKeys + 1) / 2);
301  std::vector<Vector> gs(nrUniqueKeys);
302 
303  if (this->measured_.size() != cameras.size()) // 1 observation per camera
304  throw std::runtime_error(
305  "SmartProjectionRigFactor: "
306  "measured_.size() inconsistent with input");
307 
308  // triangulate 3D point at given linearization point
309  this->triangulateSafe(cameras);
310 
311  if (!this->result_) { // failed: return "empty/zero" Hessian
313  for (Matrix& m : Gs) m = Matrix::Zero(DimPose, DimPose);
314  for (Vector& v : gs) v = Vector::Zero(DimPose);
315  return std::make_shared<RegularHessianFactor<DimPose> >(this->keys_,
316  Gs, gs, 0.0);
317  } else {
318  throw std::runtime_error(
319  "SmartProjectionRigFactor: "
320  "only supported degeneracy mode is ZERO_ON_DEGENERACY");
321  }
322  }
323 
324  // compute Jacobian given triangulated 3D Point
325  typename Base::FBlocks Fs;
326  Matrix E;
327  Vector b;
329 
330  // Whiten using noise model
331  this->noiseModel_->WhitenSystem(E, b);
332  for (size_t i = 0; i < Fs.size(); i++) {
333  Fs[i] = this->noiseModel_->Whiten(Fs[i]);
334  }
335 
336  const Matrix3 P = Base::Cameras::PointCov(E, lambda, diagonalDamping);
337 
338  // Build augmented Hessian (with last row/column being the information
339  // vector) Note: we need to get the augumented hessian wrt the unique keys
340  // in key_
341  SymmetricBlockMatrix augmentedHessianUniqueKeys =
342  Base::Cameras::template SchurComplementAndRearrangeBlocks<3, 6, 6>(
343  Fs, E, P, b, nonUniqueKeys_, this->keys_);
344 
345  return std::make_shared<RegularHessianFactor<DimPose> >(
346  this->keys_, augmentedHessianUniqueKeys);
347  }
348 
356  std::shared_ptr<GaussianFactor> linearizeDamped(
357  const Values& values, const double& lambda = 0.0) const {
358  // depending on flag set on construction we may linearize to different
359  // linear factors
360  switch (this->params_.linearizationMode) {
361  case HESSIAN:
362  return this->createHessianFactor(values, lambda);
363  default:
364  throw std::runtime_error(
365  "SmartProjectionRigFactor: unknown linearization mode");
366  }
367  }
368 
370  std::shared_ptr<GaussianFactor> linearize(
371  const Values& values) const override {
372  return this->linearizeDamped(values);
373  }
374 
375  private:
376 #if GTSAM_ENABLE_BOOST_SERIALIZATION
377  friend class boost::serialization::access;
379  template <class ARCHIVE>
380  void serialize(ARCHIVE& ar, const unsigned int /*version*/) {
381  ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
382  // ar& BOOST_SERIALIZATION_NVP(nonUniqueKeys_);
383  // ar& BOOST_SERIALIZATION_NVP(cameraRig_);
384  // ar& BOOST_SERIALIZATION_NVP(cameraIds_);
385  }
386 #endif
387 };
388 // end of class declaration
389 
391 template <class CAMERA>
393  : public Testable<SmartProjectionRigFactor<CAMERA> > {};
394 
395 } // namespace gtsam
gtsam::SmartProjectionRigFactor::equals
bool equals(const NonlinearFactor &p, double tol=1e-9) const override
equals
Definition: SmartProjectionRigFactor.h:219
H
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set set pointsize set encoding default set nopolar set noparametric set set set set surface set nocontour set clabel set mapping cartesian set nohidden3d set cntrparam order set cntrparam linear set cntrparam levels auto set cntrparam points set size set set xzeroaxis lt lw set x2zeroaxis lt lw set yzeroaxis lt lw set y2zeroaxis lt lw set tics in set ticslevel set tics set mxtics default set mytics default set mx2tics default set my2tics default set xtics border mirror norotate autofreq set ytics border mirror norotate autofreq set ztics border nomirror norotate autofreq set nox2tics set noy2tics set timestamp bottom norotate set rrange[*:*] noreverse nowriteback set trange[*:*] noreverse nowriteback set urange[*:*] noreverse nowriteback set vrange[*:*] noreverse nowriteback set xlabel matrix size set x2label set timefmt d m y n H
Definition: gnuplot_common_settings.hh:74
gtsam::SmartProjectionRigFactor::linearizeDamped
std::shared_ptr< GaussianFactor > linearizeDamped(const Values &values, const double &lambda=0.0) const
Definition: SmartProjectionRigFactor.h:356
gtsam::SmartFactorBase::body_P_sensor
Pose3 body_P_sensor() const
Definition: SmartFactorBase.h:444
gtsam::SmartProjectionRigFactor::Camera
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef CAMERA Camera
Definition: SmartProjectionRigFactor.h:101
s
RealScalar s
Definition: level1_cplx_impl.h:126
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
gtsam::HESSIAN
@ HESSIAN
Definition: SmartFactorParams.h:31
gtsam::SmartProjectionParams
Definition: SmartFactorParams.h:42
gtsam::FastVector
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
Definition: FastVector.h:34
SmartProjectionFactor.h
Smart factor on cameras (pose + calibration)
pybind_wrapper_test_script.this
this
Definition: pybind_wrapper_test_script.py:38
gtsam::LieGroup::compose
Class compose(const Class &g) const
Definition: Lie.h:48
gtsam::SmartProjectionFactor::equals
bool equals(const NonlinearFactor &p, double tol=1e-9) const override
equals
Definition: SmartProjectionFactor.h:114
gtsam::SmartFactorBase::measured
const ZVector & measured() const
Return the 2D measurements (ZDim, in general).
Definition: SmartFactorBase.h:164
gtsam::SmartFactorBase::Fs
FBlocks Fs
Definition: SmartFactorBase.h:86
gtsam::Matrix
Eigen::MatrixXd Matrix
Definition: base/Matrix.h:39
gtsam::SmartProjectionFactor::params_
SmartProjectionParams params_
Definition: SmartProjectionFactor.h:57
different_sigmas::values
HybridValues values
Definition: testHybridBayesNet.cpp:247
gtsam::SmartProjectionRigFactor::nonUniqueKeys
const KeyVector & nonUniqueKeys() const
Definition: SmartProjectionRigFactor.h:192
gtsam::Factor
Definition: Factor.h:70
vanilla::params
static const SmartProjectionParams params
Definition: smartFactorScenarios.h:69
gtsam::SmartProjectionFactor::totalReprojectionError
double totalReprojectionError(const Cameras &cameras, std::optional< Point3 > externalPoint={}) const
Definition: SmartProjectionFactor.h:411
gtsam::SmartProjectionRigFactor::SmartProjectionRigFactor
SmartProjectionRigFactor()
Default constructor, only for serialization.
Definition: SmartProjectionRigFactor.h:108
gtsam::Vector
Eigen::VectorXd Vector
Definition: Vector.h:39
gtsam::KeyVector
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:91
gtsam::SmartProjectionFactor::triangulateSafe
TriangulationResult triangulateSafe(const Cameras &cameras) const
Call gtsam::triangulateSafe iff we need to re-triangulate.
Definition: SmartProjectionFactor.h:173
gtsam::SmartProjectionRigFactor::add
void add(const MEASUREMENTS &measurements, const KeyVector &poseKeys, const FastVector< size_t > &cameraIds=FastVector< size_t >())
Definition: SmartProjectionRigFactor.h:170
gtsam::CameraSet
A set of cameras, all with their own calibration.
Definition: CameraSet.h:37
gtsam::SmartProjectionRigFactor::Cameras
CameraSet< CAMERA > Cameras
Definition: SmartProjectionRigFactor.h:102
poseKey
const gtsam::Key poseKey
Definition: testPoseRotationPrior.cpp:29
gtsam::SmartProjectionRigFactor::cameraRig
const std::shared_ptr< Cameras > & cameraRig() const
return the calibration object
Definition: SmartProjectionRigFactor.h:195
gtsam::DefaultKeyFormatter
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition: Key.cpp:30
gtsam::SmartProjectionFactor::Cameras
CameraSet< CAMERA > Cameras
Definition: SmartProjectionFactor.h:74
gtsam::SmartProjectionParams::degeneracyMode
DegeneracyMode degeneracyMode
How to linearize the factor.
Definition: SmartFactorParams.h:45
gtsam::ZERO_ON_DEGENERACY
@ ZERO_ON_DEGENERACY
Definition: SmartFactorParams.h:36
simulated2D::Measurement
GenericMeasurement< Point2, Point2 > Measurement
Definition: simulated2D.h:278
gtsam::SmartProjectionRigFactor::shared_ptr
std::shared_ptr< This > shared_ptr
shorthand for a smart pointer to a factor
Definition: SmartProjectionRigFactor.h:105
gtsam::SmartProjectionParams::linearizationMode
LinearizationMode linearizationMode
How to linearize the factor.
Definition: SmartFactorParams.h:44
gtsam::SmartProjectionFactor::result_
TriangulationResult result_
result from triangulateSafe
Definition: SmartProjectionFactor.h:62
gtsam::SmartProjectionRigFactor::This
SmartProjectionRigFactor< CAMERA > This
Definition: SmartProjectionRigFactor.h:79
gtsam::KeyFormatter
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
gtsam::Pose3
Definition: Pose3.h:37
gtsam::SmartFactorBase::noiseModel_
SharedIsotropic noiseModel_
Definition: SmartFactorBase.h:73
gtsam::Pose3::inverse
Pose3 inverse() const
inverse transformation with derivatives
Definition: Pose3.cpp:61
gtsam::SmartProjectionRigFactor::MEASUREMENTS
CAMERA::MeasurementVector MEASUREMENTS
Definition: SmartProjectionRigFactor.h:82
gtsam::SmartProjectionRigFactor::CALIBRATION
CAMERA::CalibrationType CALIBRATION
Definition: SmartProjectionRigFactor.h:80
gtsam::SmartProjectionRigFactor::cameraIds_
FastVector< size_t > cameraIds_
Definition: SmartProjectionRigFactor.h:96
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
gtsam::SharedNoiseModel
noiseModel::Base::shared_ptr SharedNoiseModel
Definition: NoiseModel.h:762
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW
Definition: Memory.h:841
gtsam::SmartProjectionRigFactor::cameraIds
const FastVector< size_t > & cameraIds() const
return the calibration object
Definition: SmartProjectionRigFactor.h:198
gtsam::SmartProjectionRigFactor::Base
SmartProjectionFactor< CAMERA > Base
Definition: SmartProjectionRigFactor.h:78
lambda
static double lambda[]
Definition: jv.c:524
gtsam::SmartProjectionFactor::print
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
Definition: SmartProjectionFactor.h:102
E
DiscreteKey E(5, 2)
estimation_fixture::measurements
std::vector< double > measurements
Definition: testHybridEstimation.cpp:52
gtsam::SmartFactorBase::FBlocks
std::vector< MatrixZD, Eigen::aligned_allocator< MatrixZD > > FBlocks
Definition: SmartFactorBase.h:64
gtsam::SmartProjectionRigFactor::SmartProjectionRigFactor
SmartProjectionRigFactor(const SharedNoiseModel &sharedNoiseModel, const std::shared_ptr< Cameras > &cameraRig, const SmartProjectionParams &params=SmartProjectionParams())
Definition: SmartProjectionRigFactor.h:118
gtsam::b
const G & b
Definition: Group.h:79
gtsam::SmartProjectionFactor
Definition: SmartProjectionFactor.h:44
gtsam::SmartProjectionRigFactor::computeJacobiansWithTriangulatedPoint
void computeJacobiansWithTriangulatedPoint(typename Base::FBlocks &Fs, Matrix &E, Vector &b, const Cameras &cameras) const
Definition: SmartProjectionRigFactor.h:268
gtsam::SmartProjectionRigFactor::linearize
std::shared_ptr< GaussianFactor > linearize(const Values &values) const override
linearize
Definition: SmartProjectionRigFactor.h:370
gtsam::SmartProjectionRigFactor::MEASUREMENT
CAMERA::Measurement MEASUREMENT
Definition: SmartProjectionRigFactor.h:81
gtsam
traits
Definition: ABC.h:17
gtsam::Testable
Definition: Testable.h:152
gtsam::SmartProjectionRigFactor::cameraRig_
std::shared_ptr< typename Base::Cameras > cameraRig_
cameras in the rig (fixed poses wrt body and intrinsics, for each camera)
Definition: SmartProjectionRigFactor.h:92
gtsam::Factor::keys_
KeyVector keys_
The keys involved in this factor.
Definition: Factor.h:88
gtsam::traits
Definition: Group.h:36
gtsam::SmartProjectionRigFactor::add
void add(const MEASUREMENT &measured, const Key &poseKey, const size_t &cameraId=0)
Definition: SmartProjectionRigFactor.h:144
gtsam::Values
Definition: Values.h:65
gtsam::NonlinearFactor
Definition: NonlinearFactor.h:69
gtsam::SmartProjectionRigFactor::nonUniqueKeys_
KeyVector nonUniqueKeys_
vector of keys (one for each observation) with potentially repeated keys
Definition: SmartProjectionRigFactor.h:89
p
float * p
Definition: Tutorial_Map_using.cpp:9
v
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
gtsam::SmartProjectionRigFactor::createHessianFactor
std::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: SmartProjectionRigFactor.h:286
gtsam::SmartProjectionFactor::Camera
CAMERA Camera
shorthand for a set of cameras
Definition: SmartProjectionFactor.h:73
P
static double P[]
Definition: ellpe.c:68
gtsam::tol
const G double tol
Definition: Group.h:79
gtsam::SmartProjectionRigFactor::cameras
Base::Cameras cameras(const Values &values) const override
Definition: SmartProjectionRigFactor.h:233
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition: 3rdparty/Eigen/Eigen/src/Core/Matrix.h:178
gtsam::SmartProjectionRigFactor::ZDim
static const int ZDim
Measurement dimension.
Definition: SmartProjectionRigFactor.h:85
Base
Definition: test_virtual_functions.cpp:156
gtsam::SmartProjectionRigFactor::error
double error(const Values &values) const override
Definition: SmartProjectionRigFactor.h:251
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:97
gtsam::SymmetricBlockMatrix
Definition: SymmetricBlockMatrix.h:53
gtsam::SmartProjectionRigFactor::DimPose
static const int DimPose
Pose3 dimension.
Definition: SmartProjectionRigFactor.h:84
gtsam::NonlinearFactor::active
virtual bool active(const Values &c) const
Definition: NonlinearFactor.h:142
gtsam::SmartProjectionRigFactor::print
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
Definition: SmartProjectionRigFactor.h:205
gtsam::equal
bool equal(const T &obj1, const T &obj2, double tol)
Definition: Testable.h:85
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
gtsam::SmartProjectionRigFactor
Definition: SmartProjectionRigFactor.h:76
gtsam::SmartFactorBase::measured_
ZVector measured_
Definition: SmartFactorBase.h:80


gtsam
Author(s):
autogenerated on Wed May 28 2025 03:03:21