OpenVDB 5.1.0

Statistics.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2018 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 {
46 OPENVDB_USE_VERSION_NAMESPACE
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 typedef typename IterT::ValueType ValueType;
233 };
234
235 template<typename TreeT, typename ValueIterT>
236 struct IterTraits<tree::TreeValueIteratorBase<TreeT, ValueIterT> > {
237 typedef typename tree::TreeValueIteratorBase<TreeT, ValueIterT>::ValueT ValueType;
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 {
262 typedef typename IterTraits<IterT>::ValueType ValueT;
263 typedef GetValImpl<ValueT, VecTraits<ValueT>::IsVec> ImplT;
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
304 math::Histogram hist;
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 typedef typename IterT::TreeT TreeT;
316 typedef typename TreeT::ValueType ValueT;
317 typedef typename tree::ValueAccessor<const TreeT> ConstAccessor;
318
319 // Each thread gets its own accessor and its own copy of the operator.
320 ConstAccessor mAcc;
321 OpT mOp;
322 StatsT mStats;
323
324 template<typename TreeT>
325 static inline TreeT* THROW_IF_NULL(TreeT* ptr) {
326 if (ptr == NULL) 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 {
370 typedef stats_internal::GetVal<IterT, math::Histogram> ValueOp;
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 {
381 stats_internal::GetVal<IterT, math::Extrema> valOp;
382 return extrema(iter, valOp, threaded);
383 }
384
385 template<typename IterT>
386 inline math::Stats
387 statistics(const IterT& iter, bool threaded)
388 {
389 stats_internal::GetVal<IterT, math::Stats> valOp;
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 {
397 stats_internal::StatsOp<IterT, const ValueOp, math::Extrema> op(valOp);
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 {
406 stats_internal::StatsOp<IterT, const ValueOp, math::Stats> op(valOp);
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 {
416 stats_internal::MathOp<IterT, OperatorT, math::Extrema> func(iter, op);
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 {
425 stats_internal::MathOp<IterT, OperatorT, math::Stats> func(iter, op);
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-2018 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/ )
openvdb::v5_1::tools::stats_internal::HistOp::getValue
ValueOp getValue
Definition: Statistics.h:305
openvdb::v5_1::tools::stats_internal::MathOp::ValueT
TreeT::ValueType ValueT
Definition: Statistics.h:316
openvdb::v5_1::tools::accumulate
void accumulate(const IterT &iter, XformOp &op, bool threaded=true)
Definition: ValueTransformer.h:693
openvdb::v5_1::tools::stats_internal::IterTraits
Definition: Statistics.h:231
Types.h
openvdb::v5_1::math::CoordBBox::max
const Coord & max() const
Definition: Coord.h:338
openvdb::v5_1::math::Stats
This class computes statistics (minimum value, maximum value, mean, variance and standard deviation) ...
Definition: Stats.h:209
openvdb::v5_1::math::CoordBBox
Axis-aligned bounding box of signed integer coordinates.
Definition: Coord.h:264
ValueTransformer.h
openvdb::v5_1::tools::stats_internal::MathOp
Definition: Statistics.h:313
openvdb::v5_1::tools::stats_internal::StatsOp::operator()
void operator()(const IterT &iter)
Definition: Statistics.h:279
openvdb::v5_1::tools::stats_internal::MathOp::MathOp
MathOp(const IterT &iter, const OpT &op)
Definition: Statistics.h:330
openvdb::v5_1::tools::stats_internal::StatsOp::join
void join(StatsOp &other)
Definition: Statistics.h:282
openvdb::v5_1::tools::stats_internal::MathOp::ConstAccessor
tree::ValueAccessor< const TreeT > ConstAccessor
Definition: Statistics.h:317
openvdb::v5_1::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:423
OPENVDB_THROW
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:109
openvdb::v5_1::tools::stats_internal::IterTraits::ValueType
IterT::ValueType ValueType
Definition: Statistics.h:232
openvdb::v5_1::tools::stats_internal::HistOp
Definition: Statistics.h:292
openvdb::v5_1::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:368
openvdb::v5_1::tools::stats_internal::MathOp::mStats
StatsT mStats
Definition: Statistics.h:322
Stats.h
Classes to compute statistics and histograms.
openvdb::v5_1::tools::stats_internal::MathOp::TreeT
IterT::TreeT TreeT
Definition: Statistics.h:315
Exceptions.h
openvdb::v5_1::tools::stats_internal::GetVal::ImplT
GetValImpl< ValueT, VecTraits< ValueT >::IsVec > ImplT
Definition: Statistics.h:263
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:136
openvdb::v5_1::math::CoordBBox::min
const Coord & min() const
Definition: Coord.h:337
openvdb::v5_1::ValueError
Definition: Exceptions.h:92
openvdb::v5_1::tree::TreeValueIteratorBase::ValueT
typename ValueIterT::NonConstValueType ValueT
Definition: TreeIterator.h:671
openvdb::v5_1::tools::stats_internal::GetValImpl
Definition: Statistics.h:243
openvdb::v5_1::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:414
openvdb::v5_1::tools::stats_internal::GetVal::operator()
void operator()(const IterT &iter, StatsT &stats) const
Definition: Statistics.h:265
openvdb::v5_1::math::Coord::z
Int32 z() const
Definition: Coord.h:159
openvdb::v5_1::tools::stats_internal::MathOp::mAcc
ConstAccessor mAcc
Definition: Statistics.h:320
openvdb
Definition: Exceptions.h:40
openvdb::v5_1::tools::stats_internal::HistOp::operator()
void operator()(const IterT &iter)
Definition: Statistics.h:299
openvdb::v5_1::tools::stats_internal::HistOp::hist
math::Histogram hist
Definition: Statistics.h:304
openvdb::v5_1::tools::stats_internal::MathOp::operator()
void operator()(const IterT &it)
Definition: Statistics.h:335
openvdb::v5_1::math::Extrema
This class computes the minimum and maximum values of a population of floating-point values...
Definition: Stats.h:119
openvdb::v5_1::math::Coord::y
Int32 y() const
Definition: Coord.h:158
openvdb::v5_1::tools::stats_internal::HistOp::join
void join(HistOp &other)
Definition: Statistics.h:302
openvdb::v5_1::tools::stats_internal::StatsOp::getValue
ValueOp getValue
Definition: Statistics.h:285
openvdb::v5_1::tools::stats_internal::MathOp::join
void join(MathOp &other)
Definition: Statistics.h:360
openvdb::v5_1::tools::stats_internal::StatsOp
Definition: Statistics.h:274
openvdb::v5_1::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:395
openvdb::v5_1::tools::stats_internal::GetVal::ValueT
IterTraits< IterT >::ValueType ValueT
Definition: Statistics.h:262
openvdb::v5_1::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:404
openvdb::v5_1::math::Histogram
This class computes a histogram, with a fixed interval width, of a population of floating-point value...
Definition: Stats.h:306
openvdb::v5_1::math::Coord
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:51
openvdb::v5_1::tools::stats_internal::StatsOp::StatsOp
StatsOp(const ValueOp &op)
Definition: Statistics.h:276
openvdb::v5_1::tools::stats_internal::MathOp::THROW_IF_NULL
static TreeT * THROW_IF_NULL(TreeT *ptr)
Definition: Statistics.h:325
openvdb::v5_1::tree::ValueAccessor< const TreeT >
openvdb::v5_1::tools::stats_internal::GetVal
Definition: Statistics.h:260
openvdb::v5_1::tools::stats_internal::HistOp::HistOp
HistOp(const ValueOp &op, double vmin, double vmax, size_t bins)
Definition: Statistics.h:294
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:188
openvdb::v5_1::tools::stats_internal::MathOp::mOp
OpT mOp
Definition: Statistics.h:321
openvdb::v5_1::tools::stats_internal::StatsOp::stats
StatsT stats
Definition: Statistics.h:284
openvdb::v5_1::tools::stats_internal::IterTraits< tree::TreeValueIteratorBase< TreeT, ValueIterT > >::ValueType
tree::TreeValueIteratorBase< TreeT, ValueIterT >::ValueT ValueType
Definition: Statistics.h:237
openvdb::v5_1::math::Coord::x
Int32 x() const
Definition: Coord.h:157