cartographer-project / cartographer

Cartographer is a system that provides real-time simultaneous localization and mapping (SLAM) in 2D and 3D across multiple platforms and sensor configurations.
Apache License 2.0
7.09k stars 2.25k forks source link

How to understand YawOnlyQuaternionPlus function #1770

Closed cyy1992 closed 3 years ago

cyy1992 commented 3 years ago

Here is the code:

struct YawOnlyQuaternionPlus {
  template <typename T>
  bool operator()(const T* x, const T* delta, T* x_plus_delta) const {
    const T clamped_delta = common::Clamp(delta[0], T(-0.5), T(0.5));
    T q_delta[4];
    q_delta[0] = ceres::sqrt(1. - clamped_delta * clamped_delta);
    q_delta[1] = T(0.);
    q_delta[2] = T(0.);
    q_delta[3] = clamped_delta;
    ceres::QuaternionProduct(q_delta, x, x_plus_delta);
    return true;
  }
}

Here is QuaternionParameterization in ceres:

bool QuaternionParameterization::Plus(const double* x,
                                      const double* delta,
                                      double* x_plus_delta) const {
  const double norm_delta =
      sqrt(delta[0] * delta[0] + delta[1] * delta[1] + delta[2] * delta[2]);
  if (norm_delta > 0.0) {
    const double sin_delta_by_delta = (sin(norm_delta) / norm_delta);
    double q_delta[4];
    q_delta[0] = cos(norm_delta);
    q_delta[1] = sin_delta_by_delta * delta[0];
    q_delta[2] = sin_delta_by_delta * delta[1];
    q_delta[3] = sin_delta_by_delta * delta[2];
    QuaternionProduct(q_delta, x, x_plus_delta);
  } else {
    for (int i = 0; i < 4; ++i) {
      x_plus_delta[i] = x[i];
    }
  }
  return true;
}

In ceres, delta means angle-axis, so delta[0] is X-axis component. However in YawOnlyQuaternionPlus, delta[0] is assigned to qz which seems stange.

Could you help explain this code, please? Thank you!

wohe commented 3 years ago

In the context of local parameterizations in Ceres, delta is the delta in tangent space (and not angle-axis), see http://ceres-solver.org/nnls_modeling.html#localparameterization.

YawOnlyQuaternionPlus is a parametrization of a unit quaternion representing a rotation which can only rotate around the z axis. Thus in the specific case of YawOnlyQuaternionPlus, delta represents the change in yaw.