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 
307 #define vtkVectorNormalized(vectorType, type, size) \
308 vectorType Normalized() const \
309 { \
310  return vectorType(vtkVector<type, size>::Normalized().GetData()); \
311 } \
312 
313 #define vtkVectorDerivedMacro(vectorType, type, size) \
314 vtkVectorNormalized(vectorType, type, size) \
315 explicit vectorType(type s) : Superclass(s) {} \
316 explicit vectorType(const type *i) : Superclass(i) {} \
317 explicit vectorType(const vtkTuple<type, size> &o) : Superclass(o.GetData()) {} \
318 vectorType(const vtkVector<type, size> &o) : Superclass(o.GetData()) {} \
319 
320 
321 
324 class vtkVector2i : public vtkVector2<int>
325 {
326 public:
329  vtkVector2i(int x, int y) : vtkVector2<int>(x, y) {}
331 };
333 
334 class vtkVector2f : public vtkVector2<float>
335 {
336 public:
339  vtkVector2f(float x, float y) : vtkVector2<float>(x, y) {}
341 };
342 
343 class vtkVector2d : public vtkVector2<double>
344 {
345 public:
348  vtkVector2d(double x, double y) : vtkVector2<double>(x, y) {}
350 };
351 
352 #define vtkVector3Cross(vectorType, type) \
353 vectorType Cross(const vectorType& other) const \
354 { \
355  return vectorType(vtkVector3<type>::Cross(other).GetData()); \
356 } \
357 
358 class vtkVector3i : public vtkVector3<int>
359 {
360 public:
363  vtkVector3i(int x, int y, int z) : vtkVector3<int>(x, y, z) {}
366 };
367 
368 class vtkVector3f : public vtkVector3<float>
369 {
370 public:
373  vtkVector3f(float x, float y, float z) : vtkVector3<float>(x, y, z) {}
376 };
377 
378 class vtkVector3d : public vtkVector3<double>
379 {
380 public:
383  vtkVector3d(double x, double y, double z) : vtkVector3<double>(x, y, z) {}
386 };
387 
388 #endif // vtkVector_h
389 // 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:352
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
#define vtkVectorDerivedMacro(vectorType, type, size)
Definition: vtkVector.h:313
vtkVector3< float > Superclass
Definition: vtkVector.h:371
templated base type for storage of vectors.
Definition: vtkVector.h:37
vtkVector3< int > Superclass
Definition: vtkVector.h:361
vtkVector< T, Size > Normalized() const
Return the normalized form of this vector.
Definition: vtkVector.h:105
vtkVector2i(int x, int y)
Definition: vtkVector.h:329
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
vtkVector3d(double x, double y, double z)
Definition: vtkVector.h:383
vtkVector2< int > Superclass
Definition: vtkVector.h:327
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:381
vtkVector2f(float x, float y)
Definition: vtkVector.h:339
vtkVector2< double > Superclass
Definition: vtkVector.h:346
vtkVector(const T &scalar)
Initialize all of the vector&#39;s elements with the supplied scalar.
Definition: vtkVector.h:47
vtkVector2d(double x, double y)
Definition: vtkVector.h:348
templated base type for containers of constant size.
Definition: vtkTuple.h:35
vtkVector3i(int x, int y, int z)
Definition: vtkVector.h:363
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
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)
Initalize the vector&#39;s elements with the elements of the supplied array.
Definition: vtkVector.h:56
vtkVector2< float > Superclass
Definition: vtkVector.h:337
Some derived classes for the different vectors commonly used.
Definition: vtkVector.h:324
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
T Dot(const vtkVector< T, Size > &other) const
The dot product of this and the supplied vector.
Definition: vtkVector.h:117
const T & GetZ() const
Get the z component of the vector, i.e.
Definition: vtkVector.h:276
vtkVector()
Definition: vtkVector.h:40
vtkVector3f(float x, float y, float z)
Definition: vtkVector.h:373