OpenVDB  2.3.0
Statistics.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2013 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 
64 
71 template<typename IterT>
72 inline math::Stats
73 statistics(const IterT& iter, bool threaded = true);
74 
75 
105 template<typename IterT, typename ValueOp>
106 inline math::Stats
107 statistics(const IterT& iter, const ValueOp& op, bool threaded);
108 
109 
172 template<typename OperatorT, typename IterT>
173 inline math::Stats
174 opStatistics(const IterT& iter, const OperatorT& op = OperatorT(), bool threaded = true);
175 
176 
178 
179 
180 namespace stats_internal {
181 
185 template<typename IterT, typename AuxT = void>
186 struct IterTraits {
187  typedef typename IterT::ValueType ValueType;
188 };
189 
190 template<typename TreeT, typename ValueIterT>
191 struct IterTraits<tree::TreeValueIteratorBase<TreeT, ValueIterT> > {
193 };
194 
195 
196 // Helper class to compute a scalar value from either a scalar or a vector value
197 // (the latter by computing the vector's magnitude)
198 template<typename T, bool IsVector> struct GetValImpl;
199 
200 template<typename T>
201 struct GetValImpl<T, /*IsVector=*/false> {
202  static inline double get(const T& val) { return double(val); }
203 };
204 
205 template<typename T>
206 struct GetValImpl<T, /*IsVector=*/true> {
207  static inline double get(const T& val) { return val.length(); }
208 };
209 
210 
211 // Helper class to compute a scalar value from a tree or node iterator
212 // that points to a value in either a scalar or a vector grid, and to
213 // add that value to a math::Stats object.
214 template<typename IterT, typename StatsT>
215 struct GetVal
216 {
219 
220  inline void operator()(const IterT& iter, StatsT& stats) const {
221  if (iter.isVoxelValue()) stats.add(ImplT::get(*iter));
222  else stats.add(ImplT::get(*iter), iter.getVoxelCount());
223  }
224 };
225 
226 
227 // Helper class to accumulate scalar voxel values or vector voxel magnitudes
228 // into a math::Stats object
229 template<typename IterT, typename ValueOp>
230 struct StatsOp
231 {
232  StatsOp(const ValueOp& op): getValue(op) {}
233 
234  // Accumulate voxel and tile values into this functor's Stats object.
235  inline void operator()(const IterT& iter) { getValue(iter, stats); }
236 
237  // Accumulate another functor's Stats object into this functor's.
238  inline void join(StatsOp& other) { stats.add(other.stats); }
239 
241  ValueOp getValue;
242 };
243 
244 
245 // Helper class to accumulate scalar voxel values or vector voxel magnitudes
246 // into a math::Histogram object
247 template<typename IterT, typename ValueOp>
248 struct HistOp
249 {
250  HistOp(const ValueOp& op, double vmin, double vmax, size_t bins):
251  hist(vmin, vmax, bins), getValue(op)
252  {}
253 
254  // Accumulate voxel and tile values into this functor's Histogram object.
255  inline void operator()(const IterT& iter) { getValue(iter, hist); }
256 
257  // Accumulate another functor's Histogram object into this functor's.
258  inline void join(HistOp& other) { hist.add(other.hist); }
259 
261  ValueOp getValue;
262 };
263 
264 
265 // Helper class to apply an operator such as math::Gradient or math::Laplacian
266 // to voxels and accumulate the scalar results or the magnitudes of vector results
267 // into a math::Stats object
268 template<typename IterT, typename OpT>
269 struct MathOp
270 {
271  typedef typename IterT::TreeT TreeT;
272  typedef typename TreeT::ValueType ValueT;
274 
275  // Each thread gets its own accessor and its own copy of the operator.
277  OpT mOp;
279 
280  template<typename TreeT>
281  static inline TreeT* THROW_IF_NULL(TreeT* ptr) {
282  if (ptr == NULL) OPENVDB_THROW(ValueError, "iterator references a null tree");
283  return ptr;
284  }
285 
286  MathOp(const IterT& iter, const OpT& op):
287  mAcc(*THROW_IF_NULL(iter.getTree())), mOp(op)
288  {}
289 
290  // Accumulate voxel and tile values into this functor's Stats object.
291  void operator()(const IterT& it)
292  {
293  if (it.isVoxelValue()) {
294  // Add the magnitude of the gradient at a single voxel.
295  mStats.add(mOp.result(mAcc, it.getCoord()));
296  } else {
297  // Iterate over the voxels enclosed by a tile and add the results
298  // of applying the operator at each voxel.
302  CoordBBox bbox = it.getBoundingBox();
303  Coord xyz;
304  int &x = xyz.x(), &y = xyz.y(), &z = xyz.z();
305  for (x = bbox.min().x(); x <= bbox.max().x(); ++x) {
306  for (y = bbox.min().y(); y <= bbox.max().y(); ++y) {
307  for (z = bbox.min().z(); z <= bbox.max().z(); ++z) {
308  mStats.add(mOp.result(mAcc, it.getCoord()));
309  }
310  }
311  }
312  }
313  }
314 
315  // Accumulate another functor's Stats object into this functor's.
316  inline void join(MathOp& other) { mStats.add(other.mStats); }
317 }; // struct MathOp
318 
319 } // namespace stats_internal
320 
321 
322 template<typename IterT>
323 inline math::Histogram
324 histogram(const IterT& iter, double vmin, double vmax, size_t numBins, bool threaded)
325 {
327  ValueOp valOp;
328  stats_internal::HistOp<IterT, ValueOp> op(valOp, vmin, vmax, numBins);
329  tools::accumulate(iter, op, threaded);
330  return op.hist;
331 }
332 
333 
334 template<typename IterT>
335 inline math::Stats
336 statistics(const IterT& iter, bool threaded)
337 {
339  return statistics(iter, valOp, threaded);
340 }
341 
342 
343 template<typename IterT, typename ValueOp>
344 inline math::Stats
345 statistics(const IterT& iter, const ValueOp& valOp, bool threaded)
346 {
348  tools::accumulate(iter, op, threaded);
349  return op.stats;
350 }
351 
352 
353 template<typename OperatorT, typename IterT>
354 inline math::Stats
355 opStatistics(const IterT& iter, const OperatorT& op, bool threaded)
356 {
358  tools::accumulate(iter, func, threaded);
359  return func.mStats;
360 }
361 
362 } // namespace tools
363 } // namespace OPENVDB_VERSION_NAME
364 } // namespace openvdb
365 
366 #endif // OPENVDB_TOOLS_STATISTICS_HAS_BEEN_INCLUDED
367 
368 // Copyright (c) 2012-2013 DreamWorks Animation LLC
369 // All rights reserved. This software is distributed under the
370 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
StatsOp(const ValueOp &op)
Definition: Statistics.h:232
GetValImpl< ValueT, VecTraits< ValueT >::IsVec > ImplT
Definition: Statistics.h:218
void join(HistOp &other)
Definition: Statistics.h:258
ValueIterT::NonConstValueType ValueT
Definition: TreeIterator.h:663
void accumulate(const IterT &iter, XformOp &op, bool threaded=true)
Definition: ValueTransformer.h:693
void join(StatsOp &other)
Definition: Statistics.h:238
IterT::TreeT TreeT
Definition: Statistics.h:271
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:97
This class computes statistics (minimum value, maximum value, mean, variance and standard deviation) ...
Definition: Stats.h:59
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:324
void operator()(const IterT &iter)
Definition: Statistics.h:255
ValueOp getValue
Definition: Statistics.h:241
HistOp(const ValueOp &op, double vmin, double vmax, size_t bins)
Definition: Statistics.h:250
void join(MathOp &other)
Definition: Statistics.h:316
Definition: Exceptions.h:88
void operator()(const IterT &iter, StatsT &stats) const
Definition: Statistics.h:220
#define OPENVDB_VERSION_NAME
Definition: version.h:45
tree::TreeValueIteratorBase< TreeT, ValueIterT >::ValueT ValueType
Definition: Statistics.h:192
TreeT::ValueType ValueT
Definition: Statistics.h:272
tree::ValueAccessor< const TreeT > ConstAccessor
Definition: Statistics.h:273
MathOp(const IterT &iter, const OpT &op)
Definition: Statistics.h:286
void operator()(const IterT &iter)
Definition: Statistics.h:235
This class computes a histogram, with a fixed interval width, of a population of floating-point value...
Definition: Stats.h:165
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:355
math::Stats mStats
Definition: Statistics.h:278
ConstAccessor mAcc
Definition: Statistics.h:276
IterT::ValueType ValueType
Definition: Statistics.h:187
ValueOp getValue
Definition: Statistics.h:261
OpT mOp
Definition: Statistics.h:277
static TreeT * THROW_IF_NULL(TreeT *ptr)
Definition: Statistics.h:281
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:345
math::Stats stats
Definition: Statistics.h:240
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:67
math::Histogram hist
Definition: Statistics.h:260
void operator()(const IterT &it)
Definition: Statistics.h:291
IterTraits< IterT >::ValueType ValueT
Definition: Statistics.h:217