gtsam/gtsam/slam/PlanarProjectionFactor.h

299 lines
10 KiB
C
Raw Normal View History

2024-12-16 07:44:45 +08:00
/**
2024-12-17 10:04:40 +08:00
* ProjectionFactor, but with pose2 (robot on the floor)
2024-12-16 07:44:45 +08:00
*
* This factor is useful for high-school robotics competitions,
* which run robots on the floor, with use fixed maps and fiducial
2024-12-17 10:04:40 +08:00
* markers.
2024-12-16 07:44:45 +08:00
*
* @see https://www.firstinspires.org/
*
2024-12-17 10:04:40 +08:00
* @file PlanarProjectionFactor.h
2024-12-16 07:44:45 +08:00
* @brief for planar smoothing
* @date Dec 2, 2024
* @author joel@truher.org
*/
#pragma once
#include <gtsam/base/Testable.h>
#include <gtsam/base/Lie.h>
#include <gtsam/geometry/Cal3DS2.h>
#include <gtsam/geometry/PinholeCamera.h>
#include <gtsam/geometry/Point3.h>
#include <gtsam/geometry/Pose2.h>
#include <gtsam/geometry/Pose3.h>
#include <gtsam/geometry/Rot3.h>
#include <gtsam/nonlinear/NonlinearFactor.h>
#include <gtsam/base/numericalDerivative.h>
namespace gtsam {
2024-12-17 10:04:40 +08:00
2024-12-16 07:44:45 +08:00
/**
2024-12-17 10:04:40 +08:00
* @class PlanarProjectionFactorBase
* @brief Camera projection for robot on the floor. Measurement is camera pixels.
2024-12-16 07:44:45 +08:00
*/
2024-12-17 10:04:40 +08:00
class PlanarProjectionFactorBase {
protected:
PlanarProjectionFactorBase() {}
/**
2024-12-18 14:39:13 +08:00
* @param measured pixels in the camera frame
2024-12-17 10:04:40 +08:00
*/
PlanarProjectionFactorBase(const Point2& measured) : measured_(measured) {}
/**
2024-12-18 14:39:13 +08:00
* Predict the projection of the landmark in camera pixels.
*
* @param landmark point3 of the target
* @param wTb "world to body": planar pose2 of vehicle body frame in world frame
* @param bTc "body to camera": camera pose in body frame, oriented parallel to pose2 zero i.e. +x
* @param calib camera calibration with distortion
* @param Hlandmark jacobian
* @param HwTb jacobian
* @param HbTc jacobian
* @param Hcalib jacobian
2024-12-17 10:04:40 +08:00
*/
2024-12-18 14:39:13 +08:00
Point2 predict(
2024-12-17 10:04:40 +08:00
const Point3& landmark,
2024-12-18 14:39:13 +08:00
const Pose2& wTb,
const Pose3& bTc,
2024-12-17 10:04:40 +08:00
const Cal3DS2& calib,
2024-12-25 01:06:25 +08:00
OptionalJacobian<2, 3> Hlandmark = {}, // (x, y, z)
OptionalJacobian<2, 3> HwTb = {}, // (x, y, theta)
OptionalJacobian<2, 6> HbTc = {}, // (rx, ry, rz, x, y, theta)
OptionalJacobian<2, 9> Hcalib = {}
2024-12-17 10:04:40 +08:00
) const {
2024-12-18 14:39:13 +08:00
#ifndef GTSAM_THROW_CHEIRALITY_EXCEPTION
2024-12-17 10:04:40 +08:00
try {
2024-12-18 14:39:13 +08:00
#endif
2024-12-25 01:06:25 +08:00
Matrix63 Hp; // 6x3
Matrix66 H0; // 6x6
Pose3 wTc = Pose3::FromPose2(wTb, HwTb ? &Hp : nullptr).compose(bTc, HwTb ? &H0 : nullptr);
PinholeCamera<Cal3DS2> camera = PinholeCamera<Cal3DS2>(wTc, calib);
2024-12-18 14:39:13 +08:00
if (HwTb || HbTc) {
2024-12-25 01:06:25 +08:00
// Dpose is for pose3 (R,t)
Matrix26 Dpose;
2024-12-18 14:39:13 +08:00
Point2 result = camera.project(landmark, Dpose, Hlandmark, Hcalib);
if (HbTc)
2024-12-25 01:06:25 +08:00
*HbTc = Dpose;
if (HwTb)
*HwTb = Dpose * H0 * Hp;
2024-12-17 10:04:40 +08:00
return result;
} else {
return camera.project(landmark, {}, {}, {});
}
2024-12-18 14:39:13 +08:00
#ifndef GTSAM_THROW_CHEIRALITY_EXCEPTION
2024-12-17 10:04:40 +08:00
} catch (CheiralityException& e) {
std::cout << "****** CHIRALITY EXCEPTION ******\n";
2024-12-25 01:06:25 +08:00
if (Hlandmark) Hlandmark->setZero();
if (HwTb) HwTb->setZero();
if (HbTc) HbTc->setZero();
if (Hcalib) Hcalib->setZero();
2024-12-17 10:04:40 +08:00
// return a large error
return Matrix::Constant(2, 1, 2.0 * calib.fx());
}
2024-12-18 14:39:13 +08:00
#endif
2024-12-17 10:04:40 +08:00
}
Point2 measured_; // pixel measurement
};
2024-12-16 07:44:45 +08:00
2024-12-17 10:04:40 +08:00
/**
* @class PlanarProjectionFactor1
* @brief One variable: the pose.
* Landmark, camera offset, camera calibration are constant.
* This is intended for localization within a known map.
*/
class PlanarProjectionFactor1
: public PlanarProjectionFactorBase, public NoiseModelFactorN<Pose2> {
public:
typedef NoiseModelFactorN<Pose2> Base;
using Base::evaluateError;
PlanarProjectionFactor1() {}
~PlanarProjectionFactor1() override {}
/// @return a deep copy of this factor
2024-12-25 01:06:25 +08:00
NonlinearFactor::shared_ptr clone() const override {
return std::static_pointer_cast<NonlinearFactor>(
NonlinearFactor::shared_ptr(new PlanarProjectionFactor1(*this)));
2024-12-17 10:04:40 +08:00
}
/**
2024-12-18 14:39:13 +08:00
* @brief constructor for known landmark, offset, and calibration
* @param poseKey index of the robot pose2 in the z=0 plane
2024-12-17 10:04:40 +08:00
* @param landmark point3 in the world
* @param measured corresponding point2 in the camera frame
2024-12-18 14:39:13 +08:00
* @param bTc "body to camera": constant camera offset from pose
2024-12-17 10:04:40 +08:00
* @param calib constant camera calibration
* @param model stddev of the measurements, ~one pixel?
*/
PlanarProjectionFactor1(
2024-12-18 14:39:13 +08:00
Key poseKey,
2024-12-17 10:04:40 +08:00
const Point3& landmark,
const Point2& measured,
2024-12-18 14:39:13 +08:00
const Pose3& bTc,
2024-12-17 10:04:40 +08:00
const Cal3DS2& calib,
2024-12-25 01:06:25 +08:00
const SharedNoiseModel& model = {})
2024-12-17 10:04:40 +08:00
: PlanarProjectionFactorBase(measured),
NoiseModelFactorN(model, poseKey),
landmark_(landmark),
2024-12-18 14:39:13 +08:00
bTc_(bTc),
2024-12-25 01:06:25 +08:00
calib_(calib) {}
2024-12-17 10:04:40 +08:00
/**
2024-12-18 14:39:13 +08:00
* @param wTb "world to body": estimated pose2
* @param HwTb jacobian
2024-12-17 10:04:40 +08:00
*/
2024-12-25 01:06:25 +08:00
Vector evaluateError(const Pose2& wTb, OptionalMatrixType HwTb) const override {
2024-12-18 14:39:13 +08:00
return predict(landmark_, wTb, bTc_, calib_, {}, HwTb, {}, {}) - measured_;
2024-12-17 10:04:40 +08:00
}
2024-12-16 07:44:45 +08:00
2024-12-17 10:04:40 +08:00
private:
2024-12-16 07:44:45 +08:00
Point3 landmark_; // landmark
2024-12-18 14:39:13 +08:00
Pose3 bTc_; // "body to camera": camera offset to robot pose
2024-12-16 07:44:45 +08:00
Cal3DS2 calib_; // camera calibration
2024-12-17 10:04:40 +08:00
};
template<>
struct traits<PlanarProjectionFactor1> :
public Testable<PlanarProjectionFactor1> {};
2024-12-16 07:44:45 +08:00
2024-12-17 10:04:40 +08:00
/**
* @class PlanarProjectionFactor2
* @brief Two unknowns: the pose and the landmark.
* Camera offset and calibration are constant.
* This is similar to GeneralSFMFactor, used for SLAM.
*/
2024-12-25 01:06:25 +08:00
class PlanarProjectionFactor2
: public PlanarProjectionFactorBase, public NoiseModelFactorN<Pose2, Point3> {
2024-12-16 07:44:45 +08:00
public:
2024-12-18 14:39:13 +08:00
typedef NoiseModelFactorN<Pose2, Point3> Base;
2024-12-16 07:44:45 +08:00
using Base::evaluateError;
2024-12-17 10:04:40 +08:00
PlanarProjectionFactor2() {}
~PlanarProjectionFactor2() override {}
/// @return a deep copy of this factor
2024-12-25 01:06:25 +08:00
NonlinearFactor::shared_ptr clone() const override {
return std::static_pointer_cast<NonlinearFactor>(
NonlinearFactor::shared_ptr(new PlanarProjectionFactor2(*this)));
2024-12-17 10:04:40 +08:00
}
2024-12-16 07:44:45 +08:00
/**
2024-12-18 14:39:13 +08:00
* @brief constructor for variable landmark, known offset and calibration
* @param poseKey index of the robot pose2 in the z=0 plane
* @param landmarkKey index of the landmark point3
2024-12-16 07:44:45 +08:00
* @param measured corresponding point in the camera frame
2024-12-18 14:39:13 +08:00
* @param bTc "body to camera": constant camera offset from pose
2024-12-16 07:44:45 +08:00
* @param calib constant camera calibration
* @param model stddev of the measurements, ~one pixel?
*/
2024-12-17 10:04:40 +08:00
PlanarProjectionFactor2(
2024-12-18 14:39:13 +08:00
Key poseKey,
Key landmarkKey,
2024-12-16 07:44:45 +08:00
const Point2& measured,
2024-12-18 14:39:13 +08:00
const Pose3& bTc,
2024-12-16 07:44:45 +08:00
const Cal3DS2& calib,
2024-12-25 01:06:25 +08:00
const SharedNoiseModel& model = {})
2024-12-17 10:04:40 +08:00
: PlanarProjectionFactorBase(measured),
2025-04-27 06:02:40 +08:00
NoiseModelFactorN(model, poseKey, landmarkKey),
2024-12-18 14:39:13 +08:00
bTc_(bTc),
2024-12-25 01:06:25 +08:00
calib_(calib) {}
2024-12-16 07:44:45 +08:00
2024-12-17 10:04:40 +08:00
/**
2024-12-18 14:39:13 +08:00
* @param wTb "world to body": estimated pose2
2024-12-17 10:04:40 +08:00
* @param landmark estimated landmark
2024-12-18 14:39:13 +08:00
* @param HwTb jacobian
* @param Hlandmark jacobian
2024-12-17 10:04:40 +08:00
*/
Vector evaluateError(
2024-12-18 14:39:13 +08:00
const Pose2& wTb,
2024-12-17 10:04:40 +08:00
const Point3& landmark,
2024-12-25 01:06:25 +08:00
OptionalMatrixType HwTb,
OptionalMatrixType Hlandmark) const override {
2024-12-18 14:39:13 +08:00
return predict(landmark, wTb, bTc_, calib_, Hlandmark, HwTb, {}, {}) - measured_;
2024-12-17 10:04:40 +08:00
}
private:
2024-12-18 14:39:13 +08:00
Pose3 bTc_; // "body to camera": camera offset to robot pose
2024-12-17 10:04:40 +08:00
Cal3DS2 calib_; // camera calibration
};
template<>
struct traits<PlanarProjectionFactor2> :
public Testable<PlanarProjectionFactor2> {};
/**
* @class PlanarProjectionFactor3
* @brief Three unknowns: the pose, the camera offset, and the camera calibration.
* Landmark is constant.
* This is intended to be used for camera calibration.
*/
class PlanarProjectionFactor3 : public PlanarProjectionFactorBase, public NoiseModelFactorN<Pose2, Pose3, Cal3DS2> {
public:
typedef NoiseModelFactorN<Pose2, Pose3, Cal3DS2> Base;
using Base::evaluateError;
PlanarProjectionFactor3() {}
~PlanarProjectionFactor3() override {}
2024-12-16 07:44:45 +08:00
/// @return a deep copy of this factor
2024-12-25 01:06:25 +08:00
NonlinearFactor::shared_ptr clone() const override {
return std::static_pointer_cast<NonlinearFactor>(
NonlinearFactor::shared_ptr(new PlanarProjectionFactor3(*this)));
2024-12-16 07:44:45 +08:00
}
2024-12-17 10:04:40 +08:00
/**
2024-12-18 14:39:13 +08:00
* @brief constructor for variable pose, offset, and calibration, known landmark.
2024-12-25 01:06:25 +08:00
* @param poseKey index of the robot pose2 in the z=0 plane
* @param offsetKey index of camera offset from pose
* @param calibKey index of camera calibration
2024-12-17 10:04:40 +08:00
* @param landmark point3 in the world
* @param measured corresponding point2 in the camera frame
* @param model stddev of the measurements, ~one pixel?
2024-12-25 01:06:25 +08:00
*/
2024-12-17 10:04:40 +08:00
PlanarProjectionFactor3(
Key poseKey,
Key offsetKey,
2024-12-18 14:39:13 +08:00
Key calibKey,
const Point3& landmark,
const Point2& measured,
2024-12-25 01:06:25 +08:00
const SharedNoiseModel& model = {})
2024-12-17 10:04:40 +08:00
: PlanarProjectionFactorBase(measured),
NoiseModelFactorN(model, poseKey, offsetKey, calibKey),
2024-12-25 01:06:25 +08:00
landmark_(landmark) {}
2024-12-16 07:44:45 +08:00
2024-12-17 10:04:40 +08:00
/**
2024-12-18 14:39:13 +08:00
* @param wTb "world to body": estimated pose2
* @param bTc "body to camera": pose3 offset from pose2 +x
2024-12-17 10:04:40 +08:00
* @param calib calibration
2024-12-18 14:39:13 +08:00
* @param HwTb pose jacobian
2024-12-25 01:06:25 +08:00
* @param HbTc offset jacobian
* @param Hcalib calibration jacobian
2024-12-17 10:04:40 +08:00
*/
2024-12-16 07:44:45 +08:00
Vector evaluateError(
2024-12-18 14:39:13 +08:00
const Pose2& wTb,
const Pose3& bTc,
2024-12-17 10:04:40 +08:00
const Cal3DS2& calib,
2024-12-25 01:06:25 +08:00
OptionalMatrixType HwTb,
OptionalMatrixType HbTc,
OptionalMatrixType Hcalib) const override {
2024-12-18 14:39:13 +08:00
return predict(landmark_, wTb, bTc, calib, {}, HwTb, HbTc, Hcalib) - measured_;
2024-12-16 07:44:45 +08:00
}
2024-12-17 10:04:40 +08:00
private:
Point3 landmark_; // landmark
};
2024-12-16 07:44:45 +08:00
template<>
2024-12-17 10:04:40 +08:00
struct traits<PlanarProjectionFactor3> :
public Testable<PlanarProjectionFactor3> {};
2024-12-16 07:44:45 +08:00
2024-12-25 06:32:44 +08:00
} // namespace gtsam