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 
35 #include <cmath> // For math functions
36 
37 template<typename T, int Size>
38 class vtkVector : public vtkTuple<T, Size>
39 {
40 public:
42  {
43  }
44 
46 
47  explicit vtkVector(const T& scalar) : vtkTuple<T, Size>(scalar)
48  {
49  }
51 
53 
57  explicit vtkVector(const T* init) : vtkTuple<T, Size>(init)
58  {
59  }
61 
63 
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 
76 
77  double Norm() const
78  {
79  return sqrt(static_cast<double>(this->SquaredNorm()));
80  }
82 
84 
85  double Normalize()
86  {
87  const double norm(this->Norm());
88  const double inv(1.0 / norm);
89  for (int i = 0; i < Size; ++i)
90  {
91  this->Data[i] = static_cast<T>(this->Data[i] * inv);
92  }
93  return norm;
94  }
96 
98 
101  {
102  vtkVector<T, Size> temp(*this);
103  temp.Normalize();
104  return temp;
105  }
107 
109 
110  T Dot(const vtkVector<T, Size>& other) const
111  {
112  T result(0);
113  for (int i = 0; i < Size; ++i)
114  {
115  result += this->Data[i] * other[i];
116  }
117  return result;
118  }
120 
122 
123  template<typename TR>
125  {
126  vtkVector<TR, Size> result;
127  for (int i = 0; i < Size; ++i)
128  {
129  result[i] = static_cast<TR>(this->Data[i]);
130  }
131  return result;
132  }
133 };
135 
136 // .NAME vtkVector2 - templated base type for storage of 2D vectors.
137 //
138 template<typename T>
139 class vtkVector2 : public vtkVector<T, 2>
140 {
141 public:
143  {
144  }
145 
146  explicit vtkVector2(const T& scalar) : vtkVector<T, 2>(scalar)
147  {
148  }
149 
150  explicit vtkVector2(const T* init) : vtkVector<T, 2>(init)
151  {
152  }
153 
154  vtkVector2(const T& x, const T& y)
155  {
156  this->Data[0] = x;
157  this->Data[1] = y;
158  }
159 
161 
162  void Set(const T& x, const T& y)
163  {
164  this->Data[0] = x;
165  this->Data[1] = y;
166  }
168 
170  void SetX(const T& x) { this->Data[0] = x; }
171 
173 
174  const T& GetX() const { return this->Data[0]; }
175  const T& X() const { return this->Data[0]; }
177 
179  void SetY(const T& y) { this->Data[1] = y; }
180 
182 
183  const T& GetY() const { return this->Data[1]; }
184  const T& Y() const { return this->Data[1]; }
185 };
187 
188 // .NAME vtkVector3 - templated base type for storage of 3D vectors.
189 //
190 template<typename T>
191 class vtkVector3 : public vtkVector<T, 3>
192 {
193 public:
195  {
196  }
197 
198  explicit vtkVector3(const T& scalar) : vtkVector<T, 3>(scalar)
199  {
200  }
201 
202  explicit vtkVector3(const T* init) : vtkVector<T, 3>(init)
203  {
204  }
205 
206  vtkVector3(const T& x, const T& y, const T& z)
207  {
208  this->Data[0] = x;
209  this->Data[1] = y;
210  this->Data[2] = z;
211  }
212 
214 
215  void Set(const T& x, const T& y, const T& z)
216  {
217  this->Data[0] = x;
218  this->Data[1] = y;
219  this->Data[2] = z;
220  }
222 
224  void SetX(const T& x) { this->Data[0] = x; }
225 
227 
228  const T& GetX() const { return this->Data[0]; }
229  const T& X() const { return this->Data[0]; }
231 
233  void SetY(const T& y) { this->Data[1] = y; }
234 
236 
237  const T& GetY() const { return this->Data[1]; }
238  const T& Y() const { return this->Data[1]; }
240 
242  void SetZ(const T& z) { this->Data[2] = z; }
243 
245 
246  const T& GetZ() const { return this->Data[2]; }
247  const T& Z() const { return this->Data[2]; }
249 
251 
252  vtkVector3<T> Cross(const vtkVector3& other) const
253  {
254  vtkVector3<T> res;
255  res[0] = this->Data[1] * other.Data[2] - this->Data[2] * other.Data[1];
256  res[1] = this->Data[2] * other.Data[0] - this->Data[0] * other.Data[2];
257  res[2] = this->Data[0] * other.Data[1] - this->Data[1] * other.Data[0];
258  return res;
259  }
261 
262 };
263 
265 
266 #define vtkVectorNormalized(vectorType, type, size) \
267 vectorType Normalized() \
268 { \
269  return vectorType(vtkVector<type, size>::Normalized().GetData()); \
270 } \
271 
272 
273 #define vtkVectorDerivedMacro(vectorType, type, size) \
274 vtkVectorNormalized(vectorType, type, size) \
275 
276 
277 
278 class vtkVector2i : public vtkVector2<int>
279 {
280 public:
282  vtkVector2i(int x, int y) : vtkVector2<int>(x, y) {}
283  explicit vtkVector2i(int scalar) : vtkVector2<int>(scalar) {}
284  explicit vtkVector2i(const int *init) : vtkVector2<int>(init) {}
286 };
288 
289 class vtkVector2f : public vtkVector2<float>
290 {
291 public:
293  vtkVector2f(float x, float y) : vtkVector2<float>(x, y) {}
294  explicit vtkVector2f(float scalar) : vtkVector2<float>(scalar) {}
295  explicit vtkVector2f(const float* i) : vtkVector2<float>(i) {}
297 };
298 
299 class vtkVector2d : public vtkVector2<double>
300 {
301 public:
303  vtkVector2d(double x, double y) : vtkVector2<double>(x, y) {}
304  explicit vtkVector2d(double scalar) : vtkVector2<double>(scalar) {}
305  explicit vtkVector2d(const double *init) : vtkVector2<double>(init) {}
307 };
308 
309 #define vtkVector3Cross(vectorType, type) \
310 vectorType Cross(const vectorType& other) \
311 { \
312  return vectorType(vtkVector3<type>::Cross(other).GetData()); \
313 } \
314 
315 class vtkVector3i : public vtkVector3<int>
316 {
317 public:
319  vtkVector3i(int x, int y, int z) : vtkVector3<int>(x, y, z) {}
320  explicit vtkVector3i(int scalar) : vtkVector3<int>(scalar) {}
321  explicit vtkVector3i(const int *init) : vtkVector3<int>(init) {}
324 };
325 
326 class vtkVector3f : public vtkVector3<float>
327 {
328 public:
330  vtkVector3f(float x, float y, float z) : vtkVector3<float>(x, y, z) {}
331  explicit vtkVector3f(float scalar) : vtkVector3<float>(scalar) {}
332  explicit vtkVector3f(const float *init) : vtkVector3<float>(init) {}
335 };
336 
337 class vtkVector3d : public vtkVector3<double>
338 {
339 public:
341  vtkVector3d(double x, double y, double z) : vtkVector3<double>(x, y, z) {}
342  explicit vtkVector3d(double scalar) : vtkVector3<double>(scalar) {}
343  explicit vtkVector3d(const double *init) : vtkVector3<double>(init) {}
346 };
347 
348 #endif // __vtkVector_h