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 
30 #ifndef vtkVector_h
31 #define vtkVector_h
32 
33 #include "vtkTuple.h"
34 #include "vtkObject.h" // for legacy macros
35 
36 #include <cmath> // For math functions
37 
38 template<typename T, int Size>
39 class vtkVector : public vtkTuple<T, Size>
40 {
41 public:
43  {
44  }
45 
47 
48  explicit vtkVector(const T& scalar) : vtkTuple<T, Size>(scalar)
49  {
50  }
52 
54 
58  explicit vtkVector(const T* init) : vtkTuple<T, Size>(init)
59  {
60  }
62 
64 
65  T SquaredNorm() const
66  {
67  T result = 0;
68  for (int i = 0; i < Size; ++i)
69  {
70  result += this->Data[i] * this->Data[i];
71  }
72  return result;
73  }
75 
77 
78  double Norm() const
79  {
80  return sqrt(static_cast<double>(this->SquaredNorm()));
81  }
83 
85 
86  double Normalize()
87  {
88  const double norm(this->Norm());
89  const double inv(1.0 / norm);
90  for (int i = 0; i < Size; ++i)
91  {
92  this->Data[i] = static_cast<T>(this->Data[i] * inv);
93  }
94  return norm;
95  }
97 
99 
102  {
103  vtkVector<T, Size> temp(*this);
104  temp.Normalize();
105  return temp;
106  }
108 
110 
111  T Dot(const vtkVector<T, Size>& other) const
112  {
113  T result(0);
114  for (int i = 0; i < Size; ++i)
115  {
116  result += this->Data[i] * other[i];
117  }
118  return result;
119  }
121 
123 
124  template<typename TR>
126  {
127  vtkVector<TR, Size> result;
128  for (int i = 0; i < Size; ++i)
129  {
130  result[i] = static_cast<TR>(this->Data[i]);
131  }
132  return result;
133  }
134 };
136 
137 // .NAME vtkVector2 - templated base type for storage of 2D vectors.
138 //
139 template<typename T>
140 class vtkVector2 : public vtkVector<T, 2>
141 {
142 public:
144  {
145  }
146 
147  explicit vtkVector2(const T& scalar) : vtkVector<T, 2>(scalar)
148  {
149  }
150 
151  explicit vtkVector2(const T* init) : vtkVector<T, 2>(init)
152  {
153  }
154 
155  vtkVector2(const T& x, const T& y)
156  {
157  this->Data[0] = x;
158  this->Data[1] = y;
159  }
160 
162 
163  void Set(const T& x, const T& y)
164  {
165  this->Data[0] = x;
166  this->Data[1] = y;
167  }
169 
171  void SetX(const T& x) { this->Data[0] = x; }
172 
174  const T& GetX() const { return this->Data[0]; }
175 
177  void SetY(const T& y) { this->Data[1] = y; }
178 
180 
181  const T& GetY() const { return this->Data[1]; }
182 };
184 
185 // .NAME vtkVector3 - templated base type for storage of 3D vectors.
186 //
187 template<typename T>
188 class vtkVector3 : public vtkVector<T, 3>
189 {
190 public:
192  {
193  }
194 
195  explicit vtkVector3(const T& scalar) : vtkVector<T, 3>(scalar)
196  {
197  }
198 
199  explicit vtkVector3(const T* init) : vtkVector<T, 3>(init)
200  {
201  }
202 
203  vtkVector3(const T& x, const T& y, const T& z)
204  {
205  this->Data[0] = x;
206  this->Data[1] = y;
207  this->Data[2] = z;
208  }
209 
211 
212  void Set(const T& x, const T& y, const T& z)
213  {
214  this->Data[0] = x;
215  this->Data[1] = y;
216  this->Data[2] = z;
217  }
219 
221  void SetX(const T& x) { this->Data[0] = x; }
222 
224  const T& GetX() const { return this->Data[0]; }
225 
227  void SetY(const T& y) { this->Data[1] = y; }
228 
230  const T& GetY() const { return this->Data[1]; }
231 
233  void SetZ(const T& z) { this->Data[2] = z; }
234 
236  const T& GetZ() const { return this->Data[2]; }
237 
239 
240  vtkVector3<T> Cross(const vtkVector3<T>& other) const
241  {
242  vtkVector3<T> res;
243  res[0] = this->Data[1] * other.Data[2] - this->Data[2] * other.Data[1];
244  res[1] = this->Data[2] * other.Data[0] - this->Data[0] * other.Data[2];
245  res[2] = this->Data[0] * other.Data[1] - this->Data[1] * other.Data[0];
246  return res;
247  }
248 };
250 
252 
253 #define vtkVectorNormalized(vectorType, type, size) \
254 vectorType Normalized() const \
255 { \
256  return vectorType(vtkVector<type, size>::Normalized().GetData()); \
257 } \
258 
259 
260 #define vtkVectorDerivedMacro(vectorType, type, size) \
261 vtkVectorNormalized(vectorType, type, size) \
262 
263 
264 
265 class vtkVector2i : public vtkVector2<int>
266 {
267 public:
269  vtkVector2i(int x, int y) : vtkVector2<int>(x, y) {}
270  explicit vtkVector2i(int scalar) : vtkVector2<int>(scalar) {}
271  explicit vtkVector2i(const int *init) : vtkVector2<int>(init) {}
273 };
275 
276 class vtkVector2f : public vtkVector2<float>
277 {
278 public:
280  vtkVector2f(float x, float y) : vtkVector2<float>(x, y) {}
281  explicit vtkVector2f(float scalar) : vtkVector2<float>(scalar) {}
282  explicit vtkVector2f(const float* i) : vtkVector2<float>(i) {}
284 };
285 
286 class vtkVector2d : public vtkVector2<double>
287 {
288 public:
290  vtkVector2d(double x, double y) : vtkVector2<double>(x, y) {}
291  explicit vtkVector2d(double scalar) : vtkVector2<double>(scalar) {}
292  explicit vtkVector2d(const double *init) : vtkVector2<double>(init) {}
294 };
295 
296 #define vtkVector3Cross(vectorType, type) \
297 vectorType Cross(const vectorType& other) const \
298 { \
299  return vectorType(vtkVector3<type>::Cross(other).GetData()); \
300 } \
301 
302 class vtkVector3i : public vtkVector3<int>
303 {
304 public:
306  vtkVector3i(int x, int y, int z) : vtkVector3<int>(x, y, z) {}
307  explicit vtkVector3i(int scalar) : vtkVector3<int>(scalar) {}
308  explicit vtkVector3i(const int *init) : vtkVector3<int>(init) {}
311 };
312 
313 class vtkVector3f : public vtkVector3<float>
314 {
315 public:
317  vtkVector3f(float x, float y, float z) : vtkVector3<float>(x, y, z) {}
318  explicit vtkVector3f(float scalar) : vtkVector3<float>(scalar) {}
319  explicit vtkVector3f(const float *init) : vtkVector3<float>(init) {}
322 };
323 
324 class vtkVector3d : public vtkVector3<double>
325 {
326 public:
328  vtkVector3d(double x, double y, double z) : vtkVector3<double>(x, y, z) {}
329  explicit vtkVector3d(double scalar) : vtkVector3<double>(scalar) {}
330  explicit vtkVector3d(const double *init) : vtkVector3<double>(init) {}
333 };
334 
335 #endif // vtkVector_h
336 // VTK-HeaderTest-Exclude: vtkVector.h
T Data[Size]
Definition: vtkTuple.h:136
#define vtkVector3Cross(vectorType, type)
Definition: vtkVector.h:296
vtkVector2d(double scalar)
Definition: vtkVector.h:291
vtkVector2(const T &scalar)
Definition: vtkVector.h:147
void SetY(const T &y)
Definition: vtkVector.h:227
double Normalize()
Definition: vtkVector.h:86
#define vtkVectorDerivedMacro(vectorType, type, size)
Definition: vtkVector.h:260
templated base type for storage of vectors.
Definition: vtkVector.h:39
vtkVector2f(float scalar)
Definition: vtkVector.h:281
const T & GetX() const
Definition: vtkVector.h:224
vtkVector2d(const double *init)
Definition: vtkVector.h:292
vtkVector3i(const int *init)
Definition: vtkVector.h:308
vtkVector2i(int x, int y)
Definition: vtkVector.h:269
vtkVector2f(const float *i)
Definition: vtkVector.h:282
vtkVector2i(int scalar)
Definition: vtkVector.h:270
vtkVector2(const T *init)
Definition: vtkVector.h:151
void Set(const T &x, const T &y)
Definition: vtkVector.h:163
vtkVector3d(double x, double y, double z)
Definition: vtkVector.h:328
const T & GetY() const
Definition: vtkVector.h:230
vtkVector3(const T &scalar)
Definition: vtkVector.h:195
vtkVector3d(double scalar)
Definition: vtkVector.h:329
T Dot(const vtkVector< T, Size > &other) const
Definition: vtkVector.h:111
const T & GetZ() const
Definition: vtkVector.h:236
vtkVector2f(float x, float y)
Definition: vtkVector.h:280
vtkVector(const T &scalar)
Definition: vtkVector.h:48
vtkVector2d(double x, double y)
Definition: vtkVector.h:290
templated base type for containers of constant size.
Definition: vtkTuple.h:34
vtkVector3i(int x, int y, int z)
Definition: vtkVector.h:306
vtkVector2(const T &x, const T &y)
Definition: vtkVector.h:155
double Norm() const
Definition: vtkVector.h:78
void SetX(const T &x)
Definition: vtkVector.h:221
vtkVector3f(float scalar)
Definition: vtkVector.h:318
void Set(const T &x, const T &y, const T &z)
Definition: vtkVector.h:212
void SetY(const T &y)
Definition: vtkVector.h:177
vtkVector(const T *init)
Definition: vtkVector.h:58
vtkVector2i(const int *init)
Definition: vtkVector.h:271
vtkVector< T, Size > Normalized() const
Definition: vtkVector.h:101
vtkVector3< T > Cross(const vtkVector3< T > &other) const
Definition: vtkVector.h:240
const T & GetX() const
Definition: vtkVector.h:174
vtkVector3d(const double *init)
Definition: vtkVector.h:330
vtkVectorDerivedMacro(vtkVector3i, int, 3) vtkVector3Cross(vtkVector3i
vtkVector< TR, Size > Cast() const
Definition: vtkVector.h:125
vtkVector3(const T &x, const T &y, const T &z)
Definition: vtkVector.h:203
void SetX(const T &x)
Definition: vtkVector.h:171
void SetZ(const T &z)
Definition: vtkVector.h:233
vtkVector3(const T *init)
Definition: vtkVector.h:199
vtkVector3i(int scalar)
Definition: vtkVector.h:307
vtkVector3f(const float *init)
Definition: vtkVector.h:319
const T & GetY() const
Definition: vtkVector.h:181
T SquaredNorm() const
Definition: vtkVector.h:65
vtkVector()
Definition: vtkVector.h:42
vtkVector3f(float x, float y, float z)
Definition: vtkVector.h:317