xsvector.c
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 #include "xsvector.h"
66 #include <stdlib.h>
67 #include <math.h>
68 #include "xsdebugcounters.h"
69 #include <string.h>
70 #include "xsmalloc.h"
71 #include "xsquaternion.h"
72 #include "xscopy.h"
73 #include <assert.h>
74 #include "xsfloatmath.h"
75 
76 #define realSwap(a,b) { XsReal t = *a; *a = *b; *b = t; }
77 
78 #ifdef __ICCARM__
79  #pragma diag_suppress=Pa039
80 #endif
81 
90 void XsVector_ref(XsVector* thisPtr, XsSize sz, XsReal* buffer, XsDataFlags flags)
92 {
93  assert(sz == 0 || buffer != 0);
94  *((XsReal**) &thisPtr->m_data) = buffer;
95  *((XsSize*) &thisPtr->m_size) = sz;
96  *((XsSize*) &thisPtr->m_flags) = flags;
97 }
98 
100 void XsVector_construct(XsVector* thisPtr, XsSize sz, const XsReal* src)
101 {
102  if (sz)
103  {
104  // init to size
105  XsReal* data = (XsReal*) xsMathMalloc(sz * sizeof(XsReal));
106  assert(data);
107  *((XsReal**) &thisPtr->m_data) = data;
109  }
110  else
111  *((XsReal**) &thisPtr->m_data) = 0;
112  *((XsSize*) &thisPtr->m_flags) = XSDF_Managed;
113  *((XsSize*) &thisPtr->m_size) = sz;
114  if (src && sz)
115  memcpy(thisPtr->m_data, src, sz * sizeof(XsReal));
116 }
117 
123 void XsVector_assign(XsVector* thisPtr, XsSize sz, const XsReal* src)
124 {
125  if (thisPtr->m_flags & XSDF_FixedSize)
126  {
127  if (sz == 0)
128  {
129  *((XsSize*) &thisPtr->m_flags) |= XSDF_Empty;
130  return;
131  }
132  assert(sz == thisPtr->m_size);
133  *((XsSize*) &thisPtr->m_flags) &= ~XSDF_Empty;
134  }
135 
136  if (sz > thisPtr->m_size || sz == 0)
137  {
138  XsVector_destruct(thisPtr);
139  if (sz)
140  {
141  // init to size
142  XsReal* data = (XsReal*) xsMathMalloc(sz * sizeof(XsReal));
143  assert(data);
144  *((XsReal**) &thisPtr->m_data) = data;
145  *((XsSize*) &thisPtr->m_flags) = XSDF_Managed;
147  }
148  }
149  *((XsSize*) &thisPtr->m_size) = sz;
150  if (src && sz)
151  memcpy(thisPtr->m_data, src, sz * sizeof(XsReal));
152 }
153 
156 {
157  if (thisPtr->m_data && (thisPtr->m_flags & XSDF_Managed))
158  {
159  // clear contents
160  xsMathFree((void*) thisPtr->m_data);
162  }
163  // init to 0
164  if (!(thisPtr->m_flags & XSDF_FixedSize))
165  {
166  *((XsReal**) &thisPtr->m_data) = 0;
167  *((XsSize*) &thisPtr->m_size) = 0;
168  *((XsSize*) &thisPtr->m_flags) = 0;
169  }
170  else
171  *((XsSize*) &thisPtr->m_flags) |= XSDF_Empty;
172 }
173 
175 void XsVector_copy(XsVector* copy, XsVector const* src)
176 {
177  if (copy == src)
178  return;
179 
180  if (src->m_flags & XSDF_Empty)
181  XsVector_destruct(copy);
182  else
183  XsVector_assign(copy, src->m_size, src->m_data);
184 }
185 
190 {
191  XsSize i;
192  XsReal r = XsMath_zero;
193  assert(a->m_size == b->m_size);
194 
195  for (i = a->m_size; i--;)
196  r += a->m_data[i] * b->m_data[i];
197  return r;
198 }
199 
205 {
206  return sqrt(XsVector_dotProduct(thisPtr, thisPtr));
207 }
208 
213 {
215 
216  if (length < XsMath_tinyValue)
217  {
218  XsVector_setZero(thisPtr);
219  return;
220  }
221 
222  XsVector_multiplyScalar(thisPtr, XsMath_one / length, thisPtr);
223 }
224 
227 {
228 #if XSREAL_ALLOWS_MEMCPY
229  memset(thisPtr->m_data, 0, sizeof(XsReal)*thisPtr->m_size);
230 #else
231  XsSize i;
232  for (i = 0; i < thisPtr->m_size; ++i)
233  thisPtr->m_data[i] = XsMath_zero;
234 #endif
235 }
236 
238 void XsVector_fill(struct XsVector* thisPtr, XsReal value)
239 {
240  XsSize i;
241  for (i = 0; i < thisPtr->m_size; ++i)
242  thisPtr->m_data[i] = value;
243 }
244 
246 int XsVector_empty(const XsVector* thisPtr)
247 {
248  return (thisPtr->m_size == 0) || (thisPtr->m_flags & XSDF_Empty);
249 }
250 
252 void XsVector_multiplyScalar(const XsVector* thisPtr, XsReal scalar, XsVector* dest)
253 {
254  XsSize i;
255  XsVector_assign(dest, thisPtr->m_size, 0);
256  for (i = 0; i < thisPtr->m_size; ++i)
257  dest->m_data[i] = thisPtr->m_data[i] * scalar;
258 }
259 
267 {
268  XsReal a;
269  if (XsQuaternion_empty(quat))
270  {
271  XsVector_destruct(thisPtr);
272  return;
273  }
274 
275  XsVector_assign(thisPtr, 3, &quat->m_data[1]);
276  a = XsVector_cartesianLength(thisPtr);
277  XsVector_multiplyScalar(thisPtr, (a > XsMath_tinyValue) ? (XsMath_two * asin(a) / (a * deltaT)) : (XsMath_two / deltaT), thisPtr);
278 }
279 
287 {
288 #ifdef __ICCARM__
289 #pragma diag_suppress=Pe370
290 #endif
292 }
293 
300 int XsVector_equal(const struct XsVector* a, const struct XsVector* b)
301 {
302  if (a == b)
303  return 1;
304  if (!a || !b)
305  return 0;
306  if (XsVector_empty(a) && XsVector_empty(b))
307  return 1;
308  if (XsVector_empty(a) || XsVector_empty(b))
309  return 0;
310  if (a->m_size != b->m_size)
311  return 0;
312  return memcmp(a->m_data, b->m_data, a->m_size * sizeof(XsReal)) == 0; //lint !e2499 memcmp is a perfect comparison here
313 }
314 
322 int XsVector_compare(const struct XsVector* thisPtr, const struct XsVector* thatPtr, XsReal epsilon)
323 {
324  XsSize i;
325 
326  if (thisPtr == thatPtr)
327  return 1;
328  if (!thisPtr || !thatPtr)
329  return 0;
330  if (XsVector_empty(thisPtr) && XsVector_empty(thatPtr))
331  return 1;
332  if (XsVector_empty(thisPtr) || XsVector_empty(thatPtr))
333  return 0;
334  if (thisPtr->m_size != thatPtr->m_size)
335  return 0;
336 
337  for (i = 0; i < thisPtr->m_size; ++i)
338  if (fabs(thisPtr->m_data[i] - thatPtr->m_data[i]) > epsilon)
339  return 0;
340 
341  return 1;
342 }
343 
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
epsilon
double epsilon
XsMath_one
XSMATHCONST XsReal XsMath_one
1.0
Definition: xsmath.h:158
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
xsquaternion.h
xsvector.h
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
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
xsfloatmath.h
XsMath_tinyValue
XSMATHCONST XsReal XsMath_tinyValue
A really small value.
Definition: xsmath.h:120
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
XsVector_incFreeCount
static int XsVector_incFreeCount(void)
Definition: xsdebugcounters.h:115
xsmalloc.h
data
data
XsQuaternion::XsQuaternion_empty
int XsQuaternion_empty(const XsQuaternion *thisPtr)
Test if this is a null object.
Definition: xsquaternion.c:94
xsMathMalloc
#define xsMathMalloc(n)
Definition: xsmalloc.h:84
xsdebugcounters.h
XSLISTSWAP3
#define XSLISTSWAP3(C, B, S)
Definition: xscopy.h:75
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
XsQuaternion::m_data
XsReal m_data[4]
Stores the quaternion in an array of four elements.
Definition: xsquaternion.h:114
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
xsMathFree
#define xsMathFree(p)
Definition: xsmalloc.h:92
XSDF_Empty
@ XSDF_Empty
The object contains undefined data / should be considered empty. Usually only relevant when XSDF_Fixe...
Definition: xstypedefs.h:112
realSwap
#define realSwap(a, b)
Definition: xsvector.c:76
XsMath_two
XSMATHCONST XsReal XsMath_two
2
Definition: xsmath.h:162
XsVector::m_size
const XsSize m_size
Size of contained data buffer in elements.
Definition: xsvector.h:117
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
XsDataFlags
XsDataFlags
These flags define the behaviour of data contained by Xsens data structures.
Definition: xstypedefs.h:107
length
TF2SIMD_FORCE_INLINE tf2Scalar length(const Quaternion &q)
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
xscopy.h
XsVector::XsVector_setZero
void XsVector_setZero(XsVector *thisPtr)
Sets all elements of the XsVector to 0.
Definition: xsvector.c:226
assert.h
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
XsVector_incAllocCount
static int XsVector_incAllocCount(void)
Definition: xsdebugcounters.h:111


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