OpenVDB 7.0.0

Statistics.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3 //
8
9 #ifndef OPENVDB_TOOLS_STATISTICS_HAS_BEEN_INCLUDED
10 #define OPENVDB_TOOLS_STATISTICS_HAS_BEEN_INCLUDED
11
12 #include <openvdb/Types.h>
13 #include <openvdb/Exceptions.h>
14 #include <openvdb/math/Stats.h>
15 #include "ValueTransformer.h"
16
17
18 namespace openvdb {
19 OPENVDB_USE_VERSION_NAMESPACE
20 namespace OPENVDB_VERSION_NAME {
21 namespace tools {
22
32 template<typename IterT>
33 inline math::Histogram
34 histogram(const IterT& iter, double minVal, double maxVal,
35 size_t numBins = 10, bool threaded = true);
36
43 template<typename IterT>
44 inline math::Extrema
45 extrema(const IterT& iter, bool threaded = true);
46
53 template<typename IterT>
54 inline math::Stats
55 statistics(const IterT& iter, bool threaded = true);
56
86 template<typename IterT, typename ValueOp>
87 inline math::Extrema
88 extrema(const IterT& iter, const ValueOp& op, bool threaded);
89
119 template<typename IterT, typename ValueOp>
120 inline math::Stats
121 statistics(const IterT& iter, const ValueOp& op, bool threaded);
122
123
186 template<typename OperatorT, typename IterT>
187 inline math::Stats
188 opStatistics(const IterT& iter, const OperatorT& op = OperatorT(), bool threaded = true);
189
191 template<typename OperatorT, typename IterT>
192 inline math::Extrema
193 opExtrema(const IterT& iter, const OperatorT& op = OperatorT(), bool threaded = true);
194
196
197
198 namespace stats_internal {
199
203 template<typename IterT, typename AuxT = void>
204 struct IterTraits {
205 using ValueType = typename IterT::ValueType;
206 };
207
208 template<typename TreeT, typename ValueIterT>
209 struct IterTraits<tree::TreeValueIteratorBase<TreeT, ValueIterT> > {
210 using ValueType = typename tree::TreeValueIteratorBase<TreeT, ValueIterT>::ValueT;
211 };
212
213
214 // Helper class to compute a scalar value from either a scalar or a vector value
215 // (the latter by computing the vector's magnitude)
216 template<typename T, bool IsVector> struct GetValImpl;
217
218 template<typename T>
219 struct GetValImpl<T, /*IsVector=*/false> {
220 static inline double get(const T& val) { return double(val); }
221 };
222
223 template<typename T>
224 struct GetValImpl<T, /*IsVector=*/true> {
225 static inline double get(const T& val) { return val.length(); }
226 };
227
228
229 // Helper class to compute a scalar value from a tree or node iterator
230 // that points to a value in either a scalar or a vector grid, and to
231 // add that value to a math::Stats object.
232 template<typename IterT, typename StatsT>
233 struct GetVal
234 {
235 using ValueT = typename IterTraits<IterT>::ValueType;
236 using ImplT = GetValImpl<ValueT, VecTraits<ValueT>::IsVec>;
237
238 inline void operator()(const IterT& iter, StatsT& stats) const {
239 if (iter.isVoxelValue()) stats.add(ImplT::get(*iter));
240 else stats.add(ImplT::get(*iter), iter.getVoxelCount());
241 }
242 };
243
244 // Helper class to accumulate scalar voxel values or vector voxel magnitudes
245 // into a math::Stats object
246 template<typename IterT, typename ValueOp, typename StatsT>
247 struct StatsOp
248 {
249 StatsOp(const ValueOp& op): getValue(op) {}
250
251 // Accumulate voxel and tile values into this functor's Stats object.
252 inline void operator()(const IterT& iter) { getValue(iter, stats); }
253
254 // Accumulate another functor's Stats object into this functor's.
255 inline void join(StatsOp& other) { stats.add(other.stats); }
256
257 StatsT stats;
258 ValueOp getValue;
259 };
260
261
262 // Helper class to accumulate scalar voxel values or vector voxel magnitudes
263 // into a math::Histogram object
264 template<typename IterT, typename ValueOp>
265 struct HistOp
266 {
267 HistOp(const ValueOp& op, double vmin, double vmax, size_t bins):
268 hist(vmin, vmax, bins), getValue(op)
269 {}
270
271 // Accumulate voxel and tile values into this functor's Histogram object.
272 inline void operator()(const IterT& iter) { getValue(iter, hist); }
273
274 // Accumulate another functor's Histogram object into this functor's.
275 inline void join(HistOp& other) { hist.add(other.hist); }
276
277 math::Histogram hist;
278 ValueOp getValue;
279 };
280
281
282 // Helper class to apply an operator such as math::Gradient or math::Laplacian
283 // to voxels and accumulate the scalar results or the magnitudes of vector results
284 // into a math::Stats object
285 template<typename IterT, typename OpT, typename StatsT>
286 struct MathOp
287 {
288 using TreeT = typename IterT::TreeT;
289 using ValueT = typename TreeT::ValueType;
290 using ConstAccessor = typename tree::ValueAccessor<const TreeT>;
291
292 // Each thread gets its own accessor and its own copy of the operator.
293 ConstAccessor mAcc;
294 OpT mOp;
295 StatsT mStats;
296
297 template<typename TreeT>
298 static inline TreeT* THROW_IF_NULL(TreeT* ptr) {
299 if (ptr == nullptr) OPENVDB_THROW(ValueError, "iterator references a null tree");
300 return ptr;
301 }
302
303 MathOp(const IterT& iter, const OpT& op):
304 mAcc(*THROW_IF_NULL(iter.getTree())), mOp(op)
305 {}
306
307 // Accumulate voxel and tile values into this functor's Stats object.
308 void operator()(const IterT& it)
309 {
310 if (it.isVoxelValue()) {
311 // Add the magnitude of the gradient at a single voxel.
312 mStats.add(mOp.result(mAcc, it.getCoord()));
313 } else {
314 // Iterate over the voxels enclosed by a tile and add the results
315 // of applying the operator at each voxel.
319 CoordBBox bbox = it.getBoundingBox();
320 Coord xyz;
321 int &x = xyz.x(), &y = xyz.y(), &z = xyz.z();
322 for (x = bbox.min().x(); x <= bbox.max().x(); ++x) {
323 for (y = bbox.min().y(); y <= bbox.max().y(); ++y) {
324 for (z = bbox.min().z(); z <= bbox.max().z(); ++z) {
325 mStats.add(mOp.result(mAcc, it.getCoord()));
326 }
327 }
328 }
329 }
330 }
331
332 // Accumulate another functor's Stats object into this functor's.
333 inline void join(MathOp& other) { mStats.add(other.mStats); }
334 }; // struct MathOp
335
336 } // namespace stats_internal
337
338
339 template<typename IterT>
340 inline math::Histogram
341 histogram(const IterT& iter, double vmin, double vmax, size_t numBins, bool threaded)
342 {
343 using ValueOp = stats_internal::GetVal<IterT, math::Histogram>;
344 ValueOp valOp;
345 stats_internal::HistOp<IterT, ValueOp> op(valOp, vmin, vmax, numBins);
346 tools::accumulate(iter, op, threaded);
347 return op.hist;
348 }
349
350 template<typename IterT>
351 inline math::Extrema
352 extrema(const IterT& iter, bool threaded)
353 {
354 stats_internal::GetVal<IterT, math::Extrema> valOp;
355 return extrema(iter, valOp, threaded);
356 }
357
358 template<typename IterT>
359 inline math::Stats
360 statistics(const IterT& iter, bool threaded)
361 {
362 stats_internal::GetVal<IterT, math::Stats> valOp;
363 return statistics(iter, valOp, threaded);
364 }
365
366 template<typename IterT, typename ValueOp>
367 inline math::Extrema
368 extrema(const IterT& iter, const ValueOp& valOp, bool threaded)
369 {
370 stats_internal::StatsOp<IterT, const ValueOp, math::Extrema> op(valOp);
371 tools::accumulate(iter, op, threaded);
372 return op.stats;
373 }
374
375 template<typename IterT, typename ValueOp>
376 inline math::Stats
377 statistics(const IterT& iter, const ValueOp& valOp, bool threaded)
378 {
379 stats_internal::StatsOp<IterT, const ValueOp, math::Stats> op(valOp);
380 tools::accumulate(iter, op, threaded);
381 return op.stats;
382 }
383
384
385 template<typename OperatorT, typename IterT>
386 inline math::Extrema
387 opExtrema(const IterT& iter, const OperatorT& op, bool threaded)
388 {
389 stats_internal::MathOp<IterT, OperatorT, math::Extrema> func(iter, op);
390 tools::accumulate(iter, func, threaded);
391 return func.mStats;
392 }
393
394 template<typename OperatorT, typename IterT>
395 inline math::Stats
396 opStatistics(const IterT& iter, const OperatorT& op, bool threaded)
397 {
398 stats_internal::MathOp<IterT, OperatorT, math::Stats> func(iter, op);
399 tools::accumulate(iter, func, threaded);
400 return func.mStats;
401 }
402
403 } // namespace tools
404 } // namespace OPENVDB_VERSION_NAME
405 } // namespace openvdb
406
407 #endif // OPENVDB_TOOLS_STATISTICS_HAS_BEEN_INCLUDED
openvdb::v7_0::tools::stats_internal::GetValImpl
Definition: Statistics.h:216
openvdb::v7_0::tools::stats_internal::MathOp::TreeT
typename IterT::TreeT TreeT
Definition: Statistics.h:288
openvdb::v7_0::tools::stats_internal::HistOp::getValue
ValueOp getValue
Definition: Statistics.h:278
openvdb::v7_0::tools::stats_internal::MathOp::mAcc
ConstAccessor mAcc
Definition: Statistics.h:293
Types.h
openvdb::v7_0::tools::stats_internal::IterTraits
Definition: Statistics.h:204
openvdb::v7_0::math::Coord
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:25
openvdb::v7_0::tools::opExtrema
math::Extrema opExtrema(const IterT &iter, const OperatorT &op=OperatorT(), bool threaded=true)
Same as opStatistics except it returns a math::Extrema vs a math::Stats.
Definition: Statistics.h:387
openvdb::v7_0::tools::stats_internal::StatsOp::join
void join(StatsOp &other)
Definition: Statistics.h:255
openvdb::v7_0::tools::stats_internal::HistOp::hist
math::Histogram hist
Definition: Statistics.h:277
openvdb::v7_0::tools::accumulate
void accumulate(const IterT &iter, XformOp &op, bool threaded=true)
Definition: ValueTransformer.h:675
openvdb::v7_0::tools::stats_internal::MathOp::mOp
OpT mOp
Definition: Statistics.h:294
openvdb::v7_0::tools::stats_internal::HistOp
Definition: Statistics.h:265
openvdb::v7_0::tree::ValueAccessor< const TreeT >
openvdb::v7_0::math::Coord::y
Int32 y() const
Definition: Coord.h:132
openvdb::v7_0::tools::stats_internal::GetVal
Definition: Statistics.h:233
openvdb::v7_0::tools::stats_internal::GetVal::ValueT
typename IterTraits< IterT >::ValueType ValueT
Definition: Statistics.h:235
openvdb::v7_0::math::Coord::x
Int32 x() const
Definition: Coord.h:131
openvdb::v7_0::math::Histogram
This class computes a histogram, with a fixed interval width, of a population of floating-point value...
Definition: Stats.h:279
openvdb::v7_0::tools::stats_internal::MathOp::ValueT
typename TreeT::ValueType ValueT
Definition: Statistics.h:289
openvdb::v7_0::tools::extrema
math::Extrema extrema(const IterT &iter, const ValueOp &op, bool threaded)
Iterate over a grid and compute extrema (min/max) of the values produced by applying the given functo...
Definition: Statistics.h:368
openvdb::v7_0::tools::statistics
math::Stats statistics(const IterT &iter, const ValueOp &op, bool threaded)
Iterate over a grid and compute statistics (mean, variance, etc.) of the values produced by applying ...
Definition: Statistics.h:377
openvdb::v7_0::math::CoordBBox
Axis-aligned bounding box of signed integer coordinates.
Definition: Coord.h:248
openvdb::v7_0::tools::stats_internal::StatsOp::operator()
void operator()(const IterT &iter)
Definition: Statistics.h:252
Stats.h
Classes to compute statistics and histograms.
openvdb::v7_0::tools::stats_internal::MathOp::ConstAccessor
typename tree::ValueAccessor< const TreeT > ConstAccessor
Definition: Statistics.h:290
openvdb::v7_0::tools::stats_internal::IterTraits::ValueType
typename IterT::ValueType ValueType
Definition: Statistics.h:205
openvdb::v7_0::math::Stats
This class computes statistics (minimum value, maximum value, mean, variance and standard deviation) ...
Definition: Stats.h:182
openvdb::v7_0::tree::TreeValueIteratorBase::ValueT
typename ValueIterT::NonConstValueType ValueT
Definition: TreeIterator.h:627
openvdb::v7_0::tools::stats_internal::HistOp::operator()
void operator()(const IterT &iter)
Definition: Statistics.h:272
openvdb::v7_0::tools::stats_internal::MathOp::operator()
void operator()(const IterT &it)
Definition: Statistics.h:308
openvdb::v7_0::tools::stats_internal::MathOp::MathOp
MathOp(const IterT &iter, const OpT &op)
Definition: Statistics.h:303
ValueTransformer.h
openvdb::v7_0::math::CoordBBox::max
const Coord & max() const
Definition: Coord.h:322
openvdb::v7_0::math::CoordBBox::min
const Coord & min() const
Definition: Coord.h:321
openvdb::v7_0::math::Coord::z
Int32 z() const
Definition: Coord.h:133
openvdb::v7_0::tools::stats_internal::GetValImpl< T, false >::get
static double get(const T &val)
Definition: Statistics.h:220
openvdb::v7_0::tools::stats_internal::GetValImpl< T, true >::get
static double get(const T &val)
Definition: Statistics.h:225
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:154
openvdb::v7_0::math::Extrema
This class computes the minimum and maximum values of a population of floating-point values.
Definition: Stats.h:92
openvdb::v7_0::tools::opStatistics
math::Stats opStatistics(const IterT &iter, const OperatorT &op=OperatorT(), bool threaded=true)
Iterate over a grid and compute statistics (mean, variance, etc.) of the values produced by applying ...
Definition: Statistics.h:396
openvdb::v7_0::tools::stats_internal::MathOp::join
void join(MathOp &other)
Definition: Statistics.h:333
openvdb::v7_0::tools::stats_internal::MathOp::THROW_IF_NULL
static TreeT * THROW_IF_NULL(TreeT *ptr)
Definition: Statistics.h:298
openvdb::v7_0::tools::stats_internal::StatsOp::stats
StatsT stats
Definition: Statistics.h:257
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:102
openvdb::v7_0::tools::stats_internal::HistOp::HistOp
HistOp(const ValueOp &op, double vmin, double vmax, size_t bins)
Definition: Statistics.h:267
openvdb::v7_0::tools::stats_internal::GetVal::operator()
void operator()(const IterT &iter, StatsT &stats) const
Definition: Statistics.h:238
openvdb::v7_0::tools::stats_internal::IterTraits< tree::TreeValueIteratorBase< TreeT, ValueIterT > >::ValueType
typename tree::TreeValueIteratorBase< TreeT, ValueIterT >::ValueT ValueType
Definition: Statistics.h:210
openvdb::v7_0::tools::stats_internal::StatsOp
Definition: Statistics.h:247
openvdb::v7_0::ValueError
Definition: Exceptions.h:65
openvdb::v7_0::tools::stats_internal::StatsOp::getValue
ValueOp getValue
Definition: Statistics.h:258
openvdb::v7_0::tools::stats_internal::MathOp::mStats
StatsT mStats
Definition: Statistics.h:295
openvdb
Definition: Exceptions.h:13
openvdb::v7_0::tools::stats_internal::MathOp
Definition: Statistics.h:286
openvdb::v7_0::tools::stats_internal::HistOp::join
void join(HistOp &other)
Definition: Statistics.h:275
OPENVDB_THROW
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:82
Exceptions.h
openvdb::v7_0::tools::stats_internal::StatsOp::StatsOp
StatsOp(const ValueOp &op)
Definition: Statistics.h:249
openvdb::v7_0::tools::histogram
math::Histogram histogram(const IterT &iter, double minVal, double maxVal, size_t numBins=10, bool threaded=true)
Iterate over a scalar grid and compute a histogram of the values of the voxels that are visited,...
Definition: Statistics.h:341