OpenVDB 14.0.0
Loading...
Searching...
No Matches
Archive.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_ARCHIVE_HAS_BEEN_INCLUDED
5#define OPENVDB_IO_ARCHIVE_HAS_BEEN_INCLUDED
6
7#include <openvdb/version.h>
8#include <openvdb/Grid.h>
9#include <openvdb/MetaMap.h>
10#include <openvdb/Platform.h>
11#include <openvdb/version.h> // for VersionId
12
13#include "Codec.h"
14#include "Compression.h" // for COMPRESS_ZIP, etc.
15#include "io.h" // for StreamMetadata
16
17#include <cstdint>
18#include <iosfwd>
19#include <map>
20#include <memory>
21#include <string>
22
23
24class TestFile;
25
26namespace openvdb {
28namespace OPENVDB_VERSION_NAME {
29namespace io {
30
31class GridDescriptor;
32
33
34/// Grid serializer/unserializer
36{
37public:
40
41 static const uint32_t DEFAULT_COMPRESSION_FLAGS;
42
44 Archive(const Archive&) = default;
45 Archive& operator=(const Archive&) = default;
46 virtual ~Archive();
47
48 /// @brief Return a copy of this archive.
49 virtual Ptr copy() const;
50
51 /// @brief Return the UUID that was most recently written (or read,
52 /// if no UUID has been written yet).
53 std::string getUniqueTag() const;
54 /// @brief Return @c true if the given UUID matches this archive's UUID.
55 bool isIdentical(const std::string& uuidStr) const;
56
57 /// @brief Return the file format version number of the input stream.
58 uint32_t fileVersion() const { return mFileVersion; }
59 /// @brief Return the (major, minor) version number of the library that was
60 /// used to write the input stream.
61 VersionId libraryVersion() const { return mLibraryVersion; }
62 /// @brief Return a string of the form "<major>.<minor>/<format>", giving the
63 /// library and file format version numbers associated with the input stream.
64 std::string version() const;
65
66 /// @brief Return @c true if trees shared by multiple grids are written out
67 /// only once, @c false if they are written out once per grid.
68 bool isInstancingEnabled() const { return mEnableInstancing; }
69 /// @brief Specify whether trees shared by multiple grids should be
70 /// written out only once (@c true) or once per grid (@c false).
71 /// @note Instancing is enabled by default.
72 void setInstancingEnabled(bool b) { mEnableInstancing = b; }
73
74 /// Return @c true if the OpenVDB library includes support for the Blosc compressor.
75 /// @note Blosc is always built in, so this always returns @c true.
76 static bool hasBloscCompression();
77
78 /// Return @c true if the OpenVDB library includes support for the ZLib compressor.
79 static bool hasZLibCompression();
80
81 /// Return a bit mask specifying compression options for the data stream.
82 uint32_t compression() const { return mCompression; }
83 /// @brief Specify whether and how the data stream should be compressed.
84 /// @param c bitwise OR (e.g., COMPRESS_ZIP | COMPRESS_ACTIVE_MASK) of
85 /// compression option flags (see Compression.h for the available flags)
86 /// @note Not all combinations of compression options are supported.
87 void setCompression(uint32_t c) { mCompression = c; }
88
89 /// @brief Return @c true if grid statistics (active voxel count and
90 /// bounding box, etc.) are computed and written as grid metadata.
91 bool isGridStatsMetadataEnabled() const { return mEnableGridStats; }
92 /// @brief Specify whether grid statistics (active voxel count and
93 /// bounding box, etc.) should be computed and written as grid metadata.
94 void setGridStatsMetadataEnabled(bool b) { mEnableGridStats = b; }
95
96 /// @brief Write the grids in the given container to this archive's output stream.
97 virtual void write(const GridCPtrVec&, const MetaMap& = MetaMap(),
98 const io::WriteOptions& = io::WriteOptions{}) const {}
99
100 /// @brief Return @c false (delayed loading has been removed).
101 static bool isDelayedLoadingEnabled() { return false; }
102
103 /// @brief Enable collection of read diagnostics (warnings, etc.) during I/O operations.
105
106 /// @brief Disable collection of read diagnostics.
108
109 /// @brief Return a const reference to the diagnostics collector.
111
112 /// @brief Clear the diagnostics list while keeping collection active.
114
115protected:
116 /// @brief Return @c true if the input stream contains grid offsets
117 /// that allow for random access or partial reading.
118 bool inputHasGridOffsets() const { return mInputHasGridOffsets; }
119 void setInputHasGridOffsets(bool b) { mInputHasGridOffsets = b; }
120
121 /// @brief Tag the given input stream with the input file format version number.
122 ///
123 /// The tag can be retrieved with getFormatVersion().
124 /// @sa getFormatVersion()
125 void setFormatVersion(std::istream&);
126
127 /// @brief Tag the given input stream with the version number of
128 /// the library with which the input stream was created.
129 ///
130 /// The tag can be retrieved with getLibraryVersion().
131 /// @sa getLibraryVersion()
132 void setLibraryVersion(std::istream&);
133
134 /// @brief Tag the given input stream with flags indicating whether
135 /// the input stream contains compressed data and how it is compressed.
136 void setDataCompression(std::istream&);
137
138 /// @brief Tag an output stream with flags specifying only those
139 /// compression options that are applicable to the given grid.
140 void setGridCompression(std::ostream&, const GridBase&) const;
141 /// @brief Read in the compression flags for a grid and
142 /// tag the given input stream with those flags.
143 static void readGridCompression(std::istream&);
144
145 /// Read in and return the number of grids on the input stream.
146 static int32_t readGridCount(std::istream&);
147
148 /// @brief Find the codec for the given grid type and options.
149 static io::Codec* findCodec(const std::string& gridType, const io::ReadOptions& options = io::ReadOptions{});
150
151 /// @brief Read in and create the grid represented by the given grid descriptor using the
152 /// given input stream, using the provided options if given.
153 static GridBase::Ptr readGrid(const GridDescriptor&, std::istream&,
154 const io::ReadOptions& readOptions, ReadDiagnostics& diagnostics);
155 static GridBase::Ptr readGrid(const GridDescriptor&, std::istream&,
156 const io::ReadOptions& readOptions = io::ReadOptions{});
157
158 using NamedGridMap = std::map<Name /*uniqueName*/, GridBase::Ptr>;
159
160 /// @brief If the grid represented by the given grid descriptor
161 /// is an instance, connect it with its instance parent.
162 void connectInstance(const GridDescriptor&, const NamedGridMap&) const;
163
164 /// Write the given grid descriptor and grid to an output stream
165 /// and update the GridDescriptor offsets.
166 /// @param seekable if true, the output stream supports seek operations
167 /// @param writeOptions options controlling how grid data is written
168 void writeGrid(GridDescriptor&, GridBase::ConstPtr, std::ostream&, bool seekable,
169 const io::WriteOptions& writeOptions = io::WriteOptions{}) const;
170 /// Write the given grid descriptor and grid metadata to an output stream
171 /// and update the GridDescriptor offsets, but don't write the grid's tree,
172 /// since it is shared with another grid.
173 /// @param seekable if true, the output stream supports seek operations
174 /// @param writeOptions options controlling how grid data is written
176 std::ostream&, bool seekable, const io::WriteOptions& writeOptions = io::WriteOptions{}) const;
177
178 /// @brief Read the magic number, version numbers, UUID, etc. from the given input stream.
179 /// @return @c true if the input UUID differs from the previously-read UUID.
180 bool readHeader(std::istream&);
181 /// @brief Write the magic number, version numbers, UUID, etc. to the given output stream.
182 /// @param seekable if true, the output stream supports seek operations
183 /// @todo This method should not be const since it actually redefines the UUID!
184 void writeHeader(std::ostream&, bool seekable) const;
185
186 //@{
187 /// Write the given grids to an output stream.
188 void write(std::ostream&, const GridPtrVec&, bool seekable, const MetaMap&,
189 const io::WriteOptions& writeOptions = io::WriteOptions{}) const;
190 void write(std::ostream&, const GridCPtrVec&, bool seekable, const MetaMap&,
191 const io::WriteOptions& writeOptions = io::WriteOptions{}) const;
192 //@}
193
194 /// Diagnostics collector for read operations (always valid; enabled/disabled via flag)
196
197private:
198 friend class ::TestFile;
199
200 /// The version of the file that was read
201 uint32_t mFileVersion;
202 /// The version of the library that was used to create the file that was read
203 VersionId mLibraryVersion;
204 /// Unique tag, a random 16-byte (128-bit) value, stored as a string format.
205 mutable std::string mUuid;// needs to be mutable since writeHeader is const!
206 /// Flag indicating whether the input stream contains grid offsets
207 /// and therefore supports partial reading
208 bool mInputHasGridOffsets;
209 /// Flag indicating whether a tree shared by multiple grids should be
210 /// written out only once (true) or once per grid (false)
211 bool mEnableInstancing;
212 /// Flags indicating whether and how the data stream is compressed
213 uint32_t mCompression;
214 /// Flag indicating whether grid statistics metadata should be written
215 bool mEnableGridStats;
216}; // class Archive
217
218} // namespace io
219} // namespace OPENVDB_VERSION_NAME
220} // namespace openvdb
221
222#endif // OPENVDB_IO_ARCHIVE_HAS_BEEN_INCLUDED
#define OPENVDB_API
Definition Platform.h:286
Abstract base class for typed grids.
Definition Grid.h:79
SharedPtr< const GridBase > ConstPtr
Definition Grid.h:82
SharedPtr< GridBase > Ptr
Definition Grid.h:81
Container that maps names (strings) to values of arbitrary types.
Definition MetaMap.h:20
void connectInstance(const GridDescriptor &, const NamedGridMap &) const
If the grid represented by the given grid descriptor is an instance, connect it with its instance par...
void setFormatVersion(std::istream &)
Tag the given input stream with the input file format version number.
bool isGridStatsMetadataEnabled() const
Return true if grid statistics (active voxel count and bounding box, etc.) are computed and written a...
Definition Archive.h:91
void setLibraryVersion(std::istream &)
Tag the given input stream with the version number of the library with which the input stream was cre...
Archive & operator=(const Archive &)=default
virtual void write(const GridCPtrVec &, const MetaMap &=MetaMap(), const io::WriteOptions &=io::WriteOptions{}) const
Write the grids in the given container to this archive's output stream.
Definition Archive.h:97
bool inputHasGridOffsets() const
Return true if the input stream contains grid offsets that allow for random access or partial reading...
Definition Archive.h:118
static bool hasBloscCompression()
static const uint32_t DEFAULT_COMPRESSION_FLAGS
Definition Archive.h:41
void setDataCompression(std::istream &)
Tag the given input stream with flags indicating whether the input stream contains compressed data an...
void writeGridInstance(GridDescriptor &, GridBase::ConstPtr, std::ostream &, bool seekable, const io::WriteOptions &writeOptions=io::WriteOptions{}) const
static GridBase::Ptr readGrid(const GridDescriptor &, std::istream &, const io::ReadOptions &readOptions, ReadDiagnostics &diagnostics)
Read in and create the grid represented by the given grid descriptor using the given input stream,...
void write(std::ostream &, const GridPtrVec &, bool seekable, const MetaMap &, const io::WriteOptions &writeOptions=io::WriteOptions{}) const
Write the given grids to an output stream.
uint32_t fileVersion() const
Return the file format version number of the input stream.
Definition Archive.h:58
void writeGrid(GridDescriptor &, GridBase::ConstPtr, std::ostream &, bool seekable, const io::WriteOptions &writeOptions=io::WriteOptions{}) const
const ReadDiagnostics & readDiagnostics() const
Return a const reference to the diagnostics collector.
std::string getUniqueTag() const
Return the UUID that was most recently written (or read, if no UUID has been written yet).
static bool isDelayedLoadingEnabled()
Return false (delayed loading has been removed).
Definition Archive.h:101
void setGridCompression(std::ostream &, const GridBase &) const
Tag an output stream with flags specifying only those compression options that are applicable to the ...
ReadDiagnostics mReadDiagnostics
Diagnostics collector for read operations (always valid; enabled/disabled via flag)
Definition Archive.h:195
void writeHeader(std::ostream &, bool seekable) const
Write the magic number, version numbers, UUID, etc. to the given output stream.
void enableReadDiagnostics()
Enable collection of read diagnostics (warnings, etc.) during I/O operations.
static io::Codec * findCodec(const std::string &gridType, const io::ReadOptions &options=io::ReadOptions{})
Find the codec for the given grid type and options.
void write(std::ostream &, const GridCPtrVec &, bool seekable, const MetaMap &, const io::WriteOptions &writeOptions=io::WriteOptions{}) const
static int32_t readGridCount(std::istream &)
Read in and return the number of grids on the input stream.
SharedPtr< const Archive > ConstPtr
Definition Archive.h:39
void clearReadDiagnostics()
Clear the diagnostics list while keeping collection active.
bool isInstancingEnabled() const
Return true if trees shared by multiple grids are written out only once, false if they are written ou...
Definition Archive.h:68
void setInstancingEnabled(bool b)
Specify whether trees shared by multiple grids should be written out only once (true) or once per gri...
Definition Archive.h:72
static void readGridCompression(std::istream &)
Read in the compression flags for a grid and tag the given input stream with those flags.
virtual Ptr copy() const
Return a copy of this archive.
static bool hasZLibCompression()
Return true if the OpenVDB library includes support for the ZLib compressor.
void setCompression(uint32_t c)
Specify whether and how the data stream should be compressed.
Definition Archive.h:87
uint32_t compression() const
Return a bit mask specifying compression options for the data stream.
Definition Archive.h:82
std::map< Name, GridBase::Ptr > NamedGridMap
Definition Archive.h:158
bool isIdentical(const std::string &uuidStr) const
Return true if the given UUID matches this archive's UUID.
void disableReadDiagnostics()
Disable collection of read diagnostics.
bool readHeader(std::istream &)
Read the magic number, version numbers, UUID, etc. from the given input stream.
SharedPtr< Archive > Ptr
Definition Archive.h:38
static GridBase::Ptr readGrid(const GridDescriptor &, std::istream &, const io::ReadOptions &readOptions=io::ReadOptions{})
Archive(const Archive &)=default
void setGridStatsMetadataEnabled(bool b)
Specify whether grid statistics (active voxel count and bounding box, etc.) should be computed and wr...
Definition Archive.h:94
VersionId libraryVersion() const
Return the (major, minor) version number of the library that was used to write the input stream.
Definition Archive.h:61
std::string version() const
Return a string of the form "<major>.<minor>/<format>", giving the library and file format version nu...
void setInputHasGridOffsets(bool b)
Definition Archive.h:119
Definition GridDescriptor.h:20
Definition Archive.h:29
std::string Name
Definition Name.h:19
std::vector< GridBase::Ptr > GridPtrVec
Definition Grid.h:508
std::shared_ptr< T > SharedPtr
Definition Types.h:95
std::vector< GridBase::ConstPtr > GridCPtrVec
Definition Grid.h:513
Definition Exceptions.h:13
Definition version.h.in:357
Abstract base class for grid I/O codecs.
Definition Codec.h:395
Thread-safe collection of read diagnostics accumulated during a codec read.
Definition Codec.h:211
Global read configuration passed to every codec during deserialization.
Definition Codec.h:167
Global write configuration passed to every codec during serialization.
Definition Codec.h:188
#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:291