Debian Codegraph @

3rdparty/xspublic/xstypes/xsvector.c

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 #include "xsvector.h"
34 #include <stdlib.h>
35 #include <math.h>
36 #include "xsdebugcounters.h"
37 #include <string.h>
38 #include "xsmalloc.h"
39 #include "xsquaternion.h"
40 #include "xscopy.h"
41 #include <assert.h>
42 #include "xsfloatmath.h"
43
44 #define realSwap(a,b) { XsReal t = *a; *a = *b; *b = t; }
45
46 /*! \class XsVector
47 \brief A class that represents a vector of real numbers
48 */
49
50 /*! \addtogroup cinterface C Interface
51 @{
52 */
53
54 //! \relates XsVector \brief Initialize the %XsVector to refer to the supplied buffer
55 void XsVector_ref(XsVector* thisPtr, XsSize sz, XsReal* buffer, XsDataFlags flags)
56 {
57 assert(sz==0 || buffer != 0);
58 *((XsReal**) &thisPtr->m_data) = buffer;
59 *((XsSize*) &thisPtr->m_size) = sz;
60 *((XsSize*) &thisPtr->m_flags) = flags;
61 }
62
63 //! \relates XsVector \brief Initialize the %XsVector using \a sz number of items from \a src
64 void XsVector_construct(XsVector* thisPtr, XsSize sz, const XsReal* src)
65 {
66 if (sz)
67 {
68 // init to size
69 XsReal* data = (XsReal*) xsMathMalloc(sz*sizeof(XsReal));
70 assert(data);
71 *((XsReal**) &thisPtr->m_data) = data;
72 XsVector_incAllocCount();
73 }
74 else
75 *((XsReal**) &thisPtr->m_data) = 0;
76 *((XsSize*) &thisPtr->m_flags) = XSDF_Managed;
77 *((XsSize*) &thisPtr->m_size) = sz;
78 if (src && sz)
79 memcpy(thisPtr->m_data, src, sz*sizeof(XsReal));
80 }
81
82 /*! \relates XsVector
83 \brief Initialize the XsVector using \a sz number of items from \a src
84 \param sz The desired size of the vector
85 \param src 0 or a pointer to a buffer containing \a sz items to copy into the %XsVector
86 */
87 void XsVector_assign(XsVector* thisPtr, XsSize sz, const XsReal* src)
88 {
89 if (thisPtr->m_flags == XSDF_FixedSize)
90 {
91 if (sz == 0)
92 {
93 *((XsSize*) &thisPtr->m_flags) |= XSDF_Empty;
94 return;
95 }
96 assert(sz == thisPtr->m_size);
97 *((XsSize*) &thisPtr->m_flags) &= ~XSDF_Empty;
98 }
99
100 if (sz > thisPtr->m_size || sz == 0)
101 {
102 XsVector_destruct(thisPtr);
103 if (sz)
104 {
105 // init to size
106 XsReal* data = (XsReal*) xsMathMalloc(sz*sizeof(XsReal));
107 assert(data);
108 *((XsReal**) &thisPtr->m_data) = data;
109 *((XsSize*) &thisPtr->m_flags) = XSDF_Managed;
110 XsVector_incAllocCount();
111 }
112 }
113 *((XsSize*) &thisPtr->m_size) = sz;
114 if (src && sz)
115 memcpy(thisPtr->m_data, src, sz*sizeof(XsReal));
116 }
117
118 //! \relates XsVector \brief Release and clear the contents of the vector
119 void XsVector_destruct(XsVector* thisPtr)
120 {
121 if (thisPtr->m_data && (thisPtr->m_flags & XSDF_Managed))
122 {
123 // clear contents
124 xsMathFree((void*) thisPtr->m_data);
125 XsVector_incFreeCount();
126 }
127 // init to 0
128 if (!(thisPtr->m_flags & XSDF_FixedSize))
129 {
130 *((XsReal**) &thisPtr->m_data) = 0;
131 *((XsSize*) &thisPtr->m_size) = 0;
132 *((XsSize*) &thisPtr->m_flags) = 0;
133 }
134 else
135 *((XsSize*) &thisPtr->m_flags) |= XSDF_Empty;
136 }
137
138 //! \relates XsVector \brief Copy the contents of the %XsVector to \a copy
139 void XsVector_copy(XsVector* copy, XsVector const* src)
140 {
141 if (copy == src)
142 return;
143
144 if (src->m_flags & XSDF_Empty)
145 XsVector_destruct(copy);
146 else
147 XsVector_assign(copy, src->m_size, src->m_data);
148 }
149
150 /*! \relates XsVector
151 \brief Compute and return the dot product of XsVectors \a a and \a b
152 */
153 XsReal XsVector_dotProduct(const XsVector* a, const XsVector* b)
154 {
155 XsSize i;
156 XsReal r = XsMath_zero;
157 assert(a->m_size == b->m_size);
158
159 for (i = a->m_size; i--; )
160 r += a->m_data[i]*b->m_data[i];
161 return r;
162 }
163
164 /*! \relates XsVector
165 \brief Compute and return the cartesian length
166 \returns The cartesian length (square root of the dot product) of the vector
167 */
168 XsReal XsVector_cartesianLength(const XsVector* thisPtr)
169 {
170 return sqrt(XsVector_dotProduct(thisPtr, thisPtr));
171 }
172
173 /*! \relates XsVector
174 \brief Normalize the vector
175 */
176 void XsVector_normalize(XsVector* thisPtr)
177 {
178 XsReal length = XsVector_cartesianLength(thisPtr);
179
180 if (length < XsMath_tinyValue)
181 {
182 XsVector_setZero(thisPtr);
183 return;
184 }
185
186 XsVector_multiplyScalar(thisPtr, XsMath_one / length, thisPtr);
187 }
188
189 //! \relates XsVector \brief Sets all elements of the %XsVector to 0
190 void XsVector_setZero(XsVector* thisPtr)
191 {
192 #if XSREAL_ALLOWS_MEMCPY
193 memset(thisPtr->m_data, 0, sizeof(XsReal)*thisPtr->m_size);
194 #else
195 XsSize i;
196 for (i = 0; i < thisPtr->m_size; ++i)
197 thisPtr->m_data[i] = XsMath_zero;
198 #endif
199 }
200
201 //! \relates XsVector \brief Sets all elements of the %XsVector to \a value
202 void XsVector_fill(struct XsVector* thisPtr, XsReal value)
203 {
204 XsSize i;
205 for (i = 0; i < thisPtr->m_size; ++i)
206 thisPtr->m_data[i] = value;
207 }
208
209 //! \relates XsVector \brief Returns a non-zero value if the XsVector does not contain any values
210 int XsVector_empty(const XsVector* thisPtr)
211 {
212 return (thisPtr->m_size == 0) || (thisPtr->m_flags & XSDF_Empty);
213 }
214
215 //! \relates XsVector \brief Multiplies all values in this XsVector by \a scalar and puts the result in XsVector \a dest
216 void XsVector_multiplyScalar(const XsVector* thisPtr, XsReal scalar, XsVector* dest)
217 {
218 XsSize i;
219 XsVector_assign(dest, thisPtr->m_size, 0);
220 for (i = 0; i < thisPtr->m_size; ++i)
221 dest->m_data[i] = thisPtr->m_data[i] * scalar;
222 }
223
224 /*! \relates XsVector
225 \brief Get an effective angular velocity from the quaternion, which must represent a delta angle.
226 \param deltaT The length of the time interval over which \a quat was integrated in seconds
227 \param quat The orientation increment to convert to an angular velocity
228 \returns A vector containing the effective angular velocity in radians around each axis.
229 */
230 void XsVector_angularVelocityFromQuaternion(XsVector* thisPtr, XsReal deltaT, const XsQuaternion* quat)
231 {
232 XsReal a;
233 if (XsQuaternion_empty(quat))
234 {
235 XsVector_destruct(thisPtr);
236 return;
237 }
238
239 XsVector_assign(thisPtr, 3, &quat->m_data[1]);
240 a = XsVector_cartesianLength(thisPtr);
241 XsVector_multiplyScalar(thisPtr, (a > XsMath_tinyValue) ? (XsMath_two*asin(a)/(a*deltaT)) : (XsMath_two/deltaT), thisPtr);
242 }
243
244 /*! \relates XsVector \brief Swap the contents of \a a and \a b
245 \details This function swaps the internal buffers so no actual data is moved around. For unmanaged
246 data an elementwise swap is done, but only if the vectors are the same size.
247 \param a Object whose contents will be placed in \a b
248 \param b Object whose contents will be placed in \a a
249 */
250 void XsVector_swap(XsVector* a, XsVector* b)
251 {
252 #ifdef __ICCARM__
253 #pragma diag_suppress=Pe370
254 #endif
255 XSLISTSWAP3(XsReal, XsVector, realSwap)
256 }
257
258 /*! \relates XsVector
259 \brief Returns non-zero when the two vectors are identical
260 \param a Vector to compare against \a b
261 \param b Vector to compare against \a a
262 \returns non-zero when the vectors are identical
263 */
264 int XsVector_equal(const struct XsVector* a, const struct XsVector* b)
265 {
266 if (a == b)
267 return 1;
268 if (!a || !b)
269 return 0;
270 if (XsVector_empty(a) && XsVector_empty(b))
271 return 1;
272 if (a->m_size != b->m_size)
273 return 0;
274 return memcmp(a->m_data, b->m_data, a->m_size*sizeof(XsReal)) == 0;
275 }
276
277 /*! \relates XsVector
278 \brief Returns non-zero if the two vectors are equal within \a epsilon
279
280 \param thatPtr the vector to compare with
281 \param epsilon the maximum difference between values
282 \returns non-zero when the vectors are equal within \a epsilon
283 */
284 int XsVector_compare(const struct XsVector* thisPtr, const struct XsVector* thatPtr, XsReal epsilon)
285 {
286 XsSize i;
287
288 if (thisPtr == thatPtr)
289 return 1;
290 if (!thisPtr || !thatPtr)
291 return 0;
292 if (XsVector_empty(thisPtr) && XsVector_empty(thatPtr))
293 return 1;
294 if (thisPtr->m_size != thatPtr->m_size)
295 return 0;
296
297 for (i = 0; i < thisPtr->m_size; i++)
298 {
299 if (fabs(thisPtr->m_data[i] - thatPtr->m_data[i]) > epsilon)
300 return 0;
301 }
302 return 1;
303 }
304
305 /*! @} */