ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
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 }
89
91 Self &
92 operator=(const VnlMatrixType & matrix);
93
94 void
95 Fill(const TValue & v)
96 {
97 this->fill(v);
98 }
99
101 const TValue &
103 {
104 return this->operator()(static_cast<unsigned int>(row), static_cast<unsigned int>(col));
105 }
106
108 void
109 SetElement(SizeValueType row, SizeValueType col, const TValue & value)
110 {
111 this->operator()(static_cast<unsigned int>(row), static_cast<unsigned int>(col)) = value;
112 }
113
115 void
116 SetSize(unsigned int m, unsigned int n);
117
119 ~Array2D() override = default;
120};
121
122template <typename TValue>
123std::ostream &
124operator<<(std::ostream & os, const Array2D<TValue> & arr)
125{
126 const unsigned int numberOfRows = arr.rows();
127 const unsigned int numberOfColumns = arr.cols();
128
129 for (unsigned int r = 0; r < numberOfRows; ++r)
130 {
131 os << '[';
132 if (numberOfColumns >= 1)
133 {
134 const unsigned int lastColumn = numberOfColumns - 1;
135 for (unsigned int c = 0; c < lastColumn; ++c)
136 {
137 os << arr(r, c) << ", ";
138 }
139 os << arr(r, lastColumn);
140 }
141 os << ']' << std::endl;
142 }
143
144 return os;
145}
146
147// declaration of specialization
148template <>
149ITKCommon_EXPORT std::ostream &
150 operator<<(std::ostream & os, const Array2D<float> & arr);
151template <>
152ITKCommon_EXPORT std::ostream &
153 operator<<(std::ostream & os, const Array2D<double> & arr);
154
155} // namespace itk
156
157#ifndef ITK_MANUAL_INSTANTIATION
158# include "itkArray2D.hxx"
159#endif
160
161#endif
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:109
~Array2D() override=default
vnl_matrix< TValue > VnlMatrixType
Definition itkArray2D.h:48
Array2D(unsigned int numberOfRows, unsigned int numberOfCols, const TValue &initialValue)
Array2D Self
Definition itkArray2D.h:47
Self & operator=(Self &&array) noexcept
Definition itkArray2D.h:83
void Fill(const TValue &v)
Definition itkArray2D.h:95
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:102
Array2D(const VnlMatrixType &matrix)
Array2D(Self &&array) noexcept
Definition itkArray2D.h:66
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
unsigned long SizeValueType
Definition itkIntTypes.h:86
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)