OpenVDB  7.0.0
Tuple.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3 //
6 
7 #ifndef OPENVDB_MATH_TUPLE_HAS_BEEN_INCLUDED
8 #define OPENVDB_MATH_TUPLE_HAS_BEEN_INCLUDED
9 
10 #include "Math.h"
11 #include <cmath>
12 #include <sstream>
13 #include <string>
14 
15 
16 namespace openvdb {
18 namespace OPENVDB_VERSION_NAME {
19 namespace math {
20 
22 struct Conversion {};
23 
24 
27 template<int SIZE, typename T>
28 class Tuple {
29 public:
30  using value_type = T;
31  using ValueType = T;
32 
33  static const int size = SIZE;
34 
38  Tuple() {}
39 
41  Tuple(Tuple const& src) {
42  for (int i = 0; i < SIZE; ++i) {
43  mm[i] = src.mm[i];
44  }
45  }
46 
50  Tuple& operator=(Tuple const& src) {
51  if (&src != this) {
52  for (int i = 0; i < SIZE; ++i) {
53  mm[i] = src.mm[i];
54  }
55  }
56  return *this;
57  }
58 
65  template <int src_size, typename src_valtype>
66  explicit Tuple(Tuple<src_size, src_valtype> const &src) {
67  enum { COPY_END = (SIZE < src_size ? SIZE : src_size) };
68 
69  for (int i = 0; i < COPY_END; ++i) {
70  mm[i] = src[i];
71  }
72  for (int i = COPY_END; i < SIZE; ++i) {
73  mm[i] = 0;
74  }
75  }
76 
77  T operator[](int i) const {
78  // we'd prefer to use size_t, but can't because gcc3.2 doesn't like
79  // it - it conflicts with child class conversion operators to
80  // pointer types.
81 // assert(i >= 0 && i < SIZE);
82  return mm[i];
83  }
84 
85  T& operator[](int i) {
86  // see above for size_t vs int
87 // assert(i >= 0 && i < SIZE);
88  return mm[i];
89  }
90 
94 
95  template <typename S>
97  void toV(S *v) const {
98  for (int i = 0; i < SIZE; ++i) {
99  v[i] = mm[i];
100  }
101  }
102 
105  return mm;
106  }
108  value_type const *asV() const {
109  return mm;
110  }
112 
114  std::string str() const {
115  std::ostringstream buffer;
116 
117  buffer << "[";
118 
119  // For each column
120  for (unsigned j(0); j < SIZE; j++) {
121  if (j) buffer << ", ";
122  buffer << PrintCast(mm[j]);
123  }
124 
125  buffer << "]";
126 
127  return buffer.str();
128  }
129 
130  void write(std::ostream& os) const {
131  os.write(reinterpret_cast<const char*>(&mm), sizeof(T)*SIZE);
132  }
133  void read(std::istream& is) {
134  is.read(reinterpret_cast<char*>(&mm), sizeof(T)*SIZE);
135  }
136 
138  bool isNan() const {
139  for (int i = 0; i < SIZE; ++i) {
140  if (math::isNan(mm[i])) return true;
141  }
142  return false;
143  }
144 
146  bool isInfinite() const {
147  for (int i = 0; i < SIZE; ++i) {
148  if (math::isInfinite(mm[i])) return true;
149  }
150  return false;
151  }
152 
154  bool isFinite() const {
155  for (int i = 0; i < SIZE; ++i) {
156  if (!math::isFinite(mm[i])) return false;
157  }
158  return true;
159  }
160 
162  bool isZero() const {
163  for (int i = 0; i < SIZE; ++i) {
164  if (!math::isZero(mm[i])) return false;
165  }
166  return true;
167  }
168 
169 protected:
170  T mm[SIZE];
171 };
172 
173 
175 
176 
178 template<int SIZE, typename T0, typename T1>
179 bool
180 operator<(const Tuple<SIZE, T0>& t0, const Tuple<SIZE, T1>& t1)
181 {
182  for (int i = 0; i < SIZE-1; ++i) {
183  if (!isExactlyEqual(t0[i], t1[i])) return t0[i] < t1[i];
184  }
185  return t0[SIZE-1] < t1[SIZE-1];
186 }
187 
188 
190 template<int SIZE, typename T0, typename T1>
191 bool
193 {
194  for (int i = 0; i < SIZE-1; ++i) {
195  if (!isExactlyEqual(t0[i], t1[i])) return t0[i] > t1[i];
196  }
197  return t0[SIZE-1] > t1[SIZE-1];
198 }
199 
200 
202 
203 
205 template<int SIZE, typename T>
208 {
209  Tuple<SIZE, T> result;
210  for (int i = 0; i < SIZE; ++i) result[i] = math::Abs(t[i]);
211  return result;
212 }
213 
215 template<int SIZE, typename T>
216 inline bool isNan(const Tuple<SIZE, T>& t) { return t.isNan(); }
217 
219 template<int SIZE, typename T>
220 inline bool isInfinite(const Tuple<SIZE, T>& t) { return t.isInfinite(); }
221 
223 template<int SIZE, typename T>
224 inline bool isFinite(const Tuple<SIZE, T>& t) { return t.isFinite(); }
225 
227 template<int SIZE, typename T>
228 inline bool isZero(const Tuple<SIZE, T>& t) { return t.isZero(); }
229 
231 
232 
234 template <int SIZE, typename T>
235 std::ostream& operator<<(std::ostream& ostr, const Tuple<SIZE, T>& classname)
236 {
237  ostr << classname.str();
238  return ostr;
239 }
240 
241 } // namespace math
242 } // namespace OPENVDB_VERSION_NAME
243 } // namespace openvdb
244 
245 #endif // OPENVDB_MATH_TUPLE_HAS_BEEN_INCLUDED
Tuple & operator=(Tuple const &src)
Assignment operator.
Definition: Tuple.h:50
bool isInfinite() const
True if an Inf is present in this tuple.
Definition: Tuple.h:146
bool isZero(const Tuple< SIZE, T > &t)
Return true if all elements are exactly equal to zero.
Definition: Tuple.h:228
Definition: Tuple.h:28
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
std::string str() const
Definition: Tuple.h:114
bool isNan() const
True if a Nan is present in this tuple.
Definition: Tuple.h:138
bool isFinite() const
True if no Nan or Inf values are present.
Definition: Tuple.h:154
bool isNan(const Tuple< SIZE, T > &t)
Return true if a Nan is present in the tuple.
Definition: Tuple.h:216
auto PrintCast(const T &val) -> typename std::enable_if<!std::is_same< T, int8_t >::value &&!std::is_same< T, uint8_t >::value, const T & >::type
8-bit integer values print to std::ostreams as characters. Cast them so that they print as integers i...
Definition: Math.h:830
Tuple(Tuple< src_size, src_valtype > const &src)
Conversion constructor.
Definition: Tuple.h:66
Tuple< SIZE, T > Abs(const Tuple< SIZE, T > &t)
Definition: Tuple.h:207
bool operator>(const Tuple< SIZE, T0 > &t0, const Tuple< SIZE, T1 > &t1)
Definition: Tuple.h:192
T mm[SIZE]
Definition: Tuple.h:170
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:102
Dummy class for tag dispatch of conversion constructors.
Definition: Tuple.h:22
void write(std::ostream &os) const
Definition: Tuple.h:130
Definition: Exceptions.h:13
bool isZero() const
True if all elements are exactly zero.
Definition: Tuple.h:162
void read(std::istream &is)
Definition: Tuple.h:133
bool isInfinite(const Tuple< SIZE, T > &t)
Return true if an Inf is present in the tuple.
Definition: Tuple.h:220
Tuple(Tuple const &src)
Copy constructor. Used when the class signature matches exactly.
Definition: Tuple.h:41
value_type * asV()
Exposes the internal array. Be careful when using this function.
Definition: Tuple.h:104
T value_type
Definition: Tuple.h:30
T ValueType
Definition: Tuple.h:31
T operator[](int i) const
Definition: Tuple.h:77
void toV(S *v) const
Copies this tuple into an array of a compatible type.
Definition: Tuple.h:97
Tuple()
Default ctor. Does nothing.
Definition: Tuple.h:38
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:388
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:154
value_type const * asV() const
Exposes the internal array. Be careful when using this function.
Definition: Tuple.h:108
bool isFinite(const Tuple< SIZE, T > &t)
Return true if no Nan or Inf values are present.
Definition: Tuple.h:224
T & operator[](int i)
Definition: Tuple.h:85