8 #ifndef _LIBALMATH_ALMATH_SCENEGRAPH_ALMATHEIGEN_H_
9 #define _LIBALMATH_ALMATH_SCENEGRAPH_ALMATHEIGEN_H_
14 #include <boost/range/begin.hpp>
15 #include <boost/range/size.hpp>
16 #include <Eigen/Dense>
22 typedef Eigen::Matrix<float, 3, 4, Eigen::RowMajor>
Matrix34frm;
27 return Eigen::Map<const Matrix34frm>(&tr.r1_c1);
31 return Matrix34f(Eigen::Map<const Matrix34frm>(&tr.r1_c1));
35 return Eigen::Matrix3f(
36 Eigen::Map<const Matrix34frm>(&tr.r1_c1).block<3, 3>(0, 0));
41 return Eigen::Quaternion<T>(
42 Eigen::Map<const Matrix34frm>(&tr.r1_c1).block<3, 3>(0, 0));
46 return Eigen::Vector3f(Eigen::Map<const Eigen::Vector3f>(&v.
x));
51 return Eigen::AffineCompact3f(Eigen::Map<const Matrix34frm>(&tr.r1_c1));
54 template <
typename Derived0>
57 (Eigen::internal::is_same<float, typename Derived0::Scalar>::value),
58 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
60 Eigen::Map<Matrix34frm>(&out.r1_c1) = in;
63 template <
typename Derived0>
70 template <
int _Mode,
int _Options>
73 Eigen::Map<Matrix34frm>(&out.r1_c1) = in.affine();
76 template <
int _Mode,
int _Options>
78 const Eigen::Transform<float, 3, _Mode, _Options> &tr) {
84 template <
typename Derived0>
87 (Eigen::internal::is_same<float, typename Derived0::Scalar>::value),
88 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
89 EIGEN_STATIC_ASSERT_MATRIX_SPECIFIC_SIZE(Derived0, 3, 1)
93 template <
typename Derived0>
97 (Eigen::internal::is_same<float, typename Derived0::Scalar>::value),
98 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
99 EIGEN_STATIC_ASSERT_MATRIX_SPECIFIC_SIZE(Derived0, 6, 1)
100 assert((in.rows() == 6) && (in.cols() == 1));
101 Eigen::Map<Vector6f>(&out.
xd) = in;
104 template <
typename Derived0>
107 (Eigen::internal::is_same<float, typename Derived0::Scalar>::value),
108 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
109 EIGEN_STATIC_ASSERT_MATRIX_SPECIFIC_SIZE(Derived0, 6, 1)
110 return Velocity6D(in[0], in[1], in[2], in[3], in[4], in[5]);
116 template <
class RandomAccessRange>
119 const auto numCumulated = boost::size(range);
120 if (numCumulated == 0)
122 throw std::runtime_error(
"Invalid empty range");
124 float averageFactor = 1.f /
static_cast<float>(numCumulated);
126 const auto firstQuat = Math::toEigenQuaternion<float>(*boost::begin(range));
127 Eigen::Vector3f cumulatedPosition = Eigen::Vector3f::Zero();
128 Eigen::Quaternion<float> cumulatedQuaternion{0.f, 0.f, 0.f, 0.f};
129 for (
const auto &tf : range)
134 auto newRotation = Math::toEigenQuaternion<float>(tf);
135 const auto areQuaternionsClose = newRotation.dot(firstQuat) >= 0.f;
136 if (!areQuaternionsClose)
139 newRotation.coeffs() = -newRotation.coeffs();
141 cumulatedQuaternion.coeffs() += newRotation.coeffs();
143 Eigen::Map<const Math::Matrix34frm>(&tf.r1_c1).block<3, 1>(0, 3);
145 cumulatedQuaternion.normalize();
147 Eigen::Map<Matrix34frm> retm(&ret.r1_c1);
148 retm.block<3, 3>(0, 0) = cumulatedQuaternion.toRotationMatrix();
149 retm.block<3, 1>(0, 3) = cumulatedPosition * averageFactor;
Math::Transform averageTransforms(const RandomAccessRange &range)
Create and play with a Velocity6D.
Eigen::Matrix< float, 3, 4, Eigen::RowMajor > Matrix34frm
void toALMathTransform(const Eigen::MatrixBase< Derived0 > &in, Transform &out)
Matrix34f toEigenMatrix34(const Math::Transform &tr)
Eigen::Matrix3f toEigenMatrix3(const Math::Transform &tr)
void toALMathVelocity6D(const Eigen::MatrixBase< Derived0 > &in, Velocity6D &out)
Eigen::Quaternion< T > toEigenQuaternion(const Math::Transform &tr)
Create and play with a Position3D.
Eigen::Vector3f toEigenVector3(const Math::Position3D &v)
Eigen::Matrix< float, 3, 4 > Matrix34f
Eigen::Map< const Matrix34frm > toEigenMapMatrix34(const Math::Transform &tr)
Position3D toALMathPosition3D(const Eigen::MatrixBase< Derived0 > &in)
Eigen::AffineCompact3f toEigenAffineCompact3(const Math::Transform &tr)
Eigen::Matrix< float, 6, 1 > Vector6f