OpenVDB  2.0.0
File.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2013 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 <iostream>
37 #include <fstream>
38 #include <map>
39 #include <string>
40 #include "Archive.h"
41 #include "GridDescriptor.h"
42 
43 
44 class TestFile;
45 class TestStream;
46 
47 namespace openvdb {
49 namespace OPENVDB_VERSION_NAME {
50 namespace io {
51 
53 class OPENVDB_API File: public Archive
54 {
55 public:
56  typedef std::multimap<Name, GridDescriptor> NameMap;
57  typedef NameMap::const_iterator NameMapCIter;
58 
59  explicit File(const std::string& filename);
60  ~File();
61 
62  const std::string& filename() const { return mFilename; }
63 
68  bool open();
69 
71  bool isOpen() const { return mIsOpen; }
72 
74  void close();
75 
77  bool hasGrid(const Name&) const;
78 
80  MetaMap::Ptr getMetadata() const;
81 
83  GridPtrVecPtr getGrids() const;
84 
88  GridPtrVecPtr readAllGridMetadata();
89 
94  GridBase::Ptr readGridMetadata(const Name&);
95 
102  GridBase::ConstPtr readGridPartial(const Name&);
103 
105  GridBase::Ptr readGrid(const Name&);
106 
109 
112  template<typename GridPtrContainerT>
113  void write(const GridPtrContainerT&, const MetaMap& = MetaMap()) const;
114 
118  {
119  public:
120  NameIterator(const NameMapCIter& iter): mIter(iter) {}
122 
123  NameIterator& operator++() { mIter++; return *this; }
124 
125  bool operator==(const NameIterator& iter) const { return mIter == iter.mIter; }
126  bool operator!=(const NameIterator& iter) const { return mIter != iter.mIter; }
127 
128  Name operator*() const { return this->gridName(); }
129 
130  Name gridName() const { return GridDescriptor::nameAsString(mIter->second.uniqueName()); }
131 
132  private:
133  NameMapCIter mIter;
134  };
135 
137  NameIterator beginName() const;
138 
140  NameIterator endName() const;
141 
142 private:
144  void resetInStream() const { mInStream.seekg(0, std::ios::beg); }
145 
147  void readGridDescriptors(std::istream&);
148 
151  NameMapCIter findDescriptor(const Name&) const;
152 
154  GridBase::Ptr createGrid(const GridDescriptor&) const;
155 
157  GridBase::ConstPtr readGridPartial(const GridDescriptor&, bool readTopology) const;
158 
160  GridBase::Ptr readGrid(const GridDescriptor&) const;
161 
164  void readGridPartial(GridBase::Ptr, std::istream&, bool isInstance, bool readTopology) const;
165 
166  void writeGrids(const GridCPtrVec&, const MetaMap&) const;
167 
168  // Disallow copying of instances of this class.
169  File(const File& other);
170  File& operator=(const File& other);
171 
172  friend class ::TestFile;
173  friend class ::TestStream;
174 
175 
176  std::string mFilename;
178  MetaMap::Ptr mMeta;
180  mutable std::ifstream mInStream;
183  bool mIsOpen;
185  NameMap mGridDescriptors;
187  Archive::NamedGridMap mNamedGrids;
189  GridPtrVecPtr mGrids;
190 };
191 
192 
194 
195 
196 template<typename GridPtrContainerT>
197 inline void
198 File::write(const GridPtrContainerT& container, const MetaMap& metadata) const
199 {
200  GridCPtrVec grids;
201  std::copy(container.begin(), container.end(), std::back_inserter(grids));
202  this->writeGrids(grids, metadata);
203 }
204 
205 template<>
206 inline void
207 File::write<GridCPtrVec>(const GridCPtrVec& grids, const MetaMap& metadata) const
208 {
209  this->writeGrids(grids, metadata);
210 }
211 
212 } // namespace io
213 } // namespace OPENVDB_VERSION_NAME
214 } // namespace openvdb
215 
216 #endif // OPENVDB_IO_FILE_HAS_BEEN_INCLUDED
217 
218 // Copyright (c) 2012-2013 DreamWorks Animation LLC
219 // All rights reserved. This software is distributed under the
220 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:162
boost::shared_ptr< MetaMap > Ptr
Definition: MetaMap.h:51
std::vector< GridBase::ConstPtr > GridCPtrVec
Definition: Grid.h:401
NameIterator & operator++()
Definition: File.h:123
const std::string & filename() const
Definition: File.h:62
NameIterator(const NameMapCIter &iter)
Definition: File.h:120
bool isOpen() const
Return true if the file has been opened for reading, false otherwise.
Definition: File.h:71
bool operator!=(const NameIterator &iter) const
Definition: File.h:126
std::string Name
Definition: Name.h:44
~NameIterator()
Definition: File.h:121
#define OPENVDB_VERSION_NAME
Definition: version.h:45
Name gridName() const
Definition: File.h:130
bool operator==(const NameIterator &iter) const
Definition: File.h:125
Provides functionality storing type agnostic metadata information. Grids and other structures can inh...
Definition: MetaMap.h:48
std::map< Name, GridBase::Ptr > NamedGridMap
Definition: Archive.h:206
Name operator*() const
Definition: File.h:128
boost::shared_ptr< const GridBase > ConstPtr
Definition: Grid.h:107
std::multimap< Name, GridDescriptor > NameMap
Definition: File.h:56
Grid archive associated with a file on disk.
Definition: File.h:53
Grid serializer/unserializer.
Definition: Archive.h:112
boost::shared_ptr< GridPtrVec > GridPtrVecPtr
Definition: Grid.h:399
NameMap::const_iterator NameMapCIter
Definition: File.h:57
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:56
GridType::Ptr createGrid(const typename GridType::ValueType &background)
Create a new grid of type GridType with a given background value.
Definition: Grid.h:1236
boost::shared_ptr< GridBase > Ptr
Definition: Grid.h:106