OpenVDB: File.h Source File

OpenVDB 7.0.0

File.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3 //
5
6 #ifndef OPENVDB_IO_FILE_HAS_BEEN_INCLUDED
7 #define OPENVDB_IO_FILE_HAS_BEEN_INCLUDED
8
9 #include <openvdb/version.h>
10 #include "io.h" // for MappedFile::Notifier
11 #include "Archive.h"
12 #include "GridDescriptor.h"
13 #include <algorithm> // for std::copy()
14 #include <iosfwd>
15 #include <iterator> // for std::back_inserter()
16 #include <map>
17 #include <memory>
18 #include <string>
19
20
21 class TestFile;
22 class TestStream;
23
24 namespace openvdb {
25 OPENVDB_USE_VERSION_NAMESPACE
26 namespace OPENVDB_VERSION_NAME {
27 namespace io {
28
30 class OPENVDB_API File: public Archive
31 {
32 public:
33 using NameMap = std::multimap<Name, GridDescriptor>;
34 using NameMapCIter = NameMap::const_iterator;
35
36 explicit File(const std::string& filename);
37 ~File() override;
38
42 File(const File& other);
46 File& operator=(const File& other);
47
51 SharedPtr<Archive> copy() const override;
52
55 const std::string& filename() const;
56
66 bool open(bool delayLoad = true, const MappedFile::Notifier& = MappedFile::Notifier());
67
69 bool isOpen() const;
70
72 void close();
73
76 Index64 getSize() const;
77
80 Index64 copyMaxBytes() const;
92 void setCopyMaxBytes(Index64 bytes);
93
95 bool hasGrid(const Name&) const;
96
98 MetaMap::Ptr getMetadata() const;
99
101 GridPtrVecPtr getGrids() const;
102
106 GridPtrVecPtr readAllGridMetadata();
107
112 GridBase::Ptr readGridMetadata(const Name&);
113
115 GridBase::Ptr readGrid(const Name&);
118 GridBase::Ptr readGrid(const Name&, const BBoxd&);
119
121
124 void write(const GridCPtrVec&, const MetaMap& = MetaMap()) const override;
125
128 template<typename GridPtrContainerT>
129 void write(const GridPtrContainerT&, const MetaMap& = MetaMap()) const;
130
133 class NameIterator
134 {
135 public:
136 NameIterator(const NameMapCIter& iter): mIter(iter) {}
137 NameIterator(const NameIterator&) = default;
138 ~NameIterator() {}
139
140 NameIterator& operator++() { mIter++; return *this; }
141
142 bool operator==(const NameIterator& iter) const { return mIter == iter.mIter; }
143 bool operator!=(const NameIterator& iter) const { return mIter != iter.mIter; }
144
145 Name operator*() const { return this->gridName(); }
146
147 Name gridName() const { return GridDescriptor::nameAsString(mIter->second.uniqueName()); }
148
149 private:
150 NameMapCIter mIter;
151 };
152
154 NameIterator beginName() const;
155
157 NameIterator endName() const;
158
159 private:
161 void readGridDescriptors(std::istream&);
162
165 NameMapCIter findDescriptor(const Name&) const;
166
168 GridBase::Ptr createGrid(const GridDescriptor&) const;
169
172 GridBase::Ptr readGridByName(const Name&, const BBoxd&);
173
175 GridBase::ConstPtr readGridPartial(const GridDescriptor&, bool readTopology) const;
176
178 GridBase::Ptr readGrid(const GridDescriptor&) const;
181 GridBase::Ptr readGrid(const GridDescriptor&, const BBoxd&) const;
184 GridBase::Ptr readGrid(const GridDescriptor&, const CoordBBox&) const;
185
188 void readGridPartial(GridBase::Ptr, std::istream&, bool isInstance, bool readTopology) const;
189
193 GridBase::Ptr retrieveCachedGrid(const Name&) const;
194
195 void writeGrids(const GridCPtrVec&, const MetaMap&) const;
196
197 MetaMap::Ptr fileMetadata();
198 MetaMap::ConstPtr fileMetadata() const;
199
200 const NameMap& gridDescriptors() const;
201 NameMap& gridDescriptors();
202
203 std::istream& inputStream() const;
204
205 friend class ::TestFile;
206 friend class ::TestStream;
207
208 struct Impl;
209 std::unique_ptr<Impl> mImpl;
210 };
211
212
214
215
216 inline void
217 File::write(const GridCPtrVec& grids, const MetaMap& meta) const
218 {
219 this->writeGrids(grids, meta);
220 }
221
222
223 template<typename GridPtrContainerT>
224 inline void
225 File::write(const GridPtrContainerT& container, const MetaMap& meta) const
226 {
227 GridCPtrVec grids;
228 std::copy(container.begin(), container.end(), std::back_inserter(grids));
229 this->writeGrids(grids, meta);
230 }
231
232 } // namespace io
233 } // namespace OPENVDB_VERSION_NAME
234 } // namespace openvdb
235
236 #endif // OPENVDB_IO_FILE_HAS_BEEN_INCLUDED
openvdb::v7_0::io::File::~File
~File() override
openvdb::v7_0::io::File::NameIterator::NameIterator
NameIterator(const NameMapCIter &iter)
Definition: File.h:136
openvdb::v7_0::io::File::isOpen
bool isOpen() const
Return true if the file has been opened for reading.
openvdb::v7_0::io::File::readGrid
GridBase::Ptr readGrid(const Name &, const BBoxd &)
Read a grid, including its data blocks, but only where it intersects the given world-space bounding b...
openvdb::v7_0::math::BBox< Vec3d >
openvdb::v7_0::io::File
Grid archive associated with a file on disk.
Definition: File.h:31
openvdb::v7_0::io::File::readGridMetadata
GridBase::Ptr readGridMetadata(const Name &)
Read a grid's metadata and transform only.
openvdb::v7_0::io::File::NameIterator::operator*
Name operator*() const
Definition: File.h:145
openvdb::v7_0::io::File::File
File(const std::string &filename)
openvdb::v7_0::io::File::NameIterator::operator++
NameIterator & operator++()
Definition: File.h:140
openvdb::v7_0::io::File::NameMap
std::multimap< Name, GridDescriptor > NameMap
Definition: File.h:33
openvdb::v7_0::io::Archive
Grid serializer/unserializer.
Definition: Archive.h:33
openvdb::v7_0::GridCPtrVec
std::vector< GridBase::ConstPtr > GridCPtrVec
Definition: Grid.h:514
openvdb::v7_0::io::File::NameMapCIter
NameMap::const_iterator NameMapCIter
Definition: File.h:34
version.h
Library and file format version numbers.
openvdb::v7_0::io::File::NameIterator
Definition: File.h:134
openvdb::v7_0::SharedPtr
std::shared_ptr< T > SharedPtr
Definition: Types.h:91
openvdb::v7_0::io::File::getGrids
GridPtrVecPtr getGrids() const
Read the entire contents of the file and return a list of grid pointers.
openvdb::v7_0::GridBase::ConstPtr
SharedPtr< const GridBase > ConstPtr
Definition: Grid.h:81
openvdb::v7_0::math::CoordBBox
Axis-aligned bounding box of signed integer coordinates.
Definition: Coord.h:249
GridDescriptor.h
openvdb::v7_0::io::File::close
void close()
Close the file once we are done reading from it.
OPENVDB_API
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:230
openvdb::v7_0::io::File::NameIterator::~NameIterator
~NameIterator()
Definition: File.h:138
openvdb::v7_0::MetaMap::Ptr
SharedPtr< MetaMap > Ptr
Definition: MetaMap.h:22
openvdb::v7_0::GridBase::Ptr
SharedPtr< GridBase > Ptr
Definition: Grid.h:80
openvdb::v7_0::io::File::File
File(const File &other)
Copy constructor.
openvdb::v7_0::MetaMap
Container that maps names (strings) to values of arbitrary types.
Definition: MetaMap.h:20
openvdb::v7_0::io::File::beginName
NameIterator beginName() const
openvdb::v7_0::io::File::NameIterator::operator==
bool operator==(const NameIterator &iter) const
Definition: File.h:142
openvdb::v7_0::io::File::getSize
Index64 getSize() const
Return this file's current size on disk in bytes.
openvdb::v7_0::io::File::NameIterator::NameIterator
NameIterator(const NameIterator &)=default
openvdb::v7_0::io::File::copyMaxBytes
Index64 copyMaxBytes() const
Return the size in bytes above which this file will not be automatically copied during delayed loadin...
Archive.h
openvdb::v7_0::io::File::NameIterator::gridName
Name gridName() const
Definition: File.h:147
openvdb::v7_0::io::GridDescriptor
Definition: GridDescriptor.h:20
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:154
openvdb::v7_0::io::File::NameIterator::operator!=
bool operator!=(const NameIterator &iter) const
Definition: File.h:143
openvdb::v7_0::io::File::copy
SharedPtr< Archive > copy() const override
Return a copy of this archive.
openvdb::v7_0::io::File::endName
NameIterator endName() const
openvdb::v7_0::io::MappedFile::Notifier
std::function< void(std::string)> Notifier
Definition: io.h:156
io.h
openvdb::v7_0::Name
std::string Name
Definition: Name.h:17
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:102
openvdb::v7_0::MetaMap::ConstPtr
SharedPtr< const MetaMap > ConstPtr
Definition: MetaMap.h:23
openvdb::v7_0::io::File::hasGrid
bool hasGrid(const Name &) const
Return true if a grid of the given name exists in this file.
openvdb::v7_0::io::File::readAllGridMetadata
GridPtrVecPtr readAllGridMetadata()
Read just the grid metadata and transforms from the file and return a list of pointers to grids that ...
openvdb::v7_0::io::File::setCopyMaxBytes
void setCopyMaxBytes(Index64 bytes)
If this file is opened with delayed loading enabled, make a private copy of the file if its size in b...
openvdb::v7_0::io::File::filename
const std::string & filename() const
Return the name of the file with which this archive is associated.
openvdb::v7_0::GridPtrVecPtr
SharedPtr< GridPtrVec > GridPtrVecPtr
Definition: Grid.h:512
openvdb
Definition: Exceptions.h:13
openvdb::v7_0::Index64
uint64_t Index64
Definition: Types.h:30
openvdb::v7_0::io::File::open
bool open(bool delayLoad=true, const MappedFile::Notifier &=MappedFile::Notifier())
Open the file, read the file header and the file-level metadata, and populate the grid descriptors,...
openvdb::v7_0::createGrid
GridType::Ptr createGrid(const typename GridType::ValueType &background)
Create a new grid of type GridType with a given background value.
Definition: Grid.h:1728
openvdb::v7_0::io::File::readGrid
GridBase::Ptr readGrid(const Name &)
Read an entire grid, including all of its data blocks.
openvdb::v7_0::io::File::operator=
File & operator=(const File &other)
Assignment.
openvdb::v7_0::io::File::getMetadata
MetaMap::Ptr getMetadata() const
Return (in a newly created MetaMap) the file-level metadata.