VTK  9.4.20241218
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
12#ifndef vtkTuple_h
13#define vtkTuple_h
14
15#include "vtkIOStream.h" // For streaming operators
16#include "vtkSystemIncludes.h"
17
18#include <algorithm> // for std::copy
19#include <array> // for std::array
20#include <cassert> // For inline assert for bounds checked methods.
21#include <cmath> // for std::abs() with float overloads
22#include <cstdlib> // for std::abs() with int overloads
23
24VTK_ABI_NAMESPACE_BEGIN
25template <typename T, int Size>
27{
28public:
34 vtkTuple() = default;
35
39 explicit vtkTuple(const T& scalar)
40 {
41 for (int i = 0; i < Size; ++i)
42 {
43 this->Data[i] = scalar;
44 }
45 }
46
52 explicit vtkTuple(const T* init)
53 {
54 for (int i = 0; i < Size; ++i)
55 {
56 this->Data[i] = init[i];
57 }
58 }
59
64 explicit vtkTuple(const std::array<T, Size>& values)
65 {
66 std::copy(values.begin(), values.end(), this->Data);
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
138protected:
140
143 T Data[Size];
145};
146
148
151template <typename A, int Size>
152ostream& 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!
172template <int Size>
173ostream& 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
198template <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
215template <typename A, int Size>
217{
218 return !(t1 == t2);
219}
220
221VTK_ABI_NAMESPACE_END
222#endif // vtkTuple_h
223// VTK-HeaderTest-Exclude: vtkTuple.h
RealT t2
Definition PyrC2Basis.h:22
templated base type for containers of constant size.
Definition vtkTuple.h:27
bool Compare(const vtkTuple< T, Size > &other, const T &tol) const
Equality operator with a tolerance to allow fuzzy comparisons.
Definition vtkTuple.h:105
vtkTuple(const T *init)
Initialize the tuple's elements with the elements of the supplied array.
Definition vtkTuple.h:52
int GetSize() const
Get the size of the tuple.
Definition vtkTuple.h:72
vtkTuple< TR, Size > Cast() const
Cast the tuple to the specified type, returning the result.
Definition vtkTuple.h:127
vtkTuple(const T &scalar)
Initialize all of the tuple's elements with the supplied scalar.
Definition vtkTuple.h:39
T * GetData()
Get a pointer to the underlying data of the tuple.
Definition vtkTuple.h:77
T & operator[](int i)
Get a reference to the underlying data element of the tuple.
Definition vtkTuple.h:85
vtkTuple(const std::array< T, Size > &values)
Initialize the tuple's elements using a std::array for matching type and size.
Definition vtkTuple.h:64
const T * GetData() const
Definition vtkTuple.h:78
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
const T & operator[](int i) const
Definition vtkTuple.h:86
vtkTuple()=default
The default constructor does not initialize values.
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
ostream & operator<<(ostream &out, const vtkTuple< A, Size > &t)
Output the contents of a tuple, mainly useful for debugging.
Definition vtkTuple.h:152