HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tuple.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2012-2018 DreamWorks Animation LLC
4 //
5 // All rights reserved. This software is distributed under the
6 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
7 //
8 // Redistributions of source code must retain the above copyright
9 // and license notice and the following restrictions and disclaimer.
10 //
11 // * Neither the name of DreamWorks Animation nor the names of
12 // its contributors may be used to endorse or promote products derived
13 // from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
27 // LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
28 //
29 ///////////////////////////////////////////////////////////////////////////
30 //
31 /// @file Tuple.h
32 /// @author Ben Kwa
33 
34 #ifndef OPENVDB_MATH_TUPLE_HAS_BEEN_INCLUDED
35 #define OPENVDB_MATH_TUPLE_HAS_BEEN_INCLUDED
36 
37 #include "Math.h"
38 #include <cmath>
39 #include <sstream>
40 #include <string>
41 
42 
43 namespace openvdb {
45 namespace OPENVDB_VERSION_NAME {
46 namespace math {
47 
48 /// @brief Dummy class for tag dispatch of conversion constructors
49 struct Conversion {};
50 
51 
52 /// @class Tuple "Tuple.h"
53 /// A base class for homogenous tuple types
54 template<int SIZE, typename T>
55 class Tuple {
56 public:
57  using value_type = T;
58  using ValueType = T;
59 
60  static const int size = SIZE;
61 
62  /// @brief Default ctor. Does nothing.
63  /// @details This is required because declaring a copy (or other) constructor
64  /// prevents the compiler from synthesizing a default constructor.
65  Tuple() {}
66 
67  /// Copy constructor. Used when the class signature matches exactly.
68  Tuple(Tuple const& src) {
69  for (int i = 0; i < SIZE; ++i) {
70  mm[i] = src.mm[i];
71  }
72  }
73 
74  /// @brief Assignment operator
75  /// @details This is required because declaring a copy (or other) constructor
76  /// prevents the compiler from synthesizing a default assignment operator.
77  Tuple& operator=(Tuple const& src) {
78  if (&src != this) {
79  for (int i = 0; i < SIZE; ++i) {
80  mm[i] = src.mm[i];
81  }
82  }
83  return *this;
84  }
85 
86  /// @brief Conversion constructor.
87  /// @details Tuples with different value types and different sizes can be
88  /// interconverted using this member. Converting from a larger tuple
89  /// results in truncation; converting from a smaller tuple results in
90  /// the extra data members being zeroed out. This function assumes that
91  /// the integer 0 is convertible to the tuple's value type.
92  template <int src_size, typename src_valtype>
94  enum { COPY_END = (SIZE < src_size ? SIZE : src_size) };
95 
96  for (int i = 0; i < COPY_END; ++i) {
97  mm[i] = src[i];
98  }
99  for (int i = COPY_END; i < SIZE; ++i) {
100  mm[i] = 0;
101  }
102  }
103 
104  T operator[](int i) const {
105  // we'd prefer to use size_t, but can't because gcc3.2 doesn't like
106  // it - it conflicts with child class conversion operators to
107  // pointer types.
108 // assert(i >= 0 && i < SIZE);
109  return mm[i];
110  }
111 
112  T& operator[](int i) {
113  // see above for size_t vs int
114 // assert(i >= 0 && i < SIZE);
115  return mm[i];
116  }
117 
118  /// @name Compatibility
119  /// These are mostly for backwards compability with functions that take
120  /// old-style Vs (which are just arrays).
121  //@{
122  /// Copies this tuple into an array of a compatible type
123  template <typename S>
124  void toV(S *v) const {
125  for (int i = 0; i < SIZE; ++i) {
126  v[i] = mm[i];
127  }
128  }
129 
130  /// Exposes the internal array. Be careful when using this function.
132  return mm;
133  }
134  /// Exposes the internal array. Be careful when using this function.
135  value_type const *asV() const {
136  return mm;
137  }
138  //@} Compatibility
139 
140  /// @return string representation of Classname
141  std::string str() const {
142  std::ostringstream buffer;
143 
144  buffer << "[";
145 
146  // For each column
147  for (unsigned j(0); j < SIZE; j++) {
148  if (j) buffer << ", ";
149  buffer << PrintCast(mm[j]);
150  }
151 
152  buffer << "]";
153 
154  return buffer.str();
155  }
156 
157  void write(std::ostream& os) const {
158  os.write(reinterpret_cast<const char*>(&mm), sizeof(T)*SIZE);
159  }
160  void read(std::istream& is) {
161  is.read(reinterpret_cast<char*>(&mm), sizeof(T)*SIZE);
162  }
163 
164  /// True if a Nan is present in this tuple
165  bool isNan() const {
166  for (int i = 0; i < SIZE; ++i) {
167  if (std::isnan(mm[i])) return true;
168  }
169  return false;
170  }
171 
172  /// True if an Inf is present in this tuple
173  bool isInfinite() const {
174  for (int i = 0; i < SIZE; ++i) {
175  if (std::isinf(mm[i])) return true;
176  }
177  return false;
178  }
179 
180  /// True if no Nan or Inf values are present
181  bool isFinite() const {
182  for (int i = 0; i < SIZE; ++i) {
183  if (!math::isFinite(mm[i])) return false;
184  }
185  return true;
186  }
187 
188  /// True if all elements are exactly zero
189  bool isZero() const {
190  for (int i = 0; i < SIZE; ++i) {
191  if (!math::isZero(mm[i])) return false;
192  }
193  return true;
194  }
195 
196 protected:
198 };
199 
200 
201 ////////////////////////////////////////
202 
203 
204 /// @return true if t0 < t1, comparing components in order of significance.
205 template<int SIZE, typename T0, typename T1>
206 bool
207 operator<(const Tuple<SIZE, T0>& t0, const Tuple<SIZE, T1>& t1)
208 {
209  for (int i = 0; i < SIZE-1; ++i) {
210  if (!isExactlyEqual(t0[i], t1[i])) return t0[i] < t1[i];
211  }
212  return t0[SIZE-1] < t1[SIZE-1];
213 }
214 
215 
216 /// @return true if t0 > t1, comparing components in order of significance.
217 template<int SIZE, typename T0, typename T1>
218 bool
220 {
221  for (int i = 0; i < SIZE-1; ++i) {
222  if (!isExactlyEqual(t0[i], t1[i])) return t0[i] > t1[i];
223  }
224  return t0[SIZE-1] > t1[SIZE-1];
225 }
226 
227 
228 ////////////////////////////////////////
229 
230 
231 /// @return the absolute value of the given Tuple.
232 template<int SIZE, typename T>
233 Tuple<SIZE, T>
235 {
236  Tuple<SIZE, T> result;
237  for (int i = 0; i < SIZE; ++i) result[i] = math::Abs(t[i]);
238  return result;
239 }
240 
241 /// Return @c true if a Nan is present in the tuple.
242 template<int SIZE, typename T>
243 inline bool isNan(const Tuple<SIZE, T>& t) { return t.isNan(); }
244 
245 /// Return @c true if an Inf is present in the tuple.
246 template<int SIZE, typename T>
247 inline bool isInfinite(const Tuple<SIZE, T>& t) { return t.isInfinite(); }
248 
249 /// Return @c true if no Nan or Inf values are present.
250 template<int SIZE, typename T>
251 inline bool isFinite(const Tuple<SIZE, T>& t) { return t.isFinite(); }
252 
253 /// Return @c true if all elements are exactly equal to zero.
254 template<int SIZE, typename T>
255 inline bool isZero(const Tuple<SIZE, T>& t) { return t.isZero(); }
256 
257 ////////////////////////////////////////
258 
259 
260 /// Write a Tuple to an output stream
261 template <int SIZE, typename T>
262 std::ostream& operator<<(std::ostream& ostr, const Tuple<SIZE, T>& classname)
263 {
264  ostr << classname.str();
265  return ostr;
266 }
267 
268 } // namespace math
269 } // namespace OPENVDB_VERSION_NAME
270 } // namespace openvdb
271 
272 #endif // OPENVDB_MATH_TUPLE_HAS_BEEN_INCLUDED
273 
274 // Copyright (c) 2012-2018 DreamWorks Animation LLC
275 // All rights reserved. This software is distributed under the
276 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
bool isInfinite() const
True if an Inf is present in this tuple.
Definition: Tuple.h:173
value_type const * asV() const
Exposes the internal array. Be careful when using this function.
Definition: Tuple.h:135
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:395
const GLdouble * v
Definition: glcorearb.h:836
GLsizei const GLchar *const * string
Definition: glcorearb.h:813
Tuple(Tuple< src_size, src_valtype > const &src)
Conversion constructor.
Definition: Tuple.h:93
Tuple(Tuple const &src)
Copy constructor. Used when the class signature matches exactly.
Definition: Tuple.h:68
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:189
Dummy class for tag dispatch of conversion constructors.
Definition: Tuple.h:49
bool isFinite() const
True if no Nan or Inf values are present.
Definition: Tuple.h:181
GLuint buffer
Definition: glcorearb.h:659
png_uint_32 i
Definition: png.h:2877
value_type * asV()
Exposes the internal array. Be careful when using this function.
Definition: Tuple.h:131
GLsizeiptr size
Definition: glcorearb.h:663
void toV(S *v) const
Copies this tuple into an array of a compatible type.
Definition: Tuple.h:124
Coord Abs(const Coord &xyz)
Definition: Coord.h:513
Tuple()
Default ctor. Does nothing.
Definition: Tuple.h:65
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
bool operator>(const Tuple< SIZE, T0 > &t0, const Tuple< SIZE, T1 > &t1)
Definition: Tuple.h:219
void write(std::ostream &os) const
Definition: Tuple.h:157
Tuple & operator=(Tuple const &src)
Assignment operator.
Definition: Tuple.h:77
#define SIZE
Definition: simple.C:40
bool isNan() const
True if a Nan is present in this tuple.
Definition: Tuple.h:165
bool isZero() const
True if all elements are exactly zero.
Definition: Tuple.h:189
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:135
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:837
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
Definition: Math.h:308
bool isFinite(const float x)
Return true if x is finite.
Definition: Math.h:346
GLenum src
Definition: glcorearb.h:1792