Zivid C++ API  2.4.2+1a2e8cfb-1
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-2021 (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 
93 #include <memory>
94 #include <string>
95 #include <type_traits>
96 
97 namespace Zivid
98 {
99 #ifndef NO_DOC
101  namespace Detail
102  {
103  ZIVID_CORE_EXPORT std::string array2DToString(std::size_t width, std::size_t height);
104  } // namespace Detail
105 #endif
106 
117  template<typename DataFormat>
118  class Array2D
119  {
120  public:
122  using ValueType = DataFormat;
123 
126  : m_width{ 0 }
127  , m_height{ 0 }
128  {}
129 
131  size_t width() const
132  {
133  return m_width;
134  }
135 
137  size_t height() const
138  {
139  return m_height;
140  }
141 
144  size_t size() const
145  {
146  return width() * height();
147  }
148 
150  bool isEmpty() const
151  {
152  return size() == 0;
153  }
154 
157  const DataFormat *data() const
158  {
159  return m_data.get();
160  }
161 
169  const DataFormat &operator()(size_t idx) const
170  {
171  return *(data() + idx);
172  }
173 
182  const DataFormat &operator()(size_t i, size_t j) const
183  {
184  return operator()(width() * i + j);
185  }
186 
188  std::string toString() const
189  {
190  return Detail::array2DToString(width(), height());
191  }
192 
193  private:
194  friend class Array2DFactory;
195 
196  Array2D(std::size_t width, std::size_t height, std::unique_ptr<DataFormat[]> data)
197  : m_width{ width }
198  , m_height{ height }
199  , m_data{ // Due to poor support for shared_ptr array types in C++11 the data
200  // ownership is transferred from the std::unique_ptr to a std::shared_ptr
201  // of non-array type with a specified array deleter.
202  data.release(),
203  std::default_delete<DataFormat[]>()
204  }
205  {}
206 
207  std::size_t m_width;
208  std::size_t m_height;
209  std::shared_ptr<DataFormat> m_data;
210  };
211 
213  template<typename T>
214  std::ostream &operator<<(std::ostream &stream, const Array2D<T> &array)
215  {
216  return stream << array.toString();
217  }
218 } // namespace Zivid
#define ZIVID_CORE_EXPORT
Definition: CoreExport.h:101
Two-dimensional container of data
Definition: Array2D.h:119
const DataFormat * data() const
Pointer to the first data element of the array
Definition: Array2D.h:157
friend class Array2DFactory
Definition: Array2D.h:194
size_t height() const
Get the height of the array (number of rows)
Definition: Array2D.h:137
const DataFormat & operator()(size_t i, size_t j) const
Constant reference to an element given by row and column
Definition: Array2D.h:182
bool isEmpty() const
Check if the array is empty
Definition: Array2D.h:150
const DataFormat & operator()(size_t idx) const
Constant reference to an element given by a 1D linear index
Definition: Array2D.h:169
std::string toString() const
Get array information as string
Definition: Array2D.h:188
DataFormat ValueType
The type of the elements stored in the Array2D
Definition: Array2D.h:122
size_t size() const
Get the number of elements in the array
Definition: Array2D.h:144
size_t width() const
Get the width of the array (number of columns)
Definition: Array2D.h:131
Array2D()
Create an empty Array2D
Definition: Array2D.h:125
The main Zivid namespace. All Zivid code is found here
Definition: Application.h:99
std::ostream & operator<<(std::ostream &stream, const Array2D< T > &array)
Serialize array information to a stream
Definition: Array2D.h:214