9 #include "imstkDataArray.h" 10 #include "imstkMath.h" 11 #include "imstkLogger.h" 22 template<
typename T,
int N>
23 class VecDataArray :
public DataArray<T>
27 using ValueType = Eigen::Matrix<T, N, 1>;
28 static constexpr
int NumComponents = N;
34 using value_type = ValueType;
35 using iterator_category = std::forward_iterator_tag;
36 using difference_type = std::ptrdiff_t;
37 using pointer = ValueType*;
38 using reference = ValueType&;
41 iterator(pointer ptr, pointer end) : ptr_(ptr), end_(end) { }
47 #ifdef IMSTK_CHECK_ARRAY_RANGE 48 if ((end_ - ptr_) < 0) {
throw std::runtime_error(
"iterator past bounds"); }
56 #ifdef IMSTK_CHECK_ARRAY_RANGE 57 if ((end_ - ptr_) < 0) {
throw std::runtime_error(
"iterator past bounds"); }
62 reference operator*() {
return *ptr_; }
64 pointer operator->() {
return ptr_; }
66 bool operator==(
const self_type& rhs)
const {
return ptr_ == rhs.ptr_; }
68 bool operator!=(
const self_type& rhs)
const {
return ptr_ != rhs.ptr_; }
79 using value_type = ValueType;
80 using iterator_category = std::forward_iterator_tag;
81 using difference_type = std::ptrdiff_t;
82 using pointer = ValueType*;
83 using reference =
const ValueType&;
86 const_iterator(pointer ptr, pointer end) : ptr_(ptr), end_(end) { }
92 #ifdef IMSTK_CHECK_ARRAY_RANGE 93 if ((end_ - ptr_) < 0) {
throw std::runtime_error(
"iterator past bounds"); }
98 self_type operator++(
int junk) { ptr_++;
return *
this; }
102 const pointer operator->() {
return ptr_; }
106 bool operator!=(
const self_type& rhs)
const {
return ptr_ != rhs.ptr_; }
128 m_vecSize(static_cast<int>(list.size())),
129 m_vecCapacity(static_cast<int>(list.size())),
130 m_dataCast(reinterpret_cast<ValueType*>(
DataArray<T>::m_data))
143 AbstractDataArray::m_size = other.m_size;
144 AbstractDataArray::m_capacity = other.m_capacity;
145 AbstractDataArray::m_scalarType = other.m_scalarType;
155 m_vecSize = other.m_vecSize;
156 m_vecCapacity = other.m_vecCapacity;
163 AbstractDataArray::m_size = other.m_size;
164 AbstractDataArray::m_capacity = other.m_capacity;
165 m_vecSize = other.m_vecSize;
166 m_vecCapacity = other.m_vecCapacity;
167 AbstractDataArray::m_scalarType = other.m_scalarType;
169 m_dataCast = other.m_dataCast;
170 other.m_mapped =
true;
184 throw(std::runtime_error(
"Can't cast a mapped array"));
187 other.reserve(size());
188 for (
auto item : *
this)
190 other.push_back(item.template cast<U>());
195 std::shared_ptr<AbstractDataArray>
cast(ScalarTypeId type)
override 197 if (type == AbstractDataArray::m_scalarType)
199 return std::make_shared<VecDataArray<T, N>>(*this);
205 throw(std::runtime_error(
"Unknown scalar type"));
213 inline void resize(
const int size)
override 221 if (size == m_vecCapacity)
224 m_vecSize = m_vecCapacity;
232 AbstractDataArray::m_size = m_vecSize = 0;
243 inline void fill(
const ValueType& val) { std::fill_n(m_dataCast, m_vecSize, val); }
245 inline int size()
const {
return m_vecSize; }
265 const int newVecSize = m_vecSize + 1;
266 if (newVecSize > m_vecCapacity)
270 m_vecSize = newVecSize;
271 AbstractDataArray::m_size = newVecSize * N;
272 m_dataCast[newVecSize - 1] = val;
275 inline void push_back(
const ValueType&& val)
283 const int newVecSize = m_vecSize + 1;
284 if (newVecSize > m_vecCapacity)
288 m_vecSize = newVecSize;
289 AbstractDataArray::m_size = newVecSize * N;
290 m_dataCast[newVecSize - 1] = val;
300 iterator end() {
return iterator(m_dataCast + m_vecSize, m_dataCast + m_vecSize); }
319 if (size < m_vecCapacity) {
return; }
321 const int currVecSize = m_vecSize;
322 const int currSize = AbstractDataArray::m_size;
324 AbstractDataArray::m_size = currSize;
325 m_vecSize = currVecSize;
328 inline ValueType* getPointer() {
return m_dataCast; }
330 inline ValueType& operator[](
const size_t pos)
332 #ifdef IMSTK_CHECK_ARRAY_RANGE 333 if (pos >= static_cast<size_t>(m_vecSize)) {
throw std::out_of_range(
"Index out of range"); }
335 return m_dataCast[pos];
338 inline const ValueType& operator[](
const size_t pos)
const 340 #ifdef IMSTK_CHECK_ARRAY_RANGE 341 if (pos >= static_cast<size_t>(m_vecSize)) {
throw std::out_of_range(
"Index out of range"); }
343 return m_dataCast[pos];
350 inline ValueType&
at(
const size_t pos)
352 #ifdef IMSTK_CHECK_ARRAY_RANGE 353 if (pos >= static_cast<size_t>(m_vecSize)) {
throw std::out_of_range(
"Index out of range"); }
355 return m_dataCast[pos];
362 inline const ValueType&
at(
const size_t pos)
const 364 #ifdef IMSTK_CHECK_ARRAY_RANGE 365 if (pos >= static_cast<size_t>(m_vecSize)) {
throw std::out_of_range(
"Index out of range"); }
367 return m_dataCast[pos];
370 inline void erase(
const int vecPos)
379 const int newSize = AbstractDataArray::m_size - N;
386 const int pos = vecPos * N;
388 if (vecPos != m_vecSize - 1)
394 AbstractDataArray::m_size -= N;
400 template<
typename U,
int M>
416 m_vecSize = m_vecCapacity =
static_cast<int>(list.size());
417 AbstractDataArray::m_size = AbstractDataArray::m_capacity = m_vecSize * N;
429 setData(other.m_dataCast, other.size());
445 AbstractDataArray::m_size = other.m_size;
446 m_vecSize = other.m_vecSize;
469 inline void setData(ValueType* ptr,
const int size)
479 AbstractDataArray::m_size = AbstractDataArray::m_capacity = size * N;
480 m_vecSize = m_vecCapacity = size;
489 std::unique_ptr<VecDataArray<T, N>>
clone()
491 return std::unique_ptr<VecDataArray<T, N>>(cloneImplementation());
503 ValueType* m_dataCast;
ValueType & at(const size_t pos)
VecDataArray(std::initializer_list< Eigen::Matrix< T, N, 1 >> list)
Constructs from intializer list.
VecDataArray(const int size)
Constructs a data array of size.
VecDataArray()
Constructs an empty data array.
VecDataArray< T, N > & operator=(std::initializer_list< Eigen::Matrix< U, M, 1 >> list)
Allow initialization from initializer list, ie: DataArray<int> arr = { 1, 2 }.
virtual void squeeze()
Resize to current size.
void reserve(const int size) override
Allocates extra capacity, for the number of values, conservative reallocate.
void resize(const int size) override
Resize data array to hold exactly size number of values.
void push_back(const ValueType &val)
Append the data array to hold the new value, resizes if neccesary.
iterator begin()
begin(), end() to mirror std::vector
void squeeze() override
Resize to current size.
Simple dynamic array implementation that also supports event posting and viewing/facade.
void clear()
Resizes to 0.
void resize(const int size) override
Resize data array to hold exactly size number of values.
std::shared_ptr< AbstractDataArray > cast(ScalarTypeId type) override
Cast array to the IMSTK type on the abstract interface.
const ValueType & at(const size_t pos) const
std::unique_ptr< VecDataArray< T, N > > clone()
Polymorphic clone, shadows the declaration in the superclasss but returns own type.
VecDataArray< U, N > cast()
Templated copy the current array with a new internal data type, does not change the number of compone...
void setData(ValueType *ptr, const int size)
Computes the range of a component of the vectors elements.
bool operator==(const Color &color_lhs, const Color &color_rhs)
Comparison operator.
Color operator*(const Color &color_lhs, const Color &color_rhs)
Multiplication operators.
int getNumberOfComponents() const override
Returns the number of components.
Simple dynamic array implementation that also supports event posting and viewing/facade.