VTK  9.3.0
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 
134  template <typename TR>
136  {
137  vtkVector<TR, Size> result;
138  for (int i = 0; i < Size; ++i)
139  {
140  result[i] = static_cast<TR>(this->Data[i]);
141  }
142  return result;
143  }
145 };
146 
147 // .NAME vtkVector2 - templated base type for storage of 2D vectors.
148 //
149 template <typename T>
150 class vtkVector2 : public vtkVector<T, 2>
151 {
152 public:
153  vtkVector2() = default;
154 
155  explicit vtkVector2(const T& scalar)
156  : vtkVector<T, 2>(scalar)
157  {
158  }
159 
160  explicit vtkVector2(const T* init)
161  : vtkVector<T, 2>(init)
162  {
163  }
164 
165  vtkVector2(const T& x, const T& y)
166  {
167  this->Data[0] = x;
168  this->Data[1] = y;
169  }
170 
172 
175  void Set(const T& x, const T& y)
176  {
177  this->Data[0] = x;
178  this->Data[1] = y;
179  }
181 
185  void SetX(const T& x) { this->Data[0] = x; }
186 
190  const T& GetX() const { return this->Data[0]; }
191 
195  void SetY(const T& y) { this->Data[1] = y; }
196 
200  const T& GetY() const { return this->Data[1]; }
201 
203 
206  bool operator<(const vtkVector2<T>& v) const
207  {
208  return (this->Data[0] < v.Data[0]) || (this->Data[0] == v.Data[0] && this->Data[1] < v.Data[1]);
209  }
211 };
212 
213 // .NAME vtkVector3 - templated base type for storage of 3D vectors.
214 //
215 template <typename T>
216 class vtkVector3 : public vtkVector<T, 3>
217 {
218 public:
219  vtkVector3() = default;
220 
221  explicit vtkVector3(const T& scalar)
222  : vtkVector<T, 3>(scalar)
223  {
224  }
225 
226  explicit vtkVector3(const T* init)
227  : vtkVector<T, 3>(init)
228  {
229  }
230 
231  vtkVector3(const T& x, const T& y, const T& z)
232  {
233  this->Data[0] = x;
234  this->Data[1] = y;
235  this->Data[2] = z;
236  }
237 
239 
242  void Set(const T& x, const T& y, const T& z)
243  {
244  this->Data[0] = x;
245  this->Data[1] = y;
246  this->Data[2] = z;
247  }
249 
253  void SetX(const T& x) { this->Data[0] = x; }
254 
258  const T& GetX() const { return this->Data[0]; }
259 
263  void SetY(const T& y) { this->Data[1] = y; }
264 
268  const T& GetY() const { return this->Data[1]; }
269 
273  void SetZ(const T& z) { this->Data[2] = z; }
274 
278  const T& GetZ() const { return this->Data[2]; }
279 
281 
284  vtkVector3<T> Cross(const vtkVector3<T>& other) const
285  {
286  vtkVector3<T> res;
287  res[0] = this->Data[1] * other.Data[2] - this->Data[2] * other.Data[1];
288  res[1] = this->Data[2] * other.Data[0] - this->Data[0] * other.Data[2];
289  res[2] = this->Data[0] * other.Data[1] - this->Data[1] * other.Data[0];
290  return res;
291  }
293 
295 
298  bool operator<(const vtkVector3<T>& v) const
299  {
300  return (this->Data[0] < v.Data[0]) ||
301  (this->Data[0] == v.Data[0] && this->Data[1] < v.Data[1]) ||
302  (this->Data[0] == v.Data[0] && this->Data[1] == v.Data[1] && this->Data[2] < v.Data[2]);
303  }
305 };
306 
307 // .NAME vtkVector4 - templated base type for storage of 4D vectors.
308 //
309 template <typename T>
310 class vtkVector4 : public vtkVector<T, 4>
311 {
312 public:
313  vtkVector4() = default;
314 
315  explicit vtkVector4(const T& scalar)
316  : vtkVector<T, 4>(scalar)
317  {
318  }
319 
320  explicit vtkVector4(const T* init)
321  : vtkVector<T, 4>(init)
322  {
323  }
324 
325  vtkVector4(const T& x, const T& y, const T& z, const T& w)
326  {
327  this->Data[0] = x;
328  this->Data[1] = y;
329  this->Data[2] = z;
330  this->Data[3] = w;
331  }
332 
334 
337  void Set(const T& x, const T& y, const T& z, const T& w)
338  {
339  this->Data[0] = x;
340  this->Data[1] = y;
341  this->Data[2] = z;
342  this->Data[3] = w;
343  }
345 
349  void SetX(const T& x) { this->Data[0] = x; }
350 
354  const T& GetX() const { return this->Data[0]; }
355 
359  void SetY(const T& y) { this->Data[1] = y; }
360 
364  const T& GetY() const { return this->Data[1]; }
365 
369  void SetZ(const T& z) { this->Data[2] = z; }
370 
374  const T& GetZ() const { return this->Data[2]; }
375 
379  void SetW(const T& w) { this->Data[3] = w; }
380 
384  const T& GetW() const { return this->Data[3]; }
385 };
386 
390 #define vtkVectorNormalized(vectorType, type, size) \
391  vectorType Normalized() const \
392  { \
393  return vectorType(vtkVector<type, size>::Normalized().GetData()); \
394  }
395 
396 #define vtkVectorDerivedMacro(vectorType, type, size) \
397  vtkVectorNormalized(vectorType, type, size); \
398  explicit vectorType(type s) \
399  : Superclass(s) \
400  { \
401  } \
402  explicit vectorType(const type* i) \
403  : Superclass(i) \
404  { \
405  } \
406  explicit vectorType(const vtkTuple<type, size>& o) \
407  : Superclass(o.GetData()) \
408  { \
409  } \
410  vectorType(const vtkVector<type, size>& o) \
411  : Superclass(o.GetData()) \
412  { \
413  }
414 
416 
419 class vtkVector2i : public vtkVector2<int>
420 {
421 public:
423  vtkVector2i() = default;
424  vtkVector2i(int x, int y)
425  : vtkVector2<int>(x, y)
426  {
427  }
429 };
431 
432 class vtkVector2f : public vtkVector2<float>
433 {
434 public:
436  vtkVector2f() = default;
437  vtkVector2f(float x, float y)
438  : vtkVector2<float>(x, y)
439  {
440  }
442 };
443 
444 class vtkVector2d : public vtkVector2<double>
445 {
446 public:
448  vtkVector2d() = default;
449  vtkVector2d(double x, double y)
450  : vtkVector2<double>(x, y)
451  {
452  }
454 };
455 
456 #define vtkVector3Cross(vectorType, type) \
457  vectorType Cross(const vectorType& other) const \
458  { \
459  return vectorType(vtkVector3<type>::Cross(other).GetData()); \
460  }
461 
462 class vtkVector3i : public vtkVector3<int>
463 {
464 public:
466  vtkVector3i() = default;
467  vtkVector3i(int x, int y, int z)
468  : vtkVector3<int>(x, y, z)
469  {
470  }
473 };
474 
475 class vtkVector3f : public vtkVector3<float>
476 {
477 public:
479  vtkVector3f() = default;
480  vtkVector3f(float x, float y, float z)
481  : vtkVector3<float>(x, y, z)
482  {
483  }
486 };
487 
488 class vtkVector3d : public vtkVector3<double>
489 {
490 public:
492  vtkVector3d() = default;
493  vtkVector3d(double x, double y, double z)
494  : vtkVector3<double>(x, y, z)
495  {
496  }
499 };
500 
501 class vtkVector4i : public vtkVector4<int>
502 {
503 public:
505  vtkVector4i() = default;
506  vtkVector4i(int x, int y, int z, int w)
507  : vtkVector4<int>(x, y, z, w)
508  {
509  }
511 };
512 
513 class vtkVector4d : public vtkVector4<double>
514 {
515 public:
517  vtkVector4d() = default;
518  vtkVector4d(double x, double y, double z, double w)
519  : vtkVector4<double>(x, y, z, w){};
521 };
522 
523 VTK_ABI_NAMESPACE_END
524 #endif // vtkVector_h
525 // 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:200
void Set(const T &x, const T &y)
Set the x and y components of the vector.
Definition: vtkVector.h:175
vtkVector2(const T *init)
Definition: vtkVector.h:160
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:195
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:190
vtkVector2(const T &x, const T &y)
Definition: vtkVector.h:165
vtkVector2(const T &scalar)
Definition: vtkVector.h:155
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:185
bool operator<(const vtkVector2< T > &v) const
Lexicographical comparison of two vector.
Definition: vtkVector.h:206
vtkVector2()=default
vtkVector2d()=default
vtkVectorDerivedMacro(vtkVector2d, double, 2)
vtkVector2d(double x, double y)
Definition: vtkVector.h:449
vtkVector2< double > Superclass
Definition: vtkVector.h:447
vtkVector2f()=default
vtkVector2< float > Superclass
Definition: vtkVector.h:435
vtkVector2f(float x, float y)
Definition: vtkVector.h:437
vtkVectorDerivedMacro(vtkVector2f, float, 2)
Some derived classes for the different vectors commonly used.
Definition: vtkVector.h:420
vtkVector2i()=default
vtkVector2i(int x, int y)
Definition: vtkVector.h:424
vtkVector2< int > Superclass
Definition: vtkVector.h:422
vtkVectorDerivedMacro(vtkVector2i, int, 2)
vtkVector3< T > Cross(const vtkVector3< T > &other) const
Return the cross product of this X other.
Definition: vtkVector.h:284
void SetZ(const T &z)
Set the z component of the vector, i.e.
Definition: vtkVector.h:273
const T & GetZ() const
Get the z component of the vector, i.e.
Definition: vtkVector.h:278
bool operator<(const vtkVector3< T > &v) const
Lexicographical comparison of two vector.
Definition: vtkVector.h:298
vtkVector3(const T *init)
Definition: vtkVector.h:226
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:253
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:268
vtkVector3(const T &scalar)
Definition: vtkVector.h:221
void Set(const T &x, const T &y, const T &z)
Set the x, y and z components of the vector.
Definition: vtkVector.h:242
vtkVector3(const T &x, const T &y, const T &z)
Definition: vtkVector.h:231
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:258
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:263
vtkVector3()=default
vtkVector3Cross(vtkVector3d, double)
vtkVector3< double > Superclass
Definition: vtkVector.h:491
vtkVector3d(double x, double y, double z)
Definition: vtkVector.h:493
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:480
vtkVector3< float > Superclass
Definition: vtkVector.h:478
vtkVector3Cross(vtkVector3i, int)
vtkVectorDerivedMacro(vtkVector3i, int, 3)
vtkVector3< int > Superclass
Definition: vtkVector.h:465
vtkVector3i()=default
vtkVector3i(int x, int y, int z)
Definition: vtkVector.h:467
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:354
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:359
vtkVector4(const T &x, const T &y, const T &z, const T &w)
Definition: vtkVector.h:325
void SetZ(const T &z)
Set the z component of the vector, i.e.
Definition: vtkVector.h:369
void SetW(const T &w)
Set the w component of the vector, i.e.
Definition: vtkVector.h:379
vtkVector4(const T *init)
Definition: vtkVector.h:320
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:337
vtkVector4()=default
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:349
vtkVector4(const T &scalar)
Definition: vtkVector.h:315
const T & GetZ() const
Get the z component of the vector, i.e.
Definition: vtkVector.h:374
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:364
const T & GetW() const
Get the w component of the vector, i.e.
Definition: vtkVector.h:384
vtkVectorDerivedMacro(vtkVector4d, double, 4)
vtkVector4d(double x, double y, double z, double w)
Definition: vtkVector.h:518
vtkVector4d()=default
vtkVector4< int > Superclass
Definition: vtkVector.h:504
vtkVector4i(int x, int y, int z, int w)
Definition: vtkVector.h:506
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:135
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
double Norm() const
Get the norm of the vector, i.e.
Definition: vtkVector.h:79
T SquaredNorm() const
Get the squared norm of the vector.
Definition: vtkVector.h:65