Mech-Eye API Reference 2.1.4-alpha
API reference documentation for Mech-Eye 3D Laser Profiler
All Classes Functions Variables Enumerations Enumerator Pages
BatchArray.h
1#pragma once
2#include <cstring>
3#include <memory>
4#include <vector>
5#include <stdexcept>
6
7namespace mmind {
8namespace eye {
9
13template <typename ElementData>
15{
16public:
20 BatchArray(size_t width) : _width(width) {}
24 ~BatchArray() = default;
25
29 size_t width() const { return _width; }
30
34 size_t height() const { return _height; }
35
39 void setHeight(size_t height)
40 {
41 if (height > _capacity) {
43 }
44 _height = height;
45 }
46
50 size_t capacity() const { return _capacity; }
51
55 bool isEmpty() const { return _height == 0; }
56
60 void reserve(size_t height)
61 {
62 if (_capacity >= height)
63 return;
64
65 std::shared_ptr<ElementData> pNewData(new ElementData[_width * height],
66 [](ElementData* p) { delete[] p; });
67 if (_pData) {
68 memcpy(pNewData.get(), _pData.get(), _width * _height * sizeof(ElementData));
69 }
70 _capacity = height;
71 _pData = std::move(pNewData);
72 }
73
77 bool append(const BatchArray& data)
78 {
79 if (_width != data.width())
80 return false;
81
82 if (_capacity - _height < data.height()) {
83 reserve(data.height() + _height);
84 }
85 memcpy(_pData.get() + _height * _width, data.data(),
86 data.height() * data.width() * sizeof(ElementData));
87 _height += data.height();
88 return true;
89 }
90
94 const ElementData* data() const { return _pData.get(); }
95
99 ElementData* data() { return _pData.get(); }
100
106 const ElementData& operator[](std::size_t n) const
107 {
108 if (n >= _height * _width || !_pData)
109 throw std::out_of_range("invalid subscript");
110 ElementData* data = _pData.get();
111 return data[n];
112 }
113
119 ElementData& operator[](std::size_t n)
120 {
121 return const_cast<ElementData&>(static_cast<const BatchArray<ElementData>&>(*this)[n]);
122 }
123
131 const ElementData& at(uint32_t row, uint32_t col) const
132 {
133 if (row >= _height || col >= _width || !_pData)
134 throw std::out_of_range("invalid subscript");
135 ElementData* data = _pData.get();
136 return data[row * _width + col];
137 }
138
146 ElementData& at(size_t row, size_t col)
147 {
148 return const_cast<ElementData&>(
149 static_cast<const BatchArray<ElementData>&>(*this).at(row, col));
150 }
151
156 {
157 BatchArray<ElementData> copy(_width);
158 copy.reserve(_height);
159 memcpy(copy.data(), data(), _height * _width * sizeof(ElementData));
160 copy.setHeight(_height);
161 return copy;
162 }
163
167 void clear()
168 {
169 memset(_pData.get(), 0, _height * _width * sizeof(ElementData));
170 _height = 0;
171 }
172
173private:
174 size_t _width{0};
175 size_t _height{0};
176 size_t _capacity{0};
177 std::shared_ptr<ElementData> _pData;
178};
179} // namespace eye
180} // namespace mmind
Represents the data structure of the profile data.
Definition BatchArray.h:15
const ElementData & at(uint32_t row, uint32_t col) const
Returns a const element reference to the specified row and column index in the BatchArray object.
Definition BatchArray.h:131
ElementData & operator[](std::size_t n)
Returns a element reference to the specified index in the BatchArray object using the operator [].
Definition BatchArray.h:119
void clear()
Clears the data of the BatchArray object.
Definition BatchArray.h:167
BatchArray(size_t width)
Constructor.
Definition BatchArray.h:20
ElementData * data()
Definition BatchArray.h:99
const ElementData * data() const
Definition BatchArray.h:94
size_t height() const
Returns the height of the BatchArray object.
Definition BatchArray.h:34
bool isEmpty() const
Returns true if the BatchArray object has no elements.
Definition BatchArray.h:55
size_t width() const
Returns the width of the BatchArray object.
Definition BatchArray.h:29
bool append(const BatchArray &data)
Appends the data variable onto the end of this BatchArray object.
Definition BatchArray.h:77
BatchArray< ElementData > clone() const
Creates a deep copy of the BatchArray object.
Definition BatchArray.h:155
void reserve(size_t height)
Requests for the capacity of the BatchArray object to be at least enough to contain the number of lin...
Definition BatchArray.h:60
size_t capacity() const
Returns the size of the storage space currently allocated for the BatchArray object,...
Definition BatchArray.h:50
ElementData & at(size_t row, size_t col)
Returns a element reference to the specified row and column index in the BatchArray object.
Definition BatchArray.h:146
const ElementData & operator[](std::size_t n) const
Returns a const element reference to the specified index in the BatchArray object using the operator ...
Definition BatchArray.h:106
~BatchArray()=default
Destructor.
void setHeight(size_t height)
Sets the height of the BatchArray object.
Definition BatchArray.h:39