VTK
|
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 "vtkIOStream.h" // For streaming operators 00027 #include "vtkSystemIncludes.h" 00028 00029 #include <cassert> // For inline assert for bounds checked methods. 00030 #include <cstdlib> // for std::abs() with int overloads 00031 #include <cmath> // for std::abs() with float overloads 00032 00033 template<typename T, int Size> 00034 class vtkTuple 00035 { 00036 public: 00038 00042 vtkTuple() 00043 { 00044 } 00046 00048 00049 explicit vtkTuple(const T& scalar) 00050 { 00051 for (int i = 0; i < Size; ++i) 00052 { 00053 this->Data[i] = scalar; 00054 } 00055 } 00057 00059 00063 explicit vtkTuple(const T* init) 00064 { 00065 for (int i = 0; i < Size; ++i) 00066 { 00067 this->Data[i] = init[i]; 00068 } 00069 } 00071 00073 int GetSize() const { return Size; } 00074 00076 00077 T* GetData() { return this->Data; } 00078 const T* GetData() const { return this->Data; } 00080 00082 00085 T& operator[](int i) { return this->Data[i]; } 00086 const T& operator[](int i) const { return this->Data[i]; } 00088 00090 00093 T operator()(int i) const 00094 { 00095 assert("pre: index_in_bounds" && i >= 0 && i < Size); 00096 return this->Data[i]; 00097 } 00099 00101 00102 bool Compare(const vtkTuple<T, Size>& other, const T& tol) const 00103 { 00104 if (Size != other.GetSize()) 00105 { 00106 return false; 00107 } 00108 for (int i = 0; i < Size; ++i) 00109 { 00110 if (std::abs(this->Data[i] - other.Data[i]) >= tol) 00111 { 00112 return false; 00113 } 00114 } 00115 return true; 00116 } 00118 00120 00121 template<typename TR> 00122 vtkTuple<TR, Size> Cast() const 00123 { 00124 vtkTuple<TR, Size> result; 00125 for (int i = 0; i < Size; ++i) 00126 { 00127 result[i] = static_cast<TR>(this->Data[i]); 00128 } 00129 return result; 00130 } 00132 00133 protected: 00135 00136 T Data[Size]; 00137 }; 00139 00141 00142 template<typename A, int Size> 00143 ostream& operator<<(ostream& out, const vtkTuple<A, Size>& t) 00144 { 00145 out << "("; 00146 bool first = true; 00147 for (int i = 0; i < Size; ++i) 00148 { 00149 if (first) 00150 { 00151 first = false; 00152 } 00153 else 00154 { 00155 out << ", "; 00156 } 00157 out << t[i]; 00158 } 00159 out << ")"; 00160 return out; 00161 } 00162 // Specialize for unsigned char so that we can see the numbers! 00163 template<int Size> 00164 ostream& operator<<(ostream& out, const vtkTuple<unsigned char, Size>& t) 00165 { 00166 out << "("; 00167 bool first = true; 00168 for (int i = 0; i < Size; ++i) 00169 { 00170 if (first) 00171 { 00172 first = false; 00173 } 00174 else 00175 { 00176 out << ", "; 00177 } 00178 out << static_cast<int>(t[i]); 00179 } 00180 out << ")"; 00181 return out; 00182 } 00184 00186 00187 template<typename A, int Size> 00188 bool operator==(const vtkTuple<A, Size>& t1, const vtkTuple<A, Size>& t2) 00189 { 00190 for (int i = 0; i < Size; ++i) 00191 { 00192 if (t1[i] != t2[i]) 00193 { 00194 return false; 00195 } 00196 } 00197 return true; 00198 } 00200 00202 00203 template<typename A, int Size> 00204 bool operator!=(const vtkTuple<A, Size>& t1, const vtkTuple<A, Size>& t2) 00205 { 00206 return !(t1 == t2); 00207 } 00209 00210 #endif // __vtkTuple_h 00211 // VTK-HeaderTest-Exclude: vtkTuple.h