HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Tuple.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2012-2017 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 <sstream>
38 #include "Math.h"
39 
40 
41 namespace openvdb {
43 namespace OPENVDB_VERSION_NAME {
44 namespace math {
45 
46 /// @brief Dummy class for tag dispatch of conversion constructors
47 struct Conversion {};
48 
49 
50 /// @class Tuple "Tuple.h"
51 /// A base class for homogenous tuple types
52 template<int SIZE, typename T>
53 class Tuple {
54 public:
55  typedef T value_type;
56  typedef T ValueType;
57 
58  static const int size = SIZE;
59 
60  /// @brief Default ctor. Does nothing.
61  /// @details This is required because declaring a copy (or other) constructor
62  /// prevents the compiler from synthesizing a default constructor.
63  Tuple() {}
64 
65  /// Copy constructor. Used when the class signature matches exactly.
66  Tuple(Tuple const& src) {
67  for (int i = 0; i < SIZE; ++i) {
68  mm[i] = src.mm[i];
69  }
70  }
71 
72  /// @brief Assignment operator
73  /// @details This is required because declaring a copy (or other) constructor
74  /// prevents the compiler from synthesizing a default assignment operator.
75  Tuple& operator=(Tuple const& src) {
76  if (&src != this) {
77  for (int i = 0; i < SIZE; ++i) {
78  mm[i] = src.mm[i];
79  }
80  }
81  return *this;
82  }
83 
84  /// @brief Conversion constructor.
85  /// @details Tuples with different value types and different sizes can be
86  /// interconverted using this member. Converting from a larger tuple
87  /// results in truncation; converting from a smaller tuple results in
88  /// the extra data members being zeroed out. This function assumes that
89  /// the integer 0 is convertible to the tuple's value type.
90  template <int src_size, typename src_valtype>
92  enum { COPY_END = (SIZE < src_size ? SIZE : src_size) };
93 
94  for (int i = 0; i < COPY_END; ++i) {
95  mm[i] = src[i];
96  }
97  for (int i = COPY_END; i < SIZE; ++i) {
98  mm[i] = 0;
99  }
100  }
101 
102  T operator[](int i) const {
103  // we'd prefer to use size_t, but can't because gcc3.2 doesn't like
104  // it - it conflicts with child class conversion operators to
105  // pointer types.
106 // assert(i >= 0 && i < SIZE);
107  return mm[i];
108  }
109 
110  T& operator[](int i) {
111  // see above for size_t vs int
112 // assert(i >= 0 && i < SIZE);
113  return mm[i];
114  }
115 
116  /// @name Compatibility
117  /// These are mostly for backwards compability with functions that take
118  /// old-style Vs (which are just arrays).
119  //@{
120  /// Copies this tuple into an array of a compatible type
121  template <typename S>
122  void toV(S *v) const {
123  for (int i = 0; i < SIZE; ++i) {
124  v[i] = mm[i];
125  }
126  }
127 
128  /// Exposes the internal array. Be careful when using this function.
130  return mm;
131  }
132  /// Exposes the internal array. Be careful when using this function.
133  value_type const *asV() const {
134  return mm;
135  }
136  //@} Compatibility
137 
138  /// @return string representation of Classname
140  str() const {
141  std::ostringstream buffer;
142 
143  buffer << "[";
144 
145  // For each column
146  for (unsigned j(0); j < SIZE; j++) {
147  if (j) buffer << ", ";
148  buffer << mm[j];
149  }
150 
151  buffer << "]";
152 
153  return buffer.str();
154  }
155 
156  void write(std::ostream& os) const {
157  os.write(reinterpret_cast<const char*>(&mm), sizeof(T)*SIZE);
158  }
159  void read(std::istream& is) {
160  is.read(reinterpret_cast<char*>(&mm), sizeof(T)*SIZE);
161  }
162 
163 protected:
165 };
166 
167 
168 ////////////////////////////////////////
169 
170 
171 /// @return true if t0 < t1, comparing components in order of significance.
172 template<int SIZE, typename T0, typename T1>
173 bool
174 operator<(const Tuple<SIZE, T0>& t0, const Tuple<SIZE, T1>& t1)
175 {
176  for (int i = 0; i < SIZE-1; ++i) {
177  if (!isExactlyEqual(t0[i], t1[i])) return t0[i] < t1[i];
178  }
179  return t0[SIZE-1] < t1[SIZE-1];
180 }
181 
182 
183 /// @return true if t0 > t1, comparing components in order of significance.
184 template<int SIZE, typename T0, typename T1>
185 bool
187 {
188  for (int i = 0; i < SIZE-1; ++i) {
189  if (!isExactlyEqual(t0[i], t1[i])) return t0[i] > t1[i];
190  }
191  return t0[SIZE-1] > t1[SIZE-1];
192 }
193 
194 
195 ////////////////////////////////////////
196 
197 
198 /// @return the absolute value of the given Tuple.
199 template<int SIZE, typename T>
200 Tuple<SIZE, T>
202 {
203  Tuple<SIZE, T> result;
204  for (int i = 0; i < SIZE; ++i) result[i] = math::Abs(t[i]);
205  return result;
206 }
207 
208 
209 ////////////////////////////////////////
210 
211 
212 /// Write a Tuple to an output stream
213 template <int SIZE, typename T>
214 std::ostream& operator<<(std::ostream& ostr, const Tuple<SIZE, T>& classname)
215 {
216  ostr << classname.str();
217  return ostr;
218 }
219 
220 } // namespace math
221 } // namespace OPENVDB_VERSION_NAME
222 } // namespace openvdb
223 
224 #endif // OPENVDB_MATH_TUPLE_HAS_BEEN_INCLUDED
225 
226 // Copyright (c) 2012-2017 DreamWorks Animation LLC
227 // All rights reserved. This software is distributed under the
228 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
value_type const * asV() const
Exposes the internal array. Be careful when using this function.
Definition: Tuple.h:133
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:407
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:91
Tuple(Tuple const &src)
Copy constructor. Used when the class signature matches exactly.
Definition: Tuple.h:66
Dummy class for tag dispatch of conversion constructors.
Definition: Tuple.h:47
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:129
GLsizeiptr size
Definition: glcorearb.h:663
void toV(S *v) const
Copies this tuple into an array of a compatible type.
Definition: Tuple.h:122
Coord Abs(const Coord &xyz)
Definition: Coord.h:254
#define OPENVDB_VERSION_NAME
Definition: version.h:43
Tuple()
Default ctor. Does nothing.
Definition: Tuple.h:63
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:186
void write(std::ostream &os) const
Definition: Tuple.h:156
Tuple & operator=(Tuple const &src)
Assignment operator.
Definition: Tuple.h:75
#define SIZE
Definition: simple.C:40
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
GLenum src
Definition: glcorearb.h:1792