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