VTK
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() {}
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:
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:
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:
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]; }
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:
424  vtkVector2i(int x, int y)
425  : vtkVector2<int>(x, y)
426  {
427  }
429 };
431 
432 class vtkVector2f : public vtkVector2<float>
433 {
434 public:
437  vtkVector2f(float x, float y)
438  : vtkVector2<float>(x, y)
439  {
440  }
442 };
443 
444 class vtkVector2d : public vtkVector2<double>
445 {
446 public:
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:
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:
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:
493  vtkVector3d(double x, double y, double z)
494  : vtkVector3<double>(x, y, z)
495  {
496  }
498  vtkVector3Cross(vtkVector3d, double);
499 };
500 
501 class vtkVector4d : public vtkVector4<double>
502 {
503 public:
506  vtkVector4d(double x, double y, double z, double w)
507  : vtkVector4<double>(x, y, z, w){};
509 };
510 
511 #endif // vtkVector_h
512 // VTK-HeaderTest-Exclude: vtkVector.h
vtkVector3::vtkVector3
vtkVector3()
Definition: vtkVector.h:218
vtkVector4
Definition: vtkVector.h:309
vtkTuple::Data
T Data[Size]
The only thing stored in memory!
Definition: vtkTuple.h:143
vtkVector4::SetX
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:348
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:37
vtkVector2
Definition: vtkVector.h:149
vtkVector3i::vtkVector3i
vtkVector3i()
Definition: vtkVector.h:466
vtkVector2i::Superclass
vtkVector2< int > Superclass
Definition: vtkVector.h:422
vtkVector2::vtkVector2
vtkVector2()
Definition: vtkVector.h:152
vtkVector3
Definition: vtkVector.h:215
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:465
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:447
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
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)
vtkVector3d::Superclass
vtkVector3< double > Superclass
Definition: vtkVector.h:491
vtkVector2i::vtkVector2i
vtkVector2i(int x, int y)
Definition: vtkVector.h:424
vtkVector2d
Definition: vtkVector.h:444
vtkVector4d::vtkVector4d
vtkVector4d(double x, double y, double z, double w)
Definition: vtkVector.h:506
vtkVector3d::vtkVector3d
vtkVector3d(double x, double y, double z)
Definition: vtkVector.h:493
vtkVector3i
Definition: vtkVector.h:462
vtkVector3i::vtkVector3i
vtkVector3i(int x, int y, int z)
Definition: vtkVector.h:467
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
vtkVector4d
Definition: vtkVector.h:501
vtkVector2f::vtkVector2f
vtkVector2f(float x, float y)
Definition: vtkVector.h:437
vtkVector3d::vtkVector3Cross
vtkVector3Cross(vtkVector3d, double)
vtkVector2d::vtkVectorDerivedMacro
vtkVectorDerivedMacro(vtkVector2d, double, 2)
vtkVector3f
Definition: vtkVector.h:475
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
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:449
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
vtkVector3::Cross
vtkVector3< T > Cross(const vtkVector3< T > &other) const
Return the cross product of this X other.
Definition: vtkVector.h:283
vtkVector2d::vtkVector2d
vtkVector2d()
Definition: vtkVector.h:448
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:435
vtkVector4::vtkVector4
vtkVector4(const T *init)
Definition: vtkVector.h:319
vtkVector2f::vtkVectorDerivedMacro
vtkVectorDerivedMacro(vtkVector2f, float, 2)
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
vtkVector2i::vtkVector2i
vtkVector2i()
Definition: vtkVector.h:423
vtkVector3d::vtkVector3d
vtkVector3d()
Definition: vtkVector.h:492
vtkVector4::SetZ
void SetZ(const T &z)
Set the z component of the vector, i.e.
Definition: vtkVector.h:368
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
vtkVector3f::vtkVector3f
vtkVector3f()
Definition: vtkVector.h:479
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:35
vtkVector::vtkVector
vtkVector()
Definition: vtkVector.h:40
vtkVector4::vtkVector4
vtkVector4()
Definition: vtkVector.h:312
vtkVector3d
Definition: vtkVector.h:488
vtkVector3f::vtkVector3f
vtkVector3f(float x, float y, float z)
Definition: vtkVector.h:480
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:478
vtkVector2f::vtkVector2f
vtkVector2f()
Definition: vtkVector.h:436
vtkVector2::Set
void Set(const T &x, const T &y)
Set the x and y components of the vector.
Definition: vtkVector.h:174
vtkVector4d::vtkVector4d
vtkVector4d()
Definition: vtkVector.h:505
vtkVector3::GetY
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:267