Mech-Eye API 2.3.3
API reference documentation for Mech-Eye 3D Laser Profiler
Loading...
Searching...
No Matches
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
51 size_t capacity() const { return _capacity; }
52
56 bool isEmpty() const { return _height == 0; }
57
62 void reserve(size_t height)
63 {
64 if (_capacity >= height)
65 return;
66
67 std::shared_ptr<ElementData> pNewData(new ElementData[_width * height],
68 [](ElementData* p) { delete[] p; });
69 if (_pData) {
70 memcpy(pNewData.get(), _pData.get(), _width * _height * sizeof(ElementData));
71 }
72 _capacity = height;
73 _pData = std::move(pNewData);
74 }
75
79 bool append(const BatchArray& data)
80 {
81 if (_width != data.width())
82 return false;
83
84 if (_capacity - _height < data.height()) {
85 reserve(data.height() + _height);
86 }
87 memcpy(_pData.get() + _height * _width, data.data(),
88 data.height() * data.width() * sizeof(ElementData));
89 _height += data.height();
90 return true;
91 }
92
96 const ElementData* data() const { return _pData.get(); }
97
101 ElementData* data() { return _pData.get(); }
102
109 const ElementData& operator[](std::size_t n) const
110 {
111 if (n >= _height * _width || !_pData)
112 throw std::out_of_range("invalid subscript");
113 ElementData* data = _pData.get();
114 return data[n];
115 }
116
123 ElementData& operator[](std::size_t n)
124 {
125 return const_cast<ElementData&>(static_cast<const BatchArray<ElementData>&>(*this)[n]);
126 }
127
136 const ElementData& at(uint32_t row, uint32_t col) const
137 {
138 if (row >= _height || col >= _width || !_pData)
139 throw std::out_of_range("invalid subscript");
140 ElementData* data = _pData.get();
141 return data[row * _width + col];
142 }
143
152 ElementData& at(size_t row, size_t col)
153 {
154 return const_cast<ElementData&>(
155 static_cast<const BatchArray<ElementData>&>(*this).at(row, col));
156 }
157
162 {
163 BatchArray<ElementData> copy(_width);
164 copy.reserve(_height);
165 memcpy(copy.data(), data(), _height * _width * sizeof(ElementData));
166 copy.setHeight(_height);
167 return copy;
168 }
169
173 void clear()
174 {
175 memset(_pData.get(), 0, _height * _width * sizeof(ElementData));
176 _height = 0;
177 }
178
179private:
180 size_t _width{0};
181 size_t _height{0};
182 size_t _capacity{0};
183 std::shared_ptr<ElementData> _pData;
184};
185} // namespace eye
186} // namespace mmind
Represents the data struct 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:136
ElementData & operator[](std::size_t n)
Returns an element reference to the specified index in the BatchArray object using the operator [].
Definition BatchArray.h:123
void clear()
Clears the data of the BatchArray object.
Definition BatchArray.h:173
BatchArray(size_t width)
Describes a constructor.
Definition BatchArray.h:20
ElementData * data()
Returns the pointer to the element data.
Definition BatchArray.h:101
const ElementData * data() const
Returns the pointer to the element data.
Definition BatchArray.h:96
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:56
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:79
BatchArray< ElementData > clone() const
Creates a deep copy of the BatchArray object.
Definition BatchArray.h:161
void reserve(size_t height)
Requests for enough capacity of the BatchArray object to contain the number of lines corresponding to...
Definition BatchArray.h:62
size_t capacity() const
Returns the size of the storage space currently allocated for the BatchArray object,...
Definition BatchArray.h:51
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:152
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:109
~BatchArray()=default
Describes a destructor.
void setHeight(size_t height)
Sets the height of the BatchArray object.
Definition BatchArray.h:39