To reimplement the BetweenFactor in GTSAM with an additional scale parameter s, you need to define a custom factor that relates two poses X_i, X_j, and a scalar scale s. The measurement equation becomes:
[
\log(X_i^{-1} \cdot X_j) = s \cdot \log(T_{\text{measured}})
]
Where:
X_i, X_j are poses (e.g., Pose3 or Pose2)
s is a scalar scale parameter
T_{\text{measured}} is the measured relative pose
Jacobian Derivation (Pose3 case)
Let’s derive the Jacobians with respect to X_i, X_j, and s:
1. Error Definition
The error vector is:
[
e = \log(X_i^{-1} \cdot X_j) - s \cdot \log(T_{\text{measured}})
]
2. Jacobians
Let T_ij = X_i^{-1} * X_j. The Jacobians are:
w.r.t. X_i (left perturbation)
[
\frac{\partial e}{\partial \delta \xi_i} = -\text{Adj}(T_{ij}^{-1}) \cdot \text{ExpmapDerivative}(\log(T_{ij}))
]
w.r.t. X_j (left perturbation)
[
\frac{\partial e}{\partial \delta \xi_j} = \text{ExpmapDerivative}(\log(T_{ij}))
]
[
\frac{\partial e}{\partial s} = -\log(T_{\text{measured}})
]
Where:
ExpmapDerivative is the derivative of the exponential map (from Lie algebra to Lie group).
Adj is the adjoint action of the group.
GTSAM Implementation (Pose3)
Here’s how to structure the evaluateError method in your custom factor:
cpp Copy code
Vector evaluateError(const Pose3& X_i, const Pose3& X_j, const double& s,
boost::optional<Matrix&> H1,
boost::optional<Matrix&> H2,
boost::optional<Matrix&> H3) const {
// Compute relative pose
Pose3 T_ij = X_i.between(X_j);
Vector6 log_T_ij = T_ij.log();
Vector6 log_measured = measured_.log();
// Error
Vector6 error = log_T_ij - s * log_measured;
// Jacobians
if (H1) {
*H1 = -T_ij.inverse().AdjointMap() * Pose3::ExpmapDerivative(log_T_ij);
}
if (H2) {
*H2 = Pose3::ExpmapDerivative(log_T_ij);
}
if (H3) {
*H3 = -log_measured;
}
return error;
}
Pose2 Case
If using Pose2, the error is 3D (x, y, theta), and the Jacobians simplify:
cpp Copy code
Vector3 evaluateError(const Pose2& X_i, const Pose2& X_j, const double& s,
boost::optional<Matrix&> H1,
boost::optional<Matrix&> H2,
boost::optional<Matrix&> H3) const {
Pose2 T_ij = X_i.between(X_j);
Vector3 log_T_ij = T_ij.logmap(T_ij);
Vector3 log_measured = measured_.logmap(measured_);
Vector3 error = log_T_ij - s * log_measured;
if (H1) {
*H1 = -T_ij.inverse().AdjointMap() * Pose2::ExpmapDerivative(log_T_ij);
}
if (H2) {
*H2 = Pose2::ExpmapDerivative(log_T_ij);
}
if (H3) {
*H3 = -log_measured;
}
return error;
}
Key Notes
The scale s must be a double (or Vector1) in GTSAM.
The Jacobians are derived using left perturbation (common in GTSAM).
ExpmapDerivative is provided by GTSAM (e.g., Pose3::ExpmapDerivative).
This gives you a fully functional custom factor with scale adjustment.