VTK
dox/Common/vtkVector.h
Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    vtkVector.h
00005 
00006   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
00007   All rights reserved.
00008   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
00009 
00010      This software is distributed WITHOUT ANY WARRANTY; without even
00011      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00012      PURPOSE.  See the above copyright notice for more information.
00013 
00014 =========================================================================*/
00015 
00030 #ifndef __vtkVector_h
00031 #define __vtkVector_h
00032 
00033 #include "vtkTuple.h"
00034 
00035 #include <cmath>   // For math functions
00036 
00037 template<typename T, int Size>
00038 class vtkVector : public vtkTuple<T, Size>
00039 {
00040 public:
00041   vtkVector()
00042   {
00043   }
00044 
00046 
00047   explicit vtkVector(const T& scalar) : vtkTuple<T, Size>(scalar)
00048   {
00049   }
00051 
00053 
00057   explicit vtkVector(const T* init) : vtkTuple<T, Size>(init)
00058   {
00059   }
00061 
00063 
00064   T SquaredNorm() const
00065   {
00066     T result = 0;
00067     for (int i = 0; i < Size; ++i)
00068       {
00069       result += this->Data[i] * this->Data[i];
00070       }
00071     return result;
00072   }
00074 
00076 
00077   double Norm() const
00078   {
00079     return sqrt(static_cast<double>(this->SquaredNorm()));
00080   }
00082 
00084 
00085   double Normalize()
00086   {
00087     const double norm(this->Norm());
00088     const double inv(1.0 / norm);
00089     for (int i = 0; i < Size; ++i)
00090       {
00091       this->Data[i] = static_cast<T>(this->Data[i] * inv);
00092       }
00093     return norm;
00094   }
00096 
00098 
00100   vtkVector<T, Size> Normalized() const
00101   {
00102     vtkVector<T, Size> temp(*this);
00103     temp.Normalize();
00104     return temp;
00105   }
00107 
00109 
00110   T Dot(const vtkVector<T, Size>& other) const
00111   {
00112     T result(0);
00113     for (int i = 0; i < Size; ++i)
00114       {
00115       result += this->Data[i] * other[i];
00116       }
00117     return result;
00118   }
00120 
00122 
00123   template<typename TR>
00124   vtkVector<TR, Size> Cast() const
00125   {
00126     vtkVector<TR, Size> result;
00127     for (int i = 0; i < Size; ++i)
00128       {
00129       result[i] = static_cast<TR>(this->Data[i]);
00130       }
00131     return result;
00132   }
00133 };
00135 
00136 // .NAME vtkVector2 - templated base type for storage of 2D vectors.
00137 //
00138 template<typename T>
00139 class vtkVector2 : public vtkVector<T, 2>
00140 {
00141 public:
00142   vtkVector2()
00143   {
00144   }
00145 
00146   explicit vtkVector2(const T& scalar) : vtkVector<T, 2>(scalar)
00147   {
00148   }
00149 
00150   explicit vtkVector2(const T* init) : vtkVector<T, 2>(init)
00151   {
00152   }
00153 
00154   vtkVector2(const T& x, const T& y)
00155   {
00156     this->Data[0] = x;
00157     this->Data[1] = y;
00158   }
00159 
00161 
00162   void Set(const T& x, const T& y)
00163   {
00164     this->Data[0] = x;
00165     this->Data[1] = y;
00166   }
00168 
00170   void SetX(const T& x) { this->Data[0] = x; }
00171 
00173 
00174   const T& GetX() const { return this->Data[0]; }
00175   const T& X() const { return this->Data[0]; }
00177 
00179   void SetY(const T& y) { this->Data[1] = y; }
00180 
00182 
00183   const T& GetY() const { return this->Data[1]; }
00184   const T& Y() const { return this->Data[1]; }
00185 };
00187 
00188 // .NAME vtkVector3 - templated base type for storage of 3D vectors.
00189 //
00190 template<typename T>
00191 class vtkVector3 : public vtkVector<T, 3>
00192 {
00193 public:
00194   vtkVector3()
00195   {
00196   }
00197 
00198   explicit vtkVector3(const T& scalar) : vtkVector<T, 3>(scalar)
00199   {
00200   }
00201 
00202   explicit vtkVector3(const T* init) : vtkVector<T, 3>(init)
00203   {
00204   }
00205 
00206   vtkVector3(const T& x, const T& y, const T& z)
00207   {
00208     this->Data[0] = x;
00209     this->Data[1] = y;
00210     this->Data[2] = z;
00211   }
00212 
00214 
00215   void Set(const T& x, const T& y, const T& z)
00216   {
00217     this->Data[0] = x;
00218     this->Data[1] = y;
00219     this->Data[2] = z;
00220   }
00222 
00224   void SetX(const T& x) { this->Data[0] = x; }
00225 
00227 
00228   const T& GetX() const { return this->Data[0]; }
00229   const T& X() const { return this->Data[0]; }
00231 
00233   void SetY(const T& y) { this->Data[1] = y; }
00234 
00236 
00237   const T& GetY() const { return this->Data[1]; }
00238   const T& Y() const { return this->Data[1]; }
00240 
00242   void SetZ(const T& z) { this->Data[2] = z; }
00243 
00245 
00246   const T& GetZ() const { return this->Data[2]; }
00247   const T& Z() const { return this->Data[2]; }
00249 
00251 
00252   vtkVector3<T> Cross(const vtkVector3& other) const
00253   {
00254     vtkVector3<T> res;
00255     res[0] = this->Data[1] * other.Data[2] - this->Data[2] * other.Data[1];
00256     res[1] = this->Data[2] * other.Data[0] - this->Data[0] * other.Data[2];
00257     res[2] = this->Data[0] * other.Data[1] - this->Data[1] * other.Data[0];
00258     return res;
00259   }
00261 
00262 };
00263 
00265 
00266 #define vtkVectorNormalized(vectorType, type, size) \
00267 vectorType Normalized() \
00268 { \
00269   return vectorType(vtkVector<type, size>::Normalized().GetData()); \
00270 } \
00271 
00272 
00273 #define vtkVectorDerivedMacro(vectorType, type, size) \
00274 vtkVectorNormalized(vectorType, type, size) \
00275 
00276 
00277 
00278 class vtkVector2i : public vtkVector2<int>
00279 {
00280 public:
00281   vtkVector2i() {}
00282   vtkVector2i(int x, int y) : vtkVector2<int>(x, y) {}
00283   explicit vtkVector2i(int scalar) : vtkVector2<int>(scalar) {}
00284   explicit vtkVector2i(const int *init) : vtkVector2<int>(init) {}
00285   vtkVectorDerivedMacro(vtkVector2i, int, 2)
00286 };
00288 
00289 class vtkVector2f : public vtkVector2<float>
00290 {
00291 public:
00292   vtkVector2f() {}
00293   vtkVector2f(float x, float y) : vtkVector2<float>(x, y) {}
00294   explicit vtkVector2f(float scalar) : vtkVector2<float>(scalar) {}
00295   explicit vtkVector2f(const float* i) : vtkVector2<float>(i) {}
00296   vtkVectorDerivedMacro(vtkVector2f, float, 2)
00297 };
00298 
00299 class vtkVector2d : public vtkVector2<double>
00300 {
00301 public:
00302   vtkVector2d() {}
00303   vtkVector2d(double x, double y) : vtkVector2<double>(x, y) {}
00304   explicit vtkVector2d(double scalar) : vtkVector2<double>(scalar) {}
00305   explicit vtkVector2d(const double *init) : vtkVector2<double>(init) {}
00306   vtkVectorDerivedMacro(vtkVector2d, double, 2)
00307 };
00308 
00309 #define vtkVector3Cross(vectorType, type) \
00310 vectorType Cross(const vectorType& other) \
00311 { \
00312   return vectorType(vtkVector3<type>::Cross(other).GetData()); \
00313 } \
00314 
00315 class vtkVector3i : public vtkVector3<int>
00316 {
00317 public:
00318   vtkVector3i() {}
00319   vtkVector3i(int x, int y, int z) : vtkVector3<int>(x, y, z) {}
00320   explicit vtkVector3i(int scalar) : vtkVector3<int>(scalar) {}
00321   explicit vtkVector3i(const int *init) : vtkVector3<int>(init) {}
00322   vtkVectorDerivedMacro(vtkVector3i, int, 3)
00323   vtkVector3Cross(vtkVector3i, int)
00324 };
00325 
00326 class vtkVector3f : public vtkVector3<float>
00327 {
00328 public:
00329   vtkVector3f() {}
00330   vtkVector3f(float x, float y, float z) : vtkVector3<float>(x, y, z) {}
00331   explicit vtkVector3f(float scalar) : vtkVector3<float>(scalar) {}
00332   explicit vtkVector3f(const float *init) : vtkVector3<float>(init) {}
00333   vtkVectorDerivedMacro(vtkVector3f, float, 3)
00334   vtkVector3Cross(vtkVector3f, float)
00335 };
00336 
00337 class vtkVector3d : public vtkVector3<double>
00338 {
00339 public:
00340   vtkVector3d() {}
00341   vtkVector3d(double x, double y, double z) : vtkVector3<double>(x, y, z) {}
00342   explicit vtkVector3d(double scalar) : vtkVector3<double>(scalar) {}
00343   explicit vtkVector3d(const double *init) : vtkVector3<double>(init) {}
00344   vtkVectorDerivedMacro(vtkVector3d, double, 3)
00345   vtkVector3Cross(vtkVector3d, double)
00346 };
00347 
00348 #endif // __vtkVector_h