VTK  9.3.1
vtkVector.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
3 
19 #ifndef vtkVector_h
20 #define vtkVector_h
21 
22 #include "vtkObject.h" // for legacy macros
23 #include "vtkTuple.h"
24 
25 #include <cmath> // For math functions
26 
27 VTK_ABI_NAMESPACE_BEGIN
28 template <typename T, int Size>
29 class vtkVector : public vtkTuple<T, Size>
30 {
31 public:
32  vtkVector() = default;
33 
37  explicit vtkVector(const T& scalar)
38  : vtkTuple<T, Size>(scalar)
39  {
40  }
41 
47  explicit vtkVector(const T* init)
48  : vtkTuple<T, Size>(init)
49  {
50  }
51 
53 
56  T SquaredNorm() const
57  {
58  T result = 0;
59  for (int i = 0; i < Size; ++i)
60  {
61  result += this->Data[i] * this->Data[i];
62  }
63  return result;
64  }
66 
70  double Norm() const { return sqrt(static_cast<double>(this->SquaredNorm())); }
71 
73 
77  double Normalize()
78  {
79  const double norm(this->Norm());
80  if (norm == 0.0)
81  {
82  return 0.0;
83  }
84  const double inv(1.0 / norm);
85  for (int i = 0; i < Size; ++i)
86  {
87  this->Data[i] = static_cast<T>(this->Data[i] * inv);
88  }
89  return norm;
90  }
92 
94 
99  {
100  vtkVector<T, Size> temp(*this);
101  temp.Normalize();
102  return temp;
103  }
105 
107 
110  T Dot(const vtkVector<T, Size>& other) const
111  {
112  T result(0);
113  for (int i = 0; i < Size; ++i)
114  {
115  result += this->Data[i] * other[i];
116  }
117  return result;
118  }
120 
122 
126  {
127  vtkVector<T, Size> result(*this);
128  for (int i = 0; i < Size; ++i)
129  {
130  result[i] = this->Data[i] - other[i];
131  }
132  return result;
133  }
135 
137 
140  vtkVector<T, Size> operator*(const T& other) const
141  {
142  vtkVector<T, Size> result(*this);
143  for (int i = 0; i < Size; ++i)
144  {
145  result[i] = this->Data[i] * other;
146  }
147  return result;
148  }
150 
152 
155  template <typename TR>
157  {
158  vtkVector<TR, Size> result;
159  for (int i = 0; i < Size; ++i)
160  {
161  result[i] = static_cast<TR>(this->Data[i]);
162  }
163  return result;
164  }
166 };
167 
168 // .NAME vtkVector2 - templated base type for storage of 2D vectors.
169 //
170 template <typename T>
171 class vtkVector2 : public vtkVector<T, 2>
172 {
173 public:
174  vtkVector2() = default;
175 
176  explicit vtkVector2(const T& scalar)
177  : vtkVector<T, 2>(scalar)
178  {
179  }
180 
181  explicit vtkVector2(const T* init)
182  : vtkVector<T, 2>(init)
183  {
184  }
185 
186  vtkVector2(const T& x, const T& y)
187  {
188  this->Data[0] = x;
189  this->Data[1] = y;
190  }
191 
193 
196  void Set(const T& x, const T& y)
197  {
198  this->Data[0] = x;
199  this->Data[1] = y;
200  }
202 
206  void SetX(const T& x) { this->Data[0] = x; }
207 
211  const T& GetX() const { return this->Data[0]; }
212 
216  void SetY(const T& y) { this->Data[1] = y; }
217 
221  const T& GetY() const { return this->Data[1]; }
222 
224 
227  bool operator<(const vtkVector2<T>& v) const
228  {
229  return (this->Data[0] < v.Data[0]) || (this->Data[0] == v.Data[0] && this->Data[1] < v.Data[1]);
230  }
232 };
233 
234 // .NAME vtkVector3 - templated base type for storage of 3D vectors.
235 //
236 template <typename T>
237 class vtkVector3 : public vtkVector<T, 3>
238 {
239 public:
240  vtkVector3() = default;
241 
242  explicit vtkVector3(const T& scalar)
243  : vtkVector<T, 3>(scalar)
244  {
245  }
246 
247  explicit vtkVector3(const T* init)
248  : vtkVector<T, 3>(init)
249  {
250  }
251 
252  vtkVector3(const T& x, const T& y, const T& z)
253  {
254  this->Data[0] = x;
255  this->Data[1] = y;
256  this->Data[2] = z;
257  }
258 
260 
263  void Set(const T& x, const T& y, const T& z)
264  {
265  this->Data[0] = x;
266  this->Data[1] = y;
267  this->Data[2] = z;
268  }
270 
274  void SetX(const T& x) { this->Data[0] = x; }
275 
279  const T& GetX() const { return this->Data[0]; }
280 
284  void SetY(const T& y) { this->Data[1] = y; }
285 
289  const T& GetY() const { return this->Data[1]; }
290 
294  void SetZ(const T& z) { this->Data[2] = z; }
295 
299  const T& GetZ() const { return this->Data[2]; }
300 
302 
305  vtkVector3<T> Cross(const vtkVector3<T>& other) const
306  {
307  vtkVector3<T> res;
308  res[0] = this->Data[1] * other.Data[2] - this->Data[2] * other.Data[1];
309  res[1] = this->Data[2] * other.Data[0] - this->Data[0] * other.Data[2];
310  res[2] = this->Data[0] * other.Data[1] - this->Data[1] * other.Data[0];
311  return res;
312  }
314 
316 
319  bool operator<(const vtkVector3<T>& v) const
320  {
321  return (this->Data[0] < v.Data[0]) ||
322  (this->Data[0] == v.Data[0] && this->Data[1] < v.Data[1]) ||
323  (this->Data[0] == v.Data[0] && this->Data[1] == v.Data[1] && this->Data[2] < v.Data[2]);
324  }
326 };
327 
328 // .NAME vtkVector4 - templated base type for storage of 4D vectors.
329 //
330 template <typename T>
331 class vtkVector4 : public vtkVector<T, 4>
332 {
333 public:
334  vtkVector4() = default;
335 
336  explicit vtkVector4(const T& scalar)
337  : vtkVector<T, 4>(scalar)
338  {
339  }
340 
341  explicit vtkVector4(const T* init)
342  : vtkVector<T, 4>(init)
343  {
344  }
345 
346  vtkVector4(const T& x, const T& y, const T& z, const T& w)
347  {
348  this->Data[0] = x;
349  this->Data[1] = y;
350  this->Data[2] = z;
351  this->Data[3] = w;
352  }
353 
355 
358  void Set(const T& x, const T& y, const T& z, const T& w)
359  {
360  this->Data[0] = x;
361  this->Data[1] = y;
362  this->Data[2] = z;
363  this->Data[3] = w;
364  }
366 
370  void SetX(const T& x) { this->Data[0] = x; }
371 
375  const T& GetX() const { return this->Data[0]; }
376 
380  void SetY(const T& y) { this->Data[1] = y; }
381 
385  const T& GetY() const { return this->Data[1]; }
386 
390  void SetZ(const T& z) { this->Data[2] = z; }
391 
395  const T& GetZ() const { return this->Data[2]; }
396 
400  void SetW(const T& w) { this->Data[3] = w; }
401 
405  const T& GetW() const { return this->Data[3]; }
406 };
407 
411 #define vtkVectorNormalized(vectorType, type, size) \
412  vectorType Normalized() const \
413  { \
414  return vectorType(vtkVector<type, size>::Normalized().GetData()); \
415  }
416 
417 #define vtkVectorDerivedMacro(vectorType, type, size) \
418  vtkVectorNormalized(vectorType, type, size); \
419  explicit vectorType(type s) \
420  : Superclass(s) \
421  { \
422  } \
423  explicit vectorType(const type* i) \
424  : Superclass(i) \
425  { \
426  } \
427  explicit vectorType(const vtkTuple<type, size>& o) \
428  : Superclass(o.GetData()) \
429  { \
430  } \
431  vectorType(const vtkVector<type, size>& o) \
432  : Superclass(o.GetData()) \
433  { \
434  }
435 
437 
440 class vtkVector2i : public vtkVector2<int>
441 {
442 public:
444  vtkVector2i() = default;
445  vtkVector2i(int x, int y)
446  : vtkVector2<int>(x, y)
447  {
448  }
450 };
452 
453 class vtkVector2f : public vtkVector2<float>
454 {
455 public:
457  vtkVector2f() = default;
458  vtkVector2f(float x, float y)
459  : vtkVector2<float>(x, y)
460  {
461  }
463 };
464 
465 class vtkVector2d : public vtkVector2<double>
466 {
467 public:
469  vtkVector2d() = default;
470  vtkVector2d(double x, double y)
471  : vtkVector2<double>(x, y)
472  {
473  }
475 };
476 
477 #define vtkVector3Cross(vectorType, type) \
478  vectorType Cross(const vectorType& other) const \
479  { \
480  return vectorType(vtkVector3<type>::Cross(other).GetData()); \
481  }
482 
483 class vtkVector3i : public vtkVector3<int>
484 {
485 public:
487  vtkVector3i() = default;
488  vtkVector3i(int x, int y, int z)
489  : vtkVector3<int>(x, y, z)
490  {
491  }
494 };
495 
496 class vtkVector3f : public vtkVector3<float>
497 {
498 public:
500  vtkVector3f() = default;
501  vtkVector3f(float x, float y, float z)
502  : vtkVector3<float>(x, y, z)
503  {
504  }
507 };
508 
509 class vtkVector3d : public vtkVector3<double>
510 {
511 public:
513  vtkVector3d() = default;
514  vtkVector3d(double x, double y, double z)
515  : vtkVector3<double>(x, y, z)
516  {
517  }
519  vtkVector3Cross(vtkVector3d, double);
520 };
521 
522 class vtkVector4i : public vtkVector4<int>
523 {
524 public:
526  vtkVector4i() = default;
527  vtkVector4i(int x, int y, int z, int w)
528  : vtkVector4<int>(x, y, z, w)
529  {
530  }
532 };
533 
534 class vtkVector4d : public vtkVector4<double>
535 {
536 public:
538  vtkVector4d() = default;
539  vtkVector4d(double x, double y, double z, double w)
540  : vtkVector4<double>(x, y, z, w){};
542 };
543 
544 VTK_ABI_NAMESPACE_END
545 #endif // vtkVector_h
546 // VTK-HeaderTest-Exclude: vtkVector.h
vtkVectorDerivedMacro(vtkVector2i, int, 2)
T Data[Size]
The only thing stored in memory!
Definition: vtkTuple.h:143
void SetZ(const T &z)
Set the z component of the vector, i.e.
Definition: vtkVector.h:390
vtkVector4< int > Superclass
Definition: vtkVector.h:525
vtkVector< T, Size > operator-(const vtkVector< T, Size > &other) const
Substraction operation of this and the supplied vector.
Definition: vtkVector.h:125
vtkVector2(const T &scalar)
Definition: vtkVector.h:176
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:284
double Normalize()
Normalize the vector in place.
Definition: vtkVector.h:77
vtkVector4(const T &scalar)
Definition: vtkVector.h:336
vtkVector3< float > Superclass
Definition: vtkVector.h:499
templated base type for storage of vectors.
Definition: vtkVector.h:29
vtkVector3< int > Superclass
Definition: vtkVector.h:486
const T & GetZ() const
Get the z component of the vector, i.e.
Definition: vtkVector.h:395
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:279
vtkVector2f()=default
vtkVector2i(int x, int y)
Definition: vtkVector.h:445
vtkVector2(const T *init)
Definition: vtkVector.h:181
void Set(const T &x, const T &y)
Set the x and y components of the vector.
Definition: vtkVector.h:196
vtkVector3()=default
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:370
vtkVector3Cross(vtkVector3d, double)
vtkVector4d(double x, double y, double z, double w)
Definition: vtkVector.h:539
vtkVector3d(double x, double y, double z)
Definition: vtkVector.h:514
vtkVector2< int > Superclass
Definition: vtkVector.h:443
vtkVector< T, Size > operator*(const T &other) const
Multiply this vector by a scalar value.
Definition: vtkVector.h:140
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:385
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:289
vtkVector4i()=default
vtkVector3(const T &scalar)
Definition: vtkVector.h:242
vtkVector3Cross(vtkVector3f, float)
vtkVector()=default
vtkVector3< double > Superclass
Definition: vtkVector.h:512
T Dot(const vtkVector< T, Size > &other) const
The dot product of this and the supplied vector.
Definition: vtkVector.h:110
vtkVector3Cross(vtkVector3i, int)
vtkVector4()=default
const T & GetZ() const
Get the z component of the vector, i.e.
Definition: vtkVector.h:299
vtkVectorDerivedMacro(vtkVector4i, int, 4)
vtkVector2f(float x, float y)
Definition: vtkVector.h:458
vtkVector2< double > Superclass
Definition: vtkVector.h:468
vtkVector(const T &scalar)
Initialize all of the vector's elements with the supplied scalar.
Definition: vtkVector.h:37
vtkVector2d(double x, double y)
Definition: vtkVector.h:470
void SetW(const T &w)
Set the w component of the vector, i.e.
Definition: vtkVector.h:400
templated base type for containers of constant size.
Definition: vtkTuple.h:26
vtkVector4(const T &x, const T &y, const T &z, const T &w)
Definition: vtkVector.h:346
vtkVector3i(int x, int y, int z)
Definition: vtkVector.h:488
vtkVector2(const T &x, const T &y)
Definition: vtkVector.h:186
double Norm() const
Get the norm of the vector, i.e.
Definition: vtkVector.h:70
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:274
vtkVector3i()=default
vtkVector4d()=default
vtkVectorDerivedMacro(vtkVector3d, double, 3)
void Set(const T &x, const T &y, const T &z, const T &w)
Set the x, y, z and w components of a 3D vector in homogeneous coordinates.
Definition: vtkVector.h:358
void Set(const T &x, const T &y, const T &z)
Set the x, y and z components of the vector.
Definition: vtkVector.h:263
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:216
vtkVector(const T *init)
Initialize the vector's elements with the elements of the supplied array.
Definition: vtkVector.h:47
vtkVector2< float > Superclass
Definition: vtkVector.h:456
vtkVectorDerivedMacro(vtkVector2d, double, 2)
vtkVector3d()=default
Some derived classes for the different vectors commonly used.
Definition: vtkVector.h:440
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:375
vtkVector3f()=default
vtkVector< T, Size > Normalized() const
Return the normalized form of this vector.
Definition: vtkVector.h:98
vtkVector3< T > Cross(const vtkVector3< T > &other) const
Return the cross product of this X other.
Definition: vtkVector.h:305
vtkVector2d()=default
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:211
vtkVector4i(int x, int y, int z, int w)
Definition: vtkVector.h:527
vtkVectorDerivedMacro(vtkVector3i, int, 3)
vtkVector< TR, Size > Cast() const
Cast the vector to the specified type, returning the result.
Definition: vtkVector.h:156
vtkVector2i()=default
vtkVectorDerivedMacro(vtkVector3f, float, 3)
vtkVector2()=default
vtkVector3(const T &x, const T &y, const T &z)
Definition: vtkVector.h:252
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:206
vtkVectorDerivedMacro(vtkVector4d, double, 4)
vtkVectorDerivedMacro(vtkVector2f, float, 2)
void SetZ(const T &z)
Set the z component of the vector, i.e.
Definition: vtkVector.h:294
const T & GetW() const
Get the w component of the vector, i.e.
Definition: vtkVector.h:405
vtkVector3(const T *init)
Definition: vtkVector.h:247
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:380
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:221
vtkVector4(const T *init)
Definition: vtkVector.h:341
T SquaredNorm() const
Get the squared norm of the vector.
Definition: vtkVector.h:56
vtkVector3f(float x, float y, float z)
Definition: vtkVector.h:501