VTK  9.0.1
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 <cmath> // for std::abs() with float overloads
32 #include <cstdlib> // for std::abs() with int overloads
33 
34 template <typename T, int Size>
35 class vtkTuple
36 {
37 public:
43  vtkTuple() {}
44 
48  explicit vtkTuple(const T& scalar)
49  {
50  for (int i = 0; i < Size; ++i)
51  {
52  this->Data[i] = scalar;
53  }
54  }
55 
61  explicit vtkTuple(const T* init)
62  {
63  for (int i = 0; i < Size; ++i)
64  {
65  this->Data[i] = init[i];
66  }
67  }
68 
72  int GetSize() const { return Size; }
73 
77  T* GetData() { return this->Data; }
78  const T* GetData() const { return this->Data; }
79 
85  T& operator[](int i) { return this->Data[i]; }
86  const T& operator[](int i) const { return this->Data[i]; }
87 
89 
94  T operator()(int i) const
95  {
96  assert("pre: index_in_bounds" && i >= 0 && i < Size);
97  return this->Data[i];
98  }
100 
102 
105  bool Compare(const vtkTuple<T, Size>& other, const T& tol) const
106  {
107  if (Size != other.GetSize())
108  {
109  return false;
110  }
111  for (int i = 0; i < Size; ++i)
112  {
113  if (std::abs(this->Data[i] - other.Data[i]) >= tol)
114  {
115  return false;
116  }
117  }
118  return true;
119  }
121 
123 
126  template <typename TR>
128  {
129  vtkTuple<TR, Size> result;
130  for (int i = 0; i < Size; ++i)
131  {
132  result[i] = static_cast<TR>(this->Data[i]);
133  }
134  return result;
135  }
137 
138 protected:
140 
143  T Data[Size];
145 };
146 
148 
151 template <typename A, int Size>
152 ostream& operator<<(ostream& out, const vtkTuple<A, Size>& t)
153 {
154  out << "(";
155  bool first = true;
156  for (int i = 0; i < Size; ++i)
157  {
158  if (first)
159  {
160  first = false;
161  }
162  else
163  {
164  out << ", ";
165  }
166  out << t[i];
167  }
168  out << ")";
169  return out;
170 }
171 // Specialize for unsigned char so that we can see the numbers!
172 template <int Size>
173 ostream& operator<<(ostream& out, const vtkTuple<unsigned char, Size>& t)
174 {
175  out << "(";
176  bool first = true;
177  for (int i = 0; i < Size; ++i)
178  {
179  if (first)
180  {
181  first = false;
182  }
183  else
184  {
185  out << ", ";
186  }
187  out << static_cast<int>(t[i]);
188  }
189  out << ")";
190  return out;
191 }
193 
195 
198 template <typename A, int Size>
200 {
201  for (int i = 0; i < Size; ++i)
202  {
203  if (t1[i] != t2[i])
204  {
205  return false;
206  }
207  }
208  return true;
209 }
211 
215 template <typename A, int Size>
217 {
218  return !(t1 == t2);
219 }
220 
221 #endif // vtkTuple_h
222 // VTK-HeaderTest-Exclude: vtkTuple.h
templated base type for containers of constant size.
Definition: vtkTuple.h:36
bool Compare(const vtkTuple< T, Size > &other, const T &tol) const
Equality operator with a tolerance to allow fuzzy comparisons.
Definition: vtkTuple.h:105
T & operator[](int i)
Get a reference to the underlying data element of the tuple.
Definition: vtkTuple.h:85
vtkTuple(const T *init)
Initialize the tuple's elements with the elements of the supplied array.
Definition: vtkTuple.h:61
const T * GetData() const
Definition: vtkTuple.h:78
T * GetData()
Get a pointer to the underlying data of the tuple.
Definition: vtkTuple.h:77
int GetSize() const
Get the size of the tuple.
Definition: vtkTuple.h:72
vtkTuple(const T &scalar)
Initialize all of the tuple's elements with the supplied scalar.
Definition: vtkTuple.h:48
const T & operator[](int i) const
Definition: vtkTuple.h:86
vtkTuple()
The default constructor does not initialize values.
Definition: vtkTuple.h:43
T Data[Size]
The only thing stored in memory!
Definition: vtkTuple.h:143
T operator()(int i) const
Get the value of the tuple at the index specified.
Definition: vtkTuple.h:94
vtkTuple< TR, Size > Cast() const
Cast the tuple to the specified type, returning the result.
Definition: vtkTuple.h:127
ostream & operator<<(ostream &out, const vtkTuple< A, Size > &t)
Output the contents of a tuple, mainly useful for debugging.
Definition: vtkTuple.h:152
bool operator!=(const vtkTuple< A, Size > &t1, const vtkTuple< A, Size > &t2)
Inequality for vector type.
Definition: vtkTuple.h:216
bool operator==(const vtkTuple< A, Size > &t1, const vtkTuple< A, Size > &t2)
Equality operator performs an equality check on each component.
Definition: vtkTuple.h:199