ITK  5.0.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 "itkIndent.h"
22 #include "itkVector.h"
23 #include "vnl/vnl_vector_ref.h"
24 
25 namespace itk
26 {
66 template< typename T, unsigned int NVectorDimension = 3 >
67 class ITK_TEMPLATE_EXPORT CovariantVector:public FixedArray< T, NVectorDimension >
68 {
69 public:
73 
76  using ValueType = T;
78 
80  using ComponentType = T;
81 
83  static constexpr unsigned int Dimension = 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();
100 
102  vnl_vector< T > GetVnlVector() const;
103 
105  CovariantVector() = default;
106  CovariantVector(const CovariantVector&) = default;
107  CovariantVector(CovariantVector&&) = default;
108  CovariantVector & operator=(const CovariantVector &) = default;
109  CovariantVector & operator=(CovariantVector &&) = default;
110  ~CovariantVector() = default;
112 
116  explicit CovariantVector(const ValueType & r);
117 
120  template< typename TVectorValueType >
121  CovariantVector(const CovariantVector< TVectorValueType,
122  NVectorDimension > & r):BaseArray(r) {}
125 
127  template< typename TCovariantVectorValueType >
129  {
130  BaseArray::operator=(r);
131  return *this;
132  }
134 
136  CovariantVector & operator=(const ValueType r[NVectorDimension]);
137 
139  template< typename Tt >
140  inline const Self & operator*=(const Tt & value)
141  {
142  for ( unsigned int i = 0; i < NVectorDimension; i++ )
143  {
144  ( *this )[i] = static_cast< ValueType >( ( *this )[i] * value );
145  }
146  return *this;
147  }
149 
151  template< typename Tt >
152  const Self & operator/=(const Tt & value)
153  {
154  for ( unsigned int i = 0; i < NVectorDimension; i++ )
155  {
156  ( *this )[i] = static_cast< ValueType >( ( *this )[i] / value );
157  }
158  return *this;
159  }
161 
163  const Self & operator+=(const Self & vec);
164 
166  const Self & operator-=(const Self & vec);
167 
170  Self operator-() const;
171 
173  Self operator+(const Self & vec) const;
174 
176  Self operator-(const Self & vec) const;
177 
182  ValueType operator *(const Self & vec) const;
183 
186  ValueType operator *(const Vector< T, NVectorDimension > & vec) const;
187 
190  inline Self operator*(const ValueType & val) const
191  {
192  Self result;
193 
194  for ( unsigned int i = 0; i < NVectorDimension; i++ )
195  {
196  result[i] = static_cast< ValueType >( ( *this )[i] * val );
197  }
198  return result;
199  }
200 
203  template< typename Tt >
204  inline Self operator/(const Tt & val) const
205  {
206  Self result;
207 
208  for ( unsigned int i = 0; i < NVectorDimension; i++ )
209  {
210  result[i] = static_cast< ValueType >( ( *this )[i] / val );
211  }
212  return result;
213  }
214 
216  RealValueType GetNorm() const;
217 
219  static unsigned int GetNumberOfComponents() { return NVectorDimension; }
220 
222  RealValueType Normalize();
223 
225  RealValueType GetSquaredNorm() const;
226 
229  template< typename TCoordRepB >
231  {
232  for ( unsigned int i = 0; i < NVectorDimension; i++ )
233  {
234  ( *this )[i] = static_cast< T >( pa[i] );
235  }
236  }
237 };
239 
242 template< typename T, unsigned int NVectorDimension >
243 inline
244 CovariantVector< T, NVectorDimension >
246 {
247  return v.operator*(scalar);
248 }
249 
252 template< typename T, unsigned int NVectorDimension >
253 inline
254 T
256 {
257  return covariant.operator*( contravariant );
258 }
259 
260 ITKCommon_EXPORT void CrossProduct(CovariantVector< double, 3 > &,
261  const Vector< double, 3 > &,
262  const Vector< double, 3 > &);
263 
264 ITKCommon_EXPORT void CrossProduct(CovariantVector< float, 3 > &,
265  const Vector< float, 3 > &,
266  const Vector< float, 3 > &);
267 
268 ITKCommon_EXPORT void CrossProduct(CovariantVector< int, 3 >,
269  const Vector< int, 3 > &,
270  const Vector< int, 3 > &);
271 
272 
273 template< typename T, unsigned int NVectorDimension >
275 {
276  a.swap(b);
277 }
278 
279 } // end namespace itk
280 
281 //
282 // Numeric traits must be included after (optionally) including the explicit
283 // instantiations control of this class, in case the implicit instantiation
284 // needs to be disabled.
285 //
286 // NumericTraits must be included before (optionally) including the .hxx file,
287 // in case the .hxx requires to use NumericTraits.
288 //
290 
291 #ifndef ITK_MANUAL_INSTANTIATION
292 #include "itkCovariantVector.hxx"
293 #endif
294 
295 #endif
void CastFrom(const CovariantVector< TCoordRepB, NVectorDimension > &pa)
ConstNeighborhoodIterator< TImage > operator-(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
Define numeric traits for std::vector.
const Self & operator/=(const Tt &value)
Self operator*(const ValueType &val) const
CovariantVector< T, NVectorDimension > operator*(const T &scalar, const CovariantVector< T, NVectorDimension > &v)
CovariantVector(const ValueType r[Dimension])
typename NumericTraits< ValueType >::RealType RealValueType
Simulate a standard C array with copy semnatics.
Definition: itkFixedArray.h:51
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:211
CovariantVector(const CovariantVector< TVectorValueType, NVectorDimension > &r)
A templated class holding a n-Dimensional vector.
Definition: itkVector.h:62
ConstNeighborhoodIterator< TImage > operator+(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
Self & operator=(const CovariantVector< TCovariantVectorValueType, NVectorDimension > &r)
ITKCommon_EXPORT void CrossProduct(CovariantVector< double, 3 > &, const Vector< double, 3 > &, const Vector< double, 3 > &)
Self operator/(const Tt &val) const
static unsigned int GetNumberOfComponents()
A templated class holding a n-Dimensional covariant vector.
const Self & operator*=(const Tt &value)
static unsigned int GetCovariantVectorDimension()