HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
io.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 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_IO_HAS_BEEN_INCLUDED
32 #define OPENVDB_IO_IO_HAS_BEEN_INCLUDED
33 
34 #include <openvdb/Platform.h>
35 #include <openvdb/Types.h> // for SharedPtr
36 #include <openvdb/version.h>
37 #include <hboost/any.hpp>
38 #include <functional>
39 #include <iosfwd> // for std::ios_base
40 #include <map>
41 #include <memory>
42 #include <string>
43 
44 class TestMappedFile;
45 
46 namespace openvdb {
48 namespace OPENVDB_VERSION_NAME {
49 
50 class MetaMap;
51 
52 namespace io {
53 
54 /// @brief Container for metadata describing how to unserialize grids from and/or
55 /// serialize grids to a stream (which file format, compression scheme, etc. to use)
56 /// @details This class is mainly for internal use.
58 {
59 public:
62 
65  explicit StreamMetadata(std::ios_base&);
66  ~StreamMetadata();
67 
68  StreamMetadata& operator=(const StreamMetadata&);
69 
70  /// @brief Transfer metadata items directly to the given stream.
71  /// @todo Deprecate direct transfer; use StreamMetadata structs everywhere.
72  void transferTo(std::ios_base&) const;
73 
74  uint32_t fileVersion() const;
75  void setFileVersion(uint32_t);
76 
77  VersionId libraryVersion() const;
78  void setLibraryVersion(VersionId);
79 
80  uint32_t compression() const;
81  void setCompression(uint32_t);
82 
83  uint32_t gridClass() const;
84  void setGridClass(uint32_t);
85 
86  const void* backgroundPtr() const;
87  void setBackgroundPtr(const void*);
88 
89  bool halfFloat() const;
90  void setHalfFloat(bool);
91 
92  bool writeGridStats() const;
93  void setWriteGridStats(bool);
94 
95  bool seekable() const;
96  void setSeekable(bool);
97 
98  bool delayedLoadMeta() const;
99 
100  bool countingPasses() const;
101  void setCountingPasses(bool);
102 
103  uint32_t pass() const;
104  void setPass(uint32_t);
105 
106  uint64_t leaf() const;
107  void setLeaf(uint64_t);
108 
109  //@{
110  /// @brief Return a (reference to a) copy of the metadata of the grid
111  /// currently being read or written.
112  /// @details Some grid metadata might duplicate information returned by
113  /// gridClass(), backgroundPtr() and other accessors, but those values
114  /// are not guaranteed to be kept in sync.
115  MetaMap& gridMetadata();
116  const MetaMap& gridMetadata() const;
117  //@}
118 
119  using AuxDataMap = std::map<std::string, hboost::any>;
120  //@{
121  /// @brief Return a map that can be populated with arbitrary user data.
122  AuxDataMap& auxData();
123  const AuxDataMap& auxData() const;
124  //@}
125 
126  /// @private
127  uint32_t __test() const;
128  /// @private
129  void __setTest(uint32_t);
130 
131  /// Return a string describing this stream metadata.
132  std::string str() const;
133 
134 private:
135  struct Impl;
136  std::unique_ptr<Impl> mImpl;
137 }; // class StreamMetadata
138 
139 
140 /// Write a description of the given metadata to an output stream.
141 std::ostream& operator<<(std::ostream&, const StreamMetadata&);
142 
143 std::ostream& operator<<(std::ostream&, const StreamMetadata::AuxDataMap&);
144 
145 
146 ////////////////////////////////////////
147 
148 
149 /// @brief Leaf nodes that require multi-pass I/O must inherit from this struct.
150 /// @sa Grid::hasMultiPassIO()
151 struct MultiPass {};
152 
153 
154 ////////////////////////////////////////
155 
156 
157 class File;
158 
159 /// @brief Handle to control the lifetime of a memory-mapped .vdb file
161 {
162 public:
164 
165  ~MappedFile();
166  MappedFile(const MappedFile&) = delete; // not copyable
167  MappedFile& operator=(const MappedFile&) = delete;
168 
169  /// Return the filename of the mapped file.
170  std::string filename() const;
171 
172  /// @brief Return a new stream buffer for the mapped file.
173  /// @details Typical usage is
174  /// @code
175  /// openvdb::io::MappedFile::Ptr mappedFile = ...;
176  /// auto buf = mappedFile->createBuffer();
177  /// std::istream istrm{buf.get()};
178  /// // Read from istrm...
179  /// @endcode
180  /// The buffer must persist as long as the stream is open.
181  SharedPtr<std::streambuf> createBuffer() const;
182 
183  using Notifier = std::function<void(std::string /*filename*/)>;
184  /// @brief Register a function that will be called with this file's name
185  /// when the file is unmapped.
186  void setNotifier(const Notifier&);
187  /// Deregister the notifier.
188  void clearNotifier();
189 
190 private:
191  friend class File;
192  friend class ::TestMappedFile;
193 
194  explicit MappedFile(const std::string& filename, bool autoDelete = false);
195 
196  class Impl;
197  std::unique_ptr<Impl> mImpl;
198 }; // class MappedFile
199 
200 
201 ////////////////////////////////////////
202 
203 
204 /// Return a string (possibly empty) describing the given system error code.
205 std::string getErrorString(int errorNum);
206 
207 
208 /// Return a string (possibly empty) describing the most recent system error.
210 
211 
212 ////////////////////////////////////////
213 
214 
215 /// @brief Return the file format version number associated with the given input stream.
216 /// @sa File::setFormatVersion()
217 OPENVDB_API uint32_t getFormatVersion(std::ios_base&);
218 
219 /// @brief Return the (major, minor) library version number associated with the given input stream.
220 /// @sa File::setLibraryVersion()
221 OPENVDB_API VersionId getLibraryVersion(std::ios_base&);
222 
223 /// @brief Return a string of the form "<major>.<minor>/<format>", giving the library
224 /// and file format version numbers associated with the given input stream.
225 OPENVDB_API std::string getVersion(std::ios_base&);
226 
227 /// Associate the current file format and library version numbers with the given input stream.
228 OPENVDB_API void setCurrentVersion(std::istream&);
229 
230 /// @brief Associate specific file format and library version numbers with the given stream.
231 /// @details This is typically called immediately after reading a header that contains
232 /// the version numbers. Data read subsequently can then be interpreted appropriately.
233 OPENVDB_API void setVersion(std::ios_base&, const VersionId& libraryVersion, uint32_t fileVersion);
234 
235 /// @brief Return a bitwise OR of compression option flags (COMPRESS_ZIP,
236 /// COMPRESS_ACTIVE_MASK, etc.) specifying whether and how input data is compressed
237 /// or output data should be compressed.
238 OPENVDB_API uint32_t getDataCompression(std::ios_base&);
239 /// @brief Associate with the given stream a bitwise OR of compression option flags
240 /// (COMPRESS_ZIP, COMPRESS_ACTIVE_MASK, etc.) specifying whether and how input data
241 /// is compressed or output data should be compressed.
242 OPENVDB_API void setDataCompression(std::ios_base&, uint32_t compressionFlags);
243 
244 /// @brief Return the class (GRID_LEVEL_SET, GRID_UNKNOWN, etc.) of the grid
245 /// currently being read from or written to the given stream.
246 OPENVDB_API uint32_t getGridClass(std::ios_base&);
247 /// @brief Associate with the given stream the class (GRID_LEVEL_SET, GRID_UNKNOWN, etc.)
248 /// of the grid currently being read or written.
249 OPENVDB_API void setGridClass(std::ios_base&, uint32_t);
250 
251 /// @brief Return true if floating-point values should be quantized to 16 bits when writing
252 /// to the given stream or promoted back from 16-bit to full precision when reading from it.
253 OPENVDB_API bool getHalfFloat(std::ios_base&);
254 /// @brief Specify whether floating-point values should be quantized to 16 bits when writing
255 /// to the given stream or promoted back from 16-bit to full precision when reading from it.
256 OPENVDB_API void setHalfFloat(std::ios_base&, bool);
257 
258 /// @brief Return a pointer to the background value of the grid
259 /// currently being read from or written to the given stream.
260 OPENVDB_API const void* getGridBackgroundValuePtr(std::ios_base&);
261 /// @brief Specify (a pointer to) the background value of the grid
262 /// currently being read from or written to the given stream.
263 /// @note The pointer must remain valid until the entire grid has been read or written.
264 OPENVDB_API void setGridBackgroundValuePtr(std::ios_base&, const void* background);
265 
266 /// @brief Return @c true if grid statistics (active voxel count and bounding box, etc.)
267 /// should be computed and stored as grid metadata when writing to the given stream.
268 OPENVDB_API bool getWriteGridStatsMetadata(std::ios_base&);
269 /// @brief Specify whether to compute grid statistics (active voxel count and bounding box, etc.)
270 /// and store them as grid metadata when writing to the given stream.
271 OPENVDB_API void setWriteGridStatsMetadata(std::ios_base&, bool writeGridStats);
272 
273 /// @brief Return a shared pointer to the memory-mapped file with which the given stream
274 /// is associated, or a null pointer if the stream is not associated with a memory-mapped file.
276 /// @brief Associate the given stream with (a shared pointer to) a memory-mapped file.
277 /// @note The shared pointer object (not just the io::MappedFile object to which it points)
278 /// must remain valid until the file is closed.
279 OPENVDB_API void setMappedFilePtr(std::ios_base&, SharedPtr<MappedFile>&);
280 
281 /// @brief Return a shared pointer to an object that stores metadata (file format,
282 /// compression scheme, etc.) for use when reading from or writing to the given stream.
284 /// @brief Associate the given stream with (a shared pointer to) an object that stores
285 /// metadata (file format, compression scheme, etc.) for use when reading from
286 /// or writing to the stream.
287 /// @details If @a transfer is true, copy metadata from the object directly to the stream
288 /// (for backward compatibility with older versions of the library).
289 /// @note The shared pointer object (not just the io::StreamMetadata object to which it points)
290 /// must remain valid until the file is closed.
291 OPENVDB_API void setStreamMetadataPtr(std::ios_base&,
292  SharedPtr<StreamMetadata>&, bool transfer = true);
293 /// @brief Dissociate the given stream from its metadata object (if it has one)
294 /// and return a shared pointer to the object.
296 
297 } // namespace io
298 } // namespace OPENVDB_VERSION_NAME
299 } // namespace openvdb
300 
301 #endif // OPENVDB_IO_IO_HAS_BEEN_INCLUDED
302 
303 // Copyright (c) DreamWorks Animation LLC
304 // All rights reserved. This software is distributed under the
305 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
OPENVDB_API SharedPtr< MappedFile > getMappedFilePtr(std::ios_base &)
Return a shared pointer to the memory-mapped file with which the given stream is associated, or a null pointer if the stream is not associated with a memory-mapped file.
std::ostream & operator<<(std::ostream &, const StreamMetadata &)
Write a description of the given metadata to an output stream.
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.
OPENVDB_API void setGridClass(std::ios_base &, uint32_t)
Associate with the given stream the class (GRID_LEVEL_SET, GRID_UNKNOWN, etc.) of the grid currently ...
GT_API const UT_StringHolder filename
OPENVDB_API 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...
OPENVDB_API void setHalfFloat(std::ios_base &, bool)
Specify whether floating-point values should be quantized to 16 bits when writing to the given stream...
std::string getErrorString(int errorNum)
Return a string (possibly empty) describing the given system error code.
OPENVDB_API std::string getVersion(std::ios_base &)
Return a string of the form "<major>.<minor>/<format>", giving the library and file format version nu...
SharedPtr< StreamMetadata > Ptr
Definition: io.h:60
OPENVDB_API uint32_t getDataCompression(std::ios_base &)
Return 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.
Leaf nodes that require multi-pass I/O must inherit from this struct.
Definition: io.h:151
Grid archive associated with a file on disk.
Definition: File.h:57
OPENVDB_API bool getHalfFloat(std::ios_base &)
Return true if floating-point values should be quantized to 16 bits when writing to the given stream ...
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:200
SharedPtr< MappedFile > Ptr
Definition: io.h:163
OPENVDB_API VersionId getLibraryVersion(std::ios_base &)
Return the (major, minor) library version number associated with the given input stream.
Container that maps names (strings) to values of arbitrary types.
Definition: MetaMap.h:46
std::function< void(std::string)> Notifier
Definition: io.h:183
OPENVDB_API 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:139
OPENVDB_API void setMappedFilePtr(std::ios_base &, SharedPtr< MappedFile > &)
Associate the given stream with (a shared pointer to) a memory-mapped file.
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:288
Handle to control the lifetime of a memory-mapped .vdb file.
Definition: io.h:160
void
Definition: png.h:1083
OPENVDB_API void setVersion(std::ios_base &, const VersionId &libraryVersion, uint32_t fileVersion)
Associate specific file format and library version numbers with the given stream. ...
SharedPtr< const StreamMetadata > ConstPtr
Definition: io.h:61
OPENVDB_API bool getWriteGridStatsMetadata(std::ios_base &)
Return true if grid statistics (active voxel count and bounding box, etc.) should be computed and sto...
OPENVDB_API 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...
GLsizei const GLchar *const * string
Definition: glew.h:1844
OPENVDB_API 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...
OPENVDB_API uint32_t getGridClass(std::ios_base &)
Return the class (GRID_LEVEL_SET, GRID_UNKNOWN, etc.) of the grid currently being read from or writte...
std::map< std::string, hboost::any > AuxDataMap
Definition: io.h:119
OPENVDB_API SharedPtr< StreamMetadata > getStreamMetadataPtr(std::ios_base &)
Return a shared pointer to an object that stores metadata (file format, compression scheme...
Container for metadata describing how to unserialize grids from and/or serialize grids to a stream (w...
Definition: io.h:57
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:146
OPENVDB_API uint32_t getFormatVersion(std::ios_base &)
Return the file format version number associated with the given input stream.
OPENVDB_API void setWriteGridStatsMetadata(std::ios_base &, bool writeGridStats)
Specify whether to compute grid statistics (active voxel count and bounding box, etc.) and store them as grid metadata when writing to the given stream.
OPENVDB_API void setCurrentVersion(std::istream &)
Associate the current file format and library version numbers with the given input stream...