MRPT  1.9.9
xsvector.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #ifndef XSVECTOR_H
10 #define XSVECTOR_H
11 
12 #include <stddef.h>
13 #include <string.h> // memcpy
14 #include "xsmath.h"
15 
16 struct XsVector;
17 struct XsQuaternion;
18 
19 #ifdef __cplusplus
20 #include <algorithm>
21 #include <vector>
22 extern "C"
23 {
24 #else
25 #define XSVECTOR_INITIALIZER \
26  { \
27  nullptr, 0, 0 \
28  }
29 typedef struct XsVector XsVector;
30 #endif
31 
33  XsVector* thisPtr, XsSize sz, XsReal* buffer, XsDataFlags flags);
35  XsVector* thisPtr, XsSize sz, const XsReal* src);
37  XsVector* thisPtr, XsSize sz, const XsReal* src);
41  XsVector_dotProduct(const XsVector* a, const XsVector* b);
44  XSTYPES_DLL_API int XsVector_empty(const XsVector* thisPtr);
46  const XsVector* thisPtr, XsReal scalar, XsVector* dest);
48  XsVector* thisPtr, XsReal deltaT, const struct XsQuaternion* quat);
52  const XsVector* thisPtr, const XsVector* thatPtr);
53 
54 #ifdef __cplusplus
55 } // extern "C"
56 #endif
57 #ifndef XSENS_NO_PACK
58 #pragma pack(push, 1)
59 #endif
60 struct XsVector
61 {
63  /** \protected Points to contained data buffer */
64  XsReal* const m_data;
65  /** \protected Size of contained data buffer in elements */
66  const XsSize m_size;
67  /** \protected Flags for data management */
68  const int m_flags;
69 
70 #ifdef __cplusplus
71  //! \brief Return the data management flags of the vector.
72  inline int flags() { return m_flags; }
73 
74  public:
75  //! \brief Initialize a vector, empty or using the data in the supplied \a
76  //! sz and \a src
77  inline explicit XsVector(XsSize sz = 0, const XsReal* src = 0)
78  : m_data(0), m_size(0), m_flags(0)
79  {
80  if (sz) XsVector_construct(this, sz, src);
81  }
82 
83  //! \brief Initialize a vector using the supplied \a other vector
84  inline XsVector(const XsVector& other) : m_data(0), m_size(0), m_flags(0)
85  {
86  *this = other;
87  }
88 
89  //! \brief Initialize a vector that references the supplied data
90  inline explicit XsVector(
91  XsReal* ref, XsSize sz, XsDataFlags flags = XSDF_None)
92  : m_data(ref), m_size(sz), m_flags(flags)
93  {
94  }
95 
96  //! \brief Initialize a vector that references the supplied data
97  inline explicit XsVector(
98  const XsVector& other, XsReal* ref, XsSize sz,
99  XsDataFlags flags = XSDF_None)
100  : m_data(ref), m_size(sz), m_flags(flags)
101  {
102  XsVector_copy(this, &other);
103  }
104 
105  //! \copydoc XsVector_angularVelocityFromQuaternion
106  inline explicit XsVector(const XsQuaternion& quat, XsReal deltaT)
107  : m_data(0), m_size(0), m_flags(0)
108  {
109  XsVector_angularVelocityFromQuaternion(this, deltaT, &quat);
110  }
111 
112  //! \brief Assignment operator. Copies from \a other into this
113  inline XsVector& operator=(const XsVector& other)
114  {
115  XsVector_copy(this, &other);
116  return *this;
117  }
118 
119  //! \copydoc XsVector_destruct
120  inline ~XsVector() { XsVector_destruct(this); }
121  //! \copydoc XsVector_assign
122  inline void assign(XsSize sz, const XsReal* src)
123  {
124  XsVector_assign(this, sz, src);
125  }
126 
127  /*! \brief Sets the size of the XsVector to \a sz items
128  \param sz The desired size of the vector
129  \sa XsVector_assign
130  */
131  inline void setSize(XsSize sz) { XsVector_assign(this, sz, 0); }
132  //! \brief Returns the number of elements in the vector
133  inline XsSize size() const { return m_size; }
134  //! \brief Return a const pointer to the data
135  inline const XsReal* data() const { return m_data; }
136  //! \brief Multiply the vector by \a scalar and return the result
137  inline XsVector operator*(XsReal scalar) const
138  {
139  XsVector v(m_size);
140  for (XsSize i = 0; i < m_size; ++i) v.m_data[i] = m_data[i] * scalar;
141  return v;
142  }
143 
144  //! \brief Multiply the vector by \a scalar and store the result in this
145  //! vector
146  inline void operator*=(XsReal scalar)
147  {
148  for (XsSize i = 0; i < m_size; ++i) m_data[i] *= scalar;
149  }
150 
151  //! \brief Returns a reference to the \a index'th item in the vector
152  inline XsReal& at(XsSize index)
153  {
154  assert(index < m_size);
155  return m_data[index];
156  }
157 
158  //! \brief Returns a const reference to the \a index'th item in the vector
159  inline const XsReal& at(XsSize index) const
160  {
161  assert(index < m_size);
162  return m_data[index];
163  }
164 
165  //! \brief Returns the \a index'th item in the vector
166  inline XsReal value(XsSize index) const
167  {
168  assert(index < m_size);
169  return m_data[index];
170  }
171 
172  //! \brief Sets the \a index'th item in the vector
173  inline void setValue(XsSize index, XsReal val)
174  {
175  assert(index < m_size);
176  m_data[index] = val;
177  }
178 
179  //! \brief Returns the \a index'th item in the vector
180  inline XsReal operator[](XsSize index) const
181  {
182  assert(index < m_size);
183  return m_data[index];
184  }
185 
186  //! \brief Returns a reference the \a index'th item in the vector
187  inline XsReal& operator[](XsSize index)
188  {
189  assert(index < m_size);
190  return m_data[index];
191  }
192 
193  //! \brief \copybrief XsVector_dotProduct
194  inline XsReal dotProduct(const XsVector& v) const
195  {
196  return XsVector_dotProduct(this, &v);
197  }
198 
199  //! \copydoc XsVector_cartesianLength
200  inline XsReal cartesianLength() const
201  {
202  return XsVector_cartesianLength(this);
203  }
204 
205  //! \brief \copybrief XsVector_setZero
206  inline void setZero() { return XsVector_setZero(this); }
207  //! \brief \copybrief XsVector_empty
208  inline bool empty() const { return 0 != XsVector_empty(this); }
209  //! \copydoc XsVector_angularVelocityFromQuaternion
210  inline XsVector& angularVelocityFromQuaternion(
211  const XsQuaternion& quat, XsReal deltaT)
212  {
213  XsVector_angularVelocityFromQuaternion(this, deltaT, &quat);
214  return *this;
215  }
216 
217  //! \brief Return \e this - \a sub
218  XsVector operator-(const XsVector& sub) const
219  {
220  assert(m_size == sub.m_size);
221  XsVector tmp(m_size);
222  for (XsSize i = 0; i < m_size; ++i) tmp[i] = m_data[i] - sub.m_data[i];
223  return tmp;
224  }
225 
226  //! \brief Return \e this + \a sub
227  XsVector operator+(const XsVector& sub) const
228  {
229  assert(m_size == sub.m_size);
230  XsVector tmp(m_size);
231  for (XsSize i = 0; i < m_size; ++i) tmp[i] = m_data[i] + sub.m_data[i];
232  return tmp;
233  }
234 
235  //! \brief Return true when the values in this vector are exactly (to the
236  //! last bit) equal to \a other
237  bool operator==(const XsVector& other) const
238  {
239  return 0 != XsVector_equal(this, &other);
240  }
241 
242 #ifndef XSENS_NO_STL
243  //! \brief Returns the XsVector as a std::vector of XsReal
244  inline std::vector<XsReal> toVector() const
245  {
246  std::vector<XsReal> tmp(m_size);
247  if (m_size) memcpy(&tmp[0], m_data, m_size * sizeof(XsReal));
248  return tmp;
249  }
250 #endif
251 
252  /*! \brief Fill the vector with zeroes */
253  inline void zero()
254  {
255  for (XsSize i = 0; i < m_size; ++i) m_data[i] = XsMath_zero;
256  }
257 
258  /*! \brief Fill the vector with \a value */
259  inline void fill(XsReal value)
260  {
261  for (XsSize i = 0; i < m_size; ++i) m_data[i] = value;
262  }
263 
264  /*! \brief Swap the contents of \a b with this
265  \details This function swaps the internal buffers so no actual data is
266  moved around. For unmanaged
267  data an elementwise swap is done, but only if the vectors are the same
268  size.
269  \param b Object whose contents will be swapped with this
270  */
271  inline void swap(XsVector& b) { XsVector_swap(this, &b); }
272 #endif
273 };
274 #ifndef XSENS_NO_PACK
275 #pragma pack(pop)
276 #endif
277 
278 #ifdef __cplusplus
279 //! \brief Multiplies all values in the vector \a v by \a scalar
280 inline XsVector operator*(XsReal scalar, const XsVector& v)
281 {
282  return v * scalar;
283 }
284 #endif
285 
286 #endif // file guard
XSTYPES_DLL_API XsReal XsVector_cartesianLength(const XsVector *thisPtr)
XSTYPES_DLL_API void XsVector_angularVelocityFromQuaternion(XsVector *thisPtr, XsReal deltaT, const struct XsQuaternion *quat)
XSTYPES_DLL_API const XsReal XsMath_zero
GLuint buffer
Definition: glext.h:3928
GLenum GLint ref
Definition: glext.h:4062
XSTYPES_DLL_API int XsVector_empty(const XsVector *thisPtr)
size_t XsSize
XsSize must be unsigned number!
Definition: xstypedefs.h:19
GLuint src
Definition: glext.h:7397
No flag set.
Definition: xstypedefs.h:44
struct XsVector XsVector
Definition: xsvector.h:29
TColor operator+(const TColor &first, const TColor &second)
Pairwise addition of their corresponding RGBA members.
Definition: TColor.cpp:19
XSTYPES_DLL_API void XsVector_swap(XsVector *a, XsVector *b)
VALUE & operator[](const KEY &key)
Write/read via [i] operator, that creates an element if it didn&#39;t exist already.
Definition: ts_hash_map.h:193
XSTYPES_DLL_API int XsVector_equal(const XsVector *thisPtr, const XsVector *thatPtr)
XSTYPES_DLL_API void XsVector_destruct(XsVector *thisPtr)
GLuint index
Definition: glext.h:4068
XSTYPES_DLL_API void XsVector_assign(XsVector *thisPtr, XsSize sz, const XsReal *src)
int val
Definition: mrpt_jpeglib.h:957
GLubyte GLubyte b
Definition: glext.h:6372
XSTYPES_DLL_API void XsVector_construct(XsVector *thisPtr, XsSize sz, const XsReal *src)
XSTYPES_DLL_API void XsVector_ref(XsVector *thisPtr, XsSize sz, XsReal *buffer, XsDataFlags flags)
#define XSCPPPROTECTED
Definition: xstypesconfig.h:57
bool empty() const
Definition: ts_hash_map.h:190
bool operator==(const mrpt::img::TCamera &a, const mrpt::img::TCamera &b)
Definition: TCamera.cpp:202
#define XSTYPES_DLL_API
Definition: xstypesconfig.h:9
const GLdouble * v
Definition: glext.h:3684
const int m_flags
Flags for data management.
Definition: xsvector.h:68
const XsSize m_size
Size of contained data buffer in elements.
Definition: xsvector.h:66
CONTAINER1::Scalar dotProduct(const CONTAINER1 &v1, const CONTAINER1 &v2)
v1*v2: The dot product of two containers (vectors/arrays/matrices)
double XsReal
Defines the floating point type used by the Xsens libraries.
Definition: xstypedefs.h:17
TColor operator-(const TColor &first, const TColor &second)
Pairwise substraction of their corresponding RGBA members.
Definition: TColor.cpp:30
XSTYPES_DLL_API void XsVector_copy(XsVector *copy, XsVector const *src)
GLsizei const GLfloat * value
Definition: glext.h:4134
GLsizeiptr size
Definition: glext.h:3934
XSTYPES_DLL_API void XsVector_setZero(XsVector *thisPtr)
XSTYPES_DLL_API XsReal XsVector_dotProduct(const XsVector *a, const XsVector *b)
XSCPPPROTECTED XsReal *const m_data
Points to contained data buffer.
Definition: xsvector.h:64
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3550
GLubyte GLubyte GLubyte a
Definition: glext.h:6372
std::vector< T1 > & operator*=(std::vector< T1 > &a, const std::vector< T2 > &b)
a*=b (element-wise multiplication)
Definition: ops_vectors.h:31
XSTYPES_DLL_API void XsVector_fill(XsVector *thisPtr, XsReal value)
XsDataFlags
These flags define the behaviour of data contained by Xsens data structures.
Definition: xstypedefs.h:41
std::vector< T1 > operator*(const std::vector< T1 > &a, const std::vector< T2 > &b)
a*b (element-wise multiplication)
Definition: ops_vectors.h:50
void memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) noexcept
An OS and compiler independent version of "memcpy".
Definition: os.cpp:358
XSTYPES_DLL_API void XsVector_multiplyScalar(const XsVector *thisPtr, XsReal scalar, XsVector *dest)



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019