[Bf-blender-cvs] [9fe64948abe] master: Fluid: Updated Mantaflow source with latest OpenVDB changes

Sebastián Barschkis noreply at git.blender.org
Wed Jun 24 16:27:40 CEST 2020


Commit: 9fe64948abe991d18c1af3a52d81e87c24d39687
Author: Sebastián Barschkis
Date:   Wed Jun 24 12:00:13 2020 +0200
Branches: master
https://developer.blender.org/rB9fe64948abe991d18c1af3a52d81e87c24d39687

Fluid: Updated Mantaflow source with latest OpenVDB changes

This updated set of Mantaflow files includes the improved  OpenVDB file IO. With this update it is finally possible to store multiple grids per file. It is also possible to save particle systems and particle data to OpenVDB files.

===================================================================

M	extern/mantaflow/CMakeLists.txt
M	extern/mantaflow/helper/pwrapper/pconvert.cpp
M	extern/mantaflow/helper/pwrapper/pconvert.h
M	extern/mantaflow/preprocessed/fileio/iogrids.cpp
M	extern/mantaflow/preprocessed/fileio/iomeshes.cpp
M	extern/mantaflow/preprocessed/fileio/ioparticles.cpp
M	extern/mantaflow/preprocessed/fileio/ioutil.cpp
A	extern/mantaflow/preprocessed/fileio/iovdb.cpp
A	extern/mantaflow/preprocessed/fileio/mantaio.cpp
M	extern/mantaflow/preprocessed/fileio/mantaio.h
M	extern/mantaflow/preprocessed/gitinfo.h
M	extern/mantaflow/preprocessed/grid.cpp
M	extern/mantaflow/preprocessed/grid.h
M	extern/mantaflow/preprocessed/grid4d.cpp
M	extern/mantaflow/preprocessed/grid4d.h
M	extern/mantaflow/preprocessed/particle.cpp
M	extern/mantaflow/preprocessed/particle.h
M	extern/mantaflow/preprocessed/python/defines.py.reg.cpp
M	extern/mantaflow/preprocessed/registration.cpp

===================================================================

diff --git a/extern/mantaflow/CMakeLists.txt b/extern/mantaflow/CMakeLists.txt
index bdee06349d2..8b7453bf4c5 100644
--- a/extern/mantaflow/CMakeLists.txt
+++ b/extern/mantaflow/CMakeLists.txt
@@ -54,6 +54,10 @@ if(WITH_OPENVDB)
   add_definitions(-DOPENVDB_STATICLIB)
 endif()
 
+if(WITH_OPENVDB_BLOSC)
+  add_definitions(-DOPENVDB_BLOSC=1)
+endif()
+
 if(WIN32)
   add_definitions(-D_USE_MATH_DEFINES)
 endif()
@@ -106,10 +110,12 @@ set(SRC
   ${MANTA_PP}/fastmarch.cpp
   ${MANTA_PP}/fastmarch.h
   ${MANTA_PP}/fastmarch.h.reg.cpp
-  ${MANTA_PP}/fileio/ioutil.cpp
   ${MANTA_PP}/fileio/iogrids.cpp
   ${MANTA_PP}/fileio/iomeshes.cpp
   ${MANTA_PP}/fileio/ioparticles.cpp
+  ${MANTA_PP}/fileio/ioutil.cpp
+  ${MANTA_PP}/fileio/iovdb.cpp
+  ${MANTA_PP}/fileio/mantaio.cpp
   ${MANTA_PP}/fileio/mantaio.h
   ${MANTA_PP}/fileio/mantaio.h.reg.cpp
   ${MANTA_PP}/fluidsolver.cpp
diff --git a/extern/mantaflow/helper/pwrapper/pconvert.cpp b/extern/mantaflow/helper/pwrapper/pconvert.cpp
index 9ada75519fc..861a2c070bd 100644
--- a/extern/mantaflow/helper/pwrapper/pconvert.cpp
+++ b/extern/mantaflow/helper/pwrapper/pconvert.cpp
@@ -96,6 +96,37 @@ template<> PyObject *toPy<PbClass *>(const PbClass_Ptr &obj)
 {
   return obj->getPyObject();
 }
+template<> PyObject *toPy<std::vector<PbClass *>>(const std::vector<PbClass *> &vec)
+{
+  PyObject *listObj = PyList_New(vec.size());
+  if (!listObj)
+    throw logic_error("Unable to allocate memory for Python list");
+  for (unsigned int i = 0; i < vec.size(); i++) {
+    PbClass *pb = vec[i];
+    PyObject *item = pb->getPyObject();
+    if (!item) {
+      Py_DECREF(listObj);
+      throw logic_error("Unable to allocate memory for Python list");
+    }
+    PyList_SET_ITEM(listObj, i, item);
+  }
+  return listObj;
+}
+template<> PyObject *toPy<std::vector<float>>(const std::vector<float> &vec)
+{
+  PyObject *listObj = PyList_New(vec.size());
+  if (!listObj)
+    throw logic_error("Unable to allocate memory for Python list");
+  for (unsigned int i = 0; i < vec.size(); i++) {
+    PyObject *item = toPy<float>(vec[i]);
+    if (!item) {
+      Py_DECREF(listObj);
+      throw logic_error("Unable to allocate memory for Python list");
+    }
+    PyList_SET_ITEM(listObj, i, item);
+  }
+  return listObj;
+}
 
 template<> float fromPy<float>(PyObject *obj)
 {
@@ -125,6 +156,42 @@ template<> PyObject *fromPy<PyObject *>(PyObject *obj)
 {
   return obj;
 }
+template<> PbClass *fromPy<PbClass *>(PyObject *obj)
+{
+  PbClass *pbo = Pb::objFromPy(obj);
+
+  if (!PyType_Check(obj))
+    return pbo;
+
+  const char *tname = ((PyTypeObject *)obj)->tp_name;
+  pbo->setName(tname);
+
+  return pbo;
+}
+template<> std::vector<PbClass *> fromPy<std::vector<PbClass *>>(PyObject *obj)
+{
+  std::vector<PbClass *> vec;
+  if (PyList_Check(obj)) {
+    int sz = PyList_Size(obj);
+    for (int i = 0; i < sz; ++i) {
+      PyObject *lobj = PyList_GetItem(obj, i);
+      vec.push_back(fromPy<PbClass *>(lobj));
+    }
+  }
+  return vec;
+}
+template<> std::vector<float> fromPy<std::vector<float>>(PyObject *obj)
+{
+  std::vector<float> vec;
+  if (PyList_Check(obj)) {
+    int sz = PyList_Size(obj);
+    for (int i = 0; i < sz; ++i) {
+      PyObject *lobj = PyList_GetItem(obj, i);
+      vec.push_back(fromPy<float>(lobj));
+    }
+  }
+  return vec;
+}
 template<> int fromPy<int>(PyObject *obj)
 {
 #if PY_MAJOR_VERSION <= 2
@@ -259,11 +326,10 @@ template<class T> T *tmpAlloc(PyObject *obj, std::vector<void *> *tmp)
 {
   if (!tmp)
     throw Error("dynamic de-ref not supported for this type");
-  void *ptr = malloc(sizeof(T));
-  tmp->push_back(ptr);
 
-  *((T *)ptr) = fromPy<T>(obj);
-  return (T *)ptr;
+  T *ptr = new T(fromPy<T>(obj));
+  tmp->push_back(ptr);
+  return ptr;
 }
 template<> float *fromPyPtr<float>(PyObject *obj, std::vector<void *> *tmp)
 {
@@ -301,6 +367,11 @@ template<> Vec4i *fromPyPtr<Vec4i>(PyObject *obj, std::vector<void *> *tmp)
 {
   return tmpAlloc<Vec4i>(obj, tmp);
 }
+template<>
+std::vector<PbClass *> *fromPyPtr<std::vector<PbClass *>>(PyObject *obj, std::vector<void *> *tmp)
+{
+  return tmpAlloc<std::vector<PbClass *>>(obj, tmp);
+}
 
 template<> bool isPy<float>(PyObject *obj)
 {
@@ -404,6 +475,18 @@ template<> bool isPy<PbType>(PyObject *obj)
 {
   return PyType_Check(obj);
 }
+template<> bool isPy<std::vector<PbClass *>>(PyObject *obj)
+{
+  if (PyList_Check(obj))
+    return true;
+  return false;
+}
+template<> bool isPy<std::vector<float>>(PyObject *obj)
+{
+  if (PyList_Check(obj))
+    return true;
+  return false;
+}
 
 //******************************************************************************
 // PbArgs class defs
@@ -417,7 +500,7 @@ PbArgs::PbArgs(PyObject *linarg, PyObject *dict) : mLinArgs(0), mKwds(0)
 PbArgs::~PbArgs()
 {
   for (int i = 0; i < (int)mTmpStorage.size(); i++)
-    free(mTmpStorage[i]);
+    operator delete(mTmpStorage[i]);
   mTmpStorage.clear();
 }
 
diff --git a/extern/mantaflow/helper/pwrapper/pconvert.h b/extern/mantaflow/helper/pwrapper/pconvert.h
index 9c72b8b57b9..87f4248f6f1 100644
--- a/extern/mantaflow/helper/pwrapper/pconvert.h
+++ b/extern/mantaflow/helper/pwrapper/pconvert.h
@@ -57,6 +57,10 @@ template<> Vec3 *fromPyPtr<Vec3>(PyObject *obj, std::vector<void *> *tmp);
 template<> Vec3i *fromPyPtr<Vec3i>(PyObject *obj, std::vector<void *> *tmp);
 template<> Vec4 *fromPyPtr<Vec4>(PyObject *obj, std::vector<void *> *tmp);
 template<> Vec4i *fromPyPtr<Vec4i>(PyObject *obj, std::vector<void *> *tmp);
+template<>
+std::vector<PbClass *> *fromPyPtr<std::vector<PbClass *>>(PyObject *obj, std::vector<void *> *tmp);
+template<>
+std::vector<float> *fromPyPtr<std::vector<float>>(PyObject *obj, std::vector<void *> *tmp);
 
 PyObject *incref(PyObject *obj);
 template<class T> PyObject *toPy(const T &v)
@@ -99,6 +103,9 @@ template<> Vec4 fromPy<Vec4>(PyObject *obj);
 template<> Vec4i fromPy<Vec4i>(PyObject *obj);
 template<> PbType fromPy<PbType>(PyObject *obj);
 template<> PbTypeVec fromPy<PbTypeVec>(PyObject *obj);
+template<> PbClass *fromPy<PbClass *>(PyObject *obj);
+template<> std::vector<PbClass *> fromPy<std::vector<PbClass *>>(PyObject *obj);
+template<> std::vector<float> fromPy<std::vector<float>>(PyObject *obj);
 
 template<> PyObject *toPy<int>(const int &v);
 template<> PyObject *toPy<std::string>(const std::string &val);
@@ -111,6 +118,8 @@ template<> PyObject *toPy<Vec4i>(const Vec4i &v);
 template<> PyObject *toPy<Vec4>(const Vec4 &v);
 typedef PbClass *PbClass_Ptr;
 template<> PyObject *toPy<PbClass *>(const PbClass_Ptr &obj);
+template<> PyObject *toPy<std::vector<PbClass *>>(const std::vector<PbClass *> &vec);
+template<> PyObject *toPy<std::vector<float>>(const std::vector<float> &vec);
 
 template<> bool isPy<float>(PyObject *obj);
 template<> bool isPy<double>(PyObject *obj);
@@ -124,6 +133,8 @@ template<> bool isPy<Vec3i>(PyObject *obj);
 template<> bool isPy<Vec4>(PyObject *obj);
 template<> bool isPy<Vec4i>(PyObject *obj);
 template<> bool isPy<PbType>(PyObject *obj);
+template<> bool isPy<std::vector<PbClass *>>(PyObject *obj);
+template<> bool isPy<std::vector<float>>(PyObject *obj);
 
 //! Encapsulation of python arguments
 class PbArgs {
diff --git a/extern/mantaflow/preprocessed/fileio/iogrids.cpp b/extern/mantaflow/preprocessed/fileio/iogrids.cpp
index acd1bda5174..e2550d6db8b 100644
--- a/extern/mantaflow/preprocessed/fileio/iogrids.cpp
+++ b/extern/mantaflow/preprocessed/fileio/iogrids.cpp
@@ -27,10 +27,6 @@ extern "C" {
 }
 #endif
 
-#if OPENVDB == 1
-#  include "openvdb/openvdb.h"
-#endif
-
 #include "cnpy.h"
 #include "mantaio.h"
 #include "grid.h"
@@ -279,54 +275,87 @@ static int unifyGridType(int type)
 // grid data
 //*****************************************************************************
 
-template<class T> void writeGridTxt(const string &name, Grid<T> *grid)
+template<class T> int writeGridTxt(const string &name, Grid<T> *grid)
 {
   debMsg("writing grid " << grid->getName() << " to text file " << name, 1);
 
   ofstream ofs(name.c_str());
   if (!ofs.good())
     errMsg("writeGridTxt: can't open file " << name);
+  return 0;
   FOR_IJK(*grid)
   {
     ofs << Vec3i(i, j, k) << " = " << (*grid)(i, j, k) << "\n";
   }
   ofs.close();
+  return 1;
+}
+
+int writeGridsTxt(const string &name, std::vector<PbClass *> *grids)
+{
+  errMsg("writeGridsTxt: writing multiple grids to one .txt file not supported yet");
+  return 0;
 }
 
-template<class T> void writeGridRaw(const string &name, Grid<T> *grid)
+int readGridsTxt(const string &name, std::vector<PbClass *> *grids)
+{
+  errMsg("readGridsTxt: writing multiple grids from one .txt file not supported yet");
+  return 0;
+}
+
+template<class T> int writeGridRaw(const string &name, Grid<T> *grid)
 {
   debMsg("writing grid " << grid->getName() << " to raw file " << name, 1);
 
 #if NO_ZLIB != 1
   gzFile gzf = (gzFile)safeGzopen(name.c_str(), "wb1");  // do some compression
-  if (!gzf)
+  if (!gzf) {
     errMsg("writeGridRaw: can't open file " << name);
+    return 0;
+  }
+
   gzwrite(gzf, &((*grid)[0]), sizeof(T) * grid->getSizeX() * grid->getSizeY() * grid->getSizeZ());
-  gzclose(gzf);
+  return (gzclose(gzf) == Z_OK);
 #else
   debMsg("file format not supported without zlib", 1);
+  return 0;
 #endif
 }
 
-template<class T> void readGridRaw(const string &name, Grid<T> *grid)
+template<class T> int readGridRaw(const string &name, Grid<T> *grid)
 {
   debMsg("reading grid " << grid->getName() << " from raw file " << name, 1);
 
 #if NO_ZLIB != 1
   gzFile gzf = (gzFile)safeGzopen(name.c_str(), "rb");
-  if (!gzf)
+  if (!gzf) {
     errMsg("readGridRaw: can't open file " << name);
+    return 0;
+  }
 
   IndexInt bytes = sizeof(T) * grid->getSizeX() * grid->getSizeY() * grid->getSizeZ();
   IndexInt readBytes = gzread(gzf, &((*grid)[0]), bytes);
   assertMsg(bytes == readBytes,
             "can't read raw file, stream length does not match, " << bytes << " vs " << readBytes);
-  gzclose(gzf);
+  return (gzclose(gzf) == Z_OK);
 #else
   debMsg("file format not supported without zlib", 1);
+  return 0;
 #endif
 }
 
+int writeGridsRaw(const string &name, std::vector<PbClass *> *grids)
+{
+  errMsg("writeGridsRaw: writing multiple grids to one .raw file not supported yet");
+  retu

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list