From 3c2d73db0bd85e3faf9d0de9a3c97f4522ec946a Mon Sep 17 00:00:00 2001 From: apradhana Date: Tue, 19 Mar 2024 10:27:45 -0700 Subject: [PATCH] Add unit test to read float grid with setSaveFloatAsHalf on disk. Signed-off-by: apradhana --- openvdb/openvdb/unittest/TestGridIO.cc | 79 ++++++++++++++++---------- 1 file changed, 50 insertions(+), 29 deletions(-) diff --git a/openvdb/openvdb/unittest/TestGridIO.cc b/openvdb/openvdb/unittest/TestGridIO.cc index ab5c99079e..1eec6ec0a0 100644 --- a/openvdb/openvdb/unittest/TestGridIO.cc +++ b/openvdb/openvdb/unittest/TestGridIO.cc @@ -199,45 +199,66 @@ void TestGridIO::readWriteHalfGridTest() { using namespace openvdb; - float TESTFLOAT = 65504.f + 100.f; - openvdb::math::half TESTHALF = 65504.f + 100.f; - std::string fileName = "testfloat.vdb"; - openvdb::FloatGrid::Ptr floatGrid = openvdb::FloatGrid::create(2.f); - floatGrid->setTransform(openvdb::math::Transform::createLinearTransform(/*voxel size=*/1.0)); - floatGrid->setName("float_grid"); - floatGrid->setSaveFloatAsHalf(true); - openvdb::FloatGrid::Accessor floatAcc = floatGrid->getAccessor(); + + float TEST_FLOAT = 65504.f + 100.f; + openvdb::math::half TEST_HALF = 65504.f + 100.f; openvdb::Coord xyz(1000, -200000000, 30000000); - floatAcc.setValue(xyz, TESTFLOAT); - openvdb::HalfGrid::Ptr halfGrid = openvdb::HalfGrid::create(2.f); - halfGrid->setTransform(openvdb::math::Transform::createLinearTransform(/*voxel size=*/1.0)); - halfGrid->setName("half_grid"); - halfGrid->setSaveFloatAsHalf(true); - openvdb::HalfGrid::Accessor halfAcc = halfGrid->getAccessor(); - halfAcc.setValue(xyz, TESTHALF); + std::string fileName = "testHalfAndFloat.vdb"; + openvdb::FloatGrid::Ptr fgfd = openvdb::FloatGrid::create(2.f); // float grid stored as float on disk + fgfd->setTransform(openvdb::math::Transform::createLinearTransform(/*voxel size=*/1.0)); + fgfd->setName("float_grid"); + + openvdb::HalfGrid::Ptr hghd = openvdb::HalfGrid::create(2.f); // half grid stored as half on disk + hghd->setTransform(openvdb::math::Transform::createLinearTransform(/*voxel size=*/1.0)); + hghd->setName("half_grid"); + + openvdb::FloatGrid::Ptr fghd = openvdb::FloatGrid::create(2.f); // float grid stored as half on disk + fghd->setTransform(openvdb::math::Transform::createLinearTransform(/*voxel size=*/1.0)); + fghd->setName("float_grid_as_half"); + fghd->setSaveFloatAsHalf(true); + + { + openvdb::FloatGrid::Accessor fgfdAcc = fgfd->getAccessor(); + openvdb::HalfGrid::Accessor hghdAcc = hghd->getAccessor(); + openvdb::FloatGrid::Accessor fghdAcc = fghd->getAccessor(); + fgfdAcc.setValue(xyz, TEST_FLOAT); + hghdAcc.setValue(xyz, TEST_HALF); + fghdAcc.setValue(xyz, TEST_FLOAT); + } openvdb::io::File writeFile(fileName); openvdb::GridPtrVec grids; - grids.push_back(floatGrid); - grids.push_back(halfGrid); + grids.push_back(fgfd); + grids.push_back(hghd); + grids.push_back(fghd); writeFile.write(grids); writeFile.close(); openvdb::io::File readFile(fileName); readFile.open(); - openvdb::FloatGrid::Ptr readFloatGrid = openvdb::gridPtrCast(readFile.readGrid("float_grid")); - openvdb::HalfGrid::Ptr readHalfGrid = openvdb::gridPtrCast(readFile.readGrid("half_grid")); - openvdb::FloatGrid::Accessor readFloatAcc = readFloatGrid->getAccessor(); - openvdb::HalfGrid::Accessor readHalfAcc = readHalfGrid->getAccessor(); - - EXPECT_EQ(math::half::posInf(), readFloatAcc.getValue(xyz)); - EXPECT_EQ(math::half::posInf(), readHalfAcc.getValue(xyz)); - - std::cout << "readFloatGrid = " << readFloatGrid << "\treadHalfGrid = " << readHalfGrid << std::endl; - std::cout << "readFloatAcc.getValue(xyz) = " << readFloatAcc.getValue(xyz) << std::endl; - std::cout << "readHalfAcc.getValue(xyz) = " << readHalfAcc.getValue(xyz) << std::endl; + openvdb::FloatGrid::Ptr readFgfd = openvdb::gridPtrCast(readFile.readGrid("float_grid")); + openvdb::HalfGrid::Ptr readHghd = openvdb::gridPtrCast(readFile.readGrid("half_grid")); + openvdb::FloatGrid::Ptr readFghdFloat = openvdb::gridPtrCast(readFile.readGrid("float_grid_as_half")); + openvdb::HalfGrid::Ptr readFghdHalf = openvdb::gridPtrCast(readFile.readGrid("float_grid_as_half")); + + EXPECT_TRUE(readFgfd != NULL); + EXPECT_TRUE(readHghd != NULL); + EXPECT_TRUE(readFghdFloat != NULL); + EXPECT_TRUE(readFghdHalf == NULL); + + { + openvdb::FloatGrid::Accessor fgfdAcc = readFgfd->getAccessor(); + openvdb::HalfGrid::Accessor hghdAcc = readHghd->getAccessor(); + openvdb::FloatGrid::Accessor fghdAcc = readFghdFloat->getAccessor(); + + EXPECT_EQ(fgfdAcc.getValue(xyz), TEST_FLOAT); + EXPECT_EQ(hghdAcc.getValue(xyz), math::half::posInf()); + EXPECT_EQ(fghdAcc.getValue(xyz), math::half::posInf()); + } + readFile.close(); + ::remove(fileName.c_str()); } @@ -248,4 +269,4 @@ TEST_F(TestGridIO, testReadAllBool) { readAllTest(); } TEST_F(TestGridIO, testReadAllFloat) { readAllTest(); } TEST_F(TestGridIO, testReadAllVec3S) { readAllTest(); } TEST_F(TestGridIO, testReadAllFloat5432) { Float5432Grid::registerGrid(); readAllTest(); } -TEST_F(TestGridIO, testReadWriteHalfGrid) { readWriteHalfGridTest(); } \ No newline at end of file +TEST_F(TestGridIO, testReadWriteHalfGrid) { readWriteHalfGridTest(); }