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 "vtkTuple.h"
32 #include "vtkObject.h" // for legacy macros
33 
34 #include <cmath> // For math functions
35 
36 template<typename T, int Size>
37 class vtkVector : public vtkTuple<T, Size>
38 {
39 public:
41  {
42  }
43 
47  explicit vtkVector(const T& scalar) : vtkTuple<T, Size>(scalar)
48  {
49  }
50 
56  explicit vtkVector(const T* init) : 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
79  {
80  return sqrt(static_cast<double>(this->SquaredNorm()));
81  }
82 
84 
88  double Normalize()
89  {
90  const double norm(this->Norm());
91  const double inv(1.0 / norm);
92  for (int i = 0; i < Size; ++i)
93  {
94  this->Data[i] = static_cast<T>(this->Data[i] * inv);
95  }
96  return norm;
97  }
99 
101 
106  {
107  vtkVector<T, Size> temp(*this);
108  temp.Normalize();
109  return temp;
110  }
112 
114 
117  T Dot(const vtkVector<T, Size>& other) const
118  {
119  T result(0);
120  for (int i = 0; i < Size; ++i)
121  {
122  result += this->Data[i] * other[i];
123  }
124  return result;
125  }
127 
129 
132  template<typename TR>
134  {
135  vtkVector<TR, Size> result;
136  for (int i = 0; i < Size; ++i)
137  {
138  result[i] = static_cast<TR>(this->Data[i]);
139  }
140  return result;
141  }
142 };
144 
145 // .NAME vtkVector2 - templated base type for storage of 2D vectors.
146 //
147 template<typename T>
148 class vtkVector2 : public vtkVector<T, 2>
149 {
150 public:
152  {
153  }
154 
155  explicit vtkVector2(const T& scalar) : vtkVector<T, 2>(scalar)
156  {
157  }
158 
159  explicit vtkVector2(const T* init) : vtkVector<T, 2>(init)
160  {
161  }
162 
163  vtkVector2(const T& x, const T& y)
164  {
165  this->Data[0] = x;
166  this->Data[1] = y;
167  }
168 
170 
173  void Set(const T& x, const T& y)
174  {
175  this->Data[0] = x;
176  this->Data[1] = y;
177  }
179 
183  void SetX(const T& x) { this->Data[0] = x; }
184 
188  const T& GetX() const { return this->Data[0]; }
189 
193  void SetY(const T& y) { this->Data[1] = y; }
194 
198  const T& GetY() const { return this->Data[1]; }
199 
201 
204  bool operator<(const vtkVector2<T> &v) const
205  {
206  return (this->Data[0] < v.Data[0]) || (this->Data[0] == v.Data[0] && this->Data[1] < v.Data[1]);
207  }
208 };
210 
211 // .NAME vtkVector3 - templated base type for storage of 3D vectors.
212 //
213 template<typename T>
214 class vtkVector3 : public vtkVector<T, 3>
215 {
216 public:
218  {
219  }
220 
221  explicit vtkVector3(const T& scalar) : vtkVector<T, 3>(scalar)
222  {
223  }
224 
225  explicit vtkVector3(const T* init) : vtkVector<T, 3>(init)
226  {
227  }
228 
229  vtkVector3(const T& x, const T& y, const T& z)
230  {
231  this->Data[0] = x;
232  this->Data[1] = y;
233  this->Data[2] = z;
234  }
235 
237 
240  void Set(const T& x, const T& y, const T& z)
241  {
242  this->Data[0] = x;
243  this->Data[1] = y;
244  this->Data[2] = z;
245  }
247 
251  void SetX(const T& x) { this->Data[0] = x; }
252 
256  const T& GetX() const { return this->Data[0]; }
257 
261  void SetY(const T& y) { this->Data[1] = y; }
262 
266  const T& GetY() const { return this->Data[1]; }
267 
271  void SetZ(const T& z) { this->Data[2] = z; }
272 
276  const T& GetZ() const { return this->Data[2]; }
277 
279 
282  vtkVector3<T> Cross(const vtkVector3<T>& other) const
283  {
284  vtkVector3<T> res;
285  res[0] = this->Data[1] * other.Data[2] - this->Data[2] * other.Data[1];
286  res[1] = this->Data[2] * other.Data[0] - this->Data[0] * other.Data[2];
287  res[2] = this->Data[0] * other.Data[1] - this->Data[1] * other.Data[0];
288  return res;
289  }
291 
293 
296  bool operator<(const vtkVector3<T> &v) const
297  {
298  return (this->Data[0] < v.Data[0]) || (this->Data[0] == v.Data[0] && this->Data[1] < v.Data[1]) ||
299  (this->Data[0] == v.Data[0] && this->Data[1] == v.Data[1] && this->Data[2] < v.Data[2]);
300  }
301 };
303 
304 // .NAME vtkVector4 - templated base type for storage of 4D vectors.
305 //
306 template<typename T>
307 class vtkVector4 : public vtkVector<T, 4>
308 {
309 public:
311  {
312  }
313 
314  explicit vtkVector4(const T& scalar) : vtkVector<T, 4>(scalar)
315  {
316  }
317 
318  explicit vtkVector4(const T* init) : vtkVector<T, 4>(init)
319  {
320  }
321 
322  vtkVector4(const T& x, const T& y, const T& z, const T& w)
323  {
324  this->Data[0] = x;
325  this->Data[1] = y;
326  this->Data[2] = z;
327  this->Data[3] = w;
328  }
329 
331 
334  void Set(const T& x, const T& y, const T& z, const T& w)
335  {
336  this->Data[0] = x;
337  this->Data[1] = y;
338  this->Data[2] = z;
339  this->Data[3] = w;
340  }
342 
346  void SetX(const T& x) { this->Data[0] = x; }
347 
351  const T& GetX() const { return this->Data[0]; }
352 
356  void SetY(const T& y) { this->Data[1] = y; }
357 
361  const T& GetY() const { return this->Data[1]; }
362 
366  void SetZ(const T& z) { this->Data[2] = z; }
367 
371  const T& GetZ() const { return this->Data[2]; }
372 
376  void SetW(const T& w) { this->Data[3] = w; }
377 
381  const T& GetW() const { return this->Data[3]; }
382 };
384 
388 #define vtkVectorNormalized(vectorType, type, size) \
389 vectorType Normalized() const \
390 { \
391  return vectorType(vtkVector<type, size>::Normalized().GetData()); \
392 } \
393 
394 #define vtkVectorDerivedMacro(vectorType, type, size) \
395 vtkVectorNormalized(vectorType, type, size) \
396 explicit vectorType(type s) : Superclass(s) {} \
397 explicit vectorType(const type *i) : Superclass(i) {} \
398 explicit vectorType(const vtkTuple<type, size> &o) : Superclass(o.GetData()) {} \
399 vectorType(const vtkVector<type, size> &o) : Superclass(o.GetData()) {} \
400 
401 
402 
405 class vtkVector2i : public vtkVector2<int>
406 {
407 public:
410  vtkVector2i(int x, int y) : vtkVector2<int>(x, y) {}
412 };
414 
415 class vtkVector2f : public vtkVector2<float>
416 {
417 public:
420  vtkVector2f(float x, float y) : vtkVector2<float>(x, y) {}
422 };
423 
424 class vtkVector2d : public vtkVector2<double>
425 {
426 public:
429  vtkVector2d(double x, double y) : vtkVector2<double>(x, y) {}
431 };
432 
433 #define vtkVector3Cross(vectorType, type) \
434 vectorType Cross(const vectorType& other) const \
435 { \
436  return vectorType(vtkVector3<type>::Cross(other).GetData()); \
437 } \
438 
439 class vtkVector3i : public vtkVector3<int>
440 {
441 public:
444  vtkVector3i(int x, int y, int z) : vtkVector3<int>(x, y, z) {}
447 };
448 
449 class vtkVector3f : public vtkVector3<float>
450 {
451 public:
454  vtkVector3f(float x, float y, float z) : vtkVector3<float>(x, y, z) {}
457 };
458 
459 class vtkVector3d : public vtkVector3<double>
460 {
461 public:
464  vtkVector3d(double x, double y, double z) : vtkVector3<double>(x, y, z) {}
467 };
468 
469 class vtkVector4d : public vtkVector4<double>
470 {
471 public:
474  vtkVector4d(double x, double y, double z, double w) :
475  vtkVector4<double>(x, y, z, w) {};
477 };
478 
479 #endif // vtkVector_h
480 // VTK-HeaderTest-Exclude: vtkVector.h
vtkVector3< T > Cross(const vtkVector3< T > &other) const
Return the cross product of this X other.
Definition: vtkVector.h:282
T Data[Size]
The only thing stored in memory!
Definition: vtkTuple.h:145
#define vtkVector3Cross(vectorType, type)
Definition: vtkVector.h:433
void SetZ(const T &z)
Set the z component of the vector, i.e.
Definition: vtkVector.h:366
vtkVector2(const T &scalar)
Definition: vtkVector.h:155
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:261
double Normalize()
Normalize the vector in place.
Definition: vtkVector.h:88
vtkVector4(const T &scalar)
Definition: vtkVector.h:314
#define vtkVectorDerivedMacro(vectorType, type, size)
Definition: vtkVector.h:394
vtkVector3< float > Superclass
Definition: vtkVector.h:452
templated base type for storage of vectors.
Definition: vtkVector.h:37
vtkVector3< int > Superclass
Definition: vtkVector.h:442
vtkVector< T, Size > Normalized() const
Return the normalized form of this vector.
Definition: vtkVector.h:105
vtkVector2i(int x, int y)
Definition: vtkVector.h:410
double Norm() const
Get the norm of the vector, i.e.
Definition: vtkVector.h:78
vtkVector2(const T *init)
Definition: vtkVector.h:159
void Set(const T &x, const T &y)
Set the x and y components of the vector.
Definition: vtkVector.h:173
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:346
const T & GetW() const
Get the w component of the vector, i.e.
Definition: vtkVector.h:381
vtkVector4d(double x, double y, double z, double w)
Definition: vtkVector.h:474
vtkVector3d(double x, double y, double z)
Definition: vtkVector.h:464
vtkVector2< int > Superclass
Definition: vtkVector.h:408
vtkVector3(const T &scalar)
Definition: vtkVector.h:221
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:256
vtkVector3< double > Superclass
Definition: vtkVector.h:462
vtkVector2f(float x, float y)
Definition: vtkVector.h:420
vtkVector2< double > Superclass
Definition: vtkVector.h:427
vtkVector(const T &scalar)
Initialize all of the vector's elements with the supplied scalar.
Definition: vtkVector.h:47
vtkVector2d(double x, double y)
Definition: vtkVector.h:429
void SetW(const T &w)
Set the w component of the vector, i.e.
Definition: vtkVector.h:376
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:322
vtkVector3i(int x, int y, int z)
Definition: vtkVector.h:444
vtkVector2(const T &x, const T &y)
Definition: vtkVector.h:163
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:251
bool operator<(const vtkVector3< T > &v) const
Lexicographical comparison of two vector.
Definition: vtkVector.h:296
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:334
void Set(const T &x, const T &y, const T &z)
Set the x, y and z components of the vector.
Definition: vtkVector.h:240
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:193
vtkVector(const T *init)
Initialize the vector's elements with the elements of the supplied array.
Definition: vtkVector.h:56
vtkVector2< float > Superclass
Definition: vtkVector.h:418
bool operator<(const vtkVector2< T > &v) const
Lexicographical comparison of two vector.
Definition: vtkVector.h:204
Some derived classes for the different vectors commonly used.
Definition: vtkVector.h:405
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:198
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:188
vtkVector< TR, Size > Cast() const
Cast the vector to the specified type, returning the result.
Definition: vtkVector.h:133
vtkVectorDerivedMacro(vtkVector3i, int, 3) vtkVector3Cross(vtkVector3i
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:266
T SquaredNorm() const
Get the squared norm of the vector.
Definition: vtkVector.h:64
vtkVector3(const T &x, const T &y, const T &z)
Definition: vtkVector.h:229
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:183
void SetZ(const T &z)
Set the z component of the vector, i.e.
Definition: vtkVector.h:271
vtkVector3(const T *init)
Definition: vtkVector.h:225
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:351
T Dot(const vtkVector< T, Size > &other) const
The dot product of this and the supplied vector.
Definition: vtkVector.h:117
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:356
const T & GetZ() const
Get the z component of the vector, i.e.
Definition: vtkVector.h:276
vtkVector4(const T *init)
Definition: vtkVector.h:318
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:361
vtkVector()
Definition: vtkVector.h:40
const T & GetZ() const
Get the z component of the vector, i.e.
Definition: vtkVector.h:371
vtkVector3f(float x, float y, float z)
Definition: vtkVector.h:454