OpenVDB  6.1.0
Statistics.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2019 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 //
35 
36 #ifndef OPENVDB_TOOLS_STATISTICS_HAS_BEEN_INCLUDED
37 #define OPENVDB_TOOLS_STATISTICS_HAS_BEEN_INCLUDED
38 
39 #include <openvdb/Types.h>
40 #include <openvdb/Exceptions.h>
41 #include <openvdb/math/Stats.h>
42 #include "ValueTransformer.h"
43 
44 
45 namespace openvdb {
47 namespace OPENVDB_VERSION_NAME {
48 namespace tools {
49 
59 template<typename IterT>
60 inline math::Histogram
61 histogram(const IterT& iter, double minVal, double maxVal,
62  size_t numBins = 10, bool threaded = true);
63 
70 template<typename IterT>
71 inline math::Extrema
72 extrema(const IterT& iter, bool threaded = true);
73 
80 template<typename IterT>
81 inline math::Stats
82 statistics(const IterT& iter, bool threaded = true);
83 
113 template<typename IterT, typename ValueOp>
114 inline math::Extrema
115 extrema(const IterT& iter, const ValueOp& op, bool threaded);
116 
146 template<typename IterT, typename ValueOp>
147 inline math::Stats
148 statistics(const IterT& iter, const ValueOp& op, bool threaded);
149 
150 
213 template<typename OperatorT, typename IterT>
214 inline math::Stats
215 opStatistics(const IterT& iter, const OperatorT& op = OperatorT(), bool threaded = true);
216 
218 template<typename OperatorT, typename IterT>
219 inline math::Extrema
220 opExtrema(const IterT& iter, const OperatorT& op = OperatorT(), bool threaded = true);
221 
223 
224 
225 namespace stats_internal {
226 
230 template<typename IterT, typename AuxT = void>
231 struct IterTraits {
232  using ValueType = typename IterT::ValueType;
233 };
234 
235 template<typename TreeT, typename ValueIterT>
236 struct IterTraits<tree::TreeValueIteratorBase<TreeT, ValueIterT> > {
238 };
239 
240 
241 // Helper class to compute a scalar value from either a scalar or a vector value
242 // (the latter by computing the vector's magnitude)
243 template<typename T, bool IsVector> struct GetValImpl;
244 
245 template<typename T>
246 struct GetValImpl<T, /*IsVector=*/false> {
247  static inline double get(const T& val) { return double(val); }
248 };
249 
250 template<typename T>
251 struct GetValImpl<T, /*IsVector=*/true> {
252  static inline double get(const T& val) { return val.length(); }
253 };
254 
255 
256 // Helper class to compute a scalar value from a tree or node iterator
257 // that points to a value in either a scalar or a vector grid, and to
258 // add that value to a math::Stats object.
259 template<typename IterT, typename StatsT>
260 struct GetVal
261 {
264 
265  inline void operator()(const IterT& iter, StatsT& stats) const {
266  if (iter.isVoxelValue()) stats.add(ImplT::get(*iter));
267  else stats.add(ImplT::get(*iter), iter.getVoxelCount());
268  }
269 };
270 
271 // Helper class to accumulate scalar voxel values or vector voxel magnitudes
272 // into a math::Stats object
273 template<typename IterT, typename ValueOp, typename StatsT>
274 struct StatsOp
275 {
276  StatsOp(const ValueOp& op): getValue(op) {}
277 
278  // Accumulate voxel and tile values into this functor's Stats object.
279  inline void operator()(const IterT& iter) { getValue(iter, stats); }
280 
281  // Accumulate another functor's Stats object into this functor's.
282  inline void join(StatsOp& other) { stats.add(other.stats); }
283 
284  StatsT stats;
285  ValueOp getValue;
286 };
287 
288 
289 // Helper class to accumulate scalar voxel values or vector voxel magnitudes
290 // into a math::Histogram object
291 template<typename IterT, typename ValueOp>
292 struct HistOp
293 {
294  HistOp(const ValueOp& op, double vmin, double vmax, size_t bins):
295  hist(vmin, vmax, bins), getValue(op)
296  {}
297 
298  // Accumulate voxel and tile values into this functor's Histogram object.
299  inline void operator()(const IterT& iter) { getValue(iter, hist); }
300 
301  // Accumulate another functor's Histogram object into this functor's.
302  inline void join(HistOp& other) { hist.add(other.hist); }
303 
305  ValueOp getValue;
306 };
307 
308 
309 // Helper class to apply an operator such as math::Gradient or math::Laplacian
310 // to voxels and accumulate the scalar results or the magnitudes of vector results
311 // into a math::Stats object
312 template<typename IterT, typename OpT, typename StatsT>
313 struct MathOp
314 {
315  using TreeT = typename IterT::TreeT;
316  using ValueT = typename TreeT::ValueType;
318 
319  // Each thread gets its own accessor and its own copy of the operator.
321  OpT mOp;
322  StatsT mStats;
323 
324  template<typename TreeT>
325  static inline TreeT* THROW_IF_NULL(TreeT* ptr) {
326  if (ptr == nullptr) OPENVDB_THROW(ValueError, "iterator references a null tree");
327  return ptr;
328  }
329 
330  MathOp(const IterT& iter, const OpT& op):
331  mAcc(*THROW_IF_NULL(iter.getTree())), mOp(op)
332  {}
333 
334  // Accumulate voxel and tile values into this functor's Stats object.
335  void operator()(const IterT& it)
336  {
337  if (it.isVoxelValue()) {
338  // Add the magnitude of the gradient at a single voxel.
339  mStats.add(mOp.result(mAcc, it.getCoord()));
340  } else {
341  // Iterate over the voxels enclosed by a tile and add the results
342  // of applying the operator at each voxel.
346  CoordBBox bbox = it.getBoundingBox();
347  Coord xyz;
348  int &x = xyz.x(), &y = xyz.y(), &z = xyz.z();
349  for (x = bbox.min().x(); x <= bbox.max().x(); ++x) {
350  for (y = bbox.min().y(); y <= bbox.max().y(); ++y) {
351  for (z = bbox.min().z(); z <= bbox.max().z(); ++z) {
352  mStats.add(mOp.result(mAcc, it.getCoord()));
353  }
354  }
355  }
356  }
357  }
358 
359  // Accumulate another functor's Stats object into this functor's.
360  inline void join(MathOp& other) { mStats.add(other.mStats); }
361 }; // struct MathOp
362 
363 } // namespace stats_internal
364 
365 
366 template<typename IterT>
367 inline math::Histogram
368 histogram(const IterT& iter, double vmin, double vmax, size_t numBins, bool threaded)
369 {
371  ValueOp valOp;
372  stats_internal::HistOp<IterT, ValueOp> op(valOp, vmin, vmax, numBins);
373  tools::accumulate(iter, op, threaded);
374  return op.hist;
375 }
376 
377 template<typename IterT>
378 inline math::Extrema
379 extrema(const IterT& iter, bool threaded)
380 {
382  return extrema(iter, valOp, threaded);
383 }
384 
385 template<typename IterT>
386 inline math::Stats
387 statistics(const IterT& iter, bool threaded)
388 {
390  return statistics(iter, valOp, threaded);
391 }
392 
393 template<typename IterT, typename ValueOp>
394 inline math::Extrema
395 extrema(const IterT& iter, const ValueOp& valOp, bool threaded)
396 {
398  tools::accumulate(iter, op, threaded);
399  return op.stats;
400 }
401 
402 template<typename IterT, typename ValueOp>
403 inline math::Stats
404 statistics(const IterT& iter, const ValueOp& valOp, bool threaded)
405 {
407  tools::accumulate(iter, op, threaded);
408  return op.stats;
409 }
410 
411 
412 template<typename OperatorT, typename IterT>
413 inline math::Extrema
414 opExtrema(const IterT& iter, const OperatorT& op, bool threaded)
415 {
417  tools::accumulate(iter, func, threaded);
418  return func.mStats;
419 }
420 
421 template<typename OperatorT, typename IterT>
422 inline math::Stats
423 opStatistics(const IterT& iter, const OperatorT& op, bool threaded)
424 {
426  tools::accumulate(iter, func, threaded);
427  return func.mStats;
428 }
429 
430 } // namespace tools
431 } // namespace OPENVDB_VERSION_NAME
432 } // namespace openvdb
433 
434 #endif // OPENVDB_TOOLS_STATISTICS_HAS_BEEN_INCLUDED
435 
436 // Copyright (c) 2012-2019 DreamWorks Animation LLC
437 // All rights reserved. This software is distributed under the
438 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
OpT mOp
Definition: Statistics.h:321
MathOp(const IterT &iter, const OpT &op)
Definition: Statistics.h:330
This class computes statistics (minimum value, maximum value, mean, variance and standard deviation) ...
Definition: Stats.h:209
typename tree::TreeValueIteratorBase< TreeT, ValueIterT >::ValueT ValueType
Definition: Statistics.h:237
typename IterT::TreeT TreeT
Definition: Statistics.h:315
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:109
StatsT mStats
Definition: Statistics.h:322
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:404
void operator()(const IterT &iter)
Definition: Statistics.h:299
typename IterT::ValueType ValueType
Definition: Statistics.h:232
void join(StatsOp &other)
Definition: Statistics.h:282
HistOp(const ValueOp &op, double vmin, double vmax, size_t bins)
Definition: Statistics.h:294
Classes to compute statistics and histograms.
void join(HistOp &other)
Definition: Statistics.h:302
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:414
void accumulate(const IterT &iter, XformOp &op, bool threaded=true)
Definition: ValueTransformer.h:702
typename tree::ValueAccessor< const TreeT > ConstAccessor
Definition: Statistics.h:317
void operator()(const IterT &iter)
Definition: Statistics.h:279
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:125
StatsOp(const ValueOp &op)
Definition: Statistics.h:276
void operator()(const IterT &iter, StatsT &stats) const
Definition: Statistics.h:265
ConstAccessor mAcc
Definition: Statistics.h:320
This class computes a histogram, with a fixed interval width, of a population of floating-point value...
Definition: Stats.h:306
Definition: Exceptions.h:40
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:423
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:395
typename TreeT::ValueType ValueT
Definition: Statistics.h:316
void join(MathOp &other)
Definition: Statistics.h:360
This class computes the minimum and maximum values of a population of floating-point values...
Definition: Stats.h:119
typename IterTraits< IterT >::ValueType ValueT
Definition: Statistics.h:262
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:368
Definition: Exceptions.h:92
static TreeT * THROW_IF_NULL(TreeT *ptr)
Definition: Statistics.h:325
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:177
math::Histogram hist
Definition: Statistics.h:304
typename ValueIterT::NonConstValueType ValueT
Definition: TreeIterator.h:654
void operator()(const IterT &it)
Definition: Statistics.h:335
StatsT stats
Definition: Statistics.h:284
ValueOp getValue
Definition: Statistics.h:305
ValueOp getValue
Definition: Statistics.h:285