VTK  9.7.1
vtkTuple.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
3 
15 #ifndef vtkTuple_h
16 #define vtkTuple_h
17 
18 #include "vtkIOStream.h" // For streaming operators
19 #include "vtkSystemIncludes.h"
20 
21 #include <algorithm> // for std::copy
22 #include <array> // for std::array
23 #include <cassert> // For inline assert for bounds checked methods.
24 #include <cmath> // for std::abs() with float overloads
25 #include <cstdlib> // for std::abs() with int overloads
26 
27 VTK_ABI_NAMESPACE_BEGIN
28 template <typename T, int Size>
29 class vtkTuple
30 {
31 public:
37  vtkTuple() = default;
38 
42  explicit vtkTuple(const T& scalar)
43  {
44  for (int i = 0; i < Size; ++i)
45  {
46  this->Data[i] = scalar;
47  }
48  }
49 
55  explicit vtkTuple(const T* init)
56  {
57  for (int i = 0; i < Size; ++i)
58  {
59  this->Data[i] = init[i];
60  }
61  }
62 
67  explicit vtkTuple(const std::array<T, Size>& values)
68  {
69  std::copy(values.begin(), values.end(), this->Data);
70  }
71 
75  int GetSize() const { return Size; }
76 
80  T* GetData() { return this->Data; }
81  const T* GetData() const { return this->Data; }
82 
88  T& operator[](int i) { return this->Data[i]; }
89  const T& operator[](int i) const { return this->Data[i]; }
90 
92 
97  T operator()(int i) const
98  {
99  assert("pre: index_in_bounds" && i >= 0 && i < Size);
100  return this->Data[i];
101  }
103 
105 
108  bool Compare(const vtkTuple<T, Size>& other, const T& tol) const
109  {
110  if (Size != other.GetSize())
111  {
112  return false;
113  }
114  for (int i = 0; i < Size; ++i)
115  {
116  if (std::abs(this->Data[i] - other.Data[i]) >= tol)
117  {
118  return false;
119  }
120  }
121  return true;
122  }
124 
126 
129  template <typename TR>
131  {
132  vtkTuple<TR, Size> result;
133  for (int i = 0; i < Size; ++i)
134  {
135  result[i] = static_cast<TR>(this->Data[i]);
136  }
137  return result;
138  }
140 
141 protected:
143 
146  T Data[Size];
148 };
149 
151 
154 template <typename A, int Size>
155 ostream& operator<<(ostream& out, const vtkTuple<A, Size>& t)
156 {
157  out << "(";
158  bool first = true;
159  for (int i = 0; i < Size; ++i)
160  {
161  if (first)
162  {
163  first = false;
164  }
165  else
166  {
167  out << ", ";
168  }
169  out << t[i];
170  }
171  out << ")";
172  return out;
173 }
174 // Specialize for unsigned char so that we can see the numbers!
175 template <int Size>
176 ostream& operator<<(ostream& out, const vtkTuple<unsigned char, Size>& t)
177 {
178  out << "(";
179  bool first = true;
180  for (int i = 0; i < Size; ++i)
181  {
182  if (first)
183  {
184  first = false;
185  }
186  else
187  {
188  out << ", ";
189  }
190  out << static_cast<int>(t[i]);
191  }
192  out << ")";
193  return out;
194 }
196 
198 
201 template <typename A, int Size>
203 {
204  for (int i = 0; i < Size; ++i)
205  {
206  if (t1[i] != t2[i])
207  {
208  return false;
209  }
210  }
211  return true;
212 }
214 
218 template <typename A, int Size>
220 {
221  return !(t1 == t2);
222 }
223 
224 VTK_ABI_NAMESPACE_END
225 #endif // vtkTuple_h
226 // VTK-HeaderTest-Exclude: vtkTuple.h
T Data[Size]
The only thing stored in memory!
Definition: vtkTuple.h:146
T & operator[](int i)
Get a reference to the underlying data element of the tuple.
Definition: vtkTuple.h:88
vtkTuple(const T &scalar)
Initialize all of the tuple's elements with the supplied scalar.
Definition: vtkTuple.h:42
vtkTuple()=default
The default constructor does not initialize values.
bool operator==(const vtkTuple< A, Size > &t1, const vtkTuple< A, Size > &t2)
Equality operator performs an equality check on each component.
Definition: vtkTuple.h:202
bool operator!=(const vtkTuple< A, Size > &t1, const vtkTuple< A, Size > &t2)
Inequality for vector type.
Definition: vtkTuple.h:219
vtkTuple< TR, Size > Cast() const
Cast the tuple to the specified type, returning the result.
Definition: vtkTuple.h:130
templated base type for containers of constant size.
Definition: vtkTuple.h:29
vtkTuple(const T *init)
Initialize the tuple's elements with the elements of the supplied array.
Definition: vtkTuple.h:55
T operator()(int i) const
Get the value of the tuple at the index specified.
Definition: vtkTuple.h:97
vtkTuple(const std::array< T, Size > &values)
Initialize the tuple's elements using a std::array for matching type and size.
Definition: vtkTuple.h:67
T * GetData()
Get a pointer to the underlying data of the tuple.
Definition: vtkTuple.h:80
const T & operator[](int i) const
Definition: vtkTuple.h:89
int GetSize() const
Get the size of the tuple.
Definition: vtkTuple.h:75
const T * GetData() const
Definition: vtkTuple.h:81
RealT t2
Definition: PyrC2Basis.h:22
bool Compare(const vtkTuple< T, Size > &other, const T &tol) const
Equality operator with a tolerance to allow fuzzy comparisons.
Definition: vtkTuple.h:108