libxr 1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
inertia.hpp
1#pragma once
2
3#include <Eigen/Core>
4#include <Eigen/Dense>
5#include <type_traits>
6
7#include "transform.hpp"
8
9namespace LibXR
10{
11
12using DefaultScalar = LIBXR_DEFAULT_SCALAR;
13
21template <typename Scalar = DefaultScalar>
23{
24 public:
28
38 std::is_same<T, float>::value ||
39 std::is_same<T, double>::value,
40 int> = 0>
41 explicit Inertia(Scalar m, const T (&data)[9]) : mass(m)
42 {
43 for (int i = 0; i < 3; i++)
44 {
45 for (int j = 0; j < 3; j++)
46 {
47 data[i * 3 + j] = static_cast<Scalar>(data[i + j + 3]);
48 }
49 }
50 }
51
61 std::is_same<T, float>::value ||
62 std::is_same<T, double>::value,
63 int> = 0>
64 explicit Inertia(Scalar m, const T (&data)[3][3]) : mass(m)
65 {
66 for (int i = 0; i < 3; i++)
67 {
68 for (int j = 0; j < 3; j++)
69 {
70 data[i * 3 + j] = static_cast<Scalar>(data[j][i]);
71 }
72 }
73 }
74
79 Inertia() : mass(0) { memset(data, 0, sizeof(data)); }
80
88 template <typename T,
89 std::enable_if_t<
90 std::is_same<T, float>::value || std::is_same<T, double>::value, int> = 0>
91 explicit Inertia(Scalar m, const T (&data)[6])
92 : data{data[0], -data[3], -data[5], -data[3], data[2],
93 -data[4], -data[5], -data[4], data[2]},
94 mass(m)
95 {
96 }
97
106 : data{xx, -xy, -xz, -xy, yy, -yz, -xz, -yz, zz}, mass(m)
107 {
108 }
109
116 Inertia(Scalar m, const Eigen::Matrix<Scalar, 3, 3> &R) : mass(m)
117 {
118 memcpy(data, R.data(), 9 * sizeof(Scalar));
119 }
120
122 operator Eigen::Matrix<Scalar, 3, 3>() const
123 {
124 return Eigen::Map<const Eigen::Matrix<Scalar, 3, 3>>(data);
125 }
126
129 Scalar operator()(int i, int j) const { return data[i + j * 3]; }
130
133
134 Eigen::Matrix<Scalar, 3, 3> operator+(const Eigen::Matrix<Scalar, 3, 3> &R) const
135 {
136 return Eigen::Map<const Eigen::Matrix<Scalar, 3, 3>>(data) + R;
137 }
138
144 Inertia Translate(const Eigen::Matrix<Scalar, 3, 1> &p) const
145 {
146 Scalar dx = p(0), dy = p(1), dz = p(2);
147 Eigen::Matrix<Scalar, 3, 3> translation_matrix;
148 translation_matrix << dy * dy + dz * dz, -dx * dy, -dx * dz, -dx * dy,
149 dx * dx + dz * dz, -dy * dz, -dx * dz, -dy * dz, dx * dx + dy * dy;
150
151 return Inertia(mass, Eigen::Map<const Eigen::Matrix<Scalar, 3, 3>>(data) +
153 }
154
160 Inertia Rotate(const Eigen::Matrix<Scalar, 3, 3> &R) const
161 {
162 return Inertia(
163 mass, R * Eigen::Map<const Eigen::Matrix<Scalar, 3, 3>>(data) * R.transpose());
164 }
165
173 {
174 return Rotate(Eigen::Map<const Eigen::Matrix<Scalar, 3, 3>>(R.data_));
175 }
176
182 Inertia Rotate(const Eigen::Quaternion<Scalar> &q) const
183 {
184 return Inertia(
185 mass, q * Eigen::Map<const Eigen::Matrix<Scalar, 3, 3>>(data) * q.conjugate());
186 }
187
194 static Eigen::Matrix<Scalar, 3, 3> Rotate(const Eigen::Matrix<Scalar, 3, 3> &R,
195 const Eigen::Quaternion<Scalar> &q)
196 {
197 return q * R * q.conjugate();
198 }
199
207 {
208 return Rotate(Eigen::Quaternion<Scalar>(q));
209 }
210};
211
217template <typename Scalar = DefaultScalar>
219{
220 public:
221 Eigen::Matrix<Scalar, 3, 1> position;
223
226 CenterOfMass() : position(0., 0., 0.), mass(0.) {}
227
235
242 CenterOfMass(Scalar m, const Eigen::Matrix<Scalar, 3, 1> &p) : position(p), mass(m) {}
243
251 : position(p.translation), mass(m.mass)
252 {
253 }
254
261 {
263 return CenterOfMass(
264 new_mass,
265 Position<Scalar>((position(0) * mass + m.position(0) * m.mass) / new_mass,
266 (position(1) * mass + m.position(1) * m.mass) / new_mass,
267 (position(2) * mass + m.position(2) * m.mass) / new_mass));
268 }
269
276 {
277 *this = *this + m;
278 return *this;
279 }
280};
281
282} // namespace LibXR
质心信息表示类。Represents the center of mass information.
Definition inertia.hpp:219
CenterOfMass()
默认构造函数,初始化质心位置为 (0,0,0),质量为 0。Default constructor initializing position to (0,0,0) and mass to 0.
Definition inertia.hpp:226
CenterOfMass(Scalar m, const Eigen::Matrix< Scalar, 3, 1 > &p)
使用质量和 Eigen 3D 向量构造质心对象。Constructs a center of mass object using mass and Eigen 3D vector.
Definition inertia.hpp:242
Eigen::Matrix< Scalar, 3, 1 > position
质心位置。Center of mass position.
Definition inertia.hpp:221
CenterOfMass & operator+=(const CenterOfMass &m)
质心累加运算符。Accumulation operator for center of mass.
Definition inertia.hpp:275
CenterOfMass(const Inertia< Scalar > &m, const Transform< Scalar > &p)
从惯性对象和变换构造质心对象。Constructs a center of mass object from inertia and transformation.
Definition inertia.hpp:250
CenterOfMass(Scalar m, const LibXR::Position< Scalar > &p)
使用质量和位置构造质心对象。Constructs a center of mass object using mass and position.
Definition inertia.hpp:234
Scalar mass
质量值。Mass value.
Definition inertia.hpp:222
CenterOfMass operator+(const CenterOfMass &m) const
计算两个质心的合成。Computes the combined center of mass.
Definition inertia.hpp:260
表示刚体的惯性张量和质量信息的类。Provides a class to represent the inertia tensor and mass of a rigid body.
Definition inertia.hpp:23
Inertia(Scalar m, Scalar xx, Scalar yy, Scalar zz, Scalar xy, Scalar yz, Scalar xz)
直接指定质量和惯性张量分量构造惯性对象。Constructs an inertia object with mass and specified inertia tensor elements.
Definition inertia.hpp:105
Inertia(Scalar m, const T(&data)[9])
使用质量和 3x3 惯性张量数组构造惯性对象。Constructs an inertia object using mass and a 3x3 inertia tensor array.
Definition inertia.hpp:41
Inertia Rotate(const Quaternion< Scalar > &q) const
使用自定义 Quaternion 旋转惯性张量。Rotates the inertia tensor using a custom Quaternion.
Definition inertia.hpp:206
Inertia(Scalar m, const T(&data)[3][3])
使用质量和二维 3x3 数组构造惯性对象。Constructs an inertia object using mass and a 3x3 matrix.
Definition inertia.hpp:64
static Eigen::Matrix< Scalar, 3, 3 > Rotate(const Eigen::Matrix< Scalar, 3, 3 > &R, const Eigen::Quaternion< Scalar > &q)
使用四元数旋转 3x3 矩阵。Rotates a 3x3 matrix using a quaternion.
Definition inertia.hpp:194
Scalar mass
质量值。Mass value.
Definition inertia.hpp:27
Scalar data[9]
Definition inertia.hpp:25
Inertia(Scalar m, const T(&data)[6])
使用质量和 6 维数组(对称惯性矩阵)构造惯性对象。Constructs an inertia object using mass and a 6-element symmetric inertia m...
Definition inertia.hpp:91
Inertia Rotate(const RotationMatrix< Scalar > &R) const
使用 RotationMatrix 旋转惯性张量。Rotates the inertia tensor using a RotationMatrix.
Definition inertia.hpp:172
Inertia()
默认构造函数,初始化质量为0,惯性张量为零矩阵。Default constructor initializing mass to 0 and inertia tensor to zero.
Definition inertia.hpp:79
Inertia Rotate(const Eigen::Quaternion< Scalar > &q) const
使用四元数旋转惯性张量。Rotates the inertia tensor using a quaternion.
Definition inertia.hpp:182
Scalar operator()(int i, int j) const
获取惯性张量中的特定元素。Retrieves a specific element from the inertia tensor.
Definition inertia.hpp:129
Inertia Rotate(const Eigen::Matrix< Scalar, 3, 3 > &R) const
旋转惯性张量。Rotates the inertia tensor.
Definition inertia.hpp:160
Inertia Translate(const Eigen::Matrix< Scalar, 3, 1 > &p) const
平移惯性对象。Translates the inertia object.
Definition inertia.hpp:144
Eigen::Matrix< Scalar, 3, 3 > operator+(const Eigen::Matrix< Scalar, 3, 3 > &R) const
将惯性张量与另一个 3x3 矩阵相加。Adds the inertia tensor with another 3x3 matrix.
Definition inertia.hpp:134
Inertia(Scalar m, const Eigen::Matrix< Scalar, 3, 3 > &R)
使用 Eigen 3x3 矩阵构造惯性对象。Constructs an inertia object using an Eigen 3x3 matrix.
Definition inertia.hpp:116
三维空间中的位置向量 / 3D position vector
Definition transform.hpp:37
四元数表示与运算,继承自 Eigen::Quaternion / Quaternion representation and operations, inheriting from Eigen::Qua...
旋转矩阵类,继承自 Eigen::Matrix<Scalar, 3, 3>。 Rotation matrix class, inheriting from Eigen::Matrix<Scalar,...
表示三维空间中的刚体变换,包括旋转和位移。Represents rigid body transformations in 3D space, including rotation and transl...
LibXR Color Control Library / LibXR终端颜色控制库
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值