| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
#ifndef XSENS_GENERIC_MATRIX_H |
| 34 |
#define XSENS_GENERIC_MATRIX_H |
| 35 |
|
| 36 |
#include "xsens_math_throw.h" |
| 37 |
|
| 38 |
#ifndef XSENS_MATH_FIRMWARE |
| 39 |
# ifndef XSENS_EXCEPTION_H |
| 40 |
# include <xstypes/xsexception.h> |
| 41 |
# endif |
| 42 |
#endif |
| 43 |
|
| 44 |
#ifdef XSENS_GENERIC_MATRIX_RANGE_CHECKS |
| 45 |
# ifdef _MSC_VER |
| 46 |
# define XSENS_GENERIC_MATRIX_THROW throw(...) |
| 47 |
# else |
| 48 |
# define XSENS_GENERIC_MATRIX_THROW |
| 49 |
# endif |
| 50 |
#else |
| 51 |
# define XSENS_GENERIC_MATRIX_THROW |
| 52 |
#endif |
| 53 |
|
| 54 |
#ifndef XSENS_THROW_BAD_ALLOC |
| 55 |
# ifdef XSENS_NO_EXCEPTIONS |
| 56 |
# include <assert.h> |
| 57 |
# define XSENS_THROW_BAD_ALLOC XM_THROW("Bad alloc") |
| 58 |
# else |
| 59 |
# define XSENS_THROW_BAD_ALLOC throw std::bad_alloc() |
| 60 |
# endif |
| 61 |
#endif |
| 62 |
|
| 63 |
#ifndef _PSTDINT_H_INCLUDED |
| 64 |
#include <xstypes/pstdint.h> |
| 65 |
#endif |
| 66 |
|
| 67 |
#include <stdlib.h> |
| 68 |
#include <string.h> |
| 69 |
|
| 70 |
#ifdef __cplusplus |
| 71 |
namespace xsens |
| 72 |
{ |
| 73 |
namespace { |
| 74 |
|
| 75 |
template <typename T> |
| 76 |
inline void swapb(T& a, T& b) |
| 77 |
{ |
| 78 |
T temp = a; |
| 79 |
a = b; |
| 80 |
b = temp; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
template <typename T> |
| 93 |
class GenericMatrix |
| 94 |
{ |
| 95 |
protected: |
| 96 |
T* m_data; |
| 97 |
uint32_t m_rows; |
| 98 |
uint32_t m_cols; |
| 99 |
uint32_t m_allocSize; |
| 100 |
int m_flags; |
| 101 |
|
| 102 |
public: |
| 103 |
|
| 104 |
GenericMatrix(); |
| 105 |
|
| 106 |
explicit GenericMatrix(const uint32_t newRows, const uint32_t newCols, bool zeroValues = true); |
| 107 |
|
| 108 |
GenericMatrix(const GenericMatrix<T>& src); |
| 109 |
|
| 110 |
explicit GenericMatrix(const uint32_t newRows, const uint32_t newCols, const T* src); |
| 111 |
|
| 112 |
explicit GenericMatrix(const uint32_t newRows, const uint32_t newCols, T* buffer, XsDataFlags flags); |
| 113 |
|
| 114 |
virtual ~GenericMatrix(); |
| 115 |
|
| 116 |
|
| 117 |
void setSize(uint32_t newRows, uint32_t newCols, bool zeroValues = true); |
| 118 |
|
| 119 |
T& get(const uint32_t row, const uint32_t cols) const XSENS_GENERIC_MATRIX_THROW; |
| 120 |
|
| 121 |
T* operator [] (const uint32_t row) const XSENS_GENERIC_MATRIX_THROW; |
| 122 |
|
| 123 |
uint32_t rows(void) const { return m_rows; } |
| 124 |
|
| 125 |
uint32_t cols(void) const { return m_cols; } |
| 126 |
|
| 127 |
uint32_t count(void) const { return m_rows*m_cols; } |
| 128 |
|
| 129 |
uint32_t size(void) const { return m_rows*m_cols; } |
| 130 |
|
| 131 |
GenericMatrix<T>& operator = (const GenericMatrix<T>& src); |
| 132 |
|
| 133 |
void swap(GenericMatrix<T>& other); |
| 134 |
|
| 135 |
void zeroValues(); |
| 136 |
}; |
| 137 |
|
| 138 |
template <typename T> |
| 139 |
GenericMatrix<T>::GenericMatrix() |
| 140 |
: m_rows(1) |
| 141 |
, m_cols(1) |
| 142 |
, m_flags(XSDF_Managed) |
| 143 |
{ |
| 144 |
m_data = (T*)malloc(m_rows*m_cols * sizeof(T)); |
| 145 |
|
| 146 |
if (!m_data) |
| 147 |
XSENS_THROW_BAD_ALLOC; |
| 148 |
|
| 149 |
m_allocSize = m_rows*m_cols; |
| 150 |
} |
| 151 |
|
| 152 |
template <typename T> |
| 153 |
GenericMatrix<T>::GenericMatrix(uint32_t newRows, uint32_t newCols, bool zeroVals) |
| 154 |
: m_rows(newRows) |
| 155 |
, m_cols(newCols) |
| 156 |
, m_flags(XSDF_Managed) |
| 157 |
{ |
| 158 |
m_data = (T*)malloc(sizeof(T) * (size_t)m_rows*m_cols); |
| 159 |
|
| 160 |
if (!m_data) |
| 161 |
XSENS_THROW_BAD_ALLOC; |
| 162 |
|
| 163 |
m_allocSize = m_rows*m_cols; |
| 164 |
|
| 165 |
if (zeroVals) |
| 166 |
zeroValues(); |
| 167 |
} |
| 168 |
|
| 169 |
template <typename T> |
| 170 |
GenericMatrix<T>::GenericMatrix(const GenericMatrix<T>& src) |
| 171 |
: m_rows(src.m_rows) |
| 172 |
, m_cols(src.m_cols) |
| 173 |
, m_flags(XSDF_Managed) |
| 174 |
{ |
| 175 |
m_data = (T*)malloc(sizeof(T) * (size_t)m_rows*m_cols); |
| 176 |
|
| 177 |
if (!m_data) |
| 178 |
XSENS_THROW_BAD_ALLOC; |
| 179 |
|
| 180 |
m_allocSize = m_rows*m_cols; |
| 181 |
|
| 182 |
memcpy(m_data, src.m_data, sizeof(T) * (size_t)m_rows*m_cols); |
| 183 |
} |
| 184 |
|
| 185 |
template <typename T> |
| 186 |
GenericMatrix<T>::GenericMatrix(const uint32_t newRows, uint32_t newCols, const T* src) |
| 187 |
: m_rows(newRows) |
| 188 |
, m_cols(newCols) |
| 189 |
, m_flags(XSDF_Managed) |
| 190 |
{ |
| 191 |
m_data = (T*)malloc(sizeof(T) * (size_t)m_rows*m_cols); |
| 192 |
|
| 193 |
if (!m_data) |
| 194 |
XSENS_THROW_BAD_ALLOC; |
| 195 |
|
| 196 |
m_allocSize = m_rows*m_cols; |
| 197 |
|
| 198 |
memcpy(m_data, src, sizeof(T) * (size_t)m_rows*m_cols); |
| 199 |
} |
| 200 |
|
| 201 |
template <typename T> |
| 202 |
GenericMatrix<T>::GenericMatrix(const uint32_t newRows, const uint32_t newCols, T* const buffer, XsDataFlags flags) |
| 203 |
: m_rows(newRows) |
| 204 |
, m_cols(newCols) |
| 205 |
, m_flags(flags) |
| 206 |
{ |
| 207 |
if (m_flags & XSDF_FixedSize) |
| 208 |
{ |
| 209 |
m_data = buffer; |
| 210 |
|
| 211 |
if (size() == 0) |
| 212 |
m_flags |= XSDF_Empty; |
| 213 |
} |
| 214 |
|
| 215 |
if (m_flags & XSDF_Managed) |
| 216 |
m_data = (T*)malloc(sizeof(T) * (size_t)m_rows*m_cols); |
| 217 |
|
| 218 |
if (!m_data) |
| 219 |
XSENS_THROW_BAD_ALLOC; |
| 220 |
|
| 221 |
m_allocSize = m_rows*m_cols; |
| 222 |
|
| 223 |
if (m_flags & XSDF_Managed) |
| 224 |
memcpy(m_data, buffer, sizeof(T) * (size_t)m_rows*m_cols); |
| 225 |
} |
| 226 |
|
| 227 |
template <typename T> |
| 228 |
GenericMatrix<T>::~GenericMatrix() |
| 229 |
{ |
| 230 |
if (m_flags & XSDF_Managed) |
| 231 |
free(m_data); |
| 232 |
|
| 233 |
if (!(m_flags & XSDF_FixedSize)) |
| 234 |
m_data = NULL; |
| 235 |
else |
| 236 |
m_flags |= XSDF_Empty; |
| 237 |
} |
| 238 |
|
| 239 |
template <typename T> |
| 240 |
void GenericMatrix<T>::setSize(uint32_t newRows, uint32_t newCols, bool zeroVals) |
| 241 |
{ |
| 242 |
if (newRows == m_rows && newCols == m_cols) |
| 243 |
{ |
| 244 |
if (zeroVals) |
| 245 |
zeroValues(); |
| 246 |
|
| 247 |
return; |
| 248 |
} |
| 249 |
|
| 250 |
#ifdef XSENS_MATH_RESTRICT_RESIZE |
| 251 |
if (m_data && !(m_flags & XSDF_Managed)) |
| 252 |
XM_THROW("resizing of this object is not allowed"); |
| 253 |
#endif |
| 254 |
|
| 255 |
const uint32_t newSize = newRows * newCols; |
| 256 |
|
| 257 |
if (m_flags & XSDF_FixedSize) |
| 258 |
{ |
| 259 |
if (newSize == 0) |
| 260 |
{ |
| 261 |
m_flags |= XSDF_Empty; |
| 262 |
m_rows = newRows; |
| 263 |
m_cols = newCols; |
| 264 |
return; |
| 265 |
} |
| 266 |
|
| 267 |
assert(newSize <= size()); |
| 268 |
m_flags &= ~XSDF_Empty; |
| 269 |
} |
| 270 |
|
| 271 |
if (newSize > m_allocSize || newSize == 0) |
| 272 |
{ |
| 273 |
if (m_flags & XSDF_Managed) |
| 274 |
{ |
| 275 |
free(m_data); |
| 276 |
m_data = 0; |
| 277 |
m_allocSize = 0; |
| 278 |
} |
| 279 |
|
| 280 |
if (newSize > 0) |
| 281 |
{ |
| 282 |
m_data = (T*)malloc(sizeof(T) * (size_t)newRows*newCols); |
| 283 |
m_flags = XSDF_Managed; |
| 284 |
m_allocSize = newRows*newCols; |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
m_rows = newRows; |
| 289 |
m_cols = newCols; |
| 290 |
|
| 291 |
if (!m_data) |
| 292 |
{ |
| 293 |
XSENS_THROW_BAD_ALLOC; |
| 294 |
} |
| 295 |
|
| 296 |
if (zeroVals) |
| 297 |
zeroValues(); |
| 298 |
} |
| 299 |
|
| 300 |
template <typename T> |
| 301 |
T& GenericMatrix<T>::get(uint32_t row, uint32_t col) const XSENS_GENERIC_MATRIX_THROW |
| 302 |
{ |
| 303 |
#ifdef XSENS_GENERIC_MATRIX_RANGE_CHECKS |
| 304 |
if (row >= m_rows || col >= m_cols) |
| 305 |
XM_THROW("index out of bounds"); |
| 306 |
#endif |
| 307 |
return m_data[row*m_cols + col]; |
| 308 |
} |
| 309 |
|
| 310 |
template <typename T> |
| 311 |
T* GenericMatrix<T>::operator [] (const uint32_t row) const XSENS_GENERIC_MATRIX_THROW |
| 312 |
{ |
| 313 |
#ifdef XSENS_GENERIC_MATRIX_RANGE_CHECKS |
| 314 |
if (row >= m_rows) |
| 315 |
XM_THROW("index out of bounds"); |
| 316 |
#endif |
| 317 |
return &m_data[row*m_cols]; |
| 318 |
} |
| 319 |
|
| 320 |
template <typename T> |
| 321 |
void GenericMatrix<T>::zeroValues() |
| 322 |
{ |
| 323 |
memset(static_cast<void*>(m_data), 0, sizeof(T) * (size_t)m_rows*m_cols); |
| 324 |
} |
| 325 |
|
| 326 |
template <typename T> |
| 327 |
void GenericMatrix<T>::swap(GenericMatrix<T>& other) |
| 328 |
{ |
| 329 |
swapb(m_rows, other.m_rows); |
| 330 |
swapb(m_cols, other.m_cols); |
| 331 |
swapb(m_allocSize, other.m_allocSize); |
| 332 |
swapb(m_flags, other.m_flags); |
| 333 |
swapb(m_data, other.m_data); |
| 334 |
} |
| 335 |
|
| 336 |
template <typename T> |
| 337 |
GenericMatrix<T>& GenericMatrix<T>::operator = (const GenericMatrix<T>& src) |
| 338 |
{ |
| 339 |
if (this != &src) |
| 340 |
{ |
| 341 |
GenericMatrix temp(src); |
| 342 |
swap(temp); |
| 343 |
} |
| 344 |
return *this; |
| 345 |
} |
| 346 |
|
| 347 |
} |
| 348 |
|
| 349 |
#endif |
| 350 |
#endif |