libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
inertia.hpp
1#pragma once
2
3#ifndef LIBXR_NO_EIGEN
4
5#include <Eigen/Core>
6#include <Eigen/Dense>
7#include <type_traits>
8
9#include "libxr_mem.hpp"
10#include "transform.hpp"
11
12namespace LibXR
13{
14
15using DefaultScalar = LIBXR_DEFAULT_SCALAR;
16
24template <typename Scalar = DefaultScalar>
26{
27 public:
28 Scalar data[9];
30 Scalar mass;
31
40 template <typename T, std::enable_if_t<std::is_same<T, Scalar>::value ||
41 std::is_same<T, float>::value ||
42 std::is_same<T, double>::value,
43 int> = 0>
44 explicit Inertia(Scalar m, const T (&values)[9]) : mass(m)
45 {
46 for (int i = 0; i < 3; i++)
47 {
48 for (int j = 0; j < 3; j++)
49 {
50 data[i * 3 + j] = static_cast<Scalar>(values[i * 3 + j]);
51 }
52 }
53 }
54
63 template <typename T, std::enable_if_t<std::is_same<T, Scalar>::value ||
64 std::is_same<T, float>::value ||
65 std::is_same<T, double>::value,
66 int> = 0>
67 explicit Inertia(Scalar m, const T (&matrix)[3][3]) : mass(m)
68 {
69 for (int i = 0; i < 3; i++)
70 {
71 for (int j = 0; j < 3; j++)
72 {
73 data[i * 3 + j] = static_cast<Scalar>(matrix[i][j]);
74 }
75 }
76 }
77
82 Inertia() : mass(0) { Memory::FastSet(data, 0, sizeof(data)); }
83
91 template <typename T,
92 std::enable_if_t<
93 std::is_same<T, float>::value || std::is_same<T, double>::value, int> = 0>
94 explicit Inertia(Scalar m, const T (&arr)[6])
95 : data{arr[0], -arr[3], -arr[5], -arr[3], arr[1],
96 -arr[4], -arr[5], -arr[4], arr[2]},
97 mass(m)
98 {
99 }
100
108 Inertia(Scalar m, Scalar xx, Scalar yy, Scalar zz, Scalar xy, Scalar yz, Scalar xz)
109 : data{xx, -xy, -xz, -xy, yy, -yz, -xz, -yz, zz}, mass(m)
110 {
111 }
112
119 Inertia(Scalar m, const Eigen::Matrix<Scalar, 3, 3>& R) : mass(m)
120 {
121 data[0] = R.data()[0];
122 data[1] = R.data()[1];
123 data[2] = R.data()[2];
124 data[3] = R.data()[3];
125 data[4] = R.data()[4];
126 data[5] = R.data()[5];
127 data[6] = R.data()[6];
128 data[7] = R.data()[7];
129 data[8] = R.data()[8];
130 }
131
133 operator Eigen::Matrix<Scalar, 3, 3>() const
134 {
135 return Eigen::Map<const Eigen::Matrix<Scalar, 3, 3>>(data);
136 }
137
140 Scalar operator()(int i, int j) const { return data[i + j * 3]; }
141
144
145 Eigen::Matrix<Scalar, 3, 3> operator+(const Eigen::Matrix<Scalar, 3, 3>& R) const
146 {
147 return Eigen::Map<const Eigen::Matrix<Scalar, 3, 3>>(data) + R;
148 }
149
155 Inertia Translate(const Eigen::Matrix<Scalar, 3, 1>& p) const
156 {
157 Scalar dx = p(0), dy = p(1), dz = p(2);
158 Eigen::Matrix<Scalar, 3, 3> translation_matrix;
159 translation_matrix << dy * dy + dz * dz, -dx * dy, -dx * dz, -dx * dy,
160 dx * dx + dz * dz, -dy * dz, -dx * dz, -dy * dz, dx * dx + dy * dy;
161
162 return Inertia(mass, Eigen::Map<const Eigen::Matrix<Scalar, 3, 3>>(data) +
163 mass * translation_matrix);
164 }
165
171 Inertia Rotate(const Eigen::Matrix<Scalar, 3, 3>& R) const
172 {
173 return Inertia(
174 mass, R * Eigen::Map<const Eigen::Matrix<Scalar, 3, 3>>(data) * R.transpose());
175 }
176
184 {
185 return Rotate(Eigen::Map<const Eigen::Matrix<Scalar, 3, 3>>(R.data_));
186 }
187
193 Inertia Rotate(const Eigen::Quaternion<Scalar>& q) const
194 {
195 return Inertia(
196 mass, q * Eigen::Map<const Eigen::Matrix<Scalar, 3, 3>>(data) * q.conjugate());
197 }
198
205 static Eigen::Matrix<Scalar, 3, 3> Rotate(const Eigen::Matrix<Scalar, 3, 3>& R,
206 const Eigen::Quaternion<Scalar>& q)
207 {
208 return q * R * q.conjugate();
209 }
210
218 {
219 return Rotate(Eigen::Quaternion<Scalar>(q));
220 }
221};
222
228template <typename Scalar = DefaultScalar>
230{
231 public:
232 Eigen::Matrix<Scalar, 3, 1> position;
233 Scalar mass;
234
237 CenterOfMass() : position(0., 0., 0.), mass(0.) {}
238
245 CenterOfMass(Scalar m, const LibXR::Position<Scalar>& p) : position(p), mass(m) {}
246
253 CenterOfMass(Scalar m, const Eigen::Matrix<Scalar, 3, 1>& p) : position(p), mass(m) {}
254
262 : position(p.translation), mass(m.mass)
263 {
264 }
265
272 {
273 Scalar new_mass = mass + m.mass;
274 return CenterOfMass(
275 new_mass,
276 Position<Scalar>((position(0) * mass + m.position(0) * m.mass) / new_mass,
277 (position(1) * mass + m.position(1) * m.mass) / new_mass,
278 (position(2) * mass + m.position(2) * m.mass) / new_mass));
279 }
280
287 {
288 *this = *this + m;
289 return *this;
290 }
291};
292
293} // namespace LibXR
294
295#endif
质心信息表示类。Represents the center of mass information.
Definition inertia.hpp:230
CenterOfMass()
默认构造函数,初始化质心位置为 (0,0,0),质量为 0。Default constructor initializing position to (0,0,0) and mass to 0.
Definition inertia.hpp:237
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:253
Eigen::Matrix< Scalar, 3, 1 > position
质心位置。Center of mass position.
Definition inertia.hpp:232
CenterOfMass & operator+=(const CenterOfMass &m)
质心累加运算符。Accumulation operator for center of mass.
Definition inertia.hpp:286
CenterOfMass(const Inertia< Scalar > &m, const Transform< Scalar > &p)
从惯性对象和变换构造质心对象。Constructs a center of mass object from inertia and transformation.
Definition inertia.hpp:261
CenterOfMass(Scalar m, const LibXR::Position< Scalar > &p)
使用质量和位置构造质心对象。Constructs a center of mass object using mass and position.
Definition inertia.hpp:245
Scalar mass
质量值。Mass value.
Definition inertia.hpp:233
CenterOfMass operator+(const CenterOfMass &m) const
计算两个质心的合成。Computes the combined center of mass.
Definition inertia.hpp:271
表示刚体的惯性张量和质量信息的类。Provides a class to represent the inertia tensor and mass of a rigid body.
Definition inertia.hpp:26
Inertia(Scalar m, const T(&values)[9])
使用质量和 3x3 惯性张量数组构造惯性对象。Constructs an inertia object using mass and a 3x3 inertia tensor array.
Definition inertia.hpp:44
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:108
Inertia Rotate(const Quaternion< Scalar > &q) const
使用自定义 Quaternion 旋转惯性张量。Rotates the inertia tensor using a custom Quaternion.
Definition inertia.hpp:217
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:205
Scalar mass
质量值。Mass value.
Definition inertia.hpp:30
Scalar data[9]
Definition inertia.hpp:28
Inertia Rotate(const RotationMatrix< Scalar > &R) const
使用 RotationMatrix 旋转惯性张量。Rotates the inertia tensor using a RotationMatrix.
Definition inertia.hpp:183
Inertia(Scalar m, const T(&matrix)[3][3])
使用质量和二维 3x3 数组构造惯性对象。Constructs an inertia object using mass and a 3x3 matrix.
Definition inertia.hpp:67
Inertia()
默认构造函数,初始化质量为0,惯性张量为零矩阵。Default constructor initializing mass to 0 and inertia tensor to zero.
Definition inertia.hpp:82
Inertia(Scalar m, const T(&arr)[6])
使用质量和 6 维数组(对称惯性矩阵)构造惯性对象。Constructs an inertia object using mass and a 6-element symmetric inertia m...
Definition inertia.hpp:94
Inertia Rotate(const Eigen::Quaternion< Scalar > &q) const
使用四元数旋转惯性张量。Rotates the inertia tensor using a quaternion.
Definition inertia.hpp:193
Scalar operator()(int i, int j) const
获取惯性张量中的特定元素。Retrieves a specific element from the inertia tensor.
Definition inertia.hpp:140
Inertia Rotate(const Eigen::Matrix< Scalar, 3, 3 > &R) const
旋转惯性张量。Rotates the inertia tensor.
Definition inertia.hpp:171
Inertia Translate(const Eigen::Matrix< Scalar, 3, 1 > &p) const
平移惯性对象。Translates the inertia object.
Definition inertia.hpp:155
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:145
Inertia(Scalar m, const Eigen::Matrix< Scalar, 3, 3 > &R)
使用 Eigen 3x3 矩阵构造惯性对象。Constructs an inertia object using an Eigen 3x3 matrix.
Definition inertia.hpp:119
static void FastSet(void *dst, uint8_t value, size_t size)
快速内存填充 / Fast memory fill
三维空间中的位置向量 / 3D position vector
Definition transform.hpp:39
四元数表示与运算,继承自 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 命名空间
Definition ch32_can.hpp:14