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 
31 #ifndef vtkVector_h
32 #define vtkVector_h
33 
34 #include "vtkTuple.h"
35 #include "vtkObject.h" // for legacy macros
36 
37 #include <cmath> // For math functions
38 
39 template<typename T, int Size>
40 class vtkVector : public vtkTuple<T, Size>
41 {
42 public:
44  {
45  }
46 
50  explicit vtkVector(const T& scalar) : vtkTuple<T, Size>(scalar)
51  {
52  }
53 
59  explicit vtkVector(const T* init) : 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
82  {
83  return sqrt(static_cast<double>(this->SquaredNorm()));
84  }
85 
87 
91  double Normalize()
92  {
93  const double norm(this->Norm());
94  const double inv(1.0 / norm);
95  for (int i = 0; i < Size; ++i)
96  {
97  this->Data[i] = static_cast<T>(this->Data[i] * inv);
98  }
99  return norm;
100  }
102 
104 
109  {
110  vtkVector<T, Size> temp(*this);
111  temp.Normalize();
112  return temp;
113  }
115 
117 
120  T Dot(const vtkVector<T, Size>& other) const
121  {
122  T result(0);
123  for (int i = 0; i < Size; ++i)
124  {
125  result += this->Data[i] * other[i];
126  }
127  return result;
128  }
130 
132 
135  template<typename TR>
137  {
138  vtkVector<TR, Size> result;
139  for (int i = 0; i < Size; ++i)
140  {
141  result[i] = static_cast<TR>(this->Data[i]);
142  }
143  return result;
144  }
145 };
147 
148 // .NAME vtkVector2 - templated base type for storage of 2D vectors.
149 //
150 template<typename T>
151 class vtkVector2 : public vtkVector<T, 2>
152 {
153 public:
155  {
156  }
157 
158  explicit vtkVector2(const T& scalar) : vtkVector<T, 2>(scalar)
159  {
160  }
161 
162  explicit vtkVector2(const T* init) : vtkVector<T, 2>(init)
163  {
164  }
165 
166  vtkVector2(const T& x, const T& y)
167  {
168  this->Data[0] = x;
169  this->Data[1] = y;
170  }
171 
173 
176  void Set(const T& x, const T& y)
177  {
178  this->Data[0] = x;
179  this->Data[1] = y;
180  }
182 
186  void SetX(const T& x) { this->Data[0] = x; }
187 
191  const T& GetX() const { return this->Data[0]; }
192 
196  void SetY(const T& y) { this->Data[1] = y; }
197 
201  const T& GetY() const { return this->Data[1]; }
202 
204 
207  bool operator<(const vtkVector2<T> &v) const
208  {
209  return (this->Data[0] < v.Data[0]) || (this->Data[0] == v.Data[0] && this->Data[1] < v.Data[1]);
210  }
211 };
213 
214 // .NAME vtkVector3 - templated base type for storage of 3D vectors.
215 //
216 template<typename T>
217 class vtkVector3 : public vtkVector<T, 3>
218 {
219 public:
221  {
222  }
223 
224  explicit vtkVector3(const T& scalar) : vtkVector<T, 3>(scalar)
225  {
226  }
227 
228  explicit vtkVector3(const T* init) : vtkVector<T, 3>(init)
229  {
230  }
231 
232  vtkVector3(const T& x, const T& y, const T& z)
233  {
234  this->Data[0] = x;
235  this->Data[1] = y;
236  this->Data[2] = z;
237  }
238 
240 
243  void Set(const T& x, const T& y, const T& z)
244  {
245  this->Data[0] = x;
246  this->Data[1] = y;
247  this->Data[2] = z;
248  }
250 
254  void SetX(const T& x) { this->Data[0] = x; }
255 
259  const T& GetX() const { return this->Data[0]; }
260 
264  void SetY(const T& y) { this->Data[1] = y; }
265 
269  const T& GetY() const { return this->Data[1]; }
270 
274  void SetZ(const T& z) { this->Data[2] = z; }
275 
279  const T& GetZ() const { return this->Data[2]; }
280 
282 
285  vtkVector3<T> Cross(const vtkVector3<T>& other) const
286  {
287  vtkVector3<T> res;
288  res[0] = this->Data[1] * other.Data[2] - this->Data[2] * other.Data[1];
289  res[1] = this->Data[2] * other.Data[0] - this->Data[0] * other.Data[2];
290  res[2] = this->Data[0] * other.Data[1] - this->Data[1] * other.Data[0];
291  return res;
292  }
294 
296 
299  bool operator<(const vtkVector3<T> &v) const
300  {
301  return (this->Data[0] < v.Data[0]) || (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  }
304 };
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:
314  {
315  }
316 
317  explicit vtkVector4(const T& scalar) : vtkVector<T, 4>(scalar)
318  {
319  }
320 
321  explicit vtkVector4(const T* init) : 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 };
387 
391 #define vtkVectorNormalized(vectorType, type, size) \
392 vectorType Normalized() const \
393 { \
394  return vectorType(vtkVector<type, size>::Normalized().GetData()); \
395 } \
396 
397 #define vtkVectorDerivedMacro(vectorType, type, size) \
398 vtkVectorNormalized(vectorType, type, size) \
399 explicit vectorType(type s) : Superclass(s) {} \
400 explicit vectorType(const type *i) : Superclass(i) {} \
401 explicit vectorType(const vtkTuple<type, size> &o) : Superclass(o.GetData()) {} \
402 vectorType(const vtkVector<type, size> &o) : Superclass(o.GetData()) {} \
403 
404 
405 
408 class vtkVector2i : public vtkVector2<int>
409 {
410 public:
413  vtkVector2i(int x, int y) : vtkVector2<int>(x, y) {}
415 };
417 
418 class vtkVector2f : public vtkVector2<float>
419 {
420 public:
423  vtkVector2f(float x, float y) : vtkVector2<float>(x, y) {}
425 };
426 
427 class vtkVector2d : public vtkVector2<double>
428 {
429 public:
432  vtkVector2d(double x, double y) : vtkVector2<double>(x, y) {}
434 };
435 
436 #define vtkVector3Cross(vectorType, type) \
437 vectorType Cross(const vectorType& other) const \
438 { \
439  return vectorType(vtkVector3<type>::Cross(other).GetData()); \
440 } \
441 
442 class vtkVector3i : public vtkVector3<int>
443 {
444 public:
447  vtkVector3i(int x, int y, int z) : vtkVector3<int>(x, y, z) {}
450 };
451 
452 class vtkVector3f : public vtkVector3<float>
453 {
454 public:
457  vtkVector3f(float x, float y, float z) : vtkVector3<float>(x, y, z) {}
460 };
461 
462 class vtkVector3d : public vtkVector3<double>
463 {
464 public:
467  vtkVector3d(double x, double y, double z) : vtkVector3<double>(x, y, z) {}
470 };
471 
472 class vtkVector4d : public vtkVector4<double>
473 {
474 public:
477  vtkVector4d(double x, double y, double z, double w) :
478  vtkVector4<double>(x, y, z, w) {};
480 };
481 
482 #endif // vtkVector_h
483 // VTK-HeaderTest-Exclude: vtkVector.h
T Data[Size]
The only thing stored in memory!
Definition: vtkTuple.h:145
#define vtkVector3Cross(vectorType, type)
Definition: vtkVector.h:436
void SetZ(const T &z)
Set the z component of the vector, i.e.
Definition: vtkVector.h:369
vtkVector2(const T &scalar)
Definition: vtkVector.h:158
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:264
double Normalize()
Normalize the vector in place.
Definition: vtkVector.h:91
vtkVector4(const T &scalar)
Definition: vtkVector.h:317
#define vtkVectorDerivedMacro(vectorType, type, size)
Definition: vtkVector.h:397
vtkVector3< float > Superclass
Definition: vtkVector.h:455
templated base type for storage of vectors.
Definition: vtkVector.h:40
vtkVector3< int > Superclass
Definition: vtkVector.h:445
const T & GetZ() const
Get the z component of the vector, i.e.
Definition: vtkVector.h:374
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:259
vtkVector2i(int x, int y)
Definition: vtkVector.h:413
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:176
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:349
vtkVector4d(double x, double y, double z, double w)
Definition: vtkVector.h:477
vtkVector3d(double x, double y, double z)
Definition: vtkVector.h:467
vtkVector2< int > Superclass
Definition: vtkVector.h:411
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:364
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:269
vtkVector3(const T &scalar)
Definition: vtkVector.h:224
vtkVector3< double > Superclass
Definition: vtkVector.h:465
T Dot(const vtkVector< T, Size > &other) const
The dot product of this and the supplied vector.
Definition: vtkVector.h:120
const T & GetZ() const
Get the z component of the vector, i.e.
Definition: vtkVector.h:279
vtkVector2f(float x, float y)
Definition: vtkVector.h:423
vtkVector2< double > Superclass
Definition: vtkVector.h:430
vtkVector(const T &scalar)
Initialize all of the vector&#39;s elements with the supplied scalar.
Definition: vtkVector.h:50
vtkVector2d(double x, double y)
Definition: vtkVector.h:432
void SetW(const T &w)
Set the w component of the vector, i.e.
Definition: vtkVector.h:379
templated base type for containers of constant size.
Definition: vtkTuple.h:35
vtkVector4(const T &x, const T &y, const T &z, const T &w)
Definition: vtkVector.h:325
vtkVector3i(int x, int y, int z)
Definition: vtkVector.h:447
vtkVector2(const T &x, const T &y)
Definition: vtkVector.h:166
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:254
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
void Set(const T &x, const T &y, const T &z)
Set the x, y and z components of the vector.
Definition: vtkVector.h:243
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:196
vtkVector(const T *init)
Initialize the vector&#39;s elements with the elements of the supplied array.
Definition: vtkVector.h:59
vtkVector2< float > Superclass
Definition: vtkVector.h:421
Some derived classes for the different vectors commonly used.
Definition: vtkVector.h:408
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:354
vtkVector< T, Size > Normalized() const
Return the normalized form of this vector.
Definition: vtkVector.h:108
vtkVector3< T > Cross(const vtkVector3< T > &other) const
Return the cross product of this X other.
Definition: vtkVector.h:285
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:191
vtkVector< TR, Size > Cast() const
Cast the vector to the specified type, returning the result.
Definition: vtkVector.h:136
vtkVector3(const T &x, const T &y, const T &z)
Definition: vtkVector.h:232
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:186
void SetZ(const T &z)
Set the z component of the vector, i.e.
Definition: vtkVector.h:274
const T & GetW() const
Get the w component of the vector, i.e.
Definition: vtkVector.h:384
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:359
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:201
vtkVector4(const T *init)
Definition: vtkVector.h:321
T SquaredNorm() const
Get the squared norm of the vector.
Definition: vtkVector.h:67
vtkVector()
Definition: vtkVector.h:43
vtkVector3f(float x, float y, float z)
Definition: vtkVector.h:457