Zivid C++ API 2.6.1+6cec8609-3
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-2022 (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
98namespace 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(
152 "Input size does not match matrix size. Expected " + std::to_string(rows * cols) + " items, got "
153 + std::to_string(std::distance(beginIt, endIt)) + " items.");
154 }
155
156 std::copy(beginIt, endIt, m_mat.begin());
157 }
158
160 explicit Matrix(std::initializer_list<T> values)
161 : Matrix{ values.begin(), values.end() }
162 {}
163
165 explicit Matrix(std::initializer_list<std::initializer_list<T>> values)
166 {
167 if(values.size() != rows)
168 {
169 throw std::out_of_range(
170 "Input size does not match matrix size. Expected " + std::to_string(rows) + "rows, got "
171 + std::to_string(values.size()) + " rows");
172 }
173
174 auto iterator = begin();
175 for(const auto &row : values)
176 {
177 if(row.size() != cols)
178 {
179 throw std::out_of_range(
180 "Input size does not match matrix size. Expected all rows to be of size " + std::to_string(cols)
181 + ", got row with " + std::to_string(row.size()) + " values");
182 }
183 for(const auto &value : row)
184 {
185 *(iterator++) = value;
186 }
187 }
188 }
189
192 {
193 return m_mat.begin();
194 }
195
198 {
199 return m_mat.end();
200 }
201
204 {
205 return m_mat.cbegin();
206 }
207
210 {
211 return m_mat.cend();
212 }
213
215 T &at(size_t row, size_t col)
216 {
217 throwIfOutOfBounds(row, col);
218 return m_mat[row * cols + col];
219 }
220
222 const T &at(size_t row, size_t col) const
223 {
224 throwIfOutOfBounds(row, col);
225 return m_mat[row * cols + col];
226 }
227
229 T &operator()(size_t row, size_t col)
230 {
231 return m_mat[row * cols + col];
232 }
233
235 const T &operator()(size_t row, size_t col) const
236 {
237 return m_mat[row * cols + col];
238 }
239
241 T *data()
242 {
243 return m_mat.data();
244 }
245
247 const T *data() const
248 {
249 return m_mat.data();
250 }
251
254 std::string toString() const
255 {
256 std::stringstream ss;
257 ss << "[ ";
258 for(size_t row = 0; row < rowCount; row++)
259 {
260 ss << (row == 0 ? "[" : "\n [");
261 for(size_t col = 0; col < colCount; col++)
262 {
263 const auto value{ m_mat[row * colCount + col] };
264 ss << (value >= 0 ? " " : "") << std::to_string(value);
265 ss << (col == colCount - 1 ? "" : ", ");
266 }
267 ss << (row == rowCount - 1 ? "]" : "], ");
268 }
269 ss << " ]";
270 return ss.str();
271 }
272
273 private:
274 static void throwIfOutOfBounds(size_t row, size_t col)
275 {
276 if(row >= rows)
277 {
278 throw std::out_of_range(
279 "Trying to access row with index " + std::to_string(row) + ", but allowed range is [0, "
280 + std::to_string(rows - 1) + "]");
281 }
282 if(col >= cols)
283 {
284 throw std::out_of_range(
285 "Trying to access column with index " + std::to_string(col) + ", but allowed range is [0, "
286 + std::to_string(cols - 1) + "]");
287 }
288 }
289
290 Storage m_mat{};
291 };
292
294 template<typename T, size_t rowCount, size_t colCount>
295 std::ostream &operator<<(std::ostream &stream, const Matrix<T, rowCount, colCount> &matrix)
296 {
297 return stream << matrix.toString();
298 }
299
304
309
314
319} // 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
T * data()
Pointer to the underlying data
Definition: Matrix.h:241
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:160
Matrix(const std::array< T, colCount *rowCount > &arrArr)
Constructor
Definition: Matrix.h:141
const T * data() const
Pointer to the underlying data
Definition: Matrix.h:247
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:191
Matrix()=default
Constructor
ConstIterator cbegin() const
Iterator to the beginning of the matrix
Definition: Matrix.h:203
T & at(size_t row, size_t col)
Access specified element with bounds checking
Definition: Matrix.h:215
std::string toString() const
Get string representation of the Matrix
Definition: Matrix.h:254
Matrix(std::initializer_list< std::initializer_list< T > > values)
Constructor
Definition: Matrix.h:165
const T & at(size_t row, size_t col) const
Access specified element with bounds checking
Definition: Matrix.h:222
const T & operator()(size_t row, size_t col) const
Access specified element without bounds checking
Definition: Matrix.h:235
Iterator end()
Iterator to the end of the matrix
Definition: Matrix.h:197
ConstIterator cend() const
Iterator to the end of the matrix
Definition: Matrix.h:209
T & operator()(size_t row, size_t col)
Access specified element without bounds checking
Definition: Matrix.h:229
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