VTK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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  {
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  const T& GetY() const { return this->Data[1]; }
181 
183  VTK_LEGACY(const T& X() const);
184 
186 
187  VTK_LEGACY(const T& Y() const);
188 };
190 
191 // .NAME vtkVector3 - templated base type for storage of 3D vectors.
192 //
193 template<typename T>
194 class vtkVector3 : public vtkVector<T, 3>
195 {
196 public:
198  {
199  }
200 
201  explicit vtkVector3(const T& scalar) : vtkVector<T, 3>(scalar)
202  {
203  }
204 
205  explicit vtkVector3(const T* init) : vtkVector<T, 3>(init)
206  {
207  }
208 
209  vtkVector3(const T& x, const T& y, const T& z)
210  {
211  this->Data[0] = x;
212  this->Data[1] = y;
213  this->Data[2] = z;
214  }
215 
217 
218  void Set(const T& x, const T& y, const T& z)
219  {
220  this->Data[0] = x;
221  this->Data[1] = y;
222  this->Data[2] = z;
223  }
225 
227  void SetX(const T& x) { this->Data[0] = x; }
228 
230  const T& GetX() const { return this->Data[0]; }
231 
233  void SetY(const T& y) { this->Data[1] = y; }
234 
236  const T& GetY() const { return this->Data[1]; }
237 
239  void SetZ(const T& z) { this->Data[2] = z; }
240 
242  const T& GetZ() const { return this->Data[2]; }
243 
245 
246  vtkVector3<T> Cross(const vtkVector3<T>& other) const
247  {
249  res[0] = this->Data[1] * other.Data[2] - this->Data[2] * other.Data[1];
250  res[1] = this->Data[2] * other.Data[0] - this->Data[0] * other.Data[2];
251  res[2] = this->Data[0] * other.Data[1] - this->Data[1] * other.Data[0];
252  return res;
253  }
255 
257  VTK_LEGACY(const T& X() const);
258 
260  VTK_LEGACY(const T& Y() const);
261 
263 
264  VTK_LEGACY(const T& Z() const);
265 };
267 
269 
270 #define vtkVectorNormalized(vectorType, type, size) \
271 vectorType Normalized() \
272 { \
273  return vectorType(vtkVector<type, size>::Normalized().GetData()); \
274 } \
275 
276 
277 #define vtkVectorDerivedMacro(vectorType, type, size) \
278 vtkVectorNormalized(vectorType, type, size) \
279 
280 
281 
282 class vtkVector2i : public vtkVector2<int>
283 {
284 public:
286  vtkVector2i(int x, int y) : vtkVector2<int>(x, y) {}
287  explicit vtkVector2i(int scalar) : vtkVector2<int>(scalar) {}
288  explicit vtkVector2i(const int *init) : vtkVector2<int>(init) {}
290 };
292 
293 class vtkVector2f : public vtkVector2<float>
294 {
295 public:
297  vtkVector2f(float x, float y) : vtkVector2<float>(x, y) {}
298  explicit vtkVector2f(float scalar) : vtkVector2<float>(scalar) {}
299  explicit vtkVector2f(const float* i) : vtkVector2<float>(i) {}
301 };
302 
303 class vtkVector2d : public vtkVector2<double>
304 {
305 public:
307  vtkVector2d(double x, double y) : vtkVector2<double>(x, y) {}
308  explicit vtkVector2d(double scalar) : vtkVector2<double>(scalar) {}
309  explicit vtkVector2d(const double *init) : vtkVector2<double>(init) {}
311 };
312 
313 #define vtkVector3Cross(vectorType, type) \
314 vectorType Cross(const vectorType& other) \
315 { \
316  return vectorType(vtkVector3<type>::Cross(other).GetData()); \
317 } \
318 
319 class vtkVector3i : public vtkVector3<int>
320 {
321 public:
323  vtkVector3i(int x, int y, int z) : vtkVector3<int>(x, y, z) {}
324  explicit vtkVector3i(int scalar) : vtkVector3<int>(scalar) {}
325  explicit vtkVector3i(const int *init) : vtkVector3<int>(init) {}
328 };
329 
330 class vtkVector3f : public vtkVector3<float>
331 {
332 public:
334  vtkVector3f(float x, float y, float z) : vtkVector3<float>(x, y, z) {}
335  explicit vtkVector3f(float scalar) : vtkVector3<float>(scalar) {}
336  explicit vtkVector3f(const float *init) : vtkVector3<float>(init) {}
339 };
340 
341 class vtkVector3d : public vtkVector3<double>
342 {
343 public:
345  vtkVector3d(double x, double y, double z) : vtkVector3<double>(x, y, z) {}
346  explicit vtkVector3d(double scalar) : vtkVector3<double>(scalar) {}
347  explicit vtkVector3d(const double *init) : vtkVector3<double>(init) {}
350 };
351 
352 #ifndef VTK_LEGACY_REMOVE
353 template<typename T>
354 const T& vtkVector2<T>::X() const
355 {
357  return this->GetX();
358 }
359 
360 template<typename T>
361 const T& vtkVector2<T>::Y() const
362 {
364  return this->GetY();
365 }
366 
367 template<typename T>
368 const T& vtkVector3<T>::X() const
369 {
371  return this->GetX();
372 }
373 
374 template<typename T>
375 const T& vtkVector3<T>::Y() const
376 {
378  return this->GetY();
379 }
380 
381 template<typename T>
382 const T& vtkVector3<T>::Z() const
383 {
385  return this->GetZ();
386 }
387 #endif // VTK_LEGACY_REMOVE
388 
389 #endif // __vtkVector_h
390 // VTK-HeaderTest-Exclude: vtkVector.h
T Data[Size]
Definition: vtkTuple.h:136
#define vtkVector3Cross(vectorType, type)
Definition: vtkVector.h:313
vtkVector2d(double scalar)
Definition: vtkVector.h:308
vtkVector2(const T &scalar)
Definition: vtkVector.h:147
void SetY(const T &y)
Definition: vtkVector.h:233
double Normalize()
Definition: vtkVector.h:86
const T & Y() const
Definition: vtkVector.h:375
#define vtkVectorDerivedMacro(vectorType, type, size)
Definition: vtkVector.h:277
templated base type for storage of vectors.
Definition: vtkVector.h:39
vtkVector2f(float scalar)
Definition: vtkVector.h:298
GLuint res
Definition: vtkgl.h:16902
const T & GetX() const
Definition: vtkVector.h:230
vtkVector2d(const double *init)
Definition: vtkVector.h:309
vtkVector3i(const int *init)
Definition: vtkVector.h:325
vtkVector2i(int x, int y)
Definition: vtkVector.h:286
vtkVector2f(const float *i)
Definition: vtkVector.h:299
#define VTK_LEGACY_REPLACED_BODY(method, version, replace)
Definition: vtkSetGet.h:814
vtkVector2i(int scalar)
Definition: vtkVector.h:287
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:345
const T & X() const
Definition: vtkVector.h:354
GLint GLint GLint GLint GLint GLint y
Definition: vtkgl.h:11318
const T & GetY() const
Definition: vtkVector.h:236
vtkVector3(const T &scalar)
Definition: vtkVector.h:201
GLdouble GLdouble z
Definition: vtkgl.h:11754
vtkVector3d(double scalar)
Definition: vtkVector.h:346
T Dot(const vtkVector< T, Size > &other) const
Definition: vtkVector.h:111
const T & GetZ() const
Definition: vtkVector.h:242
GLint GLint GLint GLint GLint x
Definition: vtkgl.h:11318
vtkVector2f(float x, float y)
Definition: vtkVector.h:297
vtkVector(const T &scalar)
Definition: vtkVector.h:48
vtkVector2d(double x, double y)
Definition: vtkVector.h:307
templated base type for containers of constant size.
Definition: vtkTuple.h:34
vtkVector3i(int x, int y, int z)
Definition: vtkVector.h:323
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:227
vtkVector3f(float scalar)
Definition: vtkVector.h:335
void Set(const T &x, const T &y, const T &z)
Definition: vtkVector.h:218
void SetY(const T &y)
Definition: vtkVector.h:177
vtkVector(const T *init)
Definition: vtkVector.h:58
vtkVector2i(const int *init)
Definition: vtkVector.h:288
const T & Y() const
Definition: vtkVector.h:361
vtkVector< T, Size > Normalized() const
Definition: vtkVector.h:101
vtkVector3< T > Cross(const vtkVector3< T > &other) const
Definition: vtkVector.h:246
const T & GetX() const
Definition: vtkVector.h:174
#define VTK_LEGACY(method)
Definition: vtkSetGet.h:787
vtkVector3d(const double *init)
Definition: vtkVector.h:347
GLuint64EXT * result
Definition: vtkgl.h:18868
vtkVectorDerivedMacro(vtkVector3i, int, 3) vtkVector3Cross(vtkVector3i
vtkVector< TR, Size > Cast() const
Definition: vtkVector.h:125
const T & Z() const
Definition: vtkVector.h:382
vtkVector3(const T &x, const T &y, const T &z)
Definition: vtkVector.h:209
void SetX(const T &x)
Definition: vtkVector.h:171
const T & X() const
Definition: vtkVector.h:368
void SetZ(const T &z)
Definition: vtkVector.h:239
vtkVector3(const T *init)
Definition: vtkVector.h:205
vtkVector3i(int scalar)
Definition: vtkVector.h:324
vtkVector3f(const float *init)
Definition: vtkVector.h:336
const T & GetY() const
Definition: vtkVector.h:180
T SquaredNorm() const
Definition: vtkVector.h:65
vtkVector()
Definition: vtkVector.h:42
vtkVector3f(float x, float y, float z)
Definition: vtkVector.h:334