VTK  9.1.20211230
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 template <typename T, int Size>
37 class vtkTuple
38 {
39 public:
45  vtkTuple() = default;
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 
75  explicit vtkTuple(const std::array<T, Size>& values)
76  {
77  std::copy(values.begin(), values.end(), this->Data);
78  }
79 
83  int GetSize() const { return Size; }
84 
88  T* GetData() { return this->Data; }
89  const T* GetData() const { return this->Data; }
90 
96  T& operator[](int i) { return this->Data[i]; }
97  const T& operator[](int i) const { return this->Data[i]; }
98 
100 
105  T operator()(int i) const
106  {
107  assert("pre: index_in_bounds" && i >= 0 && i < Size);
108  return this->Data[i];
109  }
111 
113 
116  bool Compare(const vtkTuple<T, Size>& other, const T& tol) const
117  {
118  if (Size != other.GetSize())
119  {
120  return false;
121  }
122  for (int i = 0; i < Size; ++i)
123  {
124  if (std::abs(this->Data[i] - other.Data[i]) >= tol)
125  {
126  return false;
127  }
128  }
129  return true;
130  }
132 
134 
137  template <typename TR>
139  {
140  vtkTuple<TR, Size> result;
141  for (int i = 0; i < Size; ++i)
142  {
143  result[i] = static_cast<TR>(this->Data[i]);
144  }
145  return result;
146  }
148 
149 protected:
151 
154  T Data[Size];
156 };
157 
159 
162 template <typename A, int Size>
163 ostream& operator<<(ostream& out, const vtkTuple<A, Size>& t)
164 {
165  out << "(";
166  bool first = true;
167  for (int i = 0; i < Size; ++i)
168  {
169  if (first)
170  {
171  first = false;
172  }
173  else
174  {
175  out << ", ";
176  }
177  out << t[i];
178  }
179  out << ")";
180  return out;
181 }
182 // Specialize for unsigned char so that we can see the numbers!
183 template <int Size>
184 ostream& operator<<(ostream& out, const vtkTuple<unsigned char, Size>& t)
185 {
186  out << "(";
187  bool first = true;
188  for (int i = 0; i < Size; ++i)
189  {
190  if (first)
191  {
192  first = false;
193  }
194  else
195  {
196  out << ", ";
197  }
198  out << static_cast<int>(t[i]);
199  }
200  out << ")";
201  return out;
202 }
204 
206 
209 template <typename A, int Size>
211 {
212  for (int i = 0; i < Size; ++i)
213  {
214  if (t1[i] != t2[i])
215  {
216  return false;
217  }
218  }
219  return true;
220 }
222 
226 template <typename A, int Size>
228 {
229  return !(t1 == t2);
230 }
231 
232 #endif // vtkTuple_h
233 // VTK-HeaderTest-Exclude: vtkTuple.h
templated base type for containers of constant size.
Definition: vtkTuple.h:38
bool Compare(const vtkTuple< T, Size > &other, const T &tol) const
Equality operator with a tolerance to allow fuzzy comparisons.
Definition: vtkTuple.h:116
T & operator[](int i)
Get a reference to the underlying data element of the tuple.
Definition: vtkTuple.h:96
vtkTuple(const T *init)
Initialize the tuple's elements with the elements of the supplied array.
Definition: vtkTuple.h:63
const T * GetData() const
Definition: vtkTuple.h:89
T * GetData()
Get a pointer to the underlying data of the tuple.
Definition: vtkTuple.h:88
int GetSize() const
Get the size of the tuple.
Definition: vtkTuple.h:83
vtkTuple(const T &scalar)
Initialize all of the tuple's elements with the supplied scalar.
Definition: vtkTuple.h:50
const T & operator[](int i) const
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:75
T Data[Size]
The only thing stored in memory!
Definition: vtkTuple.h:154
T operator()(int i) const
Get the value of the tuple at the index specified.
Definition: vtkTuple.h:105
vtkTuple< TR, Size > Cast() const
Cast the tuple to the specified type, returning the result.
Definition: vtkTuple.h:138
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:163
bool operator!=(const vtkTuple< A, Size > &t1, const vtkTuple< A, Size > &t2)
Inequality for vector type.
Definition: vtkTuple.h:227
bool operator==(const vtkTuple< A, Size > &t1, const vtkTuple< A, Size > &t2)
Equality operator performs an equality check on each component.
Definition: vtkTuple.h:210