AGX Dynamics  2.35.0.1
agx/Jacobian.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, is 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>
445 
446  template <typename T>
448 
449  template <typename T>
451 
452  template <typename T>
454 
455  template <typename T>
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>
529 
530  template <typename T>
532 
533  template <typename T>
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 */
#define AGX_TYPE_BINDING(_Type, _Name)
Definition: Type.h:179
Jacobian6DOFElementT< T > & normal2()
Definition: agx/Jacobian.h:431
Jacobian6DOFElementT< T > & normal1()
Definition: agx/Jacobian.h:422
Jacobian6DOFElementT< T > & uTangent1()
Definition: agx/Jacobian.h:425
Jacobian6DOFElementT< T > & vTangent1()
Definition: agx/Jacobian.h:428
Jacobian6DOFElementT< T > & vTangent2()
Definition: agx/Jacobian.h:437
Jacobian6DOFElementT< T > & uTangent2()
Definition: agx/Jacobian.h:434
void add(const T &v1, const T &v2)
Add v1 to spatial and v2 to rotational.
Definition: agx/Jacobian.h:256
void set(const T &v1, const T &v2)
Assignment.
Definition: agx/Jacobian.h:245
Real operator*(const Jacobian6DOFElementT &rhs) const
Multiplication operator, multiply this (lhs) with another element (rhs).
Definition: agx/Jacobian.h:197
Jacobian6DOFElementT & operator/=(Real rhs)
Definition: agx/Jacobian.h:222
Jacobian6DOFElementT operator/(Real rhs) const
Definition: agx/Jacobian.h:217
bool operator==(const Jacobian6DOFElementT &other)
Definition: agx/Jacobian.h:262
Jacobian6DOFElementT(const Jacobian6DOFElementT &other)
Copy constructor.
Definition: agx/Jacobian.h:118
Jacobian6DOFElementT & operator=(const Jacobian6DOFElementT &other)
Assignment operator, lhs.spatial = rhs.spatial and lhs.rotational = rhs.rotational.
Definition: agx/Jacobian.h:137
Jacobian6DOFElementT()
Default constructor.
Definition: agx/Jacobian.h:113
const T & spatial() const
Definition: agx/Jacobian.h:173
Jacobian6DOFElementT(const T &spat, const T &rot)
Construct given spatial and rotational value.
Definition: agx/Jacobian.h:128
Real mult(const T &v1, const T &v2) const
Scalar product between this and two other vectors.
Definition: agx/Jacobian.h:235
const T & rotational() const
Definition: agx/Jacobian.h:189
Jacobian6DOFElementT & operator*=(Real rhs)
Definition: agx/Jacobian.h:210
T * ptr()
Access internal buffer.
Definition: Matrix3x3.h:533
Jacobian6DOFElementT< T > & uTangent2()
Definition: agx/Jacobian.h:512
Jacobian6DOFElementT< T > & normal2()
Definition: agx/Jacobian.h:509
Jacobian6DOFElementT< T > & vTangent2()
Definition: agx/Jacobian.h:515
A class holding 3 dimensional vectors and providing basic arithmetic.
Definition: Vec3Template.h:46
A class holding 4 dimensional vectors and providing basic arithmetic.
Definition: Vec4Template.h:35
#define agxAssert(expr)
Definition: debug.h:143
#define AGX_ALIGNED(t, a)
Definition: macros.h:64
#define AGX_FORCE_INLINE
Definition: macros.h:58
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
JacobianMetaT< Vec3f > JacobianMeta32
Definition: agx/Jacobian.h:382
Jacobian6DOFElementT< Vec3 > Jacobian6DOFElement
Definition: agx/Jacobian.h:273
ParticleParticleContactJacobianT< Vec3f > ParticleParticleContactJacobian32
Definition: agx/Jacobian.h:358
ParticleBodyContactJacobianT< Vec3f > ParticleBodyContactJacobian32
Definition: agx/Jacobian.h:362
Jacobian6DOFElementT< Vec3f > Jacobian6DOFElement32
Definition: agx/Jacobian.h:272
BodyBodyContactJacobianT< Vec3 > BodyBodyContactJacobian
Definition: agx/Jacobian.h:355
BodyBodyContactJacobianT< Vec3f > BodyBodyContactJacobian32
Definition: agx/Jacobian.h:354
ParticleBodyContactJacobianT< Vec3d > ParticleBodyContactJacobian64
Definition: agx/Jacobian.h:361
BodyBodyContactJacobianT< Vec3d > BodyBodyContactJacobian64
Definition: agx/Jacobian.h:353
ParticleParticleContactJacobianT< Vec3 > ParticleParticleContactJacobian
Definition: agx/Jacobian.h:359
ParticleParticleContactJacobianT< Vec3d > ParticleParticleContactJacobian64
Definition: agx/Jacobian.h:357
uint32_t UInt
Definition: Integer.h:34
double innerProd_6(const Vec3d &v_01, const Vec3d &v_02, const Vec3d &v_11, const Vec3d &v_12)
Definition: agx/Jacobian.h:41
Real mult(const Jacobian6DOFElementT< T > &G1, const Jacobian6DOFElementT< T > &G2)
Definition: agx/Jacobian.h:386
ParticleBodyContactJacobianT< Vec3 > ParticleBodyContactJacobian
Definition: agx/Jacobian.h:363
JacobianMetaT< Vec3 > JacobianMeta
Definition: agx/Jacobian.h:383
Range< Real > RangeReal
A short hand for ranges of floating point numbers.
Definition: Range.h:191
double Real
Definition: Real.h:42
JacobianMetaT< Vec3d > JacobianMeta64
Definition: agx/Jacobian.h:381
Jacobian6DOFElementT< Vec3d > Jacobian6DOFElement64
Definition: agx/Jacobian.h:271
Vec3T< Real32 > Vec3f
Definition: agx/Vec3.h:39