Zivid C++ API  2.4.2+1a2e8cfb-1
Defining the Future of 3D Machine Vision
Matrix.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 
91 #include <array>
92 #include <exception>
93 #include <functional>
94 #include <ostream>
95 #include <sstream>
96 #include <string>
97 
98 namespace Zivid
99 {
103  template<typename T, size_t rowCount, size_t colCount>
104  class Matrix
105  {
106  static_assert(rowCount > 0 && colCount > 0, "rowCount and colCount must be > 0");
107 
108  private:
109  using Storage = std::array<T, rowCount * colCount>;
110 
111  public:
115  using ValueType = T;
116 
120  using Iterator = typename Storage::iterator;
121 
125  using ConstIterator = typename Storage::const_iterator;
126 
130  static constexpr size_t rows{ rowCount };
131 
135  static constexpr size_t cols{ colCount };
136 
138  Matrix() = default;
139 
141  explicit Matrix(const std::array<T, colCount * rowCount> &arrArr)
142  : m_mat{ arrArr }
143  {}
144 
146  template<typename Iterator>
147  Matrix(Iterator beginIt, Iterator endIt)
148  {
149  if(std::distance(beginIt, endIt) != rows * cols)
150  {
151  throw std::out_of_range("Input size does not match matrix size. Expected " + std::to_string(rows * cols)
152  + " items, got " + std::to_string(std::distance(beginIt, endIt)) + " items.");
153  }
154 
155  std::copy(beginIt, endIt, m_mat.begin());
156  }
157 
159  explicit Matrix(std::initializer_list<T> values)
160  : Matrix{ values.begin(), values.end() }
161  {}
162 
164  explicit Matrix(std::initializer_list<std::initializer_list<T>> values)
165  {
166  if(values.size() != rows)
167  {
168  throw std::out_of_range("Input size does not match matrix size. Expected " + std::to_string(rows)
169  + "rows, got " + std::to_string(values.size()) + " rows");
170  }
171 
172  auto iterator = begin();
173  for(const auto &row : values)
174  {
175  if(row.size() != cols)
176  {
177  throw std::out_of_range("Input size does not match matrix size. Expected all rows to be of size "
178  + std::to_string(cols) + ", got row with " + std::to_string(row.size())
179  + " values");
180  }
181  for(const auto &value : row)
182  {
183  *(iterator++) = value;
184  }
185  }
186  }
187 
190  {
191  return m_mat.begin();
192  }
193 
196  {
197  return m_mat.end();
198  }
199 
202  {
203  return m_mat.cbegin();
204  }
205 
208  {
209  return m_mat.cend();
210  }
211 
213  T &at(size_t row, size_t col)
214  {
215  throwIfOutOfBounds(row, col);
216  return m_mat[row * cols + col];
217  }
218 
220  const T &at(size_t row, size_t col) const
221  {
222  throwIfOutOfBounds(row, col);
223  return m_mat[row * cols + col];
224  }
225 
227  T &operator()(size_t row, size_t col)
228  {
229  return m_mat[row * cols + col];
230  }
231 
233  const T &operator()(size_t row, size_t col) const
234  {
235  return m_mat[row * cols + col];
236  }
237 
239  T *data()
240  {
241  return m_mat.data();
242  }
243 
245  const T *data() const
246  {
247  return m_mat.data();
248  }
249 
252  std::string toString() const
253  {
254  std::stringstream ss;
255  ss << "[ ";
256  for(size_t row = 0; row < rowCount; row++)
257  {
258  ss << (row == 0 ? "[" : "\n [");
259  for(size_t col = 0; col < colCount; col++)
260  {
261  const auto value{ m_mat[row * colCount + col] };
262  ss << (value >= 0 ? " " : "") << std::to_string(value);
263  ss << (col == colCount - 1 ? "" : ", ");
264  }
265  ss << (row == rowCount - 1 ? "]" : "], ");
266  }
267  ss << " ]";
268  return ss.str();
269  }
270 
271  private:
272  static void throwIfOutOfBounds(size_t row, size_t col)
273  {
274  if(row >= rows)
275  {
276  throw std::out_of_range("Trying to access row with index " + std::to_string(row)
277  + ", but allowed range is [0, " + std::to_string(rows - 1) + "]");
278  }
279  if(col >= cols)
280  {
281  throw std::out_of_range("Trying to access column with index " + std::to_string(col)
282  + ", but allowed range is [0, " + std::to_string(cols - 1) + "]");
283  }
284  }
285 
286  Storage m_mat{};
287  };
288 
290  template<typename T, size_t rowCount, size_t colCount>
291  std::ostream &operator<<(std::ostream &stream, const Matrix<T, rowCount, colCount> &matrix)
292  {
293  return stream << matrix.toString();
294  }
295 
300 
305 
310 
315 } // namespace Zivid
A fixed size matrix in row major order
Definition: Matrix.h:105
static constexpr size_t rows
The number of rows in the matrix
Definition: Matrix.h:130
T ValueType
The type stored in the matrix
Definition: Matrix.h:115
const T & operator()(size_t row, size_t col) const
Access specified element without bounds checking
Definition: Matrix.h:233
T * data()
Pointer to the underlying data
Definition: Matrix.h:239
typename Storage::iterator Iterator
The matrix iterator type for mutable access. It iterates over individual matrix elements in row major...
Definition: Matrix.h:120
Matrix(Iterator beginIt, Iterator endIt)
Constructor
Definition: Matrix.h:147
Matrix(std::initializer_list< T > values)
Constructor
Definition: Matrix.h:159
Matrix(const std::array< T, colCount *rowCount > &arrArr)
Constructor
Definition: Matrix.h:141
static constexpr size_t cols
The number of columns in the matrix
Definition: Matrix.h:135
Iterator begin()
Iterator to the beginning of the matrix
Definition: Matrix.h:189
Matrix()=default
Constructor
ConstIterator cbegin() const
Iterator to the beginning of the matrix
Definition: Matrix.h:201
std::string toString() const
Get string representation of the Matrix
Definition: Matrix.h:252
const T & at(size_t row, size_t col) const
Access specified element with bounds checking
Definition: Matrix.h:220
const T * data() const
Pointer to the underlying data
Definition: Matrix.h:245
Matrix(std::initializer_list< std::initializer_list< T >> values)
Constructor
Definition: Matrix.h:164
T & operator()(size_t row, size_t col)
Access specified element without bounds checking
Definition: Matrix.h:227
Iterator end()
Iterator to the end of the matrix
Definition: Matrix.h:195
ConstIterator cend() const
Iterator to the end of the matrix
Definition: Matrix.h:207
T & at(size_t row, size_t col)
Access specified element with bounds checking
Definition: Matrix.h:213
typename Storage::const_iterator ConstIterator
The matrix iterator type for immutable access. It iterates over individual matrix elements in row maj...
Definition: Matrix.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