Skip to content

Commit 30535e5

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 30535e5

17 files changed

Lines changed: 205 additions & 128 deletions

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/Diagnostics.h

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,36 +1098,37 @@ checkFogVolume(const GridType& grid, size_t n)
10981098

10991099
namespace diagnostics_internal {
11001100

1101+
constexpr static auto cmp = [](auto a, auto b) { return math::cwiseLessThan(a, b); };
1102+
template <typename T>
1103+
using SetType = std::set<T, decltype(cmp)>;
11011104

11021105
template<typename TreeType>
11031106
class InactiveVoxelValues
11041107
{
11051108
public:
11061109
using LeafArray = tree::LeafManager<TreeType>;
11071110
using ValueType = typename TreeType::ValueType;
1108-
using SetType = std::set<ValueType>;
11091111

11101112
InactiveVoxelValues(LeafArray&, size_t numValues);
11111113

11121114
void runParallel();
11131115
void runSerial();
11141116

1115-
void getInactiveValues(SetType&) const;
1117+
void getInactiveValues(SetType<ValueType>&) const;
11161118

11171119
inline InactiveVoxelValues(const InactiveVoxelValues<TreeType>&, tbb::split);
11181120
inline void operator()(const tbb::blocked_range<size_t>&);
11191121
inline void join(const InactiveVoxelValues<TreeType>&);
11201122

11211123
private:
11221124
LeafArray& mLeafArray;
1123-
SetType mInactiveValues;
1125+
SetType<ValueType> mInactiveValues {cmp};
11241126
size_t mNumValues;
11251127
};// InactiveVoxelValues
11261128

11271129
template<typename TreeType>
11281130
InactiveVoxelValues<TreeType>::InactiveVoxelValues(LeafArray& leafs, size_t numValues)
11291131
: mLeafArray(leafs)
1130-
, mInactiveValues()
11311132
, mNumValues(numValues)
11321133
{
11331134
}
@@ -1137,7 +1138,6 @@ inline
11371138
InactiveVoxelValues<TreeType>::InactiveVoxelValues(
11381139
const InactiveVoxelValues<TreeType>& rhs, tbb::split)
11391140
: mLeafArray(rhs.mLeafArray)
1140-
, mInactiveValues()
11411141
, mNumValues(rhs.mNumValues)
11421142
{
11431143
}
@@ -1184,7 +1184,7 @@ InactiveVoxelValues<TreeType>::join(const InactiveVoxelValues<TreeType>& rhs)
11841184

11851185
template<typename TreeType>
11861186
inline void
1187-
InactiveVoxelValues<TreeType>::getInactiveValues(SetType& values) const
1187+
InactiveVoxelValues<TreeType>::getInactiveValues(SetType<typename TreeType::ValueType>& values) const
11881188
{
11891189
values.insert(mInactiveValues.begin(), mInactiveValues.end());
11901190
}
@@ -1199,38 +1199,35 @@ class InactiveTileValues
11991199
public:
12001200
using IterRange = tree::IteratorRange<typename TreeType::ValueOffCIter>;
12011201
using ValueType = typename TreeType::ValueType;
1202-
using SetType = std::set<ValueType>;
12031202

12041203
InactiveTileValues(size_t numValues);
12051204

12061205
void runParallel(IterRange&);
12071206
void runSerial(IterRange&);
12081207

1209-
void getInactiveValues(SetType&) const;
1208+
void getInactiveValues(SetType<ValueType>&) const;
12101209

12111210
inline InactiveTileValues(const InactiveTileValues<TreeType>&, tbb::split);
12121211
inline void operator()(const IterRange&);
12131212
inline void join(const InactiveTileValues<TreeType>&);
12141213

12151214
private:
1216-
SetType mInactiveValues;
1215+
SetType<ValueType> mInactiveValues {cmp};
12171216
size_t mNumValues;
12181217
};
12191218

12201219

12211220
template<typename TreeType>
12221221
InactiveTileValues<TreeType>::InactiveTileValues(size_t numValues)
1223-
: mInactiveValues()
1224-
, mNumValues(numValues)
1222+
: mNumValues(numValues)
12251223
{
12261224
}
12271225

12281226
template <typename TreeType>
12291227
inline
12301228
InactiveTileValues<TreeType>::InactiveTileValues(
12311229
const InactiveTileValues<TreeType>& rhs, tbb::split)
1232-
: mInactiveValues()
1233-
, mNumValues(rhs.mNumValues)
1230+
: mNumValues(rhs.mNumValues)
12341231
{
12351232
}
12361233

@@ -1275,7 +1272,7 @@ InactiveTileValues<TreeType>::join(const InactiveTileValues<TreeType>& rhs)
12751272

12761273
template<typename TreeType>
12771274
inline void
1278-
InactiveTileValues<TreeType>::getInactiveValues(SetType& values) const
1275+
InactiveTileValues<TreeType>::getInactiveValues(SetType<typename TreeType::ValueType>& values) const
12791276
{
12801277
values.insert(mInactiveValues.begin(), mInactiveValues.end());
12811278
}
@@ -1294,9 +1291,8 @@ uniqueInactiveValues(const GridType& grid,
12941291
{
12951292
using TreeType = typename GridType::TreeType;
12961293
using ValueType = typename GridType::ValueType;
1297-
using SetType = std::set<ValueType>;
12981294

1299-
SetType uniqueValues;
1295+
diagnostics_internal::SetType<ValueType> uniqueValues {diagnostics_internal::cmp};
13001296

13011297
{ // Check inactive voxels
13021298
TreeType& tree = const_cast<TreeType&>(grid.tree());
@@ -1320,10 +1316,8 @@ uniqueInactiveValues(const GridType& grid,
13201316

13211317
values.clear();
13221318
values.reserve(uniqueValues.size());
1323-
1324-
typename SetType::iterator it = uniqueValues.begin();
1325-
for ( ; it != uniqueValues.end(); ++it) {
1326-
values.push_back(*it);
1319+
for (const auto& item : uniqueValues) {
1320+
values.emplace_back(item);
13271321
}
13281322

13291323
return values.size() <= numValues;

openvdb/openvdb/tools/Interpolation.h

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,21 @@ struct BoxSampler
150150
static inline bool probeValues(ValueT (&data)[N][N][N], const TreeT& inTree, Coord ijk);
151151

152152
/// @brief Find the minimum and maximum values of the eight cell
153-
/// values in @ data.
153+
/// values in @ data. The default *component wise* less than
154+
/// comparison operator is used.
154155
template<class ValueT, size_t N>
155-
static inline void extrema(ValueT (&data)[N][N][N], ValueT& vMin, ValueT& vMax);
156+
static inline void extrema(ValueT (&data)[N][N][N], ValueT& vMin, ValueT& vMax)
157+
{
158+
static auto cmp = [](auto& a, auto& b) { return math::cwiseLessThan(a,b); };
159+
BoxSampler::extrema(data, vMin, vMax, cmp);
160+
}
161+
162+
/// @brief Find the minimum and maximum values of the eight cell
163+
/// values in @ data with a custom comparison operator which returns
164+
/// true if the first argument is less than (i.e. is ordered before)
165+
/// the second.
166+
template<class ValueT, size_t N, typename CompareOpT>
167+
static inline void extrema(ValueT (&data)[N][N][N], ValueT& vMin, ValueT& vMax, const CompareOpT& op);
156168

157169
/// @return the tri-linear interpolation with the unit cell coordinates @a uvw
158170
template<class ValueT, size_t N>
@@ -686,25 +698,25 @@ BoxSampler::probeValues(ValueT (&data)[N][N][N], const TreeT& inTree, Coord ijk)
686698
return hasActiveValues;
687699
}
688700

689-
template<class ValueT, size_t N>
701+
template<class ValueT, size_t N, typename CompareOpT>
690702
inline void
691-
BoxSampler::extrema(ValueT (&data)[N][N][N], ValueT& vMin, ValueT &vMax)
703+
BoxSampler::extrema(ValueT (&data)[N][N][N], ValueT& vMin, ValueT &vMax, const CompareOpT& op)
692704
{
693705
vMin = vMax = data[0][0][0];
694-
vMin = math::Min(vMin, data[0][0][1]);
695-
vMax = math::Max(vMax, data[0][0][1]);
696-
vMin = math::Min(vMin, data[0][1][0]);
697-
vMax = math::Max(vMax, data[0][1][0]);
698-
vMin = math::Min(vMin, data[0][1][1]);
699-
vMax = math::Max(vMax, data[0][1][1]);
700-
vMin = math::Min(vMin, data[1][0][0]);
701-
vMax = math::Max(vMax, data[1][0][0]);
702-
vMin = math::Min(vMin, data[1][0][1]);
703-
vMax = math::Max(vMax, data[1][0][1]);
704-
vMin = math::Min(vMin, data[1][1][0]);
705-
vMax = math::Max(vMax, data[1][1][0]);
706-
vMin = math::Min(vMin, data[1][1][1]);
707-
vMax = math::Max(vMax, data[1][1][1]);
706+
vMin = op(vMin, data[0][0][1]) ? vMin : data[0][0][1];
707+
vMax = op(vMax, data[0][0][1]) ? data[0][0][1] : vMax;
708+
vMin = op(vMin, data[0][1][0]) ? vMin : data[0][1][0];
709+
vMax = op(vMax, data[0][1][0]) ? data[0][1][0] : vMax;
710+
vMin = op(vMin, data[0][1][1]) ? vMin : data[0][1][1];
711+
vMax = op(vMax, data[0][1][1]) ? data[0][1][1] : vMax;
712+
vMin = op(vMin, data[1][0][0]) ? vMin : data[1][0][0];
713+
vMax = op(vMax, data[1][0][0]) ? data[1][0][0] : vMax;
714+
vMin = op(vMin, data[1][0][1]) ? vMin : data[1][0][1];
715+
vMax = op(vMax, data[1][0][1]) ? data[1][0][1] : vMax;
716+
vMin = op(vMin, data[1][1][0]) ? vMin : data[1][1][0];
717+
vMax = op(vMax, data[1][1][0]) ? data[1][1][0] : vMax;
718+
vMin = op(vMin, data[1][1][1]) ? vMin : data[1][1][1];
719+
vMax = op(vMax, data[1][1][1]) ? data[1][1][1] : vMax;
708720
}
709721

710722

0 commit comments

Comments
 (0)