OpenVDB  4.0.1
File.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2017 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 "io.h" // for MappedFile::Notifier
37 #include "Archive.h"
38 #include "GridDescriptor.h"
39 #include <iosfwd>
40 #include <map>
41 #include <memory>
42 #include <string>
43 
44 
45 class TestFile;
46 class TestStream;
47 
48 namespace openvdb {
50 namespace OPENVDB_VERSION_NAME {
51 namespace io {
52 
54 class OPENVDB_API File: public Archive
55 {
56 public:
57  using NameMap = std::multimap<Name, GridDescriptor>;
58  using NameMapCIter = NameMap::const_iterator;
59 
60  explicit File(const std::string& filename);
61  ~File() override;
62 
66  File(const File& other);
70  File& operator=(const File& other);
71 
75  SharedPtr<Archive> copy() const override;
76 
79  const std::string& filename() const;
80 
90  bool open(bool delayLoad = true, const MappedFile::Notifier& = MappedFile::Notifier());
91 
93  bool isOpen() const;
94 
96  void close();
97 
100  Index64 getSize() const;
101 
104  Index64 copyMaxBytes() const;
116  void setCopyMaxBytes(Index64 bytes);
117 
119  bool hasGrid(const Name&) const;
120 
122  MetaMap::Ptr getMetadata() const;
123 
125  GridPtrVecPtr getGrids() const;
126 
130  GridPtrVecPtr readAllGridMetadata();
131 
136  GridBase::Ptr readGridMetadata(const Name&);
137 
143  OPENVDB_DEPRECATED GridBase::ConstPtr readGridPartial(const Name&);
144 
146  GridBase::Ptr readGrid(const Name&);
147 #ifndef OPENVDB_2_ABI_COMPATIBLE
148  GridBase::Ptr readGrid(const Name&, const BBoxd&);
151 #endif
152 
155 
158  void write(const GridCPtrVec&, const MetaMap& = MetaMap()) const override;
159 
162  template<typename GridPtrContainerT>
163  void write(const GridPtrContainerT&, const MetaMap& = MetaMap()) const;
164 
168  {
169  public:
170  NameIterator(const NameMapCIter& iter): mIter(iter) {}
171  NameIterator(const NameIterator&) = default;
173 
174  NameIterator& operator++() { mIter++; return *this; }
175 
176  bool operator==(const NameIterator& iter) const { return mIter == iter.mIter; }
177  bool operator!=(const NameIterator& iter) const { return mIter != iter.mIter; }
178 
179  Name operator*() const { return this->gridName(); }
180 
181  Name gridName() const { return GridDescriptor::nameAsString(mIter->second.uniqueName()); }
182 
183  private:
184  NameMapCIter mIter;
185  };
186 
188  NameIterator beginName() const;
189 
191  NameIterator endName() const;
192 
193 private:
195  void readGridDescriptors(std::istream&);
196 
199  NameMapCIter findDescriptor(const Name&) const;
200 
203 
206  GridBase::Ptr readGridByName(const Name&, const BBoxd&);
207 
209  GridBase::ConstPtr readGridPartial(const GridDescriptor&, bool readTopology) const;
210 
212  GridBase::Ptr readGrid(const GridDescriptor&) const;
213 #ifndef OPENVDB_2_ABI_COMPATIBLE
214  GridBase::Ptr readGrid(const GridDescriptor&, const BBoxd&) const;
219  GridBase::Ptr readGrid(const GridDescriptor&, const CoordBBox&) const;
220 #endif
221 
224  void readGridPartial(GridBase::Ptr, std::istream&, bool isInstance, bool readTopology) const;
225 
229  GridBase::Ptr retrieveCachedGrid(const Name&) const;
230 
231  void writeGrids(const GridCPtrVec&, const MetaMap&) const;
232 
233  MetaMap::Ptr fileMetadata();
234  MetaMap::ConstPtr fileMetadata() const;
235 
236  const NameMap& gridDescriptors() const;
237  NameMap& gridDescriptors();
238 
239  std::istream& inputStream() const;
240 
241  friend class ::TestFile;
242  friend class ::TestStream;
243 
244  struct Impl;
245  std::unique_ptr<Impl> mImpl;
246 };
247 
248 
250 
251 
252 inline void
253 File::write(const GridCPtrVec& grids, const MetaMap& meta) const
254 {
255  this->writeGrids(grids, meta);
256 }
257 
258 
259 template<typename GridPtrContainerT>
260 inline void
261 File::write(const GridPtrContainerT& container, const MetaMap& meta) const
262 {
263  GridCPtrVec grids;
264  std::copy(container.begin(), container.end(), std::back_inserter(grids));
265  this->writeGrids(grids, meta);
266 }
267 
268 } // namespace io
269 } // namespace OPENVDB_VERSION_NAME
270 } // namespace openvdb
271 
272 #endif // OPENVDB_IO_FILE_HAS_BEEN_INCLUDED
273 
274 // Copyright (c) 2012-2017 DreamWorks Animation LLC
275 // All rights reserved. This software is distributed under the
276 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
NameMap::const_iterator NameMapCIter
Definition: File.h:58
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:194
#define OPENVDB_DEPRECATED
Definition: Platform.h:49
Name gridName() const
Definition: File.h:181
~NameIterator()
Definition: File.h:172
Container that maps names (strings) to values of arbitrary types.
Definition: MetaMap.h:46
NameIterator & operator++()
Definition: File.h:174
GridType::Ptr createGrid(const typename GridType::ValueType &background)
Create a new grid of type GridType with a given background value.
Definition: Grid.h:1573
std::vector< GridBase::ConstPtr > GridCPtrVec
Definition: Grid.h:441
Definition: GridDescriptor.h:46
bool operator!=(const NameIterator &iter) const
Definition: File.h:177
std::multimap< Name, GridDescriptor > NameMap
Definition: File.h:57
std::function< void(std::string)> Notifier
Definition: io.h:172
#define OPENVDB_VERSION_NAME
Definition: version.h:43
Grid archive associated with a file on disk.
Definition: File.h:54
bool operator==(const NameIterator &iter) const
Definition: File.h:176
Definition: Exceptions.h:39
SharedPtr< MetaMap > Ptr
Definition: MetaMap.h:49
uint64_t Index64
Definition: Types.h:56
std::string Name
Definition: Name.h:44
NameIterator(const NameMapCIter &iter)
Definition: File.h:170
std::shared_ptr< T > SharedPtr
Definition: Types.h:130
Name operator*() const
Definition: File.h:179
SharedPtr< GridBase > Ptr
Definition: Grid.h:106
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
SharedPtr< const MetaMap > ConstPtr
Definition: MetaMap.h:50
SharedPtr< const GridBase > ConstPtr
Definition: Grid.h:107
Grid serializer/unserializer.
Definition: Archive.h:58
SharedPtr< GridPtrVec > GridPtrVecPtr
Definition: Grid.h:439