Main MRPT website > C++ reference for MRPT 1.5.6
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-2017, 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 { NULL, 0, 0 }
25 typedef struct XsVector XsVector;
26 #endif
27 
29 XSTYPES_DLL_API void XsVector_construct(XsVector* thisPtr, XsSize sz, const XsReal* src);
30 XSTYPES_DLL_API void XsVector_assign(XsVector* thisPtr, XsSize sz, const XsReal* src);
36 XSTYPES_DLL_API int XsVector_empty(const XsVector* thisPtr);
37 XSTYPES_DLL_API void XsVector_multiplyScalar(const XsVector* thisPtr, XsReal scalar, XsVector* dest);
38 XSTYPES_DLL_API void XsVector_angularVelocityFromQuaternion(XsVector* thisPtr, XsReal deltaT, const struct XsQuaternion* quat);
41 XSTYPES_DLL_API int XsVector_equal(const XsVector* thisPtr, const XsVector* thatPtr);
42 
43 #ifdef __cplusplus
44 } // extern "C"
45 #endif
46 #ifndef XSENS_NO_PACK
47 #pragma pack(push,1)
48 #endif
49 struct XsVector {
51  XsReal* const m_data; //!< \protected Points to contained data buffer
52  const XsSize m_size; //!< \protected Size of contained data buffer in elements
53  const int m_flags; //!< \protected Flags for data management
54 
55 #ifdef __cplusplus
56  //! \brief Return the data management flags of the vector.
57  inline int flags() { return m_flags; }
58 public:
59  //! \brief Initialize a vector, empty or using the data in the supplied \a sz and \a src
60  inline explicit XsVector(XsSize sz = 0, const XsReal* src = 0)
61  : m_data(0)
62  , m_size(0)
63  , m_flags(0)
64  {
65  if (sz)
66  XsVector_construct(this, sz, src);
67  }
68 
69  //! \brief Initialize a vector using the supplied \a other vector
70  inline XsVector(const XsVector& other)
71  : m_data(0)
72  , m_size(0)
73  , m_flags(0)
74  {
75  *this = other;
76  }
77 
78  //! \brief Initialize a vector that references the supplied data
79  inline explicit XsVector(XsReal* ref, XsSize sz, XsDataFlags flags = XSDF_None)
80  : m_data(ref)
81  , m_size(sz)
82  , m_flags(flags)
83  {
84  }
85 
86  //! \brief Initialize a vector that references the supplied data
87  inline explicit XsVector(const XsVector& other, XsReal* ref, XsSize sz, XsDataFlags flags = XSDF_None)
88  : m_data(ref)
89  , m_size(sz)
90  , m_flags(flags)
91  {
92  XsVector_copy(this, &other);
93  }
94 
95  //! \copydoc XsVector_angularVelocityFromQuaternion
96  inline explicit XsVector(const XsQuaternion& quat, XsReal deltaT)
97  : m_data(0)
98  , m_size(0)
99  , m_flags(0)
100  {
101  XsVector_angularVelocityFromQuaternion(this, deltaT, &quat);
102  }
103 
104  //! \brief Assignment operator. Copies from \a other into this
105  inline XsVector& operator=(const XsVector& other)
106  {
107  XsVector_copy(this, &other);
108  return *this;
109  }
110 
111  //! \copydoc XsVector_destruct
112  inline ~XsVector()
113  {
114  XsVector_destruct(this);
115  }
116 
117  //! \copydoc XsVector_assign
118  inline void assign(XsSize sz, const XsReal* src)
119  {
120  XsVector_assign(this, sz, src);
121  }
122 
123  /*! \brief Sets the size of the XsVector to \a sz items
124  \param sz The desired size of the vector
125  \sa XsVector_assign
126  */
127  inline void setSize(XsSize sz)
128  {
129  XsVector_assign(this, sz, 0);
130  }
131 
132  //! \brief Returns the number of elements in the vector
133  inline XsSize size() const
134  {
135  return m_size;
136  }
137 
138  //! \brief Return a const pointer to the data
139  inline const XsReal* data() const
140  {
141  return m_data;
142  }
143 
144  //! \brief Multiply the vector by \a scalar and return the result
145  inline XsVector operator * (XsReal scalar) const
146  {
147  XsVector v(m_size);
148  for (XsSize i = 0; i < m_size; ++i)
149  v.m_data[i] = m_data[i] * scalar;
150  return v;
151  }
152 
153  //! \brief Multiply the vector by \a scalar and store the result in this vector
154  inline void operator *=(XsReal scalar)
155  {
156  for (XsSize i = 0; i < m_size; ++i)
157  m_data[i] *= scalar;
158  }
159 
160  //! \brief Returns a reference to the \a index'th item in the vector
161  inline XsReal& at(XsSize index)
162  {
163  assert(index < m_size);
164  return m_data[index];
165  }
166 
167  //! \brief Returns a const reference to the \a index'th item in the vector
168  inline const XsReal& at(XsSize index) const
169  {
170  assert(index < m_size);
171  return m_data[index];
172  }
173 
174  //! \brief Returns the \a index'th item in the vector
175  inline XsReal value(XsSize index) const
176  {
177  assert(index < m_size);
178  return m_data[index];
179  }
180 
181  //! \brief Sets the \a index'th item in the vector
182  inline void setValue(XsSize index, XsReal val)
183  {
184  assert(index < m_size);
185  m_data[index] = val;
186  }
187 
188  //! \brief Returns the \a index'th item in the vector
189  inline XsReal operator[](XsSize index) const
190  {
191  assert(index < m_size);
192  return m_data[index];
193  }
194 
195  //! \brief Returns a reference the \a index'th item in the vector
196  inline XsReal& operator[](XsSize index)
197  {
198  assert(index < m_size);
199  return m_data[index];
200  }
201 
202  //! \brief \copybrief XsVector_dotProduct
203  inline XsReal dotProduct(const XsVector &v) const
204  {
205  return XsVector_dotProduct(this, &v);
206  }
207 
208  //! \copydoc XsVector_cartesianLength
209  inline XsReal cartesianLength() const
210  {
211  return XsVector_cartesianLength(this);
212  }
213 
214  //! \brief \copybrief XsVector_setZero
215  inline void setZero()
216  {
217  return XsVector_setZero(this);
218  }
219 
220  //! \brief \copybrief XsVector_empty
221  inline bool empty() const
222  {
223  return 0 != XsVector_empty(this);
224  }
225 
226  //! \copydoc XsVector_angularVelocityFromQuaternion
227  inline XsVector& angularVelocityFromQuaternion(const XsQuaternion& quat, XsReal deltaT)
228  {
229  XsVector_angularVelocityFromQuaternion(this, deltaT, &quat);
230  return *this;
231  }
232 
233  //! \brief Return \e this - \a sub
234  XsVector operator-(const XsVector& sub) const
235  {
236  assert(m_size == sub.m_size);
237  XsVector tmp(m_size);
238  for (XsSize i = 0; i < m_size; ++i)
239  tmp[i] = m_data[i] - sub.m_data[i];
240  return tmp;
241  }
242 
243  //! \brief Return \e this + \a sub
244  XsVector operator+(const XsVector& sub) const
245  {
246  assert(m_size == sub.m_size);
247  XsVector tmp(m_size);
248  for (XsSize i = 0; i < m_size; ++i)
249  tmp[i] = m_data[i] + sub.m_data[i];
250  return tmp;
251  }
252 
253  //! \brief Return true when the values in this vector are exactly (to the last bit) equal to \a other
254  bool operator==(const XsVector& other) const
255  {
256  return 0 != XsVector_equal(this, &other);
257  }
258 
259 #ifndef XSENS_NO_STL
260  //! \brief Returns the XsVector as a std::vector of XsReal
261  inline std::vector<XsReal> toVector() const
262  {
263  std::vector<XsReal> tmp(m_size);
264  if (m_size)
265  memcpy(&tmp[0], m_data, m_size * sizeof(XsReal));
266  return tmp;
267  }
268 #endif
269 
270  /*! \brief Fill the vector with zeroes */
271  inline void zero()
272  {
273  for (XsSize i = 0; i < m_size; ++i)
274  m_data[i] = XsMath_zero;
275  }
276 
277  /*! \brief Fill the vector with \a value */
278  inline void fill(XsReal value)
279  {
280  for (XsSize i = 0; i < m_size; ++i)
281  m_data[i] = value;
282  }
283 
284  /*! \brief Swap the contents of \a b with this
285  \details This function swaps the internal buffers so no actual data is moved around. For unmanaged
286  data an elementwise swap is done, but only if the vectors are the same size.
287  \param b Object whose contents will be swapped with this
288  */
289  inline void swap(XsVector& b)
290  {
291  XsVector_swap(this, &b);
292  }
293 
294 #endif
295 };
296 #ifndef XSENS_NO_PACK
297 #pragma pack(pop)
298 #endif
299 
300 #ifdef __cplusplus
301 //! \brief Multiplies all values in the vector \a v by \a scalar
302 inline XsVector operator *(XsReal scalar, const XsVector &v)
303 {
304  return v*scalar;
305 }
306 #endif
307 
308 #endif // file guard
void BASE_IMPEXP memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) MRPT_NO_THROWS
An OS and compiler independent version of "memcpy".
Definition: os.cpp:358
XSTYPES_DLL_API XsReal XsVector_cartesianLength(const XsVector *thisPtr)
bool operator==(const TPoint2D &p1, const TPoint2D &p2)
Exact comparison between 2D points.
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:38
XSTYPES_DLL_API const XsReal XsMath_zero
GLuint buffer
Definition: glext.h:3775
GLenum GLint ref
Definition: glext.h:3888
XSTYPES_DLL_API int XsVector_empty(const XsVector *thisPtr)
size_t XsSize
XsSize must be unsigned number!
Definition: xstypedefs.h:17
GLuint src
Definition: glext.h:6303
No flag set.
Definition: xstypedefs.h:38
struct XsVector XsVector
Definition: xsvector.h:25
XSTYPES_DLL_API void XsVector_swap(XsVector *a, XsVector *b)
XSTYPES_DLL_API int XsVector_equal(const XsVector *thisPtr, const XsVector *thatPtr)
std::vector< T1 > operator+(const std::vector< T1 > &a, const std::vector< T2 > &b)
a+b (element-wise sum)
Definition: ops_vectors.h:89
XSTYPES_DLL_API void XsVector_destruct(XsVector *thisPtr)
GLuint index
Definition: glext.h:3891
XSTYPES_DLL_API void XsVector_assign(XsVector *thisPtr, XsSize sz, const XsReal *src)
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:123
int val
Definition: mrpt_jpeglib.h:953
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:5575
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:56
EIGEN_STRONG_INLINE void assign(const Scalar v)
Definition: eigen_plugins.h:41
#define XSTYPES_DLL_API
Definition: xstypesconfig.h:9
const GLdouble * v
Definition: glext.h:3603
const int m_flags
Flags for data management.
Definition: xsvector.h:53
const XsSize m_size
Size of contained data buffer in elements.
Definition: xsvector.h:52
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:16
XSTYPES_DLL_API void XsVector_copy(XsVector *copy, XsVector const *src)
GLsizei const GLfloat * value
Definition: glext.h:3929
GLsizeiptr size
Definition: glext.h:3779
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:51
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3520
GLubyte GLubyte GLubyte a
Definition: glext.h:5575
TPoint3D operator-(const TPoint3D &p1)
Unary minus operator for 3D points.
std::vector< T1 > & operator*=(std::vector< T1 > &a, const std::vector< T2 > &b)
a*=b (element-wise multiplication)
Definition: ops_vectors.h:40
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:37
std::vector< T1 > operator*(const std::vector< T1 > &a, const std::vector< T2 > &b)
a*b (element-wise multiplication)
Definition: ops_vectors.h:59
XSTYPES_DLL_API void XsVector_multiplyScalar(const XsVector *thisPtr, XsReal scalar, XsVector *dest)



Page generated by Doxygen 1.8.14 for MRPT 1.5.6 Git: 4c65e8431 Tue Apr 24 08:18:17 2018 +0200 at lun oct 28 01:35:26 CET 2019