ITK  5.4.0
Insight Toolkit
itkVector.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 * https://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 itkVector_h
19#define itkVector_h
20
21#include "itkFixedArray.h"
22
23#include "vnl/vnl_vector_ref.h" // GetVnlVector method return
24
25namespace itk
26{
61template <typename T, unsigned int VVectorDimension = 3>
62class ITK_TEMPLATE_EXPORT Vector : public FixedArray<T, VVectorDimension>
63{
64public:
66 using Self = Vector;
68
71 using ValueType = T;
73
75 static constexpr unsigned int Dimension = VVectorDimension;
76
79
81 using ComponentType = T;
82
85
87 static unsigned int
89 {
90 return VVectorDimension;
91 }
92
95 void
96 SetVnlVector(const vnl_vector<T> &);
97
99 vnl_vector_ref<T>
101
103 vnl_vector<T>
105
108 Vector() = default;
109
110#if !defined(ITK_LEGACY_REMOVE)
115 Vector(const ValueType & r);
116#else
117
120 explicit Vector(const ValueType & r);
121
123 Vector(std::nullptr_t) = delete;
124#endif
125
127 template <typename TVectorValueType>
129 : BaseArray(r)
130 {}
132 : BaseArray(r)
133 {}
134 template <typename TVectorValueType>
135 Vector(const TVectorValueType r[Dimension])
136 : BaseArray(r)
137 {}
141 explicit Vector(const std::array<ValueType, VVectorDimension> & stdArray)
142 : BaseArray(stdArray)
143 {}
144
146 template <typename TVectorValueType>
147 Vector &
149 {
150 BaseArray::operator=(r);
151 return *this;
152 }
155 Vector &
156 operator=(const ValueType r[VVectorDimension]);
157
159 template <typename Tt>
160 inline const Self &
161 operator*=(const Tt & value)
162 {
163 for (unsigned int i = 0; i < VVectorDimension; ++i)
164 {
165 (*this)[i] = static_cast<ValueType>((*this)[i] * value);
166 }
167 return *this;
168 }
172 template <typename Tt>
173 inline const Self &
174 operator/=(const Tt & value)
175 {
176 for (unsigned int i = 0; i < VVectorDimension; ++i)
177 {
178 (*this)[i] = static_cast<ValueType>((*this)[i] / value);
179 }
180 return *this;
181 }
185 const Self &
186 operator+=(const Self & vec);
187
189 const Self &
190 operator-=(const Self & vec);
191
194 Self
195 operator-() const;
196
198 Self
199 operator+(const Self & vec) const;
200
202 Self
203 operator-(const Self & vec) const;
204
207 ValueType operator*(const Self & other) const;
208
211 inline Self operator*(const ValueType & value) const
212 {
213 Self result;
214
215 for (unsigned int i = 0; i < VVectorDimension; ++i)
216 {
217 result[i] = static_cast<ValueType>((*this)[i] * value);
218 }
219 return result;
220 }
221
224 template <typename Tt>
225 inline Self
226 operator/(const Tt & value) const
227 {
228 Self result;
229
230 for (unsigned int i = 0; i < VVectorDimension; ++i)
231 {
232 result[i] = static_cast<ValueType>((*this)[i] / value);
233 }
234 return result;
235 }
236
241 bool
242 operator==(const Self & v) const
243 {
244 return Superclass::operator==(v);
245 }
246
248
251 GetNorm() const;
252
256
258 static unsigned int
260 {
261 return VVectorDimension;
262 }
263
266 RealValueType
268
269 void
271 {
272 this->operator[](c) = v;
273 }
274
277 template <typename TCoordRepB>
278 void
280 {
281 for (unsigned int i = 0; i < VVectorDimension; ++i)
282 {
283 (*this)[i] = static_cast<T>(pa[i]);
284 }
285 }
288 template <typename TCoordRepB>
290 {
292 for (unsigned int i = 0; i < VVectorDimension; ++i)
293 {
294 r[i] = static_cast<TCoordRepB>((*this)[i]);
295 }
296 return r;
297 }
298};
299
302template <typename T, unsigned int VVectorDimension>
304{
305 return v.operator*(scalar);
306}
307
309template <typename T, unsigned int VVectorDimension>
310std::ostream &
311operator<<(std::ostream & os, const Vector<T, VVectorDimension> & vct);
312
314template <typename T, unsigned int VVectorDimension>
315std::istream &
316operator>>(std::istream & is, Vector<T, VVectorDimension> & vct);
317
318ITKCommon_EXPORT Vector<double, 3>
320
321ITKCommon_EXPORT Vector<float, 3>
323
324ITKCommon_EXPORT Vector<int, 3>
326
327
328template <typename T, unsigned int VVectorDimension>
329inline void
331{
332 a.swap(b);
333}
334
335
337template <typename TValue, typename... TVariadic>
338auto
339MakeVector(const TValue firstValue, const TVariadic... otherValues)
340{
341 static_assert(std::conjunction_v<std::is_same<TVariadic, TValue>...>,
342 "The other values should have the same type as the first value.");
343
344 constexpr unsigned int dimension{ 1 + sizeof...(TVariadic) };
345 const std::array<TValue, dimension> stdArray{ { firstValue, otherValues... } };
346 return Vector<TValue, dimension>{ stdArray };
347}
348
349} // end namespace itk
350
351#ifndef ITK_MANUAL_INSTANTIATION
352# include "itkVector.hxx"
353#endif
354
355#endif
Pixel-wise addition of two images.
Simulate a standard C array with copy semantics.
Definition: itkFixedArray.h:54
void swap(FixedArray &other)
A templated class holding a n-Dimensional vector.
Definition: itkVector.h:63
vnl_vector_ref< T > GetVnlVector()
Vector(const std::array< ValueType, VVectorDimension > &stdArray)
Definition: itkVector.h:141
Vector(const ValueType r[Dimension])
Definition: itkVector.h:131
Self operator/(const Tt &value) const
Definition: itkVector.h:226
bool operator==(const Self &v) const
Definition: itkVector.h:242
Vector(std::nullptr_t)=delete
Vector()=default
const Self & operator-=(const Self &vec)
ValueType operator*(const Self &other) const
Vector & operator=(const ValueType r[VVectorDimension])
static unsigned int GetVectorDimension()
Definition: itkVector.h:88
T ComponentType
Definition: itkVector.h:81
Vector(const TVectorValueType r[Dimension])
Definition: itkVector.h:135
vnl_vector< T > GetVnlVector() const
RealValueType Normalize()
RealValueType GetSquaredNorm() const
Self operator+(const Self &vec) const
void SetVnlVector(const vnl_vector< T > &)
Vector & operator=(const Vector< TVectorValueType, VVectorDimension > &r)
Definition: itkVector.h:148
const Self & operator*=(const Tt &value)
Definition: itkVector.h:161
static unsigned int GetNumberOfComponents()
Definition: itkVector.h:259
void SetNthComponent(int c, const ComponentType &v)
Definition: itkVector.h:270
const Self & operator+=(const Self &vec)
RealValueType GetNorm() const
Vector(const Vector< TVectorValueType, VVectorDimension > &r)
Definition: itkVector.h:128
Self operator-() const
void CastFrom(const Vector< TCoordRepB, VVectorDimension > &pa)
Definition: itkVector.h:279
Vector(const ValueType &r)
typename NumericTraits< ValueType >::RealType RealValueType
Definition: itkVector.h:72
Self operator-(const Self &vec) const
Self operator*(const ValueType &value) const
Definition: itkVector.h:211
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Self)
const Self & operator/=(const Tt &value)
Definition: itkVector.h:174
AddImageFilter Self
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
std::istream & operator>>(std::istream &is, Point< T, VPointDimension > &vct)
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:242
CovariantVector< T, VVectorDimension > operator*(const T &scalar, const CovariantVector< T, VVectorDimension > &v)
ITKCommon_EXPORT void CrossProduct(CovariantVector< double, 3 > &, const Vector< double, 3 > &, const Vector< double, 3 > &)
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:545
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:216
auto MakeVector(const TValue firstValue, const TVariadic... otherValues)
Definition: itkVector.h:339