ITK  6.0.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 &) = default;
62
66 Array2D(Self && array) noexcept
67 : vnl_matrix<TValue>(std::move(array))
68 {
69 // Note: GCC <= 9.5 does not yet support "defaulting" (`= default`) this `noexcept` move-constructor.
70 }
71
73 Array2D(const VnlMatrixType & matrix);
74
76 Self &
77 operator=(const Self &) = default;
78
82 Self &
83 operator=(Self && array) noexcept
84 {
85 // Note: GCC <= 9.5 does not yet support "defaulting" (`= default`) this `noexcept` move-assignment operator.
86 this->VnlMatrixType::operator=(std::move(array));
87 return *this;
88 }
92 Self &
93 operator=(const VnlMatrixType & matrix);
94
95 void
96 Fill(TValue const & v)
97 {
98 this->fill(v);
99 }
100
102 const TValue &
104 {
105 return this->operator()(static_cast<unsigned int>(row), static_cast<unsigned int>(col));
106 }
107
109 void
110 SetElement(SizeValueType row, SizeValueType col, const TValue & value)
111 {
112 this->operator()(static_cast<unsigned int>(row), static_cast<unsigned int>(col)) = value;
113 }
114
116 void
117 SetSize(unsigned int m, unsigned int n);
118
120 ~Array2D() override = default;
121};
122
123template <typename TValue>
124std::ostream &
125operator<<(std::ostream & os, const Array2D<TValue> & arr)
126{
127 const unsigned int numberOfRows = arr.rows();
128 const unsigned int numberOfColumns = arr.cols();
129
130 for (unsigned int r = 0; r < numberOfRows; ++r)
131 {
132 os << '[';
133 if (numberOfColumns >= 1)
134 {
135 const unsigned int lastColumn = numberOfColumns - 1;
136 for (unsigned int c = 0; c < lastColumn; ++c)
137 {
138 os << arr(r, c) << ", ";
139 }
140 os << arr(r, lastColumn);
141 }
142 os << ']' << std::endl;
143 }
144
145 return os;
146}
147
148// declaration of specialization
149template <>
150ITKCommon_EXPORT std::ostream &
151 operator<<(std::ostream & os, const Array2D<float> & arr);
152template <>
153ITKCommon_EXPORT std::ostream &
154 operator<<(std::ostream & os, const Array2D<double> & arr);
155
156} // namespace itk
157
158#ifndef ITK_MANUAL_INSTANTIATION
159# include "itkArray2D.hxx"
160#endif
161
162#endif
Pixel-wise addition of two images.
Array2D class representing a 2D array.
Definition: itkArray2D.h:43
Array2D(const Self &)=default
Self & operator=(const VnlMatrixType &matrix)
void SetElement(SizeValueType row, SizeValueType col, const TValue &value)
Definition: itkArray2D.h:110
~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:96
Self & operator=(Self &&array) noexcept
Definition: itkArray2D.h:83
Self & operator=(const Self &)=default
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:103
Array2D(const VnlMatrixType &matrix)
Array2D(Self &&array) noexcept
Definition: itkArray2D.h:66
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)
unsigned long SizeValueType
Definition: itkIntTypes.h:86