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 
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 & 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 
102 template <typename TValue>
103 std::ostream &
104 operator<<(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
128 template <>
129 ITKCommon_EXPORT std::ostream &
130  operator<<(std::ostream & os, const Array2D<float> & arr);
131 template <>
132 ITKCommon_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
itk::Array2D::GetElement
const TValue & GetElement(SizeValueType row, SizeValueType col) const
Definition: itkArray2D.h:82
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::Fill
void Fill(TValue const &v)
Definition: itkArray2D.h:75
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:89
itk::Array2D
Array2D class representing a 2D array.
Definition: itkArray2D.h:42
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83
itk::Array2D< WeightsValueType >::ValueType
WeightsValueType ValueType
Definition: itkArray2D.h:46