Zivid C++ API  2.3.1+1a22cbf1-1
Defining the Future of 3D Machine Vision
Array2D.h
Go to the documentation of this file.
1 
2 /*******************************************************************************
3  * This file is part of the Zivid 3D Camera API
4  *
5  * Copyright 2015-2022 (C) Zivid AS
6  * All rights reserved.
7  *
8  * Zivid Software License, v1.0
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright notice,
14  * this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright notice,
17  * this list of conditions and the following disclaimer in the documentation
18  * and/or other materials provided with the distribution.
19  *
20  * 3. Neither the name of Zivid AS nor the names of its contributors may be used
21  * to endorse or promote products derived from this software without specific
22  * prior written permission.
23  *
24  * 4. This software, with or without modification, must not be used with any
25  * other 3D camera than from Zivid AS.
26  *
27  * 5. Any software provided in binary form under this license must not be
28  * reverse engineered, decompiled, modified and/or disassembled.
29  *
30  * THIS SOFTWARE IS PROVIDED BY ZIVID AS "AS IS" AND ANY EXPRESS OR IMPLIED
31  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
32  * MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
33  * DISCLAIMED. IN NO EVENT SHALL ZIVID AS OR CONTRIBUTORS BE LIABLE FOR ANY
34  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
37  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Contact: Zivid Customer Success Team <customersuccess@zivid.com>
42  * Info: http://www.zivid.com
43  ******************************************************************************/
44 
45 #pragma once
46 
48 
49 #include <memory>
50 #include <string>
51 #include <type_traits>
52 
53 namespace Zivid
54 {
55 #ifndef NO_DOC
57  namespace Detail
58  {
59  ZIVID_CORE_EXPORT std::string array2DToString(std::size_t width, std::size_t height);
60  } // namespace Detail
61 #endif
62 
73  template<typename DataFormat>
74  class Array2D
75  {
76  public:
78  using ValueType = DataFormat;
79 
82  : m_width{ 0 }
83  , m_height{ 0 }
84  {}
85 
87  size_t width() const
88  {
89  return m_width;
90  }
91 
93  size_t height() const
94  {
95  return m_height;
96  }
97 
100  size_t size() const
101  {
102  return width() * height();
103  }
104 
106  bool isEmpty() const
107  {
108  return size() == 0;
109  }
110 
113  const DataFormat *data() const
114  {
115  return m_data.get();
116  }
117 
125  const DataFormat &operator()(size_t idx) const
126  {
127  return *(data() + idx);
128  }
129 
138  const DataFormat &operator()(size_t i, size_t j) const
139  {
140  return operator()(width() * i + j);
141  }
142 
144  std::string toString() const
145  {
146  return Detail::array2DToString(width(), height());
147  }
148 
149  private:
150  friend class Array2DFactory;
151 
152  Array2D(std::size_t width, std::size_t height, std::unique_ptr<DataFormat[]> data)
153  : m_width{ width }
154  , m_height{ height }
155  , m_data{ // Due to poor support for shared_ptr array types in C++11 the data
156  // ownership is transferred from the std::unique_ptr to a std::shared_ptr
157  // of non-array type with a specified array deleter.
158  data.release(),
159  std::default_delete<DataFormat[]>()
160  }
161  {}
162 
163  std::size_t m_width;
164  std::size_t m_height;
165  std::shared_ptr<DataFormat> m_data;
166  };
167 
169  template<typename T>
170  std::ostream &operator<<(std::ostream &stream, const Array2D<T> &array)
171  {
172  return stream << array.toString();
173  }
174 } // namespace Zivid
#define ZIVID_CORE_EXPORT
Definition: CoreExport.h:57
Two-dimensional container of data
Definition: Array2D.h:75
const DataFormat * data() const
Pointer to the first data element of the array
Definition: Array2D.h:113
friend class Array2DFactory
Definition: Array2D.h:150
size_t height() const
Get the height of the array (number of rows)
Definition: Array2D.h:93
const DataFormat & operator()(size_t i, size_t j) const
Constant reference to an element given by row and column
Definition: Array2D.h:138
bool isEmpty() const
Check if the array is empty
Definition: Array2D.h:106
const DataFormat & operator()(size_t idx) const
Constant reference to an element given by a 1D linear index
Definition: Array2D.h:125
std::string toString() const
Get array information as string
Definition: Array2D.h:144
DataFormat ValueType
The type of the elements stored in the Array2D
Definition: Array2D.h:78
size_t size() const
Get the number of elements in the array
Definition: Array2D.h:100
size_t width() const
Get the width of the array (number of columns)
Definition: Array2D.h:87
Array2D()
Create an empty Array2D
Definition: Array2D.h:81
The main Zivid namespace. All Zivid code is found here
Definition: Application.h:55
std::ostream & operator<<(std::ostream &stream, const Array2D< T > &array)
Serialize array information to a stream
Definition: Array2D.h:170