-
Notifications
You must be signed in to change notification settings - Fork 298
/
SolidTypes.h
422 lines (358 loc) · 16.3 KB
/
SolidTypes.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#ifndef SOFA_DEFAULTTYPE_SOLIDTYPES_H
#define SOFA_DEFAULTTYPE_SOLIDTYPES_H
#include <sofa/defaulttype/config.h>
#include <sofa/type/Vec.h>
#include <sofa/type/Quat.h>
#include <sofa/type/Mat.h>
#include <sofa/type/fixed_array.h>
#include <sofa/type/vector.h>
#include <iostream>
#include <map>
namespace sofa::defaulttype
{
/**
Base types for the ArticulatedSolid: position, orientation, velocity, angular velocity, etc.
@author François Faure, INRIA-UJF, 2006
*/
class SOFA_DEFAULTTYPE_API Transform;
template< class R=float >
class SOFA_DEFAULTTYPE_API SolidTypes
{
public:
typedef R Real;
typedef type::Vec<3,Real> Vec3;
typedef Vec3 Vec; ///< For compatibility
typedef type::Quat<Real> Rot;
typedef type::Mat<3,3,Real> Mat3x3;
typedef Mat3x3 Mat; ///< For compatibility
typedef type::Mat<6,6,Real> Mat6x6;
typedef Mat6x6 Mat66; ///< For compatibility
typedef type::Vec<6,Real> Vec6;
typedef Vec6 DOF; ///< For compatibility
/** A spatial vector.
When representing a velocity, lineVec is the angular velocity and freeVec is the linear velocity.
When representing a spatial force, lineVec is the force and freeVec is the torque. */
class SOFA_DEFAULTTYPE_API SpatialVector
{
public:
Vec lineVec{ type::NOINIT };
Vec freeVec{ type::NOINIT };
void clear();
SpatialVector() = default;
/**
\param l The line vector: angular velocity, or force
\param f The free vector: linear velocity, or torque
*/
SpatialVector( const Vec& l, const Vec& f );
SpatialVector& operator += (const SpatialVector& v);
//template<class Real2>
SpatialVector operator * ( Real a ) const
{
return SpatialVector( lineVec *a, freeVec * a);
}
SpatialVector& operator *= ( Real a )
{
lineVec *=a;
freeVec *= a;
return *this;
}
SpatialVector operator + ( const SpatialVector& v ) const;
SpatialVector operator - ( const SpatialVector& v ) const;
SpatialVector operator - ( ) const;
/// Spatial dot product (cross terms)
Real operator * ( const SpatialVector& v ) const;
/// Spatial cross product
SpatialVector cross( const SpatialVector& v ) const;
/// product with a dense matrix
SpatialVector operator * (const Mat66&) const;
/// write to an output stream
inline friend std::ostream& operator << (std::ostream& out, const SpatialVector& t )
{
out << t.lineVec << " " << t.freeVec;
return out;
}
/// read from an input stream
inline friend std::istream& operator >> ( std::istream& in, SpatialVector& t )
{
in >> t.lineVec >> t.freeVec;
return in;
}
/// If the SpatialVector models a spatial velocity, then the linear velocity is the freeVec.
/// Otherwise, the SpatialVector models a spatial force, and this method returns a torque.
Vec& getLinearVelocity()
{
return freeVec;
}
const Vec& getLinearVelocity() const
{
return freeVec;
}
void setLinearVelocity(const Vec& v)
{
freeVec = v;
}
/// If the SpatialVector models a spatial velocity, then the angular velocity is the lineVec.
/// Otherwise, the SpatialVector models a spatial force, and this method returns a force.
Vec& getAngularVelocity()
{
return lineVec;
}
const Vec& getAngularVelocity() const
{
return lineVec;
}
void setAngularVelocity(const Vec& v)
{
lineVec = v;
}
/// If the SpatialVector models a spatial force, then the torque is the freeVec.
/// Otherwise, the SpatialVector models a spatial velocity, and this method returns a linear velocity.
Vec& getTorque()
{
return freeVec;
}
const Vec& getTorque() const
{
return freeVec;
}
void setTorque(const Vec& v)
{
freeVec = v;
}
/// If the SpatialVector models a spatial force, then the torque is the lineVec.
/// Otherwise, the SpatialVector models a spatial velocity, and this method returns an angular velocity.
Vec& getForce()
{
return lineVec;
}
const Vec& getForce() const
{
return lineVec;
}
void setForce(const Vec& v)
{
lineVec = v;
}
};
/**
* \brief A twist aka a SpatialVector representing a velocity
* This is pratically a SpatialVector (screw) with the additionnal semantics
* that this screw represents a twist (velocity) and not a wrench (force and torque)
* @author Anthony Truchet, CEA, 2006
*/
class Twist : public SpatialVector
{
public:
Twist(const Vec3& linear, const Vec3& angular)
: SpatialVector(angular, linear) {}
};
/**
* \brief A wrench aka a SpatialVector representing a force and a torque
* This is pratically a SpatialVector (screw) with the additionnal semantics
* that this screw represents a wrench (force and torque) and not a twist (velocity)
* @author Anthony Truchet, CEA, 2006
*/
class Wrench : public SpatialVector
{
public:
Wrench(const Vec3& force, const Vec3& torque)
: SpatialVector(force, torque) {}
};
/** Define a frame (child) whith respect to another (parent). A frame represents a local coordinate system.
Internal data represents the orientation of the child wrt the parent, BUT the translation vector represents the origin of the parent with respect to the child. For example, the coordinates M_p of point M in parent given the coordinates M_c of the same point in child are given by: M_p = orientation * ( M_c - origin ). This is due to Featherstone's conventions. Use method setTranslationRotation( const Vec& t, const Rot& q ) to model the Transform the standard way (i.e. translation givne in the parent frame).
*/
class SOFA_DEFAULTTYPE_API Transform
{
public:
/// The default constructor does not initialize the transform
Transform();
/// Origin of the child in parent coordinates, orientation of the child wrt to parent
Transform( const Vec& origin, const Rot& orientation );
/// WARNING: using Featherstone's conventions (see class documentation)
Transform( const Rot& q, const Vec& o );
/// Origin of the child in the parent coordinate system and the orientation of the child wrt the parent (i.e. standard way)
void set( const Vec& t, const Rot& q );
/// Reset this to identity
void clear();
/// The identity transform (child = parent)
static Transform identity();
/// Origin of the child in the parent coordinate system and the orientation of the child wrt the parent (i.e. standard way)
//static Transform inParent(const Vec& t, const Rot& r);
/// Define child as a given SpatialVector integrated during one second, starting from the parent (used for time integration). The spatial vector is given in parent coordinates.
Transform( const SpatialVector& v );
/// The inverse transform i.e. parent wrt child
Transform inversed() const;
/// Parent origin in child coordinates (the way it is actually stored internally)
const Vec& getOriginOfParentInChild() const;
/// Origin of child in parent coordinates
Vec getOrigin() const;
/// Origin of child in parent coordinates
void setOrigin( const Vec& );
/// Orientation of the child coordinate axes wrt the parent coordinate axes
const Rot& getOrientation() const;
/// Orientation of the child coordinate axes wrt the parent coordinate axes
void setOrientation( const Rot& );
/// Matrix which projects vectors from child coordinates to parent coordinates. The columns of the matrix are the axes of the child base axes in the parent coordinate system.
Mat3x3 getRotationMatrix() const;
/**
* \brief Adjoint matrix to the transform
* This matrix transports velocities in twist coordinates from the child frame to the parent frame.
* Its inverse transpose does the same for the wrenches
*/
Mat6x6 getAdjointMatrix() const;
/// Project a vector (i.e. a direction or a displacement) from child coordinates to parent coordinates
Vec projectVector( const Vec& vectorInChild ) const;
/// Project a point from child coordinates to parent coordinates
Vec projectPoint( const Vec& pointInChild ) const;
/// Projected a vector (i.e. a direction or a displacement) from parent coordinates to child coordinates
Vec backProjectVector( const Vec& vectorInParent ) const;
/// Project point from parent coordinates to this coordinates
Vec backProjectPoint( const Vec& pointInParent ) const;
/// Combine two transforms. If (*this) locates frame B (child) wrt frame A (parent) and if f2 locates frame C (child) wrt frame B (parent) then the result locates frame C wrt to Frame A.
Transform operator * (const Transform& f2) const;
/// Combine two transforms. If (*this) locates frame B (child) wrt frame A (parent) and if f2 locates frame C (child) wrt frame B (parent) then the result locates frame C wrt to Frame A.
Transform& operator *= (const Transform& f2);
/** Project a spatial vector from child to parent
* TODO One should handle differently the transformation of a twist and a wrench !
* This applying the adjoint to velocities or its transpose to wrench :
* V_parent = Ad . V_child or W_child = Ad^T . W_parent
* To project a wrench in the child frame to the parent frame you need to do
* parent_wrench = this->inversed * child_wrench
* (this doc needs to be douv-ble checked !)
*/
// create a spatial Vector from a small transformation
SpatialVector CreateSpatialVector();
SpatialVector DTrans();
SpatialVector operator * (const SpatialVector& sv ) const;
/// Project a spatial vector from parent to child (the inverse of operator *). This method computes (*this).inversed()*sv without inverting (*this).
SpatialVector operator / (const SpatialVector& sv ) const;
/// Write an OpenGL matrix encoding the transformation of the coordinate system of the child wrt the coordinate system of the parent.
void writeOpenGlMatrix( double *m ) const;
/// Draw the axes of the child coordinate system in the parent coordinate system
/// Print the origin of the child in the parent coordinate system and the quaternion defining the orientation of the child wrt the parent
inline friend std::ostream& operator << (std::ostream& out, const Transform& t )
{
out << t.getOrigin() << " " << t.getOrientation();
return out;
}
/// read from an input stream
inline friend std::istream& operator >> ( std::istream& in, Transform& t )
{
Vec origin;
Rot orientation;
in >> origin >> orientation;
t.set(origin, orientation);
return in;
}
/// Print the internal values (i.e. using Featherstone's conventions, see class documentation)
void printInternal( std::ostream&) const;
/** @name Time integration
* Methods used in time integration
*/
///@{
/// (*this) *= Transform(v) Used for time integration. SHOULD WE RATHER APPLY (*this)=Transform(v)*(*this) ???
Transform& operator +=(const SpatialVector& a);
Transform& operator +=(const Transform& a);
template<class Real2>
Transform& operator*=(Real2 a)
{
origin_ *= a;
return *this;
}
template<class Real2>
Transform operator*(Real2 a) const
{
Transform r = *this;
r*=a;
return r;
}
///@}
protected:
Rot orientation_; ///< child wrt parent
Vec origin_; ///< parent wrt child
};
class SOFA_DEFAULTTYPE_API RigidInertia
{
public:
Real m; ///< mass
Vec h; ///< position of the mass center in the local reference frame
Mat I; /// Inertia matrix around the mass center
RigidInertia();
RigidInertia( Real m, const Vec& h, const Mat& I );
SpatialVector operator * (const SpatialVector& v ) const;
RigidInertia operator * ( const Transform& t ) const;
inline friend std::ostream& operator << (std::ostream& out, const RigidInertia& r )
{
out<<"I= "<<r.I<<std::endl;
out<<"h= "<<r.h<<std::endl;
out<<"m= "<<r.m<<std::endl;
return out;
}
};
class SOFA_DEFAULTTYPE_API ArticulatedInertia
{
public:
Mat M;
Mat H;
Mat I;
ArticulatedInertia();
ArticulatedInertia( const Mat& M, const Mat& H, const Mat& I );
SpatialVector operator * (const SpatialVector& v ) const;
ArticulatedInertia operator * ( Real r ) const;
ArticulatedInertia& operator = (const RigidInertia& Ri );
ArticulatedInertia& operator += (const ArticulatedInertia& Ai );
ArticulatedInertia operator + (const ArticulatedInertia& Ai ) const;
ArticulatedInertia operator - (const ArticulatedInertia& Ai ) const;
inline friend std::ostream& operator << (std::ostream& out, const ArticulatedInertia& r )
{
out<<"I= "<<r.I<<std::endl;
out<<"H= "<<r.H<<std::endl;
out<<"M= "<<r.M<<std::endl;
return out;
}
/// Convert to a full 6x6 matrix
void copyTo( Mat66& ) const;
};
typedef Transform Coord;
typedef SpatialVector Deriv;
typedef Coord VecCoord;
typedef Deriv VecDeriv;
typedef Real VecReal;
static Mat dyad( const Vec& u, const Vec& v );
static Vec mult( const Mat& m, const Vec& v );
static Vec multTrans( const Mat& m, const Vec& v );
/// Cross product matrix of a vector
static Mat crossM( const Vec& v );
static ArticulatedInertia dyad ( const SpatialVector& u, const SpatialVector& v );
static constexpr const char* Name()
{
return "Solid";
}
};
#if !defined(SOFA_DEFAULTTYPE_SOLIDTYPES_CPP)
extern template class SOFA_DEFAULTTYPE_API SolidTypes<double>;
extern template class SOFA_DEFAULTTYPE_API SolidTypes<float>;
#endif
}
#endif