HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Archive.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-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 "Compression.h" // for COMPRESS_ZIP, etc.
9 #include <openvdb/Grid.h>
10 #include <openvdb/MetaMap.h>
11 #include <openvdb/Platform.h>
12 #include <openvdb/version.h> // for VersionId
13 #include <hboost/uuid/uuid.hpp>
14 #include <cstdint>
15 #include <iosfwd>
16 #include <map>
17 #include <memory>
18 #include <string>
19 
20 
21 class TestFile;
22 
23 namespace openvdb {
25 namespace OPENVDB_VERSION_NAME {
26 namespace io {
27 
28 class GridDescriptor;
29 
30 
31 /// Grid serializer/unserializer
33 {
34 public:
37 
38  static const uint32_t DEFAULT_COMPRESSION_FLAGS;
39 
40  Archive();
41  Archive(const Archive&) = default;
42  Archive& operator=(const Archive&) = default;
43  virtual ~Archive();
44 
45  /// @brief Return a copy of this archive.
46  virtual Ptr copy() const;
47 
48  /// @brief Return the UUID that was most recently written (or read,
49  /// if no UUID has been written yet).
50  std::string getUniqueTag() const;
51  /// @brief Return @c true if the given UUID matches this archive's UUID.
52  bool isIdentical(const std::string& uuidStr) const;
53 
54  /// @brief Return the file format version number of the input stream.
55  uint32_t fileVersion() const { return mFileVersion; }
56  /// @brief Return the (major, minor) version number of the library that was
57  /// used to write the input stream.
58  VersionId libraryVersion() const { return mLibraryVersion; }
59  /// @brief Return a string of the form "<major>.<minor>/<format>", giving the
60  /// library and file format version numbers associated with the input stream.
61  std::string version() const;
62 
63  /// @brief Return @c true if trees shared by multiple grids are written out
64  /// only once, @c false if they are written out once per grid.
65  bool isInstancingEnabled() const { return mEnableInstancing; }
66  /// @brief Specify whether trees shared by multiple grids should be
67  /// written out only once (@c true) or once per grid (@c false).
68  /// @note Instancing is enabled by default.
69  void setInstancingEnabled(bool b) { mEnableInstancing = b; }
70 
71  /// Return @c true if the OpenVDB library includes support for the Blosc compressor.
72  static bool hasBloscCompression();
73 
74  /// Return a bit mask specifying compression options for the data stream.
75  uint32_t compression() const { return mCompression; }
76  /// @brief Specify whether and how the data stream should be compressed.
77  /// @param c bitwise OR (e.g., COMPRESS_ZIP | COMPRESS_ACTIVE_MASK) of
78  /// compression option flags (see Compression.h for the available flags)
79  /// @note Not all combinations of compression options are supported.
80  void setCompression(uint32_t c) { mCompression = c; }
81 
82  /// @brief Return @c true if grid statistics (active voxel count and
83  /// bounding box, etc.) are computed and written as grid metadata.
84  bool isGridStatsMetadataEnabled() const { return mEnableGridStats; }
85  /// @brief Specify whether grid statistics (active voxel count and
86  /// bounding box, etc.) should be computed and written as grid metadata.
87  void setGridStatsMetadataEnabled(bool b) { mEnableGridStats = b; }
88 
89  /// @brief Write the grids in the given container to this archive's output stream.
90  virtual void write(const GridCPtrVec&, const MetaMap& = MetaMap()) const {}
91 
92  /// @brief Return @c true if delayed loading is enabled.
93  /// @details If enabled, delayed loading can be disabled for individual files,
94  /// but not vice-versa.
95  /// @note Define the environment variable @c OPENVDB_DISABLE_DELAYED_LOAD
96  /// to disable delayed loading unconditionally.
97  static bool isDelayedLoadingEnabled();
98 
99 protected:
100  /// @brief Return @c true if the input stream contains grid offsets
101  /// that allow for random access or partial reading.
102  bool inputHasGridOffsets() const { return mInputHasGridOffsets; }
103  void setInputHasGridOffsets(bool b) { mInputHasGridOffsets = b; }
104 
105  /// @brief Tag the given input stream with the input file format version number.
106  ///
107  /// The tag can be retrieved with getFormatVersion().
108  /// @sa getFormatVersion()
109  void setFormatVersion(std::istream&);
110 
111  /// @brief Tag the given input stream with the version number of
112  /// the library with which the input stream was created.
113  ///
114  /// The tag can be retrieved with getLibraryVersion().
115  /// @sa getLibraryVersion()
116  void setLibraryVersion(std::istream&);
117 
118  /// @brief Tag the given input stream with flags indicating whether
119  /// the input stream contains compressed data and how it is compressed.
120  void setDataCompression(std::istream&);
121 
122  /// @brief Tag an output stream with flags specifying only those
123  /// compression options that are applicable to the given grid.
124  void setGridCompression(std::ostream&, const GridBase&) const;
125  /// @brief Read in the compression flags for a grid and
126  /// tag the given input stream with those flags.
127  static void readGridCompression(std::istream&);
128 
129  /// Read in and return the number of grids on the input stream.
130  static int32_t readGridCount(std::istream&);
131 
132  /// Populate the given grid from the input stream.
133  static void readGrid(GridBase::Ptr, const GridDescriptor&, std::istream&);
134  /// @brief Populate the given grid from the input stream, but only where it
135  /// intersects the given world-space bounding box.
136  static void readGrid(GridBase::Ptr, const GridDescriptor&, std::istream&, const BBoxd&);
137  /// @brief Populate the given grid from the input stream, but only where it
138  /// intersects the given index-space bounding box.
139  static void readGrid(GridBase::Ptr, const GridDescriptor&, std::istream&, const CoordBBox&);
140 
141  using NamedGridMap = std::map<Name /*uniqueName*/, GridBase::Ptr>;
142 
143  /// @brief If the grid represented by the given grid descriptor
144  /// is an instance, connect it with its instance parent.
145  void connectInstance(const GridDescriptor&, const NamedGridMap&) const;
146 
147  /// Write the given grid descriptor and grid to an output stream
148  /// and update the GridDescriptor offsets.
149  /// @param seekable if true, the output stream supports seek operations
150  void writeGrid(GridDescriptor&, GridBase::ConstPtr, std::ostream&, bool seekable) const;
151  /// Write the given grid descriptor and grid metadata to an output stream
152  /// and update the GridDescriptor offsets, but don't write the grid's tree,
153  /// since it is shared with another grid.
154  /// @param seekable if true, the output stream supports seek operations
155  void writeGridInstance(GridDescriptor&, GridBase::ConstPtr,
156  std::ostream&, bool seekable) const;
157 
158  /// @brief Read the magic number, version numbers, UUID, etc. from the given input stream.
159  /// @return @c true if the input UUID differs from the previously-read UUID.
160  bool readHeader(std::istream&);
161  /// @brief Write the magic number, version numbers, UUID, etc. to the given output stream.
162  /// @param seekable if true, the output stream supports seek operations
163  /// @todo This method should not be const since it actually redefines the UUID!
164  void writeHeader(std::ostream&, bool seekable) const;
165 
166  //@{
167  /// Write the given grids to an output stream.
168  void write(std::ostream&, const GridPtrVec&, bool seekable, const MetaMap& = MetaMap()) const;
169  void write(std::ostream&, const GridCPtrVec&, bool seekable, const MetaMap& = MetaMap()) const;
170  //@}
171 
172 private:
173  friend class ::TestFile;
174 
175  /// The version of the file that was read
176  uint32_t mFileVersion;
177  /// The version of the library that was used to create the file that was read
178  VersionId mLibraryVersion;
179  /// 16-byte (128-bit) UUID
180  mutable hboost::uuids::uuid mUuid;// needs to be mutable since writeHeader is const!
181  /// Flag indicating whether the input stream contains grid offsets
182  /// and therefore supports partial reading
183  bool mInputHasGridOffsets;
184  /// Flag indicating whether a tree shared by multiple grids should be
185  /// written out only once (true) or once per grid (false)
186  bool mEnableInstancing;
187  /// Flags indicating whether and how the data stream is compressed
188  uint32_t mCompression;
189  /// Flag indicating whether grid statistics metadata should be written
190  bool mEnableGridStats;
191 }; // class Archive
192 
193 } // namespace io
194 } // namespace OPENVDB_VERSION_NAME
195 } // namespace openvdb
196 
197 #endif // OPENVDB_IO_ARCHIVE_HAS_BEEN_INCLUDED
Grid serializer/unserializer.
Definition: Archive.h:32
uint32_t fileVersion() const
Return the file format version number of the input stream.
Definition: Archive.h:55
OPENVDB_API void setDataCompression(std::ios_base &, uint32_t compressionFlags)
Associate with the given stream a bitwise OR of compression option flags (COMPRESS_ZIP, COMPRESS_ACTIVE_MASK, etc.) specifying whether and how input data is compressed or output data should be compressed.
Definition: ImfName.h:54
SharedPtr< GridBase > Ptr
Definition: Grid.h:80
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:166
bool inputHasGridOffsets() const
Return true if the input stream contains grid offsets that allow for random access or partial reading...
Definition: Archive.h:102
uint32_t compression() const
Return a bit mask specifying compression options for the data stream.
Definition: Archive.h:75
Container that maps names (strings) to values of arbitrary types.
Definition: MetaMap.h:19
std::vector< GridBase::Ptr > GridPtrVec
Definition: Grid.h:509
void setGridStatsMetadataEnabled(bool b)
Specify whether grid statistics (active voxel count and bounding box, etc.) should be computed and wr...
Definition: Archive.h:87
std::shared_ptr< T > SharedPtr
Definition: Types.h:91
Abstract base class for typed grids.
Definition: Grid.h:77
void setCompression(uint32_t c)
Specify whether and how the data stream should be compressed.
Definition: Archive.h:80
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:230
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:69
bool isGridStatsMetadataEnabled() const
Return true if grid statistics (active voxel count and bounding box, etc.) are computed and written a...
Definition: Archive.h:84
const GLfloat * c
Definition: glew.h:16296
static const uint32_t DEFAULT_COMPRESSION_FLAGS
Definition: Archive.h:38
GT_API const UT_StringHolder version
SharedPtr< const Archive > ConstPtr
Definition: Archive.h:36
virtual void write(const GridCPtrVec &, const MetaMap &=MetaMap()) const
Write the grids in the given container to this archive's output stream.
Definition: Archive.h:90
GLdouble GLdouble GLdouble b
Definition: glew.h:9122
GLsizei const GLchar *const * string
Definition: glew.h:1844
SharedPtr< const GridBase > ConstPtr
Definition: Grid.h:81
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:65
OIIO_API bool copy(string_view from, string_view to, std::string &err)
VersionId libraryVersion() const
Return the (major, minor) version number of the library that was used to write the input stream...
Definition: Archive.h:58
void write(T &out, bool v)
Definition: ImfXdr.h:332
std::vector< GridBase::ConstPtr > GridCPtrVec
Definition: Grid.h:514
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:112
std::map< Name, GridBase::Ptr > NamedGridMap
Definition: Archive.h:141