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 explicit vectorType(type s) : Superclass(s) {} \
263 explicit vectorType(const type *i) : Superclass(i) {} \
264 explicit vectorType(const vtkTuple<type, size> &o) : Superclass(o.GetData()) {} \
265 vectorType(const vtkVector<type, size> &o) : Superclass(o.GetData()) {} \
266 
267 
268 
269 class vtkVector2i : public vtkVector2<int>
270 {
271 public:
274  vtkVector2i(int x, int y) : vtkVector2<int>(x, y) {}
276 };
278 
279 class vtkVector2f : public vtkVector2<float>
280 {
281 public:
284  vtkVector2f(float x, float y) : vtkVector2<float>(x, y) {}
286 };
287 
288 class vtkVector2d : public vtkVector2<double>
289 {
290 public:
293  vtkVector2d(double x, double y) : vtkVector2<double>(x, y) {}
295 };
296 
297 #define vtkVector3Cross(vectorType, type) \
298 vectorType Cross(const vectorType& other) const \
299 { \
300  return vectorType(vtkVector3<type>::Cross(other).GetData()); \
301 } \
302 
303 class vtkVector3i : public vtkVector3<int>
304 {
305 public:
308  vtkVector3i(int x, int y, int z) : vtkVector3<int>(x, y, z) {}
311 };
312 
313 class vtkVector3f : public vtkVector3<float>
314 {
315 public:
318  vtkVector3f(float x, float y, float z) : vtkVector3<float>(x, y, z) {}
321 };
322 
323 class vtkVector3d : public vtkVector3<double>
324 {
325 public:
328  vtkVector3d(double x, double y, double z) : vtkVector3<double>(x, y, z) {}
331 };
332 
333 #endif // vtkVector_h
334 // VTK-HeaderTest-Exclude: vtkVector.h
T Data[Size]
Definition: vtkTuple.h:136
#define vtkVector3Cross(vectorType, type)
Definition: vtkVector.h:297
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
vtkVector3< float > Superclass
Definition: vtkVector.h:316
templated base type for storage of vectors.
Definition: vtkVector.h:39
vtkVector3< int > Superclass
Definition: vtkVector.h:306
const T & GetX() const
Definition: vtkVector.h:224
vtkVector2i(int x, int y)
Definition: vtkVector.h:274
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
vtkVector2< int > Superclass
Definition: vtkVector.h:272
const T & GetY() const
Definition: vtkVector.h:230
vtkVector3(const T &scalar)
Definition: vtkVector.h:195
vtkVector3< double > Superclass
Definition: vtkVector.h:326
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:284
vtkVector2< double > Superclass
Definition: vtkVector.h:291
vtkVector(const T &scalar)
Definition: vtkVector.h:48
vtkVector2d(double x, double y)
Definition: vtkVector.h:293
templated base type for containers of constant size.
Definition: vtkTuple.h:34
vtkVector3i(int x, int y, int z)
Definition: vtkVector.h:308
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
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
vtkVector2< float > Superclass
Definition: vtkVector.h:282
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
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
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:318