HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
File.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 /// @file File.h
32 
33 #ifndef OPENVDB_IO_FILE_HAS_BEEN_INCLUDED
34 #define OPENVDB_IO_FILE_HAS_BEEN_INCLUDED
35 
36 #include <openvdb/version.h>
37 #include "io.h" // for MappedFile::Notifier
38 #include "Archive.h"
39 #include "GridDescriptor.h"
40 #include <algorithm> // for std::copy()
41 #include <iosfwd>
42 #include <iterator> // for std::back_inserter()
43 #include <map>
44 #include <memory>
45 #include <string>
46 
47 
48 class TestFile;
49 class TestStream;
50 
51 namespace openvdb {
53 namespace OPENVDB_VERSION_NAME {
54 namespace io {
55 
56 /// Grid archive associated with a file on disk
57 class OPENVDB_API File: public Archive
58 {
59 public:
60  using NameMap = std::multimap<Name, GridDescriptor>;
61  using NameMapCIter = NameMap::const_iterator;
62 
63  explicit File(const std::string& filename);
64  ~File() override;
65 
66  /// @brief Copy constructor
67  /// @details The copy will be closed and will not reference the same
68  /// file descriptor as the original.
69  File(const File& other);
70  /// @brief Assignment
71  /// @details After assignment, this File will be closed and will not
72  /// reference the same file descriptor as the source File.
73  File& operator=(const File& other);
74 
75  /// @brief Return a copy of this archive.
76  /// @details The copy will be closed and will not reference the same
77  /// file descriptor as the original.
78  SharedPtr<Archive> copy() const override;
79 
80  /// @brief Return the name of the file with which this archive is associated.
81  /// @details The file does not necessarily exist on disk yet.
82  const std::string& filename() const;
83 
84  /// @brief Open the file, read the file header and the file-level metadata,
85  /// and populate the grid descriptors, but do not load any grids into memory.
86  /// @details If @a delayLoad is true, map the file into memory and enable delayed loading
87  /// of grids, and if a notifier is provided, call it when the file gets unmapped.
88  /// @note Define the environment variable @c OPENVDB_DISABLE_DELAYED_LOAD to disable
89  /// delayed loading unconditionally.
90  /// @throw IoError if the file is not a valid VDB file.
91  /// @return @c true if the file's UUID has changed since it was last read.
92  /// @see setCopyMaxBytes
93  bool open(bool delayLoad = true, const MappedFile::Notifier& = MappedFile::Notifier());
94 
95  /// Return @c true if the file has been opened for reading.
96  bool isOpen() const;
97 
98  /// Close the file once we are done reading from it.
99  void close();
100 
101  /// @brief Return this file's current size on disk in bytes.
102  /// @throw IoError if the file size cannot be determined.
103  Index64 getSize() const;
104 
105  /// @brief Return the size in bytes above which this file will not be
106  /// automatically copied during delayed loading.
107  Index64 copyMaxBytes() const;
108  /// @brief If this file is opened with delayed loading enabled, make a private copy
109  /// of the file if its size in bytes is less than the specified value.
110  /// @details Making a private copy ensures that the file can't change on disk
111  /// before it has been fully read.
112  /// @warning If the file is larger than this size, it is the user's responsibility
113  /// to ensure that it does not change on disk before it has been fully read.
114  /// Undefined behavior and/or a crash might result otherwise.
115  /// @note Copying is enabled by default, but it can be disabled for individual files
116  /// by setting the maximum size to zero bytes. A default size limit can be specified
117  /// by setting the environment variable @c OPENVDB_DELAYED_LOAD_COPY_MAX_BYTES
118  /// to the desired number of bytes.
119  void setCopyMaxBytes(Index64 bytes);
120 
121  /// Return @c true if a grid of the given name exists in this file.
122  bool hasGrid(const Name&) const;
123 
124  /// Return (in a newly created MetaMap) the file-level metadata.
125  MetaMap::Ptr getMetadata() const;
126 
127  /// Read the entire contents of the file and return a list of grid pointers.
128  GridPtrVecPtr getGrids() const;
129 
130  /// @brief Read just the grid metadata and transforms from the file and return a list
131  /// of pointers to grids that are empty except for their metadata and transforms.
132  /// @throw IoError if this file is not open for reading.
133  GridPtrVecPtr readAllGridMetadata();
134 
135  /// @brief Read a grid's metadata and transform only.
136  /// @return A pointer to a grid that is empty except for its metadata and transform.
137  /// @throw IoError if this file is not open for reading.
138  /// @throw KeyError if no grid with the given name exists in this file.
139  GridBase::Ptr readGridMetadata(const Name&);
140 
141  /// Read an entire grid, including all of its data blocks.
142  GridBase::Ptr readGrid(const Name&);
143 #if OPENVDB_ABI_VERSION_NUMBER >= 3
144  /// @brief Read a grid, including its data blocks, but only where it
145  /// intersects the given world-space bounding box.
146  GridBase::Ptr readGrid(const Name&, const BBoxd&);
147 #endif
148 
149  /// @todo GridPtrVec readAllGrids(const Name&)
150 
151  /// @brief Write the grids in the given container to the file whose name
152  /// was given in the constructor.
153  void write(const GridCPtrVec&, const MetaMap& = MetaMap()) const override;
154 
155  /// @brief Write the grids in the given container to the file whose name
156  /// was given in the constructor.
157  template<typename GridPtrContainerT>
158  void write(const GridPtrContainerT&, const MetaMap& = MetaMap()) const;
159 
160  /// A const iterator that iterates over all names in the file. This is only
161  /// valid once the file has been opened.
163  {
164  public:
165  NameIterator(const NameMapCIter& iter): mIter(iter) {}
166  NameIterator(const NameIterator&) = default;
168 
169  NameIterator& operator++() { mIter++; return *this; }
170 
171  bool operator==(const NameIterator& iter) const { return mIter == iter.mIter; }
172  bool operator!=(const NameIterator& iter) const { return mIter != iter.mIter; }
173 
174  Name operator*() const { return this->gridName(); }
175 
176  Name gridName() const { return GridDescriptor::nameAsString(mIter->second.uniqueName()); }
177 
178  private:
179  NameMapCIter mIter;
180  };
181 
182  /// @return a NameIterator to iterate over all grid names in the file.
183  NameIterator beginName() const;
184 
185  /// @return the ending iterator for all grid names in the file.
186  NameIterator endName() const;
187 
188 private:
189  /// Read in all grid descriptors that are stored in the given stream.
190  void readGridDescriptors(std::istream&);
191 
192  /// @brief Return an iterator to the descriptor for the grid with the given name.
193  /// If the name is non-unique, return an iterator to the first matching descriptor.
194  NameMapCIter findDescriptor(const Name&) const;
195 
196  /// Return a newly created, empty grid of the type specified by the given grid descriptor.
198 
199  /// @brief Read a grid, including its data blocks, but only where it
200  /// intersects the given world-space bounding box.
201  GridBase::Ptr readGridByName(const Name&, const BBoxd&);
202 
203  /// Read in and return the partially-populated grid specified by the given grid descriptor.
204  GridBase::ConstPtr readGridPartial(const GridDescriptor&, bool readTopology) const;
205 
206  /// Read in and return the grid specified by the given grid descriptor.
207  GridBase::Ptr readGrid(const GridDescriptor&) const;
208 #if OPENVDB_ABI_VERSION_NUMBER >= 3
209  /// Read in and return the region of the grid specified by the given grid descriptor
210  /// that intersects the given world-space bounding box.
211  GridBase::Ptr readGrid(const GridDescriptor&, const BBoxd&) const;
212  /// Read in and return the region of the grid specified by the given grid descriptor
213  /// that intersects the given index-space bounding box.
214  GridBase::Ptr readGrid(const GridDescriptor&, const CoordBBox&) const;
215 #endif
216 
217  /// @brief Partially populate the given grid by reading its metadata and transform and,
218  /// if the grid is not an instance, its tree structure, but not the tree's leaf nodes.
219  void readGridPartial(GridBase::Ptr, std::istream&, bool isInstance, bool readTopology) const;
220 
221  /// @brief Retrieve a grid from @c mNamedGrids. Return a null pointer
222  /// if @c mNamedGrids was not populated (because this file is random-access).
223  /// @throw KeyError if no grid with the given name exists in this file.
224  GridBase::Ptr retrieveCachedGrid(const Name&) const;
225 
226  void writeGrids(const GridCPtrVec&, const MetaMap&) const;
227 
228  MetaMap::Ptr fileMetadata();
229  MetaMap::ConstPtr fileMetadata() const;
230 
231  const NameMap& gridDescriptors() const;
232  NameMap& gridDescriptors();
233 
234  std::istream& inputStream() const;
235 
236  friend class ::TestFile;
237  friend class ::TestStream;
238 
239  struct Impl;
240  std::unique_ptr<Impl> mImpl;
241 };
242 
243 
244 ////////////////////////////////////////
245 
246 
247 inline void
248 File::write(const GridCPtrVec& grids, const MetaMap& meta) const
249 {
250  this->writeGrids(grids, meta);
251 }
252 
253 
254 template<typename GridPtrContainerT>
255 inline void
256 File::write(const GridPtrContainerT& container, const MetaMap& meta) const
257 {
258  GridCPtrVec grids;
259  std::copy(container.begin(), container.end(), std::back_inserter(grids));
260  this->writeGrids(grids, meta);
261 }
262 
263 } // namespace io
264 } // namespace OPENVDB_VERSION_NAME
265 } // namespace openvdb
266 
267 #endif // OPENVDB_IO_FILE_HAS_BEEN_INCLUDED
268 
269 // Copyright (c) 2012-2018 DreamWorks Animation LLC
270 // All rights reserved. This software is distributed under the
271 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
Grid serializer/unserializer.
Definition: Archive.h:59
Definition: ImfName.h:53
void write(const GridCPtrVec &, const MetaMap &=MetaMap()) const override
Write the grids in the given container to the file whose name was given in the constructor.
Definition: File.h:248
GT_API const UT_StringHolder filename
GridType::Ptr createGrid(const typename GridType::ValueType &background)
Create a new grid of type GridType with a given background value.
Definition: Grid.h:1582
SharedPtr< GridBase > Ptr
Definition: Grid.h:107
bool operator!=(const NameIterator &iter) const
Definition: File.h:172
GLsizei const GLchar *const * string
Definition: glcorearb.h:813
Grid archive associated with a file on disk.
Definition: File.h:57
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:189
NameMap::const_iterator NameMapCIter
Definition: File.h:61
bool operator==(const NameIterator &iter) const
Definition: File.h:171
Container that maps names (strings) to values of arbitrary types.
Definition: MetaMap.h:46
std::function< void(std::string)> Notifier
Definition: io.h:172
std::shared_ptr< T > SharedPtr
Definition: Types.h:139
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:194
std::multimap< Name, GridDescriptor > NameMap
Definition: File.h:60
virtual int open(float queuesize)
SharedPtr< const MetaMap > ConstPtr
Definition: MetaMap.h:50
SharedPtr< GridPtrVec > GridPtrVecPtr
Definition: Grid.h:440
virtual void close()
SharedPtr< const GridBase > ConstPtr
Definition: Grid.h:108
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
static std::string nameAsString(const Name &)
Given a name with suffix N, return "name[N]", otherwise just return "name". Use this to produce a...