ITK  4.2.0
Insight Segmentation and Registration Toolkit
itkCovariantVector.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 __itkCovariantVector_h
19 #define __itkCovariantVector_h
20 
21 #include "vnl/vnl_vector_ref.h"
22 #include "itkIndent.h"
23 #include "itkVector.h"
24 
25 namespace itk
26 {
66 template< class T, unsigned int NVectorDimension = 3 >
67 class ITK_EXPORT CovariantVector:public FixedArray< T, NVectorDimension >
68 {
69 public:
73 
76  typedef T ValueType;
78 
80  typedef T ComponentType;
81 
83  itkStaticConstMacro(Dimension, unsigned int, NVectorDimension);
84 
87 
90 
92  static unsigned int GetCovariantVectorDimension()
93  { return NVectorDimension; }
94 
96  void SetVnlVector(const vnl_vector< T > &);
97 
99  vnl_vector_ref< T > GetVnlVector(void);
100 
102  vnl_vector< T > GetVnlVector(void) const;
103 
106  itkLegacyMacro(void Set_vnl_vector(const vnl_vector< T > &));
107 
110  itkLegacyMacro(vnl_vector_ref< T > Get_vnl_vector(void));
111 
114  itkLegacyMacro(vnl_vector< T > Get_vnl_vector(void) const);
115 
118  CovariantVector(const ValueType & r);
119 
122  template< class TVectorValueType >
123  CovariantVector(const CovariantVector< TVectorValueType,
124  NVectorDimension > & r):BaseArray(r) {}
125  CovariantVector(const ValueType r[Dimension]):BaseArray(r) {}
127 
129  template< class Tt >
130  Self & operator=(const Tt & v)
131  {
132  BaseArray::operator=(v);
133  return *this;
134  }
136 
138  CovariantVector & operator=(const Self & r);
139 
140  CovariantVector & operator=(const ValueType r[NVectorDimension]);
141 
143  template< class Tt >
144  inline const Self & operator*=(const Tt & value)
145  {
146  for ( unsigned int i = 0; i < NVectorDimension; i++ )
147  {
148  ( *this )[i] = static_cast< ValueType >( ( *this )[i] * value );
149  }
150  return *this;
151  }
153 
155  template< class Tt >
156  const Self & operator/=(const Tt & value)
157  {
158  for ( unsigned int i = 0; i < NVectorDimension; i++ )
159  {
160  ( *this )[i] = static_cast< ValueType >( ( *this )[i] / value );
161  }
162  return *this;
163  }
165 
167  const Self & operator+=(const Self & vec);
168 
170  const Self & operator-=(const Self & vec);
171 
174  Self operator-() const;
175 
177  Self operator+(const Self & vec) const;
178 
180  Self operator-(const Self & vec) const;
181 
186  ValueType operator *(const Self & vec) const;
187 
190  ValueType operator *(const Vector< T, NVectorDimension > & vec) const;
191 
194  inline Self operator*(const ValueType & val) const
195  {
196  Self result;
197 
198  for ( unsigned int i = 0; i < NVectorDimension; i++ )
199  {
200  result[i] = static_cast< ValueType >( ( *this )[i] * val );
201  }
202  return result;
203  }
204 
207  template< class Tt >
208  inline Self operator/(const Tt & val) const
209  {
210  Self result;
211 
212  for ( unsigned int i = 0; i < NVectorDimension; i++ )
213  {
214  result[i] = static_cast< ValueType >( ( *this )[i] / val );
215  }
216  return result;
217  }
218 
220  RealValueType GetNorm(void) const;
221 
223  static unsigned int GetNumberOfComponents() { return NVectorDimension; }
224 
226  void Normalize(void);
227 
229  RealValueType GetSquaredNorm(void) const;
230 
233  template< typename TCoordRepB >
235  {
236  for ( unsigned int i = 0; i < NVectorDimension; i++ )
237  {
238  ( *this )[i] = static_cast< T >( pa[i] );
239  }
240  }
241 };
243 
246 template< class T, unsigned int NVectorDimension >
247 inline
248 CovariantVector< T, NVectorDimension >
250 {
251  return v * scalar;
252 }
253 
254 ITKCommon_EXPORT void CrossProduct(CovariantVector< double, 3 > &,
255  const Vector< double, 3 > &,
256  const Vector< double, 3 > &);
257 
258 ITKCommon_EXPORT void CrossProduct(CovariantVector< float, 3 > &,
259  const Vector< float, 3 > &,
260  const Vector< float, 3 > &);
261 
262 ITKCommon_EXPORT void CrossProduct(CovariantVector< int, 3 >,
263  const Vector< int, 3 > &,
264  const Vector< int, 3 > &);
265 } // end namespace itk
266 
267 // Define instantiation macro for this template.
268 #define ITK_TEMPLATE_CovariantVector(_, EXPORT, TypeX, TypeY) \
269  namespace itk \
270  { \
271  _( 2 ( class EXPORT CovariantVector< ITK_TEMPLATE_2 TypeX > ) ) \
272  namespace Templates \
273  { \
274  typedef CovariantVector< ITK_TEMPLATE_2 TypeX > \
275  CovariantVector##TypeY; \
276  } \
277  }
278 
279 #if ITK_TEMPLATE_EXPLICIT
280 #include "Templates/itkCovariantVector+-.h"
281 #endif
282 
283 //
284 // Numeric traits must be included after (optionally) including the explicit
285 // instantiations control of this class, in case the implicit instantiation
286 // needs to be disabled.
287 //
288 // NumericTraits must be included before (optionally) including the .hxx file,
289 // in case the .hxx requires to use NumericTraits.
290 //
292 
293 #if ITK_TEMPLATE_TXX
294 #include "itkCovariantVector.hxx"
295 #endif
296 
297 #endif
298