Zivid C++ API  2.3.1+1a22cbf1-1
Defining the Future of 3D Machine Vision
Matrix.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 
47 #include <array>
48 #include <exception>
49 #include <functional>
50 #include <ostream>
51 #include <sstream>
52 #include <string>
53 
54 namespace Zivid
55 {
59  template<typename T, size_t rowCount, size_t colCount>
60  class Matrix
61  {
62  static_assert(rowCount > 0 && colCount > 0, "rowCount and colCount must be > 0");
63 
64  private:
65  using Storage = std::array<T, rowCount * colCount>;
66 
67  public:
71  using ValueType = T;
72 
76  using Iterator = typename Storage::iterator;
77 
81  using ConstIterator = typename Storage::const_iterator;
82 
86  static constexpr size_t rows{ rowCount };
87 
91  static constexpr size_t cols{ colCount };
92 
94  Matrix() = default;
95 
97  explicit Matrix(const std::array<T, colCount * rowCount> &arrArr)
98  : m_mat{ arrArr }
99  {}
100 
102  template<typename Iterator>
103  Matrix(Iterator beginIt, Iterator endIt)
104  {
105  if(std::distance(beginIt, endIt) != rows * cols)
106  {
107  throw std::out_of_range("Input size does not match matrix size. Expected " + std::to_string(rows * cols)
108  + " items, got " + std::to_string(std::distance(beginIt, endIt)) + " items.");
109  }
110 
111  std::copy(beginIt, endIt, m_mat.begin());
112  }
113 
115  explicit Matrix(std::initializer_list<T> values)
116  : Matrix{ values.begin(), values.end() }
117  {}
118 
120  explicit Matrix(std::initializer_list<std::initializer_list<T>> values)
121  {
122  if(values.size() != rows)
123  {
124  throw std::out_of_range("Input size does not match matrix size. Expected " + std::to_string(rows)
125  + "rows, got " + std::to_string(values.size()) + " rows");
126  }
127 
128  auto iterator = begin();
129  for(const auto &row : values)
130  {
131  if(row.size() != cols)
132  {
133  throw std::out_of_range("Input size does not match matrix size. Expected all rows to be of size "
134  + std::to_string(cols) + ", got row with " + std::to_string(row.size())
135  + " values");
136  }
137  for(const auto &value : row)
138  {
139  *(iterator++) = value;
140  }
141  }
142  }
143 
146  {
147  return m_mat.begin();
148  }
149 
152  {
153  return m_mat.end();
154  }
155 
158  {
159  return m_mat.cbegin();
160  }
161 
164  {
165  return m_mat.cend();
166  }
167 
169  T &at(size_t row, size_t col)
170  {
171  throwIfOutOfBounds(row, col);
172  return m_mat[row * cols + col];
173  }
174 
176  const T &at(size_t row, size_t col) const
177  {
178  throwIfOutOfBounds(row, col);
179  return m_mat[row * cols + col];
180  }
181 
183  T &operator()(size_t row, size_t col)
184  {
185  return m_mat[row * cols + col];
186  }
187 
189  const T &operator()(size_t row, size_t col) const
190  {
191  return m_mat[row * cols + col];
192  }
193 
195  T *data()
196  {
197  return m_mat.data();
198  }
199 
201  const T *data() const
202  {
203  return m_mat.data();
204  }
205 
208  std::string toString() const
209  {
210  std::stringstream ss;
211  ss << "[ ";
212  for(size_t row = 0; row < rowCount; row++)
213  {
214  ss << (row == 0 ? "[" : "\n [");
215  for(size_t col = 0; col < colCount; col++)
216  {
217  const auto value{ m_mat[row * colCount + col] };
218  ss << (value >= 0 ? " " : "") << std::to_string(value);
219  ss << (col == colCount - 1 ? "" : ", ");
220  }
221  ss << (row == rowCount - 1 ? "]" : "], ");
222  }
223  ss << " ]";
224  return ss.str();
225  }
226 
227  private:
228  static void throwIfOutOfBounds(size_t row, size_t col)
229  {
230  if(row >= rows)
231  {
232  throw std::out_of_range("Trying to access row with index " + std::to_string(row)
233  + ", but allowed range is [0, " + std::to_string(rows - 1) + "]");
234  }
235  if(col >= cols)
236  {
237  throw std::out_of_range("Trying to access column with index " + std::to_string(col)
238  + ", but allowed range is [0, " + std::to_string(cols - 1) + "]");
239  }
240  }
241 
242  Storage m_mat{};
243  };
244 
246  template<typename T, size_t rowCount, size_t colCount>
247  std::ostream &operator<<(std::ostream &stream, const Matrix<T, rowCount, colCount> &matrix)
248  {
249  return stream << matrix.toString();
250  }
251 
256 
261 
266 
271 } // namespace Zivid
A fixed size matrix in row major order
Definition: Matrix.h:61
static constexpr size_t rows
The number of rows in the matrix
Definition: Matrix.h:86
T ValueType
The type stored in the matrix
Definition: Matrix.h:71
const T & operator()(size_t row, size_t col) const
Access specified element without bounds checking
Definition: Matrix.h:189
T * data()
Pointer to the underlying data
Definition: Matrix.h:195
typename Storage::iterator Iterator
The matrix iterator type for mutable access. It iterates over individual matrix elements in row major...
Definition: Matrix.h:76
Matrix(Iterator beginIt, Iterator endIt)
Constructor
Definition: Matrix.h:103
Matrix(std::initializer_list< T > values)
Constructor
Definition: Matrix.h:115
Matrix(const std::array< T, colCount *rowCount > &arrArr)
Constructor
Definition: Matrix.h:97
static constexpr size_t cols
The number of columns in the matrix
Definition: Matrix.h:91
Iterator begin()
Iterator to the beginning of the matrix
Definition: Matrix.h:145
Matrix()=default
Constructor
ConstIterator cbegin() const
Iterator to the beginning of the matrix
Definition: Matrix.h:157
std::string toString() const
Get string representation of the Matrix
Definition: Matrix.h:208
const T & at(size_t row, size_t col) const
Access specified element with bounds checking
Definition: Matrix.h:176
const T * data() const
Pointer to the underlying data
Definition: Matrix.h:201
Matrix(std::initializer_list< std::initializer_list< T >> values)
Constructor
Definition: Matrix.h:120
T & operator()(size_t row, size_t col)
Access specified element without bounds checking
Definition: Matrix.h:183
Iterator end()
Iterator to the end of the matrix
Definition: Matrix.h:151
ConstIterator cend() const
Iterator to the end of the matrix
Definition: Matrix.h:163
T & at(size_t row, size_t col)
Access specified element with bounds checking
Definition: Matrix.h:169
typename Storage::const_iterator ConstIterator
The matrix iterator type for immutable access. It iterates over individual matrix elements in row maj...
Definition: Matrix.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