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 {
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> > {
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 {
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 
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;
291 
292  // Each thread gets its own accessor and its own copy of the operator.
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 {
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 {
355  return extrema(iter, valOp, threaded);
356 }
357 
358 template<typename IterT>
359 inline math::Stats
360 statistics(const IterT& iter, bool threaded)
361 {
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 {
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 {
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 {
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 {
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
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
void accumulate(const IterT &iter, XformOp &op, bool threaded=true)
Definition: ValueTransformer.h:675
ConstAccessor mAcc
Definition: Statistics.h:293
HistOp(const ValueOp &op, double vmin, double vmax, size_t bins)
Definition: Statistics.h:267
void join(StatsOp &other)
Definition: Statistics.h:255
void operator()(const IterT &it)
Definition: Statistics.h:308
StatsT stats
Definition: Statistics.h:257
void operator()(const IterT &iter)
Definition: Statistics.h:252
math::Histogram hist
Definition: Statistics.h:277
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:82
ValueOp getValue
Definition: Statistics.h:258
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
typename ValueIterT::NonConstValueType ValueT
Definition: TreeIterator.h:627
static TreeT * THROW_IF_NULL(TreeT *ptr)
Definition: Statistics.h:298
This class computes a histogram, with a fixed interval width, of a population of floating-point value...
Definition: Stats.h:279
Classes to compute statistics and histograms.
void operator()(const IterT &iter, StatsT &stats) const
Definition: Statistics.h:238
typename tree::ValueAccessor< const TreeT > ConstAccessor
Definition: Statistics.h:290
StatsT mStats
Definition: Statistics.h:295
Definition: Exceptions.h:65
OpT mOp
Definition: Statistics.h:294
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:102
typename TreeT::ValueType ValueT
Definition: Statistics.h:289
ValueOp getValue
Definition: Statistics.h:278
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
typename IterTraits< IterT >::ValueType ValueT
Definition: Statistics.h:235
This class computes statistics (minimum value, maximum value, mean, variance and standard deviation) ...
Definition: Stats.h:182
Definition: Exceptions.h:13
typename IterT::ValueType ValueType
Definition: Statistics.h:205
MathOp(const IterT &iter, const OpT &op)
Definition: Statistics.h:303
StatsOp(const ValueOp &op)
Definition: Statistics.h:249
void join(HistOp &other)
Definition: Statistics.h:275
typename IterT::TreeT TreeT
Definition: Statistics.h:288
This class computes the minimum and maximum values of a population of floating-point values...
Definition: Stats.h:92
void operator()(const IterT &iter)
Definition: Statistics.h:272
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
typename tree::TreeValueIteratorBase< TreeT, ValueIterT >::ValueT ValueType
Definition: Statistics.h:210
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:154
void join(MathOp &other)
Definition: Statistics.h:333
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