AGX Dynamics  2.37.2.0
OrthoMatrix3x3.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 #pragma once
17 
18 #include <agx/agxCore_export.h>
19 
20 #include <agx/Vec3.h>
21 #include <agx/Quat.h>
22 
23 #include <cstring>
24 
25 namespace agx
26 {
27 
28  class EulerAngles;
29 
34  {
35  public:
39  inline OrthoMatrix3x3() {
40  setIdentity();
41  }
42 
46  inline OrthoMatrix3x3( const OrthoMatrix3x3& mat ) {
47  set( mat.ptr() );
48  }
49  inline explicit OrthoMatrix3x3( Real const * const ptr ) {
50  set( ptr );
51  }
52  inline explicit OrthoMatrix3x3( const Quat& quat ) {
53  set( quat );
54  }
55 
56  inline explicit OrthoMatrix3x3( EulerAngles& euler ) {
57  set( euler );
58  }
59 
60 
66  inline void setRow(size_t row, const agx::Vec3& vec ) {
67  m_data[row][0] = vec[0];
68  m_data[row][1] = vec[1];
69  m_data[row][2] = vec[2];
70  }
71 
76  inline Vec3 getRow(size_t row ) const {
77  return Vec3(m_data[row][0], m_data[row][1], m_data[row][2]);
78  }
79 
80 
86  inline void setColumn(size_t col, const agx::Vec3& vec ) {
87  m_data[0][col] = vec[0];
88  m_data[1][col] = vec[1];
89  m_data[2][col] = vec[2];
90  }
91 
92 
93  OrthoMatrix3x3( Real a00, Real a01, Real a02,
94  Real a10, Real a11, Real a12,
95  Real a20, Real a21, Real a22);
96 
98 
99 
100  bool operator== ( const OrthoMatrix3x3& m ) const {
101  const int bitCompare = memcmp(m_data, m.m_data, sizeof(m_data));
102  return (bitCompare == 0);
103  }
104 
105  bool operator!= ( const OrthoMatrix3x3& m ) const {
106  const int bitCompare = memcmp(m_data, m.m_data, sizeof(m_data));
107  return (bitCompare != 0);
108  }
109 
110  inline Real& operator()( size_t row, size_t col ) {
111  return m_data[row][col];
112  }
113  inline Real operator()( size_t row, size_t col ) const {
114  return m_data[row][col];
115  }
116 
117  inline bool isValid() const {
118  return !isNaN();
119  }
120  inline bool isNaN() const {
121  return std::isnan( m_data[0][0] ) || std::isnan( m_data[0][1] ) || std::isnan( m_data[0][2] ) ||
122  std::isnan( m_data[1][0] ) || std::isnan( m_data[1][1] ) || std::isnan( m_data[1][2] ) ||
123  std::isnan( m_data[2][0] ) || std::isnan( m_data[2][1] ) || std::isnan( m_data[2][2] ) ;
124  }
125 
126  bool isIdentity() const;
127 
128  inline OrthoMatrix3x3 inverse() const;
129  inline OrthoMatrix3x3 transpose() const;
130 
131 
132  inline OrthoMatrix3x3& operator= ( const OrthoMatrix3x3& rhs ) {
133  if ( &rhs == this ) return *this;
134  set( rhs.ptr() );
135  return *this;
136  }
137 
138  inline void set( const OrthoMatrix3x3& rhs ) {
139  set( rhs.ptr() );
140  }
141 
142  inline void set( Real const * const ptr ) {
143  Real* local_ptr = ( Real* )m_data;
144  for ( size_t i = 0;i < 9;++i ) local_ptr[i] = ( Real )ptr[i];
145  }
146 
147 
148  void set( Real a00, Real a01, Real a02,
149  Real a10, Real a11, Real a12,
150  Real a20, Real a21, Real a22);
151 
156  OrthoMatrix3x3& set( const Quat& q_ );
157 
161  void get( Quat& q ) const;
162  inline Quat get( ) const;
163 
168  OrthoMatrix3x3& set( EulerAngles& e );
169 
173  void get( EulerAngles& e ) const;
174 
175  Real * ptr() {
176  return ( Real* )m_data;
177  }
178  const Real * ptr() const {
179  return ( const Real * )m_data;
180  }
181 
182  void setIdentity();
183 
184  void setRotate( const Vec3& from, const Vec3& to );
185  void setRotate( Real angle, const Vec3& axis );
186  void setRotate( Real angle, Real x, Real y, Real z );
187  void setRotate( const Quat& );
188  void setRotate( Real angle1, const Vec3& axis1,
189  Real angle2, const Vec3& axis2,
190  Real angle3, const Vec3& axis3 );
191 
192 
193  //basic utility functions to create new matrices
194  inline static OrthoMatrix3x3 rotate( const Vec3& from, const Vec3& to );
195  inline static OrthoMatrix3x3 rotate( Real angle, Real x, Real y, Real z );
196  inline static OrthoMatrix3x3 rotate( Real angle, const Vec3& axis );
197  inline static OrthoMatrix3x3 rotate( Real angle1, const Vec3& axis1,
198  Real angle2, const Vec3& axis2,
199  Real angle3, const Vec3& axis3 );
200 
201 
202  inline Vec3 preMult( const Vec3& v ) const;
203  inline Vec3 postMult( const Vec3& v ) const;
204  inline Vec3 operator* ( const Vec3& v ) const;
205 
206  inline Vec3 getScale() const {
207  return Vec3( m_data[0][0], m_data[1][1], m_data[2][2] );
208  }
209 
210 
211  // basic OrthoMatrix3x3 multiplication, our workhorse methods.
212  void mult( const OrthoMatrix3x3&, const OrthoMatrix3x3& );
213  void preMult( const OrthoMatrix3x3& );
214  void postMult( const OrthoMatrix3x3& );
215 
216  inline void operator*= ( const OrthoMatrix3x3& other ) {
217  if ( this == &other ) {
218  OrthoMatrix3x3 temp( other );
219  postMult( temp );
220  }
221  else postMult( other );
222  }
223 
224  inline OrthoMatrix3x3 operator* ( const OrthoMatrix3x3 &m ) const {
225  OrthoMatrix3x3 r;
226  r.mult( *this, m );
227  return r;
228  }
229 
230  protected:
231 
232  bool invert( const OrthoMatrix3x3& rhs );
233  bool invert_3x3_new(const agx::OrthoMatrix3x3&);
234 
235  Real m_data[3][3];
236  };
237 
238 
239  inline Quat OrthoMatrix3x3::get( ) const
240  {
241  Quat q;
242  get( q );
243  return q;
244  }
245 
247  {
248  OrthoMatrix3x3 m;
249  m.setRotate( angle, x, y, z );
250  return m;
251  }
252  inline OrthoMatrix3x3 OrthoMatrix3x3::rotate( Real angle, const Vec3& axis )
253  {
254  OrthoMatrix3x3 m;
255  m.setRotate( angle, axis );
256  return m;
257  }
258  inline OrthoMatrix3x3 OrthoMatrix3x3::rotate( Real angle1, const Vec3& axis1,
259  Real angle2, const Vec3& axis2,
260  Real angle3, const Vec3& axis3 )
261  {
262  OrthoMatrix3x3 m;
263  m.setRotate( angle1, axis1, angle2, axis2, angle3, axis3 );
264  return m;
265  }
266  inline OrthoMatrix3x3 OrthoMatrix3x3::rotate( const Vec3& from, const Vec3& to )
267  {
268  OrthoMatrix3x3 m;
269  m.setRotate( from, to );
270  return m;
271  }
272 
274  {
275  OrthoMatrix3x3 m;
276  m.invert( *this );
277  return m;
278  }
279 
281  {
282  return OrthoMatrix3x3( m_data[0][0], m_data[1][0], m_data[2][0],
283  m_data[0][1], m_data[1][1], m_data[2][1],
284  m_data[0][2], m_data[1][2], m_data[2][2]);
285  }
286 
287  inline Vec3 OrthoMatrix3x3::postMult( const Vec3& v ) const
288  {
289  return Vec3( ( m_data[0][0]*v.x() + m_data[0][1]*v.y() + m_data[0][2]*v.z() ),
290  ( m_data[1][0]*v.x() + m_data[1][1]*v.y() + m_data[1][2]*v.z() ),
291  ( m_data[2][0]*v.x() + m_data[2][1]*v.y() + m_data[2][2]*v.z() ) );
292  }
293  inline Vec3 OrthoMatrix3x3::preMult( const Vec3& v ) const
294  {
295  return Vec3( ( m_data[0][0]*v.x() + m_data[1][0]*v.y() + m_data[2][0]*v.z() ),
296  ( m_data[0][1]*v.x() + m_data[1][1]*v.y() + m_data[2][1]*v.z() ),
297  ( m_data[0][2]*v.x() + m_data[1][2]*v.y() + m_data[2][2]*v.z() ) );
298  }
299 
300  inline Vec3 operator* ( const Vec3& v, const OrthoMatrix3x3& m )
301  {
302  return m.preMult( v );
303  }
304  inline Vec3 OrthoMatrix3x3::operator* ( const Vec3& v ) const
305  {
306  return postMult( v );
307  }
308 
309  inline std::ostream& operator<< ( std::ostream& os, const OrthoMatrix3x3& m )
310  {
311  os << "{" << std::endl;
312  for ( size_t row = 0; row < 3; ++row ) {
313  os << "\t";
314  for ( size_t col = 0; col < 3; ++col )
315  os << m( row, col ) << " ";
316  os << std::endl;
317  }
318  os << "}" << std::endl;
319  return os;
320  }
321 
322 
323 } // namespace agx
324 
325 
Real mult(const Jacobian6DOFElementT< T > &G1, const Jacobian6DOFElementT< T > &G2)
Definition: Jacobian.h:386
bool isValid() const
Vec3T< Real > Vec3
The object holding 3 dimensional vectors and providing basic arithmetic.
Definition: Vec3.h:36
OrthoMatrix3x3 transpose() const
void setRow(size_t row, const agx::Vec3 &vec)
Set the row of the matrix,.
OrthoMatrix3x3()
Default constructor.
bool invert(const OrthoMatrix3x3 &rhs)
OrthoMatrix3x3(const OrthoMatrix3x3 &mat)
Copy constructor.
bool operator==(T val, InvalidIndexStruct)
Definition: agx.h:194
OrthoMatrix3x3 inverse() const
static OrthoMatrix3x3 rotate(const Vec3 &from, const Vec3 &to)
T inverse(const T &value)
Definition: Math.h:78
const Real * ptr() const
bool isNaN(T v)
Definition: Math.h:303
void mult(const OrthoMatrix3x3 &, const OrthoMatrix3x3 &)
This class provides conversion services between Euler angles in any of the 24 conventions and corresp...
Definition: EulerAngles.h:69
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
OrthoMatrix3x3(EulerAngles &euler)
Vec3 getScale() const
std::ostream & operator<<(std::ostream &os, const agx::AddedMassInteraction::Matrix6x6 &m)
void setRotate(const Vec3 &from, const Vec3 &to)
OrthoMatrix3x3(Real const *const ptr)
Real & operator()(size_t row, size_t col)
Real operator()(size_t row, size_t col) const
bool operator!=(T val, InvalidIndexStruct)
Definition: agx.h:200
Vec3 getRow(size_t row) const
Return the row of the matrix,.
Vec3 postMult(const Vec3 &v) const
double Real
Definition: Real.h:42
OrthoMatrix3x3(const Quat &quat)
Vec3 operator*(const Vec3 &v) const
#define AGXCORE_EXPORT
void setColumn(size_t col, const agx::Vec3 &vec)
Set the column of the matrix,.
Vec3T< T > operator*(const Vec3T< T > &v, const AffineMatrix4x4T< T > &m)
Specialized types of matrices for holding orthogonal transformation matrices.
Vec3 preMult(const Vec3 &v) const