Debian Codegraph @

3rdparty/xspublic/xstypes/xsmatrix.h

raw
1
2 // Copyright (c) 2003-2019 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 #ifndef XSMATRIX_H
34 #define XSMATRIX_H
35
36 #include "xsmath.h"
37 #include <math.h>
38 #include <stddef.h>
39
40 struct XsMatrix;
41 struct XsEuler;
42 struct XsQuaternion;
43
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47 #ifndef __cplusplus
48 #define XSMATRIX_INITIALIZER { NULL, 0, 0, 0, XSDF_Managed }
49 typedef struct XsMatrix XsMatrix;
50 #endif
51
52 XSTYPES_DLL_API void XsMatrix_ref(XsMatrix* thisPtr, XsSize rows, XsSize cols, XsSize stride, XsReal* buffer, XsDataFlags flags);
53 XSTYPES_DLL_API void XsMatrix_construct(XsMatrix* thisPtr, XsSize rows, XsSize cols, XsSize stride, const XsReal* src, XsSize srcStride);
54 XSTYPES_DLL_API void XsMatrix_assign(XsMatrix* thisPtr, XsSize rows, XsSize cols, XsSize stride, const XsReal* src, XsSize srcStride);
55 XSTYPES_DLL_API void XsMatrix_destruct(XsMatrix* thisPtr);
56 XSTYPES_DLL_API void XsMatrix_copy(XsMatrix* copy, XsMatrix const* src);
57 XSTYPES_DLL_API void XsMatrix_setZero(XsMatrix* thisPtr);
58 XSTYPES_DLL_API int XsMatrix_empty(const XsMatrix* thisPtr);
59 XSTYPES_DLL_API void XsMatrix_multiplyScalar(const XsMatrix* thisPtr, XsReal scalar, XsMatrix* dest);
60 XSTYPES_DLL_API XsSize XsMatrix_offset(const XsMatrix* thisPtr, XsSize row, XsSize column);
61 XSTYPES_DLL_API XsReal XsMatrix_value(const XsMatrix* thisPtr, XsSize row, XsSize column);
62 XSTYPES_DLL_API void XsMatrix_setValue(XsMatrix* thisPtr, XsSize row, XsSize column, XsReal value);
63 XSTYPES_DLL_API int XsMatrix_dimensionsMatch(const XsMatrix* thisPtr, XsSize rows, XsSize columns);
64 XSTYPES_DLL_API void XsMatrix_fromQuaternion(XsMatrix* thisPtr, const struct XsQuaternion* quat);
65 XSTYPES_DLL_API void XsMatrix_swap(XsMatrix* a, XsMatrix* b);
66
67 #define XsMatrix_offsetM(thisPtr, row, column) (thisPtr->m_stride*row + column)
68
69 #ifdef __cplusplus
70 } // extern "C"
71 #endif
72 #ifndef XSENS_NO_PACK
73 #pragma pack(push, 1)
74 #endif
75 struct XsMatrix {
76 XSCPPPROTECTED
77 XsReal* const m_data; //!< Contained data
78 const XsSize m_rows; //!< Number of rows in the matrix
79 const XsSize m_cols; //!< Number of columns in the matrix
80 const XsSize m_stride; //!< Number of items per row in memory (usually equal to cols but not always)
81 const XsSize m_flags; //!< Flags for data management
82
83 #ifdef __cplusplus
84 //! \brief Return the data management flags of the matrix.
85 inline XsSize flags() const { return m_flags; }
86 public:
87 /*! \brief Initialize an XsMatrix object with the specified number of \a rows and \a cols */
88 inline explicit XsMatrix(XsSize rows = 0, XsSize cols = 0, XsSize strde = 0, const XsReal* dat = 0)
89 : m_data(0)
90 , m_rows(0)
91 , m_cols(0)
92 , m_stride(0)
93 , m_flags(0)
94 {
95 if (rows && cols)
96 XsMatrix_construct(this, rows, cols, strde?strde:cols, dat, 0);
97 }
98
99 /*! \brief Initialize an XsMatrix object from the \a other XsMatrix */
100 inline XsMatrix(const XsMatrix& other)
101 : m_data(0)
102 , m_rows(0)
103 , m_cols(0)
104 , m_stride(0)
105 , m_flags(0)
106 {
107 XsMatrix_copy(this, &other);
108 }
109
110 /*! \brief Initialize an XsMatrix object that references the data passed in \a ref. \a rows, \a cols and \a stride can be used to specify the layout of the data */
111 inline explicit XsMatrix(XsReal* ref, XsSize rows, XsSize cols, XsSize stride, XsDataFlags flags /* = XSDF_None */)
112 : m_data(ref)
113 , m_rows(rows)
114 , m_cols(cols)
115 , m_stride(stride)
116 , m_flags((XsSize) flags)
117 {
118 }
119
120 /*! \brief Initialize a copy of \a other in an XsMatrix object that references the data passed in \a ref. \a rows, \a cols and \a stride can be used to specify the layout of the data */
121 inline explicit XsMatrix(const XsMatrix& other, XsReal* ref, XsSize rows, XsSize cols, XsSize stride, XsDataFlags flags /* = XSDF_None */)
122 : m_data(ref)
123 , m_rows(rows)
124 , m_cols(cols)
125 , m_stride(stride)
126 , m_flags((XsSize) flags)
127 {
128 XsMatrix_copy(this, &other);
129 }
130
131 //! \brief \copybrief XsMatrix_fromQuaternion
132 inline explicit XsMatrix(const XsQuaternion& quat)
133 : m_data(0)
134 , m_rows(0)
135 , m_cols(0)
136 , m_stride(0)
137 , m_flags(0)
138 {
139 XsMatrix_fromQuaternion(this, &quat);
140 }
141
142 //! \copydoc XsMatrix_destruct
143 inline ~XsMatrix()
144 {
145 XsMatrix_destruct(this);
146 }
147
148 /*! \brief Resize the matrix to the specified number of \a rows and \a cols, destroying its current contents */
149 inline void setSize(XsSize rows, XsSize cols, XsSize stride = 0)
150 {
151 XsMatrix_assign(this, rows, cols, stride, 0, 0);
152 }
153
154 /*! \brief \copybrief XsMatrix_copy */
155 inline XsMatrix& operator=(const XsMatrix& other)
156 {
157 XsMatrix_copy(this, &other);
158 return *this;
159 }
160
161 //! \brief \copybrief XsMatrix_empty
162 inline bool empty() const
163 {
164 return 0 != XsMatrix_empty(this);
165 }
166
167 //! \brief \copybrief XsMatrix_setZero
168 inline void setZero()
169 {
170 XsMatrix_setZero(this);
171 }
172
173 //! \copydoc XsMatrix_offset */
174 inline XsSize offset(XsSize row, XsSize column) const
175 {
176 return XsMatrix_offset(this, row, column);
177 }
178
179 /*! \brief Returns the value at \a row and \a column in the matrix */
180 inline XsReal value(XsSize row, XsSize column) const
181 {
182 return m_data[XsMatrix_offset(this, row, column)];
183 }
184
185 /*! \brief Sets the \a value at \a row and \a column in the matrix */
186 inline void setValue(XsSize row, XsSize column, XsReal value)
187 {
188 m_data[XsMatrix_offsetM(this, row, column)] = value;
189 }
190
191 /*! \brief Returns a pointer to the data in \a row */
192 inline const XsReal* operator[](XsSize row) const
193 {
194 return &m_data[XsMatrix_offsetM(this, row, 0)];
195 }
196
197 /*! \brief Returns a reference to the data in \a row */
198 inline XsReal* operator[](XsSize row)
199 {
200 return &m_data[XsMatrix_offsetM(this, row, 0)];
201 }
202
203 /*! \brief \copybrief XsMatrix_multiplyScalar */
204 inline XsMatrix operator*(XsReal scalar) const
205 {
206 XsMatrix tmp(m_rows, m_cols);
207 XsMatrix_multiplyScalar(this, scalar, &tmp);
208 return tmp;
209 }
210
211 /*! \brief \copybrief XsMatrix_fromQuaternion */
212 inline XsMatrix& fromQuaternion(const XsQuaternion& quat)
213 {
214 XsMatrix_fromQuaternion(this, &quat);
215 return *this;
216 }
217
218 /*! \brief Fill the matrix with zeroes */
219 inline void zero()
220 {
221 for (XsSize r = 0; r < m_rows; ++r)
222 for (XsSize c = 0; c < m_cols; ++c)
223 m_data[XsMatrix_offsetM(this, r, c)] = XsMath_zero;
224 }
225
226 /*! \brief Return the number of rows in the matrix */
227 inline XsSize rows() const
228 {
229 return m_rows;
230 }
231
232 /*! \brief Return the number of columns in the matrix */
233 inline XsSize cols() const
234 {
235 return m_cols;
236 }
237
238 /*! \brief Return the stride of the matrix.
239 \details The stride of a matrix is for internal administration. It defines the number of items
240 in a row in the data buffer. This is always greater than or equal to the number of columns.
241 Especially for matrices that reference a part of another matrix this may differ from the
242 cols() value.
243 \returns The stride of the matrix.
244 */
245 inline XsSize stride() const
246 {
247 return m_stride;
248 }
249
250 //! \brief Return a const pointer to the internal data
251 inline const XsReal* data() const
252 {
253 return m_data;
254 }
255
256 //! \brief Returns true if \a other is numerically identical to this
257 inline bool operator ==(const XsMatrix& other) const
258 {
259 if (this == &other)
260 return true;
261 if (m_rows != other.m_rows || m_cols != other.m_cols)
262 return false;
263 for (XsSize r = 0; r < m_rows; ++r)
264 for (XsSize c = 0; c < m_cols; ++c)
265 if (m_data[XsMatrix_offsetM(this, r, c)] != other.m_data[XsMatrix_offsetM((&other), r, c)])
266 return false;
267 return true;
268 }
269
270 //! \brief Returns true if the values of this and \a other are within \a tolerance of each other
271 inline bool isEqual(const XsMatrix& other, XsReal tolerance) const
272 {
273 if (this == &other)
274 return true;
275 if (m_rows != other.m_rows || m_cols != other.m_cols)
276 return false;
277 for (XsSize r = 0; r < m_rows; ++r)
278 for (XsSize c = 0; c < m_cols; ++c)
279 if (fabs(m_data[XsMatrix_offsetM(this, r, c)] - other.m_data[XsMatrix_offsetM((&other), r, c)]) > tolerance)
280 return false;
281 return true;
282 }
283
284 /*! \brief Swap the contents of \a b with this
285 \details This function swaps the internal buffers so no actual data is moved around. For unmanaged
286 data an elementwise swap is done, but only if the matrices are the same size.
287 \param b Object whose contents will be swapped with this
288 */
289 inline void swap(XsMatrix& b)
290 {
291 XsMatrix_swap(this, &b);
292 }
293
294 #endif
295 };
296 #ifndef XSENS_NO_PACK
297 #pragma pack(pop)
298 #endif
299
300 #ifdef __cplusplus
301 //! \brief Multiplies all values in the matrix \a m by \a scalar
302 inline XsMatrix operator *(XsReal scalar, const XsMatrix &m)
303 {
304 return (m*scalar);
305 }
306 #endif
307
308 #endif