Skip to content

Commit c501029

Browse files
committed
Move PointData I/O overloads to a new header
Signed-off-by: Dan Bailey <danbailey@ilm.com>
1 parent 786d025 commit c501029

3 files changed

Lines changed: 151 additions & 128 deletions

File tree

openvdb/openvdb/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ set(OPENVDB_LIBRARY_POINTS_INCLUDE_FILES
422422
points/PointConversion.h
423423
points/PointCount.h
424424
points/PointDataGrid.h
425+
points/PointDataIO.h
425426
points/PointDelete.h
426427
points/PointGroup.h
427428
points/PointMask.h

openvdb/openvdb/points/PointDataGrid.h

Lines changed: 2 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -31,140 +31,14 @@
3131
#include <utility> // std::pair, std::make_pair
3232
#include <vector>
3333

34+
#include <openvdb/points/PointDataIO.h> // io::readCompressedValues(), io::writeCompressedValues(), io::writeCompressedValuesSize()
35+
3436
class TestPointDataLeaf;
3537

3638
namespace openvdb {
3739
OPENVDB_USE_VERSION_NAMESPACE
3840
namespace OPENVDB_VERSION_NAME {
3941

40-
namespace io
41-
{
42-
43-
/// @brief openvdb::io::readCompressedValues specialized on PointDataIndex32 arrays to
44-
/// ignore the value mask, use a larger block size and use 16-bit size instead of 64-bit
45-
template<>
46-
inline void
47-
readCompressedValues( std::istream& is, PointDataIndex32* destBuf, Index destCount,
48-
const util::NodeMask<3>& /*valueMask*/, bool /*fromHalf*/)
49-
{
50-
using compression::bloscDecompress;
51-
52-
const bool seek = destBuf == nullptr;
53-
54-
const size_t destBytes = destCount*sizeof(PointDataIndex32);
55-
const size_t maximumBytes = std::numeric_limits<uint16_t>::max();
56-
if (destBytes >= maximumBytes) {
57-
OPENVDB_THROW(openvdb::IoError, "Cannot read more than " <<
58-
maximumBytes << " bytes in voxel values.")
59-
}
60-
61-
uint16_t bytes16;
62-
63-
const io::StreamMetadata::Ptr meta = io::getStreamMetadataPtr(is);
64-
65-
if (seek && meta) {
66-
// buffer size temporarily stored in the StreamMetadata pass
67-
// to avoid having to perform an expensive disk read for 2-bytes
68-
bytes16 = static_cast<uint16_t>(meta->pass());
69-
// seek over size of the compressed buffer
70-
is.seekg(sizeof(uint16_t), std::ios_base::cur);
71-
}
72-
else {
73-
// otherwise read from disk
74-
is.read(reinterpret_cast<char*>(&bytes16), sizeof(uint16_t));
75-
}
76-
77-
if (bytes16 == std::numeric_limits<uint16_t>::max()) {
78-
// read or seek uncompressed data
79-
if (seek) {
80-
is.seekg(destBytes, std::ios_base::cur);
81-
}
82-
else {
83-
is.read(reinterpret_cast<char*>(destBuf), destBytes);
84-
}
85-
}
86-
else {
87-
// read or seek uncompressed data
88-
if (seek) {
89-
is.seekg(int(bytes16), std::ios_base::cur);
90-
}
91-
else {
92-
// decompress into the destination buffer
93-
std::unique_ptr<char[]> bloscBuffer(new char[int(bytes16)]);
94-
is.read(bloscBuffer.get(), bytes16);
95-
std::unique_ptr<char[]> buffer = bloscDecompress( bloscBuffer.get(),
96-
destBytes,
97-
/*resize=*/false);
98-
std::memcpy(destBuf, buffer.get(), destBytes);
99-
}
100-
}
101-
}
102-
103-
/// @brief openvdb::io::writeCompressedValues specialized on PointDataIndex32 arrays to
104-
/// ignore the value mask, use a larger block size and use 16-bit size instead of 64-bit
105-
template<>
106-
inline void
107-
writeCompressedValues( std::ostream& os, const PointDataIndex32* srcBuf, Index srcCount,
108-
const util::NodeMask<3>& /*valueMask*/,
109-
const util::NodeMask<3>& /*childMask*/, bool /*toHalf*/)
110-
{
111-
using compression::bloscCompress;
112-
113-
const size_t srcBytes = srcCount*sizeof(PointDataIndex32);
114-
const size_t maximumBytes = std::numeric_limits<uint16_t>::max();
115-
if (srcBytes >= maximumBytes) {
116-
OPENVDB_THROW(openvdb::IoError, "Cannot write more than " <<
117-
maximumBytes << " bytes in voxel values.")
118-
}
119-
120-
const char* charBuffer = reinterpret_cast<const char*>(srcBuf);
121-
122-
size_t compressedBytes;
123-
std::unique_ptr<char[]> buffer = bloscCompress( charBuffer, srcBytes,
124-
compressedBytes, /*resize=*/false);
125-
126-
if (compressedBytes > 0) {
127-
auto bytes16 = static_cast<uint16_t>(compressedBytes); // clamp to 16-bit unsigned integer
128-
os.write(reinterpret_cast<const char*>(&bytes16), sizeof(uint16_t));
129-
os.write(reinterpret_cast<const char*>(buffer.get()), compressedBytes);
130-
}
131-
else {
132-
auto bytes16 = static_cast<uint16_t>(maximumBytes); // max value indicates uncompressed
133-
os.write(reinterpret_cast<const char*>(&bytes16), sizeof(uint16_t));
134-
os.write(reinterpret_cast<const char*>(srcBuf), srcBytes);
135-
}
136-
}
137-
138-
template <typename T>
139-
inline void
140-
writeCompressedValuesSize(std::ostream& os, const T* srcBuf, Index srcCount)
141-
{
142-
using compression::bloscCompressedSize;
143-
144-
const size_t srcBytes = srcCount*sizeof(T);
145-
const size_t maximumBytes = std::numeric_limits<uint16_t>::max();
146-
if (srcBytes >= maximumBytes) {
147-
OPENVDB_THROW(openvdb::IoError, "Cannot write more than " <<
148-
maximumBytes << " bytes in voxel values.")
149-
}
150-
151-
const char* charBuffer = reinterpret_cast<const char*>(srcBuf);
152-
153-
// calculate voxel buffer size after compression
154-
size_t compressedBytes = bloscCompressedSize(charBuffer, srcBytes);
155-
156-
if (compressedBytes > 0) {
157-
auto bytes16 = static_cast<uint16_t>(compressedBytes); // clamp to 16-bit unsigned integer
158-
os.write(reinterpret_cast<const char*>(&bytes16), sizeof(uint16_t));
159-
}
160-
else {
161-
auto bytes16 = static_cast<uint16_t>(maximumBytes); // max value indicates uncompressed
162-
os.write(reinterpret_cast<const char*>(&bytes16), sizeof(uint16_t));
163-
}
164-
}
165-
166-
} // namespace io
167-
16842

16943
// forward declaration
17044
namespace tree {
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
// Copyright Contributors to the OpenVDB Project
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#ifndef OPENVDB_POINTS_POINT_DATA_IO_HAS_BEEN_INCLUDED
5+
#define OPENVDB_POINTS_POINT_DATA_IO_HAS_BEEN_INCLUDED
6+
7+
8+
namespace openvdb {
9+
OPENVDB_USE_VERSION_NAMESPACE
10+
namespace OPENVDB_VERSION_NAME {
11+
12+
13+
////////////////////////////////////////
14+
15+
16+
namespace io
17+
{
18+
19+
/// @brief openvdb::io::readCompressedValues specialized on PointDataIndex32 arrays to
20+
/// ignore the value mask, use a larger block size and use 16-bit size instead of 64-bit
21+
template<>
22+
inline void
23+
readCompressedValues( std::istream& is, PointDataIndex32* destBuf, Index destCount,
24+
const util::NodeMask<3>& /*valueMask*/, bool /*fromHalf*/)
25+
{
26+
using compression::bloscDecompress;
27+
28+
const bool seek = destBuf == nullptr;
29+
30+
const size_t destBytes = destCount*sizeof(PointDataIndex32);
31+
const size_t maximumBytes = std::numeric_limits<uint16_t>::max();
32+
if (destBytes >= maximumBytes) {
33+
OPENVDB_THROW(openvdb::IoError, "Cannot read more than " <<
34+
maximumBytes << " bytes in voxel values.")
35+
}
36+
37+
uint16_t bytes16;
38+
39+
const io::StreamMetadata::Ptr meta = io::getStreamMetadataPtr(is);
40+
41+
if (seek && meta) {
42+
// buffer size temporarily stored in the StreamMetadata pass
43+
// to avoid having to perform an expensive disk read for 2-bytes
44+
bytes16 = static_cast<uint16_t>(meta->pass());
45+
// seek over size of the compressed buffer
46+
is.seekg(sizeof(uint16_t), std::ios_base::cur);
47+
}
48+
else {
49+
// otherwise read from disk
50+
is.read(reinterpret_cast<char*>(&bytes16), sizeof(uint16_t));
51+
}
52+
53+
if (bytes16 == std::numeric_limits<uint16_t>::max()) {
54+
// read or seek uncompressed data
55+
if (seek) {
56+
is.seekg(destBytes, std::ios_base::cur);
57+
}
58+
else {
59+
is.read(reinterpret_cast<char*>(destBuf), destBytes);
60+
}
61+
}
62+
else {
63+
// read or seek uncompressed data
64+
if (seek) {
65+
is.seekg(int(bytes16), std::ios_base::cur);
66+
}
67+
else {
68+
// decompress into the destination buffer
69+
std::unique_ptr<char[]> bloscBuffer(new char[int(bytes16)]);
70+
is.read(bloscBuffer.get(), bytes16);
71+
std::unique_ptr<char[]> buffer = bloscDecompress( bloscBuffer.get(),
72+
destBytes,
73+
/*resize=*/false);
74+
std::memcpy(destBuf, buffer.get(), destBytes);
75+
}
76+
}
77+
}
78+
79+
/// @brief openvdb::io::writeCompressedValues specialized on PointDataIndex32 arrays to
80+
/// ignore the value mask, use a larger block size and use 16-bit size instead of 64-bit
81+
template<>
82+
inline void
83+
writeCompressedValues( std::ostream& os, const PointDataIndex32* srcBuf, Index srcCount,
84+
const util::NodeMask<3>& /*valueMask*/,
85+
const util::NodeMask<3>& /*childMask*/, bool /*toHalf*/)
86+
{
87+
using compression::bloscCompress;
88+
89+
const size_t srcBytes = srcCount*sizeof(PointDataIndex32);
90+
const size_t maximumBytes = std::numeric_limits<uint16_t>::max();
91+
if (srcBytes >= maximumBytes) {
92+
OPENVDB_THROW(openvdb::IoError, "Cannot write more than " <<
93+
maximumBytes << " bytes in voxel values.")
94+
}
95+
96+
const char* charBuffer = reinterpret_cast<const char*>(srcBuf);
97+
98+
size_t compressedBytes;
99+
std::unique_ptr<char[]> buffer = bloscCompress( charBuffer, srcBytes,
100+
compressedBytes, /*resize=*/false);
101+
102+
if (compressedBytes > 0) {
103+
auto bytes16 = static_cast<uint16_t>(compressedBytes); // clamp to 16-bit unsigned integer
104+
os.write(reinterpret_cast<const char*>(&bytes16), sizeof(uint16_t));
105+
os.write(reinterpret_cast<const char*>(buffer.get()), compressedBytes);
106+
}
107+
else {
108+
auto bytes16 = static_cast<uint16_t>(maximumBytes); // max value indicates uncompressed
109+
os.write(reinterpret_cast<const char*>(&bytes16), sizeof(uint16_t));
110+
os.write(reinterpret_cast<const char*>(srcBuf), srcBytes);
111+
}
112+
}
113+
114+
template <typename T>
115+
inline void
116+
writeCompressedValuesSize(std::ostream& os, const T* srcBuf, Index srcCount)
117+
{
118+
using compression::bloscCompressedSize;
119+
120+
const size_t srcBytes = srcCount*sizeof(T);
121+
const size_t maximumBytes = std::numeric_limits<uint16_t>::max();
122+
if (srcBytes >= maximumBytes) {
123+
OPENVDB_THROW(openvdb::IoError, "Cannot write more than " <<
124+
maximumBytes << " bytes in voxel values.")
125+
}
126+
127+
const char* charBuffer = reinterpret_cast<const char*>(srcBuf);
128+
129+
// calculate voxel buffer size after compression
130+
size_t compressedBytes = bloscCompressedSize(charBuffer, srcBytes);
131+
132+
if (compressedBytes > 0) {
133+
auto bytes16 = static_cast<uint16_t>(compressedBytes); // clamp to 16-bit unsigned integer
134+
os.write(reinterpret_cast<const char*>(&bytes16), sizeof(uint16_t));
135+
}
136+
else {
137+
auto bytes16 = static_cast<uint16_t>(maximumBytes); // max value indicates uncompressed
138+
os.write(reinterpret_cast<const char*>(&bytes16), sizeof(uint16_t));
139+
}
140+
}
141+
142+
} // namespace io
143+
144+
145+
} // namespace OPENVDB_VERSION_NAME
146+
} // namespace openvdb
147+
148+
#endif // OPENVDB_POINTS_POINT_DATA_IO_HAS_BEEN_INCLUDED

0 commit comments

Comments
 (0)