OpenVDB: Tuple.h Source File

OpenVDB 3.1.0

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

Tuple.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2015 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 {
43 OPENVDB_USE_VERSION_NAMESPACE
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
55 static const int size = SIZE;
56
59 Tuple() {}
60
62 inline Tuple(Tuple const &src) {
63 for (int i = 0; i < SIZE; ++i) {
64 mm[i] = src.mm[i];
65 }
66 }
67
74 template <int src_size, typename src_valtype>
75 explicit Tuple(Tuple<src_size, src_valtype> const &src) {
76 enum { COPY_END = (SIZE < src_size ? SIZE : src_size) };
77
78 for (int i = 0; i < COPY_END; ++i) {
79 mm[i] = src[i];
80 }
81 for (int i = COPY_END; i < SIZE; ++i) {
82 mm[i] = 0;
83 }
84 }
85
86 T operator[](int i) const {
87 // we'd prefer to use size_t, but can't because gcc3.2 doesn't like
88 // it - it conflicts with child class conversion operators to
89 // pointer types.
90 // assert(i >= 0 && i < SIZE);
91 return mm[i];
92 }
93
94 T& operator[](int i) {
95 // see above for size_t vs int
96 // assert(i >= 0 && i < SIZE);
97 return mm[i];
98 }
99
103
104 template <typename S>
106 void toV(S *v) const {
107 for (int i = 0; i < SIZE; ++i) {
108 v[i] = mm[i];
109 }
110 }
111
113 value_type *asV() {
114 return mm;
115 }
117 value_type const *asV() const {
118 return mm;
119 }
121
123 std::string
124 str() const {
125 std::ostringstream buffer;
126
127 buffer << "[";
128
129 // For each column
130 for (unsigned j(0); j < SIZE; j++) {
131 if (j) buffer << ", ";
132 buffer << mm[j];
133 }
134
135 buffer << "]";
136
137 return buffer.str();
138 }
139
140 void write(std::ostream& os) const {
141 os.write(reinterpret_cast<const char*>(&mm), sizeof(T)*SIZE);
142 }
143 void read(std::istream& is) {
144 is.read(reinterpret_cast<char*>(&mm), sizeof(T)*SIZE);
145 }
146
147 protected:
148 T mm[SIZE];
149 };
150
151
153
154
156 template<int SIZE, typename T0, typename T1>
157 bool
158 operator<(const Tuple<SIZE, T0>& t0, const Tuple<SIZE, T1>& t1)
159 {
160 for (int i = 0; i < SIZE-1; ++i) {
161 if (!isExactlyEqual(t0[i], t1[i])) return t0[i] < t1[i];
162 }
163 return t0[SIZE-1] < t1[SIZE-1];
164 }
165
166
168 template<int SIZE, typename T0, typename T1>
169 bool
170 operator>(const Tuple<SIZE, T0>& t0, const Tuple<SIZE, T1>& t1)
171 {
172 for (int i = 0; i < SIZE-1; ++i) {
173 if (!isExactlyEqual(t0[i], t1[i])) return t0[i] > t1[i];
174 }
175 return t0[SIZE-1] > t1[SIZE-1];
176 }
177
178
180
181
183 template<int SIZE, typename T>
184 Tuple<SIZE, T>
185 Abs(const Tuple<SIZE, T>& t)
186 {
187 Tuple<SIZE, T> result;
188 for (int i = 0; i < SIZE; ++i) result[i] = math::Abs(t[i]);
189 return result;
190 }
191
192
194
195
197 template <int SIZE, typename T>
198 std::ostream& operator<<(std::ostream& ostr, const Tuple<SIZE, T>& classname)
199 {
200 ostr << classname.str();
201 return ostr;
202 }
203
204 } // namespace math
205 } // namespace OPENVDB_VERSION_NAME
206 } // namespace openvdb
207
208 #endif // OPENVDB_MATH_TUPLE_HAS_BEEN_INCLUDED
209
210 // Copyright (c) 2012-2015 DreamWorks Animation LLC
211 // All rights reserved. This software is distributed under the
212 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
openvdb::v3_1_0::math::Tuple::Tuple
Tuple()
Definition: Tuple.h:59
openvdb::v3_1_0::math::Tuple::ValueType
T ValueType
Definition: Tuple.h:53
Math.h
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
openvdb::v3_1_0::math::Tuple::write
void write(std::ostream &os) const
Definition: Tuple.h:140
openvdb::v3_1_0::math::isExactlyEqual
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:407
openvdb::v3_1_0::math::Tuple::asV
value_type * asV()
Exposes the internal array. Be careful when using this function.
Definition: Tuple.h:113
openvdb::v3_1_0::math::Tuple::asV
value_type const * asV() const
Exposes the internal array. Be careful when using this function.
Definition: Tuple.h:117
openvdb::v3_1_0::math::Tuple::operator[]
T operator[](int i) const
Definition: Tuple.h:86
openvdb::v3_1_0::math::Tuple::toV
void toV(S *v) const
Copies this tuple into an array of a compatible type.
Definition: Tuple.h:106
openvdb::v3_1_0::math::operator>
bool operator>(const Tuple< SIZE, T0 > &t0, const Tuple< SIZE, T1 > &t1)
Definition: Tuple.h:170
openvdb::v3_1_0::math::Tuple::Tuple
Tuple(Tuple< src_size, src_valtype > const &src)
Definition: Tuple.h:75
openvdb::v3_1_0::math::Tuple
Definition: Tuple.h:50
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
Definition: version.h:43
openvdb::v3_1_0::math::Tuple::Tuple
Tuple(Tuple const &src)
Copy constructor. Used when the class signature matches exactly.
Definition: Tuple.h:62
openvdb
Definition: Exceptions.h:39
openvdb::v3_1_0::math::Tuple::mm
T mm[SIZE]
Definition: Tuple.h:148
openvdb::v3_1_0::math::Abs
Tuple< SIZE, T > Abs(const Tuple< SIZE, T > &t)
Definition: Tuple.h:185
openvdb::v3_1_0::math::Tuple::read
void read(std::istream &is)
Definition: Tuple.h:143
openvdb::v3_1_0::math::Tuple::value_type
T value_type
Definition: Tuple.h:52
openvdb::v3_1_0::math::Tuple::operator[]
T & operator[](int i)
Definition: Tuple.h:94
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
openvdb::v3_1_0::math::Tuple::str
std::string str() const
Definition: Tuple.h:124