ITK  5.2.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  * http://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 {
44 template <typename TValue>
45 class ITK_TEMPLATE_EXPORT Array2D : public vnl_matrix<TValue>
46 {
47 public:
49  using ValueType = TValue;
50  using Self = Array2D;
51  using VnlMatrixType = vnl_matrix<TValue>;
52 
53  Array2D() = default;
54  Array2D(unsigned int numberOfRows, unsigned int numberOfCols);
55  Array2D(const Self & array);
56  Array2D(const VnlMatrixType & matrix);
57 
58  const Self &
59  operator=(const Self & array);
60 
61  const Self &
62  operator=(const VnlMatrixType & matrix);
63 
64  void
65  Fill(TValue const & v)
66  {
67  this->fill(v);
68  }
69 
71  const TValue &
73  {
74  return this->operator()(static_cast<unsigned int>(row), static_cast<unsigned int>(col));
75  }
76 
78  void
79  SetElement(SizeValueType row, SizeValueType col, const TValue & value)
80  {
81  this->operator()(static_cast<unsigned int>(row), static_cast<unsigned int>(col)) = value;
82  }
83 
85  void
86  SetSize(unsigned int m, unsigned int n);
87 
90  ~Array2D() override = default;
91 };
92 
93 template <typename TValue>
94 std::ostream &
95 operator<<(std::ostream & os, const Array2D<TValue> & arr)
96 {
97  const unsigned int numberOfRows = arr.rows();
98  const unsigned int numberOfColumns = arr.cols();
99 
100  for (unsigned int r = 0; r < numberOfRows; ++r)
101  {
102  os << "[";
103  if (numberOfColumns >= 1)
104  {
105  const unsigned int lastColumn = numberOfColumns - 1;
106  for (unsigned int c = 0; c < lastColumn; ++c)
107  {
108  os << arr(r, c) << ", ";
109  }
110  os << arr(r, lastColumn);
111  }
112  os << "]" << std::endl;
113  }
114 
115  return os;
116 }
117 
118 // declaration of specialization
119 template <>
120 ITKCommon_EXPORT std::ostream &
121  operator<<(std::ostream & os, const Array2D<float> & arr);
122 template <>
123 ITKCommon_EXPORT std::ostream &
124  operator<<(std::ostream & os, const Array2D<double> & arr);
125 
126 } // namespace itk
127 
128 #ifndef ITK_MANUAL_INSTANTIATION
129 # include "itkArray2D.hxx"
130 #endif
131 
132 #endif
itk::Array2D::GetElement
const TValue & GetElement(SizeValueType row, SizeValueType col) const
Definition: itkArray2D.h:72
itk::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:218
itk::Array2D< WeightsValueType >::VnlMatrixType
vnl_matrix< WeightsValueType > VnlMatrixType
Definition: itkArray2D.h:51
itkMacro.h
itkIntTypes.h
itk::Array2D::Fill
void Fill(TValue const &v)
Definition: itkArray2D.h:65
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:79
itk::Array2D
Array2D class representing a 2D array with size defined at construction time.
Definition: itkArray2D.h:45
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83
itk::Array2D< WeightsValueType >::ValueType
WeightsValueType ValueType
Definition: itkArray2D.h:49