VTK
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 
23 #ifndef vtkTuple_h
24 #define vtkTuple_h
25 
26 #include "vtkIOStream.h" // For streaming operators
27 #include "vtkSystemIncludes.h"
28 
29 #include <cassert> // For inline assert for bounds checked methods.
30 #include <cstdlib> // for std::abs() with int overloads
31 #include <cmath> // for std::abs() with float overloads
32 
33 template<typename T, int Size>
34 class vtkTuple
35 {
36 public:
38 
43  {
44  }
46 
48 
49  explicit vtkTuple(const T& scalar)
50  {
51  for (int i = 0; i < Size; ++i)
52  {
53  this->Data[i] = scalar;
54  }
55  }
57 
59 
63  explicit vtkTuple(const T* init)
64  {
65  for (int i = 0; i < Size; ++i)
66  {
67  this->Data[i] = init[i];
68  }
69  }
71 
73  int GetSize() const { return Size; }
74 
76 
77  T* GetData() { return this->Data; }
78  const T* GetData() const { return this->Data; }
80 
82 
85  T& operator[](int i) { return this->Data[i]; }
86  const T& operator[](int i) const { return this->Data[i]; }
88 
90 
93  T operator()(int i) const
94  {
95  assert("pre: index_in_bounds" && i >= 0 && i < Size);
96  return this->Data[i];
97  }
99 
101 
102  bool Compare(const vtkTuple<T, Size>& other, const T& tol) const
103  {
104  if (Size != other.GetSize())
105  {
106  return false;
107  }
108  for (int i = 0; i < Size; ++i)
109  {
110  if (std::abs(this->Data[i] - other.Data[i]) >= tol)
111  {
112  return false;
113  }
114  }
115  return true;
116  }
118 
120 
121  template<typename TR>
123  {
124  vtkTuple<TR, Size> result;
125  for (int i = 0; i < Size; ++i)
126  {
127  result[i] = static_cast<TR>(this->Data[i]);
128  }
129  return result;
130  }
132 
133 protected:
135 
136  T Data[Size];
137 };
139 
141 
142 template<typename A, int Size>
143 ostream& operator<<(ostream& out, const vtkTuple<A, Size>& t)
144 {
145  out << "(";
146  bool first = true;
147  for (int i = 0; i < Size; ++i)
148  {
149  if (first)
150  {
151  first = false;
152  }
153  else
154  {
155  out << ", ";
156  }
157  out << t[i];
158  }
159  out << ")";
160  return out;
161 }
162 // Specialize for unsigned char so that we can see the numbers!
163 template<int Size>
164 ostream& operator<<(ostream& out, const vtkTuple<unsigned char, 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 << static_cast<int>(t[i]);
179  }
180  out << ")";
181  return out;
182 }
184 
186 
187 template<typename A, int Size>
189 {
190  for (int i = 0; i < Size; ++i)
191  {
192  if (t1[i] != t2[i])
193  {
194  return false;
195  }
196  }
197  return true;
198 }
200 
202 
203 template<typename A, int Size>
205 {
206  return !(t1 == t2);
207 }
209 
210 #endif // vtkTuple_h
211 // VTK-HeaderTest-Exclude: vtkTuple.h
T Data[Size]
Definition: vtkTuple.h:136
T & operator[](int i)
Definition: vtkTuple.h:85
vtkTuple(const T &scalar)
Definition: vtkTuple.h:49
vtkTuple()
Definition: vtkTuple.h:42
bool operator==(const vtkTuple< A, Size > &t1, const vtkTuple< A, Size > &t2)
Definition: vtkTuple.h:188
bool operator!=(const vtkTuple< A, Size > &t1, const vtkTuple< A, Size > &t2)
Definition: vtkTuple.h:204
vtkTuple< TR, Size > Cast() const
Definition: vtkTuple.h:122
templated base type for containers of constant size.
Definition: vtkTuple.h:34
vtkTuple(const T *init)
Definition: vtkTuple.h:63
T operator()(int i) const
Definition: vtkTuple.h:93
T * GetData()
Definition: vtkTuple.h:77
const T & operator[](int i) const
Definition: vtkTuple.h:86
int GetSize() const
Definition: vtkTuple.h:73
const T * GetData() const
Definition: vtkTuple.h:78
bool Compare(const vtkTuple< T, Size > &other, const T &tol) const
Definition: vtkTuple.h:102