OpenVDB 5.1.0

Tuple.h
Go to the documentation of this file.
1 //
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 //
30 //
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 {
44 OPENVDB_USE_VERSION_NAMESPACE
45 namespace OPENVDB_VERSION_NAME {
46 namespace math {
47
49 struct Conversion {};
50
51
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
65 Tuple() {}
66
68 Tuple(Tuple const& src) {
69 for (int i = 0; i < SIZE; ++i) {
70 mm[i] = src.mm[i];
71 }
72 }
73
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
92 template <int src_size, typename src_valtype>
93 explicit Tuple(Tuple<src_size, src_valtype> const &src) {
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
121
122 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
131 value_type *asV() {
132 return mm;
133 }
135 value_type const *asV() const {
136 return mm;
137 }
139
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
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
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
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
189 bool isZero() const {
190 for (int i = 0; i < SIZE; ++i) {
191 if (!isZero(mm[i])) return false;
192 }
193 return true;
194 }
195
196 protected:
197 T mm[SIZE];
198 };
199
200
202
203
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
217 template<int SIZE, typename T0, typename T1>
218 bool
219 operator>(const Tuple<SIZE, T0>& t0, const Tuple<SIZE, T1>& t1)
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
229
230
232 template<int SIZE, typename T>
233 Tuple<SIZE, T>
234 Abs(const Tuple<SIZE, T>& 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
242 template<int SIZE, typename T>
243 inline bool isNan(const Tuple<SIZE, T>& t) { return t.isNan(); }
244
246 template<int SIZE, typename T>
247 inline bool isInfinite(const Tuple<SIZE, T>& t) { return t.isInfinite(); }
248
250 template<int SIZE, typename T>
251 inline bool isFinite(const Tuple<SIZE, T>& t) { return t.isFinite(); }
252
254 template<int SIZE, typename T>
255 inline bool isZero(const Tuple<SIZE, T>& t) { return t.isZero(); }
256
258
259
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/ )
openvdb::v5_1::math::isExactlyEqual
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:395
openvdb::v5_1::math::Tuple::isFinite
bool isFinite() const
True if no Nan or Inf values are present.
Definition: Tuple.h:181
openvdb::v5_1::math::Tuple::Tuple
Tuple()
Default ctor. Does nothing.
Definition: Tuple.h:65
Math.h
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
openvdb::v5_1::math::Tuple::str
std::string str() const
Definition: Tuple.h:141
openvdb::v5_1::math::Tuple< 2, T >::ValueType
T ValueType
Definition: Tuple.h:58
openvdb::v5_1::math::Tuple::isNan
bool isNan() const
True if a Nan is present in this tuple.
Definition: Tuple.h:165
openvdb::v5_1::math::Tuple::Tuple
Tuple(Tuple< src_size, src_valtype > const &src)
Conversion constructor.
Definition: Tuple.h:93
openvdb::v5_1::math::isZero
bool isZero(const Tuple< SIZE, T > &t)
Return true if all elements are exactly equal to zero.
Definition: Tuple.h:255
openvdb::v5_1::math::Tuple::isInfinite
bool isInfinite() const
True if an Inf is present in this tuple.
Definition: Tuple.h:173
openvdb::v5_1::math::Tuple::operator[]
T & operator[](int i)
Definition: Tuple.h:112
openvdb::v5_1::math::isInfinite
bool isInfinite(const Tuple< SIZE, T > &t)
Return true if an Inf is present in the tuple.
Definition: Tuple.h:247
openvdb::v5_1::math::Abs
Tuple< SIZE, T > Abs(const Tuple< SIZE, T > &t)
Definition: Tuple.h:234
openvdb::v5_1::math::isFinite
bool isFinite(const Tuple< SIZE, T > &t)
Return true if no Nan or Inf values are present.
Definition: Tuple.h:251
openvdb::v5_1::math::Tuple::toV
void toV(S *v) const
Copies this tuple into an array of a compatible type.
Definition: Tuple.h:124
openvdb::v5_1::math::Conversion
Dummy class for tag dispatch of conversion constructors.
Definition: Tuple.h:49
openvdb::v5_1::math::Tuple::read
void read(std::istream &is)
Definition: Tuple.h:160
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:136
openvdb::v5_1::math::Tuple::asV
value_type * asV()
Exposes the internal array. Be careful when using this function.
Definition: Tuple.h:131
openvdb::v5_1::math::isNan
bool isNan(const Tuple< SIZE, T > &t)
Return true if a Nan is present in the tuple.
Definition: Tuple.h:243
openvdb::v5_1::math::Tuple::operator=
Tuple & operator=(Tuple const &src)
Assignment operator.
Definition: Tuple.h:77
openvdb
Definition: Exceptions.h:40
openvdb::v5_1::math::Tuple
Definition: Tuple.h:55
openvdb::v5_1::math::Tuple::isZero
bool isZero() const
True if all elements are exactly zero.
Definition: Tuple.h:189
openvdb::v5_1::math::Tuple::write
void write(std::ostream &os) const
Definition: Tuple.h:157
openvdb::v5_1::math::Tuple::asV
value_type const * asV() const
Exposes the internal array. Be careful when using this function.
Definition: Tuple.h:135
openvdb::v5_1::math::Tuple::mm
T mm[SIZE]
Definition: Tuple.h:197
openvdb::v5_1::math::operator>
bool operator>(const Tuple< SIZE, T0 > &t0, const Tuple< SIZE, T1 > &t1)
Definition: Tuple.h:219
openvdb::v5_1::math::PrintCast
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
openvdb::v5_1::math::Tuple< 2, T >::value_type
T value_type
Definition: Tuple.h:57
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:188
openvdb::v5_1::math::Tuple::operator[]
T operator[](int i) const
Definition: Tuple.h:104
openvdb::v5_1::math::Tuple::Tuple
Tuple(Tuple const &src)
Copy constructor. Used when the class signature matches exactly.
Definition: Tuple.h:68