Skip to content

Commit 6cf212d

Browse files
authored
Merge pull request #520 from omeredemen/master
State_JPLQuatLocal class has been updated
2 parents d0075dd + 676042f commit 6cf212d

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

ov_init/src/ceres/State_JPLQuatLocal.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,51 @@ bool State_JPLQuatLocal::Plus(const double *x, const double *delta, double *x_pl
4848
return true;
4949
}
5050

51+
#if CERES_VERSION_MAJOR == 2 && CERES_VERSION_MINOR >= 2
52+
bool State_JPLQuatLocal::PlusJacobian(const double *x, double *jacobian) const {
53+
Eigen::Map<Eigen::Matrix<double, 4, 3, Eigen::RowMajor>> j(jacobian);
54+
j.topRows<3>().setIdentity();
55+
j.bottomRows<1>().setZero();
56+
return true;
57+
}
58+
59+
bool State_JPLQuatLocal::Minus(const double *y, const double *x, double *delta) const {
60+
Eigen::Map<const Eigen::Vector4d> q1(x);
61+
Eigen::Map<const Eigen::Vector4d> q2(y);
62+
Eigen::Vector4d q_rel = ov_core::quat_multiply(q2, ov_core::Inv(q1));
63+
Eigen::Vector3d omega;
64+
65+
double qw = q_rel(3);
66+
Eigen::Vector3d qv = q_rel.head<3>();
67+
68+
double norm_qv = qv.norm();
69+
if (norm_qv < 1e-8) {
70+
omega.setZero();
71+
} else {
72+
double angle = 2.0 * std::atan2(norm_qv, qw);
73+
omega = angle * (qv / norm_qv);
74+
}
75+
76+
Eigen::Map<Eigen::Vector3d> d_out(delta);
77+
d_out = omega;
78+
return true;
79+
}
80+
81+
bool State_JPLQuatLocal::MinusJacobian(const double *x, double *jacobian) const {
82+
// This is an approximation: ∂delta/∂x ≈ [I; 0]
83+
Eigen::Map<Eigen::Matrix<double, 3, 4, Eigen::RowMajor>> j(jacobian);
84+
j.setZero();
85+
j.leftCols<3>().setIdentity();
86+
return true;
87+
}
88+
89+
#else
90+
5191
bool State_JPLQuatLocal::ComputeJacobian(const double *x, double *jacobian) const {
5292
Eigen::Map<Eigen::Matrix<double, 4, 3, Eigen::RowMajor>> j(jacobian);
5393
j.topRows<3>().setIdentity();
5494
j.bottomRows<1>().setZero();
5595
return true;
5696
}
97+
98+
#endif

ov_init/src/ceres/State_JPLQuatLocal.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,20 @@
2424

2525
#include <ceres/ceres.h>
2626

27+
#if CERES_VERSION_MAJOR == 2 && CERES_VERSION_MINOR >= 2
28+
#include <ceres/manifold.h>
29+
#endif
30+
2731
namespace ov_init {
2832

2933
/**
3034
* @brief JPL quaternion CERES state parameterization
3135
*/
36+
#if CERES_VERSION_MAJOR == 2 && CERES_VERSION_MINOR >= 2
37+
class State_JPLQuatLocal : public ceres::Manifold {
38+
#else
3239
class State_JPLQuatLocal : public ceres::LocalParameterization {
40+
#endif
3341
public:
3442
/**
3543
* @brief State update function for a JPL quaternion representation.
@@ -43,6 +51,21 @@ class State_JPLQuatLocal : public ceres::LocalParameterization {
4351
*/
4452
bool Plus(const double *x, const double *delta, double *x_plus_delta) const override;
4553

54+
#if CERES_VERSION_MAJOR == 2 && CERES_VERSION_MINOR >= 2
55+
56+
bool PlusJacobian(const double *x, double *jacobian) const override;
57+
58+
// Inverse update: delta = Log(q2 ⊗ inv(q1))
59+
bool Minus(const double* y, const double* x, double* delta) const override;
60+
61+
// Jacobian of Minus
62+
bool MinusJacobian(const double* x, double* jacobian) const override;
63+
64+
int AmbientSize() const override { return 4; }
65+
int TangentSize() const override { return 3; }
66+
67+
#else
68+
4669
/**
4770
* @brief Computes the jacobian in respect to the local parameterization
4871
*
@@ -59,6 +82,9 @@ class State_JPLQuatLocal : public ceres::LocalParameterization {
5982
int GlobalSize() const override { return 4; };
6083

6184
int LocalSize() const override { return 3; };
85+
86+
#endif
87+
6288
};
6389

6490
} // namespace ov_init

0 commit comments

Comments
 (0)