VTK
vtkTuple.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkTuple.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
15 
24 #ifndef vtkTuple_h
25 #define vtkTuple_h
26 
27 #include "vtkIOStream.h" // For streaming operators
28 #include "vtkSystemIncludes.h"
29 
30 #include <cassert> // For inline assert for bounds checked methods.
31 #include <cstdlib> // for std::abs() with int overloads
32 #include <cmath> // for std::abs() with float overloads
33 
34 template<typename T, int Size>
35 class vtkTuple
36 {
37 public:
44  {
45  }
46 
50  explicit vtkTuple(const T& scalar)
51  {
52  for (int i = 0; i < Size; ++i)
53  {
54  this->Data[i] = scalar;
55  }
56  }
57 
63  explicit vtkTuple(const T* init)
64  {
65  for (int i = 0; i < Size; ++i)
66  {
67  this->Data[i] = init[i];
68  }
69  }
70 
74  int GetSize() const { return Size; }
75 
79  T* GetData() { return this->Data; }
80  const T* GetData() const { return this->Data; }
81 
87  T& operator[](int i) { return this->Data[i]; }
88  const T& operator[](int i) const { return this->Data[i]; }
89 
91 
96  T operator()(int i) const
97  {
98  assert("pre: index_in_bounds" && i >= 0 && i < Size);
99  return this->Data[i];
100  }
102 
104 
107  bool Compare(const vtkTuple<T, Size>& other, const T& tol) const
108  {
109  if (Size != other.GetSize())
110  {
111  return false;
112  }
113  for (int i = 0; i < Size; ++i)
114  {
115  if (std::abs(this->Data[i] - other.Data[i]) >= tol)
116  {
117  return false;
118  }
119  }
120  return true;
121  }
123 
125 
128  template<typename TR>
130  {
131  vtkTuple<TR, Size> result;
132  for (int i = 0; i < Size; ++i)
133  {
134  result[i] = static_cast<TR>(this->Data[i]);
135  }
136  return result;
137  }
139 
140 protected:
142 
145  T Data[Size];
146 };
148 
150 
153 template<typename A, int Size>
154 ostream& operator<<(ostream& out, const vtkTuple<A, Size>& t)
155 {
156  out << "(";
157  bool first = true;
158  for (int i = 0; i < Size; ++i)
159  {
160  if (first)
161  {
162  first = false;
163  }
164  else
165  {
166  out << ", ";
167  }
168  out << t[i];
169  }
170  out << ")";
171  return out;
172 }
173 // Specialize for unsigned char so that we can see the numbers!
174 template<int Size>
175 ostream& operator<<(ostream& out, const vtkTuple<unsigned char, Size>& t)
176 {
177  out << "(";
178  bool first = true;
179  for (int i = 0; i < Size; ++i)
180  {
181  if (first)
182  {
183  first = false;
184  }
185  else
186  {
187  out << ", ";
188  }
189  out << static_cast<int>(t[i]);
190  }
191  out << ")";
192  return out;
193 }
195 
197 
200 template<typename A, int Size>
202 {
203  for (int i = 0; i < Size; ++i)
204  {
205  if (t1[i] != t2[i])
206  {
207  return false;
208  }
209  }
210  return true;
211 }
213 
217 template<typename A, int Size>
219 {
220  return !(t1 == t2);
221 }
222 
223 #endif // vtkTuple_h
224 // VTK-HeaderTest-Exclude: vtkTuple.h
T Data[Size]
The only thing stored in memory!
Definition: vtkTuple.h:145
T & operator[](int i)
Get a reference to the underlying data element of the tuple.
Definition: vtkTuple.h:87
vtkTuple(const T &scalar)
Initialize all of the tuple&#39;s elements with the supplied scalar.
Definition: vtkTuple.h:50
vtkTuple()
The default constructor does not initialize values.
Definition: vtkTuple.h:43
bool operator==(const vtkTuple< A, Size > &t1, const vtkTuple< A, Size > &t2)
Equality operator performs an equality check on each component.
Definition: vtkTuple.h:201
bool operator!=(const vtkTuple< A, Size > &t1, const vtkTuple< A, Size > &t2)
Inequality for vector type.
Definition: vtkTuple.h:218
vtkTuple< TR, Size > Cast() const
Cast the tuple to the specified type, returning the result.
Definition: vtkTuple.h:129
templated base type for containers of constant size.
Definition: vtkTuple.h:35
vtkTuple(const T *init)
Initialize the tuple&#39;s elements with the elements of the supplied array.
Definition: vtkTuple.h:63
T operator()(int i) const
Get the value of the tuple at the index specified.
Definition: vtkTuple.h:96
T * GetData()
Get a pointer to the underlying data of the tuple.
Definition: vtkTuple.h:79
const T & operator[](int i) const
Definition: vtkTuple.h:88
int GetSize() const
Get the size of the tuple.
Definition: vtkTuple.h:74
const T * GetData() const
Definition: vtkTuple.h:80
bool Compare(const vtkTuple< T, Size > &other, const T &tol) const
Equality operator with a tolerance to allow fuzzy comparisons.
Definition: vtkTuple.h:107