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 <cassert> // For inline assert for bounds checked methods.
27 
28 template<typename T, int Size>
29 class vtkTuple
30 {
31 public:
33 
38  {
39  }
41 
43 
44  explicit vtkTuple(const T& scalar)
45  {
46  for (int i = 0; i < Size; ++i)
47  {
48  this->Data[i] = scalar;
49  }
50  }
52 
54 
58  explicit vtkTuple(const T* init)
59  {
60  for (int i = 0; i < Size; ++i)
61  {
62  this->Data[i] = init[i];
63  }
64  }
66 
68  int GetSize() const { return Size; }
69 
71 
72  T* GetData() { return this->Data; }
73  const T* GetData() const { return this->Data; }
75 
77 
80  T& operator[](int i) { return this->Data[i]; }
81  const T& operator[](int i) const { return this->Data[i]; }
83 
85 
88  T operator()(int i) const
89  {
90  assert("pre: index_in_bounds" && i >= 0 && i < Size);
91  return this->Data[i];
92  }
94 
96 
97  bool Compare(const vtkTuple<T, Size>& other, const T& tol) const
98  {
99  if (Size != other.GetSize())
100  {
101  return false;
102  }
103  for (int i = 0; i < Size; ++i)
104  {
105  if (fabs(this->Data[i] - other.Data[i]) >= tol)
106  {
107  return false;
108  }
109  }
110  return true;
111  }
113 
115 
116  template<typename TR>
118  {
119  vtkTuple<TR, Size> result;
120  for (int i = 0; i < Size; ++i)
121  {
122  result[i] = static_cast<TR>(this->Data[i]);
123  }
124  return result;
125  }
127 
128 protected:
130 
131  T Data[Size];
132 };
134 
135 #endif // __vtkTuple_h
136 // VTK-HeaderTest-Exclude: vtkTuple.h