ITK  4.6.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 "vnl/vnl_matrix.h"
23 
24 namespace itk
25 {
43 template< typename TValue >
44 class Array2D: public vnl_matrix< TValue >
45 {
46 public:
47 
49  typedef TValue ValueType;
50  typedef Array2D Self;
51  typedef vnl_matrix< TValue > VnlMatrixType;
52 
53  Array2D();
54  Array2D(unsigned int rows, unsigned int cols);
55  Array2D(const Self & array);
56  Array2D(const VnlMatrixType & matrix);
57 
58  const Self & operator=(const Self & array);
59 
60  const Self & operator=(const VnlMatrixType & matrix);
61 
62  void Fill(TValue const & v) { this->fill(v); }
63 
65  void SetSize(unsigned int m, unsigned int n);
66 
69  ~Array2D() {}
70 };
71 
72 template< typename TValue >
73 std::ostream & operator<<(std::ostream & os, const Array2D< TValue > & arr)
74 {
75  const unsigned int numberOfRows = arr.rows();
76  const unsigned int numberOfColumns = arr.cols();
77 
78  for ( unsigned int r = 0; r < numberOfRows; ++r )
79  {
80  os << "[";
81  if ( numberOfColumns >= 1 )
82  {
83  const unsigned int lastColumn = numberOfColumns - 1;
84  for ( unsigned int c = 0; c < lastColumn; ++c )
85  {
86  os << arr(r, c) << ", ";
87  }
88  os << arr(r, lastColumn);
89  }
90  os << "]" << std::endl;
91  }
92 
93  return os;
94 }
95 
96 // declaration of specialization
97 template<> ITKCommon_EXPORT std::ostream & operator<<(std::ostream & os, const Array2D< float > & arr);
98 template<> ITKCommon_EXPORT std::ostream & operator<<(std::ostream & os, const Array2D< double > & arr);
99 
100 } // namespace itk
101 
102 #ifndef ITK_MANUAL_INSTANTIATION
103 #include "itkArray2D.hxx"
104 #endif
105 
106 #endif
const Self & operator=(const Self &array)
void SetSize(unsigned int m, unsigned int n)
void Fill(TValue const &v)
Definition: itkArray2D.h:62
TValue ValueType
Definition: itkArray2D.h:49
Array2D class representing a 2D array with size defined at construction time.
Definition: itkArray2D.h:44
Array2D Self
Definition: itkArray2D.h:50
vnl_matrix< TValue > VnlMatrixType
Definition: itkArray2D.h:51