VTK
dox/Common/vtkTuple.h
Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    vtkTuple.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 
00023 #ifndef __vtkTuple_h
00024 #define __vtkTuple_h
00025 
00026 #include <cassert> // For inline assert for bounds checked methods.
00027 
00028 template<typename T, int Size>
00029 class vtkTuple
00030 {
00031 public:
00033 
00037   vtkTuple()
00038   {
00039   }
00041 
00043 
00044   explicit vtkTuple(const T& scalar)
00045   {
00046     for (int i = 0; i < Size; ++i)
00047       {
00048       this->Data[i] = scalar;
00049       }
00050   }
00052 
00054 
00058   explicit vtkTuple(const T* init)
00059   {
00060     for (int i = 0; i < Size; ++i)
00061       {
00062       this->Data[i] = init[i];
00063       }
00064   }
00066 
00068   int GetSize() const { return Size; }
00069 
00071 
00072   T* GetData() { return this->Data; }
00073   const T* GetData() const { return this->Data; }
00075 
00077 
00080   T& operator[](int i) { return this->Data[i]; }
00081   const T& operator[](int i) const { return this->Data[i]; }
00083 
00085 
00088   T operator()(int i) const
00089   {
00090     assert("pre: index_in_bounds" && i >= 0 && i < Size);
00091     return this->Data[i];
00092   }
00094 
00096 
00097   bool Compare(const vtkTuple<T, Size>& other, const T& tol) const
00098   {
00099     if (Size != other.GetSize())
00100       {
00101       return false;
00102       }
00103     for (int i = 0; i < Size; ++i)
00104       {
00105       if (fabs(this->Data[i] - other.Data[i]) >= tol)
00106         {
00107         return false;
00108         }
00109       }
00110     return true;
00111   }
00113 
00115 
00116   template<typename TR>
00117   vtkTuple<TR, Size> Cast() const
00118   {
00119     vtkTuple<TR, Size> result;
00120     for (int i = 0; i < Size; ++i)
00121       {
00122       result[i] = static_cast<TR>(this->Data[i]);
00123       }
00124     return result;
00125   }
00127 
00128 protected:
00130 
00131   T Data[Size];
00132 };
00134 
00135 #endif // __vtkTuple_h