VTK  9.2.6
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 
40 #ifndef vtkVector_h
41 #define vtkVector_h
42 
43 #include "vtkObject.h" // for legacy macros
44 #include "vtkTuple.h"
45 
46 #include <cmath> // For math functions
47 
48 template <typename T, int Size>
49 class vtkVector : public vtkTuple<T, Size>
50 {
51 public:
52  vtkVector() = default;
53 
57  explicit vtkVector(const T& scalar)
58  : vtkTuple<T, Size>(scalar)
59  {
60  }
61 
67  explicit vtkVector(const T* init)
68  : vtkTuple<T, Size>(init)
69  {
70  }
71 
73 
76  T SquaredNorm() const
77  {
78  T result = 0;
79  for (int i = 0; i < Size; ++i)
80  {
81  result += this->Data[i] * this->Data[i];
82  }
83  return result;
84  }
86 
90  double Norm() const { return sqrt(static_cast<double>(this->SquaredNorm())); }
91 
93 
97  double Normalize()
98  {
99  const double norm(this->Norm());
100  if (norm == 0.0)
101  {
102  return 0.0;
103  }
104  const double inv(1.0 / norm);
105  for (int i = 0; i < Size; ++i)
106  {
107  this->Data[i] = static_cast<T>(this->Data[i] * inv);
108  }
109  return norm;
110  }
112 
114 
119  {
120  vtkVector<T, Size> temp(*this);
121  temp.Normalize();
122  return temp;
123  }
125 
127 
130  T Dot(const vtkVector<T, Size>& other) const
131  {
132  T result(0);
133  for (int i = 0; i < Size; ++i)
134  {
135  result += this->Data[i] * other[i];
136  }
137  return result;
138  }
140 
142 
145  template <typename TR>
147  {
148  vtkVector<TR, Size> result;
149  for (int i = 0; i < Size; ++i)
150  {
151  result[i] = static_cast<TR>(this->Data[i]);
152  }
153  return result;
154  }
156 };
157 
158 // .NAME vtkVector2 - templated base type for storage of 2D vectors.
159 //
160 template <typename T>
161 class vtkVector2 : public vtkVector<T, 2>
162 {
163 public:
164  vtkVector2() = default;
165 
166  explicit vtkVector2(const T& scalar)
167  : vtkVector<T, 2>(scalar)
168  {
169  }
170 
171  explicit vtkVector2(const T* init)
172  : vtkVector<T, 2>(init)
173  {
174  }
175 
176  vtkVector2(const T& x, const T& y)
177  {
178  this->Data[0] = x;
179  this->Data[1] = y;
180  }
181 
183 
186  void Set(const T& x, const T& y)
187  {
188  this->Data[0] = x;
189  this->Data[1] = y;
190  }
192 
196  void SetX(const T& x) { this->Data[0] = x; }
197 
201  const T& GetX() const { return this->Data[0]; }
202 
206  void SetY(const T& y) { this->Data[1] = y; }
207 
211  const T& GetY() const { return this->Data[1]; }
212 
214 
217  bool operator<(const vtkVector2<T>& v) const
218  {
219  return (this->Data[0] < v.Data[0]) || (this->Data[0] == v.Data[0] && this->Data[1] < v.Data[1]);
220  }
222 };
223 
224 // .NAME vtkVector3 - templated base type for storage of 3D vectors.
225 //
226 template <typename T>
227 class vtkVector3 : public vtkVector<T, 3>
228 {
229 public:
230  vtkVector3() = default;
231 
232  explicit vtkVector3(const T& scalar)
233  : vtkVector<T, 3>(scalar)
234  {
235  }
236 
237  explicit vtkVector3(const T* init)
238  : vtkVector<T, 3>(init)
239  {
240  }
241 
242  vtkVector3(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  }
248 
250 
253  void Set(const T& x, const T& y, const T& z)
254  {
255  this->Data[0] = x;
256  this->Data[1] = y;
257  this->Data[2] = z;
258  }
260 
264  void SetX(const T& x) { this->Data[0] = x; }
265 
269  const T& GetX() const { return this->Data[0]; }
270 
274  void SetY(const T& y) { this->Data[1] = y; }
275 
279  const T& GetY() const { return this->Data[1]; }
280 
284  void SetZ(const T& z) { this->Data[2] = z; }
285 
289  const T& GetZ() const { return this->Data[2]; }
290 
292 
295  vtkVector3<T> Cross(const vtkVector3<T>& other) const
296  {
297  vtkVector3<T> res;
298  res[0] = this->Data[1] * other.Data[2] - this->Data[2] * other.Data[1];
299  res[1] = this->Data[2] * other.Data[0] - this->Data[0] * other.Data[2];
300  res[2] = this->Data[0] * other.Data[1] - this->Data[1] * other.Data[0];
301  return res;
302  }
304 
306 
309  bool operator<(const vtkVector3<T>& v) const
310  {
311  return (this->Data[0] < v.Data[0]) ||
312  (this->Data[0] == v.Data[0] && this->Data[1] < v.Data[1]) ||
313  (this->Data[0] == v.Data[0] && this->Data[1] == v.Data[1] && this->Data[2] < v.Data[2]);
314  }
316 };
317 
318 // .NAME vtkVector4 - templated base type for storage of 4D vectors.
319 //
320 template <typename T>
321 class vtkVector4 : public vtkVector<T, 4>
322 {
323 public:
324  vtkVector4() = default;
325 
326  explicit vtkVector4(const T& scalar)
327  : vtkVector<T, 4>(scalar)
328  {
329  }
330 
331  explicit vtkVector4(const T* init)
332  : vtkVector<T, 4>(init)
333  {
334  }
335 
336  vtkVector4(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  }
343 
345 
348  void Set(const T& x, const T& y, const T& z, const T& w)
349  {
350  this->Data[0] = x;
351  this->Data[1] = y;
352  this->Data[2] = z;
353  this->Data[3] = w;
354  }
356 
360  void SetX(const T& x) { this->Data[0] = x; }
361 
365  const T& GetX() const { return this->Data[0]; }
366 
370  void SetY(const T& y) { this->Data[1] = y; }
371 
375  const T& GetY() const { return this->Data[1]; }
376 
380  void SetZ(const T& z) { this->Data[2] = z; }
381 
385  const T& GetZ() const { return this->Data[2]; }
386 
390  void SetW(const T& w) { this->Data[3] = w; }
391 
395  const T& GetW() const { return this->Data[3]; }
396 };
397 
401 #define vtkVectorNormalized(vectorType, type, size) \
402  vectorType Normalized() const \
403  { \
404  return vectorType(vtkVector<type, size>::Normalized().GetData()); \
405  }
406 
407 #define vtkVectorDerivedMacro(vectorType, type, size) \
408  vtkVectorNormalized(vectorType, type, size); \
409  explicit vectorType(type s) \
410  : Superclass(s) \
411  { \
412  } \
413  explicit vectorType(const type* i) \
414  : Superclass(i) \
415  { \
416  } \
417  explicit vectorType(const vtkTuple<type, size>& o) \
418  : Superclass(o.GetData()) \
419  { \
420  } \
421  vectorType(const vtkVector<type, size>& o) \
422  : Superclass(o.GetData()) \
423  { \
424  }
425 
427 
430 class vtkVector2i : public vtkVector2<int>
431 {
432 public:
434  vtkVector2i() = default;
435  vtkVector2i(int x, int y)
436  : vtkVector2<int>(x, y)
437  {
438  }
440 };
442 
443 class vtkVector2f : public vtkVector2<float>
444 {
445 public:
447  vtkVector2f() = default;
448  vtkVector2f(float x, float y)
449  : vtkVector2<float>(x, y)
450  {
451  }
453 };
454 
455 class vtkVector2d : public vtkVector2<double>
456 {
457 public:
459  vtkVector2d() = default;
460  vtkVector2d(double x, double y)
461  : vtkVector2<double>(x, y)
462  {
463  }
465 };
466 
467 #define vtkVector3Cross(vectorType, type) \
468  vectorType Cross(const vectorType& other) const \
469  { \
470  return vectorType(vtkVector3<type>::Cross(other).GetData()); \
471  }
472 
473 class vtkVector3i : public vtkVector3<int>
474 {
475 public:
477  vtkVector3i() = default;
478  vtkVector3i(int x, int y, int z)
479  : vtkVector3<int>(x, y, z)
480  {
481  }
484 };
485 
486 class vtkVector3f : public vtkVector3<float>
487 {
488 public:
490  vtkVector3f() = default;
491  vtkVector3f(float x, float y, float z)
492  : vtkVector3<float>(x, y, z)
493  {
494  }
497 };
498 
499 class vtkVector3d : public vtkVector3<double>
500 {
501 public:
503  vtkVector3d() = default;
504  vtkVector3d(double x, double y, double z)
505  : vtkVector3<double>(x, y, z)
506  {
507  }
510 };
511 
512 class vtkVector4i : public vtkVector4<int>
513 {
514 public:
516  vtkVector4i() = default;
517  vtkVector4i(int x, int y, int z, int w)
518  : vtkVector4<int>(x, y, z, w)
519  {
520  }
522 };
523 
524 class vtkVector4d : public vtkVector4<double>
525 {
526 public:
528  vtkVector4d() = default;
529  vtkVector4d(double x, double y, double z, double w)
530  : vtkVector4<double>(x, y, z, w){};
532 };
533 
534 #endif // vtkVector_h
535 // VTK-HeaderTest-Exclude: vtkVector.h
templated base type for containers of constant size.
Definition: vtkTuple.h:38
T Data[Size]
The only thing stored in memory!
Definition: vtkTuple.h:154
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:211
void Set(const T &x, const T &y)
Set the x and y components of the vector.
Definition: vtkVector.h:186
vtkVector2(const T *init)
Definition: vtkVector.h:171
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:206
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:201
vtkVector2(const T &x, const T &y)
Definition: vtkVector.h:176
vtkVector2(const T &scalar)
Definition: vtkVector.h:166
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:196
bool operator<(const vtkVector2< T > &v) const
Lexicographical comparison of two vector.
Definition: vtkVector.h:217
vtkVector2()=default
vtkVector2d()=default
vtkVectorDerivedMacro(vtkVector2d, double, 2)
vtkVector2d(double x, double y)
Definition: vtkVector.h:460
vtkVector2< double > Superclass
Definition: vtkVector.h:458
vtkVector2f()=default
vtkVector2< float > Superclass
Definition: vtkVector.h:446
vtkVector2f(float x, float y)
Definition: vtkVector.h:448
vtkVectorDerivedMacro(vtkVector2f, float, 2)
Some derived classes for the different vectors commonly used.
Definition: vtkVector.h:431
vtkVector2i()=default
vtkVector2i(int x, int y)
Definition: vtkVector.h:435
vtkVector2< int > Superclass
Definition: vtkVector.h:433
vtkVectorDerivedMacro(vtkVector2i, int, 2)
vtkVector3< T > Cross(const vtkVector3< T > &other) const
Return the cross product of this X other.
Definition: vtkVector.h:295
void SetZ(const T &z)
Set the z component of the vector, i.e.
Definition: vtkVector.h:284
const T & GetZ() const
Get the z component of the vector, i.e.
Definition: vtkVector.h:289
bool operator<(const vtkVector3< T > &v) const
Lexicographical comparison of two vector.
Definition: vtkVector.h:309
vtkVector3(const T *init)
Definition: vtkVector.h:237
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:264
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:279
vtkVector3(const T &scalar)
Definition: vtkVector.h:232
void Set(const T &x, const T &y, const T &z)
Set the x, y and z components of the vector.
Definition: vtkVector.h:253
vtkVector3(const T &x, const T &y, const T &z)
Definition: vtkVector.h:242
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:269
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:274
vtkVector3()=default
vtkVector3Cross(vtkVector3d, double)
vtkVector3< double > Superclass
Definition: vtkVector.h:502
vtkVector3d(double x, double y, double z)
Definition: vtkVector.h:504
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:491
vtkVector3< float > Superclass
Definition: vtkVector.h:489
vtkVector3Cross(vtkVector3i, int)
vtkVectorDerivedMacro(vtkVector3i, int, 3)
vtkVector3< int > Superclass
Definition: vtkVector.h:476
vtkVector3i()=default
vtkVector3i(int x, int y, int z)
Definition: vtkVector.h:478
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:365
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:370
vtkVector4(const T &x, const T &y, const T &z, const T &w)
Definition: vtkVector.h:336
void SetZ(const T &z)
Set the z component of the vector, i.e.
Definition: vtkVector.h:380
void SetW(const T &w)
Set the w component of the vector, i.e.
Definition: vtkVector.h:390
vtkVector4(const T *init)
Definition: vtkVector.h:331
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:348
vtkVector4()=default
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:360
vtkVector4(const T &scalar)
Definition: vtkVector.h:326
const T & GetZ() const
Get the z 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:375
const T & GetW() const
Get the w component of the vector, i.e.
Definition: vtkVector.h:395
vtkVectorDerivedMacro(vtkVector4d, double, 4)
vtkVector4d(double x, double y, double z, double w)
Definition: vtkVector.h:529
vtkVector4d()=default
vtkVector4< int > Superclass
Definition: vtkVector.h:515
vtkVector4i(int x, int y, int z, int w)
Definition: vtkVector.h:517
vtkVector4i()=default
vtkVectorDerivedMacro(vtkVector4i, int, 4)
templated base type for storage of vectors.
Definition: vtkVector.h:50
vtkVector(const T &scalar)
Initialize all of the vector's elements with the supplied scalar.
Definition: vtkVector.h:57
T Dot(const vtkVector< T, Size > &other) const
The dot product of this and the supplied vector.
Definition: vtkVector.h:130
vtkVector< TR, Size > Cast() const
Cast the vector to the specified type, returning the result.
Definition: vtkVector.h:146
double Normalize()
Normalize the vector in place.
Definition: vtkVector.h:97
vtkVector< T, Size > Normalized() const
Return the normalized form of this vector.
Definition: vtkVector.h:118
vtkVector()=default
vtkVector(const T *init)
Initialize the vector's elements with the elements of the supplied array.
Definition: vtkVector.h:67
double Norm() const
Get the norm of the vector, i.e.
Definition: vtkVector.h:90
T SquaredNorm() const
Get the squared norm of the vector.
Definition: vtkVector.h:76