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 
00045   vtkVector(const T* init) : vtkTuple<T, Size>(init)
00046   {
00047   }
00048 
00050 
00051   T SquaredNorm() const
00052   {
00053     T result = 0;
00054     for (int i = 0; i < Size; ++i)
00055       {
00056       result += this->Data[i] * this->Data[i];
00057       }
00058     return result;
00059   }
00061 
00063 
00064   double Norm() const
00065   {
00066     return sqrt(static_cast<double>(this->SquaredNorm()));
00067   }
00069 
00071 
00072   double Normalize()
00073   {
00074     const double norm(this->Norm());
00075     const double inv(1.0 / norm);
00076     for (int i = 0; i < Size; ++i)
00077       {
00078       this->Data[i] = static_cast<T>(this->Data[i] * inv);
00079       }
00080     return norm;
00081   }
00083 
00085 
00087   vtkVector<T, Size> Normalized() const
00088   {
00089     vtkVector<T, Size> temp(*this);
00090     temp.Normalize();
00091     return temp;
00092   }
00094 
00096 
00097   T Dot(const vtkVector<T, Size>& other) const
00098   {
00099     T result(0);
00100     for (int i = 0; i < Size; ++i)
00101       {
00102       result += this->Data[i] * other[i];
00103       }
00104     return result;
00105   }
00107 
00109 
00110   template<typename TR>
00111   vtkVector<TR, Size> Cast() const
00112   {
00113     vtkVector<TR, Size> result;
00114     for (int i = 0; i < Size; ++i)
00115       {
00116       result[i] = static_cast<TR>(this->Data[i]);
00117       }
00118     return result;
00119   }
00120 };
00122 
00123 // .NAME vtkVector2 - templated base type for storage of 2D vectors.
00124 //
00125 template<typename T>
00126 class vtkVector2 : public vtkVector<T, 2>
00127 {
00128 public:
00129   vtkVector2(const T& x = 0, const T& y = 0)
00130   {
00131     this->Data[0] = x;
00132     this->Data[1] = y;
00133   }
00134 
00135   explicit vtkVector2(const T* init) : vtkVector<T, 2>(init)
00136   {
00137   }
00138 
00140 
00141   void Set(const T& x, const T& y)
00142   {
00143     this->Data[0] = x;
00144     this->Data[1] = y;
00145   }
00147 
00149   void SetX(const T& x) { this->Data[0] = x; }
00150 
00152 
00153   const T& GetX() const { return this->Data[0]; }
00154   const T& X() const { return this->Data[0]; }
00156 
00158   void SetY(const T& y) { this->Data[1] = y; }
00159 
00161 
00162   const T& GetY() const { return this->Data[1]; }
00163   const T& Y() const { return this->Data[1]; }
00164 };
00166 
00167 // .NAME vtkVector3 - templated base type for storage of 3D vectors.
00168 //
00169 template<typename T>
00170 class vtkVector3 : public vtkVector<T, 3>
00171 {
00172 public:
00173   vtkVector3(const T& x = 0, const T& y = 0, const T& z = 0)
00174   {
00175     this->Data[0] = x;
00176     this->Data[1] = y;
00177     this->Data[2] = z;
00178   }
00179 
00180   explicit vtkVector3(const T* init) : vtkVector<T, 3>(init) { }
00181 
00183 
00184   void Set(const T& x, const T& y, const T& z)
00185   {
00186     this->Data[0] = x;
00187     this->Data[1] = y;
00188     this->Data[2] = z;
00189   }
00191 
00193   void SetX(const T& x) { this->Data[0] = x; }
00194 
00196 
00197   const T& GetX() const { return this->Data[0]; }
00198   const T& X() const { return this->Data[0]; }
00200 
00202   void SetY(const T& y) { this->Data[1] = y; }
00203 
00205 
00206   const T& GetY() const { return this->Data[1]; }
00207   const T& Y() const { return this->Data[1]; }
00209 
00211   void SetZ(const T& z) { this->Data[2] = z; }
00212 
00214 
00215   const T& GetZ() const { return this->Data[2]; }
00216   const T& Z() const { return this->Data[2]; }
00218 
00220 
00221   vtkVector3<T> Cross(const vtkVector3& other) const
00222   {
00223     vtkVector3<T> res;
00224     res[0] = this->Data[1] * other.Data[2] - this->Data[2] * other.Data[1];
00225     res[1] = this->Data[2] * other.Data[0] - this->Data[0] * other.Data[2];
00226     res[2] = this->Data[0] * other.Data[1] - this->Data[1] * other.Data[0];
00227     return res;
00228   }
00230 
00231 };
00232 
00234 
00235 #define vtkVectorNormalized(vectorType, type, size) \
00236 vectorType Normalized() \
00237 { \
00238   return vectorType(vtkVector<type, size>::Normalized().GetData()); \
00239 } \
00240 
00241 
00242 #define vtkVectorDerivedMacro(vectorType, type, size) \
00243 vtkVectorNormalized(vectorType, type, size) \
00244 
00245 
00246 
00247 class vtkVector2i : public vtkVector2<int>
00248 {
00249 public:
00250   vtkVector2i(int x = 0, int y = 0) : vtkVector2<int>(x, y) {}
00251   explicit vtkVector2i(const int *init) : vtkVector2<int>(init) {}
00252   vtkVectorDerivedMacro(vtkVector2i, int, 2)
00253 };
00255 
00256 class vtkVector2f : public vtkVector2<float>
00257 {
00258 public:
00259   vtkVector2f(float x = 0.0, float y = 0.0) : vtkVector2<float>(x, y) {}
00260   vtkVector2f(const float* i) : vtkVector2<float>(i) {}
00261   vtkVectorDerivedMacro(vtkVector2f, float, 2)
00262 };
00263 
00264 class vtkVector2d : public vtkVector2<double>
00265 {
00266 public:
00267   vtkVector2d(double x = 0.0, double y = 0.0) : vtkVector2<double>(x, y) {}
00268   explicit vtkVector2d(const double *init) : vtkVector2<double>(init) {}
00269   vtkVectorDerivedMacro(vtkVector2d, double, 2)
00270 };
00271 
00272 #define vtkVector3Cross(vectorType, type) \
00273 vectorType Cross(const vectorType& other) \
00274 { \
00275   return vectorType(vtkVector3<type>::Cross(other).GetData()); \
00276 } \
00277 
00278 class vtkVector3i : public vtkVector3<int>
00279 {
00280 public:
00281   vtkVector3i(int x = 0, int y = 0, int z = 0) : vtkVector3<int>(x, y, z) {}
00282   explicit vtkVector3i(const int *init) : vtkVector3<int>(init) {}
00283   vtkVectorDerivedMacro(vtkVector3i, int, 3)
00284   vtkVector3Cross(vtkVector3i, int)
00285 };
00286 
00287 class vtkVector3f : public vtkVector3<float>
00288 {
00289 public:
00290   vtkVector3f(float x = 0.0, float y = 0.0, float z = 0.0)
00291     : vtkVector3<float>(x, y, z) {}
00292   explicit vtkVector3f(const float *init) : vtkVector3<float>(init) {}
00293   vtkVectorDerivedMacro(vtkVector3f, float, 3)
00294   vtkVector3Cross(vtkVector3f, float)
00295 };
00296 
00297 class vtkVector3d : public vtkVector3<double>
00298 {
00299 public:
00300   vtkVector3d(double x = 0.0, double y = 0.0, double z = 0.0)
00301     : vtkVector3<double>(x, y, z) {}
00302   explicit vtkVector3d(const double *init) : vtkVector3<double>(init) {}
00303   vtkVectorDerivedMacro(vtkVector3d, double, 3)
00304   vtkVector3Cross(vtkVector3d, double)
00305 };
00306 
00307 #endif // __vtkVector_h