OpenVDB  1.1.0
Tuple.h
Go to the documentation of this file.
1 
2 //
3 // Copyright (c) 2012-2013 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 <boost/type_traits/is_integral.hpp>
39 #include "Math.h"
40 
41 
42 namespace openvdb {
44 namespace OPENVDB_VERSION_NAME {
45 namespace math {
46 
49 template<int SIZE, typename T>
50 class Tuple {
51 public:
52  typedef T value_type;
53  typedef T ValueType;
54  enum SIZE_ { size = SIZE };
55 
58  Tuple() {}
59 
61  inline Tuple(Tuple const &src) {
62  for (int i = 0; i < SIZE; ++i) {
63  mm[i] = src.mm[i];
64  }
65  }
66 
73  template <int src_size, typename src_valtype>
74  explicit Tuple(Tuple<src_size, src_valtype> const &src) {
75  static const int copyEnd = SIZE < src_size ? SIZE : src_size;
76 
77  for (int i = 0; i < copyEnd; ++i) {
78  mm[i] = src[i];
79  }
80  for (int i = copyEnd; i < SIZE; ++i) {
81  mm[i] = 0;
82  }
83  }
84 
85  T operator[](int i) const {
86  // we'd prefer to use size_t, but can't because gcc3.2 doesn't like
87  // it - it conflicts with child class conversion operators to
88  // pointer types.
89 // assert(i >= 0 && i < SIZE);
90  return mm[i];
91  }
92 
93  T& operator[](int i) {
94  // see above for size_t vs int
95 // assert(i >= 0 && i < SIZE);
96  return mm[i];
97  }
98 
102 
103 
104  template <typename S>
105  void toV(S *v) const {
106  for (int i = 0; i < SIZE; ++i) {
107  v[i] = mm[i];
108  }
109  }
110 
113  return mm;
114  }
116  value_type const *asV() const {
117  return mm;
118  }
120 
122  std::string
123  str() const {
124  std::ostringstream buffer;
125 
126  buffer << "[";
127 
128  // For each column
129  for (unsigned j(0); j < SIZE; j++) {
130  if (j) buffer << ", ";
131  buffer << mm[j];
132  }
133 
134  buffer << "]";
135 
136  return buffer.str();
137  }
138 
139  void write(std::ostream& os) const {
140  os.write(reinterpret_cast<const char*>(&mm), sizeof(T)*SIZE);
141  }
142  void read(std::istream& is) {
143  is.read(reinterpret_cast<char*>(&mm), sizeof(T)*SIZE);
144  }
145 
146 protected:
147  T mm[SIZE];
148 };
149 
150 
152 
153 
155 template<int SIZE, typename T0, typename T1>
156 bool
157 operator<(const Tuple<SIZE, T0>& t0, const Tuple<SIZE, T1>& t1)
158 {
159  for (size_t i = 0; i < SIZE-1; ++i) {
160  if (!isExactlyEqual(t0[i], t1[i])) return t0[i] < t1[i];
161  }
162  return t0[SIZE-1] < t1[SIZE-1];
163 }
164 
165 
167 template<int SIZE, typename T0, typename T1>
168 bool
170 {
171  for (size_t i = 0; i < SIZE-1; ++i) {
172  if (!isExactlyEqual(t0[i], t1[i])) return t0[i] > t1[i];
173  }
174  return t0[SIZE-1] > t1[SIZE-1];
175 }
176 
177 
179 
180 
182 template<int SIZE, typename T, bool IsInteger>
183 struct TupleAbs {
184  static inline Tuple<SIZE, T> absVal(const Tuple<SIZE, T>& t)
185  {
186  Tuple<SIZE, T> result;
187  for (size_t i = 0; i < SIZE; ++i) result[i] = ::fabs(t[i]);
188  return result;
189  }
190 };
191 
192 // Partial specialization for integer types, using abs() instead of fabs()
193 template<int SIZE, typename T>
194 struct TupleAbs<SIZE, T, /*IsInteger=*/true> {
195  static inline Tuple<SIZE, T> absVal(const Tuple<SIZE, T>& t)
196  {
197  Tuple<SIZE, T> result;
198  for (size_t i = 0; i < SIZE; ++i) result[i] = ::abs(t[i]);
199  return result;
200  }
201 };
202 
203 
205 template<int SIZE, typename T>
206 Tuple<SIZE, T>
208 {
210 }
211 
212 
214 
215 
217 template <int SIZE, typename T>
218 std::ostream& operator<<(std::ostream& ostr, const Tuple<SIZE, T>& classname)
219 {
220  ostr << classname.str();
221  return ostr;
222 }
223 
224 } // namespace math
225 } // namespace OPENVDB_VERSION_NAME
226 } // namespace openvdb
227 
228 #endif // OPENVDB_MATH_TUPLE_HAS_BEEN_INCLUDED
229 
230 // Copyright (c) 2012-2013 DreamWorks Animation LLC
231 // All rights reserved. This software is distributed under the
232 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )