HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Archive.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2012-2018 DreamWorks Animation LLC
4 //
5 // All rights reserved. This software is distributed under the
6 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
7 //
8 // Redistributions of source code must retain the above copyright
9 // and license notice and the following restrictions and disclaimer.
10 //
11 // * Neither the name of DreamWorks Animation nor the names of
12 // its contributors may be used to endorse or promote products derived
13 // from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
27 // LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
28 //
29 ///////////////////////////////////////////////////////////////////////////
30 
31 #ifndef OPENVDB_IO_ARCHIVE_HAS_BEEN_INCLUDED
32 #define OPENVDB_IO_ARCHIVE_HAS_BEEN_INCLUDED
33 
34 #include <openvdb/version.h>
35 #include "Compression.h" // for COMPRESS_ZIP, etc.
36 #include <openvdb/Grid.h>
37 #include <openvdb/MetaMap.h>
38 #include <openvdb/Platform.h>
39 #include <openvdb/version.h> // for VersionId
40 #include <hboost/uuid/uuid.hpp>
41 #include <cstdint>
42 #include <iosfwd>
43 #include <map>
44 #include <memory>
45 #include <string>
46 
47 
48 class TestFile;
49 
50 namespace openvdb {
52 namespace OPENVDB_VERSION_NAME {
53 namespace io {
54 
55 class GridDescriptor;
56 
57 
58 /// Grid serializer/unserializer
60 {
61 public:
64 
65  static const uint32_t DEFAULT_COMPRESSION_FLAGS;
66 
67  Archive();
68  Archive(const Archive&) = default;
69  Archive& operator=(const Archive&) = default;
70  virtual ~Archive();
71 
72  /// @brief Return a copy of this archive.
73  virtual Ptr copy() const;
74 
75  /// @brief Return the UUID that was most recently written (or read,
76  /// if no UUID has been written yet).
77  std::string getUniqueTag() const;
78  /// @brief Return @c true if the given UUID matches this archive's UUID.
79  bool isIdentical(const std::string& uuidStr) const;
80 
81  /// @brief Return the file format version number of the input stream.
82  uint32_t fileVersion() const { return mFileVersion; }
83  /// @brief Return the (major, minor) version number of the library that was
84  /// used to write the input stream.
85  VersionId libraryVersion() const { return mLibraryVersion; }
86  /// @brief Return a string of the form "<major>.<minor>/<format>", giving the
87  /// library and file format version numbers associated with the input stream.
88  std::string version() const;
89 
90  /// @brief Return @c true if trees shared by multiple grids are written out
91  /// only once, @c false if they are written out once per grid.
92  bool isInstancingEnabled() const { return mEnableInstancing; }
93  /// @brief Specify whether trees shared by multiple grids should be
94  /// written out only once (@c true) or once per grid (@c false).
95  /// @note Instancing is enabled by default.
96  void setInstancingEnabled(bool b) { mEnableInstancing = b; }
97 
98  /// Return @c true if the OpenVDB library includes support for the Blosc compressor.
99  static bool hasBloscCompression();
100 
101  /// Return a bit mask specifying compression options for the data stream.
102  uint32_t compression() const { return mCompression; }
103  /// @brief Specify whether and how the data stream should be compressed.
104  /// @param c bitwise OR (e.g., COMPRESS_ZIP | COMPRESS_ACTIVE_MASK) of
105  /// compression option flags (see Compression.h for the available flags)
106  /// @note Not all combinations of compression options are supported.
107  void setCompression(uint32_t c) { mCompression = c; }
108 
109  /// @brief Return @c true if grid statistics (active voxel count and
110  /// bounding box, etc.) are computed and written as grid metadata.
111  bool isGridStatsMetadataEnabled() const { return mEnableGridStats; }
112  /// @brief Specify whether grid statistics (active voxel count and
113  /// bounding box, etc.) should be computed and written as grid metadata.
114  void setGridStatsMetadataEnabled(bool b) { mEnableGridStats = b; }
115 
116  /// @brief Write the grids in the given container to this archive's output stream.
117  virtual void write(const GridCPtrVec&, const MetaMap& = MetaMap()) const {}
118 
119  /// @brief Return @c true if delayed loading is enabled.
120  /// @details If enabled, delayed loading can be disabled for individual files,
121  /// but not vice-versa.
122  /// @note Define the environment variable @c OPENVDB_DISABLE_DELAYED_LOAD
123  /// to disable delayed loading unconditionally.
124  static bool isDelayedLoadingEnabled();
125 
126 protected:
127  /// @brief Return @c true if the input stream contains grid offsets
128  /// that allow for random access or partial reading.
129  bool inputHasGridOffsets() const { return mInputHasGridOffsets; }
130  void setInputHasGridOffsets(bool b) { mInputHasGridOffsets = b; }
131 
132  /// @brief Tag the given input stream with the input file format version number.
133  ///
134  /// The tag can be retrieved with getFormatVersion().
135  /// @sa getFormatVersion()
136  void setFormatVersion(std::istream&);
137 
138  /// @brief Tag the given input stream with the version number of
139  /// the library with which the input stream was created.
140  ///
141  /// The tag can be retrieved with getLibraryVersion().
142  /// @sa getLibraryVersion()
143  void setLibraryVersion(std::istream&);
144 
145  /// @brief Tag the given input stream with flags indicating whether
146  /// the input stream contains compressed data and how it is compressed.
147  void setDataCompression(std::istream&);
148 
149  /// @brief Tag an output stream with flags specifying only those
150  /// compression options that are applicable to the given grid.
151  void setGridCompression(std::ostream&, const GridBase&) const;
152  /// @brief Read in the compression flags for a grid and
153  /// tag the given input stream with those flags.
154  static void readGridCompression(std::istream&);
155 
156  /// Read in and return the number of grids on the input stream.
157  static int32_t readGridCount(std::istream&);
158 
159  /// Populate the given grid from the input stream.
160  static void readGrid(GridBase::Ptr, const GridDescriptor&, std::istream&);
161 #if OPENVDB_ABI_VERSION_NUMBER >= 3
162  /// @brief Populate the given grid from the input stream, but only where it
163  /// intersects the given world-space bounding box.
164  static void readGrid(GridBase::Ptr, const GridDescriptor&, std::istream&, const BBoxd&);
165  /// @brief Populate the given grid from the input stream, but only where it
166  /// intersects the given index-space bounding box.
167  static void readGrid(GridBase::Ptr, const GridDescriptor&, std::istream&, const CoordBBox&);
168 #endif
169 
170  using NamedGridMap = std::map<Name /*uniqueName*/, GridBase::Ptr>;
171 
172  /// @brief If the grid represented by the given grid descriptor
173  /// is an instance, connect it with its instance parent.
174  void connectInstance(const GridDescriptor&, const NamedGridMap&) const;
175 
176  /// Write the given grid descriptor and grid to an output stream
177  /// and update the GridDescriptor offsets.
178  /// @param seekable if true, the output stream supports seek operations
179  void writeGrid(GridDescriptor&, GridBase::ConstPtr, std::ostream&, bool seekable) const;
180  /// Write the given grid descriptor and grid metadata to an output stream
181  /// and update the GridDescriptor offsets, but don't write the grid's tree,
182  /// since it is shared with another grid.
183  /// @param seekable if true, the output stream supports seek operations
184  void writeGridInstance(GridDescriptor&, GridBase::ConstPtr,
185  std::ostream&, bool seekable) const;
186 
187  /// @brief Read the magic number, version numbers, UUID, etc. from the given input stream.
188  /// @return @c true if the input UUID differs from the previously-read UUID.
189  bool readHeader(std::istream&);
190  /// @brief Write the magic number, version numbers, UUID, etc. to the given output stream.
191  /// @param seekable if true, the output stream supports seek operations
192  /// @todo This method should not be const since it actually redefines the UUID!
193  void writeHeader(std::ostream&, bool seekable) const;
194 
195  //@{
196  /// Write the given grids to an output stream.
197  void write(std::ostream&, const GridPtrVec&, bool seekable, const MetaMap& = MetaMap()) const;
198  void write(std::ostream&, const GridCPtrVec&, bool seekable, const MetaMap& = MetaMap()) const;
199  //@}
200 
201 private:
202  friend class ::TestFile;
203 
204  /// The version of the file that was read
205  uint32_t mFileVersion;
206  /// The version of the library that was used to create the file that was read
207  VersionId mLibraryVersion;
208  /// 16-byte (128-bit) UUID
209  mutable hboost::uuids::uuid mUuid;// needs to be mutable since writeHeader is const!
210  /// Flag indicating whether the input stream contains grid offsets
211  /// and therefore supports partial reading
212  bool mInputHasGridOffsets;
213  /// Flag indicating whether a tree shared by multiple grids should be
214  /// written out only once (true) or once per grid (false)
215  bool mEnableInstancing;
216  /// Flags indicating whether and how the data stream is compressed
217  uint32_t mCompression;
218  /// Flag indicating whether grid statistics metadata should be written
219  bool mEnableGridStats;
220 }; // class Archive
221 
222 } // namespace io
223 } // namespace OPENVDB_VERSION_NAME
224 } // namespace openvdb
225 
226 #endif // OPENVDB_IO_ARCHIVE_HAS_BEEN_INCLUDED
227 
228 // Copyright (c) 2012-2018 DreamWorks Animation LLC
229 // All rights reserved. This software is distributed under the
230 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
Grid serializer/unserializer.
Definition: Archive.h:59
uint32_t fileVersion() const
Return the file format version number of the input stream.
Definition: Archive.h:82
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:53
SharedPtr< GridBase > Ptr
Definition: Grid.h:107
GLsizei const GLchar *const * string
Definition: glcorearb.h:813
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:189
bool inputHasGridOffsets() const
Return true if the input stream contains grid offsets that allow for random access or partial reading...
Definition: Archive.h:129
uint32_t compression() const
Return a bit mask specifying compression options for the data stream.
Definition: Archive.h:102
Container that maps names (strings) to values of arbitrary types.
Definition: MetaMap.h:46
std::vector< GridBase::Ptr > GridPtrVec
Definition: Grid.h:437
void setGridStatsMetadataEnabled(bool b)
Specify whether grid statistics (active voxel count and bounding box, etc.) should be computed and wr...
Definition: Archive.h:114
std::shared_ptr< T > SharedPtr
Definition: Types.h:139
Abstract base class for typed grids.
Definition: Grid.h:104
void setCompression(uint32_t c)
Specify whether and how the data stream should be compressed.
Definition: Archive.h:107
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:194
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:96
bool isGridStatsMetadataEnabled() const
Return true if grid statistics (active voxel count and bounding box, etc.) are computed and written a...
Definition: Archive.h:111
static const uint32_t DEFAULT_COMPRESSION_FLAGS
Definition: Archive.h:65
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1221
GT_API const UT_StringHolder version
SharedPtr< const Archive > ConstPtr
Definition: Archive.h:63
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:117
SharedPtr< const GridBase > ConstPtr
Definition: Grid.h:108
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:92
VersionId libraryVersion() const
Return the (major, minor) version number of the library that was used to write the input stream...
Definition: Archive.h:85
void write(T &out, bool v)
Definition: ImfXdr.h:332
std::vector< GridBase::ConstPtr > GridCPtrVec
Definition: Grid.h:442
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:135
std::map< Name, GridBase::Ptr > NamedGridMap
Definition: Archive.h:170