VTK  9.4.20250413
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
27VTK_ABI_NAMESPACE_BEGIN
28template <typename T, int Size>
30{
31public:
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
141protected:
143
146 T Data[Size];
148};
149
151
154template <typename A, int Size>
155ostream& 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!
175template <int Size>
176ostream& 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
201template <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
218template <typename A, int Size>
220{
221 return !(t1 == t2);
222}
223
224VTK_ABI_NAMESPACE_END
225#endif // vtkTuple_h
226// VTK-HeaderTest-Exclude: vtkTuple.h
RealT t2
Definition PyrC2Basis.h:22
templated base type for containers of constant size.
Definition vtkTuple.h:30
bool Compare(const vtkTuple< T, Size > &other, const T &tol) const
Equality operator with a tolerance to allow fuzzy comparisons.
Definition vtkTuple.h:108
vtkTuple(const T *init)
Initialize the tuple's elements with the elements of the supplied array.
Definition vtkTuple.h:55
int GetSize() const
Get the size of the tuple.
Definition vtkTuple.h:75
vtkTuple< TR, Size > Cast() const
Cast the tuple to the specified type, returning the result.
Definition vtkTuple.h:130
vtkTuple(const T &scalar)
Initialize all of the tuple's elements with the supplied scalar.
Definition vtkTuple.h:42
T * GetData()
Get a pointer to the underlying data of the tuple.
Definition vtkTuple.h:80
T & operator[](int i)
Get a reference to the underlying data element of the tuple.
Definition vtkTuple.h:88
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
const T * GetData() const
Definition vtkTuple.h:81
T Data[Size]
The only thing stored in memory!
Definition vtkTuple.h:146
T operator()(int i) const
Get the value of the tuple at the index specified.
Definition vtkTuple.h:97
const T & operator[](int i) const
Definition vtkTuple.h:89
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:219
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
ostream & operator<<(ostream &out, const vtkTuple< A, Size > &t)
Output the contents of a tuple, mainly useful for debugging.
Definition vtkTuple.h:155