/usr/include/openvdb/tools/Diagnostics.h is in libopenvdb-dev 3.1.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 | ///////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2012-2015 DreamWorks Animation LLC
//
// All rights reserved. This software is distributed under the
// Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
//
// Redistributions of source code must retain the above copyright
// and license notice and the following restrictions and disclaimer.
//
// * Neither the name of DreamWorks Animation nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
// LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
//
///////////////////////////////////////////////////////////////////////////
///
/// @file Diagnostics.h
///
/// @author Ken Museth
///
/// @brief Various diagnostic tools to identify potential issues with
/// for example narrow-band level sets or fog volumes
///
#ifndef OPENVDB_TOOLS_DIAGNOSTICS_HAS_BEEN_INCLUDED
#define OPENVDB_TOOLS_DIAGNOSTICS_HAS_BEEN_INCLUDED
#include <openvdb/Grid.h>
#include <openvdb/math/Math.h>
#include <openvdb/math/Vec3.h>
#include <openvdb/math/Stencils.h>
#include <openvdb/math/Operators.h>
#include <openvdb/tree/LeafManager.h>
#include <tbb/blocked_range.h>
#include <tbb/parallel_reduce.h>
#include <set>
#include <boost/math/special_functions/fpclassify.hpp>
#include <boost/utility/enable_if.hpp>
namespace openvdb {
OPENVDB_USE_VERSION_NAMESPACE
namespace OPENVDB_VERSION_NAME {
namespace tools {
////////////////////////////////////////////////////////////////////////////////
/// @brief Perform checks on a grid to see if it is a valid symmetric,
/// narrow-band level set.
///
/// @param grid Grid to be checked
/// @param number Number of the checks to be performed (see below)
/// @return string with a message indicating the nature of the
/// issue. If no issue is detected the return string is empty.
///
/// @details @a number refers to the following ordered list of
/// checks - always starting from the top.
/// Fast checks
/// 1: value type is floating point
/// 2: has level set class type
/// 3: has uniform scale
/// 4: background value is positive and n*dx
///
/// Slower checks
/// 5: no active tiles
/// 6: all the values are finite, i.e not NaN or infinite
/// 7: active values in range between +-background
/// 8: abs of inactive values = background, i.e. assuming a symmetric
/// narrow band!
///
/// Relatively slow check (however multithreaded)
/// 9: norm gradient is close to one, i.e. satisfied the Eikonal equation.
template<class GridType>
std::string
checkLevelSet(const GridType& grid, size_t number=9);
////////////////////////////////////////////////////////////////////////////////
/// @brief Perform checks on a grid to see if it is a valid fog volume.
///
/// @param grid Grid to be checked
/// @param number Number of the checks to be performed (see below)
/// @return string with a message indicating the nature of the
/// issue. If no issue is detected the return string is empty.
///
/// @details @a number refers to the following ordered list of
/// checks - always starting from the top.
/// Fast checks
/// 1: value type is floating point
/// 2: has FOG volume class type
/// 3: background value is zero
///
/// Slower checks
/// 4: all the values are finite, i.e not NaN or infinite
/// 5: inactive values are zero
/// 6: active values are in the range [0,1]
template<class GridType>
std::string
checkFogVolume(const GridType& grid, size_t number=6);
////////////////////////////////////////////////////////////////////////////////
/// @brief Threaded method to find unique inactive values.
///
/// @param grid A VDB volume.
/// @param values List of unique inactive values, returned by this method.
/// @param numValues Number of values to look for.
/// @return @c false if the @a grid has more than @a numValues inactive values.
template<class GridType>
bool
uniqueInactiveValues(const GridType& grid,
std::vector<typename GridType::ValueType>& values, size_t numValues);
////////////////////////////////////////////////////////////////////////////////
/// @brief Checks NaN values
template <typename GridT,
typename TreeIterT = typename GridT::ValueOnCIter>
struct CheckNan
{
typedef typename VecTraits<typename GridT::ValueType>::ElementType ElementType;
typedef TreeIterT TileIterT;
typedef typename tree::IterTraits<typename TreeIterT::NodeT, typename TreeIterT::ValueIterT>
::template NodeConverter<typename GridT::TreeType::LeafNodeType>::Type VoxelIterT;
/// @brief Default constructor
CheckNan() {}
/// Return true if the scalar value is NaN
inline bool operator()(const ElementType& v) const { return boost::math::isnan(v); }
/// @brief This allows for vector values to be checked component-wise
template <typename T>
inline typename boost::enable_if_c<VecTraits<T>::IsVec, bool>::type
operator()(const T& v) const
{
for (int i=0; i<VecTraits<T>::Size; ++i) if ((*this)(v[i])) return true;//should unroll
return false;
}
/// @brief Return true if the tile at the iterator location is NaN
bool operator()(const TreeIterT &iter) const { return (*this)(*iter); }
/// @brief Return true if the voxel at the iterator location is NaN
bool operator()(const VoxelIterT &iter) const { return (*this)(*iter); }
/// @brief Return a string describing a failed check.
std::string str() const { return "NaN"; }
};// CheckNan
////////////////////////////////////////////////////////////////////////////////
/// @brief Checks for infinite values, e.g. 1/0 or -1/0
template <typename GridT,
typename TreeIterT = typename GridT::ValueOnCIter>
struct CheckInf
{
typedef typename VecTraits<typename GridT::ValueType>::ElementType ElementType;
typedef TreeIterT TileIterT;
typedef typename tree::IterTraits<typename TreeIterT::NodeT, typename TreeIterT::ValueIterT>
::template NodeConverter<typename GridT::TreeType::LeafNodeType>::Type VoxelIterT;
/// @brief Default constructor
CheckInf() {}
/// Return true if the value is infinite
inline bool operator()(const ElementType& v) const { return boost::math::isinf(v); }
/// Return true if any of the vector components are infinite.
template <typename T> inline typename boost::enable_if_c<VecTraits<T>::IsVec, bool>::type
operator()(const T& v) const
{
for (int i=0; i<VecTraits<T>::Size; ++i) if ((*this)(v[i])) return true;
return false;
}
/// @brief Return true if the tile at the iterator location is infinite
bool operator()(const TreeIterT &iter) const { return (*this)(*iter); }
/// @brief Return true if the tile at the iterator location is infinite
bool operator()(const VoxelIterT &iter) const { return (*this)(*iter); }
/// @brief Return a string describing a failed check.
std::string str() const { return "infinite"; }
};// CheckInf
////////////////////////////////////////////////////////////////////////////////
/// @brief Checks for both NaN and inf values, i.e. any value that is not finite.
template <typename GridT,
typename TreeIterT = typename GridT::ValueOnCIter>
struct CheckFinite
{
typedef typename VecTraits<typename GridT::ValueType>::ElementType ElementType;
typedef TreeIterT TileIterT;
typedef typename tree::IterTraits<typename TreeIterT::NodeT, typename TreeIterT::ValueIterT>
::template NodeConverter<typename GridT::TreeType::LeafNodeType>::Type VoxelIterT;
/// @brief Default constructor
CheckFinite() {}
/// Return true if the value is NOT finite, i.e. it's NaN or infinite
inline bool operator()(const ElementType& v) const { return !boost::math::isfinite(v); }
/// Return true if any of the vector components are NaN or infinite.
template <typename T>
inline typename boost::enable_if_c<VecTraits<T>::IsVec, bool>::type
operator()(const T& v) const {
for (int i=0; i<VecTraits<T>::Size; ++i) if ((*this)(v[i])) return true;
return false;
}
/// @brief Return true if the tile at the iterator location is NaN or infinite.
bool operator()(const TreeIterT &iter) const { return (*this)(*iter); }
/// @brief Return true if the tile at the iterator location is NaN or infinite.
bool operator()(const VoxelIterT &iter) const { return (*this)(*iter); }
/// @brief Return a string describing a failed check.
std::string str() const { return "not finite"; }
};// CheckFinite
////////////////////////////////////////////////////////////////////////////////
/// @brief Check that the magnitude of a value, a, is close to a fixed
/// magnitude, b, given a fixed tolerance c. That is | |a| - |b| | <= c
template <typename GridT,
typename TreeIterT = typename GridT::ValueOffCIter>
struct CheckMagnitude
{
typedef typename VecTraits<typename GridT::ValueType>::ElementType ElementType;
typedef TreeIterT TileIterT;
typedef typename tree::IterTraits<typename TreeIterT::NodeT, typename TreeIterT::ValueIterT>
::template NodeConverter<typename GridT::TreeType::LeafNodeType>::Type VoxelIterT;
/// @brief Default constructor
CheckMagnitude(const ElementType& a,
const ElementType& t = math::Tolerance<ElementType>::value())
: absVal(math::Abs(a)), tolVal(math::Abs(t))
{
}
/// Return true if the magnitude of the value is not approximately
/// equal to totVal.
inline bool operator()(const ElementType& v) const
{
return math::Abs(math::Abs(v) - absVal) > tolVal;
}
/// Return true if any of the vector components are infinite.
template <typename T> inline typename boost::enable_if_c<VecTraits<T>::IsVec, bool>::type
operator()(const T& v) const
{
for (int i=0; i<VecTraits<T>::Size; ++i) if ((*this)(v[i])) return true;
return false;
}
/// @brief Return true if the tile at the iterator location is infinite
bool operator()(const TreeIterT &iter) const { return (*this)(*iter); }
/// @brief Return true if the tile at the iterator location is infinite
bool operator()(const VoxelIterT &iter) const { return (*this)(*iter); }
/// @brief Return a string describing a failed check.
std::string str() const
{
std::ostringstream ss;
ss << "not equal to +/-"<<absVal<<" with a tolerance of "<<tolVal;
return ss.str();
}
const ElementType absVal, tolVal;
};// CheckMagnitude
////////////////////////////////////////////////////////////////////////////////
/// @brief Checks a value against a range
template <typename GridT,
bool MinInclusive = true,//is min part of the range?
bool MaxInclusive = true,//is max part of the range?
typename TreeIterT = typename GridT::ValueOnCIter>
struct CheckRange
{
typedef typename VecTraits<typename GridT::ValueType>::ElementType ElementType;
typedef TreeIterT TileIterT;
typedef typename tree::IterTraits<typename TreeIterT::NodeT, typename TreeIterT::ValueIterT>
::template NodeConverter<typename GridT::TreeType::LeafNodeType>::Type VoxelIterT;
// @brief Constructor taking a range to be tested against.
CheckRange(const ElementType& _min, const ElementType& _max) : minVal(_min), maxVal(_max)
{
if (minVal > maxVal) {
OPENVDB_THROW(ValueError, "CheckRange: Invalid range (min > max)");
}
}
/// Return true if the value is smaller than min or larger than max.
inline bool operator()(const ElementType& v) const
{
return (MinInclusive ? v<minVal : v<=minVal) ||
(MaxInclusive ? v>maxVal : v>=maxVal);
}
/// Return true if any of the vector components are out of range.
template <typename T>
inline typename boost::enable_if_c<VecTraits<T>::IsVec, bool>::type
operator()(const T& v) const {
for (int i=0; i<VecTraits<T>::Size; ++i) if ((*this)(v[i])) return true;
return false;
}
/// @brief Return true if the voxel at the iterator location is out of range.
bool operator()(const TreeIterT &iter) const { return (*this)(*iter); }
/// @brief Return true if the tile at the iterator location is out of range.
bool operator()(const VoxelIterT &iter) const { return (*this)(*iter); }
/// @brief Return a string describing a failed check.
std::string str() const
{
std::ostringstream ss;
ss << "outside the value range " << (MinInclusive ? "[" : "]")
<< minVal << "," << maxVal << (MaxInclusive ? "]" : "[");
return ss.str();
}
const ElementType minVal, maxVal;
};// CheckRange
////////////////////////////////////////////////////////////////////////////////
/// @brief Checks a value against a minimum
template <typename GridT,
typename TreeIterT = typename GridT::ValueOnCIter>
struct CheckMin
{
typedef typename VecTraits<typename GridT::ValueType>::ElementType ElementType;
typedef TreeIterT TileIterT;
typedef typename tree::IterTraits<typename TreeIterT::NodeT, typename TreeIterT::ValueIterT>
::template NodeConverter<typename GridT::TreeType::LeafNodeType>::Type VoxelIterT;
// @brief Constructor taking a minimum to be tested against.
CheckMin(const ElementType& _min) : minVal(_min) {}
/// Return true if the value is smaller than min.
inline bool operator()(const ElementType& v) const { return v<minVal; }
/// Return true if any of the vector components are smaller than min.
template <typename T>
inline typename boost::enable_if_c<VecTraits<T>::IsVec, bool>::type
operator()(const T& v) const {
for (int i=0; i<VecTraits<T>::Size; ++i) if ((*this)(v[i])) return true;
return false;
}
/// @brief Return true if the voxel at the iterator location is smaller than min.
bool operator()(const TreeIterT &iter) const { return (*this)(*iter); }
/// @brief Return true if the tile at the iterator location is smaller than min.
bool operator()(const VoxelIterT &iter) const { return (*this)(*iter); }
/// @brief Return a string describing a failed check.
std::string str() const
{
std::ostringstream ss;
ss << "smaller than "<<minVal;
return ss.str();
}
const ElementType minVal;
};// CheckMin
////////////////////////////////////////////////////////////////////////////////
/// @brief Checks a value against a maximum
template <typename GridT,
typename TreeIterT = typename GridT::ValueOnCIter>
struct CheckMax
{
typedef typename VecTraits<typename GridT::ValueType>::ElementType ElementType;
typedef TreeIterT TileIterT;
typedef typename tree::IterTraits<typename TreeIterT::NodeT, typename TreeIterT::ValueIterT>
::template NodeConverter<typename GridT::TreeType::LeafNodeType>::Type VoxelIterT;
/// @brief Constructor taking a maximum to be tested against.
CheckMax(const ElementType& _max) : maxVal(_max) {}
/// Return true if the value is larger than max.
inline bool operator()(const ElementType& v) const { return v>maxVal; }
/// Return true if any of the vector components are larger than max.
template <typename T>
inline typename boost::enable_if_c<VecTraits<T>::IsVec, bool>::type
operator()(const T& v) const {
for (int i=0; i<VecTraits<T>::Size; ++i) if ((*this)(v[i])) return true;
return false;
}
/// @brief Return true if the tile at the iterator location is larger than max.
bool operator()(const TreeIterT &iter) const { return (*this)(*iter); }
/// @brief Return true if the voxel at the iterator location is larger than max.
bool operator()(const VoxelIterT &iter) const { return (*this)(*iter); }
/// @brief Return a string describing a failed check.
std::string str() const
{
std::ostringstream ss;
ss << "larger than "<<maxVal;
return ss.str();
}
const ElementType maxVal;
};// CheckMax
////////////////////////////////////////////////////////////////////////////////
/// @brief Checks the norm of the gradient against a range, i.e. @f$|\nabla\phi|\in[min,max]@f$
///
/// @note Internally the test is performed as @f$|\nabla\phi|^2\in[min^2,max^2]@f$
/// for optimization reasons.
template<typename GridT,
typename TreeIterT = typename GridT::ValueOnCIter,
math::BiasedGradientScheme GradScheme = math::FIRST_BIAS>//math::WENO5_BIAS>
struct CheckNormGrad
{
typedef typename GridT::ValueType ValueType;
BOOST_STATIC_ASSERT(boost::is_floating_point<ValueType>::value);
typedef TreeIterT TileIterT;
typedef typename tree::IterTraits<typename TreeIterT::NodeT, typename TreeIterT::ValueIterT>
::template NodeConverter<typename GridT::TreeType::LeafNodeType>::Type VoxelIterT;
typedef typename GridT::ConstAccessor AccT;
/// @brief Constructor taking a grid and a range to be tested against.
CheckNormGrad(const GridT& grid, const ValueType& _min, const ValueType& _max)
: acc(grid.getConstAccessor())
, invdx2(ValueType(1.0/math::Pow2(grid.voxelSize()[0])))
, minVal2(_min*_min)
, maxVal2(_max*_max)
{
if ( !grid.hasUniformVoxels() ) {
OPENVDB_THROW(ValueError, "CheckNormGrad: The transform must have uniform scale");
}
if (_min > _max) {
OPENVDB_THROW(ValueError, "CheckNormGrad: Invalid range (min > max)");
}
}
CheckNormGrad(const CheckNormGrad& other)
: acc(other.acc.tree())
, invdx2(other.invdx2)
, minVal2(other.minVal2)
, maxVal2(other.maxVal2)
{
}
/// Return true if the value is smaller than min or larger than max.
inline bool operator()(const ValueType& v) const { return v<minVal2 || v>maxVal2; }
/// @brief Return true if zero is outside the range.
/// @note We assume that the norm of the gradient of a tile is always zero.
inline bool operator()(const TreeIterT&) const { return (*this)(ValueType(0)); }
/// @brief Return true if the norm of the gradient at a voxel
/// location of the iterator is out of range.
inline bool operator()(const VoxelIterT &iter) const
{
const Coord ijk = iter.getCoord();
return (*this)(invdx2 * math::ISGradientNormSqrd<GradScheme>::result(acc, ijk));
}
/// @brief Return a string describing a failed check.
std::string str() const
{
std::ostringstream ss;
ss << "outside the range of NormGrad ["<<math::Sqrt(minVal2)<<","<<math::Sqrt(maxVal2)<<"]";
return ss.str();
}
AccT acc;
const ValueType invdx2, minVal2, maxVal2;
};// CheckNormGrad
////////////////////////////////////////////////////////////////////////////////
/// @brief Checks the norm of the gradient at zero-crossing voxels against a range
/// @details CheckEikonal differs from CheckNormGrad in that it only
/// checks the norm of the gradient at voxel locations where the
/// FD-stencil crosses the zero isosurface!
template<typename GridT,
typename TreeIterT = typename GridT::ValueOnCIter,
typename StencilT = math::WenoStencil<GridT> >//math::GradStencil<GridT>
struct CheckEikonal
{
typedef typename GridT::ValueType ValueType;
BOOST_STATIC_ASSERT(boost::is_floating_point<ValueType>::value);
typedef TreeIterT TileIterT;
typedef typename tree::IterTraits<typename TreeIterT::NodeT, typename TreeIterT::ValueIterT>
::template NodeConverter<typename GridT::TreeType::LeafNodeType>::Type VoxelIterT;
/// @brief Constructor taking a grid and a range to be tested against.
CheckEikonal(const GridT& grid, const ValueType& _min, const ValueType& _max)
: stencil(grid), minVal(_min), maxVal(_max)
{
if ( !grid.hasUniformVoxels() ) {
OPENVDB_THROW(ValueError, "CheckEikonal: The transform must have uniform scale");
}
if (minVal > maxVal) {
OPENVDB_THROW(ValueError, "CheckEikonal: Invalid range (min > max)");
}
}
CheckEikonal(const CheckEikonal& other)
: stencil(other.stencil.grid()), minVal(other.minVal), maxVal(other.maxVal)
{
}
/// Return true if the value is smaller than min or larger than max.
inline bool operator()(const ValueType& v) const { return v<minVal || v>maxVal; }
/// @brief Return true if zero is outside the range.
/// @note We assume that the norm of the gradient of a tile is always zero.
inline bool operator()(const TreeIterT&) const { return (*this)(ValueType(0)); }
/// @brief Return true if the norm of the gradient at a
/// zero-crossing voxel location of the iterator is out of range.
inline bool operator()(const VoxelIterT &iter) const
{
stencil.moveTo(iter);
if (!stencil.zeroCrossing()) return false;
return (*this)(stencil.normSqGrad());
}
/// @brief Return a string describing a failed check.
std::string str() const
{
std::ostringstream ss;
ss << "outside the range of NormGrad ["<<minVal<<","<<maxVal<<"]";
return ss.str();
}
mutable StencilT stencil;
const ValueType minVal, maxVal;
};// CheckEikonal
////////////////////////////////////////////////////////////////////////////////
/// @brief Checks the divergence against a range
template<typename GridT,
typename TreeIterT = typename GridT::ValueOnCIter,
math::DScheme DiffScheme = math::CD_2ND>
struct CheckDivergence
{
typedef typename GridT::ValueType ValueType;
typedef typename VecTraits<ValueType>::ElementType ElementType;
BOOST_STATIC_ASSERT(boost::is_floating_point<ElementType>::value);
typedef TreeIterT TileIterT;
typedef typename tree::IterTraits<typename TreeIterT::NodeT, typename TreeIterT::ValueIterT>
::template NodeConverter<typename GridT::TreeType::LeafNodeType>::Type VoxelIterT;
typedef typename GridT::ConstAccessor AccT;
/// @brief Constructor taking a grid and a range to be tested against.
CheckDivergence(const GridT& grid,
const ValueType& _min,
const ValueType& _max)
: acc(grid.getConstAccessor())
, invdx(ValueType(1.0/grid.voxelSize()[0]))
, minVal(_min)
, maxVal(_max)
{
if ( !grid.hasUniformVoxels() ) {
OPENVDB_THROW(ValueError, "CheckDivergence: The transform must have uniform scale");
}
if (minVal > maxVal) {
OPENVDB_THROW(ValueError, "CheckDivergence: Invalid range (min > max)");
}
}
/// Return true if the value is smaller than min or larger than max.
inline bool operator()(const ElementType& v) const { return v<minVal || v>maxVal; }
/// @brief Return true if zero is outside the range.
/// @note We assume that the divergence of a tile is always zero.
inline bool operator()(const TreeIterT&) const { return (*this)(ElementType(0)); }
/// @brief Return true if the divergence at a voxel location of
/// the iterator is out of range.
inline bool operator()(const VoxelIterT &iter) const
{
const Coord ijk = iter.getCoord();
return (*this)(invdx * math::ISDivergence<DiffScheme>::result(acc, ijk));
}
/// @brief Return a string describing a failed check.
std::string str() const
{
std::ostringstream ss;
ss << "outside the range of divergence ["<<minVal<<","<<maxVal<<"]";
return ss.str();
}
AccT acc;
const ValueType invdx, minVal, maxVal;
};// CheckDivergence
////////////////////////////////////////////////////////////////////////////////
/// @brief Performs multithreaded diagnostics of a grid
/// @note More documentation will be added soon!
template <typename GridT>
class Diagnose
{
public:
typedef typename GridT::template ValueConverter<bool>::Type MaskType;
Diagnose(const GridT& grid) : mGrid(&grid), mMask(new MaskType()), mCount(0)
{
mMask->setTransform(grid.transformPtr()->copy());
}
template <typename CheckT>
std::string check(const CheckT& check,
bool updateMask = false,
bool checkVoxels = true,
bool checkTiles = true,
bool checkBackground = true)
{
typename MaskType::TreeType* mask = updateMask ? &(mMask->tree()) : NULL;
CheckValues<CheckT> cc(mask, mGrid, check);
std::ostringstream ss;
if (checkBackground) ss << cc.checkBackground();
if (checkTiles) ss << cc.checkTiles();
if (checkVoxels) ss << cc.checkVoxels();
mCount += cc.mCount;
return ss.str();
}
//@{
/// @brief Return a boolean mask of all the values
/// (i.e. tiles and/or voxels) that have failed one or
/// more checks.
typename MaskType::ConstPtr mask() const { return mMask; }
typename MaskType::Ptr mask() { return mMask; }
//@}
/// @brief Return the number of values (i.e. background, tiles or
/// voxels) that have failed one or more checks.
Index64 valueCount() const { return mMask->activeVoxelCount(); }
/// @brief Return total number of failed checks
/// @note If only one check was performed and the mask was updated
/// failureCount equals valueCount.
Index64 failureCount() const { return mCount; }
/// @brief Return a const reference to the grid
const GridT& grid() const { return *mGrid; }
/// @brief Clear the mask and error counter
void clear() { mMask = new MaskType(); mCount = 0; }
private:
// disallow copy construction and copy by assignment!
Diagnose(const Diagnose&);// not implemented
Diagnose& operator=(const Diagnose&);// not implemented
const GridT* mGrid;
typename MaskType::Ptr mMask;
Index64 mCount;
/// @brief Private class that performs the multithreaded checks
template <typename CheckT>
struct CheckValues
{
typedef typename MaskType::TreeType MaskT;
typedef typename GridT::TreeType::LeafNodeType LeafT;
typedef typename tree::LeafManager<const typename GridT::TreeType> LeafManagerT;
const bool mOwnsMask;
MaskT* mMask;
const GridT* mGrid;
const CheckT mCheck;
Index64 mCount;
CheckValues(MaskT* mask, const GridT* grid, const CheckT& check)
: mOwnsMask(false)
, mMask(mask)
, mGrid(grid)
, mCheck(check)
, mCount(0)
{
}
CheckValues(CheckValues& other, tbb::split)
: mOwnsMask(true)
, mMask(other.mMask ? new MaskT() : NULL)
, mGrid(other.mGrid)
, mCheck(other.mCheck)
, mCount(0)
{
}
~CheckValues() { if (mOwnsMask) delete mMask; }
std::string checkBackground()
{
std::ostringstream ss;
if (mCheck(mGrid->background())) {
++mCount;
ss << "Background is " + mCheck.str() << std::endl;
}
return ss.str();
}
std::string checkTiles()
{
std::ostringstream ss;
const Index64 n = mCount;
typename CheckT::TileIterT i(mGrid->tree());
for (i.setMaxDepth(GridT::TreeType::RootNodeType::LEVEL - 1); i; ++i) {
if (mCheck(i)) {
++mCount;
if (mMask) mMask->fill(i.getBoundingBox(), true, true);
}
}
if (const Index64 m = mCount - n) {
ss << m << " tile" << (m==1 ? " is " : "s are ") + mCheck.str() << std::endl;
}
return ss.str();
}
std::string checkVoxels()
{
std::ostringstream ss;
LeafManagerT leafs(mGrid->tree());
const Index64 n = mCount;
tbb::parallel_reduce(leafs.leafRange(), *this);
if (const Index64 m = mCount - n) {
ss << m << " voxel" << (m==1 ? " is " : "s are ") + mCheck.str() << std::endl;
}
return ss.str();
}
void operator()(const typename LeafManagerT::LeafRange& r)
{
typedef typename CheckT::VoxelIterT VoxelIterT;
if (mMask) {
for (typename LeafManagerT::LeafRange::Iterator i=r.begin(); i; ++i) {
typename MaskT::LeafNodeType* maskLeaf = NULL;
for (VoxelIterT j = tree::IterTraits<LeafT, VoxelIterT>::begin(*i); j; ++j) {
if (mCheck(j)) {
++mCount;
if (maskLeaf == NULL) maskLeaf = mMask->touchLeaf(j.getCoord());
maskLeaf->setValueOn(j.pos(), true);
}
}
}
} else {
for (typename LeafManagerT::LeafRange::Iterator i=r.begin(); i; ++i) {
for (VoxelIterT j = tree::IterTraits<LeafT, VoxelIterT>::begin(*i); j; ++j) {
if (mCheck(j)) ++mCount;
}
}
}
}
void join(const CheckValues& other)
{
if (mMask) mMask->merge(*(other.mMask), openvdb::MERGE_ACTIVE_STATES_AND_NODES);
mCount += other.mCount;
}
};//End of private class CheckValues
};// End of public class Diagnose
////////////////////////////////////////////////////////////////////////////////
/// @brief Class that performs various types of checks on narrow-band level sets.
///
/// @note The most common usage is to simply call CheckLevelSet::check()
template<class GridType>
class CheckLevelSet
{
public:
typedef typename GridType::ValueType ValueType;
typedef typename GridType::template ValueConverter<bool>::Type MaskType;
CheckLevelSet(const GridType& grid) : mDiagnose(grid) {}
//@{
/// @brief Return a boolean mask of all the values
/// (i.e. tiles and/or voxels) that have failed one or
/// more checks.
typename MaskType::ConstPtr mask() const { return mDiagnose.mask(); }
typename MaskType::Ptr mask() { return mDiagnose.mask(); }
//@}
/// @brief Return the number of values (i.e. background, tiles or
/// voxels) that have failed one or more checks.
Index64 valueCount() const { return mDiagnose.valueCount(); }
/// @brief Return total number of failed checks
/// @note If only one check was performed and the mask was updated
/// failureCount equals valueCount.
Index64 failureCount() const { return mDiagnose.failureCount(); }
/// @brief Return a const reference to the grid
const GridType& grid() const { return mDiagnose.grid(); }
/// @brief Clear the mask and error counter
void clear() { mDiagnose.clear(); }
/// @brief Return a nonempty message if the grid's value type is a floating point.
///
/// @note No run-time overhead
static std::string checkValueType()
{
static const bool test = boost::is_floating_point<ValueType>::value;
return test ? "" : "Value type is not floating point\n";
}
/// @brief Return message if the grid's class is a level set.
///
/// @note Small run-time overhead
std::string checkClassType() const
{
const bool test = mDiagnose.grid().getGridClass() == GRID_LEVEL_SET;
return test ? "" : "Class type is not \"GRID_LEVEL_SET\"\n";
}
/// @brief Return a nonempty message if the grid's transform does not have uniform scaling.
///
/// @note Small run-time overhead
std::string checkTransform() const
{
return mDiagnose.grid().hasUniformVoxels() ? "" : "Does not have uniform voxels\n";
}
/// @brief Return a nonempty message if the background value is larger than or
/// equal to the halfWidth*voxelSize.
///
/// @note Small run-time overhead
std::string checkBackground(Real halfWidth = LEVEL_SET_HALF_WIDTH) const
{
const Real w = mDiagnose.grid().background() / mDiagnose.grid().voxelSize()[0];
if (w < halfWidth) {
std::ostringstream ss;
ss << "The background value ("<< mDiagnose.grid().background()<<") is less than "
<< halfWidth << " voxel units\n";
return ss.str();
}
return "";
}
/// @brief Return a nonempty message if the grid has no active tile values.
///
/// @note Medium run-time overhead
std::string checkTiles() const
{
const bool test = mDiagnose.grid().tree().hasActiveTiles();
return test ? "Has active tile values\n" : "";
}
/// @brief Return a nonempty message if any of the values are not finite. i.e. NaN or inf.
///
/// @note Medium run-time overhead
std::string checkFinite(bool updateMask = false)
{
CheckFinite<GridType,typename GridType::ValueAllCIter> c;
return mDiagnose.check(c, updateMask, /*voxel*/true, /*tiles*/true, /*background*/true);
}
/// @brief Return a nonempty message if the active voxel values are out-of-range.
///
/// @note Medium run-time overhead
std::string checkRange(bool updateMask = false)
{
const ValueType& background = mDiagnose.grid().background();
CheckRange<GridType> c(-background, background);
return mDiagnose.check(c, updateMask, /*voxel*/true, /*tiles*/false, /*background*/false);
}
/// @brief Return a nonempty message if the the inactive values do not have a
/// magnitude equal to the background value.
///
/// @note Medium run-time overhead
std::string checkInactiveValues(bool updateMask = false)
{
const ValueType& background = mDiagnose.grid().background();
CheckMagnitude<GridType, typename GridType::ValueOffCIter> c(background);
return mDiagnose.check(c, updateMask, /*voxel*/true, /*tiles*/true, /*background*/false);
}
/// @brief Return a nonempty message if the norm of the gradient of the
/// active voxels is out of the range minV to maxV.
///
/// @note Significant run-time overhead
std::string checkEikonal(bool updateMask = false, ValueType minV = 0.5, ValueType maxV = 1.5)
{
CheckEikonal<GridType> c(mDiagnose.grid(), minV, maxV);
return mDiagnose.check(c, updateMask, /*voxel*/true, /*tiles*/false, /*background*/false);
}
/// @brief Return a nonempty message if an error or issue is detected. Only
/// runs tests with a number lower than or equal to n, where:
///
/// Fast checks
/// 1: value type is floating point
/// 2: has level set class type
/// 3: has uniform scale
/// 4: background value is positive and n*dx
///
/// Slower checks
/// 5: no active tiles
/// 6: all the values are finite, i.e not NaN or infinite
/// 7: active values in range between +-background
/// 8: abs of inactive values = background, i.e. assuming a symmetric narrow band!
///
/// Relatively slow check (however multi-threaded)
/// 9: norm of gradient at zero-crossings is one, i.e. satisfied the Eikonal equation.
std::string check(size_t n=9, bool updateMask = false)
{
std::string str = this->checkValueType();
if (str.empty() && n>1) str = this->checkClassType();
if (str.empty() && n>2) str = this->checkTransform();
if (str.empty() && n>3) str = this->checkBackground();
if (str.empty() && n>4) str = this->checkTiles();
if (str.empty() && n>5) str = this->checkFinite(updateMask);
if (str.empty() && n>6) str = this->checkRange(updateMask);
if (str.empty() && n>7) str = this->checkInactiveValues(updateMask);
if (str.empty() && n>8) str = this->checkEikonal(updateMask);
return str;
}
private:
// disallow copy construction and copy by assignment!
CheckLevelSet(const CheckLevelSet&);// not implemented
CheckLevelSet& operator=(const CheckLevelSet&);// not implemented
// Member data
Diagnose<GridType> mDiagnose;
};// CheckLevelSet
template<class GridType>
std::string
checkLevelSet(const GridType& grid, size_t n)
{
CheckLevelSet<GridType> c(grid);
return c.check(n, false);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Class that performs various types of checks on fog volumes.
///
/// @note The most common usage is to simply call CheckFogVolume::check()
template<class GridType>
class CheckFogVolume
{
public:
typedef typename GridType::ValueType ValueType;
typedef typename GridType::template ValueConverter<bool>::Type MaskType;
CheckFogVolume(const GridType& grid) : mDiagnose(grid) {}
//@{
/// @brief Return a boolean mask of all the values
/// (i.e. tiles and/or voxels) that have failed one or
/// more checks.
typename MaskType::ConstPtr mask() const { return mDiagnose.mask(); }
typename MaskType::Ptr mask() { return mDiagnose.mask(); }
//@}
/// @brief Return the number of values (i.e. background, tiles or
/// voxels) that have failed one or more checks.
Index64 valueCount() const { return mDiagnose.valueCount(); }
/// @brief Return total number of failed checks
/// @note If only one check was performed and the mask was updated
/// failureCount equals valueCount.
Index64 failureCount() const { return mDiagnose.failureCount(); }
/// @brief Return a const reference to the grid
const GridType& grid() const { return mDiagnose.grid(); }
/// @brief Clear the mask and error counter
void clear() { mDiagnose.clear(); }
/// @brief Return a nonempty message if the grid's value type is a floating point.
///
/// @note No run-time overhead
static std::string checkValueType()
{
static const bool test = boost::is_floating_point<ValueType>::value;
return test ? "" : "Value type is not floating point";
}
/// @brief Return a nonempty message if the grid's class is a level set.
///
/// @note Small run-time overhead
std::string checkClassType() const
{
const bool test = mDiagnose.grid().getGridClass() == GRID_FOG_VOLUME;
return test ? "" : "Class type is not \"GRID_LEVEL_SET\"";
}
/// @brief Return a nonempty message if the background value is not zero.
///
/// @note Small run-time overhead
std::string checkBackground() const
{
if (!math::isApproxZero(mDiagnose.grid().background())) {
std::ostringstream ss;
ss << "The background value ("<< mDiagnose.grid().background()<<") is not zero";
return ss.str();
}
return "";
}
/// @brief Return a nonempty message if any of the values are not finite. i.e. NaN or inf.
///
/// @note Medium run-time overhead
std::string checkFinite(bool updateMask = false)
{
CheckFinite<GridType,typename GridType::ValueAllCIter> c;
return mDiagnose.check(c, updateMask, /*voxel*/true, /*tiles*/true, /*background*/true);
}
/// @brief Return a nonempty message if any of the inactive values are not zero.
///
/// @note Medium run-time overhead
std::string checkInactiveValues(bool updateMask = false)
{
CheckMagnitude<GridType, typename GridType::ValueOffCIter> c(0);
return mDiagnose.check(c, updateMask, /*voxel*/true, /*tiles*/true, /*background*/true);
}
/// @brief Return a nonempty message if the active voxel values
/// are out-of-range, i.e. not in the range [0,1].
///
/// @note Medium run-time overhead
std::string checkRange(bool updateMask = false)
{
CheckRange<GridType> c(0, 1);
return mDiagnose.check(c, updateMask, /*voxel*/true, /*tiles*/true, /*background*/false);
}
/// @brief Return a nonempty message if an error or issue is detected. Only
/// runs tests with a number lower than or equal to n, where:
///
/// Fast checks
/// 1: value type is floating point
/// 2: has FOG volume class type
/// 3: background value is zero
///
/// Slower checks
/// 4: all the values are finite, i.e not NaN or infinite
/// 5: inactive values are zero
/// 6: active values are in the range [0,1]
std::string check(size_t n=6, bool updateMask = false)
{
std::string str = this->checkValueType();
if (str.empty() && n>1) str = this->checkClassType();
if (str.empty() && n>2) str = this->checkBackground();
if (str.empty() && n>3) str = this->checkFinite(updateMask);
if (str.empty() && n>4) str = this->checkInactiveValues(updateMask);
if (str.empty() && n>5) str = this->checkRange(updateMask);
return str;
}
private:
// disallow copy construction and copy by assignment!
CheckFogVolume(const CheckFogVolume&);// not implemented
CheckFogVolume& operator=(const CheckFogVolume&);// not implemented
// Member data
Diagnose<GridType> mDiagnose;
};// CheckFogVolume
template<class GridType>
std::string
checkFogVolume(const GridType& grid, size_t n)
{
CheckFogVolume<GridType> c(grid);
return c.check(n, false);
}
////////////////////////////////////////////////////////////////////////////////
// Internal utility objects and implementation details
namespace diagnostics_internal {
template<typename TreeType>
class InactiveVoxelValues
{
public:
typedef tree::LeafManager<TreeType> LeafArray;
typedef typename TreeType::ValueType ValueType;
typedef std::set<ValueType> SetType;
InactiveVoxelValues(LeafArray&, size_t numValues);
void runParallel();
void runSerial();
void getInactiveValues(SetType&) const;
inline InactiveVoxelValues(const InactiveVoxelValues<TreeType>&, tbb::split);
inline void operator()(const tbb::blocked_range<size_t>&);
inline void join(const InactiveVoxelValues<TreeType>&);
private:
LeafArray& mLeafArray;
SetType mInactiveValues;
size_t mNumValues;
};// InactiveVoxelValues
template<typename TreeType>
InactiveVoxelValues<TreeType>::InactiveVoxelValues(LeafArray& leafs, size_t numValues)
: mLeafArray(leafs)
, mInactiveValues()
, mNumValues(numValues)
{
}
template <typename TreeType>
inline
InactiveVoxelValues<TreeType>::InactiveVoxelValues(
const InactiveVoxelValues<TreeType>& rhs, tbb::split)
: mLeafArray(rhs.mLeafArray)
, mInactiveValues()
, mNumValues(rhs.mNumValues)
{
}
template<typename TreeType>
void
InactiveVoxelValues<TreeType>::runParallel()
{
tbb::parallel_reduce(mLeafArray.getRange(), *this);
}
template<typename TreeType>
void
InactiveVoxelValues<TreeType>::runSerial()
{
(*this)(mLeafArray.getRange());
}
template<typename TreeType>
inline void
InactiveVoxelValues<TreeType>::operator()(const tbb::blocked_range<size_t>& range)
{
typename TreeType::LeafNodeType::ValueOffCIter iter;
for (size_t n = range.begin(); n < range.end() && !tbb::task::self().is_cancelled(); ++n) {
for (iter = mLeafArray.leaf(n).cbeginValueOff(); iter; ++iter) {
mInactiveValues.insert(iter.getValue());
}
if (mInactiveValues.size() > mNumValues) {
tbb::task::self().cancel_group_execution();
}
}
}
template<typename TreeType>
inline void
InactiveVoxelValues<TreeType>::join(const InactiveVoxelValues<TreeType>& rhs)
{
mInactiveValues.insert(rhs.mInactiveValues.begin(), rhs.mInactiveValues.end());
}
template<typename TreeType>
inline void
InactiveVoxelValues<TreeType>::getInactiveValues(SetType& values) const
{
values.insert(mInactiveValues.begin(), mInactiveValues.end());
}
////////////////////////////////////////
template<typename TreeType>
class InactiveTileValues
{
public:
typedef tree::IteratorRange<typename TreeType::ValueOffCIter> IterRange;
typedef typename TreeType::ValueType ValueType;
typedef std::set<ValueType> SetType;
InactiveTileValues(size_t numValues);
void runParallel(IterRange&);
void runSerial(IterRange&);
void getInactiveValues(SetType&) const;
inline InactiveTileValues(const InactiveTileValues<TreeType>&, tbb::split);
inline void operator()(IterRange&);
inline void join(const InactiveTileValues<TreeType>&);
private:
SetType mInactiveValues;
size_t mNumValues;
};
template<typename TreeType>
InactiveTileValues<TreeType>::InactiveTileValues(size_t numValues)
: mInactiveValues()
, mNumValues(numValues)
{
}
template <typename TreeType>
inline
InactiveTileValues<TreeType>::InactiveTileValues(
const InactiveTileValues<TreeType>& rhs, tbb::split)
: mInactiveValues()
, mNumValues(rhs.mNumValues)
{
}
template<typename TreeType>
void
InactiveTileValues<TreeType>::runParallel(IterRange& range)
{
tbb::parallel_reduce(range, *this);
}
template<typename TreeType>
void
InactiveTileValues<TreeType>::runSerial(IterRange& range)
{
(*this)(range);
}
template<typename TreeType>
inline void
InactiveTileValues<TreeType>::operator()(IterRange& range)
{
for (; range && !tbb::task::self().is_cancelled(); ++range) {
typename TreeType::ValueOffCIter iter = range.iterator();
for (; iter; ++iter) {
mInactiveValues.insert(iter.getValue());
}
if (mInactiveValues.size() > mNumValues) {
tbb::task::self().cancel_group_execution();
}
}
}
template<typename TreeType>
inline void
InactiveTileValues<TreeType>::join(const InactiveTileValues<TreeType>& rhs)
{
mInactiveValues.insert(rhs.mInactiveValues.begin(), rhs.mInactiveValues.end());
}
template<typename TreeType>
inline void
InactiveTileValues<TreeType>::getInactiveValues(SetType& values) const
{
values.insert(mInactiveValues.begin(), mInactiveValues.end());
}
} // namespace diagnostics_internal
////////////////////////////////////////
template<class GridType>
bool
uniqueInactiveValues(const GridType& grid,
std::vector<typename GridType::ValueType>& values, size_t numValues)
{
typedef typename GridType::TreeType TreeType;
typedef typename GridType::ValueType ValueType;
typedef std::set<ValueType> SetType;
SetType uniqueValues;
{ // Check inactive voxels
TreeType& tree = const_cast<TreeType&>(grid.tree());
tree::LeafManager<TreeType> leafs(tree);
diagnostics_internal::InactiveVoxelValues<TreeType> voxelOp(leafs, numValues);
voxelOp.runParallel();
voxelOp.getInactiveValues(uniqueValues);
}
// Check inactive tiles
if (uniqueValues.size() <= numValues) {
typename TreeType::ValueOffCIter iter(grid.tree());
iter.setMaxDepth(TreeType::ValueAllIter::LEAF_DEPTH - 1);
diagnostics_internal::InactiveTileValues<TreeType> tileOp(numValues);
tree::IteratorRange<typename TreeType::ValueOffCIter> range(iter);
tileOp.runParallel(range);
tileOp.getInactiveValues(uniqueValues);
}
values.clear();
values.reserve(uniqueValues.size());
typename SetType::iterator it = uniqueValues.begin();
for ( ; it != uniqueValues.end(); ++it) {
values.push_back(*it);
}
return values.size() <= numValues;
}
} // namespace tools
} // namespace OPENVDB_VERSION_NAME
} // namespace openvdb
#endif // OPENVDB_TOOLS_DIAGNOSTICS_HAS_BEEN_INCLUDED
// Copyright (c) 2012-2015 DreamWorks Animation LLC
// All rights reserved. This software is distributed under the
// Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
|