OpenVDB  2.3.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  virtual ~File();
61 
65  File(const File& other);
69  File& operator=(const File& other);
70 
74  virtual boost::shared_ptr<Archive> copy() const;
75 
78  const std::string& filename() const { return mFilename; }
79 
84  bool open();
85 
87  bool isOpen() const { return mIsOpen; }
88 
90  void close();
91 
93  bool hasGrid(const Name&) const;
94 
96  MetaMap::Ptr getMetadata() const;
97 
99  GridPtrVecPtr getGrids() const;
100 
104  GridPtrVecPtr readAllGridMetadata();
105 
110  GridBase::Ptr readGridMetadata(const Name&);
111 
118  GridBase::ConstPtr readGridPartial(const Name&);
119 
121  GridBase::Ptr readGrid(const Name&);
122 
125 
128  virtual void write(const GridCPtrVec&, const MetaMap& = MetaMap()) const;
129 
132  template<typename GridPtrContainerT>
133  void write(const GridPtrContainerT&, const MetaMap& = MetaMap()) const;
134 
138  {
139  public:
140  NameIterator(const NameMapCIter& iter): mIter(iter) {}
142 
143  NameIterator& operator++() { mIter++; return *this; }
144 
145  bool operator==(const NameIterator& iter) const { return mIter == iter.mIter; }
146  bool operator!=(const NameIterator& iter) const { return mIter != iter.mIter; }
147 
148  Name operator*() const { return this->gridName(); }
149 
150  Name gridName() const { return GridDescriptor::nameAsString(mIter->second.uniqueName()); }
151 
152  private:
153  NameMapCIter mIter;
154  };
155 
157  NameIterator beginName() const;
158 
160  NameIterator endName() const;
161 
162 private:
164  void resetInStream() const { mInStream.seekg(0, std::ios::beg); }
165 
167  void readGridDescriptors(std::istream&);
168 
171  NameMapCIter findDescriptor(const Name&) const;
172 
174  GridBase::Ptr createGrid(const GridDescriptor&) const;
175 
177  GridBase::ConstPtr readGridPartial(const GridDescriptor&, bool readTopology) const;
178 
180  GridBase::Ptr readGrid(const GridDescriptor&) const;
181 
184  void readGridPartial(GridBase::Ptr, std::istream&, bool isInstance, bool readTopology) const;
185 
186  void writeGrids(const GridCPtrVec&, const MetaMap&) const;
187 
188  friend class ::TestFile;
189  friend class ::TestStream;
190 
191 
192  std::string mFilename;
194  MetaMap::Ptr mMeta;
196  mutable std::ifstream mInStream;
199  bool mIsOpen;
201  NameMap mGridDescriptors;
203  Archive::NamedGridMap mNamedGrids;
205  GridPtrVecPtr mGrids;
206 };
207 
208 
210 
211 
212 inline void
213 File::write(const GridCPtrVec& grids, const MetaMap& metadata) const
214 {
215  this->writeGrids(grids, metadata);
216 }
217 
218 
219 template<typename GridPtrContainerT>
220 inline void
221 File::write(const GridPtrContainerT& container, const MetaMap& metadata) const
222 {
223  GridCPtrVec grids;
224  std::copy(container.begin(), container.end(), std::back_inserter(grids));
225  this->writeGrids(grids, metadata);
226 }
227 
228 } // namespace io
229 } // namespace OPENVDB_VERSION_NAME
230 } // namespace openvdb
231 
232 #endif // OPENVDB_IO_FILE_HAS_BEEN_INCLUDED
233 
234 // Copyright (c) 2012-2013 DreamWorks Animation LLC
235 // All rights reserved. This software is distributed under the
236 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
std::map< Name, GridBase::Ptr > NamedGridMap
Definition: Archive.h:213
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:187
bool operator==(const NameIterator &iter) const
Definition: File.h:145
const std::string & filename() const
Return the name of the file with which this archive is associated.
Definition: File.h:78
Grid archive associated with a file on disk.
Definition: File.h:53
Name gridName() const
Definition: File.h:150
boost::shared_ptr< MetaMap > Ptr
Definition: MetaMap.h:51
boost::shared_ptr< GridBase > Ptr
Definition: Grid.h:106
boost::shared_ptr< GridPtrVec > GridPtrVecPtr
Definition: Grid.h:399
GridType::Ptr createGrid(const typename GridType::ValueType &background)
Create a new grid of type GridType with a given background value.
Definition: Grid.h:1292
bool isOpen() const
Return true if the file has been opened for reading.
Definition: File.h:87
#define OPENVDB_VERSION_NAME
Definition: version.h:45
boost::shared_ptr< const GridBase > ConstPtr
Definition: Grid.h:107
Name operator*() const
Definition: File.h:148
std::vector< GridBase::ConstPtr > GridCPtrVec
Definition: Grid.h:401
Provides functionality storing type agnostic metadata information. Grids and other structures can inh...
Definition: MetaMap.h:48
bool operator!=(const NameIterator &iter) const
Definition: File.h:146
std::multimap< Name, GridDescriptor > NameMap
Definition: File.h:56
NameIterator(const NameMapCIter &iter)
Definition: File.h:140
Grid serializer/unserializer.
Definition: Archive.h:113
NameMap::const_iterator NameMapCIter
Definition: File.h:57
~NameIterator()
Definition: File.h:141
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:67
NameIterator & operator++()
Definition: File.h:143
std::string Name
Definition: Name.h:44