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
|
///////////////////////////////////////////////////////////////////////////
//
// 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/AttributeArrayString.h>
#include <openvdb/points/PointAttribute.h>
#include <openvdb/points/PointConversion.h>
#include <vector>
using namespace openvdb;
using namespace openvdb::points;
class TestPointAttribute: public CppUnit::TestCase
{
public:
void setUp() override { openvdb::initialize(); }
void tearDown() override { openvdb::uninitialize(); }
CPPUNIT_TEST_SUITE(TestPointAttribute);
CPPUNIT_TEST(testAppendDrop);
CPPUNIT_TEST(testRename);
CPPUNIT_TEST(testBloscCompress);
CPPUNIT_TEST_SUITE_END();
void testAppendDrop();
void testRename();
void testBloscCompress();
}; // class TestPointAttribute
CPPUNIT_TEST_SUITE_REGISTRATION(TestPointAttribute);
////////////////////////////////////////
void
TestPointAttribute::testAppendDrop()
{
using AttributeI = TypedAttributeArray<int>;
std::vector<Vec3s> positions{{1, 1, 1}, {1, 10, 1}, {10, 1, 1}, {10, 10, 1}};
const float voxelSize(1.0);
math::Transform::Ptr transform(math::Transform::createLinearTransform(voxelSize));
PointDataGrid::Ptr grid = createPointDataGrid<NullCodec, PointDataGrid>(positions, *transform);
PointDataTree& tree = grid->tree();
// check one leaf per point
CPPUNIT_ASSERT_EQUAL(tree.leafCount(), Index32(4));
// retrieve first and last leaf attribute sets
auto leafIter = tree.cbeginLeaf();
const AttributeSet& attributeSet = leafIter->attributeSet();
++leafIter;
++leafIter;
++leafIter;
const AttributeSet& attributeSet4 = leafIter->attributeSet();
// check just one attribute exists (position)
CPPUNIT_ASSERT_EQUAL(attributeSet.descriptor().size(), size_t(1));
{ // append an attribute, different initial values and collapse
appendAttribute<int>(tree, "id");
CPPUNIT_ASSERT(tree.beginLeaf()->hasAttribute("id"));
AttributeArray& array = tree.beginLeaf()->attributeArray("id");
CPPUNIT_ASSERT(array.isUniform());
CPPUNIT_ASSERT_EQUAL(AttributeI::cast(array).get(0), zeroVal<AttributeI::ValueType>());
dropAttribute(tree, "id");
appendAttribute<int>(tree, "id", 10, /*stride*/1);
CPPUNIT_ASSERT(tree.beginLeaf()->hasAttribute("id"));
AttributeArray& array2 = tree.beginLeaf()->attributeArray("id");
CPPUNIT_ASSERT(array2.isUniform());
CPPUNIT_ASSERT_EQUAL(AttributeI::cast(array2).get(0), AttributeI::ValueType(10));
array2.expand();
CPPUNIT_ASSERT(!array2.isUniform());
collapseAttribute<int>(tree, "id", 50);
AttributeArray& array3 = tree.beginLeaf()->attributeArray("id");
CPPUNIT_ASSERT(array3.isUniform());
CPPUNIT_ASSERT_EQUAL(AttributeI::cast(array3).get(0), AttributeI::ValueType(50));
dropAttribute(tree, "id");
appendAttribute<Name>(tree, "name", "test");
AttributeArray& array4 = tree.beginLeaf()->attributeArray("name");
CPPUNIT_ASSERT(array4.isUniform());
StringAttributeHandle handle(array4, attributeSet.descriptor().getMetadata());
CPPUNIT_ASSERT_EQUAL(handle.get(0), Name("test"));
dropAttribute(tree, "name");
}
{ // append a strided attribute
appendAttribute<int>(tree, "id", 0, /*stride=*/1);
AttributeArray& array = tree.beginLeaf()->attributeArray("id");
CPPUNIT_ASSERT_EQUAL(array.stride(), Index(1));
dropAttribute(tree, "id");
appendAttribute<int>(tree, "id", 0, /*stride=*/10);
CPPUNIT_ASSERT(tree.beginLeaf()->hasAttribute("id"));
AttributeArray& array2 = tree.beginLeaf()->attributeArray("id");
CPPUNIT_ASSERT_EQUAL(array2.stride(), Index(10));
dropAttribute(tree, "id");
}
{ // append an attribute, check descriptors are as expected, default value test
appendAttribute<int>(tree, "id",
/*uniformValue*/0,
/*stride=*/1,
/*constantStride=*/true,
/*defaultValue*/TypedMetadata<int>(10).copy(),
/*hidden=*/false, /*transient=*/false);
CPPUNIT_ASSERT_EQUAL(attributeSet.descriptor().size(), size_t(2));
CPPUNIT_ASSERT(attributeSet.descriptor() == attributeSet4.descriptor());
CPPUNIT_ASSERT(&attributeSet.descriptor() == &attributeSet4.descriptor());
CPPUNIT_ASSERT(attributeSet.descriptor().getMetadata()["default:id"]);
}
{ // append three attributes, check ordering is consistent with insertion
appendAttribute<float>(tree, "test3");
appendAttribute<float>(tree, "test1");
appendAttribute<float>(tree, "test2");
CPPUNIT_ASSERT_EQUAL(attributeSet.descriptor().size(), size_t(5));
CPPUNIT_ASSERT_EQUAL(attributeSet.descriptor().find("P"), size_t(0));
CPPUNIT_ASSERT_EQUAL(attributeSet.descriptor().find("id"), size_t(1));
CPPUNIT_ASSERT_EQUAL(attributeSet.descriptor().find("test3"), size_t(2));
CPPUNIT_ASSERT_EQUAL(attributeSet.descriptor().find("test1"), size_t(3));
CPPUNIT_ASSERT_EQUAL(attributeSet.descriptor().find("test2"), size_t(4));
}
{ // drop an attribute by index, check ordering remains consistent
std::vector<size_t> indices{2};
dropAttributes(tree, indices);
CPPUNIT_ASSERT_EQUAL(attributeSet.descriptor().size(), size_t(4));
CPPUNIT_ASSERT_EQUAL(attributeSet.descriptor().find("P"), size_t(0));
CPPUNIT_ASSERT_EQUAL(attributeSet.descriptor().find("id"), size_t(1));
CPPUNIT_ASSERT_EQUAL(attributeSet.descriptor().find("test1"), size_t(2));
CPPUNIT_ASSERT_EQUAL(attributeSet.descriptor().find("test2"), size_t(3));
}
{ // drop attributes by index, check ordering remains consistent
std::vector<size_t> indices{1, 3};
dropAttributes(tree, indices);
CPPUNIT_ASSERT_EQUAL(attributeSet.descriptor().size(), size_t(2));
CPPUNIT_ASSERT_EQUAL(attributeSet.descriptor().find("P"), size_t(0));
CPPUNIT_ASSERT_EQUAL(attributeSet.descriptor().find("test1"), size_t(1));
}
{ // drop last non-position attribute
std::vector<size_t> indices{1};
dropAttributes(tree, indices);
CPPUNIT_ASSERT_EQUAL(attributeSet.descriptor().size(), size_t(1));
}
{ // attempt (and fail) to drop position
std::vector<size_t> indices{0};
CPPUNIT_ASSERT_THROW(dropAttributes(tree, indices), openvdb::KeyError);
CPPUNIT_ASSERT_EQUAL(attributeSet.descriptor().size(), size_t(1));
CPPUNIT_ASSERT(attributeSet.descriptor().find("P") != AttributeSet::INVALID_POS);
}
{ // add back previous attributes
appendAttribute<int>(tree, "id");
appendAttribute<float>(tree, "test3");
appendAttribute<float>(tree, "test1");
appendAttribute<float>(tree, "test2");
CPPUNIT_ASSERT_EQUAL(attributeSet.descriptor().size(), size_t(5));
}
{ // attempt (and fail) to drop non-existing attribute
std::vector<Name> names{"test1000"};
CPPUNIT_ASSERT_THROW(dropAttributes(tree, names), openvdb::KeyError);
CPPUNIT_ASSERT_EQUAL(attributeSet.descriptor().size(), size_t(5));
}
{ // drop by name
std::vector<Name> names{"test1", "test2"};
dropAttributes(tree, names);
CPPUNIT_ASSERT_EQUAL(attributeSet.descriptor().size(), size_t(3));
CPPUNIT_ASSERT(attributeSet.descriptor() == attributeSet4.descriptor());
CPPUNIT_ASSERT(&attributeSet.descriptor() == &attributeSet4.descriptor());
CPPUNIT_ASSERT_EQUAL(attributeSet.descriptor().find("P"), size_t(0));
CPPUNIT_ASSERT_EQUAL(attributeSet.descriptor().find("id"), size_t(1));
CPPUNIT_ASSERT_EQUAL(attributeSet.descriptor().find("test3"), size_t(2));
}
{ // attempt (and fail) to drop position
std::vector<Name> names{"P"};
CPPUNIT_ASSERT_THROW(dropAttributes(tree, names), openvdb::KeyError);
CPPUNIT_ASSERT_EQUAL(attributeSet.descriptor().size(), size_t(3));
CPPUNIT_ASSERT(attributeSet.descriptor().find("P") != AttributeSet::INVALID_POS);
}
{ // drop one attribute by name
dropAttribute(tree, "test3");
CPPUNIT_ASSERT_EQUAL(attributeSet.descriptor().size(), size_t(2));
CPPUNIT_ASSERT_EQUAL(attributeSet.descriptor().find("P"), size_t(0));
CPPUNIT_ASSERT_EQUAL(attributeSet.descriptor().find("id"), size_t(1));
}
{ // drop one attribute by id
dropAttribute(tree, 1);
CPPUNIT_ASSERT_EQUAL(attributeSet.descriptor().size(), size_t(1));
CPPUNIT_ASSERT_EQUAL(attributeSet.descriptor().find("P"), size_t(0));
}
{ // attempt to add an attribute with a name that already exists
appendAttribute<float>(tree, "test3");
CPPUNIT_ASSERT_THROW(appendAttribute<float>(tree, "test3"), openvdb::KeyError);
CPPUNIT_ASSERT_EQUAL(attributeSet.descriptor().size(), size_t(2));
}
{ // attempt to add an attribute with an unregistered type (Vec2R)
CPPUNIT_ASSERT_THROW(appendAttribute<Vec2R>(tree, "unregistered"), openvdb::KeyError);
}
{ // append attributes marked as hidden, transient, group and string
appendAttribute<float>(tree, "testHidden", 0,
/*stride=*/1, /*constantStride=*/true, Metadata::Ptr(), true, false);
appendAttribute<float>(tree, "testTransient", 0,
/*stride=*/1, /*constantStride=*/true, Metadata::Ptr(), false, true);
appendAttribute<Name>(tree, "testString", "",
/*stride=*/1, /*constantStride=*/true, Metadata::Ptr(), false, false);
const AttributeArray& arrayHidden = leafIter->attributeArray("testHidden");
const AttributeArray& arrayTransient = leafIter->attributeArray("testTransient");
const AttributeArray& arrayString = leafIter->attributeArray("testString");
CPPUNIT_ASSERT(arrayHidden.isHidden());
CPPUNIT_ASSERT(!arrayTransient.isHidden());
CPPUNIT_ASSERT(!arrayHidden.isTransient());
CPPUNIT_ASSERT(arrayTransient.isTransient());
CPPUNIT_ASSERT(!arrayString.isTransient());
CPPUNIT_ASSERT(!isGroup(arrayHidden));
CPPUNIT_ASSERT(!isGroup(arrayTransient));
CPPUNIT_ASSERT(!isGroup(arrayString));
CPPUNIT_ASSERT(!isString(arrayHidden));
CPPUNIT_ASSERT(!isString(arrayTransient));
CPPUNIT_ASSERT(isString(arrayString));
}
{ // collapsing non-existing attribute throws exception
CPPUNIT_ASSERT_THROW(collapseAttribute<int>(tree, "unknown", 0), openvdb::KeyError);
CPPUNIT_ASSERT_THROW(collapseAttribute<Name>(tree, "unknown", "unknown"), openvdb::KeyError);
}
}
void
TestPointAttribute::testRename()
{
std::vector<Vec3s> positions{{1, 1, 1}, {1, 10, 1}, {10, 1, 1}, {10, 10, 1}};
const float voxelSize(1.0);
math::Transform::Ptr transform(math::Transform::createLinearTransform(voxelSize));
PointDataGrid::Ptr grid = createPointDataGrid<NullCodec, PointDataGrid>(positions, *transform);
PointDataTree& tree = grid->tree();
// check one leaf per point
CPPUNIT_ASSERT_EQUAL(tree.leafCount(), Index32(4));
const openvdb::TypedMetadata<float> defaultValue(5.0f);
appendAttribute<float>(tree, "test1", 0,
/*stride=*/1, /*constantStride=*/true, defaultValue.copy());
appendAttribute<int>(tree, "id");
appendAttribute<float>(tree, "test2");
// retrieve first and last leaf attribute sets
auto leafIter = tree.cbeginLeaf();
const AttributeSet& attributeSet = leafIter->attributeSet();
++leafIter;
const AttributeSet& attributeSet4 = leafIter->attributeSet();
{ // rename one attribute
renameAttribute(tree, "test1", "test1renamed");
CPPUNIT_ASSERT_EQUAL(attributeSet.descriptor().size(), size_t(4));
CPPUNIT_ASSERT(attributeSet.descriptor().find("test1") == AttributeSet::INVALID_POS);
CPPUNIT_ASSERT(attributeSet.descriptor().find("test1renamed") != AttributeSet::INVALID_POS);
CPPUNIT_ASSERT_EQUAL(attributeSet4.descriptor().size(), size_t(4));
CPPUNIT_ASSERT(attributeSet4.descriptor().find("test1") == AttributeSet::INVALID_POS);
CPPUNIT_ASSERT(attributeSet4.descriptor().find("test1renamed") != AttributeSet::INVALID_POS);
renameAttribute(tree, "test1renamed", "test1");
}
{ // rename non-existing, matching and existing attributes
CPPUNIT_ASSERT_THROW(renameAttribute(tree, "nonexist", "newname"), openvdb::KeyError);
CPPUNIT_ASSERT_THROW(renameAttribute(tree, "test1", "test1"), openvdb::KeyError);
CPPUNIT_ASSERT_THROW(renameAttribute(tree, "test2", "test1"), openvdb::KeyError);
}
{ // rename multiple attributes
std::vector<Name> oldNames{"test1", "test2"};
std::vector<Name> newNames{"test1renamed"};
CPPUNIT_ASSERT_THROW(renameAttributes(tree, oldNames, newNames), openvdb::ValueError);
newNames.push_back("test2renamed");
renameAttributes(tree, oldNames, newNames);
renameAttribute(tree, "test1renamed", "test1");
renameAttribute(tree, "test2renamed", "test2");
}
{ // rename an attribute with a default value
CPPUNIT_ASSERT(attributeSet.descriptor().hasDefaultValue("test1"));
renameAttribute(tree, "test1", "test1renamed");
CPPUNIT_ASSERT(attributeSet.descriptor().hasDefaultValue("test1renamed"));
}
}
void
TestPointAttribute::testBloscCompress()
{
std::vector<Vec3s> positions;
for (float i = 1.f; i < 6.f; i += 0.1f) {
positions.emplace_back(1, i, 1);
positions.emplace_back(1, 1, i);
positions.emplace_back(10, i, 1);
positions.emplace_back(10, 1, i);
}
const float voxelSize(1.0);
math::Transform::Ptr transform(math::Transform::createLinearTransform(voxelSize));
PointDataGrid::Ptr grid = createPointDataGrid<NullCodec, PointDataGrid>(positions, *transform);
PointDataTree& tree = grid->tree();
// check two leaves
CPPUNIT_ASSERT_EQUAL(tree.leafCount(), Index32(2));
// retrieve first and last leaf attribute sets
auto leafIter = tree.beginLeaf();
auto leafIter2 = ++tree.beginLeaf();
{ // append an attribute, check descriptors are as expected
appendAttribute<int>(tree, "compact");
appendAttribute<int>(tree, "id");
appendAttribute<int>(tree, "id2");
}
using AttributeHandleRWI = AttributeWriteHandle<int>;
{ // set some id values (leaf 1)
AttributeHandleRWI handleCompact(leafIter->attributeArray("compact"));
AttributeHandleRWI handleId(leafIter->attributeArray("id"));
AttributeHandleRWI handleId2(leafIter->attributeArray("id2"));
const int size = leafIter->attributeArray("id").size();
CPPUNIT_ASSERT_EQUAL(size, 102);
for (int i = 0; i < size; i++) {
handleCompact.set(i, 5);
handleId.set(i, i);
handleId2.set(i, i);
}
}
{ // set some id values (leaf 2)
AttributeHandleRWI handleCompact(leafIter2->attributeArray("compact"));
AttributeHandleRWI handleId(leafIter2->attributeArray("id"));
AttributeHandleRWI handleId2(leafIter2->attributeArray("id2"));
const int size = leafIter2->attributeArray("id").size();
CPPUNIT_ASSERT_EQUAL(size, 102);
for (int i = 0; i < size; i++) {
handleCompact.set(i, 10);
handleId.set(i, i);
handleId2.set(i, i);
}
}
compactAttributes(tree);
CPPUNIT_ASSERT(leafIter->attributeArray("compact").isUniform());
CPPUNIT_ASSERT(leafIter2->attributeArray("compact").isUniform());
bloscCompressAttribute(tree, "id");
#ifdef OPENVDB_USE_BLOSC
CPPUNIT_ASSERT(leafIter->attributeArray("id").isCompressed());
CPPUNIT_ASSERT(!leafIter->attributeArray("id2").isCompressed());
CPPUNIT_ASSERT(leafIter2->attributeArray("id").isCompressed());
CPPUNIT_ASSERT(!leafIter2->attributeArray("id2").isCompressed());
CPPUNIT_ASSERT(
leafIter->attributeArray("id").memUsage() < leafIter->attributeArray("id2").memUsage());
#endif
}
// 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/ )
|