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 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583
|
///////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2012-2018 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.
//
///////////////////////////////////////////////////////////////////////////
#include <cppunit/extensions/HelperMacros.h>
#include <openvdb/points/PointDataGrid.h>
#include <openvdb/openvdb.h>
#include <openvdb/io/io.h>
#include <cmath>
#include <ios>
#include <limits>
#include <memory>
#include <sstream>
#include <vector>
using namespace openvdb;
using namespace openvdb::points;
class TestPointDataLeaf: public CppUnit::TestCase
{
public:
virtual void setUp() { openvdb::initialize(); }
virtual void tearDown() { openvdb::uninitialize(); }
CPPUNIT_TEST_SUITE(TestPointDataLeaf);
CPPUNIT_TEST(testEmptyLeaf);
CPPUNIT_TEST(testOffsets);
CPPUNIT_TEST(testSetValue);
CPPUNIT_TEST(testMonotonicity);
CPPUNIT_TEST(testAttributes);
CPPUNIT_TEST(testTopologyCopy);
CPPUNIT_TEST(testEquivalence);
CPPUNIT_TEST(testIterators);
CPPUNIT_TEST(testReadWriteCompression);
CPPUNIT_TEST(testIO);
CPPUNIT_TEST(testSwap);
CPPUNIT_TEST(testCopyOnWrite);
CPPUNIT_TEST(testCopyDescriptor);
CPPUNIT_TEST_SUITE_END();
void testEmptyLeaf();
void testOffsets();
void testSetValue();
void testMonotonicity();
void testAttributes();
void testTopologyCopy();
void testEquivalence();
void testIterators();
void testReadWriteCompression();
void testIO();
void testSwap();
void testCopyOnWrite();
void testCopyDescriptor();
private:
}; // class TestPointDataLeaf
using LeafType = PointDataTree::LeafNodeType;
using ValueType = LeafType::ValueType;
using BufferType = LeafType::Buffer;
namespace {
bool
matchingNamePairs(const openvdb::NamePair& lhs,
const openvdb::NamePair& rhs)
{
if (lhs.first != rhs.first) return false;
if (lhs.second != rhs.second) return false;
return true;
}
bool
zeroLeafValues(const LeafType* leafNode)
{
for (openvdb::Index i = 0; i < LeafType::SIZE; i++) {
if (leafNode->buffer().getValue(i) != LeafType::ValueType(0)) return false;
}
return true;
}
bool
noAttributeData(const LeafType* leafNode)
{
const AttributeSet& attributeSet = leafNode->attributeSet();
return attributeSet.size() == 0 && attributeSet.descriptor().size() == 0;
}
bool
monotonicOffsets(const LeafType& leafNode)
{
int previous = -1;
for (auto iter = leafNode.cbeginValueOn(); iter; ++iter) {
if (previous > int(*iter)) return false;
previous = int(*iter);
}
return true;
}
// (borrowed from PointIndexGrid unit test)
class PointList
{
public:
using PosType = openvdb::Vec3R;
using value_type = openvdb::Vec3R;
PointList(const std::vector<openvdb::Vec3R>& points)
: mPoints(&points)
{
}
size_t size() const {
return mPoints->size();
}
void getPos(size_t n, openvdb::Vec3R& xyz) const {
xyz = (*mPoints)[n];
}
protected:
std::vector<openvdb::Vec3R> const * const mPoints;
}; // PointList
// Generate random points by uniformly distributing points
// on a unit-sphere.
// (borrowed from PointIndexGrid unit test)
std::vector<openvdb::Vec3R> genPoints(const int numPoints)
{
// init
openvdb::math::Random01 randNumber(0);
const int n = int(std::sqrt(double(numPoints)));
const double xScale = (2.0 * M_PI) / double(n);
const double yScale = M_PI / double(n);
double x, y, theta, phi;
std::vector<openvdb::Vec3R> points;
points.reserve(n*n);
// loop over a [0 to n) x [0 to n) grid.
for (int a = 0; a < n; ++a) {
for (int b = 0; b < n; ++b) {
// jitter, move to random pos. inside the current cell
x = double(a) + randNumber();
y = double(b) + randNumber();
// remap to a lat/long map
theta = y * yScale; // [0 to PI]
phi = x * xScale; // [0 to 2PI]
// convert to cartesian coordinates on a unit sphere.
// spherical coordinate triplet (r=1, theta, phi)
points.emplace_back( std::sin(theta)*std::cos(phi),
std::sin(theta)*std::sin(phi),
std::cos(theta) );
}
}
return points;
}
} // namespace
void
TestPointDataLeaf::testEmptyLeaf()
{
// empty leaf construction
{
LeafType* leafNode = new LeafType();
CPPUNIT_ASSERT(leafNode);
CPPUNIT_ASSERT(leafNode->isEmpty());
CPPUNIT_ASSERT(!leafNode->buffer().empty());
CPPUNIT_ASSERT(zeroLeafValues(leafNode));
CPPUNIT_ASSERT(noAttributeData(leafNode));
CPPUNIT_ASSERT(leafNode->origin() == openvdb::Coord(0, 0, 0));
delete leafNode;
}
// empty leaf with non-zero origin construction
{
openvdb::Coord coord(20, 30, 40);
LeafType* leafNode = new LeafType(coord);
CPPUNIT_ASSERT(leafNode);
CPPUNIT_ASSERT(leafNode->isEmpty());
CPPUNIT_ASSERT(!leafNode->buffer().empty());
CPPUNIT_ASSERT(zeroLeafValues(leafNode));
CPPUNIT_ASSERT(noAttributeData(leafNode));
CPPUNIT_ASSERT(leafNode->origin() == openvdb::Coord(16, 24, 40));
delete leafNode;
}
}
void
TestPointDataLeaf::testOffsets()
{
// offsets for one point per voxel (active = true)
{
LeafType* leafNode = new LeafType();
for (openvdb::Index i = 0; i < LeafType::SIZE; i++) {
leafNode->setOffsetOn(i, i);
}
CPPUNIT_ASSERT(leafNode->getValue(10) == 10);
CPPUNIT_ASSERT(leafNode->isDense());
delete leafNode;
}
// offsets for one point per voxel (active = false)
{
LeafType* leafNode = new LeafType();
for (openvdb::Index i = 0; i < LeafType::SIZE; i++) {
leafNode->setOffsetOnly(i, i);
}
CPPUNIT_ASSERT(leafNode->getValue(10) == 10);
CPPUNIT_ASSERT(leafNode->isEmpty());
delete leafNode;
}
// test bulk offset replacement without activity mask update
{
LeafType* leafNode = new LeafType();
for (openvdb::Index i = 0; i < LeafType::SIZE; ++i) {
leafNode->setOffsetOn(i, 10);
}
std::vector<LeafType::ValueType> newOffsets(LeafType::SIZE);
leafNode->setOffsets(newOffsets, /*updateValueMask*/false);
const LeafType::NodeMaskType& valueMask = leafNode->getValueMask();
for (openvdb::Index i = 0; i < LeafType::SIZE; ++i ) {
CPPUNIT_ASSERT(valueMask.isOn(i));
}
delete leafNode;
}
// test bulk offset replacement with activity mask update
{
LeafType* leafNode = new LeafType();
for (openvdb::Index i = 0; i < LeafType::SIZE; ++i) {
leafNode->setOffsetOn(i, 10);
}
std::vector<LeafType::ValueType> newOffsets(LeafType::SIZE);
leafNode->setOffsets(newOffsets, /*updateValueMask*/true);
const LeafType::NodeMaskType& valueMask = leafNode->getValueMask();
for (openvdb::Index i = 0; i < LeafType::SIZE; ++i ) {
CPPUNIT_ASSERT(valueMask.isOff(i));
}
delete leafNode;
}
// ensure bulk offset replacement fails when vector size doesn't equal number of voxels
{
LeafType* leafNode = new LeafType();
std::vector<LeafType::ValueType> newOffsets;
CPPUNIT_ASSERT_THROW(leafNode->setOffsets(newOffsets), openvdb::ValueError);
delete leafNode;
}
// test offset validation
{
using AttributeVec3s = TypedAttributeArray<Vec3s>;
using AttributeS = TypedAttributeArray<float>;
using Descriptor = AttributeSet::Descriptor;
// empty Descriptor should throw on leaf node initialize
auto emptyDescriptor = std::make_shared<Descriptor>();
LeafType* emptyLeafNode = new LeafType();
CPPUNIT_ASSERT_THROW(emptyLeafNode->initializeAttributes(emptyDescriptor, 5),
openvdb::IndexError);
// create a non-empty Descriptor
Descriptor::Ptr descriptor = Descriptor::create(AttributeVec3s::attributeType());
// ensure validateOffsets succeeds for monotonically increasing offsets that fully
// utilise the underlying attribute arrays
{
const size_t numAttributes = 1;
LeafType* leafNode = new LeafType();
leafNode->initializeAttributes(descriptor, numAttributes);
descriptor = descriptor->duplicateAppend("density", AttributeS::attributeType());
leafNode->appendAttribute(leafNode->attributeSet().descriptor(),
descriptor, descriptor->find("density"));
std::vector<LeafType::ValueType> offsets(LeafType::SIZE);
offsets.back() = numAttributes;
leafNode->setOffsets(offsets);
CPPUNIT_ASSERT_NO_THROW(leafNode->validateOffsets());
delete leafNode;
}
// ensure validateOffsets detects non-monotonic offset values
{
LeafType* leafNode = new LeafType();
std::vector<LeafType::ValueType> offsets(LeafType::SIZE);
*offsets.begin() = 1;
leafNode->setOffsets(offsets);
CPPUNIT_ASSERT_THROW(leafNode->validateOffsets(), openvdb::ValueError);
delete leafNode;
}
// ensure validateOffsets detects inconsistent attribute array sizes
{
descriptor = Descriptor::create(AttributeVec3s::attributeType());
const size_t numAttributes = 1;
LeafType* leafNode = new LeafType();
leafNode->initializeAttributes(descriptor, numAttributes);
descriptor = descriptor->duplicateAppend("density", AttributeS::attributeType());
leafNode->appendAttribute(leafNode->attributeSet().descriptor(),
descriptor, descriptor->find("density"));
AttributeSet* newSet = new AttributeSet(leafNode->attributeSet(), numAttributes);
newSet->replace("density", AttributeS::create(numAttributes+1));
leafNode->replaceAttributeSet(newSet);
std::vector<LeafType::ValueType> offsets(LeafType::SIZE);
offsets.back() = numAttributes;
leafNode->setOffsets(offsets);
CPPUNIT_ASSERT_THROW(leafNode->validateOffsets(), openvdb::ValueError);
delete leafNode;
}
// ensure validateOffsets detects unused attributes (e.g. final voxel offset not
// equal to size of attribute arrays)
{
descriptor = Descriptor::create(AttributeVec3s::attributeType());
const size_t numAttributes = 1;
LeafType* leafNode = new LeafType();
leafNode->initializeAttributes(descriptor, numAttributes);
descriptor = descriptor->duplicateAppend("density", AttributeS::attributeType());
leafNode->appendAttribute(leafNode->attributeSet().descriptor(),
descriptor, descriptor->find("density"));
std::vector<LeafType::ValueType> offsets(LeafType::SIZE);
offsets.back() = numAttributes - 1;
leafNode->setOffsets(offsets);
CPPUNIT_ASSERT_THROW(leafNode->validateOffsets(), openvdb::ValueError);
delete leafNode;
}
// ensure validateOffsets detects out-of-bounds offset values
{
descriptor = Descriptor::create(AttributeVec3s::attributeType());
const size_t numAttributes = 1;
LeafType* leafNode = new LeafType();
leafNode->initializeAttributes(descriptor, numAttributes);
descriptor = descriptor->duplicateAppend("density", AttributeS::attributeType());
leafNode->appendAttribute(leafNode->attributeSet().descriptor(),
descriptor, descriptor->find("density"));
std::vector<LeafType::ValueType> offsets(LeafType::SIZE);
offsets.back() = numAttributes + 1;
leafNode->setOffsets(offsets);
CPPUNIT_ASSERT_THROW(leafNode->validateOffsets(), openvdb::ValueError);
delete leafNode;
}
}
}
void
TestPointDataLeaf::testSetValue()
{
// the following tests are not run when in debug mode due to assertions firing
#ifdef NDEBUG
LeafType leaf(openvdb::Coord(0, 0, 0));
openvdb::Coord xyz(0, 0, 0);
openvdb::Index index(LeafType::coordToOffset(xyz));
// ensure all non-modifiable operations are no-ops
leaf.setValueOnly(xyz, 10);
leaf.setValueOnly(index, 10);
leaf.setValueOff(xyz, 10);
leaf.setValueOff(index, 10);
leaf.setValueOn(xyz, 10);
leaf.setValueOn(index, 10);
struct Local { static inline void op(unsigned int& n) { n = 10; } };
leaf.modifyValue(xyz, Local::op);
leaf.modifyValue(index, Local::op);
leaf.modifyValueAndActiveState(xyz, Local::op);
CPPUNIT_ASSERT_EQUAL(0, int(leaf.getValue(xyz)));
#endif
}
void
TestPointDataLeaf::testMonotonicity()
{
LeafType leaf(openvdb::Coord(0, 0, 0));
// assign aggregate values and activate all non-even coordinate sums
unsigned sum = 0;
for (unsigned int i = 0; i < LeafType::DIM; i++) {
for (unsigned int j = 0; j < LeafType::DIM; j++) {
for (unsigned int k = 0; k < LeafType::DIM; k++) {
if (((i + j + k) % 2) == 0) continue;
leaf.setOffsetOn(LeafType::coordToOffset(openvdb::Coord(i, j, k)), sum++);
}
}
}
CPPUNIT_ASSERT(monotonicOffsets(leaf));
// manually change a value and ensure offsets become non-monotonic
leaf.setOffsetOn(500, 4);
CPPUNIT_ASSERT(!monotonicOffsets(leaf));
}
void
TestPointDataLeaf::testAttributes()
{
using AttributeVec3s = TypedAttributeArray<Vec3s>;
using AttributeI = TypedAttributeArray<int32_t>;
// create a descriptor
using Descriptor = AttributeSet::Descriptor;
Descriptor::Ptr descrA = Descriptor::create(AttributeVec3s::attributeType());
// create a leaf and initialize attributes using this descriptor
LeafType leaf(openvdb::Coord(0, 0, 0));
CPPUNIT_ASSERT_EQUAL(leaf.attributeSet().size(), size_t(0));
leaf.initializeAttributes(descrA, /*arrayLength=*/100);
descrA = descrA->duplicateAppend("id", AttributeI::attributeType());
leaf.appendAttribute(leaf.attributeSet().descriptor(), descrA, descrA->find("id"));
CPPUNIT_ASSERT_EQUAL(leaf.attributeSet().size(), size_t(2));
{
const AttributeArray* array = leaf.attributeSet().get(/*pos=*/0);
CPPUNIT_ASSERT_EQUAL(array->size(), Index(100));
}
// manually set a voxel
leaf.setOffsetOn(LeafType::SIZE - 1, 10);
CPPUNIT_ASSERT(!zeroLeafValues(&leaf));
// neither dense nor empty
CPPUNIT_ASSERT(!leaf.isDense());
CPPUNIT_ASSERT(!leaf.isEmpty());
// clear the attributes and check voxel values are zero but value mask is not touched
leaf.clearAttributes(/*updateValueMask=*/ false);
CPPUNIT_ASSERT(!leaf.isDense());
CPPUNIT_ASSERT(!leaf.isEmpty());
CPPUNIT_ASSERT_EQUAL(leaf.attributeSet().size(), size_t(2));
CPPUNIT_ASSERT(zeroLeafValues(&leaf));
// call clearAttributes again, updating the value mask and check it is now inactive
leaf.clearAttributes();
CPPUNIT_ASSERT(leaf.isEmpty());
// ensure arrays are uniform
const AttributeArray* array0 = leaf.attributeSet().get(/*pos=*/0);
const AttributeArray* array1 = leaf.attributeSet().get(/*pos=*/1);
CPPUNIT_ASSERT_EQUAL(array0->size(), Index(1));
CPPUNIT_ASSERT_EQUAL(array1->size(), Index(1));
// test leaf returns expected result for hasAttribute()
CPPUNIT_ASSERT(leaf.hasAttribute(/*pos*/0));
CPPUNIT_ASSERT(leaf.hasAttribute("P"));
CPPUNIT_ASSERT(leaf.hasAttribute(/*pos*/1));
CPPUNIT_ASSERT(leaf.hasAttribute("id"));
CPPUNIT_ASSERT(!leaf.hasAttribute(/*pos*/2));
CPPUNIT_ASSERT(!leaf.hasAttribute("test"));
// test underlying attributeArray can be accessed by name and index,
// and that their types are as expected.
const LeafType* constLeaf = &leaf;
CPPUNIT_ASSERT(matchingNamePairs(leaf.attributeArray(/*pos*/0).type(),
AttributeVec3s::attributeType()));
CPPUNIT_ASSERT(matchingNamePairs(leaf.attributeArray("P").type(),
AttributeVec3s::attributeType()));
CPPUNIT_ASSERT(matchingNamePairs(leaf.attributeArray(/*pos*/1).type(),
AttributeI::attributeType()));
CPPUNIT_ASSERT(matchingNamePairs(leaf.attributeArray("id").type(),
AttributeI::attributeType()));
CPPUNIT_ASSERT(matchingNamePairs(constLeaf->attributeArray(/*pos*/0).type(),
AttributeVec3s::attributeType()));
CPPUNIT_ASSERT(matchingNamePairs(constLeaf->attributeArray("P").type(),
AttributeVec3s::attributeType()));
CPPUNIT_ASSERT(matchingNamePairs(constLeaf->attributeArray(/*pos*/1).type(),
AttributeI::attributeType()));
CPPUNIT_ASSERT(matchingNamePairs(constLeaf->attributeArray("id").type(),
AttributeI::attributeType()));
// check invalid pos or name throws
CPPUNIT_ASSERT_THROW(leaf.attributeArray(/*pos=*/3), openvdb::LookupError);
CPPUNIT_ASSERT_THROW(leaf.attributeArray("not_there"), openvdb::LookupError);
CPPUNIT_ASSERT_THROW(constLeaf->attributeArray(/*pos=*/3), openvdb::LookupError);
CPPUNIT_ASSERT_THROW(constLeaf->attributeArray("not_there"), openvdb::LookupError);
// test leaf can be successfully cast to TypedAttributeArray and check types
CPPUNIT_ASSERT(matchingNamePairs(leaf.attributeArray(/*pos=*/0).type(),
AttributeVec3s::attributeType()));
CPPUNIT_ASSERT(matchingNamePairs(leaf.attributeArray("P").type(),
AttributeVec3s::attributeType()));
CPPUNIT_ASSERT(matchingNamePairs(leaf.attributeArray(/*pos=*/1).type(),
AttributeI::attributeType()));
CPPUNIT_ASSERT(matchingNamePairs(leaf.attributeArray("id").type(),
AttributeI::attributeType()));
CPPUNIT_ASSERT(matchingNamePairs(constLeaf->attributeArray(/*pos=*/0).type(),
AttributeVec3s::attributeType()));
CPPUNIT_ASSERT(matchingNamePairs(constLeaf->attributeArray("P").type(),
AttributeVec3s::attributeType()));
CPPUNIT_ASSERT(matchingNamePairs(constLeaf->attributeArray(/*pos=*/1).type(),
AttributeI::attributeType()));
CPPUNIT_ASSERT(matchingNamePairs(constLeaf->attributeArray("id").type(),
AttributeI::attributeType()));
// check invalid pos or name throws
CPPUNIT_ASSERT_THROW(leaf.attributeArray(/*pos=*/2), openvdb::LookupError);
CPPUNIT_ASSERT_THROW(leaf.attributeArray("test"), openvdb::LookupError);
CPPUNIT_ASSERT_THROW(constLeaf->attributeArray(/*pos=*/2), openvdb::LookupError);
CPPUNIT_ASSERT_THROW(constLeaf->attributeArray("test"), openvdb::LookupError);
// check memory usage = attribute set + base leaf
// leaf.initializeAttributes(descrA, /*arrayLength=*/100);
const LeafType::BaseLeaf& baseLeaf = static_cast<LeafType::BaseLeaf&>(leaf);
const Index64 memUsage = baseLeaf.memUsage() + leaf.attributeSet().memUsage();
CPPUNIT_ASSERT_EQUAL(memUsage, leaf.memUsage());
}
void
TestPointDataLeaf::testTopologyCopy()
{
// test topology copy from a float Leaf
{
using FloatLeaf = openvdb::FloatTree::LeafNodeType;
// create a float leaf and activate some values
FloatLeaf floatLeaf(openvdb::Coord(0, 0, 0));
floatLeaf.setValueOn(1);
floatLeaf.setValueOn(4);
floatLeaf.setValueOn(7);
floatLeaf.setValueOn(8);
CPPUNIT_ASSERT_EQUAL(floatLeaf.onVoxelCount(), Index64(4));
// validate construction of a PointDataLeaf using a TopologyCopy
LeafType leaf(floatLeaf, 0, openvdb::TopologyCopy());
CPPUNIT_ASSERT_EQUAL(leaf.onVoxelCount(), Index64(4));
LeafType leaf2(openvdb::Coord(8, 8, 8));
leaf2.setValueOn(1);
leaf2.setValueOn(4);
leaf2.setValueOn(7);
CPPUNIT_ASSERT(!leaf.hasSameTopology(&leaf2));
leaf2.setValueOn(8);
CPPUNIT_ASSERT(leaf.hasSameTopology(&leaf2));
// validate construction of a PointDataLeaf using an Off-On TopologyCopy
LeafType leaf3(floatLeaf, 1, 2, openvdb::TopologyCopy());
CPPUNIT_ASSERT_EQUAL(leaf3.onVoxelCount(), Index64(4));
}
// test topology copy from a PointIndexLeaf
{
// generate points
// (borrowed from PointIndexGrid unit test)
const float voxelSize = 0.01f;
const openvdb::math::Transform::Ptr transform =
openvdb::math::Transform::createLinearTransform(voxelSize);
std::vector<openvdb::Vec3R> points = genPoints(40000);
PointList pointList(points);
// construct point index grid
using PointIndexGrid = openvdb::tools::PointIndexGrid;
PointIndexGrid::Ptr pointGridPtr =
openvdb::tools::createPointIndexGrid<PointIndexGrid>(pointList, *transform);
auto iter = pointGridPtr->tree().cbeginLeaf();
CPPUNIT_ASSERT(iter);
// check that the active voxel counts match for all leaves
for ( ; iter; ++iter) {
LeafType leaf(*iter);
CPPUNIT_ASSERT_EQUAL(iter->onVoxelCount(), leaf.onVoxelCount());
}
}
}
void
TestPointDataLeaf::testEquivalence()
{
using AttributeVec3s = TypedAttributeArray<openvdb::Vec3s>;
using AttributeF = TypedAttributeArray<float>;
using AttributeI = TypedAttributeArray<int32_t>;
// create a descriptor
using Descriptor = AttributeSet::Descriptor;
Descriptor::Ptr descrA = Descriptor::create(AttributeVec3s::attributeType());
// create a leaf and initialize attributes using this descriptor
LeafType leaf(openvdb::Coord(0, 0, 0));
leaf.initializeAttributes(descrA, /*arrayLength=*/100);
descrA = descrA->duplicateAppend("density", AttributeF::attributeType());
leaf.appendAttribute(leaf.attributeSet().descriptor(), descrA, descrA->find("density"));
descrA = descrA->duplicateAppend("id", AttributeI::attributeType());
leaf.appendAttribute(leaf.attributeSet().descriptor(), descrA, descrA->find("id"));
// manually activate some voxels
leaf.setValueOn(1);
leaf.setValueOn(4);
leaf.setValueOn(7);
// manually change some values in the density array
TypedAttributeArray<float>& attr =
TypedAttributeArray<float>::cast(leaf.attributeArray("density"));
attr.set(0, 5.0f);
attr.set(50, 2.0f);
attr.set(51, 8.1f);
// check deep copy construction (topology and attributes)
{
LeafType leaf2(leaf);
CPPUNIT_ASSERT_EQUAL(leaf.onVoxelCount(), leaf2.onVoxelCount());
CPPUNIT_ASSERT(leaf.hasSameTopology(&leaf2));
CPPUNIT_ASSERT_EQUAL(leaf.attributeSet().size(), leaf2.attributeSet().size());
CPPUNIT_ASSERT_EQUAL(leaf.attributeSet().get(0)->size(),
leaf2.attributeSet().get(0)->size());
}
// check equivalence
{
LeafType leaf2(leaf);
CPPUNIT_ASSERT(leaf == leaf2);
leaf2.setOrigin(openvdb::Coord(0, 8, 0));
CPPUNIT_ASSERT(leaf != leaf2);
}
{
LeafType leaf2(leaf);
CPPUNIT_ASSERT(leaf == leaf2);
leaf2.setValueOn(10);
CPPUNIT_ASSERT(leaf != leaf2);
}
}
void
TestPointDataLeaf::testIterators()
{
using AttributeVec3s = TypedAttributeArray<openvdb::Vec3s>;
using AttributeF = TypedAttributeArray<float>;
// create a descriptor
using Descriptor = AttributeSet::Descriptor;
Descriptor::Ptr descrA = Descriptor::create(AttributeVec3s::attributeType());
// create a leaf and initialize attributes using this descriptor
const size_t size = LeafType::NUM_VOXELS;
LeafType leaf(openvdb::Coord(0, 0, 0));
leaf.initializeAttributes(descrA, /*arrayLength=*/size/2);
descrA = descrA->duplicateAppend("density", AttributeF::attributeType());
leaf.appendAttribute(leaf.attributeSet().descriptor(), descrA, descrA->find("density"));
{ // uniform monotonic offsets, only even active
int offset = 0;
for (Index i = 0; i < size; i++)
{
if ((i % 2) == 0) {
leaf.setOffsetOn(i, ++offset);
}
else {
leaf.setOffsetOnly(i, ++offset);
leaf.setValueOff(i);
}
}
}
{ // test index on
LeafType::IndexOnIter iterOn(leaf.beginIndexOn());
CPPUNIT_ASSERT_EQUAL(iterCount(iterOn), Index64(size/2));
for (int i = 0; iterOn; ++iterOn, i += 2) {
CPPUNIT_ASSERT_EQUAL(*iterOn, Index32(i));
}
}
{ // test index off
LeafType::IndexOffIter iterOff(leaf.beginIndexOff());
CPPUNIT_ASSERT_EQUAL(iterCount(iterOff), Index64(size/2));
for (int i = 1; iterOff; ++iterOff, i += 2) {
CPPUNIT_ASSERT_EQUAL(*iterOff, Index32(i));
}
}
{ // test index all
LeafType::IndexAllIter iterAll(leaf.beginIndexAll());
CPPUNIT_ASSERT_EQUAL(iterCount(iterAll), Index64(size));
for (int i = 0; iterAll; ++iterAll, ++i) {
CPPUNIT_ASSERT_EQUAL(*iterAll, Index32(i));
}
}
}
void
TestPointDataLeaf::testReadWriteCompression()
{
using namespace openvdb;
util::NodeMask<3> valueMask;
util::NodeMask<3> childMask;
io::StreamMetadata::Ptr nullMetadata;
io::StreamMetadata::Ptr streamMetadata(new io::StreamMetadata);
{ // simple read/write test
std::stringstream ss;
Index count = 8*8*8;
std::unique_ptr<PointDataIndex32[]> srcBuf(new PointDataIndex32[count]);
for (Index i = 0; i < count; i++) srcBuf[i] = i;
{
io::writeCompressedValues(ss, srcBuf.get(), count, valueMask, childMask, false);
std::unique_ptr<PointDataIndex32[]> destBuf(new PointDataIndex32[count]);
io::readCompressedValues(ss, destBuf.get(), count, valueMask, false);
for (Index i = 0; i < count; i++) {
CPPUNIT_ASSERT_EQUAL(srcBuf.get()[i], destBuf.get()[i]);
}
}
const char* charBuffer = reinterpret_cast<const char*>(srcBuf.get());
size_t referenceBytes =
compression::bloscCompressedSize(charBuffer, count*sizeof(PointDataIndex32));
{
ss.str("");
io::setStreamMetadataPtr(ss, streamMetadata);
io::writeCompressedValuesSize(ss, srcBuf.get(), count);
io::writeCompressedValues(ss, srcBuf.get(), count, valueMask, childMask, false);
int magic = 1924674;
ss.write(reinterpret_cast<const char*>(&magic), sizeof(int));
std::unique_ptr<PointDataIndex32[]> destBuf(new PointDataIndex32[count]);
uint16_t size;
ss.read(reinterpret_cast<char*>(&size), sizeof(uint16_t));
if (size == std::numeric_limits<uint16_t>::max()) size = 0;
CPPUNIT_ASSERT_EQUAL(size_t(size), referenceBytes);
io::readCompressedValues(ss, destBuf.get(), count, valueMask, false);
int magic2;
ss.read(reinterpret_cast<char*>(&magic2), sizeof(int));
CPPUNIT_ASSERT_EQUAL(magic, magic2);
for (Index i = 0; i < count; i++) {
CPPUNIT_ASSERT_EQUAL(srcBuf.get()[i], destBuf.get()[i]);
}
io::setStreamMetadataPtr(ss, nullMetadata);
}
{ // repeat but using nullptr for destination to force seek behaviour
ss.str("");
io::setStreamMetadataPtr(ss, streamMetadata);
io::writeCompressedValuesSize(ss, srcBuf.get(), count);
io::writeCompressedValues(ss, srcBuf.get(), count, valueMask, childMask, false);
int magic = 3829250;
ss.write(reinterpret_cast<const char*>(&magic), sizeof(int));
uint16_t size;
ss.read(reinterpret_cast<char*>(&size), sizeof(uint16_t));
uint16_t actualSize(size);
if (size == std::numeric_limits<uint16_t>::max()) actualSize = 0;
CPPUNIT_ASSERT_EQUAL(size_t(actualSize), referenceBytes);
streamMetadata->setPass(size);
PointDataIndex32* forceSeek = nullptr;
io::readCompressedValues(ss, forceSeek, count, valueMask, false);
int magic2;
ss.read(reinterpret_cast<char*>(&magic2), sizeof(int));
CPPUNIT_ASSERT_EQUAL(magic, magic2);
io::setStreamMetadataPtr(ss, nullMetadata);
}
#ifndef OPENVDB_USE_BLOSC
{ // write to indicate Blosc compression
std::stringstream ssInvalid;
uint16_t bytes16(100); // clamp to 16-bit unsigned integer
ssInvalid.write(reinterpret_cast<const char*>(&bytes16), sizeof(uint16_t));
std::unique_ptr<PointDataIndex32[]> destBuf(new PointDataIndex32[count]);
CPPUNIT_ASSERT_THROW(io::readCompressedValues(ssInvalid, destBuf.get(),
count, valueMask, false), RuntimeError);
}
#endif
#ifdef OPENVDB_USE_BLOSC
{ // mis-matching destination bytes cause decompression failures
std::unique_ptr<PointDataIndex32[]> destBuf(new PointDataIndex32[count]);
ss.str("");
io::writeCompressedValues(ss, srcBuf.get(), count, valueMask, childMask, false);
CPPUNIT_ASSERT_THROW(io::readCompressedValues(ss, destBuf.get(),
count+1, valueMask, false), RuntimeError);
ss.str("");
io::writeCompressedValues(ss, srcBuf.get(), count, valueMask, childMask, false);
CPPUNIT_ASSERT_THROW(io::readCompressedValues(ss, destBuf.get(),
1, valueMask, false), RuntimeError);
}
#endif
{ // seek
ss.str("");
io::writeCompressedValues(ss, srcBuf.get(), count, valueMask, childMask, false);
int test(10772832);
ss.write(reinterpret_cast<const char*>(&test), sizeof(int));
PointDataIndex32* buf = nullptr;
io::readCompressedValues(ss, buf, count, valueMask, false);
int test2;
ss.read(reinterpret_cast<char*>(&test2), sizeof(int));
CPPUNIT_ASSERT_EQUAL(test, test2);
}
}
{ // two values for non-compressible example
std::stringstream ss;
Index count = 2;
std::unique_ptr<PointDataIndex32[]> srcBuf(new PointDataIndex32[count]);
for (Index i = 0; i < count; i++) srcBuf[i] = i;
io::writeCompressedValues(ss, srcBuf.get(), count, valueMask, childMask, false);
std::unique_ptr<PointDataIndex32[]> destBuf(new PointDataIndex32[count]);
io::readCompressedValues(ss, destBuf.get(), count, valueMask, false);
for (Index i = 0; i < count; i++) {
CPPUNIT_ASSERT_EQUAL(srcBuf.get()[i], destBuf.get()[i]);
}
}
{ // throw at limit of 16-bit
std::stringstream ss;
PointDataIndex32* buf = nullptr;
Index count = std::numeric_limits<uint16_t>::max();
CPPUNIT_ASSERT_THROW(io::writeCompressedValues(ss, buf, count, valueMask, childMask, false),
IoError);
CPPUNIT_ASSERT_THROW(io::readCompressedValues(ss, buf, count, valueMask, false), IoError);
}
}
void
TestPointDataLeaf::testIO()
{
using AttributeVec3s = TypedAttributeArray<openvdb::Vec3s>;
using AttributeF = TypedAttributeArray<float>;
// create a descriptor
using Descriptor = AttributeSet::Descriptor;
Descriptor::Ptr descrA = Descriptor::create(AttributeVec3s::attributeType());
// create a leaf and initialize attributes using this descriptor
const size_t size = LeafType::NUM_VOXELS;
LeafType leaf(openvdb::Coord(0, 0, 0));
leaf.initializeAttributes(descrA, /*arrayLength=*/size/2);
descrA = descrA->duplicateAppend("density", AttributeF::attributeType());
leaf.appendAttribute(leaf.attributeSet().descriptor(), descrA, descrA->find("density"));
// manually activate some voxels
leaf.setOffsetOn(1, 10);
leaf.setOffsetOn(4, 20);
leaf.setOffsetOn(7, 5);
// manually change some values in the density array
TypedAttributeArray<float>& attr =
TypedAttributeArray<float>::cast(leaf.attributeArray("density"));
attr.set(0, 5.0f);
attr.set(50, 2.0f);
attr.set(51, 8.1f);
// read and write topology to disk
{
LeafType leaf2(openvdb::Coord(0, 0, 0));
std::ostringstream ostr(std::ios_base::binary);
leaf.writeTopology(ostr);
std::istringstream istr(ostr.str(), std::ios_base::binary);
leaf2.readTopology(istr);
// check topology matches
CPPUNIT_ASSERT_EQUAL(leaf.onVoxelCount(), leaf2.onVoxelCount());
CPPUNIT_ASSERT(leaf2.isValueOn(4));
CPPUNIT_ASSERT(!leaf2.isValueOn(5));
// check only topology (values and attributes still empty)
CPPUNIT_ASSERT_EQUAL(leaf2.getValue(4), ValueType(0));
CPPUNIT_ASSERT_EQUAL(leaf2.attributeSet().size(), size_t(0));
}
// read and write buffers to disk
{
LeafType leaf2(openvdb::Coord(0, 0, 0));
io::StreamMetadata::Ptr streamMetadata(new io::StreamMetadata);
std::ostringstream ostr(std::ios_base::binary);
io::setStreamMetadataPtr(ostr, streamMetadata);
io::setDataCompression(ostr, io::COMPRESS_BLOSC);
leaf.writeTopology(ostr);
for (Index b = 0; b < leaf.buffers(); b++) {
uint32_t pass = (uint32_t(leaf.buffers()) << 16) | uint32_t(b);
streamMetadata->setPass(pass);
leaf.writeBuffers(ostr);
}
{ // error checking
streamMetadata->setPass(1000);
leaf.writeBuffers(ostr);
io::StreamMetadata::Ptr meta;
io::setStreamMetadataPtr(ostr, meta);
CPPUNIT_ASSERT_THROW(leaf.writeBuffers(ostr), openvdb::IoError);
}
std::istringstream istr(ostr.str(), std::ios_base::binary);
io::setStreamMetadataPtr(istr, streamMetadata);
io::setDataCompression(istr, io::COMPRESS_BLOSC);
// Since the input stream doesn't include a VDB header with file format version info,
// tag the input stream explicitly with the current version number.
io::setCurrentVersion(istr);
leaf2.readTopology(istr);
for (Index b = 0; b < leaf.buffers(); b++) {
uint32_t pass = (uint32_t(leaf.buffers()) << 16) | uint32_t(b);
streamMetadata->setPass(pass);
leaf2.readBuffers(istr);
}
// check topology matches
CPPUNIT_ASSERT_EQUAL(leaf.onVoxelCount(), leaf2.onVoxelCount());
CPPUNIT_ASSERT(leaf2.isValueOn(4));
CPPUNIT_ASSERT(!leaf2.isValueOn(5));
// check only topology (values and attributes still empty)
CPPUNIT_ASSERT_EQUAL(leaf2.getValue(4), ValueType(20));
CPPUNIT_ASSERT_EQUAL(leaf2.attributeSet().size(), size_t(2));
}
{ // test multi-buffer IO
// create a new grid with a single origin leaf
PointDataGrid::Ptr grid = PointDataGrid::create();
grid->setName("points");
grid->tree().addLeaf(new LeafType(leaf));
openvdb::GridCPtrVec grids;
grids.push_back(grid);
// write to file
{
io::File file("leaf.vdb");
file.write(grids);
file.close();
}
{ // read grids from file (using delayed loading)
PointDataGrid::Ptr gridFromDisk;
{
io::File file("leaf.vdb");
file.open();
openvdb::GridBase::Ptr baseGrid = file.readGrid("points");
file.close();
gridFromDisk = openvdb::gridPtrCast<PointDataGrid>(baseGrid);
}
LeafType* leafFromDisk = gridFromDisk->tree().probeLeaf(openvdb::Coord(0, 0, 0));
CPPUNIT_ASSERT(leafFromDisk);
CPPUNIT_ASSERT(leaf == *leafFromDisk);
}
#if OPENVDB_ABI_VERSION_NUMBER >= 3
{ // read grids from file and pre-fetch
PointDataGrid::Ptr gridFromDisk;
{
io::File file("leaf.vdb");
file.open();
openvdb::GridBase::Ptr baseGrid = file.readGrid("points");
file.close();
gridFromDisk = openvdb::gridPtrCast<PointDataGrid>(baseGrid);
}
LeafType* leafFromDisk = gridFromDisk->tree().probeLeaf(openvdb::Coord(0, 0, 0));
CPPUNIT_ASSERT(leafFromDisk);
const AttributeVec3s& position(
AttributeVec3s::cast(leafFromDisk->constAttributeArray("P")));
const AttributeF& density(
AttributeF::cast(leafFromDisk->constAttributeArray("density")));
CPPUNIT_ASSERT(leafFromDisk->buffer().isOutOfCore());
#if OPENVDB_USE_BLOSC
CPPUNIT_ASSERT(position.isOutOfCore());
CPPUNIT_ASSERT(density.isOutOfCore());
#else
// delayed-loading is only available on attribute arrays when using Blosc
CPPUNIT_ASSERT(!position.isOutOfCore());
CPPUNIT_ASSERT(!density.isOutOfCore());
#endif
// prefetch voxel data only
prefetch(gridFromDisk->tree(), /*position=*/false, /*attributes=*/false);
// ensure out-of-core data is now in-core after pre-fetching
CPPUNIT_ASSERT(!leafFromDisk->buffer().isOutOfCore());
#if OPENVDB_USE_BLOSC
CPPUNIT_ASSERT(position.isOutOfCore());
CPPUNIT_ASSERT(density.isOutOfCore());
#else
CPPUNIT_ASSERT(!position.isOutOfCore());
CPPUNIT_ASSERT(!density.isOutOfCore());
#endif
{ // re-open
io::File file("leaf.vdb");
file.open();
openvdb::GridBase::Ptr baseGrid = file.readGrid("points");
file.close();
gridFromDisk = openvdb::gridPtrCast<PointDataGrid>(baseGrid);
}
leafFromDisk = gridFromDisk->tree().probeLeaf(openvdb::Coord(0, 0, 0));
CPPUNIT_ASSERT(leafFromDisk);
const AttributeVec3s& position2(
AttributeVec3s::cast(leafFromDisk->constAttributeArray("P")));
const AttributeF& density2(
AttributeF::cast(leafFromDisk->constAttributeArray("density")));
// prefetch voxel and position attribute data
prefetch(gridFromDisk->tree(), /*position=*/true, /*attribute=*/false);
// ensure out-of-core voxel and position data is now in-core after pre-fetching
CPPUNIT_ASSERT(!leafFromDisk->buffer().isOutOfCore());
CPPUNIT_ASSERT(!position2.isOutOfCore());
#if OPENVDB_USE_BLOSC
CPPUNIT_ASSERT(density2.isOutOfCore());
#else
CPPUNIT_ASSERT(!density2.isOutOfCore());
#endif
{ // re-open
io::File file("leaf.vdb");
file.open();
openvdb::GridBase::Ptr baseGrid = file.readGrid("points");
file.close();
gridFromDisk = openvdb::gridPtrCast<PointDataGrid>(baseGrid);
}
leafFromDisk = gridFromDisk->tree().probeLeaf(openvdb::Coord(0, 0, 0));
CPPUNIT_ASSERT(leafFromDisk);
const AttributeVec3s& position3(
AttributeVec3s::cast(leafFromDisk->constAttributeArray("P")));
const AttributeF& density3(
AttributeF::cast(leafFromDisk->constAttributeArray("density")));
// prefetch all data
prefetch(gridFromDisk->tree());
// ensure out-of-core voxel and position data is now in-core after pre-fetching
CPPUNIT_ASSERT(!leafFromDisk->buffer().isOutOfCore());
CPPUNIT_ASSERT(!position3.isOutOfCore());
CPPUNIT_ASSERT(!density3.isOutOfCore());
}
#endif // OPENVDB_ABI_VERSION_NUMBER >= 3
remove("leaf.vdb");
}
{ // test multi-buffer IO with varying attribute storage per-leaf
// create a new grid with three leaf nodes
PointDataGrid::Ptr grid = PointDataGrid::create();
grid->setName("points");
Descriptor::Ptr descrB = Descriptor::create(AttributeVec3s::attributeType());
// create leaf nodes and initialize attributes using this descriptor
LeafType leaf0(openvdb::Coord(0, 0, 0));
LeafType leaf1(openvdb::Coord(0, 8, 0));
LeafType leaf2(openvdb::Coord(0, 0, 8));
leaf0.initializeAttributes(descrB, /*arrayLength=*/2);
leaf1.initializeAttributes(descrB, /*arrayLength=*/2);
leaf2.initializeAttributes(descrB, /*arrayLength=*/2);
descrB = descrB->duplicateAppend("density", AttributeF::attributeType());
size_t index = descrB->find("density");
// append density attribute to leaf 0 and leaf 2 (not leaf 1)
leaf0.appendAttribute(leaf0.attributeSet().descriptor(), descrB, index);
leaf2.appendAttribute(leaf2.attributeSet().descriptor(), descrB, index);
// manually change some values in the density array for leaf 0 and leaf 2
TypedAttributeArray<float>& attr0 =
TypedAttributeArray<float>::cast(leaf0.attributeArray("density"));
attr0.set(0, 2.0f);
attr0.set(1, 2.0f);
attr0.compact();
// compact only the attribute array in the second leaf
TypedAttributeArray<float>& attr2 =
TypedAttributeArray<float>::cast(leaf2.attributeArray("density"));
attr2.set(0, 5.0f);
attr2.set(1, 5.0f);
attr2.compact();
CPPUNIT_ASSERT(attr0.isUniform());
CPPUNIT_ASSERT(attr2.isUniform());
grid->tree().addLeaf(new LeafType(leaf0));
grid->tree().addLeaf(new LeafType(leaf1));
grid->tree().addLeaf(new LeafType(leaf2));
openvdb::GridCPtrVec grids;
grids.push_back(grid);
{ // write to file
io::File file("leaf.vdb");
file.write(grids);
file.close();
}
{ // read grids from file (using delayed loading)
PointDataGrid::Ptr gridFromDisk;
{
io::File file("leaf.vdb");
file.open();
openvdb::GridBase::Ptr baseGrid = file.readGrid("points");
file.close();
gridFromDisk = openvdb::gridPtrCast<PointDataGrid>(baseGrid);
}
LeafType* leafFromDisk = gridFromDisk->tree().probeLeaf(openvdb::Coord(0, 0, 0));
CPPUNIT_ASSERT(leafFromDisk);
CPPUNIT_ASSERT(leaf0 == *leafFromDisk);
leafFromDisk = gridFromDisk->tree().probeLeaf(openvdb::Coord(0, 8, 0));
CPPUNIT_ASSERT(leafFromDisk);
CPPUNIT_ASSERT(leaf1 == *leafFromDisk);
leafFromDisk = gridFromDisk->tree().probeLeaf(openvdb::Coord(0, 0, 8));
CPPUNIT_ASSERT(leafFromDisk);
CPPUNIT_ASSERT(leaf2 == *leafFromDisk);
}
remove("leaf.vdb");
}
}
void
TestPointDataLeaf::testSwap()
{
using AttributeVec3s = TypedAttributeArray<openvdb::Vec3s>;
using AttributeF = TypedAttributeArray<float>;
using AttributeI = TypedAttributeArray<int>;
// create a descriptor
using Descriptor = AttributeSet::Descriptor;
Descriptor::Ptr descrA = Descriptor::create(AttributeVec3s::attributeType());
// create a leaf and initialize attributes using this descriptor
const Index initialArrayLength = 100;
LeafType leaf(openvdb::Coord(0, 0, 0));
leaf.initializeAttributes(descrA, /*arrayLength=*/initialArrayLength);
descrA = descrA->duplicateAppend("density", AttributeF::attributeType());
leaf.appendAttribute(leaf.attributeSet().descriptor(), descrA, descrA->find("density"));
descrA = descrA->duplicateAppend("id", AttributeI::attributeType());
leaf.appendAttribute(leaf.attributeSet().descriptor(), descrA, descrA->find("id"));
// swap out the underlying attribute set with a new attribute set with a matching
// descriptor
CPPUNIT_ASSERT_EQUAL(initialArrayLength, leaf.attributeSet().get("density")->size());
CPPUNIT_ASSERT_EQUAL(initialArrayLength, leaf.attributeSet().get("id")->size());
descrA = Descriptor::create(AttributeVec3s::attributeType());
const Index newArrayLength = initialArrayLength / 2;
AttributeSet* newAttributeSet(new AttributeSet(descrA, /*arrayLength*/newArrayLength));
newAttributeSet->appendAttribute("density", AttributeF::attributeType());
newAttributeSet->appendAttribute("id", AttributeI::attributeType());
leaf.replaceAttributeSet(newAttributeSet);
CPPUNIT_ASSERT_EQUAL(newArrayLength, leaf.attributeSet().get("density")->size());
CPPUNIT_ASSERT_EQUAL(newArrayLength, leaf.attributeSet().get("id")->size());
// ensure we refuse to swap when the attribute set is null
CPPUNIT_ASSERT_THROW(leaf.replaceAttributeSet(nullptr), openvdb::ValueError);
// ensure we refuse to swap when the descriptors do not match,
// unless we explicitly allow a mismatch.
Descriptor::Ptr descrB = Descriptor::create(AttributeVec3s::attributeType());
AttributeSet* attributeSet = new AttributeSet(descrB, newArrayLength);
attributeSet->appendAttribute("extra", AttributeF::attributeType());
CPPUNIT_ASSERT_THROW(leaf.replaceAttributeSet(attributeSet), openvdb::ValueError);
leaf.replaceAttributeSet(attributeSet, true);
CPPUNIT_ASSERT_EQUAL(const_cast<AttributeSet*>(&leaf.attributeSet()), attributeSet);
}
void
TestPointDataLeaf::testCopyOnWrite()
{
using AttributeVec3s = TypedAttributeArray<openvdb::Vec3s>;
using AttributeF = TypedAttributeArray<float>;
// create a descriptor
using Descriptor = AttributeSet::Descriptor;
Descriptor::Ptr descrA = Descriptor::create(AttributeVec3s::attributeType());
// create a leaf and initialize attributes using this descriptor
const Index initialArrayLength = 100;
LeafType leaf(openvdb::Coord(0, 0, 0));
leaf.initializeAttributes(descrA, /*arrayLength=*/initialArrayLength);
descrA = descrA->duplicateAppend("density", AttributeF::attributeType());
leaf.appendAttribute(leaf.attributeSet().descriptor(), descrA, descrA->find("density"));
const AttributeSet& attributeSet = leaf.attributeSet();
CPPUNIT_ASSERT_EQUAL(attributeSet.size(), size_t(2));
// ensure attribute arrays are shared between leaf nodes until write
const LeafType leafCopy(leaf);
const AttributeSet& attributeSetCopy = leafCopy.attributeSet();
CPPUNIT_ASSERT(attributeSet.isShared(/*pos=*/1));
CPPUNIT_ASSERT(attributeSetCopy.isShared(/*pos=*/1));
// test that from a const leaf, accesses to the attribute arrays do not
// make then unique
const AttributeArray* constArray = attributeSetCopy.getConst(/*pos=*/1);
CPPUNIT_ASSERT(constArray);
CPPUNIT_ASSERT(attributeSet.isShared(/*pos=*/1));
CPPUNIT_ASSERT(attributeSetCopy.isShared(/*pos=*/1));
constArray = attributeSetCopy.get(/*pos=*/1);
CPPUNIT_ASSERT(attributeSet.isShared(/*pos=*/1));
CPPUNIT_ASSERT(attributeSetCopy.isShared(/*pos=*/1));
constArray = &(leafCopy.attributeArray(/*pos=*/1));
CPPUNIT_ASSERT(attributeSet.isShared(/*pos=*/1));
CPPUNIT_ASSERT(attributeSetCopy.isShared(/*pos=*/1));
constArray = &(leafCopy.attributeArray("density"));
CPPUNIT_ASSERT(attributeSet.isShared(/*pos=*/1));
CPPUNIT_ASSERT(attributeSetCopy.isShared(/*pos=*/1));
// test makeUnique is called from non const getters
AttributeArray* attributeArray = &(leaf.attributeArray(/*pos=*/1));
CPPUNIT_ASSERT(attributeArray);
CPPUNIT_ASSERT(!attributeSet.isShared(/*pos=*/1));
CPPUNIT_ASSERT(!attributeSetCopy.isShared(/*pos=*/1));
}
void
TestPointDataLeaf::testCopyDescriptor()
{
using AttributeVec3s = TypedAttributeArray<Vec3s>;
using AttributeS = TypedAttributeArray<float>;
using LeafNode = PointDataTree::LeafNodeType;
PointDataTree tree;
LeafNode* leaf = tree.touchLeaf(openvdb::Coord(0, 0, 0));
LeafNode* leaf2 = tree.touchLeaf(openvdb::Coord(0, 8, 0));
// create a descriptor
using Descriptor = AttributeSet::Descriptor;
Descriptor::Inserter names;
names.add("density", AttributeS::attributeType());
Descriptor::Ptr descrA = Descriptor::create(AttributeVec3s::attributeType());
// initialize attributes using this descriptor
leaf->initializeAttributes(descrA, /*arrayLength=*/100);
leaf2->initializeAttributes(descrA, /*arrayLength=*/50);
// copy the PointDataTree and ensure that descriptors are shared
PointDataTree tree2(tree);
CPPUNIT_ASSERT_EQUAL(tree2.leafCount(), openvdb::Index32(2));
descrA->setGroup("test", size_t(1));
PointDataTree::LeafCIter iter2 = tree2.cbeginLeaf();
CPPUNIT_ASSERT(iter2->attributeSet().descriptor().hasGroup("test"));
++iter2;
CPPUNIT_ASSERT(iter2->attributeSet().descriptor().hasGroup("test"));
// call makeDescriptorUnique and ensure that descriptors are no longer shared
Descriptor::Ptr newDescriptor = makeDescriptorUnique(tree2);
CPPUNIT_ASSERT(newDescriptor);
descrA->setGroup("test2", size_t(2));
iter2 = tree2.cbeginLeaf();
CPPUNIT_ASSERT(!iter2->attributeSet().descriptor().hasGroup("test2"));
++iter2;
CPPUNIT_ASSERT(!iter2->attributeSet().descriptor().hasGroup("test2"));
}
CPPUNIT_TEST_SUITE_REGISTRATION(TestPointDataLeaf);
// Copyright (c) 2012-2018 DreamWorks Animation LLC
// All rights reserved. This software is distributed under the
// Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
|