ITK  5.2.0
Insight Toolkit
itkCovariantVector.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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 {
68 
69 template <typename T, unsigned int NVectorDimension = 3>
70 class ITK_TEMPLATE_EXPORT CovariantVector : public FixedArray<T, NVectorDimension>
71 {
72 public:
76 
79  using ValueType = T;
81 
83  using ComponentType = T;
84 
86  static constexpr unsigned int Dimension = NVectorDimension;
87 
90 
93 
95  static unsigned int
97  {
98  return NVectorDimension;
99  }
100 
102  void
103  SetVnlVector(const vnl_vector<T> &);
104 
106  vnl_vector_ref<T>
107  GetVnlVector();
108 
110  vnl_vector<T>
111  GetVnlVector() const;
112 
114  CovariantVector() = default;
115  CovariantVector(const CovariantVector &) = default;
116  CovariantVector(CovariantVector &&) = default;
118  operator=(const CovariantVector &) = default;
120  operator=(CovariantVector &&) = default;
121  ~CovariantVector() = default;
123 
127  explicit CovariantVector(const ValueType & r);
128 
131  template <typename TVectorValueType>
133  : BaseArray(r)
134  {}
136  : BaseArray(r)
137  {}
139 
141  template <typename TCovariantVectorValueType>
142  Self &
144  {
145  BaseArray::operator=(r);
146  return *this;
147  }
149 
152  operator=(const ValueType r[NVectorDimension]);
153 
155  template <typename Tt>
156  inline const Self &
157  operator*=(const Tt & value)
158  {
159  for (unsigned int i = 0; i < NVectorDimension; i++)
160  {
161  (*this)[i] = static_cast<ValueType>((*this)[i] * value);
162  }
163  return *this;
164  }
166 
168  template <typename Tt>
169  const Self &
170  operator/=(const Tt & value)
171  {
172  for (unsigned int i = 0; i < NVectorDimension; i++)
173  {
174  (*this)[i] = static_cast<ValueType>((*this)[i] / value);
175  }
176  return *this;
177  }
179 
181  const Self &
182  operator+=(const Self & vec);
183 
185  const Self &
186  operator-=(const Self & vec);
187 
190  Self
191  operator-() const;
192 
194  Self
195  operator+(const Self & vec) const;
196 
198  Self
199  operator-(const Self & vec) const;
200 
205  ValueType operator*(const Self & other) const;
206 
209  ValueType operator*(const Vector<T, NVectorDimension> & other) const;
210 
213  inline Self operator*(const ValueType & val) const
214  {
215  Self result;
216 
217  for (unsigned int i = 0; i < NVectorDimension; i++)
218  {
219  result[i] = static_cast<ValueType>((*this)[i] * val);
220  }
221  return result;
222  }
223 
226  template <typename Tt>
227  inline Self
228  operator/(const Tt & val) const
229  {
230  Self result;
231 
232  for (unsigned int i = 0; i < NVectorDimension; i++)
233  {
234  result[i] = static_cast<ValueType>((*this)[i] / val);
235  }
236  return result;
237  }
238 
240  RealValueType
241  GetNorm() const;
242 
244  static unsigned int
246  {
247  return NVectorDimension;
248  }
249 
251  RealValueType
252  Normalize();
253 
255  RealValueType
256  GetSquaredNorm() const;
257 
260  template <typename TCoordRepB>
261  void
263  {
264  for (unsigned int i = 0; i < NVectorDimension; i++)
265  {
266  (*this)[i] = static_cast<T>(pa[i]);
267  }
268  }
269 };
271 
274 template <typename T, unsigned int NVectorDimension>
276 {
277  return v.operator*(scalar);
278 }
279 
282 template <typename T, unsigned int NVectorDimension>
283 inline T operator*(const Vector<T, NVectorDimension> & contravariant,
284  const CovariantVector<T, NVectorDimension> & covariant)
285 {
286  return covariant.operator*(contravariant);
287 }
288 
289 ITKCommon_EXPORT void
290 CrossProduct(CovariantVector<double, 3> &, const Vector<double, 3> &, const Vector<double, 3> &);
291 
292 ITKCommon_EXPORT void
293 CrossProduct(CovariantVector<float, 3> &, const Vector<float, 3> &, const Vector<float, 3> &);
294 
295 ITKCommon_EXPORT void
296 CrossProduct(CovariantVector<int, 3>, const Vector<int, 3> &, const Vector<int, 3> &);
297 
298 
299 template <typename T, unsigned int NVectorDimension>
300 inline void
302 {
303  a.swap(b);
304 }
305 
306 } // end namespace itk
307 
308 //
309 // Numeric traits must be included after (optionally) including the explicit
310 // instantiations control of this class, in case the implicit instantiation
311 // needs to be disabled.
312 //
313 // NumericTraits must be included before (optionally) including the .hxx file,
314 // in case the .hxx requires to use NumericTraits.
315 //
317 
318 #ifndef ITK_MANUAL_INSTANTIATION
319 # include "itkCovariantVector.hxx"
320 #endif
321 
322 #endif
itk::CovariantVector::operator/
Self operator/(const Tt &val) const
Definition: itkCovariantVector.h:228
itk::CovariantVector< double, TPointDimension >::RealValueType
typename NumericTraits< ValueType >::RealType RealValueType
Definition: itkCovariantVector.h:80
itk::CovariantVector< double, TPointDimension >::ValueType
double ValueType
Definition: itkCovariantVector.h:79
itk::CovariantVector::CovariantVector
CovariantVector(const CovariantVector< TVectorValueType, NVectorDimension > &r)
Definition: itkCovariantVector.h:132
itk::swap
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:244
itk::Vector
A templated class holding a n-Dimensional vector.
Definition: itkVector.h:62
itk::operator-
ConstNeighborhoodIterator< TImage > operator-(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
Definition: itkConstNeighborhoodIterator.h:679
itk::CovariantVector< double, TPointDimension >::ComponentType
double ComponentType
Definition: itkCovariantVector.h:83
itk::CovariantVector::operator*=
const Self & operator*=(const Tt &value)
Definition: itkCovariantVector.h:157
itk::CovariantVector::GetCovariantVectorDimension
static unsigned int GetCovariantVectorDimension()
Definition: itkCovariantVector.h:96
itk::CovariantVector::GetNumberOfComponents
static unsigned int GetNumberOfComponents()
Definition: itkCovariantVector.h:245
itkIndent.h
itkNumericTraitsCovariantVectorPixel.h
itk::CrossProduct
ITKCommon_EXPORT void CrossProduct(CovariantVector< double, 3 > &, const Vector< double, 3 > &, const Vector< double, 3 > &)
itk::FixedArray
Simulate a standard C array with copy semantics.
Definition: itkFixedArray.h:52
itk::CovariantVector::CastFrom
void CastFrom(const CovariantVector< TCoordRepB, NVectorDimension > &pa)
Definition: itkCovariantVector.h:262
itk::operator*
CovariantVector< T, NVectorDimension > operator*(const T &scalar, const CovariantVector< T, NVectorDimension > &v)
Definition: itkCovariantVector.h:275
itk::CovariantVector
A templated class holding a n-Dimensional covariant vector.
Definition: itkCovariantVector.h:70
itk::CovariantVector::operator/=
const Self & operator/=(const Tt &value)
Definition: itkCovariantVector.h:170
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::CovariantVector::CovariantVector
CovariantVector(const ValueType r[Dimension])
Definition: itkCovariantVector.h:135
itkVector.h
itk::FixedArray< T, NVectorDimension >::swap
void swap(FixedArray &other)
Definition: itkFixedArray.h:444
itk::operator+
ConstNeighborhoodIterator< TImage > operator+(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
Definition: itkConstNeighborhoodIterator.h:661
itk::CovariantVector::operator=
Self & operator=(const CovariantVector< TCovariantVectorValueType, NVectorDimension > &r)
Definition: itkCovariantVector.h:143
itk::NumericTraits::RealType
double RealType
Definition: itkNumericTraits.h:84
itk::CovariantVector::operator*
Self operator*(const ValueType &val) const
Definition: itkCovariantVector.h:213
itk::GTest::TypedefsAndConstructors::Dimension2::Dimension
constexpr unsigned int Dimension
Definition: itkGTestTypedefsAndConstructors.h:44