236 lines
7.6 KiB
C++
236 lines
7.6 KiB
C++
#include "vslam/geometry.h"
|
|
|
|
#include <algorithm>
|
|
|
|
namespace vslam {
|
|
namespace {
|
|
|
|
constexpr double kPi = 3.14159265358979323846;
|
|
constexpr double kTwoPi = 2.0 * kPi;
|
|
|
|
} // namespace
|
|
|
|
// --- Goc -------------------------------------------------------------------
|
|
|
|
double NormalizeAngle(double a) {
|
|
// Port tu wheel_odom_node.cpp:45-50. Ban goc dung vong while; std::fmod
|
|
// nhanh hon va khong treo khi input la mot so rat lon (vd NaN lan vao tu
|
|
// encoder hong -> vong while chay vo han).
|
|
if (!std::isfinite(a)) return 0.0;
|
|
a = std::fmod(a + kPi, kTwoPi);
|
|
if (a < 0.0) a += kTwoPi;
|
|
return a - kPi;
|
|
}
|
|
|
|
// --- Quaternion ------------------------------------------------------------
|
|
|
|
Quaternion QuaternionFromYaw(double yaw) {
|
|
const double h = yaw * 0.5;
|
|
return Quaternion{0.0, 0.0, std::sin(h), std::cos(h)};
|
|
}
|
|
|
|
Quaternion QuaternionFromRPY(double roll, double pitch, double yaw) {
|
|
const double cr = std::cos(roll * 0.5), sr = std::sin(roll * 0.5);
|
|
const double cp = std::cos(pitch * 0.5), sp = std::sin(pitch * 0.5);
|
|
const double cy = std::cos(yaw * 0.5), sy = std::sin(yaw * 0.5);
|
|
Quaternion q;
|
|
q.w = cr * cp * cy + sr * sp * sy;
|
|
q.x = sr * cp * cy - cr * sp * sy;
|
|
q.y = cr * sp * cy + sr * cp * sy;
|
|
q.z = cr * cp * sy - sr * sp * cy;
|
|
return q;
|
|
}
|
|
|
|
double YawFromQuaternion(const Quaternion& q) {
|
|
const double siny = 2.0 * (q.w * q.z + q.x * q.y);
|
|
const double cosy = 1.0 - 2.0 * (q.y * q.y + q.z * q.z);
|
|
return std::atan2(siny, cosy);
|
|
}
|
|
|
|
void RPYFromQuaternion(const Quaternion& q, double* roll, double* pitch, double* yaw) {
|
|
const double sinr = 2.0 * (q.w * q.x + q.y * q.z);
|
|
const double cosr = 1.0 - 2.0 * (q.x * q.x + q.y * q.y);
|
|
if (roll) *roll = std::atan2(sinr, cosr);
|
|
|
|
// Kep truoc khi asin: sai so lam tron co the day gia tri ra ngoai [-1,1]
|
|
// va asin tra NaN. NaN o pitch lan vao gravity_align roi vao TF la mot
|
|
// trong nhung duong lan truyen loi kho truy nhat.
|
|
double sinp = 2.0 * (q.w * q.y - q.z * q.x);
|
|
sinp = std::max(-1.0, std::min(1.0, sinp));
|
|
if (pitch) *pitch = std::asin(sinp);
|
|
|
|
if (yaw) *yaw = YawFromQuaternion(q);
|
|
}
|
|
|
|
Quaternion QuaternionNormalized(const Quaternion& q) {
|
|
const double n = std::sqrt(q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w);
|
|
if (n < 1e-12) return Quaternion{0.0, 0.0, 0.0, 1.0};
|
|
const double inv = 1.0 / n;
|
|
return Quaternion{q.x * inv, q.y * inv, q.z * inv, q.w * inv};
|
|
}
|
|
|
|
Quaternion QuaternionInverse(const Quaternion& q) {
|
|
// Gia dinh q da chuan hoa -> nghich dao = lien hop.
|
|
return Quaternion{-q.x, -q.y, -q.z, q.w};
|
|
}
|
|
|
|
Quaternion QuaternionMultiply(const Quaternion& a, const Quaternion& b) {
|
|
Quaternion r;
|
|
r.w = a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z;
|
|
r.x = a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y;
|
|
r.y = a.w * b.y - a.x * b.z + a.y * b.w + a.z * b.x;
|
|
r.z = a.w * b.z + a.x * b.y - a.y * b.x + a.z * b.w;
|
|
return r;
|
|
}
|
|
|
|
Quaternion QuaternionSlerp(const Quaternion& a, const Quaternion& b, double t) {
|
|
t = std::max(0.0, std::min(1.0, t));
|
|
|
|
double dot = a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
|
|
|
|
// Lat dau de di duong ngan. Thieu buoc nay thi noi suy qua mot goc gan 360
|
|
// do thay vi goc nho -- TF se "quay nguoc" mot cach ngoan muc.
|
|
Quaternion bb = b;
|
|
if (dot < 0.0) {
|
|
bb = Quaternion{-b.x, -b.y, -b.z, -b.w};
|
|
dot = -dot;
|
|
}
|
|
|
|
// Gan trung nhau: LERP roi chuan hoa, tranh chia cho sin(theta) ~ 0.
|
|
if (dot > 0.9995) {
|
|
Quaternion r{a.x + (bb.x - a.x) * t, a.y + (bb.y - a.y) * t,
|
|
a.z + (bb.z - a.z) * t, a.w + (bb.w - a.w) * t};
|
|
return QuaternionNormalized(r);
|
|
}
|
|
|
|
const double theta_0 = std::acos(dot);
|
|
const double theta = theta_0 * t;
|
|
const double sin_theta_0 = std::sin(theta_0);
|
|
const double s0 = std::cos(theta) - dot * std::sin(theta) / sin_theta_0;
|
|
const double s1 = std::sin(theta) / sin_theta_0;
|
|
|
|
return Quaternion{a.x * s0 + bb.x * s1, a.y * s0 + bb.y * s1,
|
|
a.z * s0 + bb.z * s1, a.w * s0 + bb.w * s1};
|
|
}
|
|
|
|
Vector3 RotateVector(const Quaternion& q, const Vector3& v) {
|
|
// v' = v + 2w(u x v) + 2(u x (u x v)), voi u la phan vector cua q.
|
|
// Re hon chuyen sang ma tran quay khi chi xoay mot vai vector.
|
|
const double ux = q.x, uy = q.y, uz = q.z, w = q.w;
|
|
const double cx = uy * v.z - uz * v.y;
|
|
const double cy = uz * v.x - ux * v.z;
|
|
const double cz = ux * v.y - uy * v.x;
|
|
const double ccx = uy * cz - uz * cy;
|
|
const double ccy = uz * cx - ux * cz;
|
|
const double ccz = ux * cy - uy * cx;
|
|
return Vector3{v.x + 2.0 * (w * cx + ccx),
|
|
v.y + 2.0 * (w * cy + ccy),
|
|
v.z + 2.0 * (w * cz + ccz)};
|
|
}
|
|
|
|
// --- Transform -------------------------------------------------------------
|
|
|
|
Transform TransformIdentity() {
|
|
Transform t;
|
|
t.translation = Vector3{0.0, 0.0, 0.0};
|
|
t.rotation = Quaternion{0.0, 0.0, 0.0, 1.0};
|
|
return t;
|
|
}
|
|
|
|
Transform TransformCompose(const Transform& a, const Transform& b) {
|
|
Transform r;
|
|
r.rotation = QuaternionMultiply(a.rotation, b.rotation);
|
|
const Vector3 rb = RotateVector(a.rotation, b.translation);
|
|
r.translation = a.translation + rb;
|
|
return r;
|
|
}
|
|
|
|
Transform TransformInverse(const Transform& t) {
|
|
Transform r;
|
|
r.rotation = QuaternionInverse(t.rotation);
|
|
const Vector3 neg{-t.translation.x, -t.translation.y, -t.translation.z};
|
|
r.translation = RotateVector(r.rotation, neg);
|
|
return r;
|
|
}
|
|
|
|
Point TransformPoint(const Transform& t, const Point& p) {
|
|
const Vector3 v{p.x, p.y, p.z};
|
|
const Vector3 r = RotateVector(t.rotation, v) + t.translation;
|
|
return Point{r.x, r.y, r.z};
|
|
}
|
|
|
|
Vector3 TransformVector(const Transform& t, const Vector3& v) {
|
|
return RotateVector(t.rotation, v) + t.translation;
|
|
}
|
|
|
|
Transform TransformInterpolate(const Transform& a, const Transform& b, double t) {
|
|
Transform r;
|
|
r.translation = Lerp(a.translation, b.translation, t);
|
|
r.rotation = QuaternionSlerp(a.rotation, b.rotation, t);
|
|
return r;
|
|
}
|
|
|
|
// --- SE(2) -----------------------------------------------------------------
|
|
|
|
Pose2D Pose2DFromPose(const Pose& p) {
|
|
return Pose2D{p.position.x, p.position.y, YawFromQuaternion(p.orientation)};
|
|
}
|
|
|
|
Pose PoseFromPose2D(const Pose2D& p, double z) {
|
|
Pose out;
|
|
out.position = Point{p.x, p.y, z};
|
|
out.orientation = QuaternionFromYaw(p.theta);
|
|
return out;
|
|
}
|
|
|
|
Pose2D Pose2DCompose(const Pose2D& a, const Pose2D& b) {
|
|
const double c = std::cos(a.theta), s = std::sin(a.theta);
|
|
return Pose2D{a.x + c * b.x - s * b.y,
|
|
a.y + s * b.x + c * b.y,
|
|
NormalizeAngle(a.theta + b.theta)};
|
|
}
|
|
|
|
Pose2D Pose2DInverse(const Pose2D& p) {
|
|
const double c = std::cos(p.theta), s = std::sin(p.theta);
|
|
return Pose2D{-(c * p.x + s * p.y),
|
|
-(-s * p.x + c * p.y),
|
|
NormalizeAngle(-p.theta)};
|
|
}
|
|
|
|
Pose2D Pose2DBetween(const Pose2D& a, const Pose2D& b) {
|
|
// a^-1 * b, viet truc tiep thay vi ghep hai ham -- it phep tinh hon va
|
|
// day la duong nong khi tinh delta giua hai keyframe.
|
|
const double c = std::cos(a.theta), s = std::sin(a.theta);
|
|
const double dx = b.x - a.x, dy = b.y - a.y;
|
|
return Pose2D{c * dx + s * dy,
|
|
-s * dx + c * dy,
|
|
NormalizeAngle(b.theta - a.theta)};
|
|
}
|
|
|
|
Transform TransformFromPose2D(const Pose2D& p, double z) {
|
|
Transform t;
|
|
t.translation = Vector3{p.x, p.y, z};
|
|
t.rotation = QuaternionFromYaw(p.theta);
|
|
return t;
|
|
}
|
|
|
|
Pose2D Pose2DFromTransform(const Transform& t) {
|
|
return Pose2D{t.translation.x, t.translation.y, YawFromQuaternion(t.rotation)};
|
|
}
|
|
|
|
Transform TransformFromPose(const Pose& p) {
|
|
Transform t;
|
|
t.translation = Vector3{p.position.x, p.position.y, p.position.z};
|
|
t.rotation = p.orientation;
|
|
return t;
|
|
}
|
|
|
|
Pose PoseFromTransform(const Transform& t) {
|
|
Pose p;
|
|
p.position = Point{t.translation.x, t.translation.y, t.translation.z};
|
|
p.orientation = t.rotation;
|
|
return p;
|
|
}
|
|
|
|
} // namespace vslam
|