Biorbd
RotoTrans.cpp
1 #define BIORBD_API_EXPORTS
2 #include "Utils/RotoTrans.h"
3 
4 #include "Utils/Error.h"
5 #include "Utils/Vector3d.h"
6 #include "Utils/String.h"
7 #include "Utils/Vector.h"
8 #include "Utils/Rotation.h"
9 
10 #include "RigidBody/NodeSegment.h"
11 
13  const RigidBodyDynamics::Math::Matrix4d& matrix) :
14  RigidBodyDynamics::Math::Matrix4d(matrix)
15 {
16  checkUnitary();
17 }
18 
20  const biorbd::utils::Scalar& v00, const biorbd::utils::Scalar& v01, const biorbd::utils::Scalar& v02, const biorbd::utils::Scalar& v03,
21  const biorbd::utils::Scalar& v10, const biorbd::utils::Scalar& v11, const biorbd::utils::Scalar& v12, const biorbd::utils::Scalar& v13,
22  const biorbd::utils::Scalar& v20, const biorbd::utils::Scalar& v21, const biorbd::utils::Scalar& v22, const biorbd::utils::Scalar& v23,
23  const biorbd::utils::Scalar& v30, const biorbd::utils::Scalar& v31, const biorbd::utils::Scalar& v32, const biorbd::utils::Scalar& v33) :
24  RigidBodyDynamics::Math::Matrix4d (v00, v01, v02, v03,
25  v10, v11, v12, v13,
26  v20, v21, v22, v23,
27  v30, v31, v32, v33)
28 {
29  checkUnitary();
30 }
31 
33  const biorbd::utils::Rotation& rot) :
34  RigidBodyDynamics::Math::Matrix4d(combineRotAndTrans(rot, biorbd::utils::Vector3d::Zero()))
35 {
36 
37 }
38 
40  const biorbd::utils::Rotation& rot,
41  const biorbd::utils::Vector3d& trans) :
42  RigidBodyDynamics::Math::Matrix4d(combineRotAndTrans(rot,trans))
43 {
44 
45 }
46 
48  const biorbd::utils::Vector& rotation,
49  const biorbd::utils::Vector3d& translation,
50  const biorbd::utils::String& rotationSequence) :
51  RigidBodyDynamics::Math::Matrix4d(fromEulerAngles(rotation, translation, rotationSequence))
52 {
53 
54 }
55 
57  const RigidBodyDynamics::Math::SpatialTransform& st) :
58  RigidBodyDynamics::Math::Matrix4d(fromSpatialTransform(st))
59 {
60 
61 }
62 
64  const biorbd::rigidbody::NodeSegment& origin,
65  const std::pair<biorbd::rigidbody::NodeSegment, biorbd::rigidbody::NodeSegment> &axis1markers,
66  const std::pair<biorbd::rigidbody::NodeSegment, biorbd::rigidbody::NodeSegment> &axis2markers,
67  const std::pair<biorbd::utils::String, biorbd::utils::String>& axesNames,
68  const biorbd::utils::String &axisToRecalculate)
69 {
70  RotoTrans rt_out;
71  rt_out.block(0, 0, 3, 3) = Rotation::fromMarkers(axis1markers, axis2markers, axesNames, axisToRecalculate);
72  rt_out.block(0, 3, 3, 1) = origin;
73  return rt_out;
74 }
75 
77 {
79  idx<=2, "Axis must be between 0 and 2 included");
80  return static_cast<RigidBodyDynamics::Math::VectorNd>(rot().block(0,idx,3,1));
81 }
82 
84 {
86  tp.block(0, 0, 3, 3) = block(0, 0, 3, 3).transpose();
87  tp.block(0, 3, 3, 1) = -tp.block(0, 0, 3, 3) * block(0, 3, 3, 1);
88  tp.block(3, 0, 1, 4) = RigidBodyDynamics::Math::Vector4d(0, 0, 0, 1).transpose();
89  return tp;
90 }
91 
93 {
94  return this->block<3, 1>(0,3);
95 }
96 
98 {
99  return this->block<3, 3>(0,0);
100 }
101 
103  const biorbd::utils::Rotation& rot,
104  const biorbd::utils::Vector3d& trans){
106  out.block(0,0,3,3) = rot;
107  out.block(0,3,3,1) = trans;
108  out.block(3, 0, 1, 4) = RigidBodyDynamics::Math::Vector4d(0, 0, 0, 1).transpose();
109  return out;
110 }
111 
113  const RigidBodyDynamics::Math::SpatialTransform& st)
114 {
115  return combineRotAndTrans(st.E,st.r);
116 }
117 
119  const biorbd::utils::Vector& rot,
120  const biorbd::utils::Vector3d& trans,
121  const biorbd::utils::String& seq)
122 {
123 
125 
127  out.block(0, 0, 3, 3) = rot_mat;
128  out.block(0, 3, 3, 1) = trans;
129  return out;
130 }
131 
133  const biorbd::utils::RotoTrans& rt,
134  const biorbd::utils::String &seq)
135 {
136  return biorbd::utils::Rotation::toEulerAngles(rt.block<3, 3>(0, 0), seq);
137 }
138 
139 #ifndef BIORBD_USE_CASADI_MATH
140 biorbd::utils::RotoTrans biorbd::utils::RotoTrans::mean(const std::vector<RotoTrans> & mToMean)
141 {
142  // The translation part is just the actual mean
143  RigidBodyDynamics::Math::Vector3d v_tp;
144  v_tp.setZero();
145  for (unsigned int i = 0; i<mToMean.size(); ++i){
146  v_tp += mToMean[i].trans();
147  }
148  v_tp = v_tp/mToMean.size();
149 
150  // The rotation part should call the proper way implemented in Rotation
151  std::vector<biorbd::utils::Rotation> rotations;
152  for (unsigned int i=0; i<mToMean.size(); ++i){
153  rotations.push_back(mToMean[i].block<3, 3>(0, 0));
154  }
156  biorbd::utils::Rotation::mean(rotations), v_tp);
157  return m_out;
158 }
159 #endif
160 
161 RigidBodyDynamics::Math::Vector4d biorbd::utils::RotoTrans::expand3dTo4d(const biorbd::utils::Vector3d &v1)
162 {
163  RigidBodyDynamics::Math::Vector4d v2;
164  v2.block(0,0,3,1) = v1;
165  v2(3) = 1;
166  return v2;
167 }
168 
170 {
171 #ifndef BIORBD_USE_CASADI_MATH
172 #ifndef SKIP_ASSERT
173  this->rot(); // Automatically cast the test for the rotation part
174  biorbd::utils::Error::check(this->block(3, 0, 1, 4).sum() == 1.,
175  "Last row of the RotoTrans should be (0,0,0,1");
176  biorbd::utils::Error::check((*this)(3, 3) == 1.,
177  "Last row of the RotoTrans should be (0,0,0,1");
178 #endif
179 #endif
180 }
181 
182 std::ostream &operator<<(std::ostream &os, const biorbd::utils::RotoTrans &a)
183 {
184  os << a.block(0,0,4,4);
185  return os;
186 }
biorbd::utils::RotoTrans::fromSpatialTransform
static biorbd::utils::RotoTrans fromSpatialTransform(const RigidBodyDynamics::Math::SpatialTransform &st)
set the RotoTrans from a spatial transform
Definition: RotoTrans.cpp:112
biorbd::utils::Vector
Wrapper of the Eigen VectorXd.
Definition: Vector.h:20
biorbd::utils::Vector3d
Wrapper around Eigen Vector3d and attach it to a parent.
Definition: Vector3d.h:24
biorbd::utils::RotoTrans::expand3dTo4d
RigidBodyDynamics::Math::Vector4d expand3dTo4d(const biorbd::utils::Vector3d &v1)
Expand 3D vector to 4D (padding with an extra 1)
Definition: RotoTrans.cpp:161
biorbd::utils::RotoTrans::checkUnitary
void checkUnitary()
Check if the RotoTrans has a unitary matrix of rotation and the last row is (0, 0,...
Definition: RotoTrans.cpp:169
biorbd::utils::RotoTrans::toEulerAngles
static biorbd::utils::Vector toEulerAngles(const biorbd::utils::RotoTrans &rt, const biorbd::utils::String &seq)
Return extracted angles from the rotation matrix into Euler angles using the provided sequence.
Definition: RotoTrans.cpp:132
biorbd::utils::RotoTrans::transpose
biorbd::utils::RotoTrans transpose() const
Return the tranposed matrix.
Definition: RotoTrans.cpp:83
biorbd::utils::Rotation::fromEulerAngles
static biorbd::utils::Rotation fromEulerAngles(const biorbd::utils::Vector &rot, const biorbd::utils::String &seq)
Create a Rotation from Euler angles.
Definition: Rotation.cpp:66
biorbd::utils::RotoTrans::fromMarkers
static biorbd::utils::RotoTrans fromMarkers(const biorbd::rigidbody::NodeSegment &origin, const std::pair< biorbd::rigidbody::NodeSegment, biorbd::rigidbody::NodeSegment > &axis1markers, const std::pair< biorbd::rigidbody::NodeSegment, biorbd::rigidbody::NodeSegment > &axis2markers, const std::pair< biorbd::utils::String, biorbd::utils::String > &axesNames, const biorbd::utils::String &axisToRecalculate)
fromMarkers Creates a system of axes from two axes and an origin defined by markers
Definition: RotoTrans.cpp:63
biorbd::utils::RotoTrans::trans
biorbd::utils::Vector3d trans() const
Return the translation vector.
Definition: RotoTrans.cpp:92
biorbd::utils::RotoTrans
Homogenous matrix to describe translations and rotations simultaneously.
Definition: RotoTrans.h:34
biorbd::utils::Rotation::toEulerAngles
static biorbd::utils::Vector toEulerAngles(const biorbd::utils::Rotation &r, const biorbd::utils::String &seq)
Return extracted angles from the rotation matrix into Euler angles using the provided sequence.
Definition: Rotation.cpp:197
biorbd::utils::RotoTrans::mean
static biorbd::utils::RotoTrans mean(const std::vector< biorbd::utils::RotoTrans > &rt)
Get the mean of the 4x4 matrices.
Definition: RotoTrans.cpp:140
biorbd::utils::Rotation::fromMarkers
static biorbd::utils::Rotation fromMarkers(const std::pair< biorbd::rigidbody::NodeSegment, biorbd::rigidbody::NodeSegment > &axis1markers, const std::pair< biorbd::rigidbody::NodeSegment, biorbd::rigidbody::NodeSegment > &axis2markers, const std::pair< biorbd::utils::String, biorbd::utils::String > &axesNames, const biorbd::utils::String &axisToRecalculate)
fromMarkers Creates a system of axes from two axes defined by markers
Definition: Rotation.cpp:179
biorbd::utils::Rotation
Rotation matrix.
Definition: Rotation.h:34
biorbd::utils::String
Wrapper around the std::string class with augmented functionality.
Definition: String.h:17
biorbd::utils::RotoTrans::RotoTrans
RotoTrans(const RigidBodyDynamics::Math::Matrix4d &matrix=RigidBodyDynamics::Math::Matrix4d::Identity())
Construct RotoTrans matrix.
Definition: RotoTrans.cpp:12
biorbd::utils::Rotation::mean
static biorbd::utils::Rotation mean(const std::vector< biorbd::utils::Rotation > &mToMean)
Get the mean of the Rotation matrices.
Definition: Rotation.cpp:293
biorbd::utils::RotoTrans::fromEulerAngles
static biorbd::utils::RotoTrans fromEulerAngles(const biorbd::utils::Vector &rot, const biorbd::utils::Vector3d &trans, const biorbd::utils::String &seq)
Create a RotoTrans from Euler angles.
Definition: RotoTrans.cpp:118
biorbd::rigidbody::NodeSegment
A point attached to a segment, generally speaking a skin marker.
Definition: NodeSegment.h:19
biorbd::utils::Error::check
static void check(bool cond, const biorbd::utils::String &message)
Assert that raises the error message if false.
Definition: Error.cpp:10
biorbd::utils::RotoTrans::rot
biorbd::utils::Rotation rot() const
Return the rotation matrix.
Definition: RotoTrans.cpp:97
biorbd::utils::RotoTrans::combineRotAndTrans
static biorbd::utils::RotoTrans combineRotAndTrans(const biorbd::utils::Rotation &rot, const biorbd::utils::Vector3d &trans)
Set the RotoTrans from a rotation and a translation.
Definition: RotoTrans.cpp:102
biorbd::utils::RotoTrans::axe
biorbd::utils::Vector3d axe(unsigned int idx) const
Get a particular axis of the rotation matrix.
Definition: RotoTrans.cpp:76