Skip to content

Commit 1c4162e

Browse files
authored
Merge pull request #2155 from danrbailey/remove_leaf_io_testing
Update all existing unit tests to use Grid I/O methods
2 parents 37851c8 + 7716263 commit 1c4162e

9 files changed

Lines changed: 517 additions & 284 deletions

File tree

openvdb/openvdb/unittest/TestFile.cc

Lines changed: 1 addition & 183 deletions
Large diffs are not rendered by default.

openvdb/openvdb/unittest/TestLeafBool.cc

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <openvdb/Types.h>
66
#include <openvdb/tools/Filter.h>
77
#include <openvdb/util/logging.h>
8+
#include <openvdb/io/io.h>
89
#include "util.h" // for unittest_util::makeSphere()
910
#include <gtest/gtest.h>
1011
#include <set>
@@ -293,6 +294,60 @@ TEST_F(TestLeafBool, testIO)
293294
leaf.setValueOn(openvdb::Coord(0, 1, 0));
294295
leaf.setValueOn(openvdb::Coord(1, 0, 0));
295296

297+
// read and write topology to disk
298+
299+
{
300+
// create a grid with the leaf for topology testing
301+
typedef openvdb::Grid<openvdb::tree::Tree<openvdb::tree::RootNode<
302+
openvdb::tree::InternalNode<openvdb::tree::InternalNode<LeafType, 4>, 5>>>> BoolGrid;
303+
BoolGrid::Ptr grid = BoolGrid::create();
304+
grid->setName("bool_leaf");
305+
grid->tree().addLeaf(new LeafType(leaf));
306+
307+
openvdb::GridCPtrVec grids;
308+
grids.push_back(grid);
309+
310+
// write to file
311+
{
312+
openvdb::io::File file("leaf_bool.vdb");
313+
file.write(grids);
314+
file.close();
315+
}
316+
317+
// read grid from file
318+
BoolGrid::Ptr gridFromDisk;
319+
{
320+
openvdb::io::File file("leaf_bool.vdb");
321+
file.open();
322+
openvdb::GridBase::Ptr baseGrid = file.readGrid("bool_leaf");
323+
file.close();
324+
325+
gridFromDisk = openvdb::gridPtrCast<BoolGrid>(baseGrid);
326+
}
327+
328+
LeafType* leaf2 = gridFromDisk->tree().probeLeaf(origin);
329+
EXPECT_TRUE(leaf2);
330+
331+
// check topology and values match
332+
333+
EXPECT_EQ(origin, leaf2->origin());
334+
EXPECT_TRUE(leaf2->isValueOn(openvdb::Coord(0, 1, 0)));
335+
EXPECT_TRUE(leaf2->isValueOn(openvdb::Coord(1, 0, 0)));
336+
EXPECT_TRUE(leaf2->onVoxelCount() == 2);
337+
338+
remove("leaf_bool.vdb");
339+
}
340+
}
341+
342+
343+
TEST_F(TestLeafBool, testTreeIO)
344+
{
345+
LeafType leaf(openvdb::Coord(1, 3, 5));
346+
const openvdb::Coord origin = leaf.origin();
347+
348+
leaf.setValueOn(openvdb::Coord(0, 1, 0));
349+
leaf.setValueOn(openvdb::Coord(1, 0, 0));
350+
296351
std::ostringstream ostr(std::ios_base::binary);
297352

298353
leaf.writeBuffers(ostr);
@@ -301,8 +356,6 @@ TEST_F(TestLeafBool, testIO)
301356
leaf.setValueOn(openvdb::Coord(0, 1, 1));
302357

303358
std::istringstream istr(ostr.str(), std::ios_base::binary);
304-
// Since the input stream doesn't include a VDB header with file format version info,
305-
// tag the input stream explicitly with the current version number.
306359
openvdb::io::setCurrentVersion(istr);
307360

308361
leaf.readBuffers(istr);

openvdb/openvdb/unittest/TestLeafIO.cc

Lines changed: 128 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
#include <openvdb/Exceptions.h>
5+
#include <openvdb/openvdb.h>
56
#include <openvdb/tree/LeafNode.h>
67
#include <openvdb/Types.h>
8+
#include <openvdb/io/io.h>
79
#include <gtest/gtest.h>
810

911
#include <cctype> // for toupper()
@@ -15,13 +17,73 @@ class TestLeafIO
1517
{
1618
public:
1719
static void testBuffer();
20+
static void testTreeIO();
1821
};
1922

2023
template<typename T>
2124
void
2225
TestLeafIO<T>::testBuffer()
2326
{
24-
openvdb::tree::LeafNode<T, 3> leaf(openvdb::Coord(0, 0, 0));
27+
using LeafT = openvdb::tree::LeafNode<T, 3>;
28+
LeafT leaf(openvdb::Coord(0, 0, 0));
29+
const openvdb::Coord origin = leaf.origin();
30+
31+
leaf.setValueOn(openvdb::Coord(0, 1, 0), T(1));
32+
leaf.setValueOn(openvdb::Coord(1, 0, 0), T(1));
33+
34+
// read and write topology to disk
35+
36+
{
37+
// create a grid with the leaf for topology testing
38+
typedef openvdb::Grid<openvdb::tree::Tree<openvdb::tree::RootNode<
39+
openvdb::tree::InternalNode<openvdb::tree::InternalNode<LeafT, 4>, 5>>>> GridType;
40+
if (!GridType::isRegistered()) GridType::registerGrid();
41+
42+
typename GridType::Ptr grid = GridType::create();
43+
grid->setName("leaf_io");
44+
grid->tree().addLeaf(new LeafT(leaf));
45+
46+
openvdb::GridCPtrVec grids;
47+
grids.push_back(grid);
48+
49+
// write to file
50+
{
51+
openvdb::io::File file("leaf_io.vdb");
52+
file.write(grids);
53+
file.close();
54+
}
55+
56+
// read grid from file
57+
typename GridType::Ptr gridFromDisk;
58+
{
59+
openvdb::io::File file("leaf_io.vdb");
60+
file.open();
61+
openvdb::GridBase::Ptr baseGrid = file.readGrid("leaf_io");
62+
file.close();
63+
64+
gridFromDisk = openvdb::gridPtrCast<GridType>(baseGrid);
65+
}
66+
67+
LeafT* leaf2 = gridFromDisk->tree().probeLeaf(origin);
68+
EXPECT_TRUE(leaf2);
69+
70+
// check topology and values match
71+
72+
EXPECT_NEAR(T(1), leaf2->getValue(openvdb::Coord(0, 1, 0)), /*tolerance=*/0);
73+
EXPECT_NEAR(T(1), leaf2->getValue(openvdb::Coord(1, 0, 0)), /*tolerance=*/0);
74+
EXPECT_TRUE(leaf2->onVoxelCount() == 2);
75+
76+
remove("leaf_io.vdb");
77+
}
78+
}
79+
80+
81+
template<typename T>
82+
void
83+
TestLeafIO<T>::testTreeIO()
84+
{
85+
using LeafT = openvdb::tree::LeafNode<T, 3>;
86+
LeafT leaf(openvdb::Coord(0, 0, 0));
2587

2688
leaf.setValueOn(openvdb::Coord(0, 1, 0), T(1));
2789
leaf.setValueOn(openvdb::Coord(1, 0, 0), T(1));
@@ -34,9 +96,6 @@ TestLeafIO<T>::testBuffer()
3496
leaf.setValueOn(openvdb::Coord(0, 1, 1), T(1));
3597

3698
std::istringstream istr(ostr.str(), std::ios_base::binary);
37-
38-
// Since the input stream doesn't include a VDB header with file format version info,
39-
// tag the input stream explicitly with the current version number.
4099
openvdb::io::setCurrentVersion(istr);
41100

42101
leaf.readBuffers(istr);
@@ -50,6 +109,9 @@ TestLeafIO<T>::testBuffer()
50109

51110
class TestLeafIOTest: public ::testing::Test
52111
{
112+
public:
113+
void SetUp() override { openvdb::initialize(); }
114+
void TearDown() override { openvdb::uninitialize(); }
53115
};
54116

55117

@@ -62,7 +124,68 @@ TEST_F(TestLeafIOTest, testBufferByte) { TestLeafIO<openvdb::Byte>::testBuffer()
62124

63125
TEST_F(TestLeafIOTest, testBufferVec3R)
64126
{
65-
openvdb::tree::LeafNode<openvdb::Vec3R, 3> leaf(openvdb::Coord(0, 0, 0));
127+
using LeafT = openvdb::tree::LeafNode<openvdb::Vec3R, 3>;
128+
LeafT leaf(openvdb::Coord(0, 0, 0));
129+
const openvdb::Coord origin = leaf.origin();
130+
131+
leaf.setValueOn(openvdb::Coord(0, 1, 0), openvdb::Vec3R(1, 1, 1));
132+
leaf.setValueOn(openvdb::Coord(1, 0, 0), openvdb::Vec3R(1, 1, 1));
133+
134+
// read and write topology to disk
135+
136+
{
137+
// create a grid with the leaf for topology testing
138+
typedef openvdb::Grid<openvdb::tree::Tree<openvdb::tree::RootNode<
139+
openvdb::tree::InternalNode<openvdb::tree::InternalNode<LeafT, 4>, 5>>>> GridType;
140+
GridType::Ptr grid = GridType::create();
141+
grid->setName("leaf_vec3r");
142+
grid->tree().addLeaf(new LeafT(leaf));
143+
144+
openvdb::GridCPtrVec grids;
145+
grids.push_back(grid);
146+
147+
// write to file
148+
{
149+
openvdb::io::File file("leaf_vec3r.vdb");
150+
file.write(grids);
151+
file.close();
152+
}
153+
154+
// read grid from file
155+
GridType::Ptr gridFromDisk;
156+
{
157+
openvdb::io::File file("leaf_vec3r.vdb");
158+
file.open();
159+
openvdb::GridBase::Ptr baseGrid = file.readGrid("leaf_vec3r");
160+
file.close();
161+
162+
gridFromDisk = openvdb::gridPtrCast<GridType>(baseGrid);
163+
}
164+
165+
LeafT* leaf2 = gridFromDisk->tree().probeLeaf(origin);
166+
EXPECT_TRUE(leaf2);
167+
168+
// check topology and values match
169+
170+
EXPECT_TRUE(leaf2->getValue(openvdb::Coord(0, 1, 0)) == openvdb::Vec3R(1, 1, 1));
171+
EXPECT_TRUE(leaf2->getValue(openvdb::Coord(1, 0, 0)) == openvdb::Vec3R(1, 1, 1));
172+
EXPECT_TRUE(leaf2->onVoxelCount() == 2);
173+
174+
remove("leaf_vec3r.vdb");
175+
}
176+
}
177+
178+
TEST_F(TestLeafIOTest, testTreeIOInt) { TestLeafIO<int>::testTreeIO(); }
179+
TEST_F(TestLeafIOTest, testTreeIOFloat) { TestLeafIO<float>::testTreeIO(); }
180+
TEST_F(TestLeafIOTest, testTreeIODouble) { TestLeafIO<double>::testTreeIO(); }
181+
TEST_F(TestLeafIOTest, testTreeIOBool) { TestLeafIO<bool>::testTreeIO(); }
182+
TEST_F(TestLeafIOTest, testTreeIOByte) { TestLeafIO<openvdb::Byte>::testTreeIO(); }
183+
184+
185+
TEST_F(TestLeafIOTest, testTreeIOVec3R)
186+
{
187+
using LeafT = openvdb::tree::LeafNode<openvdb::Vec3R, 3>;
188+
LeafT leaf(openvdb::Coord(0, 0, 0));
66189

67190
leaf.setValueOn(openvdb::Coord(0, 1, 0), openvdb::Vec3R(1, 1, 1));
68191
leaf.setValueOn(openvdb::Coord(1, 0, 0), openvdb::Vec3R(1, 1, 1));
@@ -75,9 +198,6 @@ TEST_F(TestLeafIOTest, testBufferVec3R)
75198
leaf.setValueOn(openvdb::Coord(0, 1, 1), openvdb::Vec3R(1, 1, 1));
76199

77200
std::istringstream istr(ostr.str(), std::ios_base::binary);
78-
79-
// Since the input stream doesn't include a VDB header with file format version info,
80-
// tag the input stream explicitly with the current version number.
81201
openvdb::io::setCurrentVersion(istr);
82202

83203
leaf.readBuffers(istr);

openvdb/openvdb/unittest/TestLeafMask.cc

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <openvdb/tools/Filter.h>
77
#include <openvdb/tree/LeafNode.h>
88
#include <openvdb/util/logging.h>
9+
#include <openvdb/io/io.h>
910
#include "util.h" // for unittest_util::makeSphere()
1011
#include <gtest/gtest.h>
1112
#include <set>
@@ -290,6 +291,60 @@ TEST_F(TestLeafMask, testIO)
290291
leaf.setValueOn(openvdb::Coord(0, 1, 0));
291292
leaf.setValueOn(openvdb::Coord(1, 0, 0));
292293

294+
// read and write topology to disk
295+
296+
{
297+
// create a grid with the leaf for topology testing
298+
typedef openvdb::Grid<openvdb::tree::Tree<openvdb::tree::RootNode<
299+
openvdb::tree::InternalNode<openvdb::tree::InternalNode<LeafType, 4>, 5>>>> MaskGrid;
300+
MaskGrid::Ptr grid = MaskGrid::create();
301+
grid->setName("leaf_mask");
302+
grid->tree().addLeaf(new LeafType(leaf));
303+
304+
openvdb::GridCPtrVec grids;
305+
grids.push_back(grid);
306+
307+
// write to file
308+
{
309+
openvdb::io::File file("leaf_mask.vdb");
310+
file.write(grids);
311+
file.close();
312+
}
313+
314+
// read grid from file
315+
MaskGrid::Ptr gridFromDisk;
316+
{
317+
openvdb::io::File file("leaf_mask.vdb");
318+
file.open();
319+
openvdb::GridBase::Ptr baseGrid = file.readGrid("leaf_mask");
320+
file.close();
321+
322+
gridFromDisk = openvdb::gridPtrCast<MaskGrid>(baseGrid);
323+
}
324+
325+
LeafType* leaf2 = gridFromDisk->tree().probeLeaf(origin);
326+
EXPECT_TRUE(leaf2);
327+
328+
// check topology and values match
329+
330+
EXPECT_EQ(origin, leaf2->origin());
331+
EXPECT_TRUE(leaf2->isValueOn(openvdb::Coord(0, 1, 0)));
332+
EXPECT_TRUE(leaf2->isValueOn(openvdb::Coord(1, 0, 0)));
333+
EXPECT_TRUE(leaf2->onVoxelCount() == 2);
334+
335+
remove("leaf_mask.vdb");
336+
}
337+
}
338+
339+
340+
TEST_F(TestLeafMask, testTreeIO)
341+
{
342+
LeafType leaf(openvdb::Coord(1, 3, 5));
343+
const openvdb::Coord origin = leaf.origin();
344+
345+
leaf.setValueOn(openvdb::Coord(0, 1, 0));
346+
leaf.setValueOn(openvdb::Coord(1, 0, 0));
347+
293348
std::ostringstream ostr(std::ios_base::binary);
294349

295350
leaf.writeBuffers(ostr);
@@ -298,8 +353,6 @@ TEST_F(TestLeafMask, testIO)
298353
leaf.setValueOn(openvdb::Coord(0, 1, 1));
299354

300355
std::istringstream istr(ostr.str(), std::ios_base::binary);
301-
// Since the input stream doesn't include a VDB header with file format version info,
302-
// tag the input stream explicitly with the current version number.
303356
openvdb::io::setCurrentVersion(istr);
304357

305358
leaf.readBuffers(istr);

openvdb/openvdb/unittest/TestMultiResGrid.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ TEST_F(TestMultiResGrid, testIO)
208208
{
209209
using namespace openvdb;
210210

211+
openvdb::initialize();
212+
211213
const float radius = 1.0f;
212214
const Vec3f center(0.0f, 0.0f, 0.0f);
213215
const float voxelSize = 0.01f;
@@ -243,7 +245,6 @@ TEST_F(TestMultiResGrid, testIO)
243245
outputFile.close();
244246

245247
// Read grids
246-
openvdb::initialize();
247248
openvdb::io::File file( filename );
248249
file.open();
249250
GridPtrVecPtr grids = file.getGrids();

0 commit comments

Comments
 (0)