77#include < openvdb/tools/ValueTransformer.h> // for tools::setValueOnMin(), et al.
88#include < openvdb/tree/LeafNode.h>
99#include < openvdb/io/Compression.h> // for io::RealToHalf
10+ #include < openvdb/io/Stream.h>
1011#include < openvdb/math/Math.h> // for Abs()
1112#include < openvdb/openvdb.h>
1213#include < openvdb/util/CpuTimer.h>
@@ -104,6 +105,10 @@ TEST_F(TestTree, testChangeBackground)
104105
105106TEST_F (TestTree, testHalf)
106107{
108+ // explicitly register these grid types as they are not registered by default
109+ openvdb::Grid<openvdb::Vec2STree>::registerGrid ();
110+ openvdb::Grid<openvdb::Vec2DTree>::registerGrid ();
111+
107112 testWriteHalf<openvdb::FloatTree>();
108113 testWriteHalf<openvdb::DoubleTree>();
109114 testWriteHalf<openvdb::Vec2STree>();
@@ -125,27 +130,54 @@ TestTree::testWriteHalf()
125130 using GridType = openvdb::Grid<TreeType>;
126131 using ValueT = typename TreeType::ValueType;
127132 ValueT background (5 );
128- GridType grid (background);
133+ typename GridType::Ptr grid = GridType::create (background);
134+ grid->setName (" density" );
129135
130136 unittest_util::makeSphere<GridType>(openvdb::Coord (64 , 64 , 64 ),
131137 openvdb::Vec3f (35 , 30 , 40 ),
132- /* radius=*/ 10 , grid,
138+ /* radius=*/ 10 , * grid,
133139 /* dx=*/ 1 .0f , unittest_util::SPHERE_DENSE);
134- EXPECT_TRUE (!grid.tree ().empty ());
140+ EXPECT_TRUE (!grid->tree ().empty ());
141+
142+ // find stream and grid header position for save as float
143+
144+ size_t headerSize = 0 ;
145+ {
146+ std::ostringstream outFull (std::ios_base::binary);
147+ openvdb::io::Stream streamFull (outFull);
148+ streamFull.write ({});
149+ openvdb::io::GridDescriptor gd (grid->getName (), grid->type ());
150+ gd.writeHeader (outFull);
151+ headerSize = outFull.str ().size ();
152+ }
153+
154+ // find stream and grid header position for save as half
155+
156+ size_t halfHeaderSize = 0 ;
157+ {
158+ std::ostringstream outHalf (std::ios_base::binary);
159+ openvdb::io::Stream streamHalf (outHalf);
160+ streamHalf.write ({});
161+ openvdb::io::GridDescriptor gdHalf (grid->getName (), grid->type (), /* half=*/ true );
162+ gdHalf.writeHeader (outHalf);
163+ halfHeaderSize = outHalf.str ().size ();
164+ }
135165
136166 // Write grid blocks in both float and half formats.
137167 std::ostringstream outFull (std::ios_base::binary);
138- grid.setSaveFloatAsHalf (false );
139- grid.writeBuffers (outFull);
168+ openvdb::io::Stream streamFull (outFull);
169+ grid->setSaveFloatAsHalf (false );
170+ streamFull.write ({grid});
140171 outFull.flush ();
141- const size_t fullBytes = outFull.str ().size ();
172+ const size_t fullBytes = outFull.str ().size () - headerSize ;
142173 if (fullBytes == 0 ) FAIL () << " wrote empty full float buffers" ;
143174
144175 std::ostringstream outHalf (std::ios_base::binary);
145- grid.setSaveFloatAsHalf (true );
146- grid.writeBuffers (outHalf);
176+ openvdb::io::Stream streamHalf (outHalf);
177+ grid->setSaveFloatAsHalf (true );
178+ streamHalf.write ({grid});
147179 outHalf.flush ();
148- const size_t halfBytes = outHalf.str ().size ();
180+ const size_t halfBytes = outHalf.str ().size () - halfHeaderSize ;
149181 if (halfBytes == 0 ) FAIL () << " wrote empty half float buffers" ;
150182
151183 if (openvdb::io::RealToHalf<ValueT>::isReal) {
@@ -166,22 +198,26 @@ TestTree::testWriteHalf()
166198 // then write it out again in half float format. Verify that the resulting file
167199 // is identical to the original half float file.
168200 {
169- openvdb::Grid<TreeType> gridCopy (grid);
170- gridCopy.setSaveFloatAsHalf (true );
171201 std::istringstream is (outHalf.str (), std::ios_base::binary);
202+ openvdb::io::Stream streamHalf2 (is);
172203
173- // Since the input stream doesn't include a VDB header with file format version info,
174- // tag the input stream explicitly with the current version number.
175- openvdb::io::setCurrentVersion (is);
176-
177- gridCopy.readBuffers (is);
204+ openvdb::GridPtrVecPtr grids = streamHalf2.getGrids ();
205+ openvdb::GridBase::Ptr gridCopy = (*grids)[0 ];
206+ gridCopy->setSaveFloatAsHalf (true );
178207
179208 std::ostringstream outDiff (std::ios_base::binary);
180- gridCopy.writeBuffers (outDiff);
209+ openvdb::io::Stream streamDiff (outDiff);
210+ streamDiff.write ({gridCopy});
181211 outDiff.flush ();
182212
183- if (outHalf.str () != outDiff.str ()) {
184- FAIL () << " half-from-full and half-from-half buffers differ" ;
213+ // Compare the two buffers, skipping the header region
214+ {
215+ const std::string s1 = outHalf.str ().substr (halfHeaderSize);
216+ const std::string s2 = outDiff.str ().substr (halfHeaderSize);
217+ if (s1 != s2)
218+ {
219+ FAIL () << " half-from-full and half-from-half buffers differ" ;
220+ }
185221 }
186222 }
187223}
0 commit comments