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 
25 namespace itk
26 {
41 template <typename TValue>
42 class ITK_TEMPLATE_EXPORT Array2D : public vnl_matrix<TValue>
43 {
44 public:
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 
123 template <typename TValue>
124 std::ostream &
125 operator<<(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
149 template <>
150 ITKCommon_EXPORT std::ostream &
151  operator<<(std::ostream & os, const Array2D<float> & arr);
152 template <>
153 ITKCommon_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
itk::Array2D::GetElement
const TValue & GetElement(SizeValueType row, SizeValueType col) const
Definition: itkArray2D.h:103
itk::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:216
itk::Array2D< WeightsValueType >::VnlMatrixType
vnl_matrix< WeightsValueType > VnlMatrixType
Definition: itkArray2D.h:48
itkMacro.h
itkIntTypes.h
itk::Array2D::Array2D
Array2D(Self &&array) noexcept
Definition: itkArray2D.h:66
itk::Array2D::Fill
void Fill(TValue const &v)
Definition: itkArray2D.h:96
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::Array2D::SetElement
void SetElement(SizeValueType row, SizeValueType col, const TValue &value)
Definition: itkArray2D.h:110
AddImageFilter
Definition: itkAddImageFilter.h:81
itk::Array2D
Array2D class representing a 2D array.
Definition: itkArray2D.h:42
itk::Array2D::operator=
Self & operator=(Self &&array) noexcept
Definition: itkArray2D.h:83
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:86
itk::Array2D< WeightsValueType >::ValueType
WeightsValueType ValueType
Definition: itkArray2D.h:46