MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
mlFloatingPointMatrix.h
Go to the documentation of this file.
1/*************************************************************************************
2**
3** Copyright 2012, MeVis Medical Solutions AG
4**
5** The user may use this file in accordance with the license agreement provided with
6** the Software or, alternatively, in accordance with the terms contained in a
7** written agreement between the user and MeVis Medical Solutions AG.
8**
9** For further information use the contact form at https://www.mevislab.de/contact
10**
11**************************************************************************************/
12
13#ifndef ML_FLOATING_POINT_MATRIX_H
14#define ML_FLOATING_POINT_MATRIX_H
15
17
18//------------------------------------------------------------------------
19// Includes
20//----------------------------------------------------------------------
22
24
25//------------------------------------------------------------------------
26// Put all code in a specific ML_LA_NAMESPACE namespace to avoid
27// collisions with other libraries.
28//------------------------------------------------------------------------
30
33template <class VectorT, size_t size> class FloatingPointMatrix
34{
35public:
37 typedef typename VectorT::ComponentType ComponentType;
38
41
44 enum {
45 RowCount = size
46 };
47
50 enum {
51 ColumnCount = VectorT::Size
52 };
53
56 enum {
57 ComponentCount = static_cast<int>(RowCount) * ColumnCount
58 };
59
60 // Constant indexed read access to row vectors; indexes must not exceed the valid range or errors will occur
61 // and the value for index 0 is returned.
62 const VectorT& operator[](const size_t i) const;
63
64 // Indexed read/write access to row vectors; indexes must not exceed the valid range or errors will occur
65 // and the value for index 0 is returned.
66 VectorT& operator[](const size_t i);
67
68 // Linear read/write access to matrix elements correctly ordered starting
69 // with first, second, ... elements of first row up to last element in last row.
70 // The first DT may \e not be used as an array base to address all DTs directly
71 // in memory since values are organized as vectors and not as an array.
72 // \p idx has to be smaller than ComponentCount. Other idx values must be avoided by the caller,
73 // as they lead to an error post and the return of the value at index 0.
75
76 // Linear and constant read access to matrix elements correctly ordered starting
77 // with first, second, ... elements of first row up to last element in last row.
78 // idx has to be smaller than ComponentCount. Other idx values must be avoided by the caller,
79 // as they lead to an error post and the return of the value at index 0.
81
82 // Returns the sum of all absolute values of all matrix elements. This is useful
83 // for testing differences of matrices less than a certain value.
85
89 void setValuesFromPtr(const ComponentType* const values);
90
94 void getValuesToPtr(ComponentType* values) const;
95
96protected:
98 VectorT v[size];
99};
100
101//-----------------------------------------------------------------------------------
102
103template <class VectorT, size_t size>
105{
106 if (i >= size) {
107 printTemplateError("FloatingPointMatrix::operator[] const, illegal index", ML_BAD_INDEX, "Using index 0");
108 return v[0];
109 }
110 return v[i];
111}
112
113//-----------------------------------------------------------------------------------
114
115template <class VectorT, size_t size> VectorT& FloatingPointMatrix<VectorT, size>::operator[](const size_t i)
116{
117 if (i >= size) {
118 printTemplateError("FloatingPointMatrix::operator[], illegal index", ML_BAD_INDEX, "Using index 0");
119 return v[0];
120 }
121 return v[i];
122}
123
124//-----------------------------------------------------------------------------------
125
126template <class VectorT, size_t size>
127inline typename VectorT::ComponentType& FloatingPointMatrix<VectorT, size>::linearIndexed(const size_t idx)
128{
129 if (idx < ComponentCount) {
130 return v[idx / VectorT::Size][idx % VectorT::Size];
131 }
132 else {
133 printTemplateError("FloatingPointMatrix::linearIndexed(), illegal index", ML_BAD_INDEX, "Using index 0 instead");
134 }
135 return v[0][0];
136}
137
138//-----------------------------------------------------------------------------------
139
140template <class VectorT, size_t size>
141inline typename VectorT::ComponentType FloatingPointMatrix<VectorT, size>::linearIndexedConst(const size_t idx) const
142{
143 typename VectorT::ComponentType retVal = v[0][0];
144 if (idx < ComponentCount) {
145 retVal = v[idx / VectorT::Size][idx % VectorT::Size];
146 }
147 else {
148 printTemplateError("FloatingPointMatrix::linearIndexedConst(), illegal index", ML_BAD_INDEX,
149 "Using index 0 instead");
150 }
151 return retVal;
152}
153
154//-----------------------------------------------------------------------------------
155
156template <class VectorT, size_t size>
157inline typename VectorT::ComponentType FloatingPointMatrix<VectorT, size>::compAbsSum() const
158{
159 typename VectorT::ComponentType retVal = 0;
160 for (size_t i = 0; i < RowCount; i++) {
161 for (size_t j = 0; j < ColumnCount; j++) {
162 retVal += MLAbs(v[i][j]);
163 }
164 }
165 return retVal;
166}
167
168//-----------------------------------------------------------------------------------
169
170template <class VectorT, size_t size>
172{
173 size_t n = 0;
174 for (size_t i = 0; i < RowCount; i++) {
175 for (size_t j = 0; j < ColumnCount; j++) {
176 v[i][j] = values[n++];
177 }
178 }
179}
180
181//-----------------------------------------------------------------------------------
182
183template <class VectorT, size_t size>
185{
186 size_t n = 0;
187 for (size_t i = 0; i < RowCount; i++) {
188 for (size_t j = 0; j < ColumnCount; j++) {
189 values[n++] = v[i][j];
190 }
191 }
192}
193
194//-----------------------------------------------------------------------------------
195
197
198
199#endif // __mlFloatingPointVector_H
Base class of all matrix classes that holds the data buffer and provides some general access methods.
ComponentType linearIndexedConst(const size_t idx) const
VectorT::ComponentType ComponentType
A typedef to 'export' the type of subcomponents.
const VectorT & operator[](const size_t i) const
VectorT & operator[](const size_t i)
VectorT VectorType
A typedef to 'export' the type of component vector.
void getValuesToPtr(ComponentType *values) const
Copies contents of *this into an array matrix, row by row; type and size must match.
void setValuesFromPtr(const ComponentType *const values)
Copies the contents from an array matrix into *this, row by row; type and size must match.
ComponentType compAbsSum() const
ComponentType & linearIndexed(const size_t idx)
DT MLAbs(const DT val)
Defines a templated MLAbs version to circumvent fabs ambiguities on different platforms.
#define ML_BAD_INDEX
The index given to the algorithm is out of range.
Definition mlTypeDefs.h:924
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
void ML_UTILS_EXPORT printTemplateError(const char *location, MLErrorCode reason, const std::string_view &handling)