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  if (norm == 0.0)
95  {
96  return 0.0;
97  }
98  const double inv(1.0 / norm);
99  for (int i = 0; i < Size; ++i)
100  {
101  this->Data[i] = static_cast<T>(this->Data[i] * inv);
102  }
103  return norm;
104  }
106 
108 
113  {
114  vtkVector<T, Size> temp(*this);
115  temp.Normalize();
116  return temp;
117  }
119 
121 
124  T Dot(const vtkVector<T, Size>& other) const
125  {
126  T result(0);
127  for (int i = 0; i < Size; ++i)
128  {
129  result += this->Data[i] * other[i];
130  }
131  return result;
132  }
134 
136 
139  template<typename TR>
141  {
142  vtkVector<TR, Size> result;
143  for (int i = 0; i < Size; ++i)
144  {
145  result[i] = static_cast<TR>(this->Data[i]);
146  }
147  return result;
148  }
149 };
151 
152 // .NAME vtkVector2 - templated base type for storage of 2D vectors.
153 //
154 template<typename T>
155 class vtkVector2 : public vtkVector<T, 2>
156 {
157 public:
159  {
160  }
161 
162  explicit vtkVector2(const T& scalar) : vtkVector<T, 2>(scalar)
163  {
164  }
165 
166  explicit vtkVector2(const T* init) : vtkVector<T, 2>(init)
167  {
168  }
169 
170  vtkVector2(const T& x, const T& y)
171  {
172  this->Data[0] = x;
173  this->Data[1] = y;
174  }
175 
177 
180  void Set(const T& x, const T& y)
181  {
182  this->Data[0] = x;
183  this->Data[1] = y;
184  }
186 
190  void SetX(const T& x) { this->Data[0] = x; }
191 
195  const T& GetX() const { return this->Data[0]; }
196 
200  void SetY(const T& y) { this->Data[1] = y; }
201 
205  const T& GetY() const { return this->Data[1]; }
206 
208 
211  bool operator<(const vtkVector2<T> &v) const
212  {
213  return (this->Data[0] < v.Data[0]) || (this->Data[0] == v.Data[0] && this->Data[1] < v.Data[1]);
214  }
215 };
217 
218 // .NAME vtkVector3 - templated base type for storage of 3D vectors.
219 //
220 template<typename T>
221 class vtkVector3 : public vtkVector<T, 3>
222 {
223 public:
225  {
226  }
227 
228  explicit vtkVector3(const T& scalar) : vtkVector<T, 3>(scalar)
229  {
230  }
231 
232  explicit vtkVector3(const T* init) : vtkVector<T, 3>(init)
233  {
234  }
235 
236  vtkVector3(const T& x, const T& y, const T& z)
237  {
238  this->Data[0] = x;
239  this->Data[1] = y;
240  this->Data[2] = z;
241  }
242 
244 
247  void Set(const T& x, const T& y, const T& z)
248  {
249  this->Data[0] = x;
250  this->Data[1] = y;
251  this->Data[2] = z;
252  }
254 
258  void SetX(const T& x) { this->Data[0] = x; }
259 
263  const T& GetX() const { return this->Data[0]; }
264 
268  void SetY(const T& y) { this->Data[1] = y; }
269 
273  const T& GetY() const { return this->Data[1]; }
274 
278  void SetZ(const T& z) { this->Data[2] = z; }
279 
283  const T& GetZ() const { return this->Data[2]; }
284 
286 
289  vtkVector3<T> Cross(const vtkVector3<T>& other) const
290  {
291  vtkVector3<T> res;
292  res[0] = this->Data[1] * other.Data[2] - this->Data[2] * other.Data[1];
293  res[1] = this->Data[2] * other.Data[0] - this->Data[0] * other.Data[2];
294  res[2] = this->Data[0] * other.Data[1] - this->Data[1] * other.Data[0];
295  return res;
296  }
298 
300 
303  bool operator<(const vtkVector3<T> &v) const
304  {
305  return (this->Data[0] < v.Data[0]) || (this->Data[0] == v.Data[0] && this->Data[1] < v.Data[1]) ||
306  (this->Data[0] == v.Data[0] && this->Data[1] == v.Data[1] && this->Data[2] < v.Data[2]);
307  }
308 };
310 
311 // .NAME vtkVector4 - templated base type for storage of 4D vectors.
312 //
313 template<typename T>
314 class vtkVector4 : public vtkVector<T, 4>
315 {
316 public:
318  {
319  }
320 
321  explicit vtkVector4(const T& scalar) : vtkVector<T, 4>(scalar)
322  {
323  }
324 
325  explicit vtkVector4(const T* init) : vtkVector<T, 4>(init)
326  {
327  }
328 
329  vtkVector4(const T& x, const T& y, const T& z, const T& w)
330  {
331  this->Data[0] = x;
332  this->Data[1] = y;
333  this->Data[2] = z;
334  this->Data[3] = w;
335  }
336 
338 
341  void Set(const T& x, const T& y, const T& z, const T& w)
342  {
343  this->Data[0] = x;
344  this->Data[1] = y;
345  this->Data[2] = z;
346  this->Data[3] = w;
347  }
349 
353  void SetX(const T& x) { this->Data[0] = x; }
354 
358  const T& GetX() const { return this->Data[0]; }
359 
363  void SetY(const T& y) { this->Data[1] = y; }
364 
368  const T& GetY() const { return this->Data[1]; }
369 
373  void SetZ(const T& z) { this->Data[2] = z; }
374 
378  const T& GetZ() const { return this->Data[2]; }
379 
383  void SetW(const T& w) { this->Data[3] = w; }
384 
388  const T& GetW() const { return this->Data[3]; }
389 };
391 
395 #define vtkVectorNormalized(vectorType, type, size) \
396 vectorType Normalized() const \
397 { \
398  return vectorType(vtkVector<type, size>::Normalized().GetData()); \
399 } \
400 
401 #define vtkVectorDerivedMacro(vectorType, type, size) \
402 vtkVectorNormalized(vectorType, type, size) \
403 explicit vectorType(type s) : Superclass(s) {} \
404 explicit vectorType(const type *i) : Superclass(i) {} \
405 explicit vectorType(const vtkTuple<type, size> &o) : Superclass(o.GetData()) {} \
406 vectorType(const vtkVector<type, size> &o) : Superclass(o.GetData()) {} \
407 
408 
409 
412 class vtkVector2i : public vtkVector2<int>
413 {
414 public:
417  vtkVector2i(int x, int y) : vtkVector2<int>(x, y) {}
419 };
421 
422 class vtkVector2f : public vtkVector2<float>
423 {
424 public:
427  vtkVector2f(float x, float y) : vtkVector2<float>(x, y) {}
429 };
430 
431 class vtkVector2d : public vtkVector2<double>
432 {
433 public:
436  vtkVector2d(double x, double y) : vtkVector2<double>(x, y) {}
438 };
439 
440 #define vtkVector3Cross(vectorType, type) \
441 vectorType Cross(const vectorType& other) const \
442 { \
443  return vectorType(vtkVector3<type>::Cross(other).GetData()); \
444 } \
445 
446 class vtkVector3i : public vtkVector3<int>
447 {
448 public:
451  vtkVector3i(int x, int y, int z) : vtkVector3<int>(x, y, z) {}
454 };
455 
456 class vtkVector3f : public vtkVector3<float>
457 {
458 public:
461  vtkVector3f(float x, float y, float z) : vtkVector3<float>(x, y, z) {}
464 };
465 
466 class vtkVector3d : public vtkVector3<double>
467 {
468 public:
471  vtkVector3d(double x, double y, double z) : vtkVector3<double>(x, y, z) {}
474 };
475 
476 class vtkVector4d : public vtkVector4<double>
477 {
478 public:
481  vtkVector4d(double x, double y, double z, double w) :
482  vtkVector4<double>(x, y, z, w) {};
484 };
485 
486 #endif // vtkVector_h
487 // 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:440
void SetZ(const T &z)
Set the z component of the vector, i.e.
Definition: vtkVector.h:373
vtkVector2(const T &scalar)
Definition: vtkVector.h:162
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:268
double Normalize()
Normalize the vector in place.
Definition: vtkVector.h:91
vtkVector4(const T &scalar)
Definition: vtkVector.h:321
#define vtkVectorDerivedMacro(vectorType, type, size)
Definition: vtkVector.h:401
vtkVector3< float > Superclass
Definition: vtkVector.h:459
templated base type for storage of vectors.
Definition: vtkVector.h:40
vtkVector3< int > Superclass
Definition: vtkVector.h:449
const T & GetZ() const
Get the z component of the vector, i.e.
Definition: vtkVector.h:378
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:263
vtkVector2i(int x, int y)
Definition: vtkVector.h:417
vtkVector2(const T *init)
Definition: vtkVector.h:166
void Set(const T &x, const T &y)
Set the x and y components of the vector.
Definition: vtkVector.h:180
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:353
vtkVector4d(double x, double y, double z, double w)
Definition: vtkVector.h:481
vtkVector3d(double x, double y, double z)
Definition: vtkVector.h:471
vtkVector2< int > Superclass
Definition: vtkVector.h:415
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:368
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:273
vtkVector3(const T &scalar)
Definition: vtkVector.h:228
vtkVector3< double > Superclass
Definition: vtkVector.h:469
T Dot(const vtkVector< T, Size > &other) const
The dot product of this and the supplied vector.
Definition: vtkVector.h:124
const T & GetZ() const
Get the z component of the vector, i.e.
Definition: vtkVector.h:283
vtkVector2f(float x, float y)
Definition: vtkVector.h:427
vtkVector2< double > Superclass
Definition: vtkVector.h:434
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:436
void SetW(const T &w)
Set the w component of the vector, i.e.
Definition: vtkVector.h:383
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:329
vtkVector3i(int x, int y, int z)
Definition: vtkVector.h:451
vtkVector2(const T &x, const T &y)
Definition: vtkVector.h:170
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:258
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:341
void Set(const T &x, const T &y, const T &z)
Set the x, y and z components of the vector.
Definition: vtkVector.h:247
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:200
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:425
Some derived classes for the different vectors commonly used.
Definition: vtkVector.h:412
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:358
vtkVector< T, Size > Normalized() const
Return the normalized form of this vector.
Definition: vtkVector.h:112
vtkVector3< T > Cross(const vtkVector3< T > &other) const
Return the cross product of this X other.
Definition: vtkVector.h:289
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:195
vtkVector< TR, Size > Cast() const
Cast the vector to the specified type, returning the result.
Definition: vtkVector.h:140
vtkVector3(const T &x, const T &y, const T &z)
Definition: vtkVector.h:236
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:190
void SetZ(const T &z)
Set the z component of the vector, i.e.
Definition: vtkVector.h:278
const T & GetW() const
Get the w component of the vector, i.e.
Definition: vtkVector.h:388
vtkVector3(const T *init)
Definition: vtkVector.h:232
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:363
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:205
vtkVector4(const T *init)
Definition: vtkVector.h:325
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:461