AGX Dynamics  2.38.0.2
Jacobian.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #ifndef AGX_JACOBIAN_H
18 #define AGX_JACOBIAN_H
19 
20 #include <agx/config/AGX_USE_SSE.h>
21 #include <agx/config/AGX_USE_AVX.h>
22 #include <agx/config.h>
23 #include <agxData/Type.h>
24 #include <agx/Vec3.h>
25 #include <agx/Matrix3x3.h>
26 #include <agx/Range.h>
27 
28 #if AGX_USE_SSE()
29 # include <pmmintrin.h>
30 #endif
31 
32 #ifdef _MSC_VER
33 # pragma warning(push)
34 # pragma warning( disable : 4251 ) // class X needs to have dll-interface to be used by clients of class Y
35 #endif
36 
37 
38 namespace agx
39 {
40 
41  AGX_FORCE_INLINE double innerProd_6( const Vec3d& v_01, const Vec3d& v_02, const Vec3d& v_11, const Vec3d& v_12 )
42  {
43 #if AGX_USE_SSE()
44  const double* v_01Ptr = v_01.ptr();
45  const double* v_11Ptr = v_11.ptr();
46 
47  const double* v_02Ptr = v_02.ptr();
48  const double* v_12Ptr = v_12.ptr();
49 
50  // A0 B0 A0 * B0
51  // A1 B1 A1 * B1 A0 * B0 + A2 * B2
52  // --------- mul => ------- add => ----------------- hadd => A0 * B0 + A2 * B2 + A1 * B1 + A3 * B3
53  // A2 B2 A2 * B2 A1 * B1 + A3 * B3
54  // A3 B3 A3 * B3
55 
56  __m128d tmp1 = _mm_mul_pd( _mm_load_pd( v_01Ptr + 0 ), _mm_load_pd( v_11Ptr + 0 ) );
57  __m128d tmp2 = _mm_mul_pd( _mm_load_pd( v_01Ptr + 2 ), _mm_load_pd( v_11Ptr + 2 ) );
58 
59  __m128d tmp3 = _mm_mul_pd( _mm_load_pd( v_02Ptr + 0 ), _mm_load_pd( v_12Ptr + 0 ) );
60  __m128d tmp4 = _mm_mul_pd( _mm_load_pd( v_02Ptr + 2 ), _mm_load_pd( v_12Ptr + 2 ) );
61 
62  tmp1 = _mm_add_pd( tmp1, tmp2 );
63  tmp3 = _mm_add_pd( tmp3, tmp4 );
64 
65  AGX_ALIGNED( double, 16 ) output[2];
66 
67  tmp1 = _mm_hadd_pd( tmp1, tmp3 );
68  _mm_store_pd( output, tmp1 );
69 
70  return output[0] + output[1];
71 #else
72  return v_01 * v_11 + v_02 * v_12;
73 #endif
74  }
75 
76  AGX_FORCE_INLINE float innerProd_6( const Vec3f& v_01, const Vec3f& v_02, const Vec3f& v_11, const Vec3f& v_12 )
77  {
78 #if AGX_USE_SSE()
79  const float* v_01Ptr = v_01.ptr();
80  const float* v_11Ptr = v_11.ptr();
81 
82  const float* v_02Ptr = v_02.ptr();
83  const float* v_12Ptr = v_12.ptr();
84 
85  AGX_ALIGNED( float, 16 ) output[4];
86 
87  __m128 tmp1 = _mm_mul_ps( _mm_load_ps( v_01Ptr ), _mm_load_ps( v_11Ptr ) );
88  __m128 tmp2 = _mm_mul_ps( _mm_load_ps( v_02Ptr ), _mm_load_ps( v_12Ptr ) );
89 
90  tmp1 = _mm_add_ps( tmp1, tmp2 );
91 
92  _mm_store_ps( output, tmp1 );
93 
94  return output[0]+output[1]+output[2];
95 
96  //return tmp1.m128_f32[ 0 ] + tmp1.m128_f32[ 1 ] + tmp1.m128_f32[ 2 ];
97 #else
98  return v_01 * v_11 + v_02 * v_12;
99 #endif
100  }
101 
102 
103  template <typename T>
105  {
106  public:
107  typedef T Vec3T;
108 
109  public:
114 
119  {
120  *this = other;
121  }
122 
128  explicit AGX_FORCE_INLINE Jacobian6DOFElementT( const T& spat, const T& rot )
129  {
130  m_data[ 0 ].set( spat );
131  m_data[ 1 ].set( rot );
132  }
133 
138  {
139  m_data[ 0 ] = other.m_data[ 0 ];
140  m_data[ 1 ] = other.m_data[ 1 ];
141  return *this;
142  }
143 
148  {
149  agxAssert( i < 2 );
150  return m_data[ i ];
151  }
152 
156  AGX_FORCE_INLINE const T& operator [] ( UInt i ) const
157  {
158  agxAssert( i < 2 );
159  return m_data[ i ];
160  }
161 
166  {
167  return m_data[ 0 ];
168  }
169 
173  AGX_FORCE_INLINE const T& spatial() const
174  {
175  return m_data[ 0 ];
176  }
177 
182  {
183  return m_data[ 1 ];
184  }
185 
189  AGX_FORCE_INLINE const T& rotational() const
190  {
191  return m_data[ 1 ];
192  }
193 
198  {
199  return m_data[ 0 ] * rhs.m_data[ 0 ] + m_data[ 1 ] * rhs.m_data[ 1 ];
200  }
201 
206  {
207  return Jacobian6DOFElementT( m_data[ 0 ] * rhs, m_data[ 1 ] * rhs );
208  }
209 
211  {
212  m_data[ 0 ] *= rhs;
213  m_data[ 1 ] *= rhs;
214  return *this;
215  }
216 
218  {
219  return Jacobian6DOFElementT( m_data[ 0 ] / rhs, m_data[ 1 ] / rhs );
220  }
221 
223  {
224  m_data[ 0 ] /= rhs;
225  m_data[ 1 ] /= rhs;
226  return *this;
227  }
228 
235  AGX_FORCE_INLINE Real mult( const T& v1, const T& v2 ) const
236  {
237  return innerProd_6( m_data[ 0 ], m_data[ 1 ], v1, v2 );
238  }
239 
245  AGX_FORCE_INLINE void set( const T& v1, const T& v2 )
246  {
247  m_data[ 0 ] = v1;
248  m_data[ 1 ] = v2;
249  }
250 
256  AGX_FORCE_INLINE void add( const T& v1, const T& v2 )
257  {
258  m_data[ 0 ] += v1;
259  m_data[ 1 ] += v2;
260  }
261 
263  {
264  return spatial() == other.spatial() && rotational() == other.rotational();
265  }
266 
267  protected:
268  T m_data[2];
269  };
270 
274 
275  // Body-body
276  template <typename T>
278  {
279  public:
283 
287 
288  const Jacobian6DOFElementT<T>& normal1() const;
289  const Jacobian6DOFElementT<T>& uTangent1() const;
290  const Jacobian6DOFElementT<T>& vTangent1() const;
291 
292  const Jacobian6DOFElementT<T>& normal2() const;
293  const Jacobian6DOFElementT<T>& uTangent2() const;
294  const Jacobian6DOFElementT<T>& vTangent2() const;
295 
296  private:
297  Jacobian6DOFElementT<T> m_body1[3];
298  Jacobian6DOFElementT<T> m_body2[3];
299  };
300 
301  // Particle-particle
302  template <typename T>
304  {
305  public:
306  T& normal1();
307  T& uTangent1();
308  T& vTangent1();
309 
310  T& normal2();
311  T& uTangent2();
312  T& vTangent2();
313 
314  const T& normal1() const;
315  const T& uTangent1() const;
316  const T& vTangent1() const;
317 
318  const T& normal2() const;
319  const T& uTangent2() const;
320  const T& vTangent2() const;
321 
322  private:
323  T m_particle1[3];
324  T m_particle2[3];
325  };
326 
327  // Particle-body
328  template <typename T>
330  {
331  public:
332  T& normal1();
333  T& uTangent1();
334  T& vTangent1();
335 
339 
340  const T& normal1() const;
341  const T& uTangent1() const;
342  const T& vTangent1() const;
343 
344  const Jacobian6DOFElementT<T>& normal2() const;
345  const Jacobian6DOFElementT<T>& uTangent2() const;
346  const Jacobian6DOFElementT<T>& vTangent2() const;
347 
348  private:
349  T m_particle[3];
350  Jacobian6DOFElementT<T> m_body[3];
351  };
352 
356 
360 
364 
365 
366  template <typename T>
368  {
371 
375 
376 #if AGX_USE_AVX()
377  Vec3f pad;
378 #endif
379  };
380 
384 
385  template <typename T>
387  {
388  return innerProd_6( G1.spatial(), G1.rotational(), G2.spatial(), G2.rotational() );
389  }
390 
392  {
393 #if AGX_USE_SSE()
394  const float* mPtr = m.ptr();
395  const float* vPtr = v.ptr();
396  __m128 r1 = _mm_load_ps( mPtr + 0 );
397  __m128 r2 = _mm_load_ps( mPtr + 4 );
398  __m128 r3 = _mm_load_ps( mPtr + 8 );
399 
400  __m128 v1 = _mm_load_ps( vPtr );
401 
402  r1 = _mm_mul_ps( r1, v1 );
403  r2 = _mm_mul_ps( r2, v1 );
404  r3 = _mm_mul_ps( r3, v1 );
405 
406  r1 = _mm_hadd_ps( r1, r2 );
407  r3 = _mm_hadd_ps( r3, v1 );
408 
409  r1 = _mm_hadd_ps( r1, r3 );
410 
411  AGX_ALIGNED( Vec4f, 16 ) output;
412  _mm_store_ps( output.ptr(), r1 );
413  return Vec3f( output.ptr() );
414 #else
415  return m * v;
416 #endif
417  }
418 
419 
420  // BodyBodyContactJacobianT
421  template <typename T>
423 
424  template <typename T>
426 
427  template <typename T>
429 
430  template <typename T>
432 
433  template <typename T>
435 
436  template <typename T>
438 
439 
440  template <typename T>
442 
443  template <typename T>
444  AGX_FORCE_INLINE const Jacobian6DOFElementT<T>& BodyBodyContactJacobianT<T>::uTangent1() const { return m_body1[1]; }
445 
446  template <typename T>
447  AGX_FORCE_INLINE const Jacobian6DOFElementT<T>& BodyBodyContactJacobianT<T>::vTangent1() const { return m_body1[2]; }
448 
449  template <typename T>
450  AGX_FORCE_INLINE const Jacobian6DOFElementT<T>& BodyBodyContactJacobianT<T>::normal2() const { return m_body2[0]; }
451 
452  template <typename T>
453  AGX_FORCE_INLINE const Jacobian6DOFElementT<T>& BodyBodyContactJacobianT<T>::uTangent2() const { return m_body2[1]; }
454 
455  template <typename T>
456  AGX_FORCE_INLINE const Jacobian6DOFElementT<T>& BodyBodyContactJacobianT<T>::vTangent2() const { return m_body2[2]; }
457 
458 
459  // ParticleParticleContactJacobianT
460  template <typename T>
462 
463  template <typename T>
465 
466  template <typename T>
468 
469  template <typename T>
471 
472  template <typename T>
474 
475  template <typename T>
477 
478 
479  template <typename T>
480  AGX_FORCE_INLINE const T& ParticleParticleContactJacobianT<T>::normal1() const { return m_particle1[0]; }
481 
482  template <typename T>
483  AGX_FORCE_INLINE const T& ParticleParticleContactJacobianT<T>::uTangent1() const { return m_particle1[1]; }
484 
485  template <typename T>
486  AGX_FORCE_INLINE const T& ParticleParticleContactJacobianT<T>::vTangent1() const { return m_particle1[2]; }
487 
488  template <typename T>
489  AGX_FORCE_INLINE const T& ParticleParticleContactJacobianT<T>::normal2() const { return m_particle2[0]; }
490 
491  template <typename T>
492  AGX_FORCE_INLINE const T& ParticleParticleContactJacobianT<T>::uTangent2() const { return m_particle2[1]; }
493 
494  template <typename T>
495  AGX_FORCE_INLINE const T& ParticleParticleContactJacobianT<T>::vTangent2() const { return m_particle2[2]; }
496 
497 
498  // ParticleBodyContactJacobianT
499  template <typename T>
501 
502  template <typename T>
504 
505  template <typename T>
507 
508  template <typename T>
510 
511  template <typename T>
513 
514  template <typename T>
516 
517 
518  template <typename T>
519  AGX_FORCE_INLINE const T& ParticleBodyContactJacobianT<T>::normal1() const { return m_particle[0]; }
520 
521  template <typename T>
522  AGX_FORCE_INLINE const T& ParticleBodyContactJacobianT<T>::uTangent1() const { return m_particle[1]; }
523 
524  template <typename T>
525  AGX_FORCE_INLINE const T& ParticleBodyContactJacobianT<T>::vTangent1() const { return m_particle[2]; }
526 
527  template <typename T>
528  AGX_FORCE_INLINE const Jacobian6DOFElementT<T>& ParticleBodyContactJacobianT<T>::normal2() const { return m_body[0]; }
529 
530  template <typename T>
531  AGX_FORCE_INLINE const Jacobian6DOFElementT<T>& ParticleBodyContactJacobianT<T>::uTangent2() const { return m_body[1]; }
532 
533  template <typename T>
534  AGX_FORCE_INLINE const Jacobian6DOFElementT<T>& ParticleBodyContactJacobianT<T>::vTangent2() const { return m_body[2]; }
535 
536 }
537 
538 AGX_TYPE_BINDING(agx::Jacobian6DOFElement32, "Jacobian6DOFElement")
547 
550 
551 #ifdef _MSC_VER
552 # pragma warning(pop)
553 #endif
554 
555 
556 #endif /* AGX_JACOBIAN_H */
agx::BodyBodyContactJacobian64
BodyBodyContactJacobianT< Vec3d > BodyBodyContactJacobian64
Definition: Jacobian.h:353
agx::UInt
uint64_t UInt
Definition: Integer.h:27
agx::Vec3T::ptr
T * ptr()
Definition: Vec3Template.h:447
Matrix3x3.h
agx::Real
double Real
Definition: Real.h:42
agx::Jacobian6DOFElementT::Jacobian6DOFElementT
Jacobian6DOFElementT(const T &spat, const T &rot)
Construct given spatial and rotational value.
Definition: Jacobian.h:128
agx::Jacobian6DOFElementT
Definition: Jacobian.h:104
agx::Jacobian6DOFElementT::operator*=
Jacobian6DOFElementT & operator*=(Real rhs)
Definition: Jacobian.h:210
agx::ParticleParticleContactJacobianT::uTangent2
T & uTangent2()
Definition: Jacobian.h:473
agx::BodyBodyContactJacobianT
Definition: Jacobian.h:277
agx::Jacobian6DOFElementT::spatial
const T & spatial() const
Definition: Jacobian.h:173
agx::ParticleParticleContactJacobianT::normal2
T & normal2()
Definition: Jacobian.h:470
agxAssert
#define agxAssert(expr)
Definition: debug.h:143
agx::ParticleBodyContactJacobian
ParticleBodyContactJacobianT< Vec3 > ParticleBodyContactJacobian
Definition: Jacobian.h:363
agx::Jacobian6DOFElementT::m_data
T m_data[2]
Definition: Jacobian.h:268
agx::ParticleBodyContactJacobianT::normal2
Jacobian6DOFElementT< T > & normal2()
Definition: Jacobian.h:509
agx::Jacobian6DOFElementT::operator*
Real operator*(const Jacobian6DOFElementT &rhs) const
Multiplication operator, multiply this (lhs) with another element (rhs).
Definition: Jacobian.h:197
agx::Vec4T
A class holding 4 dimensional vectors and providing basic arithmetic.
Definition: Vec4Template.h:34
agx::Jacobian6DOFElement64
Jacobian6DOFElementT< Vec3d > Jacobian6DOFElement64
Definition: Jacobian.h:271
agx::Jacobian6DOFElementT::spatial
T & spatial()
Definition: Jacobian.h:165
agx::JacobianMeta64
JacobianMetaT< Vec3d > JacobianMeta64
Definition: Jacobian.h:381
agx::JacobianMetaT::localPoint2
T localPoint2
Definition: Jacobian.h:370
agx::ParticleParticleContactJacobianT
Definition: Jacobian.h:303
agx
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
Definition: AddedMassInteraction.h:22
agx::Jacobian6DOFElement
Jacobian6DOFElementT< Vec3 > Jacobian6DOFElement
Definition: Jacobian.h:273
agx::ParticleBodyContactJacobian64
ParticleBodyContactJacobianT< Vec3d > ParticleBodyContactJacobian64
Definition: Jacobian.h:361
agx::ParticleParticleContactJacobian64
ParticleParticleContactJacobianT< Vec3d > ParticleParticleContactJacobian64
Definition: Jacobian.h:357
agx::JacobianMetaT
Definition: Jacobian.h:367
agx::JacobianMeta
JacobianMetaT< Vec3 > JacobianMeta
Definition: Jacobian.h:383
agx::Jacobian6DOFElementT::operator/
Jacobian6DOFElementT operator/(Real rhs) const
Definition: Jacobian.h:217
agx::BodyBodyContactJacobianT::vTangent1
Jacobian6DOFElementT< T > & vTangent1()
Definition: Jacobian.h:428
agx::BodyBodyContactJacobianT::uTangent2
Jacobian6DOFElementT< T > & uTangent2()
Definition: Jacobian.h:434
agx::Jacobian6DOFElement32
Jacobian6DOFElementT< Vec3f > Jacobian6DOFElement32
Definition: Jacobian.h:272
agx::Jacobian6DOFElementT::set
void set(const T &v1, const T &v2)
Assignment.
Definition: Jacobian.h:245
Vec3.h
Type.h
agx::JacobianMetaT::normal
Vec3f normal
Definition: Jacobian.h:372
agx::ParticleParticleContactJacobianT::uTangent1
T & uTangent1()
Definition: Jacobian.h:464
agx::innerProd_6
double innerProd_6(const Vec3d &v_01, const Vec3d &v_02, const Vec3d &v_11, const Vec3d &v_12)
Definition: Jacobian.h:41
agx::Jacobian6DOFElementT::Vec3T
T Vec3T
Definition: Jacobian.h:107
agx::Jacobian6DOFElementT::Jacobian6DOFElementT
Jacobian6DOFElementT()
Default constructor.
Definition: Jacobian.h:113
agx::BodyBodyContactJacobianT::uTangent1
Jacobian6DOFElementT< T > & uTangent1()
Definition: Jacobian.h:425
agx::ParticleParticleContactJacobian
ParticleParticleContactJacobianT< Vec3 > ParticleParticleContactJacobian
Definition: Jacobian.h:359
Range.h
agx::BodyBodyContactJacobianT::vTangent2
Jacobian6DOFElementT< T > & vTangent2()
Definition: Jacobian.h:437
agx::Jacobian6DOFElementT::operator==
bool operator==(const Jacobian6DOFElementT &other)
Definition: Jacobian.h:262
agx::Jacobian6DOFElementT::add
void add(const T &v1, const T &v2)
Add v1 to spatial and v2 to rotational.
Definition: Jacobian.h:256
agx::ParticleBodyContactJacobianT::uTangent2
Jacobian6DOFElementT< T > & uTangent2()
Definition: Jacobian.h:512
agx::Jacobian6DOFElementT::rotational
const T & rotational() const
Definition: Jacobian.h:189
agx::Jacobian6DOFElementT::operator=
Jacobian6DOFElementT & operator=(const Jacobian6DOFElementT &other)
Assignment operator, lhs.spatial = rhs.spatial and lhs.rotational = rhs.rotational.
Definition: Jacobian.h:137
agx::Jacobian6DOFElementT::operator/=
Jacobian6DOFElementT & operator/=(Real rhs)
Definition: Jacobian.h:222
agx::Matrix3x3T::ptr
T * ptr()
Access internal buffer.
Definition: Matrix3x3.h:526
agx::ParticleParticleContactJacobianT::vTangent2
T & vTangent2()
Definition: Jacobian.h:476
agx::JacobianMetaT::localPoint1
T localPoint1
Definition: Jacobian.h:369
agx::JacobianMetaT::vTangent
Vec3f vTangent
Definition: Jacobian.h:374
agx::BodyBodyContactJacobian32
BodyBodyContactJacobianT< Vec3f > BodyBodyContactJacobian32
Definition: Jacobian.h:354
agx::RangeReal
Range< Real > RangeReal
A short hand for ranges of floating point numbers.
Definition: Range.h:196
agx::Jacobian6DOFElementT::mult
Real mult(const T &v1, const T &v2) const
Scalar product between this and two other vectors.
Definition: Jacobian.h:235
agx::Jacobian6DOFElementT::operator[]
T & operator[](UInt i)
Definition: Jacobian.h:147
agx::mult
Real mult(const Jacobian6DOFElementT< T > &G1, const Jacobian6DOFElementT< T > &G2)
Definition: Jacobian.h:386
AGX_TYPE_BINDING
#define AGX_TYPE_BINDING(_Type, _Name)
Definition: Type.h:179
agx::JacobianMeta32
JacobianMetaT< Vec3f > JacobianMeta32
Definition: Jacobian.h:382
agx::ParticleBodyContactJacobianT::normal1
T & normal1()
Definition: Jacobian.h:500
agx::ParticleParticleContactJacobianT::vTangent1
T & vTangent1()
Definition: Jacobian.h:467
agx::BodyBodyContactJacobian
BodyBodyContactJacobianT< Vec3 > BodyBodyContactJacobian
Definition: Jacobian.h:355
agx::Vec3T
A class holding 3 dimensional vectors and providing basic arithmetic.
Definition: Vec3Template.h:45
agx::ParticleBodyContactJacobianT::uTangent1
T & uTangent1()
Definition: Jacobian.h:503
AGX_ALIGNED
#define AGX_ALIGNED(t, a)
Definition: macros.h:64
agx::Jacobian6DOFElementT::rotational
T & rotational()
Definition: Jacobian.h:181
agx::Matrix3x3T
Definition: EulerAngles.h:31
agx::Vec3f
Vec3T< Real32 > Vec3f
Definition: Vec3.h:39
agx::ParticleParticleContactJacobian32
ParticleParticleContactJacobianT< Vec3f > ParticleParticleContactJacobian32
Definition: Jacobian.h:358
agx::ParticleBodyContactJacobianT::vTangent1
T & vTangent1()
Definition: Jacobian.h:506
agx::Jacobian6DOFElementT::Jacobian6DOFElementT
Jacobian6DOFElementT(const Jacobian6DOFElementT &other)
Copy constructor.
Definition: Jacobian.h:118
agx::ParticleBodyContactJacobianT::vTangent2
Jacobian6DOFElementT< T > & vTangent2()
Definition: Jacobian.h:515
AGX_FORCE_INLINE
#define AGX_FORCE_INLINE
Definition: macros.h:58
agx::ParticleBodyContactJacobian32
ParticleBodyContactJacobianT< Vec3f > ParticleBodyContactJacobian32
Definition: Jacobian.h:362
agx::ParticleBodyContactJacobianT
Definition: Jacobian.h:329
agx::BodyBodyContactJacobianT::normal1
Jacobian6DOFElementT< T > & normal1()
Definition: Jacobian.h:422
agx::ParticleParticleContactJacobianT::normal1
T & normal1()
Definition: Jacobian.h:461
agx::JacobianMetaT::uTangent
Vec3f uTangent
Definition: Jacobian.h:373
agx::BodyBodyContactJacobianT::normal2
Jacobian6DOFElementT< T > & normal2()
Definition: Jacobian.h:431