OpenVDB  4.0.1
Tuple.h
Go to the documentation of this file.
1 //
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 //
30 //
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 
47 struct Conversion {};
48 
49 
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 
63  Tuple() {}
64 
66  Tuple(Tuple const& src) {
67  for (int i = 0; i < SIZE; ++i) {
68  mm[i] = src.mm[i];
69  }
70  }
71 
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 
90  template <int src_size, typename src_valtype>
91  explicit Tuple(Tuple<src_size, src_valtype> const &src) {
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 
119 
120  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 
129  value_type *asV() {
130  return mm;
131  }
133  value_type const *asV() const {
134  return mm;
135  }
137 
139  std::string
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:
164  T mm[SIZE];
165 };
166 
167 
169 
170 
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 
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 
196 
197 
199 template<int SIZE, typename 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 
210 
211 
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/ )
Tuple(Tuple< src_size, src_valtype > const &src)
Conversion constructor.
Definition: Tuple.h:91
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
T ValueType
Definition: Tuple.h:56
Tuple()
Default ctor. Does nothing.
Definition: Tuple.h:63
T mm[SIZE]
Definition: Tuple.h:164
std::string str() const
Definition: Tuple.h:140
value_type * asV()
Exposes the internal array. Be careful when using this function.
Definition: Tuple.h:129
T value_type
Definition: Tuple.h:55
Tuple & operator=(Tuple const &src)
Assignment operator.
Definition: Tuple.h:75
Definition: Tuple.h:53
#define OPENVDB_VERSION_NAME
Definition: version.h:43
Dummy class for tag dispatch of conversion constructors.
Definition: Tuple.h:47
Tuple(Tuple const &src)
Copy constructor. Used when the class signature matches exactly.
Definition: Tuple.h:66
value_type const * asV() const
Exposes the internal array. Be careful when using this function.
Definition: Tuple.h:133
Definition: Exceptions.h:39
T operator[](int i) const
Definition: Tuple.h:102
Tuple< SIZE, T > Abs(const Tuple< SIZE, T > &t)
Definition: Tuple.h:201
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:407
T & operator[](int i)
Definition: Tuple.h:110
void toV(S *v) const
Copies this tuple into an array of a compatible type.
Definition: Tuple.h:122
void read(std::istream &is)
Definition: Tuple.h:159
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
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