xsens_generic_matrix.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 XSENS_GENERIC_MATRIX_H
66 #define XSENS_GENERIC_MATRIX_H
67 
68 #include "xsens_math_throw.h"
69 
70 #ifndef XSENS_MATH_FIRMWARE
71  #ifndef XSENS_EXCEPTION_H
72  #include <xstypes/xsexception.h>
73  #endif
74 #endif
75 
76 #ifdef XSENS_GENERIC_MATRIX_RANGE_CHECKS
77  #ifdef _MSC_VER
78  #define XSENS_GENERIC_MATRIX_THROW throw(...)
79  #else
80  #define XSENS_GENERIC_MATRIX_THROW
81  #endif
82 #else
83  #define XSENS_GENERIC_MATRIX_THROW
84 #endif
85 
86 #ifndef XSENS_THROW_BAD_ALLOC
87  #ifdef XSENS_NO_EXCEPTIONS
88  #include <assert.h>
89  #define XSENS_THROW_BAD_ALLOC XM_THROW("Bad alloc")
90  #else
91  #define XSENS_THROW_BAD_ALLOC throw std::bad_alloc()
92  #endif
93 #endif
94 
95 #ifndef _PSTDINT_H_INCLUDED
96  #include <xstypes/pstdint.h>
97 #endif
98 
99 #include <stdlib.h>
100 #include <string.h>
101 
102 #ifdef __cplusplus
103 namespace xsens
104 {
105 namespace
106 {
108 template <typename T>
109 inline void swapb(T& a, T& b)
110 {
111  T temp = a;
112  a = b;
113  b = temp;
114 }
115 }
116 
125 template <typename T>
126 class GenericMatrix
127 {
128 protected:
129  T* m_data;
130  uint32_t m_rows;
131  uint32_t m_cols;
132  uint32_t m_allocSize;
133  int m_flags;
134 
135 public:
137  GenericMatrix();
139  explicit GenericMatrix(const uint32_t newRows, const uint32_t newCols, bool zeroValues = true);
141  GenericMatrix(const GenericMatrix<T>& src);
143  explicit GenericMatrix(const uint32_t newRows, const uint32_t newCols, const T* src);
145  explicit GenericMatrix(const uint32_t newRows, const uint32_t newCols, T* buffer, XsDataFlags flags);
147  virtual ~GenericMatrix();
148 
150  void setSize(uint32_t newRows, uint32_t newCols, bool zeroValues = true);
152  T& get(const uint32_t row, const uint32_t cols) const XSENS_GENERIC_MATRIX_THROW;
154  T* operator [](const uint32_t row) const XSENS_GENERIC_MATRIX_THROW;
156  uint32_t rows(void) const
157  {
158  return m_rows;
159  }
161  uint32_t cols(void) const
162  {
163  return m_cols;
164  }
166  uint32_t count(void) const
167  {
168  return m_rows * m_cols;
169  }
171  uint32_t size(void) const
172  {
173  return m_rows * m_cols;
174  }
176  GenericMatrix<T>& operator = (const GenericMatrix<T>& src);
178  void swap(GenericMatrix<T>& other);
180  friend void swap(GenericMatrix<T>& first, GenericMatrix<T>& second)
181  {
182  first.swap(second);
183  }
185  void zeroValues();
186 };
187 
188 template <typename T> GenericMatrix<T>::GenericMatrix()
189  : m_rows(1)
190  , m_cols(1)
191  , m_flags(XSDF_Managed)
192 {
193  m_data = (T*)malloc(m_rows * m_cols * sizeof(T));
194 
195  if (!m_data)
197 
198  m_allocSize = m_rows * m_cols;
199 }
200 
201 template <typename T> GenericMatrix<T>::GenericMatrix(uint32_t newRows, uint32_t newCols, bool zeroVals)
202  : m_rows(newRows)
203  , m_cols(newCols)
204  , m_flags(XSDF_Managed)
205 {
206  m_data = (T*)malloc(sizeof(T) * (size_t)m_rows * m_cols);
207 
208  if (!m_data)
210 
211  m_allocSize = m_rows * m_cols;
212 
213  if (zeroVals)
214  zeroValues();
215 }
216 
217 template <typename T> GenericMatrix<T>::GenericMatrix(const GenericMatrix<T>& src)
218  : m_rows(src.m_rows)
219  , m_cols(src.m_cols)
220  , m_flags(XSDF_Managed)
221 {
222  m_data = (T*)malloc(sizeof(T) * (size_t)m_rows * m_cols);
223 
224  if (!m_data)
226 
227  m_allocSize = m_rows * m_cols;
228 
229  memcpy(m_data, src.m_data, sizeof(T) * (size_t)m_rows * m_cols);
230 }
231 
232 template <typename T> GenericMatrix<T>::GenericMatrix(const uint32_t newRows, uint32_t newCols, const T* src)
233  : m_rows(newRows)
234  , m_cols(newCols)
235  , m_flags(XSDF_Managed)
236 {
237  m_data = (T*)malloc(sizeof(T) * (size_t)m_rows * m_cols);
238 
239  if (!m_data)
241 
242  m_allocSize = m_rows * m_cols;
243 
244  memcpy(m_data, src, sizeof(T) * (size_t)m_rows * m_cols);
245 }
246 
247 template <typename T> GenericMatrix<T>::GenericMatrix(const uint32_t newRows, const uint32_t newCols, T* const buffer, XsDataFlags flags)
248  : m_rows(newRows)
249  , m_cols(newCols)
250  , m_flags(flags)
251 {
252  if (m_flags & XSDF_FixedSize)
253  {
254  m_data = buffer;
255 
256  if (size() == 0)
257  m_flags |= XSDF_Empty;
258  }
259 
260  if (m_flags & XSDF_Managed)
261  m_data = (T*)malloc(sizeof(T) * (size_t)m_rows * m_cols);
262 
263  if (!m_data)
265 
266  m_allocSize = m_rows * m_cols;
267 
268  if (m_flags & XSDF_Managed)
269  memcpy(m_data, buffer, sizeof(T) * (size_t)m_rows * m_cols);
270 }
271 
272 template <typename T> GenericMatrix<T>::~GenericMatrix()
273 {
274  if (m_flags & XSDF_Managed)
275  free(m_data);
276 
277  if (!(m_flags & XSDF_FixedSize))
278  m_data = NULL;
279  else
280  m_flags |= XSDF_Empty;
281 }
282 
283 template <typename T>
284 void GenericMatrix<T>::setSize(uint32_t newRows, uint32_t newCols, bool zeroVals)
285 {
286  if (newRows == m_rows && newCols == m_cols)
287  {
288  if (zeroVals)
289  zeroValues();
290 
291  return;
292  }
293 
294 #ifdef XSENS_MATH_RESTRICT_RESIZE
295  if (m_data && !(m_flags & XSDF_Managed))
296  XM_THROW("resizing of this object is not allowed");
297 #endif
298 
299  const uint32_t newSize = newRows * newCols;
300 
301  if (m_flags & XSDF_FixedSize)
302  {
303  if (newSize == 0)
304  {
305  m_flags |= XSDF_Empty;
306  m_rows = newRows;
307  m_cols = newCols;
308  return;
309  }
310 
311  assert(newSize <= size());
312  m_flags &= ~XSDF_Empty;
313  }
314 
315  if (newSize > m_allocSize || newSize == 0)
316  {
317  if (m_flags & XSDF_Managed)
318  {
319  free(m_data);
320  m_data = 0;
321  m_allocSize = 0;
322  }
323 
324  if (newSize > 0)
325  {
326  m_data = (T*)malloc(sizeof(T) * (size_t)newRows * newCols);
327  m_flags = XSDF_Managed;
328  m_allocSize = newRows * newCols;
329  }
330  }
331 
332  m_rows = newRows;
333  m_cols = newCols;
334 
335  if (!m_data)
337 
338  if (zeroVals)
339  zeroValues();
340 }
341 
342 template <typename T>
343 T& GenericMatrix<T>::get(uint32_t row, uint32_t col) const XSENS_GENERIC_MATRIX_THROW
344 {
345 #ifdef XSENS_GENERIC_MATRIX_RANGE_CHECKS
346  if (row >= m_rows || col >= m_cols)
347  XM_THROW("index out of bounds");
348 #endif
349  return m_data[row * m_cols + col];
350 }
351 
352 template <typename T>
353 T* GenericMatrix<T>::operator [](const uint32_t row) const XSENS_GENERIC_MATRIX_THROW
354 {
355 #ifdef XSENS_GENERIC_MATRIX_RANGE_CHECKS
356  if (row >= m_rows)
357  XM_THROW("index out of bounds");
358 #endif
359  return &m_data[row * m_cols];
360 }
361 
362 template <typename T>
363 void GenericMatrix<T>::zeroValues()
364 {
365  memset(static_cast<void*>(m_data), 0, sizeof(T) * (size_t)m_rows * m_cols);
366 }
367 
368 template <typename T>
369 void GenericMatrix<T>::swap(GenericMatrix<T>& other)
370 {
371  swapb(m_rows, other.m_rows);
372  swapb(m_cols, other.m_cols);
373  swapb(m_allocSize, other.m_allocSize);
374  swapb(m_flags, other.m_flags);
375  swapb(m_data, other.m_data);
376 }
377 
378 template <typename T>
379 GenericMatrix<T>& GenericMatrix<T>::operator = (const GenericMatrix<T>& src)
380 {
381  if (this != &src)
382  {
383  GenericMatrix temp(src);
384  swap(temp);
385  }
386  return *this;
387 }
388 
389 }
390 
391 #endif
392 #endif
xsexception.h
get
ROSCPP_DECL bool get(const std::string &key, bool &b)
XSDF_FixedSize
@ XSDF_FixedSize
The contained data points to a fixed-size buffer, this allows creation of dynamic objects on the stac...
Definition: xstypedefs.h:111
uint32_t
unsigned int uint32_t
Definition: pstdint.h:485
XSENS_THROW_BAD_ALLOC
#define XSENS_THROW_BAD_ALLOC
Definition: xsens_generic_matrix.h:91
XSENS_GENERIC_MATRIX_THROW
#define XSENS_GENERIC_MATRIX_THROW
Definition: xsens_generic_matrix.h:83
XSDF_Empty
@ XSDF_Empty
The object contains undefined data / should be considered empty. Usually only relevant when XSDF_Fixe...
Definition: xstypedefs.h:112
XM_THROW
#define XM_THROW(a)
Definition: xsens_math_throw.h:109
pstdint.h
xsens_math_throw.h
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
assert.h
xsens
Definition: threading.cpp:78


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