OpenVDB: Tuple.h Source File

OpenVDB 4.0.2

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 "Math.h"
38 #include <cmath>
39 #include <sstream>
40
41
42 namespace openvdb {
43 OPENVDB_USE_VERSION_NAMESPACE
44 namespace OPENVDB_VERSION_NAME {
45 namespace math {
46
48 struct Conversion {};
49
50
53 template<int SIZE, typename T>
54 class Tuple {
55 public:
56 typedef T value_type;
57 typedef T ValueType;
58
59 static const int size = SIZE;
60
64 Tuple() {}
65
67 Tuple(Tuple const& src) {
68 for (int i = 0; i < SIZE; ++i) {
69 mm[i] = src.mm[i];
70 }
71 }
72
76 Tuple& operator=(Tuple const& src) {
77 if (&src != this) {
78 for (int i = 0; i < SIZE; ++i) {
79 mm[i] = src.mm[i];
80 }
81 }
82 return *this;
83 }
84
91 template <int src_size, typename src_valtype>
92 explicit Tuple(Tuple<src_size, src_valtype> const &src) {
93 enum { COPY_END = (SIZE < src_size ? SIZE : src_size) };
94
95 for (int i = 0; i < COPY_END; ++i) {
96 mm[i] = src[i];
97 }
98 for (int i = COPY_END; i < SIZE; ++i) {
99 mm[i] = 0;
100 }
101 }
102
103 T operator[](int i) const {
104 // we'd prefer to use size_t, but can't because gcc3.2 doesn't like
105 // it - it conflicts with child class conversion operators to
106 // pointer types.
107 // assert(i >= 0 && i < SIZE);
108 return mm[i];
109 }
110
111 T& operator[](int i) {
112 // see above for size_t vs int
113 // assert(i >= 0 && i < SIZE);
114 return mm[i];
115 }
116
120
121 template <typename S>
123 void toV(S *v) const {
124 for (int i = 0; i < SIZE; ++i) {
125 v[i] = mm[i];
126 }
127 }
128
130 value_type *asV() {
131 return mm;
132 }
134 value_type const *asV() const {
135 return mm;
136 }
138
140 std::string 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 << PrintCast(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
164 bool isNan() const {
165 for (int i = 0; i < SIZE; ++i) {
166 if (std::isnan(mm[i])) return true;
167 }
168 return false;
169 }
170
172 bool isInfinite() const {
173 for (int i = 0; i < SIZE; ++i) {
174 if (std::isinf(mm[i])) return true;
175 }
176 return false;
177 }
178
180 bool isFinite() const {
181 for (int i = 0; i < SIZE; ++i) {
182 if (!std::isfinite(mm[i])) return false;
183 }
184 return true;
185 }
186
188 bool isZero() const {
189 for (int i = 0; i < SIZE; ++i) {
190 if (!isZero(mm[i])) return false;
191 }
192 return true;
193 }
194
195 protected:
196 T mm[SIZE];
197 };
198
199
201
202
204 template<int SIZE, typename T0, typename T1>
205 bool
206 operator<(const Tuple<SIZE, T0>& t0, const Tuple<SIZE, T1>& t1)
207 {
208 for (int i = 0; i < SIZE-1; ++i) {
209 if (!isExactlyEqual(t0[i], t1[i])) return t0[i] < t1[i];
210 }
211 return t0[SIZE-1] < t1[SIZE-1];
212 }
213
214
216 template<int SIZE, typename T0, typename T1>
217 bool
218 operator>(const Tuple<SIZE, T0>& t0, const Tuple<SIZE, T1>& t1)
219 {
220 for (int i = 0; i < SIZE-1; ++i) {
221 if (!isExactlyEqual(t0[i], t1[i])) return t0[i] > t1[i];
222 }
223 return t0[SIZE-1] > t1[SIZE-1];
224 }
225
226
228
229
231 template<int SIZE, typename T>
232 Tuple<SIZE, T>
233 Abs(const Tuple<SIZE, T>& t)
234 {
235 Tuple<SIZE, T> result;
236 for (int i = 0; i < SIZE; ++i) result[i] = math::Abs(t[i]);
237 return result;
238 }
239
241 template<int SIZE, typename T>
242 inline bool isNan(const Tuple<SIZE, T>& t) { return t.isNan(); }
243
245 template<int SIZE, typename T>
246 inline bool isInfinite(const Tuple<SIZE, T>& t) { return t.isInfinite(); }
247
249 template<int SIZE, typename T>
250 inline bool isFinite(const Tuple<SIZE, T>& t) { return t.isFinite(); }
251
253 template<int SIZE, typename T>
254 inline bool isZero(const Tuple<SIZE, T>& t) { return t.isZero(); }
255
257
258
260 template <int SIZE, typename T>
261 std::ostream& operator<<(std::ostream& ostr, const Tuple<SIZE, T>& classname)
262 {
263 ostr << classname.str();
264 return ostr;
265 }
266
267 } // namespace math
268 } // namespace OPENVDB_VERSION_NAME
269 } // namespace openvdb
270
271 #endif // OPENVDB_MATH_TUPLE_HAS_BEEN_INCLUDED
272
273 // Copyright (c) 2012-2017 DreamWorks Animation LLC
274 // All rights reserved. This software is distributed under the
275 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
openvdb::v4_0_2::math::Tuple::Tuple
Tuple(Tuple< src_size, src_valtype > const &src)
Conversion constructor.
Definition: Tuple.h:92
openvdb::v4_0_2::math::Tuple::operator[]
T operator[](int i) const
Definition: Tuple.h:103
openvdb::v4_0_2::math::operator>
bool operator>(const Tuple< SIZE, T0 > &t0, const Tuple< SIZE, T1 > &t1)
Definition: Tuple.h:218
openvdb::v4_0_2::math::Conversion
Dummy class for tag dispatch of conversion constructors.
Definition: Tuple.h:48
openvdb::v4_0_2::math::Tuple::isInfinite
bool isInfinite() const
True if an Inf is present in this tuple.
Definition: Tuple.h:172
openvdb::v4_0_2::math::Tuple::isZero
bool isZero() const
True if all elements are exactly zero.
Definition: Tuple.h:188
Math.h
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
openvdb::v4_0_2::math::Tuple::toV
void toV(S *v) const
Copies this tuple into an array of a compatible type.
Definition: Tuple.h:123
openvdb::v4_0_2::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:833
openvdb::v4_0_2::math::Tuple::ValueType
T ValueType
Definition: Tuple.h:57
openvdb::v4_0_2::math::Tuple::Tuple
Tuple()
Default ctor. Does nothing.
Definition: Tuple.h:64
openvdb::v4_0_2::math::isExactlyEqual
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:391
openvdb::v4_0_2::math::isNan
bool isNan(const Tuple< SIZE, T > &t)
Return true if a Nan is present in the tuple.
Definition: Tuple.h:242
openvdb::v4_0_2::math::isFinite
bool isFinite(const Tuple< SIZE, T > &t)
Return true if no Nan or Inf values are present.
Definition: Tuple.h:250
i
tbb::atomic< Index32 > i
Definition: LeafBuffer.h:71
openvdb::v4_0_2::math::Tuple::mm
T mm[SIZE]
Definition: Tuple.h:196
openvdb::v4_0_2::math::Tuple::str
std::string str() const
Definition: Tuple.h:140
openvdb::v4_0_2::math::Tuple::write
void write(std::ostream &os) const
Definition: Tuple.h:156
openvdb::v4_0_2::math::isZero
bool isZero(const Tuple< SIZE, T > &t)
Return true if all elements are exactly equal to zero.
Definition: Tuple.h:254
openvdb::v4_0_2::math::isInfinite
bool isInfinite(const Tuple< SIZE, T > &t)
Return true if an Inf is present in the tuple.
Definition: Tuple.h:246
openvdb::v4_0_2::math::Tuple::value_type
T value_type
Definition: Tuple.h:56
openvdb::v4_0_2::math::Tuple::operator=
Tuple & operator=(Tuple const &src)
Assignment operator.
Definition: Tuple.h:76
openvdb::v4_0_2::math::Tuple::asV
value_type * asV()
Exposes the internal array. Be careful when using this function.
Definition: Tuple.h:130
openvdb::v4_0_2::math::Tuple::asV
value_type const * asV() const
Exposes the internal array. Be careful when using this function.
Definition: Tuple.h:134
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
Definition: version.h:43
openvdb::v4_0_2::math::Tuple::Tuple
Tuple(Tuple const &src)
Copy constructor. Used when the class signature matches exactly.
Definition: Tuple.h:67
openvdb::v4_0_2::math::Tuple::isFinite
bool isFinite() const
True if no Nan or Inf values are present.
Definition: Tuple.h:180
openvdb
Definition: Exceptions.h:39
openvdb::v4_0_2::math::Tuple::isNan
bool isNan() const
True if a Nan is present in this tuple.
Definition: Tuple.h:164
openvdb::v4_0_2::math::Tuple::operator[]
T & operator[](int i)
Definition: Tuple.h:111
openvdb::v4_0_2::math::Abs
Tuple< SIZE, T > Abs(const Tuple< SIZE, T > &t)
Definition: Tuple.h:233
openvdb::v4_0_2::math::Tuple::read
void read(std::istream &is)
Definition: Tuple.h:159
openvdb::v4_0_2::math::Tuple
Definition: Tuple.h:54
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
size
static constexpr size_t size
The size of a LeafBuffer when LeafBuffer::mOutOfCore is atomic.
Definition: LeafBuffer.h:85