VTK  9.2.20230330
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 <algorithm> // for std::copy
31 #include <array> // for std::array
32 #include <cassert> // For inline assert for bounds checked methods.
33 #include <cmath> // for std::abs() with float overloads
34 #include <cstdlib> // for std::abs() with int overloads
35 
36 VTK_ABI_NAMESPACE_BEGIN
37 template <typename T, int Size>
38 class vtkTuple
39 {
40 public:
46  vtkTuple() = default;
47 
51  explicit vtkTuple(const T& scalar)
52  {
53  for (int i = 0; i < Size; ++i)
54  {
55  this->Data[i] = scalar;
56  }
57  }
58 
64  explicit vtkTuple(const T* init)
65  {
66  for (int i = 0; i < Size; ++i)
67  {
68  this->Data[i] = init[i];
69  }
70  }
71 
76  explicit vtkTuple(const std::array<T, Size>& values)
77  {
78  std::copy(values.begin(), values.end(), this->Data);
79  }
80 
84  int GetSize() const { return Size; }
85 
89  T* GetData() { return this->Data; }
90  const T* GetData() const { return this->Data; }
91 
97  T& operator[](int i) { return this->Data[i]; }
98  const T& operator[](int i) const { return this->Data[i]; }
99 
101 
106  T operator()(int i) const
107  {
108  assert("pre: index_in_bounds" && i >= 0 && i < Size);
109  return this->Data[i];
110  }
112 
114 
117  bool Compare(const vtkTuple<T, Size>& other, const T& tol) const
118  {
119  if (Size != other.GetSize())
120  {
121  return false;
122  }
123  for (int i = 0; i < Size; ++i)
124  {
125  if (std::abs(this->Data[i] - other.Data[i]) >= tol)
126  {
127  return false;
128  }
129  }
130  return true;
131  }
133 
135 
138  template <typename TR>
140  {
141  vtkTuple<TR, Size> result;
142  for (int i = 0; i < Size; ++i)
143  {
144  result[i] = static_cast<TR>(this->Data[i]);
145  }
146  return result;
147  }
149 
150 protected:
152 
155  T Data[Size];
157 };
158 
160 
163 template <typename A, int Size>
164 ostream& operator<<(ostream& out, const vtkTuple<A, Size>& t)
165 {
166  out << "(";
167  bool first = true;
168  for (int i = 0; i < Size; ++i)
169  {
170  if (first)
171  {
172  first = false;
173  }
174  else
175  {
176  out << ", ";
177  }
178  out << t[i];
179  }
180  out << ")";
181  return out;
182 }
183 // Specialize for unsigned char so that we can see the numbers!
184 template <int Size>
185 ostream& operator<<(ostream& out, const vtkTuple<unsigned char, Size>& t)
186 {
187  out << "(";
188  bool first = true;
189  for (int i = 0; i < Size; ++i)
190  {
191  if (first)
192  {
193  first = false;
194  }
195  else
196  {
197  out << ", ";
198  }
199  out << static_cast<int>(t[i]);
200  }
201  out << ")";
202  return out;
203 }
205 
207 
210 template <typename A, int Size>
212 {
213  for (int i = 0; i < Size; ++i)
214  {
215  if (t1[i] != t2[i])
216  {
217  return false;
218  }
219  }
220  return true;
221 }
223 
227 template <typename A, int Size>
229 {
230  return !(t1 == t2);
231 }
232 
233 VTK_ABI_NAMESPACE_END
234 #endif // vtkTuple_h
235 // VTK-HeaderTest-Exclude: vtkTuple.h
templated base type for containers of constant size.
Definition: vtkTuple.h:39
bool Compare(const vtkTuple< T, Size > &other, const T &tol) const
Equality operator with a tolerance to allow fuzzy comparisons.
Definition: vtkTuple.h:117
T & operator[](int i)
Get a reference to the underlying data element of the tuple.
Definition: vtkTuple.h:97
vtkTuple(const T *init)
Initialize the tuple's elements with the elements of the supplied array.
Definition: vtkTuple.h:64
const T * GetData() const
Definition: vtkTuple.h:90
T * GetData()
Get a pointer to the underlying data of the tuple.
Definition: vtkTuple.h:89
int GetSize() const
Get the size of the tuple.
Definition: vtkTuple.h:84
vtkTuple(const T &scalar)
Initialize all of the tuple's elements with the supplied scalar.
Definition: vtkTuple.h:51
const T & operator[](int i) const
Definition: vtkTuple.h:98
vtkTuple(const std::array< T, Size > &values)
Initialize the tuple's elements using a std::array for matching type and size.
Definition: vtkTuple.h:76
T Data[Size]
The only thing stored in memory!
Definition: vtkTuple.h:155
T operator()(int i) const
Get the value of the tuple at the index specified.
Definition: vtkTuple.h:106
vtkTuple< TR, Size > Cast() const
Cast the tuple to the specified type, returning the result.
Definition: vtkTuple.h:139
vtkTuple()=default
The default constructor does not initialize values.
ostream & operator<<(ostream &out, const vtkTuple< A, Size > &t)
Output the contents of a tuple, mainly useful for debugging.
Definition: vtkTuple.h:164
bool operator!=(const vtkTuple< A, Size > &t1, const vtkTuple< A, Size > &t2)
Inequality for vector type.
Definition: vtkTuple.h:228
bool operator==(const vtkTuple< A, Size > &t1, const vtkTuple< A, Size > &t2)
Equality operator performs an equality check on each component.
Definition: vtkTuple.h:211