diff options
Diffstat (limited to 'intern/openvdb')
-rw-r--r-- | intern/openvdb/CMakeLists.txt | 69 | ||||
-rw-r--r-- | intern/openvdb/intern/openvdb_dense_convert.cc | 167 | ||||
-rw-r--r-- | intern/openvdb/intern/openvdb_dense_convert.h | 130 | ||||
-rw-r--r-- | intern/openvdb/intern/openvdb_reader.cc | 136 | ||||
-rw-r--r-- | intern/openvdb/intern/openvdb_reader.h | 55 | ||||
-rw-r--r-- | intern/openvdb/intern/openvdb_writer.cc | 118 | ||||
-rw-r--r-- | intern/openvdb/intern/openvdb_writer.h | 57 | ||||
-rw-r--r-- | intern/openvdb/openvdb_capi.cc | 240 | ||||
-rw-r--r-- | intern/openvdb/openvdb_capi.h | 108 | ||||
-rw-r--r-- | intern/openvdb/openvdb_util.cc | 38 | ||||
-rw-r--r-- | intern/openvdb/openvdb_util.h | 57 |
11 files changed, 1175 insertions, 0 deletions
diff --git a/intern/openvdb/CMakeLists.txt b/intern/openvdb/CMakeLists.txt new file mode 100644 index 00000000000..ce683e7d319 --- /dev/null +++ b/intern/openvdb/CMakeLists.txt @@ -0,0 +1,69 @@ +# ***** BEGIN GPL LICENSE BLOCK ***** +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# The Original Code is Copyright (C) 2015, Blender Foundation +# All rights reserved. +# +# The Original Code is: all of this file. +# +# Contributor(s): Kevin Dietrich. +# +# ***** END GPL LICENSE BLOCK ***** + +set(INC + . + intern +) + +set(INC_SYS +) + +set(SRC + openvdb_capi.h +) + +if(WITH_OPENVDB) + add_definitions( + -DWITH_OPENVDB + ) + + list(APPEND INC_SYS + ${BOOST_INCLUDE_DIR} + ${OPENEXR_INCLUDE_DIRS} + ${OPENVDB_INCLUDE_DIRS} + ) + + list(APPEND SRC + intern/openvdb_dense_convert.cc + intern/openvdb_reader.cc + intern/openvdb_writer.cc + openvdb_capi.cc + openvdb_util.cc + + intern/openvdb_dense_convert.h + intern/openvdb_reader.h + intern/openvdb_writer.h + openvdb_util.h + ) + + if(WITH_OPENVDB_BLOSC) + add_definitions( + -DWITH_OPENVDB_BLOSC + ) + endif() +endif() + +blender_add_lib(bf_intern_openvdb "${SRC}" "${INC}" "${INC_SYS}") diff --git a/intern/openvdb/intern/openvdb_dense_convert.cc b/intern/openvdb/intern/openvdb_dense_convert.cc new file mode 100644 index 00000000000..d4f62776988 --- /dev/null +++ b/intern/openvdb/intern/openvdb_dense_convert.cc @@ -0,0 +1,167 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2015 Blender Foundation. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): Kevin Dietrich + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#include "openvdb_dense_convert.h" + +#include <openvdb/tools/ValueTransformer.h> /* for tools::foreach */ + +namespace internal { + +openvdb::Mat4R convertMatrix(const float mat[4][4]) +{ + return openvdb::Mat4R( + mat[0][0], mat[0][1], mat[0][2], mat[0][3], + mat[1][0], mat[1][1], mat[1][2], mat[1][3], + mat[2][0], mat[2][1], mat[2][2], mat[2][3], + mat[3][0], mat[3][1], mat[3][2], mat[3][3]); +} + + +class MergeScalarGrids { + typedef openvdb::FloatTree ScalarTree; + + openvdb::tree::ValueAccessor<const ScalarTree> m_acc_x, m_acc_y, m_acc_z; + +public: + MergeScalarGrids(const ScalarTree *x_tree, const ScalarTree *y_tree, const ScalarTree *z_tree) + : m_acc_x(*x_tree) + , m_acc_y(*y_tree) + , m_acc_z(*z_tree) + {} + + MergeScalarGrids(const MergeScalarGrids &other) + : m_acc_x(other.m_acc_x) + , m_acc_y(other.m_acc_y) + , m_acc_z(other.m_acc_z) + {} + + void operator()(const openvdb::Vec3STree::ValueOnIter &it) const + { + using namespace openvdb; + + const math::Coord xyz = it.getCoord(); + float x = m_acc_x.getValue(xyz); + float y = m_acc_y.getValue(xyz); + float z = m_acc_z.getValue(xyz); + + it.setValue(math::Vec3s(x, y, z)); + } +}; + +openvdb::GridBase *OpenVDB_export_vector_grid( + OpenVDBWriter *writer, + const openvdb::Name &name, + const float *data_x, const float *data_y, const float *data_z, + const int res[3], + float fluid_mat[4][4], + openvdb::VecType vec_type, + const bool is_color, + const openvdb::FloatGrid *mask) +{ + using namespace openvdb; + + math::CoordBBox bbox(Coord(0), Coord(res[0] - 1, res[1] - 1, res[2] - 1)); + Mat4R mat = convertMatrix(fluid_mat); + math::Transform::Ptr transform = math::Transform::createLinearTransform(mat); + + FloatGrid::Ptr grid[3]; + + grid[0] = FloatGrid::create(0.0f); + tools::Dense<const float, tools::LayoutXYZ> dense_grid_x(bbox, data_x); + tools::copyFromDense(dense_grid_x, grid[0]->tree(), TOLERANCE); + + grid[1] = FloatGrid::create(0.0f); + tools::Dense<const float, tools::LayoutXYZ> dense_grid_y(bbox, data_y); + tools::copyFromDense(dense_grid_y, grid[1]->tree(), TOLERANCE); + + grid[2] = FloatGrid::create(0.0f); + tools::Dense<const float, tools::LayoutXYZ> dense_grid_z(bbox, data_z); + tools::copyFromDense(dense_grid_z, grid[2]->tree(), TOLERANCE); + + Vec3SGrid::Ptr vecgrid = Vec3SGrid::create(Vec3s(0.0f)); + + /* Activate voxels in the vector grid based on the scalar grids to ensure + * thread safety later on */ + for (int i = 0; i < 3; ++i) { + vecgrid->tree().topologyUnion(grid[i]->tree()); + } + + MergeScalarGrids op(&(grid[0]->tree()), &(grid[1]->tree()), &(grid[2]->tree())); + tools::foreach(vecgrid->beginValueOn(), op, true, false); + + vecgrid->setTransform(transform); + + if (mask) { + vecgrid = tools::clip(*vecgrid, *mask); + } + + vecgrid->setName(name); + vecgrid->setIsInWorldSpace(false); + vecgrid->setVectorType(vec_type); + vecgrid->insertMeta("is_color", BoolMetadata(is_color)); + vecgrid->setGridClass(GRID_STAGGERED); + + writer->insert(vecgrid); + + return vecgrid.get(); +} + +void OpenVDB_import_grid_vector( + OpenVDBReader *reader, + const openvdb::Name &name, + float **data_x, float **data_y, float **data_z, + const int res[3]) +{ + using namespace openvdb; + + if (!reader->hasGrid(name)) { + std::fprintf(stderr, "OpenVDB grid %s not found in file!\n", name.c_str()); + memset(*data_x, 0, sizeof(float) * res[0] * res[1] * res[2]); + memset(*data_y, 0, sizeof(float) * res[0] * res[1] * res[2]); + memset(*data_z, 0, sizeof(float) * res[0] * res[1] * res[2]); + return; + } + + Vec3SGrid::Ptr vgrid = gridPtrCast<Vec3SGrid>(reader->getGrid(name)); + Vec3SGrid::ConstAccessor acc = vgrid->getConstAccessor(); + math::Coord xyz; + int &x = xyz[0], &y = xyz[1], &z = xyz[2]; + + size_t index = 0; + for (z = 0; z < res[2]; ++z) { + for (y = 0; y < res[1]; ++y) { + for (x = 0; x < res[0]; ++x, ++index) { + math::Vec3s value = acc.getValue(xyz); + (*data_x)[index] = value.x(); + (*data_y)[index] = value.y(); + (*data_z)[index] = value.z(); + } + } + } +} + +} /* namespace internal */ diff --git a/intern/openvdb/intern/openvdb_dense_convert.h b/intern/openvdb/intern/openvdb_dense_convert.h new file mode 100644 index 00000000000..fd10334c4ad --- /dev/null +++ b/intern/openvdb/intern/openvdb_dense_convert.h @@ -0,0 +1,130 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2015 Blender Foundation. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): Kevin Dietrich + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#ifndef __OPENVDB_DENSE_CONVERT_H__ +#define __OPENVDB_DENSE_CONVERT_H__ + +#include "openvdb_reader.h" +#include "openvdb_writer.h" + +#include <openvdb/tools/Clip.h> +#include <openvdb/tools/Dense.h> + +#include <cstdio> + +#define TOLERANCE 1e-3f + +namespace internal { + +openvdb::Mat4R convertMatrix(const float mat[4][4]); + +template <typename GridType, typename T> +GridType *OpenVDB_export_grid( + OpenVDBWriter *writer, + const openvdb::Name &name, + const T *data, + const int res[3], + float fluid_mat[4][4], + const openvdb::FloatGrid *mask) +{ + using namespace openvdb; + + math::CoordBBox bbox(Coord(0), Coord(res[0] - 1, res[1] - 1, res[2] - 1)); + Mat4R mat = convertMatrix(fluid_mat); + math::Transform::Ptr transform = math::Transform::createLinearTransform(mat); + + typename GridType::Ptr grid = GridType::create(T(0)); + + tools::Dense<const T, openvdb::tools::LayoutXYZ> dense_grid(bbox, data); + tools::copyFromDense(dense_grid, grid->tree(), (T)TOLERANCE); + + grid->setTransform(transform); + + if (mask) { + grid = tools::clip(*grid, *mask); + } + + grid->setName(name); + grid->setIsInWorldSpace(false); + grid->setVectorType(openvdb::VEC_INVARIANT); + + writer->insert(grid); + + return grid.get(); +} + +template <typename GridType, typename T> +void OpenVDB_import_grid( + OpenVDBReader *reader, + const openvdb::Name &name, + T **data, + const int res[3]) +{ + using namespace openvdb; + + if (!reader->hasGrid(name)) { + std::fprintf(stderr, "OpenVDB grid %s not found in file!\n", name.c_str()); + memset(*data, 0, sizeof(T) * res[0] * res[1] * res[2]); + return; + } + + typename GridType::Ptr grid = gridPtrCast<GridType>(reader->getGrid(name)); + typename GridType::ConstAccessor acc = grid->getConstAccessor(); + + math::Coord xyz; + int &x = xyz[0], &y = xyz[1], &z = xyz[2]; + + size_t index = 0; + for (z = 0; z < res[2]; ++z) { + for (y = 0; y < res[1]; ++y) { + for (x = 0; x < res[0]; ++x, ++index) { + (*data)[index] = acc.getValue(xyz); + } + } + } +} + +openvdb::GridBase *OpenVDB_export_vector_grid( + OpenVDBWriter *writer, + const openvdb::Name &name, + const float *data_x, const float *data_y, const float *data_z, + const int res[3], + float fluid_mat[4][4], + openvdb::VecType vec_type, + const bool is_color, + const openvdb::FloatGrid *mask); + + +void OpenVDB_import_grid_vector( + OpenVDBReader *reader, + const openvdb::Name &name, + float **data_x, float **data_y, float **data_z, + const int res[3]); + +} /* namespace internal */ + +#endif /* __OPENVDB_DENSE_CONVERT_H__ */ diff --git a/intern/openvdb/intern/openvdb_reader.cc b/intern/openvdb/intern/openvdb_reader.cc new file mode 100644 index 00000000000..8b15b81710d --- /dev/null +++ b/intern/openvdb/intern/openvdb_reader.cc @@ -0,0 +1,136 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2015 Blender Foundation. + * All rights reserved. + * + * Contributor(s): Kevin Dietrich + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#include "openvdb_reader.h" +#include "openvdb_util.h" + +OpenVDBReader::OpenVDBReader() + : m_meta_map(new openvdb::MetaMap) + , m_file(NULL) +{ + /* Although it is safe, it may not be good to have this here, could be done + * once instead of everytime we read a file. */ + openvdb::initialize(); +} + +OpenVDBReader::~OpenVDBReader() +{ + cleanupFile(); +} + +void OpenVDBReader::open(const openvdb::Name &filename) +{ + cleanupFile(); + + try { + m_file = new openvdb::io::File(filename); + m_file->setCopyMaxBytes(0); + m_file->open(); + + m_meta_map = m_file->getMetadata(); + } + /* Mostly to catch exceptions related to Blosc not being supported. */ + catch (const openvdb::IoError &e) { + std::cerr << e.what() << '\n'; + cleanupFile(); + } +} + +void OpenVDBReader::floatMeta(const openvdb::Name &name, float &value) const +{ + try { + value = m_meta_map->metaValue<float>(name); + } + CATCH_KEYERROR; +} + +void OpenVDBReader::intMeta(const openvdb::Name &name, int &value) const +{ + try { + value = m_meta_map->metaValue<int>(name); + } + CATCH_KEYERROR; +} + +void OpenVDBReader::vec3sMeta(const openvdb::Name &name, float value[3]) const +{ + try { + openvdb::Vec3s meta_val = m_meta_map->metaValue<openvdb::Vec3s>(name); + + value[0] = meta_val.x(); + value[1] = meta_val.y(); + value[2] = meta_val.z(); + } + CATCH_KEYERROR; +} + +void OpenVDBReader::vec3IMeta(const openvdb::Name &name, int value[3]) const +{ + try { + openvdb::Vec3i meta_val = m_meta_map->metaValue<openvdb::Vec3i>(name); + + value[0] = meta_val.x(); + value[1] = meta_val.y(); + value[2] = meta_val.z(); + } + CATCH_KEYERROR; +} + +void OpenVDBReader::mat4sMeta(const openvdb::Name &name, float value[4][4]) const +{ + try { + openvdb::Mat4s meta_val = m_meta_map->metaValue<openvdb::Mat4s>(name); + + for (int i = 0; i < 4; ++i) { + for (int j = 0; j < 4; ++j) { + value[i][j] = meta_val[i][j]; + } + } + } + CATCH_KEYERROR; +} + +bool OpenVDBReader::hasGrid(const openvdb::Name &name) const +{ + return m_file->hasGrid(name); +} + +openvdb::GridBase::Ptr OpenVDBReader::getGrid(const openvdb::Name &name) const +{ + return m_file->readGrid(name); +} + +size_t OpenVDBReader::numGrids() const +{ + return m_file->getGrids()->size(); +} + +void OpenVDBReader::cleanupFile() +{ + if (m_file) { + m_file->close(); + delete m_file; + } +} diff --git a/intern/openvdb/intern/openvdb_reader.h b/intern/openvdb/intern/openvdb_reader.h new file mode 100644 index 00000000000..07f77130ff9 --- /dev/null +++ b/intern/openvdb/intern/openvdb_reader.h @@ -0,0 +1,55 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2015 Blender Foundation. + * All rights reserved. + * + * Contributor(s): Kevin Dietrich + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#ifndef __OPENVDB_READER_H__ +#define __OPENVDB_READER_H__ + +#include <openvdb/openvdb.h> + +struct OpenVDBReader { +private: + openvdb::MetaMap::Ptr m_meta_map; + openvdb::io::File *m_file; + + void cleanupFile(); + +public: + OpenVDBReader(); + ~OpenVDBReader(); + + void open(const openvdb::Name &filename); + + void floatMeta(const openvdb::Name &name, float &value) const; + void intMeta(const openvdb::Name &name, int &value) const; + void vec3sMeta(const openvdb::Name &name, float value[3]) const; + void vec3IMeta(const openvdb::Name &name, int value[3]) const; + void mat4sMeta(const openvdb::Name &name, float value[4][4]) const; + + bool hasGrid(const openvdb::Name &name) const; + openvdb::GridBase::Ptr getGrid(const openvdb::Name &name) const; + size_t numGrids() const; +}; + +#endif /* __OPENVDB_READER_H__ */ diff --git a/intern/openvdb/intern/openvdb_writer.cc b/intern/openvdb/intern/openvdb_writer.cc new file mode 100644 index 00000000000..923752909d9 --- /dev/null +++ b/intern/openvdb/intern/openvdb_writer.cc @@ -0,0 +1,118 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2015 Blender Foundation. + * All rights reserved. + * + * Contributor(s): Kevin Dietrich + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#include "openvdb_writer.h" +#include "openvdb_util.h" + +OpenVDBWriter::OpenVDBWriter() + : m_grids(new openvdb::GridPtrVec()) + , m_meta_map(new openvdb::MetaMap()) + , m_save_as_half(false) +{ + m_meta_map->insertMeta("creator", openvdb::StringMetadata("Blender/Smoke")); +} + +OpenVDBWriter::~OpenVDBWriter() +{} + +void OpenVDBWriter::insert(const openvdb::GridBase::Ptr &grid) +{ + grid->setSaveFloatAsHalf(m_save_as_half); + m_grids->push_back(grid); +} + +void OpenVDBWriter::insert(const openvdb::GridBase &grid) +{ + m_grids->push_back(grid.copyGrid()); +} + +void OpenVDBWriter::insertFloatMeta(const openvdb::Name &name, const float value) +{ + try { + m_meta_map->insertMeta(name, openvdb::FloatMetadata(value)); + } + CATCH_KEYERROR; +} + +void OpenVDBWriter::insertIntMeta(const openvdb::Name &name, const int value) +{ + try { + m_meta_map->insertMeta(name, openvdb::Int32Metadata(value)); + } + CATCH_KEYERROR; +} + +void OpenVDBWriter::insertVec3sMeta(const openvdb::Name &name, const openvdb::Vec3s &value) +{ + try { + m_meta_map->insertMeta(name, openvdb::Vec3SMetadata(value)); + } + CATCH_KEYERROR; +} + +void OpenVDBWriter::insertVec3IMeta(const openvdb::Name &name, const openvdb::Vec3I &value) +{ + try { + m_meta_map->insertMeta(name, openvdb::Vec3IMetadata(value)); + } + CATCH_KEYERROR; +} + +void OpenVDBWriter::insertMat4sMeta(const openvdb::Name &name, const float value[4][4]) +{ + openvdb::Mat4s mat = openvdb::Mat4s( + value[0][0], value[0][1], value[0][2], value[0][3], + value[1][0], value[1][1], value[1][2], value[1][3], + value[2][0], value[2][1], value[2][2], value[2][3], + value[3][0], value[3][1], value[3][2], value[3][3]); + + try { + m_meta_map->insertMeta(name, openvdb::Mat4SMetadata(mat)); + } + CATCH_KEYERROR; +} + +void OpenVDBWriter::setFlags(const int compression, const bool save_as_half) +{ + m_compression_flags = compression; + m_save_as_half = save_as_half; +} + +void OpenVDBWriter::write(const openvdb::Name &filename) const +{ + try { + openvdb::io::File file(filename); + file.setCompression(m_compression_flags); + file.write(*m_grids, *m_meta_map); + file.close(); + + /* Should perhaps be an option at some point */ + m_grids->clear(); + } + /* Mostly to catch exceptions related to Blosc not being supported. */ + catch (const openvdb::IoError &e) { + std::cerr << e.what() << '\n'; + } +} diff --git a/intern/openvdb/intern/openvdb_writer.h b/intern/openvdb/intern/openvdb_writer.h new file mode 100644 index 00000000000..69f42473975 --- /dev/null +++ b/intern/openvdb/intern/openvdb_writer.h @@ -0,0 +1,57 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2015 Blender Foundation. + * All rights reserved. + * + * Contributor(s): Kevin Dietrich + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#ifndef __OPENVDB_WRITER_H__ +#define __OPENVDB_WRITER_H__ + +#include <openvdb/openvdb.h> + +struct OpenVDBWriter { +private: + openvdb::GridPtrVecPtr m_grids; + openvdb::MetaMap::Ptr m_meta_map; + + int m_compression_flags; + bool m_save_as_half; + +public: + OpenVDBWriter(); + ~OpenVDBWriter(); + + void insert(const openvdb::GridBase::Ptr &grid); + void insert(const openvdb::GridBase &grid); + + void insertFloatMeta(const openvdb::Name &name, const float value); + void insertIntMeta(const openvdb::Name &name, const int value); + void insertVec3sMeta(const openvdb::Name &name, const openvdb::Vec3s &value); + void insertVec3IMeta(const openvdb::Name &name, const openvdb::Vec3I &value); + void insertMat4sMeta(const openvdb::Name &name, const float value[4][4]); + + void setFlags(const int compression, const bool save_as_half); + + void write(const openvdb::Name &filename) const; +}; + +#endif /* __OPENVDB_WRITER_H__ */ diff --git a/intern/openvdb/openvdb_capi.cc b/intern/openvdb/openvdb_capi.cc new file mode 100644 index 00000000000..ef4f8c8820f --- /dev/null +++ b/intern/openvdb/openvdb_capi.cc @@ -0,0 +1,240 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2015 Blender Foundation. + * All rights reserved. + * + * Contributor(s): Kevin Dietrich + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#include "openvdb_capi.h" +#include "openvdb_dense_convert.h" +#include "openvdb_util.h" + +struct OpenVDBFloatGrid { int unused; }; +struct OpenVDBIntGrid { int unused; }; +struct OpenVDBVectorGrid { int unused; }; + +int OpenVDB_getVersionHex() +{ + return openvdb::OPENVDB_LIBRARY_VERSION; +} + +OpenVDBFloatGrid *OpenVDB_export_grid_fl( + OpenVDBWriter *writer, + const char *name, float *data, + const int res[3], float matrix[4][4], + OpenVDBFloatGrid *mask) +{ + Timer(__func__); + + using openvdb::FloatGrid; + + FloatGrid *mask_grid = reinterpret_cast<FloatGrid *>(mask); + FloatGrid *grid = internal::OpenVDB_export_grid<FloatGrid>( + writer, + name, + data, + res, + matrix, + mask_grid); + + return reinterpret_cast<OpenVDBFloatGrid *>(grid); +} + +OpenVDBIntGrid *OpenVDB_export_grid_ch( + OpenVDBWriter *writer, + const char *name, unsigned char *data, + const int res[3], float matrix[4][4], + OpenVDBFloatGrid *mask) +{ + Timer(__func__); + + using openvdb::FloatGrid; + using openvdb::Int32Grid; + + FloatGrid *mask_grid = reinterpret_cast<FloatGrid *>(mask); + Int32Grid *grid = internal::OpenVDB_export_grid<Int32Grid>( + writer, + name, + data, + res, + matrix, + mask_grid); + + return reinterpret_cast<OpenVDBIntGrid *>(grid); +} + +OpenVDBVectorGrid *OpenVDB_export_grid_vec( + struct OpenVDBWriter *writer, + const char *name, + const float *data_x, const float *data_y, const float *data_z, + const int res[3], float matrix[4][4], short vec_type, + const bool is_color, OpenVDBFloatGrid *mask) +{ + Timer(__func__); + + using openvdb::GridBase; + using openvdb::FloatGrid; + using openvdb::VecType; + + FloatGrid *mask_grid = reinterpret_cast<FloatGrid *>(mask); + GridBase *grid = internal::OpenVDB_export_vector_grid( + writer, + name, + data_x, + data_y, + data_z, + res, + matrix, + static_cast<VecType>(vec_type), + is_color, + mask_grid); + + return reinterpret_cast<OpenVDBVectorGrid *>(grid); +} + +void OpenVDB_import_grid_fl( + OpenVDBReader *reader, + const char *name, float **data, + const int res[3]) +{ + Timer(__func__); + + internal::OpenVDB_import_grid<openvdb::FloatGrid>(reader, name, data, res); +} + +void OpenVDB_import_grid_ch( + OpenVDBReader *reader, + const char *name, unsigned char **data, + const int res[3]) +{ + internal::OpenVDB_import_grid<openvdb::Int32Grid>(reader, name, data, res); +} + +void OpenVDB_import_grid_vec( + struct OpenVDBReader *reader, + const char *name, + float **data_x, float **data_y, float **data_z, + const int res[3]) +{ + Timer(__func__); + + internal::OpenVDB_import_grid_vector(reader, name, data_x, data_y, data_z, res); +} + +OpenVDBWriter *OpenVDBWriter_create() +{ + return new OpenVDBWriter(); +} + +void OpenVDBWriter_free(OpenVDBWriter *writer) +{ + delete writer; +} + +void OpenVDBWriter_set_flags(OpenVDBWriter *writer, const int flag, const bool half) +{ + int compression_flags = openvdb::io::COMPRESS_ACTIVE_MASK; + +#ifdef WITH_OPENVDB_BLOSC + if (flag == 0) { + compression_flags |= openvdb::io::COMPRESS_BLOSC; + } + else +#endif + if (flag == 1) { + compression_flags |= openvdb::io::COMPRESS_ZIP; + } + else { + compression_flags = openvdb::io::COMPRESS_NONE; + } + + writer->setFlags(compression_flags, half); +} + +void OpenVDBWriter_add_meta_fl(OpenVDBWriter *writer, const char *name, const float value) +{ + writer->insertFloatMeta(name, value); +} + +void OpenVDBWriter_add_meta_int(OpenVDBWriter *writer, const char *name, const int value) +{ + writer->insertIntMeta(name, value); +} + +void OpenVDBWriter_add_meta_v3(OpenVDBWriter *writer, const char *name, const float value[3]) +{ + writer->insertVec3sMeta(name, value); +} + +void OpenVDBWriter_add_meta_v3_int(OpenVDBWriter *writer, const char *name, const int value[3]) +{ + writer->insertVec3IMeta(name, value); +} + +void OpenVDBWriter_add_meta_mat4(OpenVDBWriter *writer, const char *name, float value[4][4]) +{ + writer->insertMat4sMeta(name, value); +} + +void OpenVDBWriter_write(OpenVDBWriter *writer, const char *filename) +{ + writer->write(filename); +} + +OpenVDBReader *OpenVDBReader_create() +{ + return new OpenVDBReader(); +} + +void OpenVDBReader_free(OpenVDBReader *reader) +{ + delete reader; +} + +void OpenVDBReader_open(OpenVDBReader *reader, const char *filename) +{ + reader->open(filename); +} + +void OpenVDBReader_get_meta_fl(OpenVDBReader *reader, const char *name, float *value) +{ + reader->floatMeta(name, *value); +} + +void OpenVDBReader_get_meta_int(OpenVDBReader *reader, const char *name, int *value) +{ + reader->intMeta(name, *value); +} + +void OpenVDBReader_get_meta_v3(OpenVDBReader *reader, const char *name, float value[3]) +{ + reader->vec3sMeta(name, value); +} + +void OpenVDBReader_get_meta_v3_int(OpenVDBReader *reader, const char *name, int value[3]) +{ + reader->vec3IMeta(name, value); +} + +void OpenVDBReader_get_meta_mat4(OpenVDBReader *reader, const char *name, float value[4][4]) +{ + reader->mat4sMeta(name, value); +} diff --git a/intern/openvdb/openvdb_capi.h b/intern/openvdb/openvdb_capi.h new file mode 100644 index 00000000000..2d2feeadcf1 --- /dev/null +++ b/intern/openvdb/openvdb_capi.h @@ -0,0 +1,108 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2015 Blender Foundation. + * All rights reserved. + * + * Contributor(s): Kevin Dietrich + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#ifndef __OPENVDB_CAPI_H__ +#define __OPENVDB_CAPI_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +struct OpenVDBReader; +struct OpenVDBWriter; +struct OpenVDBFloatGrid; +struct OpenVDBIntGrid; +struct OpenVDBVectorGrid; + +int OpenVDB_getVersionHex(void); + +enum { + VEC_INVARIANT = 0, + VEC_COVARIANT = 1, + VEC_COVARIANT_NORMALIZE = 2, + VEC_CONTRAVARIANT_RELATIVE = 3, + VEC_CONTRAVARIANT_ABSOLUTE = 4, +}; + +struct OpenVDBFloatGrid *OpenVDB_export_grid_fl( + struct OpenVDBWriter *writer, + const char *name, float *data, + const int res[3], float matrix[4][4], + struct OpenVDBFloatGrid *mask); + +struct OpenVDBIntGrid *OpenVDB_export_grid_ch( + struct OpenVDBWriter *writer, + const char *name, unsigned char *data, + const int res[3], float matrix[4][4], + struct OpenVDBFloatGrid *mask); + +struct OpenVDBVectorGrid *OpenVDB_export_grid_vec( + struct OpenVDBWriter *writer, + const char *name, + const float *data_x, const float *data_y, const float *data_z, + const int res[3], float matrix[4][4], short vec_type, + const bool is_color, + struct OpenVDBFloatGrid *mask); + +void OpenVDB_import_grid_fl( + struct OpenVDBReader *reader, + const char *name, float **data, + const int res[3]); + +void OpenVDB_import_grid_ch( + struct OpenVDBReader *reader, + const char *name, unsigned char **data, + const int res[3]); + +void OpenVDB_import_grid_vec( + struct OpenVDBReader *reader, + const char *name, + float **data_x, float **data_y, float **data_z, + const int res[3]); + +struct OpenVDBWriter *OpenVDBWriter_create(void); +void OpenVDBWriter_free(struct OpenVDBWriter *writer); +void OpenVDBWriter_set_flags(struct OpenVDBWriter *writer, const int flag, const bool half); +void OpenVDBWriter_add_meta_fl(struct OpenVDBWriter *writer, const char *name, const float value); +void OpenVDBWriter_add_meta_int(struct OpenVDBWriter *writer, const char *name, const int value); +void OpenVDBWriter_add_meta_v3(struct OpenVDBWriter *writer, const char *name, const float value[3]); +void OpenVDBWriter_add_meta_v3_int(struct OpenVDBWriter *writer, const char *name, const int value[3]); +void OpenVDBWriter_add_meta_mat4(struct OpenVDBWriter *writer, const char *name, float value[4][4]); +void OpenVDBWriter_write(struct OpenVDBWriter *writer, const char *filename); + +struct OpenVDBReader *OpenVDBReader_create(void); +void OpenVDBReader_free(struct OpenVDBReader *reader); +void OpenVDBReader_open(struct OpenVDBReader *reader, const char *filename); +void OpenVDBReader_get_meta_fl(struct OpenVDBReader *reader, const char *name, float *value); +void OpenVDBReader_get_meta_int(struct OpenVDBReader *reader, const char *name, int *value); +void OpenVDBReader_get_meta_v3(struct OpenVDBReader *reader, const char *name, float value[3]); +void OpenVDBReader_get_meta_v3_int(struct OpenVDBReader *reader, const char *name, int value[3]); +void OpenVDBReader_get_meta_mat4(struct OpenVDBReader *reader, const char *name, float value[4][4]); + +#ifdef __cplusplus +} +#endif + +#endif /* __OPENVDB_CAPI_H__ */ diff --git a/intern/openvdb/openvdb_util.cc b/intern/openvdb/openvdb_util.cc new file mode 100644 index 00000000000..d187f55970d --- /dev/null +++ b/intern/openvdb/openvdb_util.cc @@ -0,0 +1,38 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2015 Blender Foundation. + * All rights reserved. + * + * Contributor(s): Kevin Dietrich + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#include "openvdb_util.h" + +#include <cstdio> + +ScopeTimer::ScopeTimer(const std::string &message) + : m_message(message) + , m_timer() +{} + +ScopeTimer::~ScopeTimer() +{ + std::printf("%s: %fms\n", m_message.c_str(), m_timer.delta()); +} diff --git a/intern/openvdb/openvdb_util.h b/intern/openvdb/openvdb_util.h new file mode 100644 index 00000000000..8e14ed54345 --- /dev/null +++ b/intern/openvdb/openvdb_util.h @@ -0,0 +1,57 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2015 Blender Foundation. + * All rights reserved. + * + * Contributor(s): Kevin Dietrich + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#ifndef __OPENVDB_UTIL_H__ +#define __OPENVDB_UTIL_H__ + +#include <openvdb/openvdb.h> +#include <openvdb/util/CpuTimer.h> + +#define CATCH_KEYERROR \ + catch (const openvdb::KeyError &e) { \ + std::cerr << e.what() << '\n'; \ + } + +//#define DEBUG_TIME + +/* A utility class which prints the time elapsed during its lifetime, useful for + * e.g. timing the overall execution time of a function */ +class ScopeTimer { + std::string m_message; + openvdb::util::CpuTimer m_timer; + +public: + ScopeTimer(const std::string &message); + ~ScopeTimer(); +}; + +#ifdef DEBUG_TIME +# define Timer(x) \ + ScopeTimer prof(x); +#else +# define Timer(x) +#endif + +#endif /* __OPENVDB_UTIL_H__ */ |