ITK  4.8.0
Insight Segmentation and Registration Toolkit
itkArray2D.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
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 Array2D: public vnl_matrix< TValue >
46 {
47 public:
48 
50  typedef TValue ValueType;
51  typedef Array2D Self;
52  typedef vnl_matrix< TValue > VnlMatrixType;
53 
54  Array2D();
55  Array2D(unsigned int rows, unsigned int cols);
56  Array2D(const Self & array);
57  Array2D(const VnlMatrixType & matrix);
58 
59  const Self & operator=(const Self & array);
60 
61  const Self & operator=(const VnlMatrixType & matrix);
62 
63  void Fill(TValue const & v) { this->fill(v); }
64 
66  const TValue & GetElement(SizeValueType row, SizeValueType col) const
67  {
68  return this->operator()(static_cast<unsigned int>(row), static_cast<unsigned int>(col));
69  }
70 
72  void SetElement(SizeValueType row, SizeValueType col, const TValue & value)
73  {
74  this->operator()(static_cast<unsigned int>(row), static_cast<unsigned int>(col)) = value;
75  }
76 
78  void SetSize(unsigned int m, unsigned int n);
79 
82  ~Array2D() {}
83 };
84 
85 template< typename TValue >
86 std::ostream & operator<<(std::ostream & os, const Array2D< TValue > & arr)
87 {
88  const unsigned int numberOfRows = arr.rows();
89  const unsigned int numberOfColumns = arr.cols();
90 
91  for ( unsigned int r = 0; r < numberOfRows; ++r )
92  {
93  os << "[";
94  if ( numberOfColumns >= 1 )
95  {
96  const unsigned int lastColumn = numberOfColumns - 1;
97  for ( unsigned int c = 0; c < lastColumn; ++c )
98  {
99  os << arr(r, c) << ", ";
100  }
101  os << arr(r, lastColumn);
102  }
103  os << "]" << std::endl;
104  }
105 
106  return os;
107 }
108 
109 // declaration of specialization
110 template<> ITKCommon_EXPORT std::ostream & operator<<(std::ostream & os, const Array2D< float > & arr);
111 template<> ITKCommon_EXPORT std::ostream & operator<<(std::ostream & os, const Array2D< double > & arr);
112 
113 } // namespace itk
114 
115 #ifndef ITK_MANUAL_INSTANTIATION
116 #include "itkArray2D.hxx"
117 #endif
118 
119 #endif
const TValue & GetElement(SizeValueType row, SizeValueType col) const
Definition: itkArray2D.h:66
const Self & operator=(const Self &array)
void SetSize(unsigned int m, unsigned int n)
unsigned long SizeValueType
Definition: itkIntTypes.h:143
void Fill(TValue const &v)
Definition: itkArray2D.h:63
TValue ValueType
Definition: itkArray2D.h:50
Array2D class representing a 2D array with size defined at construction time.
Definition: itkArray2D.h:45
Array2D Self
Definition: itkArray2D.h:51
vnl_matrix< TValue > VnlMatrixType
Definition: itkArray2D.h:52
void SetElement(SizeValueType row, SizeValueType col, const TValue &value)
Definition: itkArray2D.h:72