ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
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
128 template <typename TVectorValueType>
133 : BaseArray(r)
134 {}
135 template <typename TVectorValueType>
136 Vector(const TVectorValueType r[Dimension])
137 : BaseArray(r)
138 {}
139
140
142 explicit Vector(const std::array<ValueType, VVectorDimension> & stdArray)
143 : BaseArray(stdArray)
144 {}
145
147 template <typename TVectorValueType>
148 Vector &
150 {
152 return *this;
153 }
154
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 }
169
171 template <typename Tt>
172 inline const Self &
173 operator/=(const Tt & value)
174 {
175 for (unsigned int i = 0; i < VVectorDimension; ++i)
176 {
177 (*this)[i] = static_cast<ValueType>((*this)[i] / value);
178 }
179 return *this;
180 }
181
183 const Self &
184 operator+=(const Self & vec);
185
187 const Self &
188 operator-=(const Self & vec);
189
192 Self
193 operator-() const;
194
196 Self
197 operator+(const Self & vec) const;
198
200 Self
201 operator-(const Self & vec) const;
202
206 operator*(const Self & other) const;
207
210 inline Self
211 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 TCoordinateB>
278 void
280 {
281 for (unsigned int i = 0; i < VVectorDimension; ++i)
282 {
283 (*this)[i] = static_cast<T>(pa[i]);
284 }
285 }
286
287 template <typename TCoordinateB>
289 {
291 for (unsigned int i = 0; i < VVectorDimension; ++i)
292 {
293 r[i] = static_cast<TCoordinateB>((*this)[i]);
294 }
295 return r;
296 }
297};
298
301template <typename T, unsigned int VVectorDimension>
302inline Vector<T, VVectorDimension>
303operator*(const T & scalar, const Vector<T, VVectorDimension> & v)
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
bool operator==(const FixedArray &r) const
FixedArray & operator=(const FixedArray< TFixedArrayValueType, VLength > &r)
ITK_GCC_PRAGMA_PUSH ITK_GCC_SUPPRESS_Warray_bounds constexpr reference operator[](unsigned int index)
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:142
void CastFrom(const Vector< TCoordinateB, VVectorDimension > &pa)
Definition itkVector.h:279
FixedArray< double, VVectorDimension > BaseArray
Definition itkVector.h:84
Vector(const ValueType r[Dimension])
Definition itkVector.h:132
Self operator/(const Tt &value) const
Definition itkVector.h:226
static constexpr unsigned int Dimension
Definition itkVector.h:75
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
Vector(const TVectorValueType r[Dimension])
Definition itkVector.h:136
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:149
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:129
FixedArray< double, VVectorDimension > Superclass
Definition itkVector.h:67
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:211
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Self)
const Self & operator/=(const Tt &value)
Definition itkVector.h:173
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
void swap(Array< T > &a, Array< T > &b) noexcept
Definition itkArray.h:247
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 > &)
auto MakeVector(const TValue firstValue, const TVariadic... otherValues)
Definition itkVector.h:339