ITK  4.2.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 TValueType >
44 class Array2D:public vnl_matrix< TValueType >
45 {
46 public:
47 
49  typedef TValueType ValueType;
50  typedef Array2D Self;
52 public:
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(TValueType const & v) { this->fill(v); }
64 
66  void SetSize(unsigned int m, unsigned int n);
67 
70  ~Array2D() {}
71 };
72 
73 template< typename TValueType >
74 std::ostream & operator<<(std::ostream & os, const Array2D< TValueType > & arr)
75 {
76  const unsigned int numberOfColumns = arr.cols();
77  const unsigned int numberOfRows = arr.rows();
78  const signed int lastColumn = (signed int)numberOfColumns - 1;
79 
80  for ( unsigned int r = 0; r < numberOfRows; ++r )
81  {
82  os << "[";
83  for ( signed int c = 0; c < lastColumn; ++c )
84  {
85  os << arr(r, c) << ", ";
86  }
87  if ( numberOfColumns >= 1 )
88  {
89  os << arr(r, lastColumn);
90  }
91  os << "]" << std::endl;
92  }
93 
94  return os;
95 }
96 } // namespace itk
97 
98 // Define instantiation macro for this template.
99 #define ITK_TEMPLATE_Array2D(_, EXPORT, TypeX, TypeY) \
100  namespace itk \
101  { \
102  _( 1 ( class EXPORT Array2D< ITK_TEMPLATE_1 TypeX > ) ) \
103  _( 1 ( EXPORT std::ostream & operator<<(std::ostream &, \
104  const Array2D< ITK_TEMPLATE_1 TypeX > &) ) ) \
105  namespace Templates \
106  { \
107  typedef Array2D< ITK_TEMPLATE_1 TypeX > Array2D##TypeY; \
108  } \
109  }
110 
111 #if ITK_TEMPLATE_EXPLICIT
112 #include "Templates/itkArray2D+-.h"
113 #endif
114 
115 #if ITK_TEMPLATE_TXX
116 #include "itkArray2D.hxx"
117 #endif
118 
119 #endif
120