ITK  5.4.0
Insight Toolkit
itkArray2D.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright NumFOCUS
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18#ifndef itkArray2D_h
19#define itkArray2D_h
20
21#include "itkMacro.h"
22#include "itkIntTypes.h"
23#include "vnl/vnl_matrix.h"
24
25namespace itk
26{
41template <typename TValue>
42class ITK_TEMPLATE_EXPORT Array2D : public vnl_matrix<TValue>
43{
44public:
46 using ValueType = TValue;
47 using Self = Array2D;
48 using VnlMatrixType = vnl_matrix<TValue>;
49
51 Array2D() = default;
52
54 Array2D(unsigned int numberOfRows, unsigned int numberOfCols);
55
58 Array2D(unsigned int numberOfRows, unsigned int numberOfCols, const TValue & initialValue);
59
61 Array2D(const Self & array);
62
64 Array2D(const VnlMatrixType & matrix);
65
67 Self &
68 operator=(const Self & array);
69
71 Self &
72 operator=(const VnlMatrixType & matrix);
73
74 void
75 Fill(TValue const & v)
76 {
77 this->fill(v);
78 }
79
81 const TValue &
83 {
84 return this->operator()(static_cast<unsigned int>(row), static_cast<unsigned int>(col));
85 }
86
88 void
89 SetElement(SizeValueType row, SizeValueType col, const TValue & value)
90 {
91 this->operator()(static_cast<unsigned int>(row), static_cast<unsigned int>(col)) = value;
92 }
93
95 void
96 SetSize(unsigned int m, unsigned int n);
97
99 ~Array2D() override = default;
100};
101
102template <typename TValue>
103std::ostream &
104operator<<(std::ostream & os, const Array2D<TValue> & arr)
105{
106 const unsigned int numberOfRows = arr.rows();
107 const unsigned int numberOfColumns = arr.cols();
108
109 for (unsigned int r = 0; r < numberOfRows; ++r)
110 {
111 os << '[';
112 if (numberOfColumns >= 1)
113 {
114 const unsigned int lastColumn = numberOfColumns - 1;
115 for (unsigned int c = 0; c < lastColumn; ++c)
116 {
117 os << arr(r, c) << ", ";
118 }
119 os << arr(r, lastColumn);
120 }
121 os << ']' << std::endl;
122 }
123
124 return os;
125}
126
127// declaration of specialization
128template <>
129ITKCommon_EXPORT std::ostream &
130 operator<<(std::ostream & os, const Array2D<float> & arr);
131template <>
132ITKCommon_EXPORT std::ostream &
133 operator<<(std::ostream & os, const Array2D<double> & arr);
134
135} // namespace itk
136
137#ifndef ITK_MANUAL_INSTANTIATION
138# include "itkArray2D.hxx"
139#endif
140
141#endif
Array2D class representing a 2D array.
Definition: itkArray2D.h:43
Self & operator=(const VnlMatrixType &matrix)
void SetElement(SizeValueType row, SizeValueType col, const TValue &value)
Definition: itkArray2D.h:89
~Array2D() override=default
vnl_matrix< TValue > VnlMatrixType
Definition: itkArray2D.h:48
Array2D(unsigned int numberOfRows, unsigned int numberOfCols, const TValue &initialValue)
void Fill(TValue const &v)
Definition: itkArray2D.h:75
Self & operator=(const Self &array)
Array2D(unsigned int numberOfRows, unsigned int numberOfCols)
Array2D()=default
TValue ValueType
Definition: itkArray2D.h:46
void SetSize(unsigned int m, unsigned int n)
const TValue & GetElement(SizeValueType row, SizeValueType col) const
Definition: itkArray2D.h:82
Array2D(const VnlMatrixType &matrix)
Array2D(const Self &array)
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:216
unsigned long SizeValueType
Definition: itkIntTypes.h:83