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 template<typename T, int Size>
00034 class vtkVector
00035 {
00036 public:
00037   vtkVector()
00038   {
00039     for (int i = 0; i < Size; ++i)
00040       {
00041       Data[i] = 0;
00042       }
00043   }
00044 
00045   explicit vtkVector(const T* init)
00046   {
00047     for (int i = 0; i < Size; ++i)
00048       {
00049       Data[i] = init[i];
00050       }
00051   }
00052 
00054   int GetSize() const { return Size; }
00055 
00057 
00058   T* GetData() { return this->Data; }
00059   const T* GetData() const { return this->Data; }
00061 
00063 
00065   T& operator[](int i) { return this->Data[i]; }
00066   const T& operator[](int i) const { return this->Data[i]; }
00068 
00071   T operator()(int i) const { return this->Data[i]; }
00072 
00074 
00075   template<typename TR>
00076   vtkVector<TR, Size> Cast() const
00077   {
00078     vtkVector<TR, Size> result;
00079     for (int i = 0; i < Size; ++i)
00080       {
00081       result[i] = static_cast<TR>(Data[i]);
00082       }
00083     return result;
00084   }
00086 
00087 protected:
00089 
00090   T Data[Size];
00091 };
00093 
00094 // .NAME vtkVector2 - templated base type for storage of 2D vectors.
00095 //
00096 template<typename T>
00097 class vtkVector2 : public vtkVector<T, 2>
00098 {
00099 public:
00100   vtkVector2(const T& x = 0, const T& y = 0)
00101   {
00102     this->Data[0] = x;
00103     this->Data[1] = y;
00104   }
00105 
00106   explicit vtkVector2(const T* init) : vtkVector<T, 2>(init)
00107   {
00108   }
00109 
00111 
00112   void Set(const T& x, const T& y)
00113   {
00114     this->Data[0] = x;
00115     this->Data[1] = y;
00116   }
00118 
00120   void SetX(const T& x) { this->Data[0] = x; }
00121 
00123 
00124   const T& GetX() const { return this->Data[0]; }
00125   const T& X() const { return this->Data[0]; }
00127 
00129   void SetY(const T& y) { this->Data[1] = y; }
00130 
00132 
00133   const T& GetY() const { return this->Data[1]; }
00134   const T& Y() const { return this->Data[1]; }
00135 };
00137 
00138 // .NAME vtkVector3 - templated base type for storage of 3D vectors.
00139 //
00140 template<typename T>
00141 class vtkVector3 : public vtkVector<T, 3>
00142 {
00143 public:
00144   vtkVector3(const T& x = 0, const T& y = 0, const T& z = 0)
00145   {
00146     this->Data[0] = x;
00147     this->Data[1] = y;
00148     this->Data[2] = z;
00149   }
00150 
00151   explicit vtkVector3(const T* init) : vtkVector<T, 3>(init) { }
00152 
00154 
00155   void Set(const T& x, const T& y, const T& z)
00156   {
00157     this->Data[0] = x;
00158     this->Data[1] = y;
00159     this->Data[2] = z;
00160   }
00162 
00164   void SetX(const T& x) { this->Data[0] = x; }
00165 
00167 
00168   const T& GetX() const { return this->Data[0]; }
00169   const T& X() const { return this->Data[0]; }
00171 
00173   void SetY(const T& y) { this->Data[1] = y; }
00174 
00176 
00177   const T& GetY() const { return this->Data[1]; }
00178   const T& Y() const { return this->Data[1]; }
00180 
00182   void SetZ(const T& z) { this->Data[2] = z; }
00183 
00185 
00186   const T& GetZ() const { return this->Data[2]; }
00187   const T& Z() const { return this->Data[2]; }
00189 
00190 };
00191 
00193 
00194 class vtkVector2i : public vtkVector2<int>
00195 {
00196 public:
00197   vtkVector2i(int x = 0, int y = 0) : vtkVector2<int>(x, y) {}
00198   explicit vtkVector2i(const int *init) : vtkVector2<int>(init) {}
00199 };
00201 
00202 class vtkVector2f : public vtkVector2<float>
00203 {
00204 public:
00205   vtkVector2f(float x = 0.0, float y = 0.0) : vtkVector2<float>(x, y) {}
00206   vtkVector2f(const float* i) : vtkVector2<float>(i) {}
00207 };
00208 
00209 class vtkVector2d : public vtkVector2<double>
00210 {
00211 public:
00212   vtkVector2d(double x = 0.0, double y = 0.0) : vtkVector2<double>(x, y) {}
00213   explicit vtkVector2d(const double *init) : vtkVector2<double>(init) {}
00214 };
00215 
00216 class vtkVector3i : public vtkVector3<int>
00217 {
00218 public:
00219   vtkVector3i(int x = 0, int y = 0, int z = 0) : vtkVector3<int>(x, y, z) {}
00220   explicit vtkVector3i(const int *init) : vtkVector3<int>(init) {}
00221 };
00222 
00223 class vtkVector3f : public vtkVector3<float>
00224 {
00225 public:
00226   vtkVector3f(float x = 0.0, float y = 0.0, float z = 0.0)
00227     : vtkVector3<float>(x, y, z) {}
00228   explicit vtkVector3f(const float *init) : vtkVector3<float>(init) {}
00229 };
00230 
00231 class vtkVector3d : public vtkVector3<double>
00232 {
00233 public:
00234   vtkVector3d(double x = 0.0, double y = 0.0, double z = 0.0)
00235     : vtkVector3<double>(x, y, z) {}
00236   explicit vtkVector3d(const double *init) : vtkVector3<double>(init) {}
00237 };
00238 
00239 // Some operators for easy addition etc
00240 inline const vtkVector2f operator+(const vtkVector2f &lhs, const vtkVector2f &rhs)
00241 {
00242   return vtkVector2f(lhs[0] + rhs[0], lhs[1] + rhs[1]);
00243 }
00244 inline const vtkVector2f operator-(const vtkVector2f &lhs, const vtkVector2f &rhs)
00245 {
00246   return vtkVector2f(lhs[0] - rhs[0], lhs[1] - rhs[1]);
00247 }
00248 
00249 #endif // __vtkVector_h