xsvector.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 XSVECTOR_H
66 #define XSVECTOR_H
67 
68 #include "xsmath.h"
69 #include <stddef.h>
70 #include <string.h> // memcpy
71 
72 struct XsVector;
73 struct XsQuaternion;
74 
75 #ifdef __cplusplus
76 #include <vector>
77 #include <algorithm>
78 #if __cplusplus >= 201103L && !defined(XSENS_HAVE_TYPE_TRAITS)
79 #include <type_traits>
80 #define XSENS_HAVE_TYPE_TRAITS
81 #endif
82 
83 extern "C" {
84 #endif
85 #ifndef __cplusplus
86 #define XSVECTOR_INITIALIZER { NULL, 0, 0 }
87 typedef struct XsVector XsVector;
88 #endif
89 
90 XSTYPES_DLL_API void XsVector_ref(XsVector* thisPtr, XsSize sz, XsReal* buffer, XsDataFlags flags);
91 XSTYPES_DLL_API void XsVector_construct(XsVector* thisPtr, XsSize sz, const XsReal* src);
92 XSTYPES_DLL_API void XsVector_assign(XsVector* thisPtr, XsSize sz, const XsReal* src);
94 XSTYPES_DLL_API void XsVector_copy(XsVector* copy, XsVector const* src);
99 XSTYPES_DLL_API int XsVector_empty(const XsVector* thisPtr);
100 XSTYPES_DLL_API void XsVector_multiplyScalar(const XsVector* thisPtr, XsReal scalar, XsVector* dest);
101 XSTYPES_DLL_API void XsVector_angularVelocityFromQuaternion(XsVector* thisPtr, XsReal deltaT, const struct XsQuaternion* quat);
103 XSTYPES_DLL_API void XsVector_fill(XsVector* thisPtr, XsReal value);
104 XSTYPES_DLL_API int XsVector_equal(const XsVector* thisPtr, const XsVector* thatPtr);
105 XSTYPES_DLL_API int XsVector_compare(const XsVector* thisPtr, const XsVector* thatPtr, XsReal epsilon);
106 
107 #ifdef __cplusplus
108 } // extern "C"
109 #endif
110 #ifndef XSENS_NO_PACK
111  #pragma pack(push,1)
112 #endif
113 struct XsVector
114 {
116  XsReal* const m_data;
117  const XsSize m_size;
118  const XsSize m_flags;
119 
120 #ifdef __cplusplus
121 
122 #ifdef __ICCARM__
123 #pragma diag_suppress=Pa039
124 #endif
125 
127  inline XsSize flags() const
128  {
129  return m_flags;
130  }
131 public:
133  inline explicit XsVector(XsSize sz = 0, const XsReal* src = 0)
134  : m_data(0)
135  , m_size(0)
136  , m_flags(0)
137  {
138  if (sz)
139  XsVector_construct(this, sz, src);
140  }
141 
143  inline XsVector(const XsVector& other)
144  : m_data(0)
145  , m_size(0)
146  , m_flags(0)
147  {
148  XsVector_copy(this, &other);
149  }
150 
152  inline explicit XsVector(XsReal* ref, XsSize sz, XsDataFlags flags_ = XSDF_None)
153  : m_data(ref)
154  , m_size(sz)
155  , m_flags((XsSize) flags_)
156  {
157  }
158 
160  inline explicit XsVector(const XsVector& other, XsReal* ref, XsSize sz, XsDataFlags flags_ = XSDF_None)
161  : m_data(ref)
162  , m_size(sz)
163  , m_flags((XsSize) flags_)
164  {
165  XsVector_copy(this, &other);
166  }
167 
169  inline explicit XsVector(const XsQuaternion& quat, XsReal deltaT)
170  : m_data(0)
171  , m_size(0)
172  , m_flags(0)
173  {
174  XsVector_angularVelocityFromQuaternion(this, deltaT, &quat);
175  }
176 
177 #if !defined(SWIG) && !defined(__ADSP21000__) && !defined(__AVR32__)
178  inline XsVector(XsVector&& other)
180  : m_data(0)
181  , m_size(0)
182  , m_flags(0)
183  {
184  if (!(other.m_flags & XSDF_Managed))
185  XsVector_copy(this, &other);
186  else
187  XsVector_swap(this, &other);
188  }
189 #endif
190 
192  inline XsVector& operator=(const XsVector& other)
193  {
194  XsVector_copy(this, &other);
195  return *this;
196  }
197 
199  inline ~XsVector()
200  {
201  XsVector_destruct(this);
202  }
203 
205  inline void assign(XsSize sz, const XsReal* src)
206  {
207  XsVector_assign(this, sz, src);
208  }
209 
214  inline void setSize(XsSize sz)
215  {
216  XsVector_assign(this, sz, 0);
217  }
218 
220  inline XsSize size() const
221  {
222  return m_size;
223  }
224 
226  inline const XsReal* data() const
227  {
228  return m_data;
229  }
230 
232  inline XsVector operator * (XsReal scalar) const
233  {
234  XsVector v(m_size);
235  for (XsSize i = 0; i < m_size; ++i)
236  v.m_data[i] = m_data[i] * scalar;
237  return v;
238  }
239 
241  inline void operator *=(XsReal scalar)
242  {
243  for (XsSize i = 0; i < m_size; ++i)
244  m_data[i] *= scalar;
245  }
246 
248  inline XsReal& at(XsSize index)
249  {
250  assert(index < m_size);
251  return m_data[index];
252  }
253 
255  inline const XsReal& at(XsSize index) const
256  {
257  assert(index < m_size);
258  return m_data[index];
259  }
260 
262  inline XsReal value(XsSize index) const
263  {
264  assert(index < m_size);
265  return m_data[index];
266  }
267 
269  inline void setValue(XsSize index, XsReal val)
270  {
271  assert(index < m_size);
272  m_data[index] = val;
273  }
274 
275 #ifndef SWIG
276  template<typename J>
278  inline XsReal operator[](J index) const
279  {
280 #ifdef XSENS_HAVE_TYPE_TRAITS
281  static_assert(std::is_integral<J>::value || std::is_enum<J>::value, "Integral index required.");
282 #endif
283  assert(static_cast<XsSize>(index) < m_size);
284  return m_data[index];
285  }
286 
288  template<typename J>
289  inline XsReal& operator[](J index)
290  {
291 #ifdef XSENS_HAVE_TYPE_TRAITS
292  static_assert(std::is_integral<J>::value || std::is_enum<J>::value, "Integral index required.");
293 #endif
294  assert(static_cast<XsSize>(index) < m_size);
295  return m_data[index];
296  }
297 #else
298  inline XsReal operator[](XsSize index) const
300  {
301  assert(index < m_size);
302  return m_data[index];
303  }
304 
306  inline XsReal& operator[](XsSize index)
307  {
308  assert(index < m_size);
309  return m_data[index];
310  }
311 #endif
312 
314  inline XsReal dotProduct(const XsVector& v) const
315  {
316  return XsVector_dotProduct(this, &v);
317  }
318 
320  inline XsReal cartesianLength() const
321  {
322  return XsVector_cartesianLength(this);
323  }
324 
326  inline void normalize()
327  {
328  XsVector_normalize(this);
329  }
330 
332  inline void setZero()
333  {
334  return XsVector_setZero(this);
335  }
336 
338  inline bool empty() const
339  {
340  return 0 != XsVector_empty(this);
341  }
342 
344  inline XsVector& angularVelocityFromQuaternion(const XsQuaternion& quat, XsReal deltaT)
345  {
346  XsVector_angularVelocityFromQuaternion(this, deltaT, &quat);
347  return *this;
348  }
349 
351  XsVector operator-(const XsVector& sub) const
352  {
353  assert(m_size == sub.m_size);
354  XsVector tmp(m_size);
355  for (XsSize i = 0; i < m_size; ++i)
356  tmp[i] = m_data[i] - sub.m_data[i];
357  return tmp;
358  }
359 
361  XsVector operator+(const XsVector& sub) const
362  {
363  assert(m_size == sub.m_size);
364  XsVector tmp(m_size);
365  for (XsSize i = 0; i < m_size; ++i)
366  tmp[i] = m_data[i] + sub.m_data[i];
367  return tmp;
368  }
369 
371  XsVector operator-=(const XsVector& sub)
372  {
373  assert(m_size == sub.m_size);
374  for (XsSize i = 0; i < m_size; ++i)
375  m_data[i] -= sub.m_data[i];
376  return *this;
377  }
378 
380  XsVector operator+=(const XsVector& sub)
381  {
382  assert(m_size == sub.m_size);
383  for (XsSize i = 0; i < m_size; ++i)
384  m_data[i] += sub.m_data[i];
385  return *this;
386  }
387 
389  bool operator==(const XsVector& other) const
390  {
391  return 0 != XsVector_equal(this, &other);
392  }
393 
399  bool isEqual(const XsVector& other, XsReal epsilon) const
400  {
401  return 0 != XsVector_compare(this, &other, epsilon);
402  }
403 
404 #ifndef XSENS_NO_STL
405  inline std::vector<XsReal> toVector() const
407  {
408  std::vector<XsReal> tmp(m_size);
409  if (m_size)
410  memcpy(&tmp[0], m_data, m_size * sizeof(XsReal));
411  return tmp;
412  }
413 #endif
414 
416  inline void zero()
417  {
418  for (XsSize i = 0; i < m_size; ++i)
419  m_data[i] = XsMath_zero;
420  }
421 
423  inline void fill(XsReal val)
424  {
425  for (XsSize i = 0; i < m_size; ++i)
426  m_data[i] = val;
427  }
428 
434  inline void swap(XsVector& b)
435  {
436  XsVector_swap(this, &b);
437  }
438 
440  friend void swap(XsVector& first, XsVector& second)
441  {
442  first.swap(second);
443  }
444 
449  inline void append(XsVector const& other)
450  {
451  XsVector tmp(size() + other.size());
452  for (XsSize i = 0; i < size(); ++i)
453  tmp[i] = (*this)[i];
454  for (XsSize i = 0; i < other.size(); ++i)
455  tmp[i + size()] = other[i];
456  swap(tmp);
457  }
458 
460  inline void clear()
461  {
462  setSize(0);
463  }
464 
465 #ifndef XSENS_NO_STL
466 
467  inline void reverse()
468  {
469  using std::swap;
470 
471  XsSize sz = size();
472  XsSize half = sz >> 1;
473  --sz;
474  for (XsSize i = 0; i < half; ++i)
475  swap(operator[](i), operator[](sz - i));
476  }
477 #endif
478 
479 #ifdef __ICCARM__
480 #pragma diag_default=Pa039
481 #endif
482 
483 #endif
484 };
485 #ifndef XSENS_NO_PACK
486  #pragma pack(pop)
487 #endif
488 
489 #ifdef __cplusplus
490 inline XsVector operator *(XsReal scalar, const XsVector& v)
492 {
493  return v * scalar;
494 }
495 #endif
496 
497 #endif
XsVector::m_data
XSCPPPROTECTED XsReal *const m_data
Points to contained data buffer.
Definition: xsvector.h:116
XsVector
A class that represents a vector of real numbers.
Definition: xsvector.h:113
XsVector::XsVector_fill
void XsVector_fill(struct XsVector *thisPtr, XsReal value)
Sets all elements of the XsVector to value.
Definition: xsvector.c:238
XsVector::XsVector_assign
void XsVector_assign(XsVector *thisPtr, XsSize sz, const XsReal *src)
Initialize the XsVector using sz number of items from src.
Definition: xsvector.c:123
XsVector::XsVector_copy
void XsVector_copy(XsVector *copy, XsVector const *src)
Copy the contents of the XsVector to copy.
Definition: xsvector.c:175
XsVector::XsVector_swap
void XsVector_swap(XsVector *a, XsVector *b)
Swap the contents of a and b.
Definition: xsvector.c:286
XsVector::XsVector_cartesianLength
XsReal XsVector_cartesianLength(const XsVector *thisPtr)
Compute and return the cartesian length.
Definition: xsvector.c:204
XsVector::XsVector_angularVelocityFromQuaternion
void XsVector_angularVelocityFromQuaternion(XsVector *thisPtr, XsReal deltaT, const XsQuaternion *quat)
Get an effective angular velocity from the quaternion, which must represent a delta angle.
Definition: xsvector.c:266
XsVector::XsVector_equal
int XsVector_equal(const struct XsVector *a, const struct XsVector *b)
Returns non-zero when the two vectors are identical.
Definition: xsvector.c:300
XsVector::XsVector_empty
int XsVector_empty(const XsVector *thisPtr)
Returns a non-zero value if the XsVector does not contain any values.
Definition: xsvector.c:246
XsQuaternion
A class that implements a quaternion.
Definition: xsquaternion.h:102
data
data
XSCPPPROTECTED
#define XSCPPPROTECTED
Definition: xstypesconfig.h:116
operator==
bool operator==(const XsFilterProfile &lhs, const XsFilterProfile &rhs)
Returns true if lhs has the same type as rhs.
Definition: scenariomatchpred.h:81
XsReal
double XsReal
Defines the floating point type used by the Xsens libraries.
Definition: xstypedefs.h:73
XsVector::XsVector_compare
int XsVector_compare(const struct XsVector *thisPtr, const struct XsVector *thatPtr, XsReal epsilon)
Returns non-zero if the two vectors are equal within epsilon.
Definition: xsvector.c:322
XsSize
size_t XsSize
XsSize must be unsigned number!
Definition: xstypedefs.h:74
XsVector
struct XsVector XsVector
Definition: xsvector.h:87
XsVector::XsVector_dotProduct
XsReal XsVector_dotProduct(const XsVector *a, const XsVector *b)
Compute and return the dot product of XsVectors a and b.
Definition: xsvector.c:189
xsmath.h
XsVector::m_size
const XsSize m_size
Size of contained data buffer in elements.
Definition: xsvector.h:117
append
ROSCPP_DECL std::string append(const std::string &left, const std::string &right)
XsMath_zero
XSMATHCONST XsReal XsMath_zero
0
Definition: xsmath.h:150
XsVector::XsVector_normalize
void XsVector_normalize(XsVector *thisPtr)
Normalize the vector.
Definition: xsvector.c:212
XsVector::XsVector_ref
void XsVector_ref(XsVector *thisPtr, XsSize sz, XsReal *buffer, XsDataFlags flags)
Initialize the XsVector to refer to the supplied buffer.
Definition: xsvector.c:91
XSDF_None
@ XSDF_None
No flag set.
Definition: xstypedefs.h:109
XSTYPES_DLL_API
#define XSTYPES_DLL_API
Definition: xstypesconfig.h:65
operator+
TF2SIMD_FORCE_INLINE Vector3 operator+(const Vector3 &v1, const Vector3 &v2)
XsDataFlags
XsDataFlags
These flags define the behaviour of data contained by Xsens data structures.
Definition: xstypedefs.h:107
XsVector::XsVector_construct
void XsVector_construct(XsVector *thisPtr, XsSize sz, const XsReal *src)
Initialize the XsVector using sz number of items from src.
Definition: xsvector.c:100
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
XsVector::XsVector_setZero
void XsVector_setZero(XsVector *thisPtr)
Sets all elements of the XsVector to 0.
Definition: xsvector.c:226
operator-
TF2SIMD_FORCE_INLINE Quaternion operator-(const Quaternion &q)
XsVector::m_flags
const XsSize m_flags
Flags for data management.
Definition: xsvector.h:118
XsVector::XsVector_multiplyScalar
void XsVector_multiplyScalar(const XsVector *thisPtr, XsReal scalar, XsVector *dest)
Multiplies all values in this XsVector by scalar and puts the result in XsVector dest.
Definition: xsvector.c:252
XsVector::XsVector_destruct
void XsVector_destruct(XsVector *thisPtr)
Release and clear the contents of the vector.
Definition: xsvector.c:155


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