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{
1618public:
1719 static void testBuffer ();
20+ static void testTreeIO ();
1821};
1922
2023template <typename T>
2124void
2225TestLeafIO<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
51110class 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
63125TEST_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);
0 commit comments