Mech-Eye API 2.5.2
API reference documentation for Mech-Eye 3D Laser Profiler
Loading...
Searching...
No Matches
BatchArray.h
1#pragma once
2#include <algorithm>
3#include <cstring>
4#include <memory>
5#include <vector>
6#include <stdexcept>
7
8namespace mmind {
9namespace eye {
10
14template <typename ElementData>
16{
17public:
21 BatchArray(size_t width) : _width(width) {}
25 ~BatchArray() = default;
26
30 size_t width() const { return _width; }
31
35 size_t height() const { return _height; }
36
40 void setHeight(size_t height)
41 {
42 if (height > _capacity) {
44 }
45 _height = height;
46 }
47
52 size_t capacity() const { return _capacity; }
53
57 bool isEmpty() const { return _height == 0; }
58
63 void reserve(size_t height)
64 {
65 if (_capacity >= height)
66 return;
67
68 std::shared_ptr<ElementData> pNewData(new ElementData[_width * height],
69 [](ElementData* p) { delete[] p; });
70 if (_pData) {
71 memcpy(pNewData.get(), _pData.get(), _width * _height * sizeof(ElementData));
72 }
73 _capacity = height;
74 _pData = std::move(pNewData);
75 }
76
80 bool append(const BatchArray& data)
81 {
82 if (_width != data.width())
83 return false;
84
85 if (_capacity - _height < data.height()) {
86 reserve(data.height() + _height);
87 }
88 memcpy(_pData.get() + _height * _width, data.data(),
89 data.height() * data.width() * sizeof(ElementData));
90 _height += data.height();
91 return true;
92 }
93
97 const ElementData* data() const { return _pData.get(); }
98
102 ElementData* data() { return _pData.get(); }
103
110 const ElementData& operator[](std::size_t n) const
111 {
112 if (n >= _height * _width || !_pData)
113 throw std::out_of_range("invalid subscript");
114 ElementData* data = _pData.get();
115 return data[n];
116 }
117
124 ElementData& operator[](std::size_t n)
125 {
126 return const_cast<ElementData&>(static_cast<const BatchArray<ElementData>&>(*this)[n]);
127 }
128
137 const ElementData& at(uint32_t row, uint32_t col) const
138 {
139 if (row >= _height || col >= _width || !_pData)
140 throw std::out_of_range("invalid subscript");
141 ElementData* data = _pData.get();
142 return data[row * _width + col];
143 }
144
153 ElementData& at(size_t row, size_t col)
154 {
155 return const_cast<ElementData&>(
156 static_cast<const BatchArray<ElementData>&>(*this).at(row, col));
157 }
158
163 {
164 BatchArray<ElementData> copy(_width);
165 copy.reserve(_height);
166 memcpy(copy.data(), data(), _height * _width * sizeof(ElementData));
167 copy.setHeight(_height);
168 return copy;
169 }
170
174 void clear()
175 {
176 memset(_pData.get(), 0, _height * _width * sizeof(ElementData));
177 _height = 0;
178 }
179
185 void flip(bool flipX, bool flipY)
186 {
187 auto* data = _pData.get();
188 const size_t height = _height;
189 const size_t width = _width;
190
191 if (flipX) {
192 const size_t rowsToFlip = height / 2;
193 for (size_t i = 0; i < rowsToFlip; ++i) {
194 const size_t mirrorRow = height - 1 - i;
195 auto* rowStart = data + i * width;
196 auto* mirrorRowStart = data + mirrorRow * width;
197 std::swap_ranges(rowStart, rowStart + width, mirrorRowStart);
198 }
199 }
200
201 if (flipY) {
202 for (size_t i = 0; i < height; ++i) {
203 auto* rowStart = data + i * width;
204 std::reverse(rowStart, rowStart + width);
205 }
206 }
207 }
208
209private:
210 size_t _width{0};
211 size_t _height{0};
212 size_t _capacity{0};
213 std::shared_ptr<ElementData> _pData;
214};
215} // namespace eye
216} // namespace mmind
Represents the data struct of the profile data.
Definition BatchArray.h:16
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:137
ElementData & operator[](std::size_t n)
Returns an element reference to the specified index in the BatchArray object using the operator [].
Definition BatchArray.h:124
void clear()
Clears the data of the BatchArray object.
Definition BatchArray.h:174
BatchArray(size_t width)
Describes a constructor.
Definition BatchArray.h:21
ElementData * data()
Returns the pointer to the element data.
Definition BatchArray.h:102
const ElementData * data() const
Returns the pointer to the element data.
Definition BatchArray.h:97
size_t height() const
Returns the height of the BatchArray object.
Definition BatchArray.h:35
bool isEmpty() const
Returns true if the BatchArray object has no elements.
Definition BatchArray.h:57
size_t width() const
Returns the width of the BatchArray object.
Definition BatchArray.h:30
bool append(const BatchArray &data)
Appends the data variable onto the end of this BatchArray object.
Definition BatchArray.h:80
BatchArray< ElementData > clone() const
Creates a deep copy of the BatchArray object.
Definition BatchArray.h:162
void reserve(size_t height)
Requests for enough capacity of the BatchArray object to contain the number of lines corresponding to...
Definition BatchArray.h:63
size_t capacity() const
Returns the size of the storage space currently allocated for the BatchArray object,...
Definition BatchArray.h:52
ElementData & at(size_t row, size_t col)
Returns an element reference to the specified row and column index in the BatchArray object.
Definition BatchArray.h:153
void flip(bool flipX, bool flipY)
Flips the BatchArray object.
Definition BatchArray.h:185
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:110
~BatchArray()=default
Describes a destructor.
void setHeight(size_t height)
Sets the height of the BatchArray object.
Definition BatchArray.h:40