Loading...
Searching...
No Matches
Archive.h
Go to the documentation of this file.
// Copyright Contributors to the OpenVDB Project
// SPDX-License-Identifier: MPL-2.0
#ifndef OPENVDB_IO_ARCHIVE_HAS_BEEN_INCLUDED
#define OPENVDB_IO_ARCHIVE_HAS_BEEN_INCLUDED
#include <openvdb/version.h>
#include "Compression.h" // for COMPRESS_ZIP, etc.
#include <openvdb/Grid.h>
#include <openvdb/MetaMap.h>
#include <openvdb/Platform.h>
#include <openvdb/version.h> // for VersionId
#include <cstdint>
#include <iosfwd>
#include <map>
#include <memory>
#include <string>
class TestFile;
namespace openvdb {
OPENVDB_USE_VERSION_NAMESPACE
namespace OPENVDB_VERSION_NAME {
namespace io {
class GridDescriptor;
/// Grid serializer/unserializer
class OPENVDB_API Archive
{
public:
using Ptr = SharedPtr<Archive>;
using ConstPtr = SharedPtr<const Archive>;
static const uint32_t DEFAULT_COMPRESSION_FLAGS;
Archive();
Archive(const Archive&) = default;
Archive& operator=(const Archive&) = default;
virtual ~Archive();
/// @brief Return a copy of this archive.
virtual Ptr copy() const;
/// @brief Return the UUID that was most recently written (or read,
/// if no UUID has been written yet).
std::string getUniqueTag() const;
/// @brief Return @c true if the given UUID matches this archive's UUID.
bool isIdentical(const std::string& uuidStr) const;
/// @brief Return the file format version number of the input stream.
uint32_t fileVersion() const { return mFileVersion; }
/// @brief Return the (major, minor) version number of the library that was
/// used to write the input stream.
VersionId libraryVersion() const { return mLibraryVersion; }
/// @brief Return a string of the form "<major>.<minor>/<format>", giving the
/// library and file format version numbers associated with the input stream.
std::string version() const;
/// @brief Return @c true if trees shared by multiple grids are written out
/// only once, @c false if they are written out once per grid.
bool isInstancingEnabled() const { return mEnableInstancing; }
/// @brief Specify whether trees shared by multiple grids should be
/// written out only once (@c true) or once per grid (@c false).
/// @note Instancing is enabled by default.
void setInstancingEnabled(bool b) { mEnableInstancing = b; }
/// Return @c true if the OpenVDB library includes support for the Blosc compressor.
static bool hasBloscCompression();
/// Return @c true if the OpenVDB library includes support for the ZLib compressor.
static bool hasZLibCompression();
/// Return a bit mask specifying compression options for the data stream.
uint32_t compression() const { return mCompression; }
/// @brief Specify whether and how the data stream should be compressed.
/// @param c bitwise OR (e.g., COMPRESS_ZIP | COMPRESS_ACTIVE_MASK) of
/// compression option flags (see Compression.h for the available flags)
/// @note Not all combinations of compression options are supported.
void setCompression(uint32_t c) { mCompression = c; }
/// @brief Return @c true if grid statistics (active voxel count and
/// bounding box, etc.) are computed and written as grid metadata.
bool isGridStatsMetadataEnabled() const { return mEnableGridStats; }
/// @brief Specify whether grid statistics (active voxel count and
/// bounding box, etc.) should be computed and written as grid metadata.
void setGridStatsMetadataEnabled(bool b) { mEnableGridStats = b; }
/// @brief Write the grids in the given container to this archive's output stream.
virtual void write(const GridCPtrVec&, const MetaMap& = MetaMap()) const {}
/// @brief Return @c true if delayed loading is enabled.
/// @details If enabled, delayed loading can be disabled for individual files,
/// but not vice-versa.
/// @note Define the environment variable @c OPENVDB_DISABLE_DELAYED_LOAD
/// to disable delayed loading unconditionally.
static bool isDelayedLoadingEnabled();
protected:
/// @brief Return @c true if the input stream contains grid offsets
/// that allow for random access or partial reading.
bool inputHasGridOffsets() const { return mInputHasGridOffsets; }
void setInputHasGridOffsets(bool b) { mInputHasGridOffsets = b; }
/// @brief Tag the given input stream with the input file format version number.
///
/// The tag can be retrieved with getFormatVersion().
/// @sa getFormatVersion()
void setFormatVersion(std::istream&);
/// @brief Tag the given input stream with the version number of
/// the library with which the input stream was created.
///
/// The tag can be retrieved with getLibraryVersion().
/// @sa getLibraryVersion()
void setLibraryVersion(std::istream&);
/// @brief Tag the given input stream with flags indicating whether
/// the input stream contains compressed data and how it is compressed.
void setDataCompression(std::istream&);
/// @brief Tag an output stream with flags specifying only those
/// compression options that are applicable to the given grid.
void setGridCompression(std::ostream&, const GridBase&) const;
/// @brief Read in the compression flags for a grid and
/// tag the given input stream with those flags.
static void readGridCompression(std::istream&);
/// Read in and return the number of grids on the input stream.
static int32_t readGridCount(std::istream&);
/// Populate the given grid from the input stream.
static void readGrid(GridBase::Ptr, const GridDescriptor&, std::istream&);
/// @brief Populate the given grid from the input stream, but only where it
/// intersects the given world-space bounding box.
static void readGrid(GridBase::Ptr, const GridDescriptor&, std::istream&, const BBoxd&);
/// @brief Populate the given grid from the input stream, but only where it
/// intersects the given index-space bounding box.
static void readGrid(GridBase::Ptr, const GridDescriptor&, std::istream&, const CoordBBox&);
using NamedGridMap = std::map<Name /*uniqueName*/, GridBase::Ptr>;
/// @brief If the grid represented by the given grid descriptor
/// is an instance, connect it with its instance parent.
void connectInstance(const GridDescriptor&, const NamedGridMap&) const;
/// Write the given grid descriptor and grid to an output stream
/// and update the GridDescriptor offsets.
/// @param seekable if true, the output stream supports seek operations
void writeGrid(GridDescriptor&, GridBase::ConstPtr, std::ostream&, bool seekable) const;
/// Write the given grid descriptor and grid metadata to an output stream
/// and update the GridDescriptor offsets, but don't write the grid's tree,
/// since it is shared with another grid.
/// @param seekable if true, the output stream supports seek operations
void writeGridInstance(GridDescriptor&, GridBase::ConstPtr,
std::ostream&, bool seekable) const;
/// @brief Read the magic number, version numbers, UUID, etc. from the given input stream.
/// @return @c true if the input UUID differs from the previously-read UUID.
bool readHeader(std::istream&);
/// @brief Write the magic number, version numbers, UUID, etc. to the given output stream.
/// @param seekable if true, the output stream supports seek operations
/// @todo This method should not be const since it actually redefines the UUID!
void writeHeader(std::ostream&, bool seekable) const;
//@{
/// Write the given grids to an output stream.
void write(std::ostream&, const GridPtrVec&, bool seekable, const MetaMap& = MetaMap()) const;
void write(std::ostream&, const GridCPtrVec&, bool seekable, const MetaMap& = MetaMap()) const;
//@}
private:
friend class ::TestFile;
/// The version of the file that was read
uint32_t mFileVersion;
/// The version of the library that was used to create the file that was read
VersionId mLibraryVersion;
/// Unique tag, a random 16-byte (128-bit) value, stored as a string format.
mutable std::string mUuid;// needs to be mutable since writeHeader is const!
/// Flag indicating whether the input stream contains grid offsets
/// and therefore supports partial reading
bool mInputHasGridOffsets;
/// Flag indicating whether a tree shared by multiple grids should be
/// written out only once (true) or once per grid (false)
bool mEnableInstancing;
/// Flags indicating whether and how the data stream is compressed
uint32_t mCompression;
/// Flag indicating whether grid statistics metadata should be written
bool mEnableGridStats;
}; // class Archive
} // namespace io
} // namespace OPENVDB_VERSION_NAME
} // namespace openvdb
#endif // OPENVDB_IO_ARCHIVE_HAS_BEEN_INCLUDED
Compression.h
Grid.h
MetaMap.h
Platform.h
OPENVDB_API
#define OPENVDB_API
Definition Platform.h:274
openvdb::v11_0::GridBase
Abstract base class for typed grids.
Definition Grid.h:78
openvdb::v11_0::GridBase::ConstPtr
SharedPtr< const GridBase > ConstPtr
Definition Grid.h:81
openvdb::v11_0::GridBase::Ptr
SharedPtr< GridBase > Ptr
Definition Grid.h:80
openvdb::v11_0::MetaMap
Container that maps names (strings) to values of arbitrary types.
Definition MetaMap.h:20
openvdb::v11_0::io::Archive::connectInstance
void connectInstance(const GridDescriptor &, const NamedGridMap &) const
If the grid represented by the given grid descriptor is an instance, connect it with its instance par...
openvdb::v11_0::io::Archive::setFormatVersion
void setFormatVersion(std::istream &)
Tag the given input stream with the input file format version number.
openvdb::v11_0::io::Archive::isGridStatsMetadataEnabled
bool isGridStatsMetadataEnabled() const
Return true if grid statistics (active voxel count and bounding box, etc.) are computed and written a...
Definition Archive.h:86
openvdb::v11_0::io::Archive::setLibraryVersion
void setLibraryVersion(std::istream &)
Tag the given input stream with the version number of the library with which the input stream was cre...
openvdb::v11_0::io::Archive::operator=
Archive & operator=(const Archive &)=default
openvdb::v11_0::io::Archive::readGrid
static void readGrid(GridBase::Ptr, const GridDescriptor &, std::istream &)
Populate the given grid from the input stream.
openvdb::v11_0::io::Archive::inputHasGridOffsets
bool inputHasGridOffsets() const
Return true if the input stream contains grid offsets that allow for random access or partial reading...
Definition Archive.h:104
openvdb::v11_0::io::Archive::hasBloscCompression
static bool hasBloscCompression()
Return true if the OpenVDB library includes support for the Blosc compressor.
openvdb::v11_0::io::Archive::DEFAULT_COMPRESSION_FLAGS
static const uint32_t DEFAULT_COMPRESSION_FLAGS
Definition Archive.h:37
openvdb::v11_0::io::Archive::setDataCompression
void setDataCompression(std::istream &)
Tag the given input stream with flags indicating whether the input stream contains compressed data an...
openvdb::v11_0::io::Archive::fileVersion
uint32_t fileVersion() const
Return the file format version number of the input stream.
Definition Archive.h:54
openvdb::v11_0::io::Archive::write
virtual void write(const GridCPtrVec &, const MetaMap &=MetaMap()) const
Write the grids in the given container to this archive's output stream.
Definition Archive.h:92
openvdb::v11_0::io::Archive::readGrid
static void readGrid(GridBase::Ptr, const GridDescriptor &, std::istream &, const BBoxd &)
Populate the given grid from the input stream, but only where it intersects the given world-space bou...
openvdb::v11_0::io::Archive::getUniqueTag
std::string getUniqueTag() const
Return the UUID that was most recently written (or read, if no UUID has been written yet).
openvdb::v11_0::io::Archive::isDelayedLoadingEnabled
static bool isDelayedLoadingEnabled()
Return true if delayed loading is enabled.
openvdb::v11_0::io::Archive::setGridCompression
void setGridCompression(std::ostream &, const GridBase &) const
Tag an output stream with flags specifying only those compression options that are applicable to the ...
openvdb::v11_0::io::Archive::writeHeader
void writeHeader(std::ostream &, bool seekable) const
Write the magic number, version numbers, UUID, etc. to the given output stream.
openvdb::v11_0::io::Archive::readGridCount
static int32_t readGridCount(std::istream &)
Read in and return the number of grids on the input stream.
openvdb::v11_0::io::Archive::write
void write(std::ostream &, const GridCPtrVec &, bool seekable, const MetaMap &=MetaMap()) const
openvdb::v11_0::io::Archive::ConstPtr
SharedPtr< const Archive > ConstPtr
Definition Archive.h:35
openvdb::v11_0::io::Archive::isInstancingEnabled
bool isInstancingEnabled() const
Return true if trees shared by multiple grids are written out only once, false if they are written ou...
Definition Archive.h:64
openvdb::v11_0::io::Archive::readGrid
static void readGrid(GridBase::Ptr, const GridDescriptor &, std::istream &, const CoordBBox &)
Populate the given grid from the input stream, but only where it intersects the given index-space bou...
openvdb::v11_0::io::Archive::setInstancingEnabled
void setInstancingEnabled(bool b)
Specify whether trees shared by multiple grids should be written out only once (true) or once per gri...
Definition Archive.h:68
openvdb::v11_0::io::Archive::readGridCompression
static void readGridCompression(std::istream &)
Read in the compression flags for a grid and tag the given input stream with those flags.
openvdb::v11_0::io::Archive::copy
virtual Ptr copy() const
Return a copy of this archive.
openvdb::v11_0::io::Archive::hasZLibCompression
static bool hasZLibCompression()
Return true if the OpenVDB library includes support for the ZLib compressor.
openvdb::v11_0::io::Archive::setCompression
void setCompression(uint32_t c)
Specify whether and how the data stream should be compressed.
Definition Archive.h:82
openvdb::v11_0::io::Archive::Archive
Archive()
openvdb::v11_0::io::Archive::compression
uint32_t compression() const
Return a bit mask specifying compression options for the data stream.
Definition Archive.h:77
openvdb::v11_0::io::Archive::NamedGridMap
std::map< Name, GridBase::Ptr > NamedGridMap
Definition Archive.h:143
openvdb::v11_0::io::Archive::isIdentical
bool isIdentical(const std::string &uuidStr) const
Return true if the given UUID matches this archive's UUID.
openvdb::v11_0::io::Archive::writeGridInstance
void writeGridInstance(GridDescriptor &, GridBase::ConstPtr, std::ostream &, bool seekable) const
openvdb::v11_0::io::Archive::writeGrid
void writeGrid(GridDescriptor &, GridBase::ConstPtr, std::ostream &, bool seekable) const
openvdb::v11_0::io::Archive::readHeader
bool readHeader(std::istream &)
Read the magic number, version numbers, UUID, etc. from the given input stream.
openvdb::v11_0::io::Archive::Ptr
SharedPtr< Archive > Ptr
Definition Archive.h:34
openvdb::v11_0::io::Archive::Archive
Archive(const Archive &)=default
openvdb::v11_0::io::Archive::setGridStatsMetadataEnabled
void setGridStatsMetadataEnabled(bool b)
Specify whether grid statistics (active voxel count and bounding box, etc.) should be computed and wr...
Definition Archive.h:89
openvdb::v11_0::io::Archive::libraryVersion
VersionId libraryVersion() const
Return the (major, minor) version number of the library that was used to write the input stream.
Definition Archive.h:57
openvdb::v11_0::io::Archive::write
void write(std::ostream &, const GridPtrVec &, bool seekable, const MetaMap &=MetaMap()) const
Write the given grids to an output stream.
openvdb::v11_0::io::Archive::~Archive
virtual ~Archive()
openvdb::v11_0::io::Archive::version
std::string version() const
Return a string of the form "<major>.<minor>/<format>", giving the library and file format version nu...
openvdb::v11_0::io::Archive::setInputHasGridOffsets
void setInputHasGridOffsets(bool b)
Definition Archive.h:105
openvdb::v11_0::io::GridDescriptor
Definition GridDescriptor.h:20
openvdb::v11_0::math::CoordBBox
Axis-aligned bounding box of signed integer coordinates.
Definition Coord.h:251
openvdb::v11_0::Name
std::string Name
Definition Name.h:19
openvdb::v11_0::GridPtrVec
std::vector< GridBase::Ptr > GridPtrVec
Definition Grid.h:508
openvdb::v11_0::BBoxd
math::BBox< Vec3d > BBoxd
Definition Types.h:84
openvdb::v11_0::SharedPtr
std::shared_ptr< T > SharedPtr
Definition Types.h:114
openvdb::v11_0::GridCPtrVec
std::vector< GridBase::ConstPtr > GridCPtrVec
Definition Grid.h:513
openvdb
Definition Exceptions.h:13
openvdb::v11_0::VersionId
Definition version.h.in:273
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition version.h.in:121
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition version.h.in:212