23 #include <gtsam_unstable/dllexport.h> 44 template <
class CAMERA>
76 static const int DimPose = 6;
81 typedef std::vector<MatrixZD, Eigen::aligned_allocator<MatrixZD>>
102 const std::shared_ptr<Cameras>& cameraRig,
104 : Base(sharedNoiseModel,
params), cameraRig_(cameraRig) {
107 throw std::runtime_error(
108 "SmartProjectionRigFactor: " 109 "degeneracyMode must be set to ZERO_ON_DEGENERACY");
111 throw std::runtime_error(
112 "SmartProjectionRigFactor: " 113 "linearizationMode must be set to HESSIAN");
129 const Key& world_P_body_key2,
const double&
alpha,
130 const size_t& cameraId = 0) {
132 this->measured_.push_back(measured);
135 world_P_body_key_pairs_.push_back(
136 std::make_pair(world_P_body_key1, world_P_body_key2));
140 if (std::find(this->keys_.begin(), this->keys_.end(), world_P_body_key1) ==
142 this->keys_.push_back(world_P_body_key1);
143 if (std::find(this->keys_.begin(), this->keys_.end(), world_P_body_key2) ==
145 this->keys_.push_back(world_P_body_key2);
148 alphas_.push_back(alpha);
151 cameraIds_.push_back(cameraId);
167 void add(
const MEASUREMENTS& measurements,
168 const std::vector<std::pair<Key, Key>>& world_P_body_key_pairs,
169 const std::vector<double>& alphas,
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)) {
175 throw std::runtime_error(
176 "SmartProjectionPoseFactorRollingShutter: " 177 "trying to add inconsistent inputs");
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");
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
197 return world_P_body_key_pairs_;
201 const std::vector<double>&
alphas()
const {
return alphas_; }
204 const std::shared_ptr<Cameras>&
cameraRig()
const {
return cameraRig_; }
215 const std::string&
s =
"",
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");
237 double keyPairsEqual =
true;
238 if (this->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;
243 const Key key2own = world_P_body_key_pairs_[k].second;
245 if (!(key1own == key1e) || !(key2own == key2e)) {
246 keyPairsEqual =
false;
251 keyPairsEqual =
false;
254 return e && Base::equals(p,
tol) && alphas_ == e->
alphas() &&
256 std::equal(cameraIds_.begin(), cameraIds_.end(),
268 for (
size_t i = 0;
i < this->measured_.size();
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();
280 cameras.emplace_back(w_P_cam,
281 make_shared<typename CAMERA::CalibrationType>(
282 camera_i.calibration()));
291 if (this->active(values)) {
292 return this->totalReprojectionError(this->cameras(values));
309 if (!this->result_) {
310 throw(
"computeJacobiansWithTriangulatedPoint");
312 size_t numViews = this->measured_.size();
313 E = Matrix::Zero(2 * numViews,
315 b = Vector::Zero(2 * numViews);
319 dInterpPose_dPoseBody2, dPoseCam_dInterpPose;
322 for (
size_t i = 0;
i < numViews;
i++) {
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];
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);
334 w_P_cam, make_shared<typename CAMERA::CalibrationType>(
335 camera_i.calibration()));
338 Point2 reprojectionError_i = camera.reprojectionError(
339 *this->result_, this->measured_.at(
i), dProject_dPoseCam, Ei);
341 J.block(0, 0, ZDim, 6) =
342 dProject_dPoseCam * dPoseCam_dInterpPose *
343 dInterpPose_dPoseBody1;
344 J.block(0, 6, ZDim, 6) =
345 dProject_dPoseCam * dPoseCam_dInterpPose *
346 dInterpPose_dPoseBody2;
351 b.segment<ZDim>(
row) = -reprojectionError_i;
360 bool diagonalDamping =
false)
const {
364 size_t nrUniqueKeys =
372 std::vector<Matrix> Gs(nrUniqueKeys * (nrUniqueKeys + 1) / 2);
373 std::vector<Vector> gs(nrUniqueKeys);
375 if (this->measured_.size() !=
377 throw std::runtime_error(
378 "SmartProjectionPoseFactorRollingShutter: " 379 "measured_.size() inconsistent with input");
384 if (!this->result_) {
386 for (
Matrix&
m : Gs)
m = Matrix::Zero(DimPose, DimPose);
387 for (
Vector&
v : gs)
v = Vector::Zero(DimPose);
388 return std::make_shared<RegularHessianFactor<DimPose>>(this->keys_,
391 throw std::runtime_error(
392 "SmartProjectionPoseFactorRollingShutter: " 393 "only supported degeneracy mode is ZERO_ON_DEGENERACY");
400 this->computeJacobiansWithTriangulatedPoint(Fs, E, b, values);
403 this->noiseModel_->WhitenSystem(E, b);
404 for (
size_t i = 0;
i < Fs.size();
i++)
405 Fs[
i] = this->noiseModel_->Whiten(Fs[
i]);
407 Matrix3
P = Cameras::PointCov(E,
lambda, diagonalDamping);
412 for (
size_t i = 0; i < world_P_body_key_pairs_.size(); i++) {
413 nonuniqueKeys.push_back(world_P_body_key_pairs_.at(i).first);
414 nonuniqueKeys.push_back(world_P_body_key_pairs_.at(i).second);
421 Base::Cameras::template SchurComplementAndRearrangeBlocks<3, 12, 6>(
422 Fs,
E,
P,
b, nonuniqueKeys, this->keys_);
424 return std::make_shared<RegularHessianFactor<DimPose>>(
425 this->keys_, augmentedHessianUniqueKeys);
439 switch (this->params_.linearizationMode) {
441 return this->createHessianFactor(values,
lambda);
443 throw std::runtime_error(
444 "SmartProjectionPoseFactorRollingShutter: " 445 "unknown linearization mode");
452 return this->linearizeDamped(values);
456 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION 457 friend class boost::serialization::access; 459 template <
class ARCHIVE>
460 void serialize(ARCHIVE& ar,
const unsigned int ) {
461 ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
468 template <
class CAMERA>
470 :
public Testable<SmartProjectionPoseFactorRollingShutter<CAMERA>> {};
static const int ZDim
Measurement dimension (Point2)
std::shared_ptr< GaussianFactor > linearizeDamped(const Values &values, const double &lambda=0.0) const
const MATRIX::ConstRowXpr row(const MATRIX &A, size_t j)
std::string serialize(const T &input)
serializes to a string
const FastVector< size_t > & cameraIds() const
return the calibration object
const ValueType at(Key j) const
Base class to create smart factors on poses or cameras.
SmartProjectionFactor< CAMERA > Base
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
CameraSet< Camera > Cameras
std::vector< MatrixZD, Eigen::aligned_allocator< MatrixZD > > FBlocks
#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW
std::vector< MatrixZD, Eigen::aligned_allocator< MatrixZD > > FBlocks
std::shared_ptr< typename Base::Cameras > cameraRig_
EIGEN_STRONG_INLINE Packet4f print(const Packet4f &a)
void computeJacobiansWithTriangulatedPoint(FBlocks &Fs, Matrix &E, Vector &b, const Values &values) const
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
A set of cameras, all with their own calibration.
static const int DimBlock
std::vector< std::pair< Key, Key > > world_P_body_key_pairs_
static const KeyFormatter DefaultKeyFormatter
static const SmartProjectionParams params
SmartProjectionPoseFactorRollingShutter< CAMERA > This
Smart factor on cameras (pose + calibration)
void add(const MEASUREMENT &measured, const Key &world_P_body_key1, const Key &world_P_body_key2, const double &alpha, const size_t &cameraId=0)
CAMERA::Measurement MEASUREMENT
CAMERA::CalibrationType CALIBRATION
const std::shared_ptr< Cameras > & cameraRig() const
return the calibration object
void add(const MEASUREMENTS &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 >())
GenericMeasurement< Point2, Point2 > Measurement
std::shared_ptr< GaussianFactor > linearize(const Values &values) const override
linearize
Array< int, Dynamic, 1 > v
Array< double, 1, 3 > e(1./3., 0.5, 2.)
std::vector< double > alphas_
TriangulationResult triangulateSafe(const CameraSet< CAMERA > &cameras, const typename CAMERA::MeasurementVector &measured, const TriangulationParameters ¶ms)
triangulateSafe: extensive checking of the outcome
JacobiRotation< float > J
bool equals(const NonlinearFactor &p, double tol=1e-9) const override
equals
const std::vector< std::pair< Key, Key > > & world_P_body_key_pairs() const
Class compose(const Class &g) const
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
cout<< "The eigenvalues of A are:"<< endl<< ces.eigenvalues()<< endl;cout<< "The matrix of eigenvectors, V, is:"<< endl<< ces.eigenvectors()<< endl<< endl;complex< float > lambda
Base::Cameras cameras(const Values &values) const override
Eigen::Matrix< double, ZDim, DimBlock > MatrixZD
SmartProjectionPoseFactorRollingShutter()
Default constructor, only for serialization.
const std::vector< double > & alphas() const
return the interpolation factors alphas
std::shared_ptr< This > shared_ptr
shorthand for a smart pointer to a factor
double error(const Values &values) const override
graph add(PriorFactor< Pose2 >(1, priorMean, priorNoise))
PinholePose< Cal3_S2 > Camera
CameraSet< CAMERA > Cameras
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
FastVector< size_t > cameraIds_
The matrix class, also used for vectors and row-vectors.
static const CalibratedCamera camera(kDefaultPose)
bool equal(const T &obj1, const T &obj2, double tol)
std::uint64_t Key
Integer nonlinear key type.
SmartProjectionPoseFactorRollingShutter(const SharedNoiseModel &sharedNoiseModel, const std::shared_ptr< Cameras > &cameraRig, const SmartProjectionParams ¶ms=SmartProjectionParams())
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)
noiseModel::Base::shared_ptr SharedNoiseModel
CAMERA::MeasurementVector MEASUREMENTS