| 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 XSVECTOR_H |
| 34 |
#define XSVECTOR_H |
| 35 |
|
| 36 |
#include "xsmath.h" |
| 37 |
#include <stddef.h> |
| 38 |
#include <string.h> // memcpy |
| 39 |
|
| 40 |
struct XsVector; |
| 41 |
struct XsQuaternion; |
| 42 |
|
| 43 |
#ifdef __cplusplus |
| 44 |
#include <vector> |
| 45 |
#include <algorithm> |
| 46 |
extern "C" { |
| 47 |
#endif |
| 48 |
#ifndef __cplusplus |
| 49 |
#define XSVECTOR_INITIALIZER { NULL, 0, 0 } |
| 50 |
typedef struct XsVector XsVector; |
| 51 |
#endif |
| 52 |
|
| 53 |
XSTYPES_DLL_API void XsVector_ref(XsVector* thisPtr, XsSize sz, XsReal* buffer, XsDataFlags flags); |
| 54 |
XSTYPES_DLL_API void XsVector_construct(XsVector* thisPtr, XsSize sz, const XsReal* src); |
| 55 |
XSTYPES_DLL_API void XsVector_assign(XsVector* thisPtr, XsSize sz, const XsReal* src); |
| 56 |
XSTYPES_DLL_API void XsVector_destruct(XsVector* thisPtr); |
| 57 |
XSTYPES_DLL_API void XsVector_copy(XsVector* copy, XsVector const* src); |
| 58 |
XSTYPES_DLL_API XsReal XsVector_dotProduct(const XsVector* a, const XsVector* b); |
| 59 |
XSTYPES_DLL_API XsReal XsVector_cartesianLength(const XsVector* thisPtr); |
| 60 |
XSTYPES_DLL_API void XsVector_normalize(XsVector* thisPtr); |
| 61 |
XSTYPES_DLL_API void XsVector_setZero(XsVector* thisPtr); |
| 62 |
XSTYPES_DLL_API int XsVector_empty(const XsVector* thisPtr); |
| 63 |
XSTYPES_DLL_API void XsVector_multiplyScalar(const XsVector* thisPtr, XsReal scalar, XsVector* dest); |
| 64 |
XSTYPES_DLL_API void XsVector_angularVelocityFromQuaternion(XsVector* thisPtr, XsReal deltaT, const struct XsQuaternion* quat); |
| 65 |
XSTYPES_DLL_API void XsVector_swap(XsVector* a, XsVector* b); |
| 66 |
XSTYPES_DLL_API void XsVector_fill(XsVector* thisPtr, XsReal value); |
| 67 |
XSTYPES_DLL_API int XsVector_equal(const XsVector* thisPtr, const XsVector* thatPtr); |
| 68 |
XSTYPES_DLL_API int XsVector_compare(const XsVector* thisPtr, const XsVector* thatPtr, XsReal epsilon); |
| 69 |
|
| 70 |
#ifdef __cplusplus |
| 71 |
} // extern "C" |
| 72 |
#endif |
| 73 |
#ifndef XSENS_NO_PACK |
| 74 |
#pragma pack(push,1) |
| 75 |
#endif |
| 76 |
struct XsVector { |
| 77 |
XSCPPPROTECTED |
| 78 |
XsReal* const m_data; //!< \protected Points to contained data buffer |
| 79 |
const XsSize m_size; //!< \protected Size of contained data buffer in elements |
| 80 |
const XsSize m_flags; //!< \protected Flags for data management |
| 81 |
|
| 82 |
#ifdef __cplusplus |
| 83 |
//! \brief Return the data management flags of the vector. |
| 84 |
inline XsSize flags() const { return m_flags; } |
| 85 |
public: |
| 86 |
//! \brief Initialize a vector, empty or using the data in the supplied \a sz and \a src |
| 87 |
inline explicit XsVector(XsSize sz = 0, const XsReal* src = 0) |
| 88 |
: m_data(0) |
| 89 |
, m_size(0) |
| 90 |
, m_flags(0) |
| 91 |
{ |
| 92 |
if (sz) |
| 93 |
XsVector_construct(this, sz, src); |
| 94 |
} |
| 95 |
|
| 96 |
//! \brief Initialize a vector using the supplied \a other vector |
| 97 |
inline XsVector(const XsVector& other) |
| 98 |
: m_data(0) |
| 99 |
, m_size(0) |
| 100 |
, m_flags(0) |
| 101 |
{ |
| 102 |
*this = other; |
| 103 |
} |
| 104 |
|
| 105 |
//! \brief Initialize a vector that references the supplied data |
| 106 |
inline explicit XsVector(XsReal* ref, XsSize sz, XsDataFlags flags_ = XSDF_None) |
| 107 |
: m_data(ref) |
| 108 |
, m_size(sz) |
| 109 |
, m_flags((XsSize) flags_) |
| 110 |
{ |
| 111 |
} |
| 112 |
|
| 113 |
//! \brief Initialize a vector that references the supplied data |
| 114 |
inline explicit XsVector(const XsVector& other, XsReal* ref, XsSize sz, XsDataFlags flags_ = XSDF_None) |
| 115 |
: m_data(ref) |
| 116 |
, m_size(sz) |
| 117 |
, m_flags((XsSize) flags_) |
| 118 |
{ |
| 119 |
XsVector_copy(this, &other); |
| 120 |
} |
| 121 |
|
| 122 |
//! \copydoc XsVector_angularVelocityFromQuaternion |
| 123 |
inline explicit XsVector(const XsQuaternion& quat, XsReal deltaT) |
| 124 |
: m_data(0) |
| 125 |
, m_size(0) |
| 126 |
, m_flags(0) |
| 127 |
{ |
| 128 |
XsVector_angularVelocityFromQuaternion(this, deltaT, &quat); |
| 129 |
} |
| 130 |
|
| 131 |
//! \brief Assignment operator. Copies from \a other into this |
| 132 |
inline XsVector& operator=(const XsVector& other) |
| 133 |
{ |
| 134 |
XsVector_copy(this, &other); |
| 135 |
return *this; |
| 136 |
} |
| 137 |
|
| 138 |
//! \copydoc XsVector_destruct |
| 139 |
inline ~XsVector() |
| 140 |
{ |
| 141 |
XsVector_destruct(this); |
| 142 |
} |
| 143 |
|
| 144 |
//! \copydoc XsVector_assign |
| 145 |
inline void assign(XsSize sz, const XsReal* src) |
| 146 |
{ |
| 147 |
XsVector_assign(this, sz, src); |
| 148 |
} |
| 149 |
|
| 150 |
/*! \brief Sets the size of the XsVector to \a sz items |
| 151 |
\param sz The desired size of the vector |
| 152 |
\sa XsVector_assign |
| 153 |
*/ |
| 154 |
inline void setSize(XsSize sz) |
| 155 |
{ |
| 156 |
XsVector_assign(this, sz, 0); |
| 157 |
} |
| 158 |
|
| 159 |
//! \brief Returns the number of elements in the vector |
| 160 |
inline XsSize size() const |
| 161 |
{ |
| 162 |
return m_size; |
| 163 |
} |
| 164 |
|
| 165 |
//! \brief Return a const pointer to the data |
| 166 |
inline const XsReal* data() const |
| 167 |
{ |
| 168 |
return m_data; |
| 169 |
} |
| 170 |
|
| 171 |
//! \brief Multiply the vector by \a scalar and return the result |
| 172 |
inline XsVector operator * (XsReal scalar) const |
| 173 |
{ |
| 174 |
XsVector v(m_size); |
| 175 |
for (XsSize i = 0; i < m_size; ++i) |
| 176 |
v.m_data[i] = m_data[i] * scalar; |
| 177 |
return v; |
| 178 |
} |
| 179 |
|
| 180 |
//! \brief Multiply the vector by \a scalar and store the result in this vector |
| 181 |
inline void operator *=(XsReal scalar) |
| 182 |
{ |
| 183 |
for (XsSize i = 0; i < m_size; ++i) |
| 184 |
m_data[i] *= scalar; |
| 185 |
} |
| 186 |
|
| 187 |
//! \brief Returns a reference to the \a index'th item in the vector |
| 188 |
inline XsReal& at(XsSize index) |
| 189 |
{ |
| 190 |
assert(index < m_size); |
| 191 |
return m_data[index]; |
| 192 |
} |
| 193 |
|
| 194 |
//! \brief Returns a const reference to the \a index'th item in the vector |
| 195 |
inline const XsReal& at(XsSize index) const |
| 196 |
{ |
| 197 |
assert(index < m_size); |
| 198 |
return m_data[index]; |
| 199 |
} |
| 200 |
|
| 201 |
//! \brief Returns the \a index'th item in the vector |
| 202 |
inline XsReal value(XsSize index) const |
| 203 |
{ |
| 204 |
assert(index < m_size); |
| 205 |
return m_data[index]; |
| 206 |
} |
| 207 |
|
| 208 |
//! \brief Sets the \a index'th item in the vector |
| 209 |
inline void setValue(XsSize index, XsReal val) |
| 210 |
{ |
| 211 |
assert(index < m_size); |
| 212 |
m_data[index] = val; |
| 213 |
} |
| 214 |
|
| 215 |
//! \brief Returns the \a index'th item in the vector |
| 216 |
inline XsReal operator[](XsSize index) const |
| 217 |
{ |
| 218 |
assert(index < m_size); |
| 219 |
return m_data[index]; |
| 220 |
} |
| 221 |
|
| 222 |
//! \brief Returns a reference the \a index'th item in the vector |
| 223 |
inline XsReal& operator[](XsSize index) |
| 224 |
{ |
| 225 |
assert(index < m_size); |
| 226 |
return m_data[index]; |
| 227 |
} |
| 228 |
|
| 229 |
//! \brief \copybrief XsVector_dotProduct |
| 230 |
inline XsReal dotProduct(const XsVector &v) const |
| 231 |
{ |
| 232 |
return XsVector_dotProduct(this, &v); |
| 233 |
} |
| 234 |
|
| 235 |
//! \copydoc XsVector_cartesianLength |
| 236 |
inline XsReal cartesianLength() const |
| 237 |
{ |
| 238 |
return XsVector_cartesianLength(this); |
| 239 |
} |
| 240 |
|
| 241 |
//! \copydoc XsVector_normalize |
| 242 |
inline void normalize() |
| 243 |
{ |
| 244 |
XsVector_normalize(this); |
| 245 |
} |
| 246 |
|
| 247 |
//! \brief \copybrief XsVector_setZero |
| 248 |
inline void setZero() |
| 249 |
{ |
| 250 |
return XsVector_setZero(this); |
| 251 |
} |
| 252 |
|
| 253 |
//! \brief \copybrief XsVector_empty |
| 254 |
inline bool empty() const |
| 255 |
{ |
| 256 |
return 0 != XsVector_empty(this); |
| 257 |
} |
| 258 |
|
| 259 |
//! \copydoc XsVector_angularVelocityFromQuaternion |
| 260 |
inline XsVector& angularVelocityFromQuaternion(const XsQuaternion& quat, XsReal deltaT) |
| 261 |
{ |
| 262 |
XsVector_angularVelocityFromQuaternion(this, deltaT, &quat); |
| 263 |
return *this; |
| 264 |
} |
| 265 |
|
| 266 |
//! \brief Return \e this - \a sub |
| 267 |
XsVector operator-(const XsVector& sub) const |
| 268 |
{ |
| 269 |
assert(m_size == sub.m_size); |
| 270 |
XsVector tmp(m_size); |
| 271 |
for (XsSize i = 0; i < m_size; ++i) |
| 272 |
tmp[i] = m_data[i] - sub.m_data[i]; |
| 273 |
return tmp; |
| 274 |
} |
| 275 |
|
| 276 |
//! \brief Return \e this + \a sub |
| 277 |
XsVector operator+(const XsVector& sub) const |
| 278 |
{ |
| 279 |
assert(m_size == sub.m_size); |
| 280 |
XsVector tmp(m_size); |
| 281 |
for (XsSize i = 0; i < m_size; ++i) |
| 282 |
tmp[i] = m_data[i] + sub.m_data[i]; |
| 283 |
return tmp; |
| 284 |
} |
| 285 |
|
| 286 |
//! \brief Return \e this - \a sub after updating this with the new values |
| 287 |
XsVector operator-=(const XsVector& sub) |
| 288 |
{ |
| 289 |
assert(m_size == sub.m_size); |
| 290 |
for (XsSize i = 0; i < m_size; ++i) |
| 291 |
m_data[i] -= sub.m_data[i]; |
| 292 |
return *this; |
| 293 |
} |
| 294 |
|
| 295 |
//! \brief Return \e this + \a sub after updating this with the new values |
| 296 |
XsVector operator+=(const XsVector& sub) |
| 297 |
{ |
| 298 |
assert(m_size == sub.m_size); |
| 299 |
for (XsSize i = 0; i < m_size; ++i) |
| 300 |
m_data[i] += sub.m_data[i]; |
| 301 |
return *this; |
| 302 |
} |
| 303 |
|
| 304 |
//! \brief Return true when the values in this vector are exactly (to the last bit) equal to \a other |
| 305 |
bool operator==(const XsVector& other) const |
| 306 |
{ |
| 307 |
return 0 != XsVector_equal(this, &other); |
| 308 |
} |
| 309 |
|
| 310 |
/*! \brief Return true when the values in this vector are equal within \a epsilon |
| 311 |
\param other the vector to compare with |
| 312 |
\param epsilon the maximum difference between individual values |
| 313 |
\returns true if the vectors are equal within \a epsilon |
| 314 |
*/ |
| 315 |
bool isEqual(const XsVector &other, XsReal epsilon) const |
| 316 |
{ |
| 317 |
return 0 != XsVector_compare(this, &other, epsilon); |
| 318 |
} |
| 319 |
|
| 320 |
#ifndef XSENS_NO_STL |
| 321 |
//! \brief Returns the XsVector as a std::vector of XsReal |
| 322 |
inline std::vector<XsReal> toVector() const |
| 323 |
{ |
| 324 |
std::vector<XsReal> tmp(m_size); |
| 325 |
if (m_size) |
| 326 |
memcpy(&tmp[0], m_data, m_size * sizeof(XsReal)); |
| 327 |
return tmp; |
| 328 |
} |
| 329 |
#endif |
| 330 |
|
| 331 |
/*! \brief Fill the vector with zeroes */ |
| 332 |
inline void zero() |
| 333 |
{ |
| 334 |
for (XsSize i = 0; i < m_size; ++i) |
| 335 |
m_data[i] = XsMath_zero; |
| 336 |
} |
| 337 |
|
| 338 |
/*! \brief Fill the vector with \a val */ |
| 339 |
inline void fill(XsReal val) |
| 340 |
{ |
| 341 |
for (XsSize i = 0; i < m_size; ++i) |
| 342 |
m_data[i] = val; |
| 343 |
} |
| 344 |
|
| 345 |
/*! \brief Swap the contents of \a b with this |
| 346 |
\details This function swaps the internal buffers so no actual data is moved around. For unmanaged |
| 347 |
data an elementwise swap is done, but only if the vectors are the same size. |
| 348 |
\param b Object whose contents will be swapped with this |
| 349 |
*/ |
| 350 |
inline void swap(XsVector& b) |
| 351 |
{ |
| 352 |
XsVector_swap(this, &b); |
| 353 |
} |
| 354 |
|
| 355 |
/*! \brief Append \a other to this |
| 356 |
\details Append the vector in \a other to the end of this vector |
| 357 |
\param other The vector to append |
| 358 |
*/ |
| 359 |
inline void append(XsVector const& other) |
| 360 |
{ |
| 361 |
XsVector tmp(size() + other.size()); |
| 362 |
for (XsSize i = 0; i < size(); ++i) |
| 363 |
tmp[i] = (*this)[i]; |
| 364 |
for (XsSize i = 0; i < other.size(); ++i) |
| 365 |
tmp[i+size()] = other[i]; |
| 366 |
swap(tmp); |
| 367 |
} |
| 368 |
|
| 369 |
/*! \brief Clear the vector, making it size 0 */ |
| 370 |
inline void clear() |
| 371 |
{ |
| 372 |
setSize(0); |
| 373 |
} |
| 374 |
|
| 375 |
#ifndef XSENS_NO_STL |
| 376 |
/*! \brief Reverse the values in the vector */ |
| 377 |
inline void reverse() |
| 378 |
{ |
| 379 |
XsSize sz = size(); |
| 380 |
XsSize half = sz >> 1; |
| 381 |
--sz; |
| 382 |
for (XsSize i = 0; i < half; ++i) |
| 383 |
std::swap(operator[](i), operator[](sz-i)); |
| 384 |
} |
| 385 |
#endif |
| 386 |
#endif |
| 387 |
}; |
| 388 |
#ifndef XSENS_NO_PACK |
| 389 |
#pragma pack(pop) |
| 390 |
#endif |
| 391 |
|
| 392 |
#ifdef __cplusplus |
| 393 |
//! \brief Multiplies all values in the vector \a v by \a scalar |
| 394 |
inline XsVector operator *(XsReal scalar, const XsVector &v) |
| 395 |
{ |
| 396 |
return v*scalar; |
| 397 |
} |
| 398 |
#endif |
| 399 |
|
| 400 |
#endif |