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<std::shared_ptr<Cal3_S2Stereo>> K_all_;
48
51
54
55 public:
58
61
63 typedef std::shared_ptr<This> shared_ptr;
64
65 static const int DimBlock = 12;
66 static const int DimPose = 6;
67 static const int ZDim = 3;
68 typedef Eigen::Matrix<double, ZDim, DimBlock> MatrixZD; // F blocks (derivatives wrt camera)
69 typedef std::vector<MatrixZD, Eigen::aligned_allocator<MatrixZD> > FBlocks; // vector of F blocks
70
76 SmartStereoProjectionFactorPP(const SharedNoiseModel& sharedNoiseModel,
77 const SmartStereoProjectionParams& params =
78 SmartStereoProjectionParams());
79
88 void add(const StereoPoint2& measured, const Key& world_P_body_key,
89 const Key& body_P_cam_key,
90 const std::shared_ptr<Cal3_S2Stereo>& K);
91
101 void add(const std::vector<StereoPoint2>& measurements,
102 const KeyVector& w_P_body_keys, const KeyVector& body_P_cam_keys,
103 const std::vector<std::shared_ptr<Cal3_S2Stereo>>& Ks);
104
115 void add(const std::vector<StereoPoint2>& measurements,
116 const KeyVector& w_P_body_keys, const KeyVector& body_P_cam_keys,
117 const std::shared_ptr<Cal3_S2Stereo>& K);
118
124 void print(const std::string& s = "", const KeyFormatter& keyFormatter =
125 DefaultKeyFormatter) const override;
126
128 bool equals(const NonlinearFactor& p, double tol = 1e-9) const override;
129
132 return body_P_cam_keys_;
133 }
134
138 double error(const Values& values) const override;
139
141 inline std::vector<std::shared_ptr<Cal3_S2Stereo>> calibration() const {
142 return K_all_;
143 }
144
152 Base::Cameras cameras(const Values& values) const override;
153
163 FBlocks& Fs, Matrix& E, Vector& b, const Values& values) const {
164 if (!result_) {
165 throw("computeJacobiansWithTriangulatedPoint");
166 } else { // valid result: compute jacobians
167 size_t numViews = measured_.size();
168 E = Matrix::Zero(3 * numViews, 3); // a StereoPoint2 for each view (point jacobian)
169 b = Vector::Zero(3 * numViews); // a StereoPoint2 for each view
170 Matrix dPoseCam_dPoseBody_i, dPoseCam_dPoseExt_i, dProject_dPoseCam_i, Ei;
171
172 for (size_t i = 0; i < numViews; i++) { // for each camera/measurement
173 Pose3 w_P_body = values.at<Pose3>(world_P_body_keys_.at(i));
174 Pose3 body_P_cam = values.at<Pose3>(body_P_cam_keys_.at(i));
175 StereoCamera camera(
176 w_P_body.compose(body_P_cam, dPoseCam_dPoseBody_i, dPoseCam_dPoseExt_i),
177 K_all_[i]);
178 // get jacobians and error vector for current measurement
179 StereoPoint2 reprojectionError_i = StereoPoint2(
180 camera.project(*result_, dProject_dPoseCam_i, Ei) - measured_.at(i));
181 Eigen::Matrix<double, ZDim, DimBlock> J; // 3 x 12
182 J.block<ZDim, 6>(0, 0) = dProject_dPoseCam_i * dPoseCam_dPoseBody_i; // (3x6) * (6x6)
183 J.block<ZDim, 6>(0, 6) = dProject_dPoseCam_i * dPoseCam_dPoseExt_i; // (3x6) * (6x6)
184 // if the right pixel is invalid, fix jacobians
185 if (std::isnan(measured_.at(i).uR()))
186 {
187 J.block<1, 12>(1, 0) = Matrix::Zero(1, 12);
188 Ei.block<1, 3>(1, 0) = Matrix::Zero(1, 3);
189 reprojectionError_i = StereoPoint2(reprojectionError_i.uL(), 0.0,
190 reprojectionError_i.v());
191 }
192 // fit into the output structures
193 Fs.push_back(J);
194 size_t row = 3 * i;
195 b.segment<ZDim>(row) = -reprojectionError_i.vector();
196 E.block<3, 3>(row, 0) = Ei;
197 }
198 }
199 }
200
202 std::shared_ptr<RegularHessianFactor<DimPose>> createHessianFactor(
203 const Values& values, const double lambda = 0.0,
204 bool diagonalDamping = false) const {
205 // we may have multiple cameras sharing the same extrinsic cals, hence the number
206 // of keys may be smaller than 2 * nrMeasurements (which is the upper bound where we
207 // have a body key and an extrinsic calibration key for each measurement)
208 size_t nrUniqueKeys = keys_.size();
209
210 // Create structures for Hessian Factors
211 KeyVector js;
212 std::vector<Matrix> Gs(nrUniqueKeys * (nrUniqueKeys + 1) / 2);
213 std::vector<Vector> gs(nrUniqueKeys);
214
215 if (this->measured_.size() != cameras(values).size())
216 throw std::runtime_error("SmartStereoProjectionHessianFactor: this->"
217 "measured_.size() inconsistent with input");
218
219 // triangulate 3D point at given linearization point
220 triangulateSafe(cameras(values));
221
222 // failed: return "empty/zero" Hessian
223 if (!result_) {
224 for (Matrix& m : Gs) m = Matrix::Zero(DimPose, DimPose);
225 for (Vector& v : gs) v = Vector::Zero(DimPose);
226 return std::make_shared<RegularHessianFactor<DimPose>>(keys_, Gs, gs,
227 0.0);
228 }
229
230 // compute Jacobian given triangulated 3D Point
231 FBlocks Fs;
232 Matrix F, E;
233 Vector b;
235
236 // Whiten using noise model
237 noiseModel_->WhitenSystem(E, b);
238 for (size_t i = 0; i < Fs.size(); i++) {
239 Fs[i] = noiseModel_->Whiten(Fs[i]);
240 }
241
242 // build augmented Hessian (with last row/column being the information vector)
243 Matrix3 P;
244 Cameras::ComputePointCovariance<3>(P, E, lambda, diagonalDamping);
245
246 // these are the keys that correspond to the blocks in augmentedHessian (output of SchurComplement)
247 KeyVector nonuniqueKeys;
248 for (size_t i = 0; i < world_P_body_keys_.size(); i++) {
249 nonuniqueKeys.push_back(world_P_body_keys_.at(i));
250 nonuniqueKeys.push_back(body_P_cam_keys_.at(i));
251 }
252 // but we need to get the augumented hessian wrt the unique keys in key_
253 SymmetricBlockMatrix augmentedHessianUniqueKeys =
254 Base::Cameras::template SchurComplementAndRearrangeBlocks<3, DimBlock,
255 DimPose>(
256 Fs, E, P, b, nonuniqueKeys, keys_);
257
258 return std::make_shared<RegularHessianFactor<DimPose>>(
259 keys_, augmentedHessianUniqueKeys);
260 }
261
267 std::shared_ptr<GaussianFactor> linearizeDamped(
268 const Values& values, const double lambda = 0.0) const {
269 // depending on flag set on construction we may linearize to different linear factors
270 switch (params_.linearizationMode) {
271 case HESSIAN:
272 return createHessianFactor(values, lambda);
273 default:
274 throw std::runtime_error(
275 "SmartStereoProjectionFactorPP: unknown linearization mode");
276 }
277 }
278
280 std::shared_ptr<GaussianFactor> linearize(const Values& values) const
281 override {
282 return linearizeDamped(values);
283 }
284
285 private:
286#if GTSAM_ENABLE_BOOST_SERIALIZATION
288 friend class boost::serialization::access;
289 template<class ARCHIVE>
290 void serialize(ARCHIVE& ar, const unsigned int /*version*/) {
291 ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
292 ar & BOOST_SERIALIZATION_NVP(K_all_);
293 }
294#endif
295};
296// end of class declaration
297
299template<>
301 SmartStereoProjectionFactorPP> {
302};
303
304} // namespace gtsam
Smart stereo factor on StereoCameras (pose).
Global functions in a separate testing namespace.
Definition chartTesting.h:28
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition Key.cpp:30
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition Key.h:91
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:846
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:43
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition Group.h:37
This class stores a dense matrix and allows it to be accessed as a collection of blocks.
Definition SymmetricBlockMatrix.h:80
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:152
static void ComputePointCovariance(Eigen::Matrix< double, N, N > &P, const Matrix &E, double lambda, bool diagonalDamping=false)
Definition CameraSet.h:344
A 3D pose (R,t) : (Rot3,Point3).
Definition Pose3.h:42
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:34
Vector3 vector() const
convert to vector
Definition StereoPoint2.h:119
double uL() const
get uL
Definition StereoPoint2.h:110
double v() const
get v
Definition StereoPoint2.h:116
KeyVector keys_
The keys involved in this factor.
Definition Factor.h:88
size_t size() const
Definition Factor.h:160
Nonlinear factor base class.
Definition NonlinearFactor.h:70
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:260
SharedIsotropic noiseModel_
Definition SmartFactorBase.h:73
ZVector measured_
Definition SmartFactorBase.h:80
const ZVector & measured() const
Definition SmartFactorBase.h:162
TriangulationResult triangulateSafe(const Cameras &cameras) const
triangulateSafe
Definition SmartStereoProjectionFactor.h:167
TriangulationResult result_
result from triangulateSafe
Definition SmartStereoProjectionFactor.h:70
SmartStereoProjectionFactor(const SharedNoiseModel &sharedNoiseModel, const SmartStereoProjectionParams &params=SmartStereoProjectionParams(), const std::optional< Pose3 > body_P_sensor={})
Constructor.
Definition SmartStereoProjectionFactor.h:91
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:65
SmartStereoProjectionFactorPP This
shorthand for this class
Definition SmartStereoProjectionFactorPP.h:60
std::vector< std::shared_ptr< Cal3_S2Stereo > > K_all_
shared pointer to calibration object (one for each camera)
Definition SmartStereoProjectionFactorPP.h:47
static const int ZDim
Measurement dimension (for a StereoPoint2 measurement).
Definition SmartStereoProjectionFactorPP.h:67
std::shared_ptr< This > shared_ptr
shorthand for a smart pointer to a factor
Definition SmartStereoProjectionFactorPP.h:63
bool equals(const NonlinearFactor &p, double tol=1e-9) const override
equals
Definition SmartStereoProjectionFactorPP.cpp:96
std::shared_ptr< GaussianFactor > linearize(const Values &values) const override
linearize
Definition SmartStereoProjectionFactorPP.h:280
SmartStereoProjectionFactorPP(const SharedNoiseModel &sharedNoiseModel, const SmartStereoProjectionParams &params=SmartStereoProjectionParams())
Constructor.
Definition SmartStereoProjectionFactorPP.cpp:25
void add(const StereoPoint2 &measured, const Key &world_P_body_key, const Key &body_P_cam_key, const std::shared_ptr< Cal3_S2Stereo > &K)
add a new measurement, with a pose key, and an extrinsic pose key
Definition SmartStereoProjectionFactorPP.cpp:30
const KeyVector & getExtrinsicPoseKeys() const
equals
Definition SmartStereoProjectionFactorPP.h:131
std::vector< std::shared_ptr< Cal3_S2Stereo > > calibration() const
return the calibration object
Definition SmartStereoProjectionFactorPP.h:141
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 SmartStereoProjectionFactorPP.h:202
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:162
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
std::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:267
Base::Cameras cameras(const Values &values) const override
Collect all cameras involved in this factor.
Definition SmartStereoProjectionFactorPP.cpp:114
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
print
Definition SmartStereoProjectionFactorPP.cpp:86
SmartStereoProjectionFactor Base
shorthand for base class type
Definition SmartStereoProjectionFactorPP.h:57
static const int DimPose
Pose3 dimension.
Definition SmartStereoProjectionFactorPP.h:66