Below is the pseudo codes.
class BACostFunction
{
public:
BACostFunction(const Eigen::Vector2d &point2D, const Camera &camera) : point2D_(point2D), camera_(camera)
{
}
static ceres::CostFunction* create(const Eigen::Vector2d& point2D, const Camera &camera)
{
return (new ceres::AutoDiffCostFunction
<BACostFunction,2, 3, 1, 1, 1, 3>
(new BACostFunction(point2D,camera)));
}
template <typename T> bool operator()(const T* const rvec,
const T* const tx,
const T* const ty,
const T* const tz,
const T* const point3D,
T* residuals) const
{
.....
reproject_error = re_project(camera_,current_pose,worldPt,reprojected_pixel);
if(!reproject_error)
{
residuals[0] = T(0.0);
residuals[1] = T(0.0);
return true;
}
// Re-projection error.
residuals[0] = (reprojected_pixel(0)-T(point2D_(0)));
residuals[1] = (reprojected_pixel(1)-T(point2D_(1)));
return true;
}
private:
const Eigen::Vector2d &point2D_;
const Camera &camera_;
};
-----------------------------------------------------------
struct GPSCostFunction
{
GPSCostFunction(Eigen::Vector3d _gps_measurement, double _weight):gps_measurement(_gps_measurement),weight(_weight)
{
}
static ceres::CostFunction* create(Eigen::Vector3d gps_measurement, double weight)
{
return (new ceres::AutoDiffCostFunction
<GPSCostFunction, 3, 1, 1, 1>
(new GPSCostFunction(gps_measurement,weight)));
}
template <typename T>
bool operator()(const T* const tx,
const T* const ty,
const T* const tz, T* residuals) const
{
Eigen::Matrix<T, 3, 1> translation;
translation(0)=tx[0];
translation(1)=ty[0];
translation(2)=tz[0];
Eigen::Matrix<T, 3, 1> residual_dist;
residuals[0] = translation(0)-T(gps_measurement(0));
residuals[1] = translation(1)-T(gps_measurement(1));
residuals[2] = translation(2)-T(gps_measurement(2));
return true;
}
private:
const Eigen::Vector3d &gps_measurement;
const double &weight;
};