From 2ec07dfa182d4503ccf3930406e8c7b239751e18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastia=CC=81n=20Barschkis?= Date: Tue, 13 Oct 2020 21:36:14 +0200 Subject: Fluid: Update Mantaflow source files Updated files includes: - Fix for smoke / fire emission from particles - Custom precision for liquid particles when saving in OpenVDB format --- extern/mantaflow/preprocessed/fileio/iovdb.cpp | 139 +++++++++++++++-------- extern/mantaflow/preprocessed/fileio/mantaio.cpp | 16 ++- extern/mantaflow/preprocessed/fileio/mantaio.h | 7 +- 3 files changed, 112 insertions(+), 50 deletions(-) (limited to 'extern/mantaflow/preprocessed/fileio') diff --git a/extern/mantaflow/preprocessed/fileio/iovdb.cpp b/extern/mantaflow/preprocessed/fileio/iovdb.cpp index b990274e1c4..287c78f0608 100644 --- a/extern/mantaflow/preprocessed/fileio/iovdb.cpp +++ b/extern/mantaflow/preprocessed/fileio/iovdb.cpp @@ -60,7 +60,7 @@ template void importVDB(typename GridType::Ptr from, Gr template void importVDB(VDBType vdbValue, ParticleDataImpl *to, int index, float voxelSize) { - (void)voxelSize; // Unused + unusedParameter(voxelSize); // Unused for now T toMantaValue; convertFrom(vdbValue, &toMantaValue); to->set(index, toMantaValue); @@ -165,12 +165,12 @@ static void setGridOptions(typename GridType::Ptr grid, string name, openvdb::GridClass cls, float voxelSize, - bool precisionHalf) + int precision) { grid->setTransform(openvdb::math::Transform::createLinearTransform(voxelSize)); grid->setGridClass(cls); grid->setName(name); - grid->setSaveFloatAsHalf(precisionHalf); + grid->setSaveFloatAsHalf(precision == PRECISION_MINI || precision == PRECISION_HALF); } template typename GridType::Ptr exportVDB(Grid *from) @@ -194,7 +194,8 @@ template void exportVDB(ParticleDataImpl *from, openvdb::points::PointDataGrid::Ptr to, openvdb::tools::PointIndexGrid::Ptr pIndex, - bool skipDeletedParts) + bool skipDeletedParts, + int precision) { std::vector vdbValues; std::string name = from->getName(); @@ -212,8 +213,21 @@ void exportVDB(ParticleDataImpl *from, vdbValues.push_back(vdbValue); } - openvdb::NamePair attribute = - openvdb::points::TypedAttributeArray::attributeType(); + // Use custom codec for precision of the attribute + openvdb::NamePair attribute; + if (precision == PRECISION_FULL) { + attribute = + openvdb::points::TypedAttributeArray::attributeType(); + } + else if (precision == PRECISION_HALF || + precision == PRECISION_MINI) { // Mini uses same precision as half for now + attribute = + openvdb::points::TypedAttributeArray::attributeType(); + } + else { + errMsg("exportVDB: invalid precision level"); + } openvdb::points::appendAttribute(to->tree(), name, attribute); // Create a wrapper around the vdb values vector. @@ -229,7 +243,8 @@ void exportVDB(ParticleDataImpl *from, openvdb::points::PointDataGrid::Ptr exportVDB(BasicParticleSystem *from, std::vector &fromPData, bool skipDeletedParts, - float voxelSize) + float voxelSize, + int precision) { std::vector positions; std::vector flags; @@ -257,16 +272,34 @@ openvdb::points::PointDataGrid::Ptr exportVDB(BasicParticleSystem *from, openvdb::tools::createPointIndexGrid(positionsWrapper, *transform); - // TODO (sebbas): Use custom codec for attributes? - // using Codec = openvdb::points::FixedPointCodec; - openvdb::points::PointDataGrid::Ptr to = - openvdb::points::createPointDataGrid( - *pointIndexGrid, positionsWrapper, *transform); + openvdb::points::PointDataGrid::Ptr to; + openvdb::NamePair flagAttribute; + + using CodecNull = openvdb::points::NullCodec; + using CodecTrunc = openvdb::points::TruncateCodec; + using CodecFixPoint = openvdb::points::FixedPointCodec; + + // Use custom codec for precision of the particle position and the flag attribute + if (precision == PRECISION_FULL) { + to = openvdb::points::createPointDataGrid( + *pointIndexGrid, positionsWrapper, *transform); + flagAttribute = openvdb::points::TypedAttributeArray::attributeType(); + } + else if (precision == PRECISION_HALF) { + to = openvdb::points::createPointDataGrid( + *pointIndexGrid, positionsWrapper, *transform); + flagAttribute = openvdb::points::TypedAttributeArray::attributeType(); + } + else if (precision == PRECISION_MINI) { + to = openvdb::points::createPointDataGrid( + *pointIndexGrid, positionsWrapper, *transform); + flagAttribute = openvdb::points::TypedAttributeArray:: + attributeType(); // Use 16 bit trunc for flag for now + } + else { + errMsg("exportVDB: invalid precision level"); + } - openvdb::NamePair flagAttribute = - openvdb::points::TypedAttributeArray::attributeType(); openvdb::points::appendAttribute(to->tree(), FLAG_NAME, flagAttribute); // Create a wrapper around the flag vector. openvdb::points::PointAttributeVector flagWrapper(flags); @@ -281,17 +314,17 @@ openvdb::points::PointDataGrid::Ptr exportVDB(BasicParticleSystem *from, if (pdb->getType() == ParticleDataBase::TypeInt) { debMsg("Writing int particle data '" << pdb->getName() << "'", 1); ParticleDataImpl *pdi = dynamic_cast *>(pdb); - exportVDB(pdi, to, pointIndexGrid, skipDeletedParts); + exportVDB(pdi, to, pointIndexGrid, skipDeletedParts, precision); } else if (pdb->getType() == ParticleDataBase::TypeReal) { debMsg("Writing real particle data '" << pdb->getName() << "'", 1); ParticleDataImpl *pdi = dynamic_cast *>(pdb); - exportVDB(pdi, to, pointIndexGrid, skipDeletedParts); + exportVDB(pdi, to, pointIndexGrid, skipDeletedParts, precision); } else if (pdb->getType() == ParticleDataBase::TypeVec3) { debMsg("Writing Vec3 particle data '" << pdb->getName() << "'", 1); ParticleDataImpl *pdi = dynamic_cast *>(pdb); - exportVDB(pdi, to, pointIndexGrid, skipDeletedParts); + exportVDB(pdi, to, pointIndexGrid, skipDeletedParts, precision); } else { errMsg("exportVDB: unknown ParticleDataBase type"); @@ -302,8 +335,10 @@ openvdb::points::PointDataGrid::Ptr exportVDB(BasicParticleSystem *from, static void registerCustomCodecs() { - using Codec = openvdb::points::FixedPointCodec; - openvdb::points::TypedAttributeArray::registerType(); + openvdb::points::TypedAttributeArray::registerType(); + openvdb::points::TypedAttributeArray::registerType(); + openvdb::points::TypedAttributeArray::registerType(); } int writeObjectsVDB(const string &filename, @@ -311,15 +346,14 @@ int writeObjectsVDB(const string &filename, float worldSize, bool skipDeletedParts, int compression, - bool precisionHalf) + int precision) { openvdb::initialize(); openvdb::io::File file(filename); openvdb::GridPtrVec gridsVDB; - // TODO (sebbas): Use custom codec for flag attribute? - // Register codecs one, this makes sure custom attributes can be read - // registerCustomCodecs(); + // Register custom codecs, this makes sure custom attributes can be read + registerCustomCodecs(); std::vector pdbBuffer; @@ -365,7 +399,7 @@ int writeObjectsVDB(const string &filename, debMsg("Writing particle system '" << mantaPP->getName() << "' (and buffered pData) to vdb file " << filename, 1); - vdbGrid = exportVDB(mantaPP, pdbBuffer, skipDeletedParts, voxelSize); + vdbGrid = exportVDB(mantaPP, pdbBuffer, skipDeletedParts, voxelSize, precision); gridsVDB.push_back(vdbGrid); pdbBuffer.clear(); } @@ -382,7 +416,7 @@ int writeObjectsVDB(const string &filename, // Set additional grid attributes, e.g. name, grid class, compression level, etc. if (vdbGrid) { - setGridOptions(vdbGrid, objectName, gClass, voxelSize, precisionHalf); + setGridOptions(vdbGrid, objectName, gClass, voxelSize, precision); } } @@ -434,19 +468,18 @@ int readObjectsVDB(const string &filename, std::vector *objects, floa openvdb::io::File file(filename); openvdb::GridPtrVec gridsVDB; - // TODO (sebbas): Use custom codec for flag attribute? - // Register codecs one, this makes sure custom attributes can be read - // registerCustomCodecs(); + // Register custom codecs, this makes sure custom attributes can be read + registerCustomCodecs(); try { file.setCopyMaxBytes(0); file.open(); gridsVDB = *(file.getGrids()); openvdb::MetaMap::Ptr metadata = file.getMetadata(); - (void)metadata; // Unused for now + unusedParameter(metadata); // Unused for now } catch (const openvdb::IoError &e) { - (void)e; // Unused for now + unusedParameter(e); // Unused for now debMsg("readObjectsVDB: Could not open vdb file " << filename, 1); file.close(); return 0; @@ -494,27 +527,36 @@ int readObjectsVDB(const string &filename, std::vector *objects, floa if (GridBase *mantaGrid = dynamic_cast(*iter)) { if (mantaGrid->getType() & GridBase::TypeInt) { + openvdb::Int32Grid::Ptr vdbIntGrid = openvdb::gridPtrCast(vdbGrid); + if (!vdbIntGrid) + continue; // Sanity check: Cast can fail if onlyGrid is true but object count > 1 + + Grid *mantaIntGrid = (Grid *)mantaGrid; debMsg("Reading into grid '" << mantaGrid->getName() << "' from int grid '" << vdbGrid->getName() << "' in vdb file " << filename, 1); - openvdb::Int32Grid::Ptr vdbIntGrid = openvdb::gridPtrCast(vdbGrid); - Grid *mantaIntGrid = (Grid *)mantaGrid; importVDB(vdbIntGrid, mantaIntGrid); } else if (mantaGrid->getType() & GridBase::TypeReal) { + openvdb::FloatGrid::Ptr vdbFloatGrid = openvdb::gridPtrCast(vdbGrid); + if (!vdbFloatGrid) + continue; // Sanity check: Cast can fail if onlyGrid is true but object count > 1 + + Grid *mantaRealGrid = (Grid *)mantaGrid; debMsg("Reading into grid '" << mantaGrid->getName() << "' from real grid '" << vdbGrid->getName() << "' in vdb file " << filename, 1); - openvdb::FloatGrid::Ptr vdbFloatGrid = openvdb::gridPtrCast(vdbGrid); - Grid *mantaRealGrid = (Grid *)mantaGrid; importVDB(vdbFloatGrid, mantaRealGrid); } else if (mantaGrid->getType() & GridBase::TypeVec3) { + openvdb::Vec3SGrid::Ptr vdbVec3Grid = openvdb::gridPtrCast(vdbGrid); + if (!vdbVec3Grid) + continue; // Sanity check: Cast can fail if onlyGrid is true but object count > 1 + + Grid *mantaVec3Grid = (Grid *)mantaGrid; debMsg("Reading into grid '" << mantaGrid->getName() << "' from vec3 grid '" << vdbGrid->getName() << "' in vdb file " << filename, 1); - openvdb::Vec3SGrid::Ptr vdbVec3Grid = openvdb::gridPtrCast(vdbGrid); - Grid *mantaVec3Grid = (Grid *)mantaGrid; importVDB(vdbVec3Grid, mantaVec3Grid); } else { @@ -523,12 +565,15 @@ int readObjectsVDB(const string &filename, std::vector *objects, floa } } else if (BasicParticleSystem *mantaPP = dynamic_cast(*iter)) { + openvdb::points::PointDataGrid::Ptr vdbPointGrid = + openvdb::gridPtrCast(vdbGrid); + if (!vdbPointGrid) + continue; // Sanity check: Cast can fail if onlyGrid is true but objects > 1 + debMsg("Reading into particle system '" << mantaPP->getName() << "' from particle system '" << vdbGrid->getName() << "' in vdb file " << filename, 1); - openvdb::points::PointDataGrid::Ptr vdbPointGrid = - openvdb::gridPtrCast(vdbGrid); importVDB(vdbPointGrid, mantaPP, pdbBuffer, voxelSize); pdbBuffer.clear(); } @@ -582,19 +627,23 @@ template openvdb::Vec3SGrid::Ptr exportVDB(Grid openvdb::points::PointDataGrid::Ptr exportVDB(BasicParticleSystem *from, std::vector &fromPData, bool skipDeletedParts = false, - float voxelSize = 1.0); + float voxelSize = 1.0, + int precision = PRECISION_HALF); template void exportVDB(ParticleDataImpl *from, openvdb::points::PointDataGrid::Ptr to, openvdb::tools::PointIndexGrid::Ptr pIndex, - bool skipDeletedParts = false); + bool skipDeletedParts = false, + int precision = PRECISION_HALF); template void exportVDB(ParticleDataImpl *from, openvdb::points::PointDataGrid::Ptr to, openvdb::tools::PointIndexGrid::Ptr pIndex, - bool skipDeletedParts = false); + bool skipDeletedParts = false, + int precision = PRECISION_HALF); template void exportVDB(ParticleDataImpl *from, openvdb::points::PointDataGrid::Ptr to, openvdb::tools::PointIndexGrid::Ptr pIndex, - bool skipDeletedParts = false); + bool skipDeletedParts = false, + int precision = PRECISION_HALF); #else @@ -603,7 +652,7 @@ int writeObjectsVDB(const string &filename, float worldSize, bool skipDeletedParts, int compression, - bool precisionHalf) + int precision) { errMsg("Cannot save to .vdb file. Mantaflow has not been built with OpenVDB support."); return 0; diff --git a/extern/mantaflow/preprocessed/fileio/mantaio.cpp b/extern/mantaflow/preprocessed/fileio/mantaio.cpp index fd2b36bf7cb..a03bd98d449 100644 --- a/extern/mantaflow/preprocessed/fileio/mantaio.cpp +++ b/extern/mantaflow/preprocessed/fileio/mantaio.cpp @@ -82,8 +82,15 @@ int save(const string &name, float worldSize = 1.0, bool skipDeletedParts = false, int compression = COMPRESSION_ZIP, - bool precisionHalf = true) + bool precisionHalf = true, + int precision = PRECISION_HALF) { + + if (!precisionHalf) { + debMsg("Warning: precisionHalf argument is deprecated. Please use precision level instead", 0); + precision = PRECISION_HALF; // for backwards compatibility + } + if (name.find_last_of('.') == string::npos) errMsg("file '" + name + "' does not have an extension"); string ext = name.substr(name.find_last_of('.')); @@ -95,8 +102,7 @@ int save(const string &name, else if (ext == ".vol") return writeGridsVol(name, &objects); if (ext == ".vdb") - return writeObjectsVDB( - name, &objects, worldSize, skipDeletedParts, compression, precisionHalf); + return writeObjectsVDB(name, &objects, worldSize, skipDeletedParts, compression, precision); else if (ext == ".npz") return writeGridsNumpy(name, &objects); else if (ext == ".txt") @@ -122,7 +128,9 @@ static PyObject *_W_1(PyObject *_self, PyObject *_linargs, PyObject *_kwds) bool skipDeletedParts = _args.getOpt("skipDeletedParts", 3, false, &_lock); int compression = _args.getOpt("compression", 4, COMPRESSION_ZIP, &_lock); bool precisionHalf = _args.getOpt("precisionHalf", 5, true, &_lock); - _retval = toPy(save(name, objects, worldSize, skipDeletedParts, compression, precisionHalf)); + int precision = _args.getOpt("precision", 6, PRECISION_HALF, &_lock); + _retval = toPy( + save(name, objects, worldSize, skipDeletedParts, compression, precisionHalf, precision)); _args.check(); } pbFinalizePlugin(parent, "save", !noTiming); diff --git a/extern/mantaflow/preprocessed/fileio/mantaio.h b/extern/mantaflow/preprocessed/fileio/mantaio.h index 8b543ad4f93..d3c7bb3ba6d 100644 --- a/extern/mantaflow/preprocessed/fileio/mantaio.h +++ b/extern/mantaflow/preprocessed/fileio/mantaio.h @@ -28,6 +28,11 @@ #define COMPRESSION_ZIP 1 #define COMPRESSION_BLOSC 2 +// OpenVDB precision flags +#define PRECISION_FULL 0 +#define PRECISION_HALF 1 +#define PRECISION_MINI 2 + namespace Manta { // Forward declations @@ -70,7 +75,7 @@ int writeObjectsVDB(const std::string &filename, float scale = 1.0, bool skipDeletedParts = false, int compression = COMPRESSION_ZIP, - bool precisionHalf = true); + int precision = PRECISION_HALF); int readObjectsVDB(const std::string &filename, std::vector *objects, float scale = 1.0); -- cgit v1.2.3