Zivid C++ API 2.10.1+50b274e8-7
Defining the Future of 3D Machine Vision
Array2D.h
Go to the documentation of this file.
1
2
3/*******************************************************************************
4
5 * This file is part of the Zivid 3D Camera API
6
7 *
8
9 * Copyright 2015-2023 (C) Zivid AS
10
11 * All rights reserved.
12
13 *
14
15 * Zivid Software License, v1.0
16
17 *
18
19 * Redistribution and use in source and binary forms, with or without
20
21 * modification, are permitted provided that the following conditions are met:
22
23 *
24
25 * 1. Redistributions of source code must retain the above copyright notice,
26
27 * this list of conditions and the following disclaimer.
28
29 *
30
31 * 2. Redistributions in binary form must reproduce the above copyright notice,
32
33 * this list of conditions and the following disclaimer in the documentation
34
35 * and/or other materials provided with the distribution.
36
37 *
38
39 * 3. Neither the name of Zivid AS nor the names of its contributors may be used
40
41 * to endorse or promote products derived from this software without specific
42
43 * prior written permission.
44
45 *
46
47 * 4. This software, with or without modification, must not be used with any
48
49 * other 3D camera than from Zivid AS.
50
51 *
52
53 * 5. Any software provided in binary form under this license must not be
54
55 * reverse engineered, decompiled, modified and/or disassembled.
56
57 *
58
59 * THIS SOFTWARE IS PROVIDED BY ZIVID AS "AS IS" AND ANY EXPRESS OR IMPLIED
60
61 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
62
63 * MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
64
65 * DISCLAIMED. IN NO EVENT SHALL ZIVID AS OR CONTRIBUTORS BE LIABLE FOR ANY
66
67 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
68
69 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
70
71 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
72
73 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
74
75 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
76
77 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
78
79 *
80
81 * Contact: Zivid Customer Success Team <customersuccess@zivid.com>
82
83 * Info: http://www.zivid.com
84
85 ******************************************************************************/
86
87
88
89#pragma once
90
92#include "Zivid/Resolution.h"
93
94#include <cstddef>
95#include <cstring>
96#include <iterator>
97#include <memory>
98#include <sstream>
99#include <stdexcept>
100#include <string>
101#include <type_traits>
102
103namespace Zivid
104{
105 template<class PixelFormat>
106 class Image;
107
108#ifndef NO_DOC
110 namespace Detail
111 {
112 ZIVID_CORE_EXPORT std::string array2DToString(std::size_t width, std::size_t height);
113 } // namespace Detail
114#endif
115
126 template<typename DataFormat>
128 {
129 public:
131 using ValueType = DataFormat;
132
136 using ConstIterator = const DataFormat *;
137
140 : m_width{ 0 }
141 , m_height{ 0 }
142 {}
143
145 size_t width() const
146 {
147 return m_width;
148 }
149
151 size_t height() const
152 {
153 return m_height;
154 }
155
158 size_t size() const
159 {
160 return width() * height();
161 }
162
164 bool isEmpty() const
165 {
166 return size() == 0;
167 }
168
171 const DataFormat *data() const
172 {
173 return m_data.get();
174 }
175
183 const DataFormat &operator()(size_t idx) const
184 {
185 return *(data() + idx);
186 }
187
196 const DataFormat &operator()(size_t i, size_t j) const
197 {
198 return operator()(width() * i + j);
199 }
200
203 {
204 return data();
205 }
206
209 {
210 return data() + size();
211 }
212
215 {
216 return data();
217 }
218
221 {
222 return data() + size();
223 }
224
226 std::string toString() const
227 {
228 return Detail::array2DToString(width(), height());
229 }
230
231 private:
232 template<
233 typename Iterator,
234 typename T = DataFormat,
235 typename std::enable_if<
236 std::is_convertible<typename std::iterator_traits<Iterator>::value_type, T>::value,
237 std::nullptr_t>::type = nullptr>
238 Array2D(const Resolution &resolution, Iterator beginIt, Iterator endIt)
239 : m_width{ resolution.width() }
240 , m_height{ resolution.height() }
241 , m_data{ [&] {
242 const auto inputSize = std::distance(beginIt, endIt);
243 if(inputSize != static_cast<std::ptrdiff_t>(resolution.size()))
244 {
245 /* NOLINT */ throw std::out_of_range(
246 "Array2D constructor got input that does not match image size. Expected "
247 + std::to_string(resolution.size()) + " items, got " + std::to_string(inputSize)
248 + " items.");
249 }
250 std::unique_ptr<DataFormat[]> data{ new DataFormat[resolution.size()] };
251 std::copy(beginIt, endIt, data.get());
252 return data.release();
253 }(),
254 std::default_delete<DataFormat[]>() }
255 {}
256
257 template<
258 typename T = DataFormat,
259 typename std::enable_if<std::is_trivial<T>::value, std::nullptr_t>::type = nullptr>
260 Array2D(const Resolution &resolution, const unsigned char *begin, const unsigned char *end)
261 : m_width{ resolution.width() }
262 , m_height{ resolution.height() }
263 {
264 const auto byteCount = std::distance(begin, end);
265 {
266 const auto expectedByteCount = static_cast<std::ptrdiff_t>(resolution.size() * sizeof(DataFormat));
267 if(expectedByteCount != byteCount)
268 {
269 std::ostringstream errorMessage;
270 errorMessage
271 << "The provided input buffer size does not match the expected size. The provided input buffer size is "
272 << byteCount << " bytes. Expected " << expectedByteCount << " bytes (" << resolution.width()
273 << " x " << resolution.height() << " x " << sizeof(DataFormat) << " bytes).";
274 /* NOLINT */ throw std::logic_error(std::move(errorMessage).str());
275 }
276 }
277 std::shared_ptr<DataFormat> data(new DataFormat[resolution.size()], std::default_delete<DataFormat[]>{});
278
279 if(resolution.size() != 0) // To handle the case where resolution is zero and begin == end == nullptr.
280 {
281 std::memcpy(data.get(), begin, byteCount);
282 }
283
284 m_data = std::move(data);
285 }
286
287 friend class Array2DFactory;
288 friend class Image<DataFormat>;
289
290 Array2D(std::size_t width, std::size_t height, std::unique_ptr<DataFormat[]> data)
291 : m_width{ width }
292 , m_height{ height }
293 , m_data{ // Due to poor support for shared_ptr array types in C++11 the data
294 // ownership is transferred from the std::unique_ptr to a std::shared_ptr
295 // of non-array type with a specified array deleter.
296 data.release(),
297 std::default_delete<DataFormat[]>()
298 }
299 {}
300
301 std::size_t m_width;
302 std::size_t m_height;
303 std::shared_ptr<const DataFormat> m_data;
304 };
305
307 template<typename T>
308 std::ostream &operator<<(std::ostream &stream, const Array2D<T> &array)
309 {
310 return stream << array.toString();
311 }
312
314 template<typename T>
316 {
317 return array.cbegin();
318 }
319
321 template<typename T>
323 {
324 return array.begin();
325 }
326
328 template<typename T>
330 {
331 return array.cend();
332 }
333
335 template<typename T>
337 {
338 return array.end();
339 }
340} // namespace Zivid
#define ZIVID_CORE_EXPORT
Definition: CoreExport.h:101
Two-dimensional container of data
Definition: Array2D.h:128
ConstIterator cbegin() const
Iterator to the beginning of the array
Definition: Array2D.h:214
const DataFormat * ConstIterator
The iterator type for immutable access. It iterates over individual Array2D elements in row major ord...
Definition: Array2D.h:136
const DataFormat & operator()(size_t i, size_t j) const
Constant reference to an element given by row and column
Definition: Array2D.h:196
ConstIterator cend() const
Iterator to the end of the array
Definition: Array2D.h:220
const DataFormat & operator()(size_t idx) const
Constant reference to an element given by a 1D linear index
Definition: Array2D.h:183
size_t height() const
Get the height of the array (number of rows)
Definition: Array2D.h:151
const DataFormat * data() const
Pointer to the first data element of the array
Definition: Array2D.h:171
bool isEmpty() const
Check if the array is empty
Definition: Array2D.h:164
std::string toString() const
Get array information as string
Definition: Array2D.h:226
ConstIterator end() const
Iterator to the end of the array
Definition: Array2D.h:208
DataFormat ValueType
The type of the elements stored in the Array2D
Definition: Array2D.h:131
size_t size() const
Get the number of elements in the array
Definition: Array2D.h:158
size_t width() const
Get the width of the array (number of columns)
Definition: Array2D.h:145
ConstIterator begin() const
Iterator to the beginning of the array
Definition: Array2D.h:202
Array2D()
Create an empty Array2D
Definition: Array2D.h:139
A 2-dimensional image
Definition: Image.h:115
Class describing a given resolution
Definition: Resolution.h:101
ZIVID_CORE_EXPORT size_t size() const
Get the size value of the resolution
ZIVID_CORE_EXPORT size_t height() const
Get the height value of the resolution
ZIVID_CORE_EXPORT size_t width() const
Get the width value of the resolution
ZIVID_CORE_EXPORT Resolution resolution(const CameraInfo &cameraInfo, const Settings &settings)
The main Zivid namespace. All Zivid code is found here
Definition: Application.h:99
Array2D< T >::ConstIterator end(const Array2D< T > &array)
Iterator to the end of the array
Definition: Array2D.h:336
Array2D< T >::ConstIterator cbegin(const Array2D< T > &array)
Iterator to the beginning of the array
Definition: Array2D.h:315
std::ostream & operator<<(std::ostream &stream, const Array2D< T > &array)
Serialize array information to a stream
Definition: Array2D.h:308
Array2D< T >::ConstIterator cend(const Array2D< T > &array)
Iterator to the end of the array
Definition: Array2D.h:329
Array2D< T >::ConstIterator begin(const Array2D< T > &array)
Iterator to the beginning of the array
Definition: Array2D.h:322