Skip to content

Commit 25f1686

Browse files
committed
Removed Tuple < and > operators - added explicit overloads for cwiseLessThan and cwiseGreaterThan
Signed-off-by: Nick Avramoussis <4256455+Idclip@users.noreply.github.com>
1 parent bd518a4 commit 25f1686

14 files changed

Lines changed: 152 additions & 88 deletions

File tree

openvdb/openvdb/math/Math.h

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -354,30 +354,27 @@ isZero(const Type& x)
354354
OPENVDB_NO_FP_EQUALITY_WARNING_END
355355
}
356356

357-
358-
/// @brief Return @c true if @a x is equal to zero to within
359-
/// the default floating-point comparison tolerance.
357+
/// Return @c true if @a x is equal to zero to within the given tolerance.
360358
template<typename Type>
361359
inline bool
362-
isApproxZero(const Type& x)
360+
isApproxZero(const Type& x, const Type& tolerance)
363361
{
364-
const Type tolerance = Type(zeroVal<Type>() + Tolerance<Type>::value());
365-
return !(x > tolerance) && !(x < -tolerance);
362+
return !cwiseGreaterThan(x, tolerance) && !cwiseLessThan(x, -tolerance);
366363
}
367364

368-
/// Return @c true if @a x is equal to zero to within the given tolerance.
365+
/// @brief Return @c true if @a x is equal to zero to within
366+
/// the default floating-point comparison tolerance.
369367
template<typename Type>
370368
inline bool
371-
isApproxZero(const Type& x, const Type& tolerance)
369+
isApproxZero(const Type& x)
372370
{
373-
return !(x > tolerance) && !(x < -tolerance);
371+
return isApproxZero(x, Type(zeroVal<Type>() + Tolerance<Type>::value()));
374372
}
375373

376-
377374
/// Return @c true if @a x is less than zero.
378375
template<typename Type>
379376
inline bool
380-
isNegative(const Type& x) { return x < zeroVal<Type>(); }
377+
isNegative(const Type& x) { return cwiseLessThan(x, zeroVal<Type>()); }
381378

382379
// Return false, since bool values are never less than zero.
383380
template<> inline bool isNegative<bool>(const bool&) { return false; }

openvdb/openvdb/math/Stencils.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,10 @@ class BaseStencil
124124
OPENVDB_ASSERT(!tmp.empty());
125125
size_t midpoint = (tmp.size() - 1) >> 1;
126126
// Partially sort the vector until the median value is at the midpoint.
127-
#if !defined(_MSC_VER) || _MSC_VER < 1924
128-
std::nth_element(tmp.begin(), tmp.begin() + midpoint, tmp.end());
129-
#else
130-
// Workaround MSVC bool warning C4804 unsafe use of type 'bool'
131127
std::nth_element(tmp.begin(), tmp.begin() + midpoint, tmp.end(),
132-
std::less<ValueType>());
133-
#endif
128+
[](const auto& a, const auto& b) {
129+
return math::cwiseLessThan(a, b);
130+
});
134131
return tmp[midpoint];
135132
}
136133

openvdb/openvdb/math/Tuple.h

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -170,36 +170,21 @@ class Tuple
170170
T mm[SIZE];
171171
};
172172

173+
///////////////////////////////////////////////////////////////////////////
173174

174-
////////////////////////////////////////
175-
176-
177-
/// @return true if t0 < t1, comparing components in order of significance.
178-
template<int SIZE, typename T0, typename T1>
179-
bool
180-
operator<(const Tuple<SIZE, T0>& t0, const Tuple<SIZE, T1>& t1)
175+
template<int SIZE>
176+
inline Tuple<SIZE, bool> operator&&(const Tuple<SIZE, bool> a, const Tuple<SIZE, bool> b)
181177
{
182-
for (int i = 0; i < SIZE-1; ++i) {
183-
if (!isExactlyEqual(t0[i], t1[i])) return t0[i] < t1[i];
184-
}
185-
return t0[SIZE-1] < t1[SIZE-1];
178+
return a & b;
186179
}
187180

188-
189-
/// @return true if t0 > t1, comparing components in order of significance.
190-
template<int SIZE, typename T0, typename T1>
191-
bool
192-
operator>(const Tuple<SIZE, T0>& t0, const Tuple<SIZE, T1>& t1)
181+
template<int SIZE>
182+
inline Tuple<SIZE, bool> operator||(const Tuple<SIZE, bool> a, const Tuple<SIZE, bool> b)
193183
{
194-
for (int i = 0; i < SIZE-1; ++i) {
195-
if (!isExactlyEqual(t0[i], t1[i])) return t0[i] > t1[i];
196-
}
197-
return t0[SIZE-1] > t1[SIZE-1];
184+
return a | b;
198185
}
199186

200-
201-
////////////////////////////////////////
202-
187+
///////////////////////////////////////////////////////////////////////////
203188

204189
/// @return the absolute value of the given Tuple.
205190
template<int SIZE, typename T>

openvdb/openvdb/math/Vec2.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,23 @@ inline Vec2<T> Exp(Vec2<T> v) { return v.exp(); }
527527
template <typename T>
528528
inline Vec2<T> Log(Vec2<T> v) { return v.log(); }
529529

530+
/// @brief Componentwise less than for Vec2 types.
531+
template <typename T1, typename T2>
532+
inline bool cwiseLessThan(const Vec2<T1>& a, const Vec2<T2>& b)
533+
{
534+
if (!isExactlyEqual(a[0], b[0])) return a[0] < b[0];
535+
return a[1] < b[1];
536+
}
537+
538+
/// @brief Componentwise greater than for Vec2 types.
539+
template <typename T1, typename T2>
540+
inline bool cwiseGreaterThan(const Vec2<T1>& a, const Vec2<T2>& b)
541+
{
542+
if (!isExactlyEqual(a[0], b[0])) return a[0] > b[0];
543+
return a[1] > b[1];
544+
}
545+
546+
530547
using Vec2i = Vec2<int32_t>;
531548
using Vec2ui = Vec2<uint32_t>;
532549
using Vec2s = Vec2<float>;

openvdb/openvdb/math/Vec3.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,26 @@ inline Vec3<T> Exp(Vec3<T> v) { return v.exp(); }
659659
template <typename T>
660660
inline Vec3<T> Log(Vec3<T> v) { return v.log(); }
661661

662+
/// @brief Componentwise less than for Vec3 types.
663+
template <typename T1, typename T2>
664+
inline bool cwiseLessThan(const Vec3<T1>& a, const Vec3<T2>& b)
665+
{
666+
for (int i = 0; i < 2; ++i) {
667+
if (!isExactlyEqual(a[i], b[i])) return a[i] < b[i];
668+
}
669+
return a[2] < b[2];
670+
}
671+
672+
/// @brief Componentwise greater than for Vec3 types.
673+
template <typename T1, typename T2>
674+
inline bool cwiseGreaterThan(const Vec3<T1>& a, const Vec3<T2>& b)
675+
{
676+
for (int i = 0; i < 2; ++i) {
677+
if (!isExactlyEqual(a[i], b[i])) return a[i] > b[i];
678+
}
679+
return a[2] > b[2];
680+
}
681+
662682
using Vec3i = Vec3<int32_t>;
663683
using Vec3ui = Vec3<uint32_t>;
664684
using Vec3s = Vec3<float>;

openvdb/openvdb/math/Vec4.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,26 @@ inline Vec4<T> Exp(Vec4<T> v) { return v.exp(); }
556556
template <typename T>
557557
inline Vec4<T> Log(Vec4<T> v) { return v.log(); }
558558

559+
/// @brief Componentwise less than for Vec4 types.
560+
template <typename T1, typename T2>
561+
inline bool cwiseLessThan(const Vec4<T1>& a, const Vec4<T2>& b)
562+
{
563+
for (int i = 0; i < 3; ++i) {
564+
if (!isExactlyEqual(a[i], b[i])) return a[i] < b[i];
565+
}
566+
return a[3] < b[3];
567+
}
568+
569+
/// @brief Componentwise greater than for Vec4 types.
570+
template <typename T1, typename T2>
571+
inline bool cwiseGreaterThan(const Vec4<T1>& a, const Vec4<T2>& b)
572+
{
573+
for (int i = 0; i < 3; ++i) {
574+
if (!isExactlyEqual(a[i], b[i])) return a[i] > b[i];
575+
}
576+
return a[3] > b[3];
577+
}
578+
559579
using Vec4i = Vec4<int32_t>;
560580
using Vec4ui = Vec4<uint32_t>;
561581
using Vec4s = Vec4<float>;

openvdb/openvdb/tools/Composite.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,15 @@ const typename std::enable_if<VecTraits<T>::IsVec, T>::type& // = T if T is a ve
116116
min(const T& a, const T& b)
117117
{
118118
const typename T::ValueType aMag = a.lengthSqr(), bMag = b.lengthSqr();
119-
return (aMag < bMag ? a : (bMag < aMag ? b : std::min(a, b)));
119+
return (aMag < bMag ? a : (bMag < aMag ? b : math::cwiseLessThan(a, b) ? a : b));
120120
}
121121

122122
template<typename T> inline
123123
const typename std::enable_if<VecTraits<T>::IsVec, T>::type&
124124
max(const T& a, const T& b)
125125
{
126126
const typename T::ValueType aMag = a.lengthSqr(), bMag = b.lengthSqr();
127-
return (aMag < bMag ? b : (bMag < aMag ? a : std::max(a, b)));
127+
return (aMag < bMag ? b : (bMag < aMag ? a : math::cwiseGreaterThan(a, b) ? a : b));
128128
}
129129

130130

openvdb/openvdb/tools/Prune.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,9 @@ class TolerancePruneOp
231231
using UnionT = typename NodeT::UnionType;
232232
UnionT* data = const_cast<UnionT*>(node.getTable());//never do this at home kids :)
233233
static const size_t midpoint = (NodeT::NUM_VALUES - 1) >> 1;
234-
auto op = [](const UnionT& a, const UnionT& b){return a.getValue() < b.getValue();};
234+
auto op = [](const UnionT& a, const UnionT& b) {
235+
return math::cwiseLessThan(a.getValue(), b.getValue());
236+
};
235237
std::nth_element(data, data + midpoint, data + NodeT::NUM_VALUES, op);
236238
return data[midpoint].getValue();
237239
}

openvdb/openvdb/tools/ValueTransformer.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,18 @@ template<typename ValueType>
237237
struct MinOp {
238238
const ValueType val;
239239
MinOp(const ValueType& v): val(v) {}
240-
inline void operator()(ValueType& v) const { v = std::min<ValueType>(v, val); }
240+
inline void operator()(ValueType& v) const {
241+
if (math::cwiseLessThan(val, v)) v = val;
242+
}
241243
};
242244

243245
template<typename ValueType>
244246
struct MaxOp {
245247
const ValueType val;
246248
MaxOp(const ValueType& v): val(v) {}
247-
inline void operator()(ValueType& v) const { v = std::max<ValueType>(v, val); }
249+
inline void operator()(ValueType& v) const {
250+
if (math::cwiseGreaterThan(val, v)) v = val;
251+
}
248252
};
249253

250254
template<typename ValueType>

openvdb/openvdb/tree/InternalNode.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,16 +1723,15 @@ InternalNode<ChildT, Log2Dim>::isConstant(ValueType& minValue,
17231723
bool& state,
17241724
const ValueType& tolerance) const
17251725
{
1726-
17271726
if (!mChildMask.isOff() || !mValueMask.isConstant(state)) return false;// early termination
17281727
minValue = maxValue = mNodes[0].getValue();
17291728
for (Index i = 1; i < NUM_VALUES; ++i) {
17301729
const ValueType& v = mNodes[i].getValue();
1731-
if (v < minValue) {
1732-
if ((maxValue - v) > tolerance) return false;// early termination
1730+
if (math::cwiseLessThan(v, minValue)) {
1731+
if (math::cwiseGreaterThan((maxValue - v), tolerance)) return false;// early termination
17331732
minValue = v;
1734-
} else if (v > maxValue) {
1735-
if ((v - minValue) > tolerance) return false;// early termination
1733+
} else if (math::cwiseGreaterThan(v, maxValue)) {
1734+
if (math::cwiseGreaterThan((v - minValue), tolerance)) return false;// early termination
17361735
maxValue = v;
17371736
}
17381737
}

0 commit comments

Comments
 (0)