65 #ifndef XSENS_GENERIC_MATRIX_H
66 #define XSENS_GENERIC_MATRIX_H
70 #ifndef XSENS_MATH_FIRMWARE
71 #ifndef XSENS_EXCEPTION_H
76 #ifdef XSENS_GENERIC_MATRIX_RANGE_CHECKS
78 #define XSENS_GENERIC_MATRIX_THROW throw(...)
80 #define XSENS_GENERIC_MATRIX_THROW
83 #define XSENS_GENERIC_MATRIX_THROW
86 #ifndef XSENS_THROW_BAD_ALLOC
87 #ifdef XSENS_NO_EXCEPTIONS
89 #define XSENS_THROW_BAD_ALLOC XM_THROW("Bad alloc")
91 #define XSENS_THROW_BAD_ALLOC throw std::bad_alloc()
95 #ifndef _PSTDINT_H_INCLUDED
108 template <
typename T>
109 inline void swapb(T& a, T& b)
125 template <
typename T>
139 explicit GenericMatrix(
const uint32_t newRows,
const uint32_t newCols,
bool zeroValues =
true);
141 GenericMatrix(
const GenericMatrix<T>& src);
143 explicit GenericMatrix(
const uint32_t newRows,
const uint32_t newCols,
const T* src);
147 virtual ~GenericMatrix();
168 return m_rows * m_cols;
173 return m_rows * m_cols;
176 GenericMatrix<T>& operator = (
const GenericMatrix<T>& src);
178 void swap(GenericMatrix<T>& other);
180 friend void swap(GenericMatrix<T>& first, GenericMatrix<T>& second)
188 template <
typename T> GenericMatrix<T>::GenericMatrix()
193 m_data = (T*)malloc(m_rows * m_cols *
sizeof(T));
198 m_allocSize = m_rows * m_cols;
201 template <
typename T> GenericMatrix<T>::GenericMatrix(
uint32_t newRows,
uint32_t newCols,
bool zeroVals)
206 m_data = (T*)malloc(
sizeof(T) * (size_t)m_rows * m_cols);
211 m_allocSize = m_rows * m_cols;
217 template <
typename T> GenericMatrix<T>::GenericMatrix(
const GenericMatrix<T>& src)
222 m_data = (T*)malloc(
sizeof(T) * (size_t)m_rows * m_cols);
227 m_allocSize = m_rows * m_cols;
229 memcpy(m_data, src.m_data,
sizeof(T) * (
size_t)m_rows * m_cols);
232 template <
typename T> GenericMatrix<T>::GenericMatrix(
const uint32_t newRows,
uint32_t newCols,
const T* src)
237 m_data = (T*)malloc(
sizeof(T) * (size_t)m_rows * m_cols);
242 m_allocSize = m_rows * m_cols;
244 memcpy(m_data, src,
sizeof(T) * (
size_t)m_rows * m_cols);
247 template <
typename T> GenericMatrix<T>::GenericMatrix(
const uint32_t newRows,
const uint32_t newCols, T*
const buffer,
XsDataFlags flags)
261 m_data = (T*)malloc(
sizeof(T) * (size_t)m_rows * m_cols);
266 m_allocSize = m_rows * m_cols;
269 memcpy(m_data, buffer,
sizeof(T) * (
size_t)m_rows * m_cols);
272 template <
typename T> GenericMatrix<T>::~GenericMatrix()
283 template <
typename T>
284 void GenericMatrix<T>::setSize(
uint32_t newRows,
uint32_t newCols,
bool zeroVals)
286 if (newRows == m_rows && newCols == m_cols)
294 #ifdef XSENS_MATH_RESTRICT_RESIZE
296 XM_THROW(
"resizing of this object is not allowed");
299 const uint32_t newSize = newRows * newCols;
311 assert(newSize <= size());
315 if (newSize > m_allocSize || newSize == 0)
326 m_data = (T*)malloc(
sizeof(T) * (size_t)newRows * newCols);
328 m_allocSize = newRows * newCols;
342 template <
typename T>
345 #ifdef XSENS_GENERIC_MATRIX_RANGE_CHECKS
346 if (row >= m_rows || col >= m_cols)
349 return m_data[row * m_cols + col];
352 template <
typename T>
355 #ifdef XSENS_GENERIC_MATRIX_RANGE_CHECKS
359 return &m_data[row * m_cols];
362 template <
typename T>
363 void GenericMatrix<T>::zeroValues()
365 memset(
static_cast<void*
>(m_data), 0,
sizeof(T) * (
size_t)m_rows * m_cols);
368 template <
typename T>
369 void GenericMatrix<T>::swap(GenericMatrix<T>& other)
371 swapb(m_rows, other.m_rows);
372 swapb(m_cols, other.m_cols);
373 swapb(m_allocSize, other.m_allocSize);
374 swapb(m_flags, other.m_flags);
375 swapb(m_data, other.m_data);
378 template <
typename T>
379 GenericMatrix<T>& GenericMatrix<T>::operator = (
const GenericMatrix<T>& src)
383 GenericMatrix temp(src);