9 #ifndef LIBALMATH_SCENEGRAPH_QIGEOMETRY_H
10 #define LIBALMATH_SCENEGRAPH_QIGEOMETRY_H
13 #include <Eigen/Geometry>
21 return Vector3{x, y, z};
24 inline std::ostream&
operator<<(std::ostream &o,
const Vector3 &t)
26 return o <<
"Vector3(" << t.x <<
", " << t.y <<
", " << t.z <<
")";
31 return Quaternion{x, y, z, w};
37 Eigen::Map<Eigen::Quaterniond>(&result.x) =
38 Eigen::AngleAxisd(angle,
39 Eigen::Map<const Eigen::Vector3d>(&axis.x).normalized());
43 inline std::ostream&
operator<<(std::ostream &o,
const Quaternion &r)
45 return o <<
"Quaternion(" << r.x <<
", " << r.y <<
", " << r.z <<
", "
49 inline double norm(
const Quaternion &r)
51 return Eigen::Map<const Eigen::Quaterniond>(&r.x).
norm();
56 return std::abs(
norm(r) - 1.0) < epsilon;
62 Eigen::Map<Eigen::Quaterniond>(&r.x).
normalize();
68 Quaternion result = r;
74 Transform
makeTransform(
const Quaternion &rotation,
const Vector3 &translation)
76 return Transform{rotation, translation};
80 inline std::ostream&
operator<<(std::ostream &o,
const Transform &tf)
82 return o <<
"Transform(" << tf.rotation <<
", " << tf.translation <<
")";
89 const auto& t = tf.translation;
90 const auto& r = tf.rotation;
91 return Eigen::Affine3d(Eigen::Translation3d(t.x, t.y, t.z) *
92 Eigen::Quaterniond(r.w, r.x, r.y, r.z));
96 inline Transform
operator*(
const Transform &lhs,
const Transform &rhs)
101 auto lv = Eigen::Map<const Eigen::Vector3d>(&lhs.translation.x);
102 auto rv = Eigen::Map<const Eigen::Vector3d>(&rhs.translation.x);
103 auto lq = Eigen::Map<const Eigen::Quaterniond>(&lhs.rotation.x);
104 auto rq = Eigen::Map<const Eigen::Quaterniond>(&rhs.rotation.x);
105 Eigen::Map<Eigen::Quaterniond>(&result.rotation.x) = lq * rq;
106 Eigen::Map<Eigen::Vector3d>(&result.translation.x) = lv + lq * rv;
111 inline bool isNear(
const Transform &lhs,
const Transform &rhs,
double epsilon)
115 return la.isApprox(ra, epsilon);
124 Eigen::Map<Eigen::Quaterniond>(&result.rotation.x) = inv.rotation();
125 Eigen::Map<Eigen::Vector3d>(&result.translation.x) = inv.translation();
std::ostream & operator<<(std::ostream &o, const Vector3 &t)
Quaternion normalized(const Quaternion &r)
bool isNormalized(const Quaternion &r, double epsilon)
void normalize(Quaternion &r)
Quaternion makeQuaternion(double x, double y, double z, double w)
bool isNear(const Transform &lhs, const Transform &rhs, double epsilon)
Transform inverse(const Transform &tf)
Vector3 makeVector3(double x, double y, double z)
Transform operator*(const Transform &lhs, const Transform &rhs)
Quaternion makeQuaternionFromAngleAxis(double angle, const Vector3 &axis)
double norm(const Quaternion &r)
Eigen::Affine3d toEigenAffine3d(const Transform &tf)
Transform makeTransform(const Quaternion &rotation, const Vector3 &translation)