modernized factor

release/4.3a0
lcarlone 2021-10-02 21:00:25 -04:00
parent b49bd123f4
commit a5db090fb4
4 changed files with 1119 additions and 1134 deletions

View File

@ -71,6 +71,7 @@ protected:
typedef boost::shared_ptr<This> shared_ptr; typedef boost::shared_ptr<This> shared_ptr;
/// shorthand for a set of cameras /// shorthand for a set of cameras
typedef CAMERA Camera;
typedef CameraSet<CAMERA> Cameras; typedef CameraSet<CAMERA> Cameras;
/** /**

View File

@ -14,11 +14,10 @@
* @brief Smart factor on poses, assuming camera calibration is fixed. * @brief Smart factor on poses, assuming camera calibration is fixed.
* Same as SmartProjectionPoseFactor, except: * Same as SmartProjectionPoseFactor, except:
* - it is templated on CAMERA (i.e., it allows cameras beyond pinhole) * - it is templated on CAMERA (i.e., it allows cameras beyond pinhole)
* - it admits a different calibration for each measurement (i.e., it can model a multi-camera system) * - it admits a different calibration for each measurement (i.e., it can model a multi-camera rig system)
* - it allows multiple observations from the same pose/key (again, to model a multi-camera system) * - it allows multiple observations from the same pose/key (again, to model a multi-camera system)
* @author Luca Carlone * @author Luca Carlone
* @author Chris Beall * @author Frank Dellaert
* @author Zsolt Kira
*/ */
#pragma once #pragma once
@ -34,7 +33,6 @@ namespace gtsam {
* L. Carlone, Z. Kira, C. Beall, V. Indelman, F. Dellaert, Eliminating conditionally * L. Carlone, Z. Kira, C. Beall, V. Indelman, F. Dellaert, Eliminating conditionally
* independent sets in factor graphs: a unifying perspective based on smart factors, * independent sets in factor graphs: a unifying perspective based on smart factors,
* Int. Conf. on Robotics and Automation (ICRA), 2014. * Int. Conf. on Robotics and Automation (ICRA), 2014.
*
*/ */
/** /**
@ -60,13 +58,13 @@ class SmartProjectionRigFactor : public SmartProjectionFactor<CAMERA> {
protected: protected:
/// vector of keys (one for each observation) with potentially repeated keys /// vector of keys (one for each observation) with potentially repeated keys
std::vector<Key> nonUniqueKeys_; FastVector<Key> nonUniqueKeys_;
/// shared pointer to calibration object (one for each observation) /// cameras in the rig (fixed poses wrt body + fixed intrinsics)
std::vector<boost::shared_ptr<CALIBRATION> > K_all_; typename Base::Cameras cameraRig_;
/// Pose of the camera in the body frame (one for each observation) /// vector of camera Ids (one for each observation), identifying which camera took the measurement
std::vector<Pose3> body_P_sensors_; FastVector<Key> cameraIds_;
public: public:
typedef CAMERA Camera; typedef CAMERA Camera;
@ -82,12 +80,14 @@ class SmartProjectionRigFactor : public SmartProjectionFactor<CAMERA> {
/** /**
* Constructor * Constructor
* @param sharedNoiseModel isotropic noise model for the 2D feature measurements * @param sharedNoiseModel isotropic noise model for the 2D feature measurements
* @param cameraRig set of cameras (fixed poses wrt body and intrinsics) in the camera rig
* @param params parameters for the smart projection factors * @param params parameters for the smart projection factors
*/ */
SmartProjectionRigFactor(const SharedNoiseModel& sharedNoiseModel, SmartProjectionRigFactor(const SharedNoiseModel& sharedNoiseModel,
const Cameras& cameraRig,
const SmartProjectionParams& params = const SmartProjectionParams& params =
SmartProjectionParams()) SmartProjectionParams())
: Base(sharedNoiseModel, params) { : Base(sharedNoiseModel, params), cameraRig_(cameraRig) {
// use only configuration that works with this factor // use only configuration that works with this factor
Base::params_.degeneracyMode = gtsam::ZERO_ON_DEGENERACY; Base::params_.degeneracyMode = gtsam::ZERO_ON_DEGENERACY;
Base::params_.linearizationMode = gtsam::HESSIAN; Base::params_.linearizationMode = gtsam::HESSIAN;
@ -98,17 +98,14 @@ class SmartProjectionRigFactor : public SmartProjectionFactor<CAMERA> {
} }
/** /**
* add a new measurement, corresponding to an observation from pose "poseKey" whose camera * add a new measurement, corresponding to an observation from pose "poseKey"
* has intrinsic calibration K and extrinsic calibration body_P_sensor. * and taken from the camera in the rig identified by "cameraId"
* @param measured 2-dimensional location of the projection of a * @param measured 2-dimensional location of the projection of a
* single landmark in a single view (the measurement) * single landmark in a single view (the measurement)
* @param poseKey key corresponding to the body pose of the camera taking the measurement * @param poseKey key corresponding to the body pose of the camera taking the measurement
* @param K (fixed) camera intrinsic calibration * @param cameraId ID of the camera in the rig taking the measurement
* @param body_P_sensor (fixed) camera extrinsic calibration
*/ */
void add(const Point2& measured, const Key& poseKey, void add(const Point2& measured, const Key& poseKey, const size_t cameraId) {
const boost::shared_ptr<CALIBRATION>& K, const Pose3 body_P_sensor =
Pose3::identity()) {
// store measurement and key // store measurement and key
this->measured_.push_back(measured); this->measured_.push_back(measured);
this->nonUniqueKeys_.push_back(poseKey); this->nonUniqueKeys_.push_back(poseKey);
@ -117,10 +114,8 @@ class SmartProjectionRigFactor : public SmartProjectionFactor<CAMERA> {
if (std::find(this->keys_.begin(), this->keys_.end(), poseKey) == this->keys_.end()) if (std::find(this->keys_.begin(), this->keys_.end(), poseKey) == this->keys_.end())
this->keys_.push_back(poseKey); // add only unique keys this->keys_.push_back(poseKey); // add only unique keys
// store fixed intrinsic calibration // store id of the camera taking the measurement
K_all_.push_back(K); cameraIds_.push_back(cameraId);
// store fixed extrinsics of the camera
body_P_sensors_.push_back(body_P_sensor);
} }
/** /**
@ -128,38 +123,32 @@ class SmartProjectionRigFactor : public SmartProjectionFactor<CAMERA> {
* @param measurements vector of the 2m dimensional location of the projection * @param measurements vector of the 2m dimensional location of the projection
* of a single landmark in the m views (the measurements) * of a single landmark in the m views (the measurements)
* @param poseKeys keys corresponding to the body poses of the cameras taking the measurements * @param poseKeys keys corresponding to the body poses of the cameras taking the measurements
* @param Ks vector of (fixed) intrinsic calibration objects * @param cameraIds IDs of the cameras in the rig taking each measurement (same order as measurements)
* @param body_P_sensors vector of (fixed) extrinsic calibration objects
*/ */
void add(const Point2Vector& measurements, const std::vector<Key>& poseKeys, void add(const Point2Vector& measurements, const FastVector<Key>& poseKeys,
const std::vector<boost::shared_ptr<CALIBRATION>>& Ks, const FastVector<size_t>& cameraIds) {
const std::vector<Pose3> body_P_sensors = std::vector<Pose3>()) {
assert(poseKeys.size() == measurements.size()); assert(poseKeys.size() == measurements.size());
assert(poseKeys.size() == Ks.size()); assert(poseKeys.size() == cameraIds.size());
for (size_t i = 0; i < measurements.size(); i++) { for (size_t i = 0; i < measurements.size(); i++) {
if (poseKeys.size() == body_P_sensors.size()) { add(measurements[i], poseKeys[i], cameraIds[i]);
add(measurements[i], poseKeys[i], Ks[i], body_P_sensors[i]);
} else {
add(measurements[i], poseKeys[i], Ks[i]); // use default extrinsics
}
} }
} }
/// return the calibration object
inline std::vector<boost::shared_ptr<CALIBRATION>> calibration() const {
return K_all_;
}
/// return the extrinsic camera calibration body_P_sensors
const std::vector<Pose3> body_P_sensors() const {
return body_P_sensors_;
}
/// return (for each observation) the (possibly non unique) keys involved in the measurements /// return (for each observation) the (possibly non unique) keys involved in the measurements
const std::vector<Key> nonUniqueKeys() const { const FastVector<Key> nonUniqueKeys() const {
return nonUniqueKeys_; return nonUniqueKeys_;
} }
/// return the calibration object
inline Cameras cameraRig() const {
return cameraRig_;
}
/// return the calibration object
inline FastVector<size_t> cameraIds() const {
return cameraIds_;
}
/** /**
* print * print
* @param s optional string naming the factor * @param s optional string naming the factor
@ -168,11 +157,11 @@ class SmartProjectionRigFactor : public SmartProjectionFactor<CAMERA> {
void print(const std::string& s = "", const KeyFormatter& keyFormatter = void print(const std::string& s = "", const KeyFormatter& keyFormatter =
DefaultKeyFormatter) const override { DefaultKeyFormatter) const override {
std::cout << s << "SmartProjectionRigFactor: \n "; std::cout << s << "SmartProjectionRigFactor: \n ";
for (size_t i = 0; i < K_all_.size(); i++) { for (size_t i = 0; i < nonUniqueKeys_.size(); i++) {
std::cout << "-- Measurement nr " << i << std::endl; std::cout << "-- Measurement nr " << i << std::endl;
std::cout << "key: " << keyFormatter(nonUniqueKeys_[i]) << std::endl; std::cout << "key: " << keyFormatter(nonUniqueKeys_[i]) << std::endl;
body_P_sensors_[i].print("extrinsic calibration:\n"); std::cout << "cameraId: " << cameraIds_[i] << std::endl;
K_all_[i]->print("intrinsic calibration = "); cameraRig_[ cameraIds_[i] ].print("camera in rig:\n");
} }
Base::print("", keyFormatter); Base::print("", keyFormatter);
} }
@ -180,35 +169,26 @@ class SmartProjectionRigFactor : public SmartProjectionFactor<CAMERA> {
/// equals /// equals
bool equals(const NonlinearFactor& p, double tol = 1e-9) const override { bool equals(const NonlinearFactor& p, double tol = 1e-9) const override {
const This *e = dynamic_cast<const This*>(&p); const This *e = dynamic_cast<const This*>(&p);
double extrinsicCalibrationEqual = true; return e && Base::equals(p, tol)
if (this->body_P_sensors_.size() == e->body_P_sensors().size()) { && nonUniqueKeys_ == e->nonUniqueKeys()
for (size_t i = 0; i < this->body_P_sensors_.size(); i++) { && cameraRig_.equals(e->cameraRig());
if (!body_P_sensors_[i].equals(e->body_P_sensors()[i])) { // && cameraIds_ == e->cameraIds();
extrinsicCalibrationEqual = false;
break;
}
}
} else {
extrinsicCalibrationEqual = false;
}
return e && Base::equals(p, tol) && K_all_ == e->calibration()
&& nonUniqueKeys_ == e->nonUniqueKeys() && extrinsicCalibrationEqual;
} }
/** /**
* Collect all cameras involved in this factor * Collect all cameras involved in this factor
* @param values Values structure which must contain camera poses corresponding * @param values Values structure which must contain body poses corresponding
* to keys involved in this factor * to keys involved in this factor
* @return vector of cameras * @return vector of cameras
*/ */
typename Base::Cameras cameras(const Values& values) const override { typename Base::Cameras cameras(const Values& values) const override {
typename Base::Cameras cameras; typename Base::Cameras cameras;
for (size_t i = 0; i < nonUniqueKeys_.size(); i++) { for (size_t i = 0; i < nonUniqueKeys_.size(); i++) {
const Pose3& body_P_cam_i = body_P_sensors_[i]; const Pose3& body_P_cam_i = cameraRig_[i].pose();
const Pose3 world_P_sensor_i = values.at<Pose3>(nonUniqueKeys_[i]) const Pose3 world_P_sensor_i = values.at<Pose3>(nonUniqueKeys_[i])
* body_P_cam_i; * body_P_cam_i;
cameras.emplace_back(world_P_sensor_i, K_all_[i]); cameras.emplace_back(world_P_sensor_i,
make_shared<typename CAMERA::CalibrationType>(cameraRig_[i].calibration()));
} }
return cameras; return cameras;
} }
@ -240,10 +220,11 @@ class SmartProjectionRigFactor : public SmartProjectionFactor<CAMERA> {
} else { // valid result: compute jacobians } else { // valid result: compute jacobians
b = -cameras.reprojectionError(*this->result_, this->measured_, Fs, E); b = -cameras.reprojectionError(*this->result_, this->measured_, Fs, E);
for (size_t i = 0; i < Fs.size(); i++) { for (size_t i = 0; i < Fs.size(); i++) {
const Pose3 sensor_P_body = body_P_sensors_[i].inverse(); const Pose3 body_P_sensor = cameraRig_[i].pose();
const Pose3 sensor_P_body = body_P_sensor.inverse();
const Pose3 world_P_body = cameras[i].pose() * sensor_P_body; const Pose3 world_P_body = cameras[i].pose() * sensor_P_body;
Eigen::Matrix<double, DimPose, DimPose> H; Eigen::Matrix<double, DimPose, DimPose> H;
world_P_body.compose(body_P_sensors_[i], H); world_P_body.compose(body_P_sensor, H);
Fs.at(i) = Fs.at(i) * H; Fs.at(i) = Fs.at(i) * H;
} }
} }
@ -262,8 +243,8 @@ class SmartProjectionRigFactor : public SmartProjectionFactor<CAMERA> {
// Create structures for Hessian Factors // Create structures for Hessian Factors
KeyVector js; KeyVector js;
std::vector < Matrix > Gs(nrUniqueKeys * (nrUniqueKeys + 1) / 2); FastVector < Matrix > Gs(nrUniqueKeys * (nrUniqueKeys + 1) / 2);
std::vector < Vector > gs(nrUniqueKeys); FastVector < Vector > gs(nrUniqueKeys);
if (this->measured_.size() != this->cameras(values).size()) // 1 observation per camera if (this->measured_.size() != this->cameras(values).size()) // 1 observation per camera
throw std::runtime_error("SmartProjectionRigFactor: " throw std::runtime_error("SmartProjectionRigFactor: "
@ -324,7 +305,7 @@ class SmartProjectionRigFactor : public SmartProjectionFactor<CAMERA> {
return this->createHessianFactor(values, lambda); return this->createHessianFactor(values, lambda);
default: default:
throw std::runtime_error( throw std::runtime_error(
"SmartProjectioFactorP: unknown linearization mode"); "SmartProjectionRigFactor: unknown linearization mode");
} }
} }
@ -341,9 +322,9 @@ class SmartProjectionRigFactor : public SmartProjectionFactor<CAMERA> {
template<class ARCHIVE> template<class ARCHIVE>
void serialize(ARCHIVE & ar, const unsigned int /*version*/) { void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base); ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
ar & BOOST_SERIALIZATION_NVP(K_all_);
ar & BOOST_SERIALIZATION_NVP(nonUniqueKeys_); ar & BOOST_SERIALIZATION_NVP(nonUniqueKeys_);
ar & BOOST_SERIALIZATION_NVP(body_P_sensors_); ar & BOOST_SERIALIZATION_NVP(cameraRig_);
ar & BOOST_SERIALIZATION_NVP(cameraIds_);
} }
}; };

View File

@ -69,6 +69,7 @@ SmartProjectionParams params;
// default Cal3_S2 poses // default Cal3_S2 poses
namespace vanillaPose { namespace vanillaPose {
typedef PinholePose<Cal3_S2> Camera; typedef PinholePose<Cal3_S2> Camera;
typedef CameraSet<Camera> Cameras;
typedef SmartProjectionPoseFactor<Cal3_S2> SmartFactor; typedef SmartProjectionPoseFactor<Cal3_S2> SmartFactor;
typedef SmartProjectionRigFactor<Camera> SmartFactorP; typedef SmartProjectionRigFactor<Camera> SmartFactorP;
static Cal3_S2::shared_ptr sharedK(new Cal3_S2(fov, w, h)); static Cal3_S2::shared_ptr sharedK(new Cal3_S2(fov, w, h));

File diff suppressed because it is too large Load Diff