xsmatrix.h
Go to the documentation of this file.
1 
2 // Copyright (c) 2003-2021 Xsens Technologies B.V. or subsidiaries worldwide.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions, and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions, and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 //
15 // 3. Neither the names of the copyright holders nor the names of their contributors
16 // may be used to endorse or promote products derived from this software without
17 // specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24 // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR
26 // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS
28 // SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES
29 // OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE
30 // ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES.
31 //
32 
33 
34 // Copyright (c) 2003-2021 Xsens Technologies B.V. or subsidiaries worldwide.
35 // All rights reserved.
36 //
37 // Redistribution and use in source and binary forms, with or without modification,
38 // are permitted provided that the following conditions are met:
39 //
40 // 1. Redistributions of source code must retain the above copyright notice,
41 // this list of conditions, and the following disclaimer.
42 //
43 // 2. Redistributions in binary form must reproduce the above copyright notice,
44 // this list of conditions, and the following disclaimer in the documentation
45 // and/or other materials provided with the distribution.
46 //
47 // 3. Neither the names of the copyright holders nor the names of their contributors
48 // may be used to endorse or promote products derived from this software without
49 // specific prior written permission.
50 //
51 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
52 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
53 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
54 // THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 // SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
56 // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR
58 // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
59 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS
60 // SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES
61 // OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE
62 // ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES.
63 //
64 
65 #ifndef XSMATRIX_H
66 #define XSMATRIX_H
67 
68 #include "xsmath.h"
69 #include <math.h>
70 #include <stddef.h>
71 
72 struct XsMatrix;
73 struct XsEuler;
74 struct XsQuaternion;
75 
76 #ifdef __cplusplus
77 extern "C" {
78 #endif
79 #ifndef __cplusplus
80 #define XSMATRIX_INITIALIZER { NULL, 0, 0, 0, XSDF_Managed }
81 typedef struct XsMatrix XsMatrix;
82 #endif
83 
84 XSTYPES_DLL_API void XsMatrix_ref(XsMatrix* thisPtr, XsSize rows, XsSize cols, XsSize stride, XsReal* buffer, XsDataFlags flags);
85 XSTYPES_DLL_API void XsMatrix_construct(XsMatrix* thisPtr, XsSize rows, XsSize cols, XsSize stride, const XsReal* src, XsSize srcStride);
86 XSTYPES_DLL_API void XsMatrix_assign(XsMatrix* thisPtr, XsSize rows, XsSize cols, XsSize stride, const XsReal* src, XsSize srcStride);
88 XSTYPES_DLL_API void XsMatrix_copy(XsMatrix* copy, XsMatrix const* src);
90 XSTYPES_DLL_API int XsMatrix_empty(const XsMatrix* thisPtr);
91 XSTYPES_DLL_API void XsMatrix_multiplyScalar(const XsMatrix* thisPtr, XsReal scalar, XsMatrix* dest);
92 XSTYPES_DLL_API XsSize XsMatrix_offset(const XsMatrix* thisPtr, XsSize row, XsSize column);
93 XSTYPES_DLL_API XsReal XsMatrix_value(const XsMatrix* thisPtr, XsSize row, XsSize column);
94 XSTYPES_DLL_API void XsMatrix_setValue(XsMatrix* thisPtr, XsSize row, XsSize column, XsReal value);
95 XSTYPES_DLL_API int XsMatrix_dimensionsMatch(const XsMatrix* thisPtr, XsSize rows, XsSize columns);
96 XSTYPES_DLL_API void XsMatrix_fromQuaternion(XsMatrix* thisPtr, const struct XsQuaternion* quat);
98 
99 #define XsMatrix_offsetM(thisPtr, row, column) (thisPtr->m_stride*row + column)
100 
101 #ifdef __cplusplus
102 } // extern "C"
103 #endif
104 #ifndef XSENS_NO_PACK
105  #pragma pack(push, 1)
106 #endif
107 struct XsMatrix
108 {
110  XsReal* const m_data;
111  const XsSize m_rows;
112  const XsSize m_cols;
113  const XsSize m_stride;
114  const XsSize m_flags;
115 
116 #ifdef __cplusplus
117 
118 #ifdef __ICCARM__
119 #pragma diag_suppress=Pa039
120 #endif
121 
123  inline XsSize flags() const
124  {
125  return m_flags;
126  }
127 public:
129  inline explicit XsMatrix(XsSize rows = 0, XsSize cols = 0, XsSize strde = 0, const XsReal* dat = 0)
130  : m_data(0)
131  , m_rows(0)
132  , m_cols(0)
133  , m_stride(0)
134  , m_flags(0)
135  {
136  if (rows && cols)
137  XsMatrix_construct(this, rows, cols, strde ? strde : cols, dat, 0);
138  }
139 
141  inline XsMatrix(const XsMatrix& other)
142  : m_data(0)
143  , m_rows(0)
144  , m_cols(0)
145  , m_stride(0)
146  , m_flags(0)
147  {
148  XsMatrix_copy(this, &other);
149  }
150 
151 #if !defined(SWIG) && !defined(__ADSP21000__) && !defined(__AVR32__)
152 
153  inline XsMatrix(XsMatrix&& other)
154  : m_data(0)
155  , m_rows(0)
156  , m_cols(0)
157  , m_stride(0)
158  , m_flags(0)
159  {
160  if (!(other.m_flags & XSDF_Managed))
161  XsMatrix_copy(this, &other);
162  else
163  XsMatrix_swap(this, &other);
164  }
165 #endif
166 
168  inline explicit XsMatrix(XsReal* ref, XsSize rows, XsSize cols, XsSize stride, XsDataFlags flags /* = XSDF_None */)
169  : m_data(ref)
170  , m_rows(rows)
171  , m_cols(cols)
172  , m_stride(stride)
173  , m_flags((XsSize) flags)
174  {
175  }
176 
178  inline explicit XsMatrix(const XsMatrix& other, XsReal* ref, XsSize rows, XsSize cols, XsSize stride, XsDataFlags flags /* = XSDF_None */)
179  : m_data(ref)
180  , m_rows(rows)
181  , m_cols(cols)
182  , m_stride(stride)
183  , m_flags((XsSize) flags)
184  {
185  XsMatrix_copy(this, &other);
186  }
187 
189  inline explicit XsMatrix(const XsQuaternion& quat)
190  : m_data(0)
191  , m_rows(0)
192  , m_cols(0)
193  , m_stride(0)
194  , m_flags(0)
195  {
196  XsMatrix_fromQuaternion(this, &quat);
197  }
198 
200  inline ~XsMatrix()
201  {
202  XsMatrix_destruct(this);
203  }
204 
206  inline void setSize(XsSize rows, XsSize cols, XsSize stride = 0)
207  {
208  XsMatrix_assign(this, rows, cols, stride, 0, 0);
209  }
210 
212  inline XsMatrix& operator=(const XsMatrix& other)
213  {
214  XsMatrix_copy(this, &other);
215  return *this;
216  }
217 
219  inline bool empty() const
220  {
221  return 0 != XsMatrix_empty(this);
222  }
223 
225  inline void setZero()
226  {
227  XsMatrix_setZero(this);
228  }
229 
231  inline XsSize offset(XsSize row, XsSize column) const
232  {
233  return XsMatrix_offset(this, row, column);
234  }
235 
237  inline XsReal value(XsSize row, XsSize column) const
238  {
239  return m_data[XsMatrix_offset(this, row, column)];
240  }
241 
243  inline void setValue(XsSize row, XsSize column, XsReal value)
244  {
245  m_data[XsMatrix_offsetM(this, row, column)] = value;
246  }
247 
249  inline const XsReal* operator[](XsSize row) const
250  {
251  return &m_data[XsMatrix_offsetM(this, row, 0)];
252  }
253 
255  inline XsReal* operator[](XsSize row)
256  {
257  return &m_data[XsMatrix_offsetM(this, row, 0)];
258  }
259 
261  inline XsMatrix operator*(XsReal scalar) const
262  {
263  XsMatrix tmp(m_rows, m_cols);
264  XsMatrix_multiplyScalar(this, scalar, &tmp);
265  return tmp;
266  }
267 
269  inline XsMatrix& fromQuaternion(const XsQuaternion& quat)
270  {
271  XsMatrix_fromQuaternion(this, &quat);
272  return *this;
273  }
274 
276  inline void zero()
277  {
278  for (XsSize r = 0; r < m_rows; ++r)
279  for (XsSize c = 0; c < m_cols; ++c)
280  m_data[XsMatrix_offsetM(this, r, c)] = XsMath_zero;
281  }
282 
284  inline XsSize rows() const
285  {
286  return m_rows;
287  }
288 
290  inline XsSize cols() const
291  {
292  return m_cols;
293  }
294 
302  inline XsSize stride() const
303  {
304  return m_stride;
305  }
306 
308  inline const XsReal* data() const
309  {
310  return m_data;
311  }
312 
314  inline bool operator ==(const XsMatrix& other) const
315  {
316  if (this == &other)
317  return true;
318  if (m_rows != other.m_rows || m_cols != other.m_cols)
319  return false;
320  for (XsSize r = 0; r < m_rows; ++r)
321  for (XsSize c = 0; c < m_cols; ++c)
322  if (m_data[XsMatrix_offsetM(this, r, c)] != other.m_data[XsMatrix_offsetM((&other), r, c)])
323  return false;
324  return true;
325  }
326 
328  inline bool isEqual(const XsMatrix& other, XsReal tolerance) const
329  {
330  if (this == &other)
331  return true;
332  if (m_rows != other.m_rows || m_cols != other.m_cols)
333  return false;
334  for (XsSize r = 0; r < m_rows; ++r)
335  for (XsSize c = 0; c < m_cols; ++c)
336  if (fabs(m_data[XsMatrix_offsetM(this, r, c)] - other.m_data[XsMatrix_offsetM((&other), r, c)]) > tolerance)
337  return false;
338  return true;
339  }
340 
346  inline void swap(XsMatrix& b)
347  {
348  XsMatrix_swap(this, &b);
349  }
350 
352  friend void swap(XsMatrix& first, XsMatrix& second)
353  {
354  first.swap(second);
355  }
356 
357 #ifdef __ICCARM__
358 #pragma diag_default=Pa039
359 #endif
360 
361 #endif
362 };
363 #ifndef XSENS_NO_PACK
364  #pragma pack(pop)
365 #endif
366 
367 #ifdef __cplusplus
368 inline XsMatrix operator *(XsReal scalar, const XsMatrix& m)
370 {
371  return (m * scalar);
372 }
373 #endif
374 
375 #endif
XsMatrix::XsMatrix_swap
void XsMatrix_swap(XsMatrix *a, XsMatrix *b)
Swap the contents of a and b.
Definition: xsmatrix.c:362
XsMatrix::XsMatrix_ref
void XsMatrix_ref(XsMatrix *thisPtr, XsSize rows, XsSize cols, XsSize stride, XsReal *buffer, XsDataFlags flags)
Construct the XsMatrix as a reference to data in buffer.
Definition: xsmatrix.c:87
XsMatrix
A class that represents a matrix of real numbers.
Definition: xsmatrix.h:107
XsMatrix::XsMatrix_destruct
void XsMatrix_destruct(XsMatrix *thisPtr)
Clear the XsMatrix and release allocated resources.
Definition: xsmatrix.c:218
XsMatrix::XsMatrix_setZero
void XsMatrix_setZero(XsMatrix *thisPtr)
Set all the values in the matrix to zero.
Definition: xsmatrix.c:248
XsMatrix::XsMatrix_value
XsReal XsMatrix_value(const XsMatrix *thisPtr, XsSize row, XsSize column)
Returns the data value at row and column.
Definition: xsmatrix.c:299
XsMatrix::XsMatrix_copy
void XsMatrix_copy(XsMatrix *copy, XsMatrix const *src)
Copy the contents of copy to the XsMatrix.
Definition: xsmatrix.c:240
XsMatrix::XsMatrix_empty
int XsMatrix_empty(const XsMatrix *thisPtr)
Returns not zero if the matrix contains no values.
Definition: xsmatrix.c:268
XsMatrix::XsMatrix_dimensionsMatch
int XsMatrix_dimensionsMatch(const XsMatrix *thisPtr, XsSize rows, XsSize columns)
Returns not zero if the dimensions of the XsMatrix are equal to rows and columns.
Definition: xsmatrix.c:312
XsQuaternion
A class that implements a quaternion.
Definition: xsquaternion.h:102
data
data
XsMatrix::m_rows
const XsSize m_rows
Number of rows in the matrix.
Definition: xsmatrix.h:111
XSCPPPROTECTED
#define XSCPPPROTECTED
Definition: xstypesconfig.h:116
XsMatrix::XsMatrix_offset
XsSize XsMatrix_offset(const XsMatrix *thisPtr, XsSize row, XsSize column)
Returns the offset in the data for accessing the value at row and column.
Definition: xsmatrix.c:293
operator==
bool operator==(const XsFilterProfile &lhs, const XsFilterProfile &rhs)
Returns true if lhs has the same type as rhs.
Definition: scenariomatchpred.h:81
XsMatrix::XsMatrix_setValue
void XsMatrix_setValue(XsMatrix *thisPtr, XsSize row, XsSize column, XsReal value)
Sets the data value at row and column.
Definition: xsmatrix.c:306
XsMatrix::m_data
XSCPPPROTECTED XsReal *const m_data
Contained data.
Definition: xsmatrix.h:110
operator*
TF2SIMD_FORCE_INLINE Vector3 operator*(const Matrix3x3 &m, const Vector3 &v)
XsMatrix_offsetM
#define XsMatrix_offsetM(thisPtr, row, column)
Definition: xsmatrix.h:99
XsReal
double XsReal
Defines the floating point type used by the Xsens libraries.
Definition: xstypedefs.h:73
XsMatrix
struct XsMatrix XsMatrix
Definition: xsmatrix.h:81
XsSize
size_t XsSize
XsSize must be unsigned number!
Definition: xstypedefs.h:74
XsMatrix::XsMatrix_construct
void XsMatrix_construct(XsMatrix *thisPtr, XsSize rows, XsSize cols, XsSize stride, const XsReal *src, XsSize srcStride)
Init the XsMatrix and copy the data from src into the matrix if src is not null.
Definition: xsmatrix.c:100
XsMatrix::m_cols
const XsSize m_cols
Number of columns in the matrix.
Definition: xsmatrix.h:112
xsmath.h
XsMath_zero
XSMATHCONST XsReal XsMath_zero
0
Definition: xsmath.h:150
XsMatrix::m_stride
const XsSize m_stride
Number of items per row in memory (usually equal to cols but not always)
Definition: xsmatrix.h:113
XsMatrix::XsMatrix_assign
void XsMatrix_assign(XsMatrix *thisPtr, XsSize rows, XsSize cols, XsSize stride, const XsReal *src, XsSize srcStride)
Init the XsMatrix and copy the data from src into the matrix if src is not null.
Definition: xsmatrix.c:146
XSTYPES_DLL_API
#define XSTYPES_DLL_API
Definition: xstypesconfig.h:65
XsMatrix::XsMatrix_fromQuaternion
void XsMatrix_fromQuaternion(XsMatrix *thisPtr, const XsQuaternion *quat)
Get an orientation matrix representation of the quaternion.
Definition: xsmatrix.c:318
XsEuler
Contains Euler Angle data and conversion from Quaternion.
Definition: xseuler.h:92
XsDataFlags
XsDataFlags
These flags define the behaviour of data contained by Xsens data structures.
Definition: xstypedefs.h:107
XSDF_Managed
@ XSDF_Managed
The contained data should be managed (freed) by the object, when false, the object assumes the memory...
Definition: xstypedefs.h:110
XsMatrix::XsMatrix_multiplyScalar
void XsMatrix_multiplyScalar(const XsMatrix *thisPtr, XsReal scalar, XsMatrix *dest)
Multiplies all values in this XsMatrix by scalar.
Definition: xsmatrix.c:277
XsMatrix::m_flags
const XsSize m_flags
Flags for data management.
Definition: xsmatrix.h:114


xsens_mti_driver
Author(s):
autogenerated on Sun Sep 3 2023 02:43:20