gtsam 4.2
gtsam
Loading...
Searching...
No Matches
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
18
19#pragma once
20
22
23namespace gtsam {
34
43class GTSAM_UNSTABLE_EXPORT SmartStereoProjectionFactorPP
45 protected:
47 std::vector<boost::shared_ptr<Cal3_S2Stereo>> K_all_;
48
51
54
55 public:
56 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
57
60
63
65 typedef boost::shared_ptr<This> shared_ptr;
66
67 static const int DimBlock = 12;
68 static const int DimPose = 6;
69 static const int ZDim = 3;
70 typedef Eigen::Matrix<double, ZDim, DimBlock> MatrixZD; // F blocks (derivatives wrt camera)
71 typedef std::vector<MatrixZD, Eigen::aligned_allocator<MatrixZD> > FBlocks; // vector of F blocks
72
78 SmartStereoProjectionFactorPP(const SharedNoiseModel& sharedNoiseModel,
79 const SmartStereoProjectionParams& params =
80 SmartStereoProjectionParams());
81
83 ~SmartStereoProjectionFactorPP() override = default;
84
93 void add(const StereoPoint2& measured, const Key& world_P_body_key,
94 const Key& body_P_cam_key,
95 const boost::shared_ptr<Cal3_S2Stereo>& K);
96
106 void add(const std::vector<StereoPoint2>& measurements,
107 const KeyVector& w_P_body_keys, const KeyVector& body_P_cam_keys,
108 const std::vector<boost::shared_ptr<Cal3_S2Stereo>>& Ks);
109
120 void add(const std::vector<StereoPoint2>& measurements,
121 const KeyVector& w_P_body_keys, const KeyVector& body_P_cam_keys,
122 const boost::shared_ptr<Cal3_S2Stereo>& K);
123
129 void print(const std::string& s = "", const KeyFormatter& keyFormatter =
130 DefaultKeyFormatter) const override;
131
133 bool equals(const NonlinearFactor& p, double tol = 1e-9) const override;
134
137 return body_P_cam_keys_;
138 }
139
143 double error(const Values& values) const override;
144
146 inline std::vector<boost::shared_ptr<Cal3_S2Stereo>> calibration() const {
147 return K_all_;
148 }
149
157 Base::Cameras cameras(const Values& values) const override;
158
168 FBlocks& Fs, Matrix& E, Vector& b, const Values& values) const {
169 if (!result_) {
170 throw("computeJacobiansWithTriangulatedPoint");
171 } else { // valid result: compute jacobians
172 size_t numViews = measured_.size();
173 E = Matrix::Zero(3 * numViews, 3); // a StereoPoint2 for each view (point jacobian)
174 b = Vector::Zero(3 * numViews); // a StereoPoint2 for each view
175 Matrix dPoseCam_dPoseBody_i, dPoseCam_dPoseExt_i, dProject_dPoseCam_i, Ei;
176
177 for (size_t i = 0; i < numViews; i++) { // for each camera/measurement
178 Pose3 w_P_body = values.at<Pose3>(world_P_body_keys_.at(i));
179 Pose3 body_P_cam = values.at<Pose3>(body_P_cam_keys_.at(i));
180 StereoCamera camera(
181 w_P_body.compose(body_P_cam, dPoseCam_dPoseBody_i, dPoseCam_dPoseExt_i),
182 K_all_[i]);
183 // get jacobians and error vector for current measurement
184 StereoPoint2 reprojectionError_i = StereoPoint2(
185 camera.project(*result_, dProject_dPoseCam_i, Ei) - measured_.at(i));
186 Eigen::Matrix<double, ZDim, DimBlock> J; // 3 x 12
187 J.block<ZDim, 6>(0, 0) = dProject_dPoseCam_i * dPoseCam_dPoseBody_i; // (3x6) * (6x6)
188 J.block<ZDim, 6>(0, 6) = dProject_dPoseCam_i * dPoseCam_dPoseExt_i; // (3x6) * (6x6)
189 // if the right pixel is invalid, fix jacobians
190 if (std::isnan(measured_.at(i).uR()))
191 {
192 J.block<1, 12>(1, 0) = Matrix::Zero(1, 12);
193 Ei.block<1, 3>(1, 0) = Matrix::Zero(1, 3);
194 reprojectionError_i = StereoPoint2(reprojectionError_i.uL(), 0.0,
195 reprojectionError_i.v());
196 }
197 // fit into the output structures
198 Fs.push_back(J);
199 size_t row = 3 * i;
200 b.segment<ZDim>(row) = -reprojectionError_i.vector();
201 E.block<3, 3>(row, 0) = Ei;
202 }
203 }
204 }
205
207 boost::shared_ptr<RegularHessianFactor<DimPose> > createHessianFactor(
208 const Values& values, const double lambda = 0.0, bool diagonalDamping =
209 false) const {
210
211 // we may have multiple cameras sharing the same extrinsic cals, hence the number
212 // of keys may be smaller than 2 * nrMeasurements (which is the upper bound where we
213 // have a body key and an extrinsic calibration key for each measurement)
214 size_t nrUniqueKeys = keys_.size();
215
216 // Create structures for Hessian Factors
217 KeyVector js;
218 std::vector < Matrix > Gs(nrUniqueKeys * (nrUniqueKeys + 1) / 2);
219 std::vector<Vector> gs(nrUniqueKeys);
220
221 if (this->measured_.size() != cameras(values).size())
222 throw std::runtime_error("SmartStereoProjectionHessianFactor: this->"
223 "measured_.size() inconsistent with input");
224
225 // triangulate 3D point at given linearization point
226 triangulateSafe(cameras(values));
227
228 if (!result_) { // failed: return "empty/zero" Hessian
229 for (Matrix& m : Gs)
230 m = Matrix::Zero(DimPose, DimPose);
231 for (Vector& v : gs)
232 v = Vector::Zero(DimPose);
233 return boost::make_shared < RegularHessianFactor<DimPose>
234 > (keys_, Gs, gs, 0.0);
235 }
236
237 // compute Jacobian given triangulated 3D Point
238 FBlocks Fs;
239 Matrix F, E;
240 Vector b;
242
243 // Whiten using noise model
244 noiseModel_->WhitenSystem(E, b);
245 for (size_t i = 0; i < Fs.size(); i++)
246 Fs[i] = noiseModel_->Whiten(Fs[i]);
247
248 // build augmented Hessian (with last row/column being the information vector)
249 Matrix3 P;
250 Cameras::ComputePointCovariance <3> (P, E, lambda, diagonalDamping);
251
252 // these are the keys that correspond to the blocks in augmentedHessian (output of SchurComplement)
253 KeyVector nonuniqueKeys;
254 for (size_t i = 0; i < world_P_body_keys_.size(); i++) {
255 nonuniqueKeys.push_back(world_P_body_keys_.at(i));
256 nonuniqueKeys.push_back(body_P_cam_keys_.at(i));
257 }
258 // but we need to get the augumented hessian wrt the unique keys in key_
259 SymmetricBlockMatrix augmentedHessianUniqueKeys =
261 nonuniqueKeys, keys_);
262
263 return boost::make_shared < RegularHessianFactor<DimPose>
264 > (keys_, augmentedHessianUniqueKeys);
265 }
266
272 boost::shared_ptr<GaussianFactor> linearizeDamped(
273 const Values& values, const double lambda = 0.0) const {
274 // depending on flag set on construction we may linearize to different linear factors
275 switch (params_.linearizationMode) {
276 case HESSIAN:
277 return createHessianFactor(values, lambda);
278 default:
279 throw std::runtime_error(
280 "SmartStereoProjectionFactorPP: unknown linearization mode");
281 }
282 }
283
285 boost::shared_ptr<GaussianFactor> linearize(const Values& values) const
286 override {
287 return linearizeDamped(values);
288 }
289
290 private:
292 friend class boost::serialization::access;
293 template<class ARCHIVE>
294 void serialize(ARCHIVE& ar, const unsigned int /*version*/) {
295 ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
296 ar & BOOST_SERIALIZATION_NVP(K_all_);
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
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
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
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:100
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
static SymmetricBlockMatrix SchurComplementAndRearrangeBlocks(const std::vector< Eigen::Matrix< double, ZDim, ND >, Eigen::aligned_allocator< Eigen::Matrix< double, ZDim, ND > > > &Fs, const Matrix &E, const Eigen::Matrix< double, N, N > &P, const Vector &b, const KeyVector &jacobianKeys, const KeyVector &hessianKeys)
Definition CameraSet.h:218
A 3D pose (R,t) : (Rot3,Point3).
Definition Pose3.h:37
A stereo camera class, parameterize by left camera pose and stereo calibration.
Definition StereoCamera.h:47
StereoPoint2 project(const Point3 &point) const
Project 3D point to StereoPoint2 (uL,uR,v).
Definition StereoCamera.cpp:32
A 2D stereo point, v will be same for rectified images.
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
KeyVector keys_
The keys involved in this factor.
Definition Factor.h:85
size_t size() const
Definition Factor.h:157
Nonlinear factor base class.
Definition NonlinearFactor.h:42
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_
Definition SmartFactorBase.h:72
ZVector measured_
Definition SmartFactorBase.h:79
const ZVector & measured() const
Definition SmartFactorBase.h:159
TriangulationResult triangulateSafe(const Cameras &cameras) const
triangulateSafe
Definition SmartStereoProjectionFactor.h:164
TriangulationResult result_
result from triangulateSafe
Definition SmartStereoProjectionFactor.h:67
SmartStereoProjectionFactor(const SharedNoiseModel &sharedNoiseModel, const SmartStereoProjectionParams &params=SmartStereoProjectionParams(), const boost::optional< Pose3 > body_P_sensor=boost::none)
Constructor.
Definition SmartStereoProjectionFactor.h:88
If you are using the factor, please cite: L.
Definition SmartStereoProjectionFactorPP.h:44
static const int DimBlock
Camera dimension: 6 for body pose, 6 for extrinsic pose.
Definition SmartStereoProjectionFactorPP.h:67
SmartStereoProjectionFactorPP This
shorthand for this class
Definition SmartStereoProjectionFactorPP.h:62
static const int ZDim
Measurement dimension (for a StereoPoint2 measurement).
Definition SmartStereoProjectionFactorPP.h:69
boost::shared_ptr< This > shared_ptr
shorthand for a smart pointer to a factor
Definition SmartStereoProjectionFactorPP.h:65
bool equals(const NonlinearFactor &p, double tol=1e-9) const override
equals
Definition SmartStereoProjectionFactorPP.cpp:94
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:207
const KeyVector & getExtrinsicPoseKeys() const
equals
Definition SmartStereoProjectionFactorPP.h:136
~SmartStereoProjectionFactorPP() override=default
Virtual destructor.
std::vector< boost::shared_ptr< Cal3_S2Stereo > > calibration() const
return the calibration object
Definition SmartStereoProjectionFactorPP.h:146
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:50
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:167
KeyVector body_P_cam_keys_
The keys corresponding to the extrinsic pose calibration for each view (pose that transform from came...
Definition SmartStereoProjectionFactorPP.h:53
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:59
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:272
boost::shared_ptr< GaussianFactor > linearize(const Values &values) const override
linearize
Definition SmartStereoProjectionFactorPP.h:285
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
static const int DimPose
Pose3 dimension.
Definition SmartStereoProjectionFactorPP.h:68
std::vector< boost::shared_ptr< Cal3_S2Stereo > > K_all_
shared pointer to calibration object (one for each camera)
Definition SmartStereoProjectionFactorPP.h:47