OpenVDB  6.1.0
File.h
Go to the documentation of this file.
1 //
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 //
30 //
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 
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 
69  File(const File& other);
73  File& operator=(const File& other);
74 
78  SharedPtr<Archive> copy() const override;
79 
82  const std::string& filename() const;
83 
93  bool open(bool delayLoad = true, const MappedFile::Notifier& = MappedFile::Notifier());
94 
96  bool isOpen() const;
97 
99  void close();
100 
103  Index64 getSize() const;
104 
107  Index64 copyMaxBytes() const;
119  void setCopyMaxBytes(Index64 bytes);
120 
122  bool hasGrid(const Name&) const;
123 
125  MetaMap::Ptr getMetadata() const;
126 
128  GridPtrVecPtr getGrids() const;
129 
133  GridPtrVecPtr readAllGridMetadata();
134 
139  GridBase::Ptr readGridMetadata(const Name&);
140 
142  GridBase::Ptr readGrid(const Name&);
143 #if OPENVDB_ABI_VERSION_NUMBER >= 3
144  GridBase::Ptr readGrid(const Name&, const BBoxd&);
147 #endif
148 
150 
153  void write(const GridCPtrVec&, const MetaMap& = MetaMap()) const override;
154 
157  template<typename GridPtrContainerT>
158  void write(const GridPtrContainerT&, const MetaMap& = MetaMap()) const;
159 
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 
183  NameIterator beginName() const;
184 
186  NameIterator endName() const;
187 
188 private:
190  void readGridDescriptors(std::istream&);
191 
194  NameMapCIter findDescriptor(const Name&) const;
195 
198 
201  GridBase::Ptr readGridByName(const Name&, const BBoxd&);
202 
204  GridBase::ConstPtr readGridPartial(const GridDescriptor&, bool readTopology) const;
205 
207  GridBase::Ptr readGrid(const GridDescriptor&) const;
208 #if OPENVDB_ABI_VERSION_NUMBER >= 3
209  GridBase::Ptr readGrid(const GridDescriptor&, const BBoxd&) const;
214  GridBase::Ptr readGrid(const GridDescriptor&, const CoordBBox&) const;
215 #endif
216 
219  void readGridPartial(GridBase::Ptr, std::istream&, bool isInstance, bool readTopology) const;
220 
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 
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/ )
SharedPtr< const GridBase > ConstPtr
Definition: Grid.h:108
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:288
SharedPtr< const MetaMap > ConstPtr
Definition: MetaMap.h:50
Definition: GridDescriptor.h:46
Name gridName() const
Definition: File.h:176
SharedPtr< MetaMap > Ptr
Definition: MetaMap.h:49
Grid archive associated with a file on disk.
Definition: File.h:57
std::shared_ptr< T > SharedPtr
Definition: Types.h:139
SharedPtr< GridPtrVec > GridPtrVecPtr
Definition: Grid.h:530
std::string Name
Definition: Name.h:44
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:125
uint64_t Index64
Definition: Types.h:60
GridType::Ptr createGrid(const typename GridType::ValueType &background)
Create a new grid of type GridType with a given background value.
Definition: Grid.h:1677
NameIterator(const NameMapCIter &iter)
Definition: File.h:165
Definition: Exceptions.h:40
SharedPtr< GridBase > Ptr
Definition: Grid.h:107
bool operator==(const NameIterator &iter) const
Definition: File.h:171
std::vector< GridBase::ConstPtr > GridCPtrVec
Definition: Grid.h:532
std::function< void(std::string)> Notifier
Definition: io.h:173
std::multimap< Name, GridDescriptor > NameMap
Definition: File.h:60
NameIterator & operator++()
Definition: File.h:169
Library and file format version numbers.
Grid serializer/unserializer.
Definition: Archive.h:59
bool operator!=(const NameIterator &iter) const
Definition: File.h:172
NameMap::const_iterator NameMapCIter
Definition: File.h:61
Name operator*() const
Definition: File.h:174
~NameIterator()
Definition: File.h:167
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:177
Container that maps names (strings) to values of arbitrary types.
Definition: MetaMap.h:46