VTK  9.3.20240318
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 
48 #ifndef vtkVector_h
49 #define vtkVector_h
50 
51 #include "vtkObject.h" // for legacy macros
52 #include "vtkTuple.h"
53 
54 #include <cmath> // For math functions
55 
56 VTK_ABI_NAMESPACE_BEGIN
57 template <typename T, int Size>
58 class vtkVector : public vtkTuple<T, Size>
59 {
60 public:
61  vtkVector() = default;
62 
66  explicit vtkVector(const T& scalar)
67  : vtkTuple<T, Size>(scalar)
68  {
69  }
70 
76  explicit vtkVector(const T* init)
77  : vtkTuple<T, Size>(init)
78  {
79  }
80 
82 
85  T SquaredNorm() const
86  {
87  T result = 0;
88  for (int i = 0; i < Size; ++i)
89  {
90  result += this->Data[i] * this->Data[i];
91  }
92  return result;
93  }
95 
99  double Norm() const { return sqrt(static_cast<double>(this->SquaredNorm())); }
100 
102 
106  double Normalize()
107  {
108  const double norm(this->Norm());
109  if (norm == 0.0)
110  {
111  return 0.0;
112  }
113  const double inv(1.0 / norm);
114  for (int i = 0; i < Size; ++i)
115  {
116  this->Data[i] = static_cast<T>(this->Data[i] * inv);
117  }
118  return norm;
119  }
121 
123 
128  {
129  vtkVector<T, Size> temp(*this);
130  temp.Normalize();
131  return temp;
132  }
134 
136 
139  T Dot(const vtkVector<T, Size>& other) const
140  {
141  T result(0);
142  for (int i = 0; i < Size; ++i)
143  {
144  result += this->Data[i] * other[i];
145  }
146  return result;
147  }
149 
151 
155  {
156  vtkVector<T, Size> result(*this);
157  for (int i = 0; i < Size; ++i)
158  {
159  result[i] = this->Data[i] - other[i];
160  }
161  return result;
162  }
164 
166 
169  vtkVector<T, Size> operator*(const T& other) const
170  {
171  vtkVector<T, Size> result(*this);
172  for (int i = 0; i < Size; ++i)
173  {
174  result[i] = this->Data[i] * other;
175  }
176  return result;
177  }
179 
181 
184  template <typename TR>
186  {
187  vtkVector<TR, Size> result;
188  for (int i = 0; i < Size; ++i)
189  {
190  result[i] = static_cast<TR>(this->Data[i]);
191  }
192  return result;
193  }
195 };
196 
197 // .NAME vtkVector2 - templated base type for storage of 2D vectors.
198 //
199 template <typename T>
200 class vtkVector2 : public vtkVector<T, 2>
201 {
202 public:
203  vtkVector2() = default;
204 
205  explicit vtkVector2(const T& scalar)
206  : vtkVector<T, 2>(scalar)
207  {
208  }
209 
210  explicit vtkVector2(const T* init)
211  : vtkVector<T, 2>(init)
212  {
213  }
214 
215  vtkVector2(const T& x, const T& y)
216  {
217  this->Data[0] = x;
218  this->Data[1] = y;
219  }
220 
222 
225  void Set(const T& x, const T& y)
226  {
227  this->Data[0] = x;
228  this->Data[1] = y;
229  }
231 
235  void SetX(const T& x) { this->Data[0] = x; }
236 
240  const T& GetX() const { return this->Data[0]; }
241 
245  void SetY(const T& y) { this->Data[1] = y; }
246 
250  const T& GetY() const { return this->Data[1]; }
251 
253 
256  bool operator<(const vtkVector2<T>& v) const
257  {
258  return (this->Data[0] < v.Data[0]) || (this->Data[0] == v.Data[0] && this->Data[1] < v.Data[1]);
259  }
261 };
262 
263 // .NAME vtkVector3 - templated base type for storage of 3D vectors.
264 //
265 template <typename T>
266 class vtkVector3 : public vtkVector<T, 3>
267 {
268 public:
269  vtkVector3() = default;
270 
271  explicit vtkVector3(const T& scalar)
272  : vtkVector<T, 3>(scalar)
273  {
274  }
275 
276  explicit vtkVector3(const T* init)
277  : vtkVector<T, 3>(init)
278  {
279  }
280 
281  vtkVector3(const T& x, const T& y, const T& z)
282  {
283  this->Data[0] = x;
284  this->Data[1] = y;
285  this->Data[2] = z;
286  }
287 
289 
292  void Set(const T& x, const T& y, const T& z)
293  {
294  this->Data[0] = x;
295  this->Data[1] = y;
296  this->Data[2] = z;
297  }
299 
303  void SetX(const T& x) { this->Data[0] = x; }
304 
308  const T& GetX() const { return this->Data[0]; }
309 
313  void SetY(const T& y) { this->Data[1] = y; }
314 
318  const T& GetY() const { return this->Data[1]; }
319 
323  void SetZ(const T& z) { this->Data[2] = z; }
324 
328  const T& GetZ() const { return this->Data[2]; }
329 
331 
334  vtkVector3<T> Cross(const vtkVector3<T>& other) const
335  {
336  vtkVector3<T> res;
337  res[0] = this->Data[1] * other.Data[2] - this->Data[2] * other.Data[1];
338  res[1] = this->Data[2] * other.Data[0] - this->Data[0] * other.Data[2];
339  res[2] = this->Data[0] * other.Data[1] - this->Data[1] * other.Data[0];
340  return res;
341  }
343 
345 
348  bool operator<(const vtkVector3<T>& v) const
349  {
350  return (this->Data[0] < v.Data[0]) ||
351  (this->Data[0] == v.Data[0] && this->Data[1] < v.Data[1]) ||
352  (this->Data[0] == v.Data[0] && this->Data[1] == v.Data[1] && this->Data[2] < v.Data[2]);
353  }
355 };
356 
357 // .NAME vtkVector4 - templated base type for storage of 4D vectors.
358 //
359 template <typename T>
360 class vtkVector4 : public vtkVector<T, 4>
361 {
362 public:
363  vtkVector4() = default;
364 
365  explicit vtkVector4(const T& scalar)
366  : vtkVector<T, 4>(scalar)
367  {
368  }
369 
370  explicit vtkVector4(const T* init)
371  : vtkVector<T, 4>(init)
372  {
373  }
374 
375  vtkVector4(const T& x, const T& y, const T& z, const T& w)
376  {
377  this->Data[0] = x;
378  this->Data[1] = y;
379  this->Data[2] = z;
380  this->Data[3] = w;
381  }
382 
384 
387  void Set(const T& x, const T& y, const T& z, const T& w)
388  {
389  this->Data[0] = x;
390  this->Data[1] = y;
391  this->Data[2] = z;
392  this->Data[3] = w;
393  }
395 
399  void SetX(const T& x) { this->Data[0] = x; }
400 
404  const T& GetX() const { return this->Data[0]; }
405 
409  void SetY(const T& y) { this->Data[1] = y; }
410 
414  const T& GetY() const { return this->Data[1]; }
415 
419  void SetZ(const T& z) { this->Data[2] = z; }
420 
424  const T& GetZ() const { return this->Data[2]; }
425 
429  void SetW(const T& w) { this->Data[3] = w; }
430 
434  const T& GetW() const { return this->Data[3]; }
435 };
436 
440 #define vtkVectorNormalized(vectorType, type, size) \
441  vectorType Normalized() const \
442  { \
443  return vectorType(vtkVector<type, size>::Normalized().GetData()); \
444  }
445 
446 #define vtkVectorDerivedMacro(vectorType, type, size) \
447  vtkVectorNormalized(vectorType, type, size); \
448  explicit vectorType(type s) \
449  : Superclass(s) \
450  { \
451  } \
452  explicit vectorType(const type* i) \
453  : Superclass(i) \
454  { \
455  } \
456  explicit vectorType(const vtkTuple<type, size>& o) \
457  : Superclass(o.GetData()) \
458  { \
459  } \
460  vectorType(const vtkVector<type, size>& o) \
461  : Superclass(o.GetData()) \
462  { \
463  }
464 
466 
469 class vtkVector2i : public vtkVector2<int>
470 {
471 public:
473  vtkVector2i() = default;
474  vtkVector2i(int x, int y)
475  : vtkVector2<int>(x, y)
476  {
477  }
479 };
481 
482 class vtkVector2f : public vtkVector2<float>
483 {
484 public:
486  vtkVector2f() = default;
487  vtkVector2f(float x, float y)
488  : vtkVector2<float>(x, y)
489  {
490  }
492 };
493 
494 class vtkVector2d : public vtkVector2<double>
495 {
496 public:
498  vtkVector2d() = default;
499  vtkVector2d(double x, double y)
500  : vtkVector2<double>(x, y)
501  {
502  }
504 };
505 
506 #define vtkVector3Cross(vectorType, type) \
507  vectorType Cross(const vectorType& other) const \
508  { \
509  return vectorType(vtkVector3<type>::Cross(other).GetData()); \
510  }
511 
512 class vtkVector3i : public vtkVector3<int>
513 {
514 public:
516  vtkVector3i() = default;
517  vtkVector3i(int x, int y, int z)
518  : vtkVector3<int>(x, y, z)
519  {
520  }
523 };
524 
525 class vtkVector3f : public vtkVector3<float>
526 {
527 public:
529  vtkVector3f() = default;
530  vtkVector3f(float x, float y, float z)
531  : vtkVector3<float>(x, y, z)
532  {
533  }
536 };
537 
538 class vtkVector3d : public vtkVector3<double>
539 {
540 public:
542  vtkVector3d() = default;
543  vtkVector3d(double x, double y, double z)
544  : vtkVector3<double>(x, y, z)
545  {
546  }
549 };
550 
551 class vtkVector4i : public vtkVector4<int>
552 {
553 public:
555  vtkVector4i() = default;
556  vtkVector4i(int x, int y, int z, int w)
557  : vtkVector4<int>(x, y, z, w)
558  {
559  }
561 };
562 
563 class vtkVector4d : public vtkVector4<double>
564 {
565 public:
567  vtkVector4d() = default;
568  vtkVector4d(double x, double y, double z, double w)
569  : vtkVector4<double>(x, y, z, w)
570  {
571  }
573 };
574 
575 VTK_ABI_NAMESPACE_END
576 #endif // vtkVector_h
577 // 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:250
void Set(const T &x, const T &y)
Set the x and y components of the vector.
Definition: vtkVector.h:225
vtkVector2(const T *init)
Definition: vtkVector.h:210
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:245
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:240
vtkVector2(const T &x, const T &y)
Definition: vtkVector.h:215
vtkVector2(const T &scalar)
Definition: vtkVector.h:205
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:235
bool operator<(const vtkVector2< T > &v) const
Lexicographical comparison of two vector.
Definition: vtkVector.h:256
vtkVector2()=default
vtkVector2d()=default
vtkVectorDerivedMacro(vtkVector2d, double, 2)
vtkVector2d(double x, double y)
Definition: vtkVector.h:499
vtkVector2< double > Superclass
Definition: vtkVector.h:497
vtkVector2f()=default
vtkVector2< float > Superclass
Definition: vtkVector.h:485
vtkVector2f(float x, float y)
Definition: vtkVector.h:487
vtkVectorDerivedMacro(vtkVector2f, float, 2)
Some derived classes for the different vectors commonly used.
Definition: vtkVector.h:470
vtkVector2i()=default
vtkVector2i(int x, int y)
Definition: vtkVector.h:474
vtkVector2< int > Superclass
Definition: vtkVector.h:472
vtkVectorDerivedMacro(vtkVector2i, int, 2)
vtkVector3< T > Cross(const vtkVector3< T > &other) const
Return the cross product of this X other.
Definition: vtkVector.h:334
void SetZ(const T &z)
Set the z component of the vector, i.e.
Definition: vtkVector.h:323
const T & GetZ() const
Get the z component of the vector, i.e.
Definition: vtkVector.h:328
bool operator<(const vtkVector3< T > &v) const
Lexicographical comparison of two vector.
Definition: vtkVector.h:348
vtkVector3(const T *init)
Definition: vtkVector.h:276
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:303
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:318
vtkVector3(const T &scalar)
Definition: vtkVector.h:271
void Set(const T &x, const T &y, const T &z)
Set the x, y and z components of the vector.
Definition: vtkVector.h:292
vtkVector3(const T &x, const T &y, const T &z)
Definition: vtkVector.h:281
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:308
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:313
vtkVector3()=default
vtkVector3Cross(vtkVector3d, double)
vtkVector3< double > Superclass
Definition: vtkVector.h:541
vtkVector3d(double x, double y, double z)
Definition: vtkVector.h:543
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:530
vtkVector3< float > Superclass
Definition: vtkVector.h:528
vtkVector3Cross(vtkVector3i, int)
vtkVectorDerivedMacro(vtkVector3i, int, 3)
vtkVector3< int > Superclass
Definition: vtkVector.h:515
vtkVector3i()=default
vtkVector3i(int x, int y, int z)
Definition: vtkVector.h:517
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:404
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:409
vtkVector4(const T &x, const T &y, const T &z, const T &w)
Definition: vtkVector.h:375
void SetZ(const T &z)
Set the z component of the vector, i.e.
Definition: vtkVector.h:419
void SetW(const T &w)
Set the w component of the vector, i.e.
Definition: vtkVector.h:429
vtkVector4(const T *init)
Definition: vtkVector.h:370
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:387
vtkVector4()=default
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:399
vtkVector4(const T &scalar)
Definition: vtkVector.h:365
const T & GetZ() const
Get the z component of the vector, i.e.
Definition: vtkVector.h:424
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:414
const T & GetW() const
Get the w component of the vector, i.e.
Definition: vtkVector.h:434
vtkVectorDerivedMacro(vtkVector4d, double, 4)
vtkVector4d(double x, double y, double z, double w)
Definition: vtkVector.h:568
vtkVector4d()=default
vtkVector4< int > Superclass
Definition: vtkVector.h:554
vtkVector4i(int x, int y, int z, int w)
Definition: vtkVector.h:556
vtkVector4i()=default
vtkVectorDerivedMacro(vtkVector4i, int, 4)
templated base type for storage of vectors.
Definition: vtkVector.h:59
vtkVector(const T &scalar)
Initialize all of the vector's elements with the supplied scalar.
Definition: vtkVector.h:66
T Dot(const vtkVector< T, Size > &other) const
The dot product of this and the supplied vector.
Definition: vtkVector.h:139
vtkVector< TR, Size > Cast() const
Cast the vector to the specified type, returning the result.
Definition: vtkVector.h:185
double Normalize()
Normalize the vector in place.
Definition: vtkVector.h:106
vtkVector< T, Size > Normalized() const
Return the normalized form of this vector.
Definition: vtkVector.h:127
vtkVector()=default
vtkVector(const T *init)
Initialize the vector's elements with the elements of the supplied array.
Definition: vtkVector.h:76
vtkVector< T, Size > operator*(const T &other) const
Multiply this vector by a scalar value.
Definition: vtkVector.h:169
double Norm() const
Get the norm of the vector, i.e.
Definition: vtkVector.h:99
vtkVector< T, Size > operator-(const vtkVector< T, Size > &other) const
Substraction operation of this and the supplied vector.
Definition: vtkVector.h:154
T SquaredNorm() const
Get the squared norm of the vector.
Definition: vtkVector.h:85