Rigid Body Dynamics Library
rbdl_mathutils.h
Go to the documentation of this file.
1/*
2 * RBDL - Rigid Body Dynamics Library
3 * Copyright (c) 2011-2018 Martin Felis <martin@fysx.org>
4 *
5 * Licensed under the zlib license. See LICENSE for more details.
6 */
7
8#ifndef RBDL_MATHUTILS_H
9#define RBDL_MATHUTILS_H
10
11#include <assert.h>
12//#include <cmath>
13
14#include "rbdl/rbdl_math.h"
15
16namespace RigidBodyDynamics {
17struct Model;
18
19namespace Math {
20
27enum RBDL_DLLAPI LinearSolver {
34};
35
36extern RBDL_DLLAPI Vector3d Vector3dZero;
37extern RBDL_DLLAPI Matrix3d Matrix3dIdentity;
38extern RBDL_DLLAPI Matrix3d Matrix3dZero;
39
40RBDL_DLLAPI inline VectorNd VectorFromPtr (unsigned int n, Scalar *ptr) {
41 // TODO: use memory mapping operators for Eigen
42 VectorNd result (n);
43
44 for (unsigned int i = 0; i < n; i++) {
45 result[i] = ptr[i];
46 }
47
48 return result;
49}
50
51RBDL_DLLAPI inline MatrixNd MatrixFromPtr (unsigned int rows, unsigned int cols, Scalar *ptr, bool row_major = true) {
52 MatrixNd result (rows, cols);
53
54 if (row_major) {
55 for (unsigned int i = 0; i < rows; i++) {
56 for (unsigned int j = 0; j < cols; j++) {
57 result(i,j) = ptr[i * cols + j];
58 }
59 }
60 } else {
61 for (unsigned int i = 0; i < rows; i++) {
62 for (unsigned int j = 0; j < cols; j++) {
63 result(i,j) = ptr[i + j * rows];
64 }
65 }
66 }
67
68 return result;
69}
70
71#ifndef RBDL_USE_CASADI_MATH
73RBDL_DLLAPI bool LinSolveGaussElimPivot (MatrixNd A, VectorNd b, VectorNd &x);
74#endif
75
76// \todo write test
77RBDL_DLLAPI void SpatialMatrixSetSubmatrix(SpatialMatrix &dest, unsigned int row, unsigned int col, const Matrix3d &matrix);
78
79#ifndef RBDL_USE_CASADI_MATH
80RBDL_DLLAPI bool SpatialMatrixCompareEpsilon (const SpatialMatrix &matrix_a,
81 const SpatialMatrix &matrix_b, Scalar epsilon);
82RBDL_DLLAPI bool SpatialVectorCompareEpsilon (const SpatialVector &vector_a,
83 const SpatialVector &vector_b, Scalar epsilon);
84#endif
85
87RBDL_DLLAPI Matrix3d parallel_axis (const Matrix3d &inertia, Scalar mass, const Vector3d &com);
88
99RBDL_DLLAPI SpatialMatrix Xtrans_mat (const Vector3d &displacement);
100
108RBDL_DLLAPI SpatialMatrix Xrotz_mat (const Scalar &zrot);
109
117RBDL_DLLAPI SpatialMatrix Xroty_mat (const Scalar &yrot);
118
126RBDL_DLLAPI SpatialMatrix Xrotx_mat (const Scalar &xrot);
127
137RBDL_DLLAPI SpatialMatrix XtransRotZYXEuler (const Vector3d &displacement, const Vector3d &zyx_euler);
138
139RBDL_DLLAPI inline Matrix3d rotx (const Scalar &xrot) {
140 Scalar s, c;
141 s = sin (xrot);
142 c = cos (xrot);
143 return Matrix3d (
144 1., 0., 0.,
145 0., c, s,
146 0., -s, c
147 );
148}
149
150RBDL_DLLAPI inline Matrix3d roty (const Scalar &yrot) {
151 Scalar s, c;
152 s = sin (yrot);
153 c = cos (yrot);
154 return Matrix3d (
155 c, 0., -s,
156 0., 1., 0.,
157 s, 0., c
158 );
159}
160
161RBDL_DLLAPI inline Matrix3d rotz (const Scalar &zrot) {
162 Scalar s, c;
163 s = sin (zrot);
164 c = cos (zrot);
165 return Matrix3d (
166 c, s, 0.,
167 -s, c, 0.,
168 0., 0., 1.
169 );
170}
171
172RBDL_DLLAPI inline Matrix3d rotxdot (const Scalar &x, const Scalar &xdot) {
173 Scalar s, c;
174 s = sin (x);
175 c = cos (x);
176 return Matrix3d (
177 0., 0., 0.,
178 0., -s * xdot, c * xdot,
179 0., -c * xdot,-s * xdot
180 );
181}
182
183RBDL_DLLAPI inline Matrix3d rotydot (const Scalar &y, const Scalar &ydot) {
184 Scalar s, c;
185 s = sin (y);
186 c = cos (y);
187 return Matrix3d (
188 -s * ydot, 0., - c * ydot,
189 0., 0., 0.,
190 c * ydot, 0., - s * ydot
191 );
192}
193
194RBDL_DLLAPI inline Matrix3d rotzdot (const Scalar &z, const Scalar &zdot) {
195 Scalar s, c;
196 s = sin (z);
197 c = cos (z);
198 return Matrix3d (
199 -s * zdot, c * zdot, 0.,
200 -c * zdot, -s * zdot, 0.,
201 0., 0., 0.
202 );
203}
204
205RBDL_DLLAPI inline Vector3d angular_velocity_from_angle_rates (const Vector3d &zyx_angles, const Vector3d &zyx_angle_rates) {
206 Scalar sy = sin(zyx_angles[1]);
207 Scalar cy = cos(zyx_angles[1]);
208 Scalar sx = sin(zyx_angles[2]);
209 Scalar cx = cos(zyx_angles[2]);
210
211 return Vector3d (
212 zyx_angle_rates[2] - sy * zyx_angle_rates[0],
213 cx * zyx_angle_rates[1] + sx * cy * zyx_angle_rates[0],
214 -sx * zyx_angle_rates[1] + cx * cy * zyx_angle_rates[0]
215 );
216}
217
218RBDL_DLLAPI inline Vector3d global_angular_velocity_from_rates (const Vector3d &zyx_angles, const Vector3d &zyx_rates) {
219 Matrix3d RzT = rotz(zyx_angles[0]).transpose();
220 Matrix3d RyT = roty(zyx_angles[1]).transpose();
221
222 return Vector3d (
223 Vector3d (0., 0., zyx_rates[0])
224 + RzT * Vector3d (0., zyx_rates[1], 0.)
225 + RzT * RyT * Vector3d (zyx_rates[2], 0., 0.)
226 );
227}
228
229RBDL_DLLAPI inline Vector3d angular_acceleration_from_angle_rates (const Vector3d &zyx_angles, const Vector3d &zyx_angle_rates, const Vector3d &zyx_angle_rates_dot) {
230 Scalar sy = sin(zyx_angles[1]);
231 Scalar cy = cos(zyx_angles[1]);
232 Scalar sx = sin(zyx_angles[2]);
233 Scalar cx = cos(zyx_angles[2]);
234 Scalar xdot = zyx_angle_rates[2];
235 Scalar ydot = zyx_angle_rates[1];
236 Scalar zdot = zyx_angle_rates[0];
237 Scalar xddot = zyx_angle_rates_dot[2];
238 Scalar yddot = zyx_angle_rates_dot[1];
239 Scalar zddot = zyx_angle_rates_dot[0];
240
241 return Vector3d (
242 xddot - (cy * ydot * zdot + sy * zddot),
243 -sx * xdot * ydot + cx * yddot + cx * xdot * cy * zdot + sx * ( - sy * ydot * zdot + cy * zddot),
244 -cx * xdot * ydot - sx * yddot - sx * xdot * cy * zdot + cx * ( - sy * ydot * zdot + cy * zddot)
245 );
246}
247
248RBDL_DLLAPI
249void SparseFactorizeLTL (Model &model, Math::MatrixNd &H);
250
251RBDL_DLLAPI
252void SparseMultiplyHx (Model &model, Math::MatrixNd &L);
253
254RBDL_DLLAPI
255void SparseMultiplyLx (Model &model, Math::MatrixNd &L);
256RBDL_DLLAPI
257void SparseMultiplyLTx (Model &model, Math::MatrixNd &L);
258
259RBDL_DLLAPI
260void SparseSolveLx (Model &model, Math::MatrixNd &L, Math::VectorNd &x);
261RBDL_DLLAPI
262void SparseSolveLTx (Model &model, Math::MatrixNd &L, Math::VectorNd &x);
263
264} /* Math */
265
266} /* RigidBodyDynamics */
267
268/* RBDL_MATHUTILS_H */
269#endif
RBDL_DLLAPI Vector3d global_angular_velocity_from_rates(const Vector3d &zyx_angles, const Vector3d &zyx_rates)
RBDL_DLLAPI Matrix3d rotxdot(const Scalar &x, const Scalar &xdot)
RBDL_DLLAPI SpatialMatrix XtransRotZYXEuler(const Vector3d &displacement, const Vector3d &zyx_euler)
Creates a spatial transformation for given parameters.
RBDL_DLLAPI SpatialMatrix Xrotx_mat(const Scalar &xrot)
Creates a rotational transformation around the X-axis.
RBDL_DLLAPI Matrix3d rotz(const Scalar &zrot)
RBDL_DLLAPI Matrix3d rotx(const Scalar &xrot)
RBDL_DLLAPI Vector3d angular_acceleration_from_angle_rates(const Vector3d &zyx_angles, const Vector3d &zyx_angle_rates, const Vector3d &zyx_angle_rates_dot)
RBDL_DLLAPI void SpatialMatrixSetSubmatrix(SpatialMatrix &dest, unsigned int row, unsigned int col, const Matrix3d &matrix)
RBDL_DLLAPI VectorNd VectorFromPtr(unsigned int n, Scalar *ptr)
RBDL_DLLAPI MatrixNd MatrixFromPtr(unsigned int rows, unsigned int cols, Scalar *ptr, bool row_major=true)
RBDL_DLLAPI void SparseMultiplyLx(Model &model, Math::MatrixNd &L)
RBDL_DLLAPI Matrix3d rotydot(const Scalar &y, const Scalar &ydot)
RBDL_DLLAPI Matrix3d rotzdot(const Scalar &z, const Scalar &zdot)
RBDL_DLLAPI void SparseFactorizeLTL(Model &model, Math::MatrixNd &H)
RBDL_DLLAPI void SparseSolveLx(Model &model, Math::MatrixNd &L, Math::VectorNd &x)
RBDL_DLLAPI Matrix3d roty(const Scalar &yrot)
RBDL_DLLAPI bool LinSolveGaussElimPivot(MatrixNd A, VectorNd b, VectorNd &x)
Solves a linear system using gaussian elimination with pivoting.
RBDL_DLLAPI bool SpatialVectorCompareEpsilon(const SpatialVector &vector_a, const SpatialVector &vector_b, Scalar epsilon)
RBDL_DLLAPI void SparseMultiplyHx(Model &model, Math::MatrixNd &L)
RBDL_DLLAPI void SparseSolveLTx(Model &model, Math::MatrixNd &L, Math::VectorNd &x)
RBDL_DLLAPI SpatialMatrix Xroty_mat(const Scalar &yrot)
Creates a rotational transformation around the Y-axis.
RBDL_DLLAPI void SparseMultiplyLTx(Model &model, Math::MatrixNd &L)
RBDL_DLLAPI Matrix3d parallel_axis(const Matrix3d &inertia, Scalar mass, const Vector3d &com)
Translates the inertia matrix to a new center.
RBDL_DLLAPI SpatialMatrix Xrotz_mat(const Scalar &zrot)
Creates a rotational transformation around the Z-axis.
RBDL_DLLAPI SpatialMatrix Xtrans_mat(const Vector3d &r)
Creates a transformation of a linear displacement.
RBDL_DLLAPI Matrix3d Matrix3dZero
RBDL_DLLAPI Matrix3d Matrix3dIdentity
RBDL_DLLAPI Vector3d Vector3dZero
RBDL_DLLAPI Vector3d angular_velocity_from_angle_rates(const Vector3d &zyx_angles, const Vector3d &zyx_angle_rates)
RBDL_DLLAPI bool SpatialMatrixCompareEpsilon(const SpatialMatrix &matrix_a, const SpatialMatrix &matrix_b, Scalar epsilon)
MX_Xd_scalar cos(const MX_Xd_scalar &x)
Definition: MX_Xd_utils.h:351
MX_Xd_scalar sin(const MX_Xd_scalar &x)
Definition: MX_Xd_utils.h:345
LinearSolverPartialPivLU
LinearSolverColPivHouseholderQR
LinearSolverHouseholderQR
LinearSolverLLT
LinearSolverLast
LinearSolverUnknown