VTK
|
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 #include "vtkObject.h" // for legacy macros 00035 00036 #include <cmath> // For math functions 00037 00038 template<typename T, int Size> 00039 class vtkVector : public vtkTuple<T, Size> 00040 { 00041 public: 00042 vtkVector() 00043 { 00044 } 00045 00047 00048 explicit vtkVector(const T& scalar) : vtkTuple<T, Size>(scalar) 00049 { 00050 } 00052 00054 00058 explicit vtkVector(const T* init) : vtkTuple<T, Size>(init) 00059 { 00060 } 00062 00064 00065 T SquaredNorm() const 00066 { 00067 T result = 0; 00068 for (int i = 0; i < Size; ++i) 00069 { 00070 result += this->Data[i] * this->Data[i]; 00071 } 00072 return result; 00073 } 00075 00077 00078 double Norm() const 00079 { 00080 return sqrt(static_cast<double>(this->SquaredNorm())); 00081 } 00083 00085 00086 double Normalize() 00087 { 00088 const double norm(this->Norm()); 00089 const double inv(1.0 / norm); 00090 for (int i = 0; i < Size; ++i) 00091 { 00092 this->Data[i] = static_cast<T>(this->Data[i] * inv); 00093 } 00094 return norm; 00095 } 00097 00099 00101 vtkVector<T, Size> Normalized() const 00102 { 00103 vtkVector<T, Size> temp(*this); 00104 temp.Normalize(); 00105 return temp; 00106 } 00108 00110 00111 T Dot(const vtkVector<T, Size>& other) const 00112 { 00113 T result(0); 00114 for (int i = 0; i < Size; ++i) 00115 { 00116 result += this->Data[i] * other[i]; 00117 } 00118 return result; 00119 } 00121 00123 00124 template<typename TR> 00125 vtkVector<TR, Size> Cast() const 00126 { 00127 vtkVector<TR, Size> result; 00128 for (int i = 0; i < Size; ++i) 00129 { 00130 result[i] = static_cast<TR>(this->Data[i]); 00131 } 00132 return result; 00133 } 00134 }; 00136 00137 // .NAME vtkVector2 - templated base type for storage of 2D vectors. 00138 // 00139 template<typename T> 00140 class vtkVector2 : public vtkVector<T, 2> 00141 { 00142 public: 00143 vtkVector2() 00144 { 00145 } 00146 00147 explicit vtkVector2(const T& scalar) : vtkVector<T, 2>(scalar) 00148 { 00149 } 00150 00151 explicit vtkVector2(const T* init) : vtkVector<T, 2>(init) 00152 { 00153 } 00154 00155 vtkVector2(const T& x, const T& y) 00156 { 00157 this->Data[0] = x; 00158 this->Data[1] = y; 00159 } 00160 00162 00163 void Set(const T& x, const T& y) 00164 { 00165 this->Data[0] = x; 00166 this->Data[1] = y; 00167 } 00169 00171 void SetX(const T& x) { this->Data[0] = x; } 00172 00174 const T& GetX() const { return this->Data[0]; } 00175 00177 void SetY(const T& y) { this->Data[1] = y; } 00178 00180 const T& GetY() const { return this->Data[1]; } 00181 00183 VTK_LEGACY(const T& X() const); 00184 00186 00187 VTK_LEGACY(const T& Y() const); 00188 }; 00190 00191 // .NAME vtkVector3 - templated base type for storage of 3D vectors. 00192 // 00193 template<typename T> 00194 class vtkVector3 : public vtkVector<T, 3> 00195 { 00196 public: 00197 vtkVector3() 00198 { 00199 } 00200 00201 explicit vtkVector3(const T& scalar) : vtkVector<T, 3>(scalar) 00202 { 00203 } 00204 00205 explicit vtkVector3(const T* init) : vtkVector<T, 3>(init) 00206 { 00207 } 00208 00209 vtkVector3(const T& x, const T& y, const T& z) 00210 { 00211 this->Data[0] = x; 00212 this->Data[1] = y; 00213 this->Data[2] = z; 00214 } 00215 00217 00218 void Set(const T& x, const T& y, const T& z) 00219 { 00220 this->Data[0] = x; 00221 this->Data[1] = y; 00222 this->Data[2] = z; 00223 } 00225 00227 void SetX(const T& x) { this->Data[0] = x; } 00228 00230 const T& GetX() const { return this->Data[0]; } 00231 00233 void SetY(const T& y) { this->Data[1] = y; } 00234 00236 const T& GetY() const { return this->Data[1]; } 00237 00239 void SetZ(const T& z) { this->Data[2] = z; } 00240 00242 const T& GetZ() const { return this->Data[2]; } 00243 00245 00246 vtkVector3<T> Cross(const vtkVector3<T>& other) const 00247 { 00248 vtkVector3<T> res; 00249 res[0] = this->Data[1] * other.Data[2] - this->Data[2] * other.Data[1]; 00250 res[1] = this->Data[2] * other.Data[0] - this->Data[0] * other.Data[2]; 00251 res[2] = this->Data[0] * other.Data[1] - this->Data[1] * other.Data[0]; 00252 return res; 00253 } 00255 00257 VTK_LEGACY(const T& X() const); 00258 00260 VTK_LEGACY(const T& Y() const); 00261 00263 00264 VTK_LEGACY(const T& Z() const); 00265 }; 00267 00269 00270 #define vtkVectorNormalized(vectorType, type, size) \ 00271 vectorType Normalized() \ 00272 { \ 00273 return vectorType(vtkVector<type, size>::Normalized().GetData()); \ 00274 } \ 00275 00276 00277 #define vtkVectorDerivedMacro(vectorType, type, size) \ 00278 vtkVectorNormalized(vectorType, type, size) \ 00279 00280 00281 00282 class vtkVector2i : public vtkVector2<int> 00283 { 00284 public: 00285 vtkVector2i() {} 00286 vtkVector2i(int x, int y) : vtkVector2<int>(x, y) {} 00287 explicit vtkVector2i(int scalar) : vtkVector2<int>(scalar) {} 00288 explicit vtkVector2i(const int *init) : vtkVector2<int>(init) {} 00289 vtkVectorDerivedMacro(vtkVector2i, int, 2) 00290 }; 00292 00293 class vtkVector2f : public vtkVector2<float> 00294 { 00295 public: 00296 vtkVector2f() {} 00297 vtkVector2f(float x, float y) : vtkVector2<float>(x, y) {} 00298 explicit vtkVector2f(float scalar) : vtkVector2<float>(scalar) {} 00299 explicit vtkVector2f(const float* i) : vtkVector2<float>(i) {} 00300 vtkVectorDerivedMacro(vtkVector2f, float, 2) 00301 }; 00302 00303 class vtkVector2d : public vtkVector2<double> 00304 { 00305 public: 00306 vtkVector2d() {} 00307 vtkVector2d(double x, double y) : vtkVector2<double>(x, y) {} 00308 explicit vtkVector2d(double scalar) : vtkVector2<double>(scalar) {} 00309 explicit vtkVector2d(const double *init) : vtkVector2<double>(init) {} 00310 vtkVectorDerivedMacro(vtkVector2d, double, 2) 00311 }; 00312 00313 #define vtkVector3Cross(vectorType, type) \ 00314 vectorType Cross(const vectorType& other) \ 00315 { \ 00316 return vectorType(vtkVector3<type>::Cross(other).GetData()); \ 00317 } \ 00318 00319 class vtkVector3i : public vtkVector3<int> 00320 { 00321 public: 00322 vtkVector3i() {} 00323 vtkVector3i(int x, int y, int z) : vtkVector3<int>(x, y, z) {} 00324 explicit vtkVector3i(int scalar) : vtkVector3<int>(scalar) {} 00325 explicit vtkVector3i(const int *init) : vtkVector3<int>(init) {} 00326 vtkVectorDerivedMacro(vtkVector3i, int, 3) 00327 vtkVector3Cross(vtkVector3i, int) 00328 }; 00329 00330 class vtkVector3f : public vtkVector3<float> 00331 { 00332 public: 00333 vtkVector3f() {} 00334 vtkVector3f(float x, float y, float z) : vtkVector3<float>(x, y, z) {} 00335 explicit vtkVector3f(float scalar) : vtkVector3<float>(scalar) {} 00336 explicit vtkVector3f(const float *init) : vtkVector3<float>(init) {} 00337 vtkVectorDerivedMacro(vtkVector3f, float, 3) 00338 vtkVector3Cross(vtkVector3f, float) 00339 }; 00340 00341 class vtkVector3d : public vtkVector3<double> 00342 { 00343 public: 00344 vtkVector3d() {} 00345 vtkVector3d(double x, double y, double z) : vtkVector3<double>(x, y, z) {} 00346 explicit vtkVector3d(double scalar) : vtkVector3<double>(scalar) {} 00347 explicit vtkVector3d(const double *init) : vtkVector3<double>(init) {} 00348 vtkVectorDerivedMacro(vtkVector3d, double, 3) 00349 vtkVector3Cross(vtkVector3d, double) 00350 }; 00351 00352 #ifndef VTK_LEGACY_REMOVE 00353 template<typename T> 00354 const T& vtkVector2<T>::X() const 00355 { 00356 VTK_LEGACY_REPLACED_BODY(vtkVector2::X, "VTK 6.0", vtkVector2::GetX); 00357 return this->GetX(); 00358 } 00359 00360 template<typename T> 00361 const T& vtkVector2<T>::Y() const 00362 { 00363 VTK_LEGACY_REPLACED_BODY(vtkVector2::Y, "VTK 6.0", vtkVector2::GetY); 00364 return this->GetY(); 00365 } 00366 00367 template<typename T> 00368 const T& vtkVector3<T>::X() const 00369 { 00370 VTK_LEGACY_REPLACED_BODY(vtkVector3::X, "VTK 6.0", vtkVector3::GetX); 00371 return this->GetX(); 00372 } 00373 00374 template<typename T> 00375 const T& vtkVector3<T>::Y() const 00376 { 00377 VTK_LEGACY_REPLACED_BODY(vtkVector3::Y, "VTK 6.0", vtkVector3::GetY); 00378 return this->GetY(); 00379 } 00380 00381 template<typename T> 00382 const T& vtkVector3<T>::Z() const 00383 { 00384 VTK_LEGACY_REPLACED_BODY(vtkVector3::Z, "VTK 6.0", vtkVector3::GetZ); 00385 return this->GetZ(); 00386 } 00387 #endif // VTK_LEGACY_REMOVE 00388 00389 #endif // __vtkVector_h 00390 // VTK-HeaderTest-Exclude: vtkVector.h