MeVisLab Toolbox Reference
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 //----------------------------------------------------------------------
21 #include "mlLinearAlgebraSystem.h"
22 
23 #include <mlPrintTemplateErrors.h>
24 
25 //------------------------------------------------------------------------
26 // Put all stuff in a specific ML_LA_NAMESPACE namespace to avoid
27 // collisions with other libraries.
28 //------------------------------------------------------------------------
29 ML_LA_START_NAMESPACE
30 
33 template <class VectorT, size_t size>
35 {
36 public:
38  typedef typename VectorT::ComponentType ComponentType;
39 
41  typedef VectorT VectorType;
42 
45  enum { RowCount = size };
46 
49  enum { ColumnCount = VectorT::Size };
50 
53  enum { ComponentCount = RowCount*ColumnCount };
54 
55  // Constant indexed read access to row vectors, indexes must not exceed the valid range or errors will occur
56  // and the value for index 0 is returned.
57  const VectorT& operator[](const size_t i) const;
58 
59  // Indexed read/write access to row vectors, indexes must not exceed the valid range or errors will occur
60  // and the value for index 0 is returned.
61  VectorT& operator[](const size_t i);
62 
63  // Linear read/write access to matrix elements correctly ordered starting
64  // with first, second, ... elements of first row up to last element in last row.
65  // The first DT may NOT be used as an array base to address all DTs directly
66  // in memory since values are organized as vectors and not as an array.
67  // idx has to be smaller than ComponentCount. Other idx values must be avoided by the caller,
68  // because they lead to an error post and a the return of value at index 0.
69  ComponentType &linearIndexed(const size_t idx);
70 
71  // Linear and constant read access to matrix elements correctly ordered starting
72  // with first, second, ... elements of first row up to last element in last row.
73  // idx has to be smaller than ComponentCount. Other idx values must be avoided by the caller,
74  // because they lead to an error post and a the return of value at index 0.
75  ComponentType linearIndexedConst(const size_t idx) const;
76 
77  // Returns sum of all MLAbs values of all matrix elements. This is useful
78  // for testing differences of matrices less than a certain value.
80 
84  void setValuesFromPtr(const ComponentType* const values);
85 
89  void getValuesToPtr(ComponentType* values) const;
90 
91 protected:
93  VectorT v[size];
94 };
95 
96 //-----------------------------------------------------------------------------------
97 
98 template <class VectorT, size_t size>
99 const VectorT& FloatingPointMatrix<VectorT,size>::operator[](const size_t i) const
100 {
101  if (i >= size){
102  printTemplateError("FloatingPointMatrix::operator[] const, illegal index", ML_BAD_INDEX, "Using index 0");
103  return v[0];
104  }
105  return v[i];
106 }
107 
108 //-----------------------------------------------------------------------------------
109 
110 template <class VectorT, size_t size>
112 {
113  if (i >= size){
114  printTemplateError("FloatingPointMatrix::operator[], illegal index", ML_BAD_INDEX, "Using index 0");
115  return v[0];
116  }
117  return v[i];
118 }
119 
120 //-----------------------------------------------------------------------------------
121 
122 template <class VectorT, size_t size>
123 inline typename VectorT::ComponentType &FloatingPointMatrix<VectorT,size>::linearIndexed(const size_t idx)
124 {
125  if (idx < ComponentCount) {
126  return v[idx/VectorT::Size][idx%VectorT::Size];
127  } else {
128  printTemplateError("FloatingPointMatrix::linearIndexed(), illegal index", ML_BAD_INDEX, "Using index 0 instead");
129  }
130  return v[0][0];
131 }
132 
133 //-----------------------------------------------------------------------------------
134 
135 template <class VectorT, size_t size>
136 inline typename VectorT::ComponentType FloatingPointMatrix<VectorT,size>::linearIndexedConst(const size_t idx) const
137 {
138  typename VectorT::ComponentType retVal = v[0][0];
139  if (idx < ComponentCount) {
140  retVal = v[idx/VectorT::Size][idx%VectorT::Size];
141  } else {
142  printTemplateError("FloatingPointMatrix::linearIndexedConst(), illegal index", ML_BAD_INDEX, "Using index 0 instead");
143  }
144  return retVal;
145 }
146 
147 //-----------------------------------------------------------------------------------
148 
149 template <class VectorT, size_t size>
150 inline typename VectorT::ComponentType FloatingPointMatrix<VectorT,size>::compAbsSum() const
151 {
152  typename VectorT::ComponentType retVal = 0;
153  for (size_t i=0;i<RowCount;i++) {
154  for (size_t j=0;j<ColumnCount;j++) {
155  retVal += MLAbs(v[i][j]);
156  }
157  }
158  return retVal;
159 }
160 
161 //-----------------------------------------------------------------------------------
162 
163 template <class VectorT, size_t size>
165 {
166  size_t n = 0;
167  for (size_t i=0;i<RowCount;i++) {
168  for (size_t j=0;j<ColumnCount;j++) {
169  v[i][j] = values[n++];
170  }
171  }
172 }
173 
174 //-----------------------------------------------------------------------------------
175 
176 template <class VectorT, size_t size>
178 {
179  size_t n = 0;
180  for (size_t i=0;i<RowCount;i++) {
181  for (size_t j=0;j<ColumnCount;j++) {
182  values[n++] = v[i][j];
183  }
184  }
185 }
186 
187 //-----------------------------------------------------------------------------------
188 
189 ML_LA_END_NAMESPACE
190 
191 
192 #endif // __mlFloatingPointVector_H
193 
194 
195 
Base class of all matrix classes which 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 sub-components.
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 array mat, row by row; type and size must match.
void setValuesFromPtr(const ComponentType *const values)
Copies contents from array mat 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 templated MLAbs version to circumvent fabs ambiguities on different platforms.
#define ML_BAD_INDEX
The index given to the algorithm is out of range; sometimes this is a programming error or a sloppy i...
Definition: mlTypeDefs.h:970
void ML_UTILS_EXPORT printTemplateError(const char *location, MLErrorCode reason, const std::string_view &handling)