Zivid C++ API  1.8.1+6967bc1b-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-2020 (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 Support <support@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 
145  Iterator begin()
146  {
147  return m_mat.begin();
148  }
149 
151  Iterator end()
152  {
153  return m_mat.end();
154  }
155 
157  ConstIterator cbegin() const
158  {
159  return m_mat.cbegin();
160  }
161 
163  ConstIterator cend() const
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 
196  std::string toString() const
197  {
198  std::stringstream ss;
199  ss << "[ ";
200  for(size_t row = 0; row < rowCount; row++)
201  {
202  ss << (row == 0 ? "[" : "\n [");
203  for(size_t col = 0; col < colCount; col++)
204  {
205  const auto value{ m_mat[row * colCount + col] };
206  ss << (value >= 0 ? " " : "") << std::to_string(value);
207  ss << (col == colCount - 1 ? "" : ", ");
208  }
209  ss << (row == rowCount - 1 ? "]" : "], ");
210  }
211  ss << " ]";
212  return ss.str();
213  }
214 
215  private:
216  static void throwIfOutOfBounds(size_t row, size_t col)
217  {
218  if(row >= rows)
219  {
220  throw std::out_of_range("Trying to access row with index " + std::to_string(row)
221  + ", but allowed range is [0, " + std::to_string(rows - 1) + "]");
222  }
223  if(col >= cols)
224  {
225  throw std::out_of_range("Trying to access column with index " + std::to_string(col)
226  + ", but allowed range is [0, " + std::to_string(cols - 1) + "]");
227  }
228  }
229 
230  Storage m_mat{};
231  };
232 
234  template<typename T, size_t rowCount, size_t colCount>
235  std::ostream &operator<<(std::ostream &stream, const Matrix<T, rowCount, colCount> &matrix)
236  {
237  return stream << matrix.toString();
238  }
239 
244 
249 
254 
259 } // namespace Zivid
Zivid::Matrix::end
Iterator end()
Iterator to the end of the matrix
Definition: Matrix.h:230
Zivid::Matrix
A fixed size matrix in row major order
Definition: Matrix.h:99
Zivid::Matrix::Iterator
typename Storage::iterator Iterator
The matrix iterator type for mutable access. It iterates over individual matrix elements in row major...
Definition: Matrix.h:155
Zivid::Matrix::operator()
T & operator()(size_t row, size_t col)
Access specified element without bounds checking
Definition: Matrix.h:262
Zivid::Matrix::toString
std::string toString() const
Get string representation of the Matrix
Definition: Matrix.h:275
Zivid::Matrix::cend
ConstIterator cend() const
Iterator to the end of the matrix
Definition: Matrix.h:242
Zivid::Matrix::cols
static constexpr size_t cols
The number of columns in the matrix
Definition: Matrix.h:170
Zivid::Matrix::Matrix
Matrix()=default
Constructor
Zivid::Matrix::ConstIterator
typename Storage::const_iterator ConstIterator
The matrix iterator type for immutable access. It iterates over individual matrix elements in row maj...
Definition: Matrix.h:160
Zivid::Matrix::at
T & at(size_t row, size_t col)
Access specified element with bounds checking
Definition: Matrix.h:248
Zivid::Matrix::begin
Iterator begin()
Iterator to the beginning of the matrix
Definition: Matrix.h:224
Zivid
The main Zivid namespace. All Zivid code is found here
Definition: Application.h:52
Zivid::Matrix::cbegin
ConstIterator cbegin() const
Iterator to the beginning of the matrix
Definition: Matrix.h:236
Zivid::Matrix::rows
static constexpr size_t rows
The number of rows in the matrix
Definition: Matrix.h:165
Zivid::operator<<
ZIVID_API_EXPORT std::ostream & operator<<(std::ostream &stream, const Camera &camera)
Serialize the value to a stream
Zivid::Matrix::ValueType
T ValueType
The type stored in the matrix
Definition: Matrix.h:150