VTK  9.0.20210725
vtkVector.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkVector.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
15 
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 template <typename T, int Size>
37 class vtkVector : public vtkTuple<T, Size>
38 {
39 public:
40  vtkVector() = default;
41 
45  explicit vtkVector(const T& scalar)
46  : vtkTuple<T, Size>(scalar)
47  {
48  }
49 
55  explicit vtkVector(const T* init)
56  : vtkTuple<T, Size>(init)
57  {
58  }
59 
61 
64  T SquaredNorm() const
65  {
66  T result = 0;
67  for (int i = 0; i < Size; ++i)
68  {
69  result += this->Data[i] * this->Data[i];
70  }
71  return result;
72  }
74 
78  double Norm() const { return sqrt(static_cast<double>(this->SquaredNorm())); }
79 
81 
85  double Normalize()
86  {
87  const double norm(this->Norm());
88  if (norm == 0.0)
89  {
90  return 0.0;
91  }
92  const double inv(1.0 / norm);
93  for (int i = 0; i < Size; ++i)
94  {
95  this->Data[i] = static_cast<T>(this->Data[i] * inv);
96  }
97  return norm;
98  }
100 
102 
107  {
108  vtkVector<T, Size> temp(*this);
109  temp.Normalize();
110  return temp;
111  }
113 
115 
118  T Dot(const vtkVector<T, Size>& other) const
119  {
120  T result(0);
121  for (int i = 0; i < Size; ++i)
122  {
123  result += this->Data[i] * other[i];
124  }
125  return result;
126  }
128 
130 
133  template <typename TR>
135  {
136  vtkVector<TR, Size> result;
137  for (int i = 0; i < Size; ++i)
138  {
139  result[i] = static_cast<TR>(this->Data[i]);
140  }
141  return result;
142  }
144 };
145 
146 // .NAME vtkVector2 - templated base type for storage of 2D vectors.
147 //
148 template <typename T>
149 class vtkVector2 : public vtkVector<T, 2>
150 {
151 public:
152  vtkVector2() = default;
153 
154  explicit vtkVector2(const T& scalar)
155  : vtkVector<T, 2>(scalar)
156  {
157  }
158 
159  explicit vtkVector2(const T* init)
160  : vtkVector<T, 2>(init)
161  {
162  }
163 
164  vtkVector2(const T& x, const T& y)
165  {
166  this->Data[0] = x;
167  this->Data[1] = y;
168  }
169 
171 
174  void Set(const T& x, const T& y)
175  {
176  this->Data[0] = x;
177  this->Data[1] = y;
178  }
180 
184  void SetX(const T& x) { this->Data[0] = x; }
185 
189  const T& GetX() const { return this->Data[0]; }
190 
194  void SetY(const T& y) { this->Data[1] = y; }
195 
199  const T& GetY() const { return this->Data[1]; }
200 
202 
205  bool operator<(const vtkVector2<T>& v) const
206  {
207  return (this->Data[0] < v.Data[0]) || (this->Data[0] == v.Data[0] && this->Data[1] < v.Data[1]);
208  }
210 };
211 
212 // .NAME vtkVector3 - templated base type for storage of 3D vectors.
213 //
214 template <typename T>
215 class vtkVector3 : public vtkVector<T, 3>
216 {
217 public:
218  vtkVector3() = default;
219 
220  explicit vtkVector3(const T& scalar)
221  : vtkVector<T, 3>(scalar)
222  {
223  }
224 
225  explicit vtkVector3(const T* init)
226  : vtkVector<T, 3>(init)
227  {
228  }
229 
230  vtkVector3(const T& x, const T& y, const T& z)
231  {
232  this->Data[0] = x;
233  this->Data[1] = y;
234  this->Data[2] = z;
235  }
236 
238 
241  void Set(const T& x, const T& y, const T& z)
242  {
243  this->Data[0] = x;
244  this->Data[1] = y;
245  this->Data[2] = z;
246  }
248 
252  void SetX(const T& x) { this->Data[0] = x; }
253 
257  const T& GetX() const { return this->Data[0]; }
258 
262  void SetY(const T& y) { this->Data[1] = y; }
263 
267  const T& GetY() const { return this->Data[1]; }
268 
272  void SetZ(const T& z) { this->Data[2] = z; }
273 
277  const T& GetZ() const { return this->Data[2]; }
278 
280 
283  vtkVector3<T> Cross(const vtkVector3<T>& other) const
284  {
285  vtkVector3<T> res;
286  res[0] = this->Data[1] * other.Data[2] - this->Data[2] * other.Data[1];
287  res[1] = this->Data[2] * other.Data[0] - this->Data[0] * other.Data[2];
288  res[2] = this->Data[0] * other.Data[1] - this->Data[1] * other.Data[0];
289  return res;
290  }
292 
294 
297  bool operator<(const vtkVector3<T>& v) const
298  {
299  return (this->Data[0] < v.Data[0]) ||
300  (this->Data[0] == v.Data[0] && this->Data[1] < v.Data[1]) ||
301  (this->Data[0] == v.Data[0] && this->Data[1] == v.Data[1] && this->Data[2] < v.Data[2]);
302  }
304 };
305 
306 // .NAME vtkVector4 - templated base type for storage of 4D vectors.
307 //
308 template <typename T>
309 class vtkVector4 : public vtkVector<T, 4>
310 {
311 public:
312  vtkVector4() = default;
313 
314  explicit vtkVector4(const T& scalar)
315  : vtkVector<T, 4>(scalar)
316  {
317  }
318 
319  explicit vtkVector4(const T* init)
320  : vtkVector<T, 4>(init)
321  {
322  }
323 
324  vtkVector4(const T& x, const T& y, const T& z, const T& w)
325  {
326  this->Data[0] = x;
327  this->Data[1] = y;
328  this->Data[2] = z;
329  this->Data[3] = w;
330  }
331 
333 
336  void Set(const T& x, const T& y, const T& z, const T& w)
337  {
338  this->Data[0] = x;
339  this->Data[1] = y;
340  this->Data[2] = z;
341  this->Data[3] = w;
342  }
344 
348  void SetX(const T& x) { this->Data[0] = x; }
349 
353  const T& GetX() const { return this->Data[0]; }
354 
358  void SetY(const T& y) { this->Data[1] = y; }
359 
363  const T& GetY() const { return this->Data[1]; }
364 
368  void SetZ(const T& z) { this->Data[2] = z; }
369 
373  const T& GetZ() const { return this->Data[2]; }
374 
378  void SetW(const T& w) { this->Data[3] = w; }
379 
383  const T& GetW() const { return this->Data[3]; }
384 };
385 
389 #define vtkVectorNormalized(vectorType, type, size) \
390  vectorType Normalized() const \
391  { \
392  return vectorType(vtkVector<type, size>::Normalized().GetData()); \
393  }
394 
395 #define vtkVectorDerivedMacro(vectorType, type, size) \
396  vtkVectorNormalized(vectorType, type, size); \
397  explicit vectorType(type s) \
398  : Superclass(s) \
399  { \
400  } \
401  explicit vectorType(const type* i) \
402  : Superclass(i) \
403  { \
404  } \
405  explicit vectorType(const vtkTuple<type, size>& o) \
406  : Superclass(o.GetData()) \
407  { \
408  } \
409  vectorType(const vtkVector<type, size>& o) \
410  : Superclass(o.GetData()) \
411  { \
412  }
413 
415 
418 class vtkVector2i : public vtkVector2<int>
419 {
420 public:
422  vtkVector2i() = default;
423  vtkVector2i(int x, int y)
424  : vtkVector2<int>(x, y)
425  {
426  }
428 };
430 
431 class vtkVector2f : public vtkVector2<float>
432 {
433 public:
435  vtkVector2f() = default;
436  vtkVector2f(float x, float y)
437  : vtkVector2<float>(x, y)
438  {
439  }
441 };
442 
443 class vtkVector2d : public vtkVector2<double>
444 {
445 public:
447  vtkVector2d() = default;
448  vtkVector2d(double x, double y)
449  : vtkVector2<double>(x, y)
450  {
451  }
453 };
454 
455 #define vtkVector3Cross(vectorType, type) \
456  vectorType Cross(const vectorType& other) const \
457  { \
458  return vectorType(vtkVector3<type>::Cross(other).GetData()); \
459  }
460 
461 class vtkVector3i : public vtkVector3<int>
462 {
463 public:
465  vtkVector3i() = default;
466  vtkVector3i(int x, int y, int z)
467  : vtkVector3<int>(x, y, z)
468  {
469  }
472 };
473 
474 class vtkVector3f : public vtkVector3<float>
475 {
476 public:
478  vtkVector3f() = default;
479  vtkVector3f(float x, float y, float z)
480  : vtkVector3<float>(x, y, z)
481  {
482  }
485 };
486 
487 class vtkVector3d : public vtkVector3<double>
488 {
489 public:
491  vtkVector3d() = default;
492  vtkVector3d(double x, double y, double z)
493  : vtkVector3<double>(x, y, z)
494  {
495  }
498 };
499 
500 class vtkVector4i : public vtkVector4<int>
501 {
502 public:
504  vtkVector4i() = default;
505  vtkVector4i(int x, int y, int z, int w)
506  : vtkVector4<int>(x, y, z, w)
507  {
508  }
510 };
511 
512 class vtkVector4d : public vtkVector4<double>
513 {
514 public:
516  vtkVector4d() = default;
517  vtkVector4d(double x, double y, double z, double w)
518  : vtkVector4<double>(x, y, z, w){};
520 };
521 
522 #endif // vtkVector_h
523 // VTK-HeaderTest-Exclude: vtkVector.h
vtkVector4
Definition: vtkVector.h:310
vtkTuple::Data
T Data[Size]
The only thing stored in memory!
Definition: vtkTuple.h:154
vtkVector4::SetX
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:348
vtkVector2::vtkVector2
vtkVector2()=default
vtkVector4i::Superclass
vtkVector4< int > Superclass
Definition: vtkVector.h:503
vtkVector3::SetX
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:252
vtkVector3::vtkVector3
vtkVector3(const T &scalar)
Definition: vtkVector.h:220
vtkVector::Normalize
double Normalize()
Normalize the vector in place.
Definition: vtkVector.h:85
vtkVector::Norm
double Norm() const
Get the norm of the vector, i.e.
Definition: vtkVector.h:78
vtkVector
templated base type for storage of vectors.
Definition: vtkVector.h:38
vtkVector2
Definition: vtkVector.h:150
vtkVector2i::Superclass
vtkVector2< int > Superclass
Definition: vtkVector.h:421
vtkVector3
Definition: vtkVector.h:216
vtkVector3::vtkVector3
vtkVector3(const T *init)
Definition: vtkVector.h:225
vtkVector::SquaredNorm
T SquaredNorm() const
Get the squared norm of the vector.
Definition: vtkVector.h:64
vtkVector2::GetX
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:189
vtkVector3i::vtkVector3Cross
vtkVector3Cross(vtkVector3i, int)
vtkVector3i::Superclass
vtkVector3< int > Superclass
Definition: vtkVector.h:464
vtkVector2::operator<
bool operator<(const vtkVector2< T > &v) const
Lexicographical comparison of two vector.
Definition: vtkVector.h:205
vtkVector2d::Superclass
vtkVector2< double > Superclass
Definition: vtkVector.h:446
vtkVector2f::vtkVector2f
vtkVector2f()=default
vtkVector3::GetZ
const T & GetZ() const
Get the z component of the vector, i.e.
Definition: vtkVector.h:277
vtkVector::vtkVector
vtkVector(const T &scalar)
Initialize all of the vector's elements with the supplied scalar.
Definition: vtkVector.h:45
vtkVector4i::vtkVector4i
vtkVector4i()=default
vtkVector4::GetZ
const T & GetZ() const
Get the z component of the vector, i.e.
Definition: vtkVector.h:373
vtkVector3::SetY
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:262
vtkVector3f::vtkVector3Cross
vtkVector3Cross(vtkVector3f, float)
vtkVector4d::vtkVector4d
vtkVector4d()=default
vtkVector3d::Superclass
vtkVector3< double > Superclass
Definition: vtkVector.h:490
vtkVector2i::vtkVector2i
vtkVector2i(int x, int y)
Definition: vtkVector.h:423
vtkVector2d
Definition: vtkVector.h:444
vtkVector4d::vtkVector4d
vtkVector4d(double x, double y, double z, double w)
Definition: vtkVector.h:517
vtkVector3d::vtkVector3d
vtkVector3d(double x, double y, double z)
Definition: vtkVector.h:492
vtkVector3i
Definition: vtkVector.h:462
vtkVector3i::vtkVector3i
vtkVector3i(int x, int y, int z)
Definition: vtkVector.h:466
vtkVector::Normalized
vtkVector< T, Size > Normalized() const
Return the normalized form of this vector.
Definition: vtkVector.h:106
vtkVector3::vtkVector3
vtkVector3(const T &x, const T &y, const T &z)
Definition: vtkVector.h:230
vtkVector3d::vtkVectorDerivedMacro
vtkVectorDerivedMacro(vtkVector3d, double, 3)
vtkVector2::SetX
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:184
vtkVector4i::vtkVectorDerivedMacro
vtkVectorDerivedMacro(vtkVector4i, int, 4)
vtkVector4d
Definition: vtkVector.h:513
vtkVector3f::vtkVector3f
vtkVector3f()=default
vtkVector2f::vtkVector2f
vtkVector2f(float x, float y)
Definition: vtkVector.h:436
vtkVector3d::vtkVector3Cross
vtkVector3Cross(vtkVector3d, double)
vtkVector2d::vtkVectorDerivedMacro
vtkVectorDerivedMacro(vtkVector2d, double, 2)
vtkVector3f
Definition: vtkVector.h:475
vtkVector3d::vtkVector3d
vtkVector3d()=default
vtkVector4::GetX
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:353
vtkVector2::vtkVector2
vtkVector2(const T *init)
Definition: vtkVector.h:159
vtkVector3::vtkVector3
vtkVector3()=default
vtkVector4::SetY
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:358
vtkVector4::GetW
const T & GetW() const
Get the w component of the vector, i.e.
Definition: vtkVector.h:383
vtkVector2d::vtkVector2d
vtkVector2d(double x, double y)
Definition: vtkVector.h:448
vtkVector4::GetY
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:363
vtkVector3::SetZ
void SetZ(const T &z)
Set the z component of the vector, i.e.
Definition: vtkVector.h:272
vtkVector3f::vtkVectorDerivedMacro
vtkVectorDerivedMacro(vtkVector3f, float, 3)
vtkVector::Cast
vtkVector< TR, Size > Cast() const
Cast the vector to the specified type, returning the result.
Definition: vtkVector.h:134
vtkVector3i::vtkVector3i
vtkVector3i()=default
vtkVector3::Cross
vtkVector3< T > Cross(const vtkVector3< T > &other) const
Return the cross product of this X other.
Definition: vtkVector.h:283
vtkObject.h
vtkVector3::GetX
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:257
vtkVector2f::Superclass
vtkVector2< float > Superclass
Definition: vtkVector.h:434
vtkVector4::vtkVector4
vtkVector4(const T *init)
Definition: vtkVector.h:319
vtkVector2f::vtkVectorDerivedMacro
vtkVectorDerivedMacro(vtkVector2f, float, 2)
vtkVector2d::vtkVector2d
vtkVector2d()=default
vtkVector2::vtkVector2
vtkVector2(const T &x, const T &y)
Definition: vtkVector.h:164
vtkTuple.h
vtkVector3::operator<
bool operator<(const vtkVector3< T > &v) const
Lexicographical comparison of two vector.
Definition: vtkVector.h:297
vtkVector4i::vtkVector4i
vtkVector4i(int x, int y, int z, int w)
Definition: vtkVector.h:505
vtkVector4::SetZ
void SetZ(const T &z)
Set the z component of the vector, i.e.
Definition: vtkVector.h:368
vtkVector4::vtkVector4
vtkVector4()=default
vtkVector3::Set
void Set(const T &x, const T &y, const T &z)
Set the x, y and z components of the vector.
Definition: vtkVector.h:241
vtkVector2::vtkVector2
vtkVector2(const T &scalar)
Definition: vtkVector.h:154
vtkVector4d::vtkVectorDerivedMacro
vtkVectorDerivedMacro(vtkVector4d, double, 4)
vtkVector4::SetW
void SetW(const T &w)
Set the w component of the vector, i.e.
Definition: vtkVector.h:378
vtkVector4::vtkVector4
vtkVector4(const T &scalar)
Definition: vtkVector.h:314
vtkVector::Dot
T Dot(const vtkVector< T, Size > &other) const
The dot product of this and the supplied vector.
Definition: vtkVector.h:118
vtkVector4::vtkVector4
vtkVector4(const T &x, const T &y, const T &z, const T &w)
Definition: vtkVector.h:324
vtkVector::vtkVector
vtkVector(const T *init)
Initialize the vector's elements with the elements of the supplied array.
Definition: vtkVector.h:55
vtkVector2i::vtkVectorDerivedMacro
vtkVectorDerivedMacro(vtkVector2i, int, 2)
vtkVector4::Set
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:336
vtkVector3i::vtkVectorDerivedMacro
vtkVectorDerivedMacro(vtkVector3i, int, 3)
vtkVector2i
Some derived classes for the different vectors commonly used.
Definition: vtkVector.h:419
vtkVector2::SetY
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:194
vtkTuple
templated base type for containers of constant size.
Definition: vtkTuple.h:38
vtkVector2i::vtkVector2i
vtkVector2i()=default
vtkVector::vtkVector
vtkVector()=default
vtkVector3d
Definition: vtkVector.h:488
vtkVector3f::vtkVector3f
vtkVector3f(float x, float y, float z)
Definition: vtkVector.h:479
vtkVector2::GetY
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:199
vtkVector2f
Definition: vtkVector.h:432
vtkVector3f::Superclass
vtkVector3< float > Superclass
Definition: vtkVector.h:477
vtkVector2::Set
void Set(const T &x, const T &y)
Set the x and y components of the vector.
Definition: vtkVector.h:174
vtkVector4i
Definition: vtkVector.h:501
vtkVector3::GetY
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:267