gtsam 4.1.1
gtsam
SmartStereoProjectionFactorPP.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
22
23namespace gtsam {
44 protected:
46 std::vector<boost::shared_ptr<Cal3_S2Stereo>> K_all_;
47
50
53
54 public:
55 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
56
59
62
64 typedef boost::shared_ptr<This> shared_ptr;
65
66 static const int DimBlock = 12;
67 static const int DimPose = 6;
68 static const int ZDim = 3;
69 typedef Eigen::Matrix<double, ZDim, DimBlock> MatrixZD; // F blocks (derivatives wrt camera)
70 typedef std::vector<MatrixZD, Eigen::aligned_allocator<MatrixZD> > FBlocks; // vector of F blocks
71
77 SmartStereoProjectionFactorPP(const SharedNoiseModel& sharedNoiseModel,
78 const SmartStereoProjectionParams& params =
80
82 ~SmartStereoProjectionFactorPP() override = default;
83
92 void add(const StereoPoint2& measured, const Key& world_P_body_key,
93 const Key& body_P_cam_key,
94 const boost::shared_ptr<Cal3_S2Stereo>& K);
95
105 void add(const std::vector<StereoPoint2>& measurements,
106 const KeyVector& w_P_body_keys, const KeyVector& body_P_cam_keys,
107 const std::vector<boost::shared_ptr<Cal3_S2Stereo>>& Ks);
108
119 void add(const std::vector<StereoPoint2>& measurements,
120 const KeyVector& w_P_body_keys, const KeyVector& body_P_cam_keys,
121 const boost::shared_ptr<Cal3_S2Stereo>& K);
122
128 void print(const std::string& s = "", const KeyFormatter& keyFormatter =
129 DefaultKeyFormatter) const override;
130
132 bool equals(const NonlinearFactor& p, double tol = 1e-9) const override;
133
136 return body_P_cam_keys_;
137 }
138
142 double error(const Values& values) const override;
143
145 inline std::vector<boost::shared_ptr<Cal3_S2Stereo>> calibration() const {
146 return K_all_;
147 }
148
156 Base::Cameras cameras(const Values& values) const override;
157
167 FBlocks& Fs, Matrix& E, Vector& b, const Values& values) const {
168 if (!result_) {
169 throw("computeJacobiansWithTriangulatedPoint");
170 } else { // valid result: compute jacobians
171 size_t numViews = measured_.size();
172 E = Matrix::Zero(3 * numViews, 3); // a StereoPoint2 for each view (point jacobian)
173 b = Vector::Zero(3 * numViews); // a StereoPoint2 for each view
174 Matrix dPoseCam_dPoseBody_i, dPoseCam_dPoseExt_i, dProject_dPoseCam_i, Ei;
175
176 for (size_t i = 0; i < numViews; i++) { // for each camera/measurement
177 Pose3 w_P_body = values.at<Pose3>(world_P_body_keys_.at(i));
178 Pose3 body_P_cam = values.at<Pose3>(body_P_cam_keys_.at(i));
179 StereoCamera camera(
180 w_P_body.compose(body_P_cam, dPoseCam_dPoseBody_i, dPoseCam_dPoseExt_i),
181 K_all_[i]);
182 // get jacobians and error vector for current measurement
183 StereoPoint2 reprojectionError_i = StereoPoint2(
184 camera.project(*result_, dProject_dPoseCam_i, Ei) - measured_.at(i));
185 Eigen::Matrix<double, ZDim, DimBlock> J; // 3 x 12
186 J.block<ZDim, 6>(0, 0) = dProject_dPoseCam_i * dPoseCam_dPoseBody_i; // (3x6) * (6x6)
187 J.block<ZDim, 6>(0, 6) = dProject_dPoseCam_i * dPoseCam_dPoseExt_i; // (3x6) * (6x6)
188 // if the right pixel is invalid, fix jacobians
189 if (std::isnan(measured_.at(i).uR()))
190 {
191 J.block<1, 12>(1, 0) = Matrix::Zero(1, 12);
192 Ei.block<1, 3>(1, 0) = Matrix::Zero(1, 3);
193 reprojectionError_i = StereoPoint2(reprojectionError_i.uL(), 0.0,
194 reprojectionError_i.v());
195 }
196 // fit into the output structures
197 Fs.push_back(J);
198 size_t row = 3 * i;
199 b.segment<ZDim>(row) = -reprojectionError_i.vector();
200 E.block<3, 3>(row, 0) = Ei;
201 }
202 }
203 }
204
206 boost::shared_ptr<RegularHessianFactor<DimPose> > createHessianFactor(
207 const Values& values, const double lambda = 0.0, bool diagonalDamping =
208 false) const {
209
210 // we may have multiple cameras sharing the same extrinsic cals, hence the number
211 // of keys may be smaller than 2 * nrMeasurements (which is the upper bound where we
212 // have a body key and an extrinsic calibration key for each measurement)
213 size_t nrUniqueKeys = keys_.size();
214
215 // Create structures for Hessian Factors
216 KeyVector js;
217 std::vector < Matrix > Gs(nrUniqueKeys * (nrUniqueKeys + 1) / 2);
218 std::vector<Vector> gs(nrUniqueKeys);
219
220 if (this->measured_.size() != cameras(values).size())
221 throw std::runtime_error("SmartStereoProjectionHessianFactor: this->"
222 "measured_.size() inconsistent with input");
223
224 // triangulate 3D point at given linearization point
225 triangulateSafe(cameras(values));
226
227 if (!result_) { // failed: return "empty/zero" Hessian
228 for (Matrix& m : Gs)
229 m = Matrix::Zero(DimPose, DimPose);
230 for (Vector& v : gs)
231 v = Vector::Zero(DimPose);
232 return boost::make_shared < RegularHessianFactor<DimPose>
233 > (keys_, Gs, gs, 0.0);
234 }
235
236 // compute Jacobian given triangulated 3D Point
237 FBlocks Fs;
238 Matrix F, E;
239 Vector b;
241
242 // Whiten using noise model
243 noiseModel_->WhitenSystem(E, b);
244 for (size_t i = 0; i < Fs.size(); i++)
245 Fs[i] = noiseModel_->Whiten(Fs[i]);
246
247 // build augmented Hessian (with last row/column being the information vector)
248 Matrix3 P;
249 Cameras::ComputePointCovariance <3> (P, E, lambda, diagonalDamping);
250
251 // these are the keys that correspond to the blocks in augmentedHessian (output of SchurComplement)
252 KeyVector nonuniqueKeys;
253 for (size_t i = 0; i < world_P_body_keys_.size(); i++) {
254 nonuniqueKeys.push_back(world_P_body_keys_.at(i));
255 nonuniqueKeys.push_back(body_P_cam_keys_.at(i));
256 }
257 // but we need to get the augumented hessian wrt the unique keys in key_
258 SymmetricBlockMatrix augmentedHessianUniqueKeys =
259 Cameras::SchurComplementAndRearrangeBlocks<3,DimBlock,DimPose>(Fs,E,P,b,
260 nonuniqueKeys, keys_);
261
262 return boost::make_shared < RegularHessianFactor<DimPose>
263 > (keys_, augmentedHessianUniqueKeys);
264 }
265
271 boost::shared_ptr<GaussianFactor> linearizeDamped(
272 const Values& values, const double lambda = 0.0) const {
273 // depending on flag set on construction we may linearize to different linear factors
274 switch (params_.linearizationMode) {
275 case HESSIAN:
276 return createHessianFactor(values, lambda);
277 default:
278 throw std::runtime_error(
279 "SmartStereoProjectionFactorPP: unknown linearization mode");
280 }
281 }
282
284 boost::shared_ptr<GaussianFactor> linearize(const Values& values) const
285 override {
286 return linearizeDamped(values);
287 }
288
289 private:
292 template<class ARCHIVE>
293 void serialize(ARCHIVE& ar, const unsigned int /*version*/) {
294 ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
295 ar & BOOST_SERIALIZATION_NVP(K_all_);
296 }
297
298};
299// end of class declaration
300
302template<>
304 SmartStereoProjectionFactorPP> {
305};
306
307} // namespace gtsam
Smart stereo factor on StereoCameras (pose)
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
noiseModel::Base::shared_ptr SharedNoiseModel
Note, deliberately not in noiseModel namespace.
Definition: NoiseModel.h:736
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
Definition: Pose3.h:37
Definition: StereoCamera.h:47
Definition: StereoPoint2.h:32
Vector3 vector() const
convert to vector
Definition: StereoPoint2.h:115
double uL() const
get uL
Definition: StereoPoint2.h:106
double v() const
get v
Definition: StereoPoint2.h:112
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
size_t size() const
Definition: Factor.h:136
Nonlinear factor base class.
Definition: NonlinearFactor.h:43
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
SmartStereoProjectionFactor: triangulates point and keeps an estimate of it around.
Definition: SmartStereoProjectionFactor.h:52
TriangulationResult triangulateSafe(const Cameras &cameras) const
triangulateSafe
Definition: SmartStereoProjectionFactor.h:163
TriangulationResult result_
result from triangulateSafe
Definition: SmartStereoProjectionFactor.h:66
Definition: SmartStereoProjectionFactorPP.h:43
static const int DimBlock
Camera dimension: 6 for body pose, 6 for extrinsic pose.
Definition: SmartStereoProjectionFactorPP.h:66
SmartStereoProjectionFactorPP This
shorthand for this class
Definition: SmartStereoProjectionFactorPP.h:61
static const int ZDim
Measurement dimension (for a StereoPoint2 measurement)
Definition: SmartStereoProjectionFactorPP.h:68
boost::shared_ptr< This > shared_ptr
shorthand for a smart pointer to a factor
Definition: SmartStereoProjectionFactorPP.h:64
bool equals(const NonlinearFactor &p, double tol=1e-9) const override
equals
Definition: SmartStereoProjectionFactorPP.cpp:94
double error(const Values &values) const override
error calculates the error of the factor.
Definition: SmartStereoProjectionFactorPP.cpp:103
SmartStereoProjectionFactorPP(const SharedNoiseModel &sharedNoiseModel, const SmartStereoProjectionParams &params=SmartStereoProjectionParams())
Constructor.
Definition: SmartStereoProjectionFactorPP.cpp:23
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: SmartStereoProjectionFactorPP.h:206
const KeyVector & getExtrinsicPoseKeys() const
equals
Definition: SmartStereoProjectionFactorPP.h:135
~SmartStereoProjectionFactorPP() override=default
Virtual destructor.
std::vector< boost::shared_ptr< Cal3_S2Stereo > > calibration() const
return the calibration object
Definition: SmartStereoProjectionFactorPP.h:145
KeyVector world_P_body_keys_
The keys corresponding to the pose of the body (with respect to an external world frame) for each vie...
Definition: SmartStereoProjectionFactorPP.h:49
void computeJacobiansAndCorrectForMissingMeasurements(FBlocks &Fs, Matrix &E, Vector &b, const Values &values) const
Compute jacobian F, E and error vector at a given linearization point.
Definition: SmartStereoProjectionFactorPP.h:166
KeyVector body_P_cam_keys_
The keys corresponding to the extrinsic pose calibration for each view (pose that transform from came...
Definition: SmartStereoProjectionFactorPP.h:52
void add(const StereoPoint2 &measured, const Key &world_P_body_key, const Key &body_P_cam_key, const boost::shared_ptr< Cal3_S2Stereo > &K)
add a new measurement, with a pose key, and an extrinsic pose key
Definition: SmartStereoProjectionFactorPP.cpp:28
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef SmartStereoProjectionFactor Base
shorthand for base class type
Definition: SmartStereoProjectionFactorPP.h:58
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: SmartStereoProjectionFactorPP.h:271
boost::shared_ptr< GaussianFactor > linearize(const Values &values) const override
linearize
Definition: SmartStereoProjectionFactorPP.h:284
Base::Cameras cameras(const Values &values) const override
Collect all cameras involved in this factor.
Definition: SmartStereoProjectionFactorPP.cpp:112
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
print
Definition: SmartStereoProjectionFactorPP.cpp:84
friend class boost::serialization::access
Serialization function.
Definition: SmartStereoProjectionFactorPP.h:291
static const int DimPose
Pose3 dimension.
Definition: SmartStereoProjectionFactorPP.h:67
std::vector< boost::shared_ptr< Cal3_S2Stereo > > K_all_
shared pointer to calibration object (one for each camera)
Definition: SmartStereoProjectionFactorPP.h:46