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 
31 #ifndef vtkVector_h
32 #define vtkVector_h
33 
34 #include "vtkObject.h" // for legacy macros
35 #include "vtkTuple.h"
36 
37 #include <cmath> // For math functions
38 
39 template <typename T, int Size>
40 class vtkVector : public vtkTuple<T, Size>
41 {
42 public:
43  vtkVector() = default;
44 
48  explicit vtkVector(const T& scalar)
49  : vtkTuple<T, Size>(scalar)
50  {
51  }
52 
58  explicit vtkVector(const T* init)
59  : vtkTuple<T, Size>(init)
60  {
61  }
62 
64 
67  T SquaredNorm() const
68  {
69  T result = 0;
70  for (int i = 0; i < Size; ++i)
71  {
72  result += this->Data[i] * this->Data[i];
73  }
74  return result;
75  }
77 
81  double Norm() const { return sqrt(static_cast<double>(this->SquaredNorm())); }
82 
84 
88  double Normalize()
89  {
90  const double norm(this->Norm());
91  if (norm == 0.0)
92  {
93  return 0.0;
94  }
95  const double inv(1.0 / norm);
96  for (int i = 0; i < Size; ++i)
97  {
98  this->Data[i] = static_cast<T>(this->Data[i] * inv);
99  }
100  return norm;
101  }
103 
105 
110  {
111  vtkVector<T, Size> temp(*this);
112  temp.Normalize();
113  return temp;
114  }
116 
118 
121  T Dot(const vtkVector<T, Size>& other) const
122  {
123  T result(0);
124  for (int i = 0; i < Size; ++i)
125  {
126  result += this->Data[i] * other[i];
127  }
128  return result;
129  }
131 
133 
136  template <typename TR>
138  {
139  vtkVector<TR, Size> result;
140  for (int i = 0; i < Size; ++i)
141  {
142  result[i] = static_cast<TR>(this->Data[i]);
143  }
144  return result;
145  }
147 };
148 
149 // .NAME vtkVector2 - templated base type for storage of 2D vectors.
150 //
151 template <typename T>
152 class vtkVector2 : public vtkVector<T, 2>
153 {
154 public:
155  vtkVector2() = default;
156 
157  explicit vtkVector2(const T& scalar)
158  : vtkVector<T, 2>(scalar)
159  {
160  }
161 
162  explicit vtkVector2(const T* init)
163  : vtkVector<T, 2>(init)
164  {
165  }
166 
167  vtkVector2(const T& x, const T& y)
168  {
169  this->Data[0] = x;
170  this->Data[1] = y;
171  }
172 
174 
177  void Set(const T& x, const T& y)
178  {
179  this->Data[0] = x;
180  this->Data[1] = y;
181  }
183 
187  void SetX(const T& x) { this->Data[0] = x; }
188 
192  const T& GetX() const { return this->Data[0]; }
193 
197  void SetY(const T& y) { this->Data[1] = y; }
198 
202  const T& GetY() const { return this->Data[1]; }
203 
205 
208  bool operator<(const vtkVector2<T>& v) const
209  {
210  return (this->Data[0] < v.Data[0]) || (this->Data[0] == v.Data[0] && this->Data[1] < v.Data[1]);
211  }
213 };
214 
215 // .NAME vtkVector3 - templated base type for storage of 3D vectors.
216 //
217 template <typename T>
218 class vtkVector3 : public vtkVector<T, 3>
219 {
220 public:
221  vtkVector3() = default;
222 
223  explicit vtkVector3(const T& scalar)
224  : vtkVector<T, 3>(scalar)
225  {
226  }
227 
228  explicit vtkVector3(const T* init)
229  : vtkVector<T, 3>(init)
230  {
231  }
232 
233  vtkVector3(const T& x, const T& y, const T& z)
234  {
235  this->Data[0] = x;
236  this->Data[1] = y;
237  this->Data[2] = z;
238  }
239 
241 
244  void Set(const T& x, const T& y, const T& z)
245  {
246  this->Data[0] = x;
247  this->Data[1] = y;
248  this->Data[2] = z;
249  }
251 
255  void SetX(const T& x) { this->Data[0] = x; }
256 
260  const T& GetX() const { return this->Data[0]; }
261 
265  void SetY(const T& y) { this->Data[1] = y; }
266 
270  const T& GetY() const { return this->Data[1]; }
271 
275  void SetZ(const T& z) { this->Data[2] = z; }
276 
280  const T& GetZ() const { return this->Data[2]; }
281 
283 
286  vtkVector3<T> Cross(const vtkVector3<T>& other) const
287  {
288  vtkVector3<T> res;
289  res[0] = this->Data[1] * other.Data[2] - this->Data[2] * other.Data[1];
290  res[1] = this->Data[2] * other.Data[0] - this->Data[0] * other.Data[2];
291  res[2] = this->Data[0] * other.Data[1] - this->Data[1] * other.Data[0];
292  return res;
293  }
295 
297 
300  bool operator<(const vtkVector3<T>& v) const
301  {
302  return (this->Data[0] < v.Data[0]) ||
303  (this->Data[0] == v.Data[0] && this->Data[1] < v.Data[1]) ||
304  (this->Data[0] == v.Data[0] && this->Data[1] == v.Data[1] && this->Data[2] < v.Data[2]);
305  }
307 };
308 
309 // .NAME vtkVector4 - templated base type for storage of 4D vectors.
310 //
311 template <typename T>
312 class vtkVector4 : public vtkVector<T, 4>
313 {
314 public:
315  vtkVector4() = default;
316 
317  explicit vtkVector4(const T& scalar)
318  : vtkVector<T, 4>(scalar)
319  {
320  }
321 
322  explicit vtkVector4(const T* init)
323  : vtkVector<T, 4>(init)
324  {
325  }
326 
327  vtkVector4(const T& x, const T& y, const T& z, const T& w)
328  {
329  this->Data[0] = x;
330  this->Data[1] = y;
331  this->Data[2] = z;
332  this->Data[3] = w;
333  }
334 
336 
339  void Set(const T& x, const T& y, const T& z, const T& w)
340  {
341  this->Data[0] = x;
342  this->Data[1] = y;
343  this->Data[2] = z;
344  this->Data[3] = w;
345  }
347 
351  void SetX(const T& x) { this->Data[0] = x; }
352 
356  const T& GetX() const { return this->Data[0]; }
357 
361  void SetY(const T& y) { this->Data[1] = y; }
362 
366  const T& GetY() const { return this->Data[1]; }
367 
371  void SetZ(const T& z) { this->Data[2] = z; }
372 
376  const T& GetZ() const { return this->Data[2]; }
377 
381  void SetW(const T& w) { this->Data[3] = w; }
382 
386  const T& GetW() const { return this->Data[3]; }
387 };
388 
392 #define vtkVectorNormalized(vectorType, type, size) \
393  vectorType Normalized() const \
394  { \
395  return vectorType(vtkVector<type, size>::Normalized().GetData()); \
396  }
397 
398 #define vtkVectorDerivedMacro(vectorType, type, size) \
399  vtkVectorNormalized(vectorType, type, size); \
400  explicit vectorType(type s) \
401  : Superclass(s) \
402  { \
403  } \
404  explicit vectorType(const type* i) \
405  : Superclass(i) \
406  { \
407  } \
408  explicit vectorType(const vtkTuple<type, size>& o) \
409  : Superclass(o.GetData()) \
410  { \
411  } \
412  vectorType(const vtkVector<type, size>& o) \
413  : Superclass(o.GetData()) \
414  { \
415  }
416 
418 
421 class vtkVector2i : public vtkVector2<int>
422 {
423 public:
425  vtkVector2i() = default;
426  vtkVector2i(int x, int y)
427  : vtkVector2<int>(x, y)
428  {
429  }
431 };
433 
434 class vtkVector2f : public vtkVector2<float>
435 {
436 public:
438  vtkVector2f() = default;
439  vtkVector2f(float x, float y)
440  : vtkVector2<float>(x, y)
441  {
442  }
444 };
445 
446 class vtkVector2d : public vtkVector2<double>
447 {
448 public:
450  vtkVector2d() = default;
451  vtkVector2d(double x, double y)
452  : vtkVector2<double>(x, y)
453  {
454  }
456 };
457 
458 #define vtkVector3Cross(vectorType, type) \
459  vectorType Cross(const vectorType& other) const \
460  { \
461  return vectorType(vtkVector3<type>::Cross(other).GetData()); \
462  }
463 
464 class vtkVector3i : public vtkVector3<int>
465 {
466 public:
468  vtkVector3i() = default;
469  vtkVector3i(int x, int y, int z)
470  : vtkVector3<int>(x, y, z)
471  {
472  }
475 };
476 
477 class vtkVector3f : public vtkVector3<float>
478 {
479 public:
481  vtkVector3f() = default;
482  vtkVector3f(float x, float y, float z)
483  : vtkVector3<float>(x, y, z)
484  {
485  }
488 };
489 
490 class vtkVector3d : public vtkVector3<double>
491 {
492 public:
494  vtkVector3d() = default;
495  vtkVector3d(double x, double y, double z)
496  : vtkVector3<double>(x, y, z)
497  {
498  }
500  vtkVector3Cross(vtkVector3d, double);
501 };
502 
503 class vtkVector4i : public vtkVector4<int>
504 {
505 public:
507  vtkVector4i() = default;
508  vtkVector4i(int x, int y, int z, int w)
509  : vtkVector4<int>(x, y, z, w)
510  {
511  }
513 };
514 
515 class vtkVector4d : public vtkVector4<double>
516 {
517 public:
519  vtkVector4d() = default;
520  vtkVector4d(double x, double y, double z, double w)
521  : vtkVector4<double>(x, y, z, w){};
523 };
524 
525 #endif // vtkVector_h
526 // VTK-HeaderTest-Exclude: vtkVector.h
vtkVectorDerivedMacro(vtkVector2i, int, 2)
T Data[Size]
The only thing stored in memory!
Definition: vtkTuple.h:154
void SetZ(const T &z)
Set the z component of the vector, i.e.
Definition: vtkVector.h:371
vtkVector4< int > Superclass
Definition: vtkVector.h:506
vtkVector2(const T &scalar)
Definition: vtkVector.h:157
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:265
double Normalize()
Normalize the vector in place.
Definition: vtkVector.h:88
vtkVector4(const T &scalar)
Definition: vtkVector.h:317
vtkVector3< float > Superclass
Definition: vtkVector.h:480
templated base type for storage of vectors.
Definition: vtkVector.h:40
vtkVector3< int > Superclass
Definition: vtkVector.h:467
const T & GetZ() const
Get the z component of the vector, i.e.
Definition: vtkVector.h:376
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:260
vtkVector2f()=default
vtkVector2i(int x, int y)
Definition: vtkVector.h:426
vtkVector2(const T *init)
Definition: vtkVector.h:162
void Set(const T &x, const T &y)
Set the x and y components of the vector.
Definition: vtkVector.h:177
vtkVector3()=default
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:351
vtkVector3Cross(vtkVector3d, double)
vtkVector4d(double x, double y, double z, double w)
Definition: vtkVector.h:520
vtkVector3d(double x, double y, double z)
Definition: vtkVector.h:495
vtkVector2< int > Superclass
Definition: vtkVector.h:424
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:366
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:270
vtkVector4i()=default
vtkVector3(const T &scalar)
Definition: vtkVector.h:223
vtkVector3Cross(vtkVector3f, float)
vtkVector()=default
vtkVector3< double > Superclass
Definition: vtkVector.h:493
T Dot(const vtkVector< T, Size > &other) const
The dot product of this and the supplied vector.
Definition: vtkVector.h:121
vtkVector3Cross(vtkVector3i, int)
vtkVector4()=default
const T & GetZ() const
Get the z component of the vector, i.e.
Definition: vtkVector.h:280
vtkVectorDerivedMacro(vtkVector4i, int, 4)
vtkVector2f(float x, float y)
Definition: vtkVector.h:439
vtkVector2< double > Superclass
Definition: vtkVector.h:449
vtkVector(const T &scalar)
Initialize all of the vector's elements with the supplied scalar.
Definition: vtkVector.h:48
vtkVector2d(double x, double y)
Definition: vtkVector.h:451
void SetW(const T &w)
Set the w component of the vector, i.e.
Definition: vtkVector.h:381
templated base type for containers of constant size.
Definition: vtkTuple.h:37
vtkVector4(const T &x, const T &y, const T &z, const T &w)
Definition: vtkVector.h:327
vtkVector3i(int x, int y, int z)
Definition: vtkVector.h:469
vtkVector2(const T &x, const T &y)
Definition: vtkVector.h:167
double Norm() const
Get the norm of the vector, i.e.
Definition: vtkVector.h:81
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:255
vtkVector3i()=default
vtkVector4d()=default
vtkVectorDerivedMacro(vtkVector3d, double, 3)
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:339
void Set(const T &x, const T &y, const T &z)
Set the x, y and z components of the vector.
Definition: vtkVector.h:244
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:197
vtkVector(const T *init)
Initialize the vector's elements with the elements of the supplied array.
Definition: vtkVector.h:58
vtkVector2< float > Superclass
Definition: vtkVector.h:437
vtkVectorDerivedMacro(vtkVector2d, double, 2)
vtkVector3d()=default
Some derived classes for the different vectors commonly used.
Definition: vtkVector.h:421
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:356
vtkVector3f()=default
vtkVector< T, Size > Normalized() const
Return the normalized form of this vector.
Definition: vtkVector.h:109
vtkVector3< T > Cross(const vtkVector3< T > &other) const
Return the cross product of this X other.
Definition: vtkVector.h:286
vtkVector2d()=default
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:192
vtkVector4i(int x, int y, int z, int w)
Definition: vtkVector.h:508
vtkVectorDerivedMacro(vtkVector3i, int, 3)
vtkVector< TR, Size > Cast() const
Cast the vector to the specified type, returning the result.
Definition: vtkVector.h:137
vtkVector2i()=default
vtkVectorDerivedMacro(vtkVector3f, float, 3)
vtkVector2()=default
vtkVector3(const T &x, const T &y, const T &z)
Definition: vtkVector.h:233
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:187
vtkVectorDerivedMacro(vtkVector4d, double, 4)
vtkVectorDerivedMacro(vtkVector2f, float, 2)
void SetZ(const T &z)
Set the z component of the vector, i.e.
Definition: vtkVector.h:275
const T & GetW() const
Get the w component of the vector, i.e.
Definition: vtkVector.h:386
vtkVector3(const T *init)
Definition: vtkVector.h:228
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:361
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:202
vtkVector4(const T *init)
Definition: vtkVector.h:322
T SquaredNorm() const
Get the squared norm of the vector.
Definition: vtkVector.h:67
vtkVector3f(float x, float y, float z)
Definition: vtkVector.h:482