Zivid C++ API 2.7.0+e31dcbe2-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-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
92
93#include <array>
94#include <exception>
95#include <functional>
96#include <ostream>
97#include <sstream>
98#include <string>
99
100namespace Zivid
101{
105 template<typename T, size_t rowCount, size_t colCount>
106 class Matrix
107 {
108 static_assert(rowCount > 0 && colCount > 0, "rowCount and colCount must be > 0");
109
110 private:
111 using Storage = std::array<T, rowCount * colCount>;
112
113 static constexpr bool invertSupported = ((rowCount == 4 && colCount == 4) || (rowCount == 3 && colCount == 3))
114 && (std::is_same<T, float>::value || std::is_same<T, double>::value);
115
116 public:
120 using ValueType = T;
121
125 using Iterator = typename Storage::iterator;
126
130 using ConstIterator = typename Storage::const_iterator;
131
135 static constexpr size_t rows{ rowCount };
136
140 static constexpr size_t cols{ colCount };
141
143 Matrix() = default;
144
146 explicit Matrix(const std::array<T, colCount * rowCount> &arrArr)
147 : m_mat{ arrArr }
148 {}
149
151 template<typename Iterator>
152 Matrix(Iterator beginIt, Iterator endIt)
153 {
154 if(std::distance(beginIt, endIt) != rows * cols)
155 {
156 throw std::out_of_range(
157 "Input size does not match matrix size. Expected " + std::to_string(rows * cols) + " items, got "
158 + std::to_string(std::distance(beginIt, endIt)) + " items.");
159 }
160
161 std::copy(beginIt, endIt, m_mat.begin());
162 }
163
165 explicit Matrix(std::initializer_list<T> values)
166 : Matrix{ values.begin(), values.end() }
167 {}
168
170 explicit Matrix(std::initializer_list<std::initializer_list<T>> values)
171 {
172 if(values.size() != rows)
173 {
174 throw std::out_of_range(
175 "Input size does not match matrix size. Expected " + std::to_string(rows) + "rows, got "
176 + std::to_string(values.size()) + " rows");
177 }
178
179 auto iterator = begin();
180 for(const auto &row : values)
181 {
182 if(row.size() != cols)
183 {
184 throw std::out_of_range(
185 "Input size does not match matrix size. Expected all rows to be of size " + std::to_string(cols)
186 + ", got row with " + std::to_string(row.size()) + " values");
187 }
188 for(const auto &value : row)
189 {
190 *(iterator++) = value;
191 }
192 }
193 }
194
197 {
198 return m_mat.begin();
199 }
200
203 {
204 return m_mat.end();
205 }
206
209 {
210 return m_mat.begin();
211 }
212
215 {
216 return m_mat.end();
217 }
218
221 {
222 return m_mat.cbegin();
223 }
224
227 {
228 return m_mat.cend();
229 }
230
232 T &at(size_t row, size_t col)
233 {
234 throwIfOutOfBounds(row, col);
235 return m_mat[row * cols + col];
236 }
237
239 const T &at(size_t row, size_t col) const
240 {
241 throwIfOutOfBounds(row, col);
242 return m_mat[row * cols + col];
243 }
244
246 T &operator()(size_t row, size_t col)
247 {
248 return m_mat[row * cols + col];
249 }
250
252 const T &operator()(size_t row, size_t col) const
253 {
254 return m_mat[row * cols + col];
255 }
256
258 T *data()
259 {
260 return m_mat.data();
261 }
262
264 const T *data() const
265 {
266 return m_mat.data();
267 }
268
274 template<typename Q = T, typename = typename std::enable_if<invertSupported, Q>::type>
276
279 std::string toString() const
280 {
281 std::stringstream ss;
282 ss << "[ ";
283 for(size_t row = 0; row < rowCount; row++)
284 {
285 ss << (row == 0 ? "[" : "\n [");
286 for(size_t col = 0; col < colCount; col++)
287 {
288 const auto value{ m_mat[row * colCount + col] };
289 ss << (value >= 0 ? " " : "") << std::to_string(value);
290 ss << (col == colCount - 1 ? "" : ", ");
291 }
292 ss << (row == rowCount - 1 ? "]" : "], ");
293 }
294 ss << " ]";
295 return ss.str();
296 }
297
298 private:
299 static void throwIfOutOfBounds(size_t row, size_t col)
300 {
301 if(row >= rows)
302 {
303 throw std::out_of_range(
304 "Trying to access row with index " + std::to_string(row) + ", but allowed range is [0, "
305 + std::to_string(rows - 1) + "]");
306 }
307 if(col >= cols)
308 {
309 throw std::out_of_range(
310 "Trying to access column with index " + std::to_string(col) + ", but allowed range is [0, "
311 + std::to_string(cols - 1) + "]");
312 }
313 }
314
315 Storage m_mat{};
316 };
317
319 template<typename T, size_t rowCount, size_t colCount>
320 std::ostream &operator<<(std::ostream &stream, const Matrix<T, rowCount, colCount> &matrix)
321 {
322 return stream << matrix.toString();
323 }
324
329
334
339
344
345#ifndef NO_DOC
346
348
350
352
354
355#endif
356
357} // namespace Zivid
#define ZIVID_CORE_EXPORT
Definition: CoreExport.h:101
A fixed size matrix in row major order
Definition: Matrix.h:107
static constexpr size_t rows
The number of rows in the matrix
Definition: Matrix.h:135
T ValueType
The type stored in the matrix
Definition: Matrix.h:120
T * data()
Pointer to the underlying data
Definition: Matrix.h:258
ConstIterator begin() const
Iterator to the beginning of the matrix
Definition: Matrix.h:208
typename Storage::iterator Iterator
The matrix iterator type for mutable access. It iterates over individual matrix elements in row major...
Definition: Matrix.h:125
Matrix(Iterator beginIt, Iterator endIt)
Constructor
Definition: Matrix.h:152
Matrix(std::initializer_list< T > values)
Constructor
Definition: Matrix.h:165
Matrix(const std::array< T, colCount *rowCount > &arrArr)
Constructor
Definition: Matrix.h:146
const T * data() const
Pointer to the underlying data
Definition: Matrix.h:264
static constexpr size_t cols
The number of columns in the matrix
Definition: Matrix.h:140
Iterator begin()
Iterator to the beginning of the matrix
Definition: Matrix.h:196
Matrix()=default
Constructor
ConstIterator cbegin() const
Iterator to the beginning of the matrix
Definition: Matrix.h:220
T & at(size_t row, size_t col)
Access specified element with bounds checking
Definition: Matrix.h:232
std::string toString() const
Get string representation of the Matrix
Definition: Matrix.h:279
ConstIterator end() const
Iterator to the end of the matrix
Definition: Matrix.h:214
Matrix(std::initializer_list< std::initializer_list< T > > values)
Constructor
Definition: Matrix.h:170
const T & at(size_t row, size_t col) const
Access specified element with bounds checking
Definition: Matrix.h:239
const T & operator()(size_t row, size_t col) const
Access specified element without bounds checking
Definition: Matrix.h:252
Iterator end()
Iterator to the end of the matrix
Definition: Matrix.h:202
ZIVID_CORE_EXPORT Matrix inverse() const
Get the inverse of this matrix
ConstIterator cend() const
Iterator to the end of the matrix
Definition: Matrix.h:226
T & operator()(size_t row, size_t col)
Access specified element without bounds checking
Definition: Matrix.h:246
typename Storage::const_iterator ConstIterator
The matrix iterator type for immutable access. It iterates over individual matrix elements in row maj...
Definition: Matrix.h:130
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