OpenVDB 13.2.0
Loading...
Searching...
No Matches
io.h
Go to the documentation of this file.
1// Copyright Contributors to the OpenVDB Project
2// SPDX-License-Identifier: Apache-2.0
3
4#ifndef OPENVDB_IO_IO_HAS_BEEN_INCLUDED
5#define OPENVDB_IO_IO_HAS_BEEN_INCLUDED
6
7#include <openvdb/Platform.h>
8#include <openvdb/Types.h> // for SharedPtr
9#include <openvdb/version.h>
10#include <any>
11#include <functional>
12#include <iosfwd> // for std::ios_base
13#include <map>
14#include <memory>
15#include <string>
16
17class TestMappedFile;
18
19namespace openvdb {
21namespace OPENVDB_VERSION_NAME {
22
23class MetaMap;
24
25namespace io {
26
27/// @brief Container for metadata describing how to unserialize grids from and/or
28/// serialize grids to a stream (which file format, compression scheme, etc. to use)
29/// @details This class is mainly for internal use.
31{
32public:
35
38 explicit StreamMetadata(std::ios_base&);
40
42
43 /// @brief Transfer metadata items directly to the given stream.
44 /// @todo Deprecate direct transfer; use StreamMetadata structs everywhere.
45 void transferTo(std::ios_base&) const;
46
47 uint32_t fileVersion() const;
48 void setFileVersion(uint32_t);
49
52
53 uint32_t compression() const;
54 void setCompression(uint32_t);
55
56 uint32_t gridClass() const;
57 void setGridClass(uint32_t);
58
59 const void* backgroundPtr() const;
60 void setBackgroundPtr(const void*);
61
62 bool halfFloat() const;
63 void setHalfFloat(bool);
64
65 bool writeGridStats() const;
67
68 bool seekable() const;
69 void setSeekable(bool);
70
71 bool delayedLoadMeta() const;
72
73 bool countingPasses() const;
75
76 /// @brief Return @c true if readTopology() should allocate and zero-fill
77 /// leaf buffers after loading the tree structure (topology-only read mode).
78 bool allocateLeafBuffers() const;
80
81 uint32_t pass() const;
82 void setPass(uint32_t);
83
84 //@{
85 /// @brief Return a (reference to a) copy of the metadata of the grid
86 /// currently being read or written.
87 /// @details Some grid metadata might duplicate information returned by
88 /// gridClass(), backgroundPtr() and other accessors, but those values
89 /// are not guaranteed to be kept in sync.
91 const MetaMap& gridMetadata() const;
92 //@}
93
94 using AuxDataMap = std::map<std::string, std::any>;
95 //@{
96 /// @brief Return a map that can be populated with arbitrary user data.
98 const AuxDataMap& auxData() const;
99 //@}
100
101 /// @private
102 uint32_t __test() const;
103 /// @private
104 void __setTest(uint32_t);
105
106 /// Return a string describing this stream metadata.
107 std::string str() const;
108
109private:
110 struct Impl;
111 std::unique_ptr<Impl> mImpl;
112}; // class StreamMetadata
113
114
115/// Write a description of the given metadata to an output stream.
116std::ostream& operator<<(std::ostream&, const StreamMetadata&);
117
118std::ostream& operator<<(std::ostream&, const StreamMetadata::AuxDataMap&);
119
120
121////////////////////////////////////////
122
123
124/// @brief Tag for multi-pass I/O. Only points::PointDataLeafNode may inherit from this.
125/// @sa Grid::hasMultiPassIO(), points::IsPointDataLeafNode
127
128
129////////////////////////////////////////
130
131
132class File;
133
134
135// This class is deprecated and will be removed. Delayed loading is no longer supported.
136// It is retained for API backwards compatibility only, but all methods are no-ops.
138{
139public:
141 using Notifier = std::function<void(std::string /*filename*/)>;
142
143 OPENVDB_DEPRECATED_MESSAGE("This class is deprecated and will be removed. Delayed loading is no longer supported.")
144 explicit MappedFile(const std::string& /*filename*/, bool /*autoDelete*/ = false) {}
145
146 /// @brief Destructor
147 ~MappedFile() = default;
148
149 MappedFile(const MappedFile&) = delete;
150 MappedFile& operator=(const MappedFile&) = delete;
151
152 OPENVDB_DEPRECATED_MESSAGE("Always returns an empty string. This method is deprecated and will be removed. Delayed loading is no longer supported.")
153 std::string filename() const { return std::string(); }
154
155 OPENVDB_DEPRECATED_MESSAGE("Always returns a null stream buffer pointer. This method is deprecated and will be removed. Delayed loading is no longer supported.")
156 SharedPtr<std::streambuf> createBuffer() const { return SharedPtr<std::streambuf>(); }
157
158 OPENVDB_DEPRECATED_MESSAGE("This method is deprecated and will be removed. Delayed loading is no longer supported.")
159 void setNotifier(const Notifier&) {}
160
161 OPENVDB_DEPRECATED_MESSAGE("This method is deprecated and will be removed. Delayed loading is no longer supported.")
163}; // class MappedFile
164
165
166////////////////////////////////////////
167
168
169/// Return a string (possibly empty) describing the given system error code.
170std::string getErrorString(int errorNum);
171
172
173/// Return a string (possibly empty) describing the most recent system error.
174std::string getErrorString();
175
176
177////////////////////////////////////////
178
179
180/// @brief Return the file format version number associated with the given input stream.
181/// @sa File::setFormatVersion()
182OPENVDB_API uint32_t getFormatVersion(std::ios_base&);
183
184/// @brief Throws an IoError if the file format version number is not supported.
185OPENVDB_API void checkFormatVersion(std::ios_base&);
186
187/// @brief Return the (major, minor) library version number associated with the given input stream.
188/// @sa File::setLibraryVersion()
190
191/// @brief Return a string of the form "<major>.<minor>/<format>", giving the library
192/// and file format version numbers associated with the given input stream.
193OPENVDB_API std::string getVersion(std::ios_base&);
194
195/// Associate the current file format and library version numbers with the given input stream.
196OPENVDB_API void setCurrentVersion(std::istream&);
197
198/// @brief Associate specific file format and library version numbers with the given stream.
199/// @details This is typically called immediately after reading a header that contains
200/// the version numbers. Data read subsequently can then be interpreted appropriately.
201OPENVDB_API void setVersion(std::ios_base&, const VersionId& libraryVersion, uint32_t fileVersion);
202
203/// @brief Return a bitwise OR of compression option flags (COMPRESS_ZIP,
204/// COMPRESS_ACTIVE_MASK, etc.) specifying whether and how input data is compressed
205/// or output data should be compressed.
206OPENVDB_API uint32_t getDataCompression(std::ios_base&);
207/// @brief Associate with the given stream a bitwise OR of compression option flags
208/// (COMPRESS_ZIP, COMPRESS_ACTIVE_MASK, etc.) specifying whether and how input data
209/// is compressed or output data should be compressed.
210OPENVDB_API void setDataCompression(std::ios_base&, uint32_t compressionFlags);
211
212/// @brief Return the class (GRID_LEVEL_SET, GRID_UNKNOWN, etc.) of the grid
213/// currently being read from or written to the given stream.
214OPENVDB_API uint32_t getGridClass(std::ios_base&);
215/// @brief Associate with the given stream the class (GRID_LEVEL_SET, GRID_UNKNOWN, etc.)
216/// of the grid currently being read or written.
217OPENVDB_API void setGridClass(std::ios_base&, uint32_t);
218
219/// @brief Return true if floating-point values should be quantized to 16 bits when writing
220/// to the given stream or promoted back from 16-bit to full precision when reading from it.
221OPENVDB_API bool getHalfFloat(std::ios_base&);
222/// @brief Specify whether floating-point values should be quantized to 16 bits when writing
223/// to the given stream or promoted back from 16-bit to full precision when reading from it.
224OPENVDB_API void setHalfFloat(std::ios_base&, bool);
225
226/// @brief Return a pointer to the background value of the grid
227/// currently being read from or written to the given stream.
228OPENVDB_API const void* getGridBackgroundValuePtr(std::ios_base&);
229/// @brief Specify (a pointer to) the background value of the grid
230/// currently being read from or written to the given stream.
231/// @note The pointer must remain valid until the entire grid has been read or written.
232OPENVDB_API void setGridBackgroundValuePtr(std::ios_base&, const void* background);
233
234/// @brief Return @c true if grid statistics (active voxel count and bounding box, etc.)
235/// should be computed and stored as grid metadata when writing to the given stream.
237/// @brief Specify whether to compute grid statistics (active voxel count and bounding box, etc.)
238/// and store them as grid metadata when writing to the given stream.
239OPENVDB_API void setWriteGridStatsMetadata(std::ios_base&, bool writeGridStats);
240
241OPENVDB_DEPRECATED_MESSAGE("Always returns a null pointer. This function is deprecated and will be removed. Delayed loading is no longer supported.")
243OPENVDB_DEPRECATED_MESSAGE("This function is deprecated and will be removed. Delayed loading is no longer supported.")
244inline void setMappedFilePtr(std::ios_base&, SharedPtr<MappedFile>&) { }
245
246/// @brief Return a shared pointer to an object that stores metadata (file format,
247/// compression scheme, etc.) for use when reading from or writing to the given stream.
249/// @brief Associate the given stream with (a shared pointer to) an object that stores
250/// metadata (file format, compression scheme, etc.) for use when reading from
251/// or writing to the stream.
252/// @details If @a transfer is true, copy metadata from the object directly to the stream
253/// (for backward compatibility with older versions of the library).
254/// @note The shared pointer object (not just the io::StreamMetadata object to which it points)
255/// must remain valid until the file is closed.
257 SharedPtr<StreamMetadata>&, bool transfer = true);
258/// @brief Dissociate the given stream from its metadata object (if it has one)
259/// and return a shared pointer to the object.
261
262/// @brief Replaces any StreamMetadata shared pointer associated with the stream
263/// with the given one for the lifetime of this object. When the destructor of this
264/// object is called, the StreamMetadata shared pointer is replaced with the prior one
265/// again.
266/// @details The stream only stores the address of the shared pointer, so this object
267/// owns it and cannot be copied or moved. The stream must outlive this object.
269{
270public:
273
276
277private:
278 std::ios_base& mStream;
280 void* mPrevious;
281}; // class StreamMetadataScope
282
283} // namespace io
284} // namespace OPENVDB_VERSION_NAME
285} // namespace openvdb
286
287#endif // OPENVDB_IO_IO_HAS_BEEN_INCLUDED
#define OPENVDB_API
Definition Platform.h:286
#define OPENVDB_DEPRECATED_MESSAGE(msg)
Definition Platform.h:158
Container that maps names (strings) to values of arbitrary types.
Definition MetaMap.h:20
Grid archive associated with a file on disk.
Definition File.h:31
SharedPtr< MappedFile > Ptr
Definition io.h:140
MappedFile(const std::string &, bool=false)
Definition io.h:144
SharedPtr< std::streambuf > createBuffer() const
Definition io.h:156
MappedFile(const MappedFile &)=delete
std::string filename() const
Definition io.h:153
std::function< void(std::string)> Notifier
Definition io.h:141
MappedFile & operator=(const MappedFile &)=delete
~MappedFile()=default
Destructor.
void clearNotifier()
Definition io.h:162
void setNotifier(const Notifier &)
Definition io.h:159
StreamMetadataScope(std::ios_base &, SharedPtr< StreamMetadata >)
StreamMetadataScope & operator=(const StreamMetadataScope &)=delete
StreamMetadataScope(const StreamMetadataScope &)=delete
Container for metadata describing how to unserialize grids from and/or serialize grids to a stream (w...
Definition io.h:31
std::map< std::string, std::any > AuxDataMap
Definition io.h:94
StreamMetadata & operator=(const StreamMetadata &)
SharedPtr< const StreamMetadata > ConstPtr
Definition io.h:34
SharedPtr< StreamMetadata > Ptr
Definition io.h:33
MetaMap & gridMetadata()
Return a (reference to a) copy of the metadata of the grid currently being read or written.
const void * backgroundPtr() const
const AuxDataMap & auxData() const
AuxDataMap & auxData()
Return a map that can be populated with arbitrary user data.
void setBackgroundPtr(const void *)
bool allocateLeafBuffers() const
Return true if readTopology() should allocate and zero-fill leaf buffers after loading the tree struc...
void transferTo(std::ios_base &) const
Transfer metadata items directly to the given stream.
StreamMetadata(const StreamMetadata &)
const MetaMap & gridMetadata() const
std::string str() const
Return a string describing this stream metadata.
std::ostream & operator<<(std::ostream &os, const nanovdb::math::BBox< nanovdb::math::Vec3< T > > &b)
Definition IO.h:757
bool getWriteGridStatsMetadata(std::ios_base &)
Return true if grid statistics (active voxel count and bounding box, etc.) should be computed and sto...
void setVersion(std::ios_base &, const VersionId &libraryVersion, uint32_t fileVersion)
Associate specific file format and library version numbers with the given stream.
void setWriteGridStatsMetadata(std::ios_base &, bool writeGridStats)
Specify whether to compute grid statistics (active voxel count and bounding box, etc....
std::string getErrorString(int errorNum)
Return a string (possibly empty) describing the given system error code.
void setMappedFilePtr(std::ios_base &, SharedPtr< MappedFile > &)
Definition io.h:244
void setStreamMetadataPtr(std::ios_base &, SharedPtr< StreamMetadata > &, bool transfer=true)
Associate the given stream with (a shared pointer to) an object that stores metadata (file format,...
SharedPtr< MappedFile > getMappedFilePtr(std::ios_base &)
Definition io.h:242
bool getHalfFloat(std::ios_base &)
Return true if floating-point values should be quantized to 16 bits when writing to the given stream ...
void setGridClass(std::ios_base &, uint32_t)
Associate with the given stream the class (GRID_LEVEL_SET, GRID_UNKNOWN, etc.) of the grid currently ...
VersionId getLibraryVersion(std::ios_base &)
Return the (major, minor) library version number associated with the given input stream.
void setCurrentVersion(std::istream &)
Associate the current file format and library version numbers with the given input stream.
uint32_t getDataCompression(std::ios_base &)
Return a bitwise OR of compression option flags (COMPRESS_ZIP, COMPRESS_ACTIVE_MASK,...
void checkFormatVersion(std::ios_base &)
Throws an IoError if the file format version number is not supported.
void setDataCompression(std::ios_base &, uint32_t compressionFlags)
Associate with the given stream a bitwise OR of compression option flags (COMPRESS_ZIP,...
std::string getVersion(std::ios_base &)
Return a string of the form "<major>.<minor>/<format>", giving the library and file format version nu...
uint32_t getFormatVersion(std::ios_base &)
Return the file format version number associated with the given input stream.
SharedPtr< StreamMetadata > getStreamMetadataPtr(std::ios_base &)
Return a shared pointer to an object that stores metadata (file format, compression scheme,...
void setGridBackgroundValuePtr(std::ios_base &, const void *background)
Specify (a pointer to) the background value of the grid currently being read from or written to the g...
uint32_t getGridClass(std::ios_base &)
Return the class (GRID_LEVEL_SET, GRID_UNKNOWN, etc.) of the grid currently being read from or writte...
void setHalfFloat(std::ios_base &, bool)
Specify whether floating-point values should be quantized to 16 bits when writing to the given stream...
const void * getGridBackgroundValuePtr(std::ios_base &)
Return a pointer to the background value of the grid currently being read from or written to the give...
SharedPtr< StreamMetadata > clearStreamMetadataPtr(std::ios_base &)
Dissociate the given stream from its metadata object (if it has one) and return a shared pointer to t...
std::shared_ptr< T > SharedPtr
Definition Types.h:95
Definition Exceptions.h:13
Definition Coord.h:590
Definition version.h.in:352
Tag for multi-pass I/O. Only points::PointDataLeafNode may inherit from this.
Definition io.h:126
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition version.h.in:121
#define OPENVDB_USE_VERSION_NAMESPACE
Definition version.h.in:286