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 
28 #ifndef vtkVector_h
29 #define vtkVector_h
30 
31 #include "vtkObject.h" // for legacy macros
32 #include "vtkTuple.h"
33 
34 #include <cmath> // For math functions
35 
36 VTK_ABI_NAMESPACE_BEGIN
37 template <typename T, int Size>
38 class vtkVector : public vtkTuple<T, Size>
39 {
40 public:
41  vtkVector() = default;
42 
46  explicit vtkVector(const T& scalar)
47  : vtkTuple<T, Size>(scalar)
48  {
49  }
50 
56  explicit vtkVector(const T* init)
57  : vtkTuple<T, Size>(init)
58  {
59  }
60 
62 
65  T SquaredNorm() const
66  {
67  T result = 0;
68  for (int i = 0; i < Size; ++i)
69  {
70  result += this->Data[i] * this->Data[i];
71  }
72  return result;
73  }
75 
79  double Norm() const { return sqrt(static_cast<double>(this->SquaredNorm())); }
80 
82 
86  double Normalize()
87  {
88  const double norm(this->Norm());
89  if (norm == 0.0)
90  {
91  return 0.0;
92  }
93  const double inv(1.0 / norm);
94  for (int i = 0; i < Size; ++i)
95  {
96  this->Data[i] = static_cast<T>(this->Data[i] * inv);
97  }
98  return norm;
99  }
101 
103 
108  {
109  vtkVector<T, Size> temp(*this);
110  temp.Normalize();
111  return temp;
112  }
114 
116 
119  T Dot(const vtkVector<T, Size>& other) const
120  {
121  T result(0);
122  for (int i = 0; i < Size; ++i)
123  {
124  result += this->Data[i] * other[i];
125  }
126  return result;
127  }
129 
131 
135  {
136  vtkVector<T, Size> result(*this);
137  for (int i = 0; i < Size; ++i)
138  {
139  result[i] = this->Data[i] - other[i];
140  }
141  return result;
142  }
144 
146 
149  vtkVector<T, Size> operator*(const T& other) const
150  {
151  vtkVector<T, Size> result(*this);
152  for (int i = 0; i < Size; ++i)
153  {
154  result[i] = this->Data[i] * other;
155  }
156  return result;
157  }
159 
161 
164  template <typename TR>
166  {
167  vtkVector<TR, Size> result;
168  for (int i = 0; i < Size; ++i)
169  {
170  result[i] = static_cast<TR>(this->Data[i]);
171  }
172  return result;
173  }
175 };
176 
177 // .NAME vtkVector2 - templated base type for storage of 2D vectors.
178 //
179 template <typename T>
180 class vtkVector2 : public vtkVector<T, 2>
181 {
182 public:
183  vtkVector2() = default;
184 
185  explicit vtkVector2(const T& scalar)
186  : vtkVector<T, 2>(scalar)
187  {
188  }
189 
190  explicit vtkVector2(const T* init)
191  : vtkVector<T, 2>(init)
192  {
193  }
194 
195  vtkVector2(const T& x, const T& y)
196  {
197  this->Data[0] = x;
198  this->Data[1] = y;
199  }
200 
202 
205  void Set(const T& x, const T& y)
206  {
207  this->Data[0] = x;
208  this->Data[1] = y;
209  }
211 
215  void SetX(const T& x) { this->Data[0] = x; }
216 
220  const T& GetX() const { return this->Data[0]; }
221 
225  void SetY(const T& y) { this->Data[1] = y; }
226 
230  const T& GetY() const { return this->Data[1]; }
231 
233 
236  bool operator<(const vtkVector2<T>& v) const
237  {
238  return (this->Data[0] < v.Data[0]) || (this->Data[0] == v.Data[0] && this->Data[1] < v.Data[1]);
239  }
241 };
242 
243 // .NAME vtkVector3 - templated base type for storage of 3D vectors.
244 //
245 template <typename T>
246 class vtkVector3 : public vtkVector<T, 3>
247 {
248 public:
249  vtkVector3() = default;
250 
251  explicit vtkVector3(const T& scalar)
252  : vtkVector<T, 3>(scalar)
253  {
254  }
255 
256  explicit vtkVector3(const T* init)
257  : vtkVector<T, 3>(init)
258  {
259  }
260 
261  vtkVector3(const T& x, const T& y, const T& z)
262  {
263  this->Data[0] = x;
264  this->Data[1] = y;
265  this->Data[2] = z;
266  }
267 
269 
272  void Set(const T& x, const T& y, const T& z)
273  {
274  this->Data[0] = x;
275  this->Data[1] = y;
276  this->Data[2] = z;
277  }
279 
283  void SetX(const T& x) { this->Data[0] = x; }
284 
288  const T& GetX() const { return this->Data[0]; }
289 
293  void SetY(const T& y) { this->Data[1] = y; }
294 
298  const T& GetY() const { return this->Data[1]; }
299 
303  void SetZ(const T& z) { this->Data[2] = z; }
304 
308  const T& GetZ() const { return this->Data[2]; }
309 
311 
314  vtkVector3<T> Cross(const vtkVector3<T>& other) const
315  {
316  vtkVector3<T> res;
317  res[0] = this->Data[1] * other.Data[2] - this->Data[2] * other.Data[1];
318  res[1] = this->Data[2] * other.Data[0] - this->Data[0] * other.Data[2];
319  res[2] = this->Data[0] * other.Data[1] - this->Data[1] * other.Data[0];
320  return res;
321  }
323 
325 
328  bool operator<(const vtkVector3<T>& v) const
329  {
330  return (this->Data[0] < v.Data[0]) ||
331  (this->Data[0] == v.Data[0] && this->Data[1] < v.Data[1]) ||
332  (this->Data[0] == v.Data[0] && this->Data[1] == v.Data[1] && this->Data[2] < v.Data[2]);
333  }
335 };
336 
337 // .NAME vtkVector4 - templated base type for storage of 4D vectors.
338 //
339 template <typename T>
340 class vtkVector4 : public vtkVector<T, 4>
341 {
342 public:
343  vtkVector4() = default;
344 
345  explicit vtkVector4(const T& scalar)
346  : vtkVector<T, 4>(scalar)
347  {
348  }
349 
350  explicit vtkVector4(const T* init)
351  : vtkVector<T, 4>(init)
352  {
353  }
354 
355  vtkVector4(const T& x, const T& y, const T& z, const T& w)
356  {
357  this->Data[0] = x;
358  this->Data[1] = y;
359  this->Data[2] = z;
360  this->Data[3] = w;
361  }
362 
364 
367  void Set(const T& x, const T& y, const T& z, const T& w)
368  {
369  this->Data[0] = x;
370  this->Data[1] = y;
371  this->Data[2] = z;
372  this->Data[3] = w;
373  }
375 
379  void SetX(const T& x) { this->Data[0] = x; }
380 
384  const T& GetX() const { return this->Data[0]; }
385 
389  void SetY(const T& y) { this->Data[1] = y; }
390 
394  const T& GetY() const { return this->Data[1]; }
395 
399  void SetZ(const T& z) { this->Data[2] = z; }
400 
404  const T& GetZ() const { return this->Data[2]; }
405 
409  void SetW(const T& w) { this->Data[3] = w; }
410 
414  const T& GetW() const { return this->Data[3]; }
415 };
416 
420 #define vtkVectorNormalized(vectorType, type, size) \
421  vectorType Normalized() const \
422  { \
423  return vectorType(vtkVector<type, size>::Normalized().GetData()); \
424  }
425 
426 #define vtkVectorDerivedMacro(vectorType, type, size) \
427  vtkVectorNormalized(vectorType, type, size); \
428  explicit vectorType(type s) \
429  : Superclass(s) \
430  { \
431  } \
432  explicit vectorType(const type* i) \
433  : Superclass(i) \
434  { \
435  } \
436  explicit vectorType(const vtkTuple<type, size>& o) \
437  : Superclass(o.GetData()) \
438  { \
439  } \
440  vectorType(const vtkVector<type, size>& o) \
441  : Superclass(o.GetData()) \
442  { \
443  }
444 
446 
449 class vtkVector2i : public vtkVector2<int>
450 {
451 public:
453  vtkVector2i() = default;
454  vtkVector2i(int x, int y)
455  : vtkVector2<int>(x, y)
456  {
457  }
459 };
461 
462 class vtkVector2f : public vtkVector2<float>
463 {
464 public:
466  vtkVector2f() = default;
467  vtkVector2f(float x, float y)
468  : vtkVector2<float>(x, y)
469  {
470  }
472 };
473 
474 class vtkVector2d : public vtkVector2<double>
475 {
476 public:
478  vtkVector2d() = default;
479  vtkVector2d(double x, double y)
480  : vtkVector2<double>(x, y)
481  {
482  }
484 };
485 
486 #define vtkVector3Cross(vectorType, type) \
487  vectorType Cross(const vectorType& other) const \
488  { \
489  return vectorType(vtkVector3<type>::Cross(other).GetData()); \
490  }
491 
492 class vtkVector3i : public vtkVector3<int>
493 {
494 public:
496  vtkVector3i() = default;
497  vtkVector3i(int x, int y, int z)
498  : vtkVector3<int>(x, y, z)
499  {
500  }
503 };
504 
505 class vtkVector3f : public vtkVector3<float>
506 {
507 public:
509  vtkVector3f() = default;
510  vtkVector3f(float x, float y, float z)
511  : vtkVector3<float>(x, y, z)
512  {
513  }
516 };
517 
518 class vtkVector3d : public vtkVector3<double>
519 {
520 public:
522  vtkVector3d() = default;
523  vtkVector3d(double x, double y, double z)
524  : vtkVector3<double>(x, y, z)
525  {
526  }
529 };
530 
531 class vtkVector4i : public vtkVector4<int>
532 {
533 public:
535  vtkVector4i() = default;
536  vtkVector4i(int x, int y, int z, int w)
537  : vtkVector4<int>(x, y, z, w)
538  {
539  }
541 };
542 
543 class vtkVector4d : public vtkVector4<double>
544 {
545 public:
547  vtkVector4d() = default;
548  vtkVector4d(double x, double y, double z, double w)
549  : vtkVector4<double>(x, y, z, w){};
551 };
552 
553 VTK_ABI_NAMESPACE_END
554 #endif // vtkVector_h
555 // VTK-HeaderTest-Exclude: vtkVector.h
templated base type for containers of constant size.
Definition: vtkTuple.h:27
T Data[Size]
The only thing stored in memory!
Definition: vtkTuple.h:143
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:230
void Set(const T &x, const T &y)
Set the x and y components of the vector.
Definition: vtkVector.h:205
vtkVector2(const T *init)
Definition: vtkVector.h:190
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:225
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:220
vtkVector2(const T &x, const T &y)
Definition: vtkVector.h:195
vtkVector2(const T &scalar)
Definition: vtkVector.h:185
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:215
bool operator<(const vtkVector2< T > &v) const
Lexicographical comparison of two vector.
Definition: vtkVector.h:236
vtkVector2()=default
vtkVector2d()=default
vtkVectorDerivedMacro(vtkVector2d, double, 2)
vtkVector2d(double x, double y)
Definition: vtkVector.h:479
vtkVector2< double > Superclass
Definition: vtkVector.h:477
vtkVector2f()=default
vtkVector2< float > Superclass
Definition: vtkVector.h:465
vtkVector2f(float x, float y)
Definition: vtkVector.h:467
vtkVectorDerivedMacro(vtkVector2f, float, 2)
Some derived classes for the different vectors commonly used.
Definition: vtkVector.h:450
vtkVector2i()=default
vtkVector2i(int x, int y)
Definition: vtkVector.h:454
vtkVector2< int > Superclass
Definition: vtkVector.h:452
vtkVectorDerivedMacro(vtkVector2i, int, 2)
vtkVector3< T > Cross(const vtkVector3< T > &other) const
Return the cross product of this X other.
Definition: vtkVector.h:314
void SetZ(const T &z)
Set the z component of the vector, i.e.
Definition: vtkVector.h:303
const T & GetZ() const
Get the z component of the vector, i.e.
Definition: vtkVector.h:308
bool operator<(const vtkVector3< T > &v) const
Lexicographical comparison of two vector.
Definition: vtkVector.h:328
vtkVector3(const T *init)
Definition: vtkVector.h:256
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:283
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:298
vtkVector3(const T &scalar)
Definition: vtkVector.h:251
void Set(const T &x, const T &y, const T &z)
Set the x, y and z components of the vector.
Definition: vtkVector.h:272
vtkVector3(const T &x, const T &y, const T &z)
Definition: vtkVector.h:261
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:288
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:293
vtkVector3()=default
vtkVector3Cross(vtkVector3d, double)
vtkVector3< double > Superclass
Definition: vtkVector.h:521
vtkVector3d(double x, double y, double z)
Definition: vtkVector.h:523
vtkVector3d()=default
vtkVectorDerivedMacro(vtkVector3d, double, 3)
vtkVector3Cross(vtkVector3f, float)
vtkVector3f()=default
vtkVectorDerivedMacro(vtkVector3f, float, 3)
vtkVector3f(float x, float y, float z)
Definition: vtkVector.h:510
vtkVector3< float > Superclass
Definition: vtkVector.h:508
vtkVector3Cross(vtkVector3i, int)
vtkVectorDerivedMacro(vtkVector3i, int, 3)
vtkVector3< int > Superclass
Definition: vtkVector.h:495
vtkVector3i()=default
vtkVector3i(int x, int y, int z)
Definition: vtkVector.h:497
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:384
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:389
vtkVector4(const T &x, const T &y, const T &z, const T &w)
Definition: vtkVector.h:355
void SetZ(const T &z)
Set the z component of the vector, i.e.
Definition: vtkVector.h:399
void SetW(const T &w)
Set the w component of the vector, i.e.
Definition: vtkVector.h:409
vtkVector4(const T *init)
Definition: vtkVector.h:350
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:367
vtkVector4()=default
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:379
vtkVector4(const T &scalar)
Definition: vtkVector.h:345
const T & GetZ() const
Get the z component of the vector, i.e.
Definition: vtkVector.h:404
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:394
const T & GetW() const
Get the w component of the vector, i.e.
Definition: vtkVector.h:414
vtkVectorDerivedMacro(vtkVector4d, double, 4)
vtkVector4d(double x, double y, double z, double w)
Definition: vtkVector.h:548
vtkVector4d()=default
vtkVector4< int > Superclass
Definition: vtkVector.h:534
vtkVector4i(int x, int y, int z, int w)
Definition: vtkVector.h:536
vtkVector4i()=default
vtkVectorDerivedMacro(vtkVector4i, int, 4)
templated base type for storage of vectors.
Definition: vtkVector.h:39
vtkVector(const T &scalar)
Initialize all of the vector's elements with the supplied scalar.
Definition: vtkVector.h:46
T Dot(const vtkVector< T, Size > &other) const
The dot product of this and the supplied vector.
Definition: vtkVector.h:119
vtkVector< TR, Size > Cast() const
Cast the vector to the specified type, returning the result.
Definition: vtkVector.h:165
double Normalize()
Normalize the vector in place.
Definition: vtkVector.h:86
vtkVector< T, Size > Normalized() const
Return the normalized form of this vector.
Definition: vtkVector.h:107
vtkVector()=default
vtkVector(const T *init)
Initialize the vector's elements with the elements of the supplied array.
Definition: vtkVector.h:56
vtkVector< T, Size > operator*(const T &other) const
Multiply this vector by a scalar value.
Definition: vtkVector.h:149
double Norm() const
Get the norm of the vector, i.e.
Definition: vtkVector.h:79
vtkVector< T, Size > operator-(const vtkVector< T, Size > &other) const
Substraction operation of this and the supplied vector.
Definition: vtkVector.h:134
T SquaredNorm() const
Get the squared norm of the vector.
Definition: vtkVector.h:65