ITK  6.0.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
208 operator*(const Self & other) const;
209
212 inline Self
213 operator*(const ValueType & value) const
214 {
215 Self result;
216
217 for (unsigned int i = 0; i < VVectorDimension; ++i)
218 {
219 result[i] = static_cast<ValueType>((*this)[i] * value);
220 }
221 return result;
222 }
223
226 template <typename Tt>
227 inline Self
228 operator/(const Tt & value) const
229 {
230 Self result;
231
232 for (unsigned int i = 0; i < VVectorDimension; ++i)
233 {
234 result[i] = static_cast<ValueType>((*this)[i] / value);
235 }
236 return result;
237 }
238
243 bool
244 operator==(const Self & v) const
245 {
246 return Superclass::operator==(v);
247 }
248
250
253 GetNorm() const;
254
258
260 static unsigned int
262 {
263 return VVectorDimension;
264 }
265
268 RealValueType
270
271 void
273 {
274 this->operator[](c) = v;
275 }
276
279 template <typename TCoordinateB>
280 void
282 {
283 for (unsigned int i = 0; i < VVectorDimension; ++i)
284 {
285 (*this)[i] = static_cast<T>(pa[i]);
286 }
287 }
290 template <typename TCoordinateB>
292 {
294 for (unsigned int i = 0; i < VVectorDimension; ++i)
295 {
296 r[i] = static_cast<TCoordinateB>((*this)[i]);
297 }
298 return r;
299 }
300};
301
304template <typename T, unsigned int VVectorDimension>
305inline Vector<T, VVectorDimension>
306operator*(const T & scalar, const Vector<T, VVectorDimension> & v)
307{
308 return v.operator*(scalar);
309}
310
312template <typename T, unsigned int VVectorDimension>
313std::ostream &
314operator<<(std::ostream & os, const Vector<T, VVectorDimension> & vct);
315
317template <typename T, unsigned int VVectorDimension>
318std::istream &
319operator>>(std::istream & is, Vector<T, VVectorDimension> & vct);
320
321ITKCommon_EXPORT Vector<double, 3>
323
324ITKCommon_EXPORT Vector<float, 3>
326
327ITKCommon_EXPORT Vector<int, 3>
329
330
331template <typename T, unsigned int VVectorDimension>
332inline void
334{
335 a.swap(b);
336}
337
338
340template <typename TValue, typename... TVariadic>
341auto
342MakeVector(const TValue firstValue, const TVariadic... otherValues)
343{
344 static_assert(std::conjunction_v<std::is_same<TVariadic, TValue>...>,
345 "The other values should have the same type as the first value.");
346
347 constexpr unsigned int dimension{ 1 + sizeof...(TVariadic) };
348 const std::array<TValue, dimension> stdArray{ { firstValue, otherValues... } };
349 return Vector<TValue, dimension>{ stdArray };
350}
351
352} // end namespace itk
353
354#ifndef ITK_MANUAL_INSTANTIATION
355# include "itkVector.hxx"
356#endif
357
358#endif
Pixel-wise addition of two images.
Simulate a standard C array with copy semantics.
Definition: itkFixedArray.h:54
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
void CastFrom(const Vector< TCoordinateB, VVectorDimension > &pa)
Definition: itkVector.h:281
Vector(const ValueType r[Dimension])
Definition: itkVector.h:131
Self operator/(const Tt &value) const
Definition: itkVector.h:228
bool operator==(const Self &v) const
Definition: itkVector.h:244
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:261
void SetNthComponent(int c, const ComponentType &v)
Definition: itkVector.h:272
const Self & operator+=(const Self &vec)
RealValueType GetNorm() const
Vector(const Vector< TVectorValueType, VVectorDimension > &r)
Definition: itkVector.h:128
Self operator-() const
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:213
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....
void swap(Array< T > &a, Array< T > &b) noexcept
Definition: itkArray.h:242
std::istream & operator>>(std::istream &is, Point< T, VPointDimension > &vct)
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)
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:552
auto MakeVector(const TValue firstValue, const TVariadic... otherValues)
Definition: itkVector.h:342