Main MRPT website > C++ reference for 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
XsVector::m_data
XSCPPPROTECTED XsReal *const m_data
Points to contained data buffer.
Definition: xsvector.h:63
XsVector_empty
XSTYPES_DLL_API int XsVector_empty(const XsVector *thisPtr)
mrpt::img::operator+
TColor operator+(const TColor &first, const TColor &second)
Pairwise addition of their corresponding RGBA members.
Definition: TColor.cpp:20
XsQuaternion
Definition: xsquaternion.h:57
src
GLuint src
Definition: glext.h:7278
XsVector_swap
XSTYPES_DLL_API void XsVector_swap(XsVector *a, XsVector *b)
mrpt::containers::operator[]
VALUE & operator[](const KEY &key)
Write/read via [i] operator, that creates an element if it didn't exist already.
Definition: ts_hash_map.h:199
mrpt::math::dotProduct
CONTAINER1::Scalar dotProduct(const CONTAINER1 &v1, const CONTAINER1 &v2)
v1*v2: The dot product of two containers (vectors/arrays/matrices)
Definition: ops_containers.h:190
XsVector_copy
XSTYPES_DLL_API void XsVector_copy(XsVector *copy, XsVector const *src)
XsVector_setZero
XSTYPES_DLL_API void XsVector_setZero(XsVector *thisPtr)
XsVector_assign
XSTYPES_DLL_API void XsVector_assign(XsVector *thisPtr, XsSize sz, const XsReal *src)
mrpt::img::operator==
bool operator==(const mrpt::img::TCamera &a, const mrpt::img::TCamera &b)
Definition: TCamera.cpp:201
XsVector
Definition: xsvector.h:59
XsVector_equal
XSTYPES_DLL_API int XsVector_equal(const XsVector *thisPtr, const XsVector *thatPtr)
XsVector_cartesianLength
XSTYPES_DLL_API XsReal XsVector_cartesianLength(const XsVector *thisPtr)
XSCPPPROTECTED
#define XSCPPPROTECTED
Definition: xstypesconfig.h:57
v
const GLdouble * v
Definition: glext.h:3678
mrpt::img::operator-
TColor operator-(const TColor &first, const TColor &second)
Pairwise substraction of their corresponding RGBA members.
Definition: TColor.cpp:31
val
int val
Definition: mrpt_jpeglib.h:955
data
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3547
XsVector_fill
XSTYPES_DLL_API void XsVector_fill(XsVector *thisPtr, XsReal value)
XsReal
double XsReal
Defines the floating point type used by the Xsens libraries.
Definition: xstypedefs.h:17
XsVector_multiplyScalar
XSTYPES_DLL_API void XsVector_multiplyScalar(const XsVector *thisPtr, XsReal scalar, XsVector *dest)
index
GLuint index
Definition: glext.h:4054
b
GLubyte GLubyte b
Definition: glext.h:6279
XsSize
size_t XsSize
XsSize must be unsigned number!
Definition: xstypedefs.h:19
XsVector
struct XsVector XsVector
Definition: xsvector.h:28
XsVector_dotProduct
XSTYPES_DLL_API XsReal XsVector_dotProduct(const XsVector *a, const XsVector *b)
XsVector::m_size
const XsSize m_size
Size of contained data buffer in elements.
Definition: xsvector.h:65
setSize
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...
Definition: eigen_plugins.h:343
buffer
GLuint buffer
Definition: glext.h:3917
xsmath.h
assign
EIGEN_STRONG_INLINE void assign(const Scalar v)
Definition: eigen_plugins.h:48
XsVector::m_flags
const int m_flags
Flags for data management.
Definition: xsvector.h:67
XsVector_construct
XSTYPES_DLL_API void XsVector_construct(XsVector *thisPtr, XsSize sz, const XsReal *src)
XsVector_ref
XSTYPES_DLL_API void XsVector_ref(XsVector *thisPtr, XsSize sz, XsReal *buffer, XsDataFlags flags)
XsMath_zero
const XSTYPES_DLL_API XsReal XsMath_zero
XSDF_None
@ XSDF_None
No flag set.
Definition: xstypedefs.h:44
mrpt::math::operator*
std::vector< T1 > operator*(const std::vector< T1 > &a, const std::vector< T2 > &b)
a*b (element-wise multiplication)
Definition: ops_vectors.h:55
value
GLsizei const GLfloat * value
Definition: glext.h:4117
XSTYPES_DLL_API
#define XSTYPES_DLL_API
Definition: xstypesconfig.h:9
XsDataFlags
XsDataFlags
These flags define the behaviour of data contained by Xsens data structures.
Definition: xstypedefs.h:41
empty
EIGEN_STRONG_INLINE bool empty() const
Definition: eigen_plugins.h:601
ref
GLenum GLint ref
Definition: glext.h:4050
mrpt::math::operator*=
std::vector< T1 > & operator*=(std::vector< T1 > &a, const std::vector< T2 > &b)
a*=b (element-wise multiplication)
Definition: ops_vectors.h:36
fill
EIGEN_STRONG_INLINE void fill(const Scalar v)
Definition: eigen_plugins.h:46
XsVector_angularVelocityFromQuaternion
XSTYPES_DLL_API void XsVector_angularVelocityFromQuaternion(XsVector *thisPtr, XsReal deltaT, const struct XsQuaternion *quat)
size
GLsizeiptr size
Definition: glext.h:3923
XsVector_destruct
XSTYPES_DLL_API void XsVector_destruct(XsVector *thisPtr)
a
GLubyte GLubyte GLubyte a
Definition: glext.h:6279
mrpt::system::os::memcpy
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



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST