diff options
Diffstat (limited to 'intern/openvdb')
-rw-r--r-- | intern/openvdb/CMakeLists.txt | 61 | ||||
-rw-r--r-- | intern/openvdb/SConscript | 53 | ||||
-rw-r--r-- | intern/openvdb/intern/openvdb_dense_convert.cpp | 263 | ||||
-rw-r--r-- | intern/openvdb/intern/openvdb_dense_convert.h | 140 | ||||
-rw-r--r-- | intern/openvdb/intern/openvdb_primitive.cpp | 92 | ||||
-rw-r--r-- | intern/openvdb/intern/openvdb_primitive.h | 47 | ||||
-rw-r--r-- | intern/openvdb/intern/openvdb_reader.cpp | 107 | ||||
-rw-r--r-- | intern/openvdb/intern/openvdb_reader.h | 51 | ||||
-rw-r--r-- | intern/openvdb/intern/openvdb_render.cpp | 136 | ||||
-rw-r--r-- | intern/openvdb/intern/openvdb_render.h | 15 | ||||
-rw-r--r-- | intern/openvdb/intern/openvdb_writer.cpp | 93 | ||||
-rw-r--r-- | intern/openvdb/intern/openvdb_writer.h | 55 | ||||
-rw-r--r-- | intern/openvdb/openvdb_capi.cpp | 248 | ||||
-rw-r--r-- | intern/openvdb/openvdb_capi.h | 131 | ||||
-rw-r--r-- | intern/openvdb/openvdb_util.cpp | 80 | ||||
-rw-r--r-- | intern/openvdb/openvdb_util.h | 31 |
16 files changed, 1603 insertions, 0 deletions
diff --git a/intern/openvdb/CMakeLists.txt b/intern/openvdb/CMakeLists.txt new file mode 100644 index 00000000000..d7186338a16 --- /dev/null +++ b/intern/openvdb/CMakeLists.txt @@ -0,0 +1,61 @@ +# ***** 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 + ${GLEW_INCLUDE_PATH} +) + +set(SRC + openvdb_capi.h +) + +if(WITH_OPENVDB) + add_definitions( + -DWITH_OPENVDB + -DOPENVDB_USE_BLOSC + ) + + list(APPEND INC_SYS + ${OPENEXR_INCLUDE_DIRS} + ${OPENVDB_INCLUDE_DIRS} + ) + + list(APPEND SRC + intern/openvdb_dense_convert.cpp + intern/openvdb_primitive.cpp + intern/openvdb_reader.cpp + intern/openvdb_render.cpp + intern/openvdb_writer.cpp + openvdb_capi.cpp + openvdb_util.cpp + ) +endif() + +blender_add_lib(bf_intern_openvdb "${SRC}" "${INC}" "${INC_SYS}") diff --git a/intern/openvdb/SConscript b/intern/openvdb/SConscript new file mode 100644 index 00000000000..b05437b36fe --- /dev/null +++ b/intern/openvdb/SConscript @@ -0,0 +1,53 @@ +#!/usr/bin/env python +# +# ***** 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 ***** + +Import ('env') + +sources = [] + +defs = [] +incs = [ + '.', + 'intern', + '../guardedalloc', + '../../source/blender/blenkernel', + '../../source/blender/blenlib', + '../../source/blender/makesdna', + '../../source/blender/makesrna', + env['BF_OPENVDB_INC'], + env['BF_BOOST_INC'], + env['BF_GLEW_INC'] +] + +incs.append(env['BF_OPENEXR_INC'].split()) + +if env['WITH_BF_OPENVDB']: + defs.append('WITH_OPENVDB') + defs.append('DDF_DEBUG=0') + sources = env.Glob('*.cpp') + env.Glob('intern/*.cpp') + +env.BlenderLib ( libname = 'bf_intern_openvdb', sources = sources, includes = Split(incs), defines = defs, libtype=['core'], priority = [364] ) diff --git a/intern/openvdb/intern/openvdb_dense_convert.cpp b/intern/openvdb/intern/openvdb_dense_convert.cpp new file mode 100644 index 00000000000..d7a9896675e --- /dev/null +++ b/intern/openvdb/intern/openvdb_dense_convert.cpp @@ -0,0 +1,263 @@ +/* + * ***** 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/tools/ValueTransformer.h> + +#include "openvdb_capi.h" +#include "openvdb_dense_convert.h" +#include "openvdb_writer.h" + +using namespace openvdb; + +namespace internal { + +class MergeScalarGrids { + tree::ValueAccessor<const FloatTree> m_acc_x, m_acc_y, m_acc_z; + +public: + MergeScalarGrids(const FloatTree *x_tree, const FloatTree *y_tree, const FloatTree *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 Vec3STree::ValueOnIter &it) const + { + 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(Vec3s(x, y, z)); + } +}; + +GridBase *OpenVDB_export_vector_grid(OpenVDBWriter *writer, + const std::string &name, + const float *data_x, const float *data_y, const float *data_z, + const int res[3], + float fluid_mat[4][4], + VecType vec_type, + const bool is_color, + const FloatGrid *mask) +{ + + math::CoordBBox bbox(Coord(0), Coord(res[0] - 1, res[1] - 1, res[2] - 1)); + + Mat4R mat = Mat4R( + fluid_mat[0][0], fluid_mat[0][1], fluid_mat[0][2], fluid_mat[0][3], + fluid_mat[1][0], fluid_mat[1][1], fluid_mat[1][2], fluid_mat[1][3], + fluid_mat[2][0], fluid_mat[2][1], fluid_mat[2][2], fluid_mat[2][3], + fluid_mat[3][0], fluid_mat[3][1], fluid_mat[3][2], fluid_mat[3][3]); + + 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)); + + writer->insert(vecgrid); + + return vecgrid.get(); +} + +class SplitVectorGrid { + FloatGrid::Ptr m_grid_x, m_grid_y, m_grid_z; + +public: + SplitVectorGrid() + {} + + void operator()(const Vec3SGrid::Ptr &vecgrid) + { + Vec3s bg = vecgrid->background(); + m_grid_x = FloatGrid::create(bg.x()); + m_grid_y = FloatGrid::create(bg.y()); + m_grid_z = FloatGrid::create(bg.z()); + + if (math::Transform::Ptr xform = vecgrid->transform().copy()) { + m_grid_x->setTransform(xform); + m_grid_y->setTransform(xform); + m_grid_z->setTransform(xform); + } + + FloatGrid::Accessor acc_x = m_grid_x->getAccessor(), + acc_y = m_grid_y->getAccessor(), + acc_z = m_grid_z->getAccessor(); + + CoordBBox bbox; + for (Vec3SGrid::ValueOnCIter it = vecgrid->cbeginValueOn(); it; ++it) { + if (!it.getBoundingBox(bbox)) continue; + + const Vec3s &val = it.getValue(); + + if (it.isTileValue()) { + m_grid_x->fill(bbox, val.x()); + m_grid_y->fill(bbox, val.y()); + m_grid_z->fill(bbox, val.z()); + } + else { + acc_x.setValueOn(bbox.min(), val.x()); + acc_y.setValueOn(bbox.min(), val.y()); + acc_z.setValueOn(bbox.min(), val.z()); + } + } + } + + const FloatGrid::Ptr &grid_x() { return m_grid_x; } + const FloatGrid::Ptr &grid_y() { return m_grid_y; } + const FloatGrid::Ptr &grid_z() { return m_grid_z; } +}; + +void OpenVDB_import_grid_vector(OpenVDBReader *reader, + const std::string &name, + float **data_x, float **data_y, float **data_z, + const int res[3]) +{ + Vec3SGrid::Ptr vgrid = gridPtrCast<Vec3SGrid>(reader->getGrid(name)); + +#if 0 + SplitVectorGrid vector_split; + vector_split(vgrid); + + FloatGrid::Ptr grid[3]; + math::CoordBBox bbox(Coord(0), Coord(res[0] - 1, res[1] - 1, res[2] - 1)); + + grid[0] = vector_split.grid_x(); + tools::Dense<float, tools::LayoutXYZ> dense_grid_x(bbox); + tools::copyToDense(*grid[0], dense_grid_x); + *data_x = dense_grid_x.data(); + + grid[1] = vector_split.grid_y(); + tools::Dense<float, tools::LayoutXYZ> dense_grid_y(bbox); + tools::copyToDense(*grid[1], dense_grid_y); + *data_y = dense_grid_y.data(); + + grid[2] = vector_split.grid_z(); + tools::Dense<float, tools::LayoutXYZ> dense_grid_z(bbox); + tools::copyToDense(*grid[2], dense_grid_z); + *data_z = dense_grid_z.data(); +#else + Vec3SGrid::Accessor acc = vgrid->getAccessor(); + math::Coord xyz; + int &x = xyz[0], &y = xyz[1], &z = xyz[2]; + + int 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(); + } + } + } +#endif +} + +void OpenVDB_update_fluid_transform(const char *filename, float matrix[4][4], float matrix_high[4][4]) +{ + /* TODO(kevin): deduplicate this call */ + initialize(); + + Mat4R fluid_mat = Mat4R( + matrix[0][0], matrix[0][1], matrix[0][2], matrix[0][3], + matrix[1][0], matrix[1][1], matrix[1][2], matrix[1][3], + matrix[2][0], matrix[2][1], matrix[2][2], matrix[2][3], + matrix[3][0], matrix[3][1], matrix[3][2], matrix[3][3]); + + Mat4R fluid_matBig = Mat4R( + matrix_high[0][0], matrix_high[0][1], matrix_high[0][2], matrix_high[0][3], + matrix_high[1][0], matrix_high[1][1], matrix_high[1][2], matrix_high[1][3], + matrix_high[2][0], matrix_high[2][1], matrix_high[2][2], matrix_high[2][3], + matrix_high[3][0], matrix_high[3][1], matrix_high[3][2], matrix_high[3][3]); + + math::Transform::Ptr transform = math::Transform::createLinearTransform(fluid_mat); + math::Transform::Ptr transformBig = math::Transform::createLinearTransform(fluid_matBig); + + io::File file(filename); + file.open(); + GridPtrVecPtr grids = file.getGrids(); + GridBase::Ptr grid; + + for (size_t i = 0; i < grids->size(); ++i) { + grid = (*grids)[i]; + + const std::string name = grid->getName(); + size_t found = name.find("High"); + + if (found != std::string::npos) { + grid->setTransform(transformBig); + } + else { + grid->setTransform(transform); + } + } + + file.close(); +} + +} // 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..d0243decf53 --- /dev/null +++ b/intern/openvdb/intern/openvdb_dense_convert.h @@ -0,0 +1,140 @@ +/* + * ***** 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/openvdb.h> +#include <openvdb/tools/Dense.h> +#include <openvdb/tools/Clip.h> + +#include "openvdb_primitive.h" +#include "openvdb_reader.h" +#include "openvdb_writer.h" + +#define TOLERANCE 1e-3f + +namespace internal { + +template <typename GridType, typename T> +GridType *OpenVDB_export_grid(OpenVDBWriter *writer, + const std::string &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 = Mat4R( + fluid_mat[0][0], fluid_mat[0][1], fluid_mat[0][2], fluid_mat[0][3], + fluid_mat[1][0], fluid_mat[1][1], fluid_mat[1][2], fluid_mat[1][3], + fluid_mat[2][0], fluid_mat[2][1], fluid_mat[2][2], fluid_mat[2][3], + fluid_mat[3][0], fluid_mat[3][1], fluid_mat[3][2], fluid_mat[3][3]); + + 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(), TOLERANCE); + + grid->setTransform(transform); + + if (mask) { + grid = tools::clip(*grid, *mask); + } + + grid->setName(name); + grid->setIsInWorldSpace(false); + + writer->insert(grid); + + return grid.get(); +} + +template <typename GridType, typename T> +OpenVDBPrimitive *OpenVDB_import_grid(OpenVDBReader *reader, + const std::string &name, + T **data, + const int res[3]) +{ + using namespace openvdb; + + typename GridType::Ptr grid_tmp = gridPtrCast<GridType>(reader->getGrid(name)); +#if 0 + math::CoordBBox bbox(Coord(0), Coord(res[0] - 1, res[1] - 1, res[2] - 1)); + + tools::Dense<T, tools::LayoutXYZ> dense_grid(bbox); + tools::copyToDense(*grid_tmp, dense_grid); + memcpy(*data, dense_grid.data(), sizeof(T) * res[0] * res[1] * res[2]); +#else + typename GridType::Accessor acc = grid_tmp->getAccessor(); + math::Coord xyz; + int &x = xyz[0], &y = xyz[1], &z = xyz[2]; + + int 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); + } + } + } +#endif + + OpenVDBPrimitive *vdb_prim = new OpenVDBPrimitive(); + vdb_prim->setGrid(grid_tmp); + + return vdb_prim; +} + +openvdb::GridBase *OpenVDB_export_vector_grid(OpenVDBWriter *writer, + const std::string &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 std::string &name, + float **data_x, float **data_y, float **data_z, + const int res[3]); + +void OpenVDB_update_fluid_transform(const char *filename, + float matrix[4][4], + float matrix_high[4][4]); + +} + +#endif /* __OPENVDB_DENSE_CONVERT_H__ */ + diff --git a/intern/openvdb/intern/openvdb_primitive.cpp b/intern/openvdb/intern/openvdb_primitive.cpp new file mode 100644 index 00000000000..301a1a5fcbe --- /dev/null +++ b/intern/openvdb/intern/openvdb_primitive.cpp @@ -0,0 +1,92 @@ +/* + * ***** 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_primitive.h" + +OpenVDBPrimitive::OpenVDBPrimitive() +{} + +OpenVDBPrimitive::~OpenVDBPrimitive() +{} + +openvdb::GridBase &OpenVDBPrimitive::getGrid() +{ + return *m_grid; +} + +const openvdb::GridBase &OpenVDBPrimitive::getConstGrid() const +{ + return *m_grid; +} + +openvdb::GridBase::Ptr OpenVDBPrimitive::getGridPtr() +{ + return m_grid; +} + +openvdb::GridBase::ConstPtr OpenVDBPrimitive::getConstGridPtr() const +{ + return m_grid; +} + +void OpenVDBPrimitive::setGrid(openvdb::GridBase::Ptr grid) +{ + m_grid = grid->copyGrid(); +} + +static 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]); +} + +/* A simple protection to avoid crashes for cases when something goes wrong for + * some reason in the matrix creation. */ +static openvdb::math::MapBase::Ptr createAffineMap(const float mat[4][4]) +{ + using namespace openvdb::math; + MapBase::Ptr transform; + + try { + transform.reset(new AffineMap(convertMatrix(mat))); + } + catch (const openvdb::ArithmeticError &e) { + std::cerr << e.what() << "\n"; + transform.reset(new AffineMap()); + } + + return transform; +} + +void OpenVDBPrimitive::setTransform(const float mat[4][4]) +{ + using namespace openvdb::math; + + Transform::Ptr transform = Transform::Ptr(new Transform(createAffineMap(mat))); + m_grid->setTransform(transform); +} diff --git a/intern/openvdb/intern/openvdb_primitive.h b/intern/openvdb/intern/openvdb_primitive.h new file mode 100644 index 00000000000..65633dfa86a --- /dev/null +++ b/intern/openvdb/intern/openvdb_primitive.h @@ -0,0 +1,47 @@ +/* + * ***** 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_PRIMITIVE_H__ +#define __OPENVDB_PRIMITIVE_H__ + +#include <openvdb/openvdb.h> + +class OpenVDBPrimitive { + openvdb::GridBase::Ptr m_grid; + +public: + OpenVDBPrimitive(); + ~OpenVDBPrimitive(); + + openvdb::GridBase &getGrid(); + const openvdb::GridBase &getConstGrid() const; + openvdb::GridBase::Ptr getGridPtr(); + openvdb::GridBase::ConstPtr getConstGridPtr() const; + + void setGrid(openvdb::GridBase::Ptr grid); + void setTransform(const float mat[4][4]); +}; + +#endif /* __OPENVDB_PRIMITIVE_H__ */ diff --git a/intern/openvdb/intern/openvdb_reader.cpp b/intern/openvdb/intern/openvdb_reader.cpp new file mode 100644 index 00000000000..1ed080e7574 --- /dev/null +++ b/intern/openvdb/intern/openvdb_reader.cpp @@ -0,0 +1,107 @@ +/* + * ***** 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" + +#define COPY_MAX_BYTES 10485760 /* 10 Mb */ + +OpenVDBReader::OpenVDBReader() + : m_meta_map(new openvdb::MetaMap) +{ + /* Although it is safe, it may not be good to have this here... */ + openvdb::initialize(); + m_file = NULL; +} + +OpenVDBReader::~OpenVDBReader() +{ + if (m_file) { + m_file->close(); + delete m_file; + } +} + +void OpenVDBReader::open(const std::string &filename) +{ + if (m_file) { + m_file->close(); + delete m_file; + } + + m_file = new openvdb::io::File(filename); + m_file->setCopyMaxBytes(COPY_MAX_BYTES); + m_file->open(); + + m_meta_map = m_file->getMetadata(); +} + +void OpenVDBReader::floatMeta(const std::string &name, float &value) +{ + value = m_meta_map->metaValue<float>(name); +} + +void OpenVDBReader::intMeta(const std::string &name, int &value) +{ + value = m_meta_map->metaValue<int>(name); +} + +void OpenVDBReader::vec3sMeta(const std::string &name, float value[3]) +{ + 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(); +} + +void OpenVDBReader::vec3IMeta(const std::string &name, int value[3]) +{ + 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(); +} + +void OpenVDBReader::mat4sMeta(const std::string &name, float value[4][4]) +{ + 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]; + } + } +} + +openvdb::GridBase::Ptr OpenVDBReader::getGrid(const std::string &name) +{ + return m_file->readGrid(name); +} + +size_t OpenVDBReader::numGrids() const +{ + return m_file->getGrids()->size(); +} diff --git a/intern/openvdb/intern/openvdb_reader.h b/intern/openvdb/intern/openvdb_reader.h new file mode 100644 index 00000000000..ae79b63c9c7 --- /dev/null +++ b/intern/openvdb/intern/openvdb_reader.h @@ -0,0 +1,51 @@ +/* + * ***** 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> + +class OpenVDBReader { + openvdb::MetaMap::Ptr m_meta_map; + openvdb::io::File *m_file; + +public: + OpenVDBReader(); + ~OpenVDBReader(); + + void open(const std::string &filename); + + void floatMeta(const std::string &name, float &value); + void intMeta(const std::string &name, int &value); + void vec3sMeta(const std::string &name, float value[3]); + void vec3IMeta(const std::string &name, int value[3]); + void mat4sMeta(const std::string &name, float value[4][4]); + + openvdb::GridBase::Ptr getGrid(const std::string &name); + size_t numGrids() const; +}; + +#endif /* __OPENVDB_READER_H__ */ diff --git a/intern/openvdb/intern/openvdb_render.cpp b/intern/openvdb/intern/openvdb_render.cpp new file mode 100644 index 00000000000..51aa43676c9 --- /dev/null +++ b/intern/openvdb/intern/openvdb_render.cpp @@ -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 <GL/glew.h> +#include <openvdb/openvdb.h> + +#include "openvdb_primitive.h" +#include "openvdb_render.h" + +namespace internal { + +void OpenVDBPrimitive_draw_tree(OpenVDBPrimitive *vdb_prim, const bool draw_root, const bool draw_level_1, const bool draw_level_2, const bool draw_leaves) +{ + using namespace openvdb; + using namespace openvdb::math; + + FloatGrid::Ptr grid = gridPtrCast<FloatGrid>(vdb_prim->getGridPtr()); + + math::Vec3d wmin, wmax; + math::Vec3s color(0.0f); + + math::Vec3s node_color[4] = { + math::Vec3s(0.0060f, 0.2790f, 0.6250f), // leaf nodes + math::Vec3s(0.8710f, 0.3940f, 0.0191f), // intermediate internal node levels + math::Vec3s(0.0432f, 0.3300f, 0.0411f), // first internal node level + math::Vec3s(0.0450f, 0.0450f, 0.0450f) // root + }; + + math::CoordBBox bbox; + + glBegin(GL_LINES); + + for (FloatTree::NodeCIter node_iter = grid->tree().cbeginNode(); node_iter; ++node_iter) { + node_iter.getBoundingBox(bbox); + + const Vec3d min(bbox.min().x() - 0.5, bbox.min().y() - 0.5, bbox.min().z() - 0.5); + const Vec3d max(bbox.max().x() + 0.5, bbox.max().y() + 0.5, bbox.max().z() + 0.5); + + wmin = grid->indexToWorld(min); + wmax = grid->indexToWorld(max); + + const int level = node_iter.getLevel(); + + if (level == 0) { + if (!draw_leaves) { + continue; + } + color = node_color[0]; + } + + if (level == 1) { + if (!draw_level_2) { + continue; + } + color = node_color[1]; + } + + if (level == 2) { + if (!draw_level_1) { + continue; + } + color = node_color[2]; + } + + if (level == 3) { + if (!draw_root) { + continue; + } + color = node_color[3]; + } + + glColor3f(color[0], color[1], color[2]); + + glVertex3f(wmin.x(), wmax.y(), wmax.z()); + glVertex3f(wmax.x(), wmax.y(), wmax.z()); + + glVertex3f(wmin.x(), wmax.y(), wmin.z()); + glVertex3f(wmax.x(), wmax.y(), wmin.z()); + + glVertex3f(wmin.x(), wmin.y(), wmax.z()); + glVertex3f(wmax.x(), wmin.y(), wmax.z()); + + glVertex3f(wmin.x(), wmin.y(), wmin.z()); + glVertex3f(wmax.x(), wmin.y(), wmin.z()); + + glVertex3f(wmin.x(), wmin.y(), wmin.z()); + glVertex3f(wmin.x(), wmin.y(), wmax.z()); + + glVertex3f(wmin.x(), wmax.y(), wmin.z()); + glVertex3f(wmin.x(), wmax.y(), wmax.z()); + + glVertex3f(wmax.x(), wmin.y(), wmin.z()); + glVertex3f(wmax.x(), wmin.y(), wmax.z()); + + glVertex3f(wmax.x(), wmax.y(), wmin.z()); + glVertex3f(wmax.x(), wmax.y(), wmax.z()); + + glVertex3f(wmin.x(), wmin.y(), wmin.z()); + glVertex3f(wmin.x(), wmax.y(), wmin.z()); + + glVertex3f(wmax.x(), wmin.y(), wmin.z()); + glVertex3f(wmax.x(), wmax.y(), wmin.z()); + + glVertex3f(wmax.x(), wmin.y(), wmax.z()); + glVertex3f(wmax.x(), wmax.y(), wmax.z()); + + glVertex3f(wmin.x(), wmin.y(), wmax.z()); + glVertex3f(wmin.x(), wmax.y(), wmax.z()); + } + + glEnd(); +} + +} diff --git a/intern/openvdb/intern/openvdb_render.h b/intern/openvdb/intern/openvdb_render.h new file mode 100644 index 00000000000..06bff3540b7 --- /dev/null +++ b/intern/openvdb/intern/openvdb_render.h @@ -0,0 +1,15 @@ +#ifndef __OPENVDB_RENDER_H__ +#define __OPENVDB_RENDER_H__ + +struct OpenVDBPrimitive; + +namespace internal { + +void OpenVDBPrimitive_draw_tree(OpenVDBPrimitive *vdb_prim, const bool draw_root, + const bool draw_level_1, const bool draw_level_2, + const bool draw_leaves); + +} + +#endif /* __OPENVDB_RENDER_H__ */ + diff --git a/intern/openvdb/intern/openvdb_writer.cpp b/intern/openvdb/intern/openvdb_writer.cpp new file mode 100644 index 00000000000..9c2ad40c425 --- /dev/null +++ b/intern/openvdb/intern/openvdb_writer.cpp @@ -0,0 +1,93 @@ +/* + * ***** 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" + +OpenVDBWriter::OpenVDBWriter() + : m_grids(new openvdb::GridPtrVec()) + , m_meta_map(new openvdb::MetaMap()) +{ + m_meta_map->insertMeta("creator", openvdb::StringMetadata("Blender/OpenVDBWriter")); +} + +OpenVDBWriter::~OpenVDBWriter() +{} + +void OpenVDBWriter::insert(const openvdb::GridBase::Ptr &grid) +{ + m_grids->push_back(grid); +} + +void OpenVDBWriter::insert(const openvdb::GridBase &grid) +{ + m_grids->push_back(grid.copyGrid()); +} + +void OpenVDBWriter::insertFloatMeta(const std::string &name, const float value) +{ + m_meta_map->insertMeta(name, openvdb::FloatMetadata(value)); +} + +void OpenVDBWriter::insertIntMeta(const std::string &name, const int value) +{ + m_meta_map->insertMeta(name, openvdb::Int32Metadata(value)); +} + +void OpenVDBWriter::insertVec3sMeta(const std::string &name, const openvdb::Vec3s value) +{ + m_meta_map->insertMeta(name, openvdb::Vec3SMetadata(value)); +} + +void OpenVDBWriter::insertVec3IMeta(const std::string &name, const openvdb::Vec3I value) +{ + m_meta_map->insertMeta(name, openvdb::Vec3IMetadata(value)); +} + +void OpenVDBWriter::insertMat4sMeta(const std::string &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]); + + m_meta_map->insertMeta(name, openvdb::Mat4SMetadata(mat)); +} + +void OpenVDBWriter::setFileCompression(const int flags) +{ + m_flags = flags; +} + +void OpenVDBWriter::write(const std::string &filename) const +{ + openvdb::io::File file(filename); + file.setCompression(m_flags); + file.write(*m_grids, *m_meta_map); + file.close(); + + /* Should perhaps be an option at some point */ + m_grids->clear(); +} diff --git a/intern/openvdb/intern/openvdb_writer.h b/intern/openvdb/intern/openvdb_writer.h new file mode 100644 index 00000000000..6f36d6233a3 --- /dev/null +++ b/intern/openvdb/intern/openvdb_writer.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_WRITER_H__ +#define __OPENVDB_WRITER_H__ + +#include <openvdb/openvdb.h> + +class OpenVDBWriter { + openvdb::GridPtrVecPtr m_grids; + openvdb::MetaMap::Ptr m_meta_map; + + int m_flags; + +public: + OpenVDBWriter(); + ~OpenVDBWriter(); + + void insert(const openvdb::GridBase::Ptr &grid); + void insert(const openvdb::GridBase &grid); + + void insertFloatMeta(const std::string &name, const float value); + void insertIntMeta(const std::string &name, const int value); + void insertVec3sMeta(const std::string &name, const openvdb::Vec3s value); + void insertVec3IMeta(const std::string &name, const openvdb::Vec3I value); + void insertMat4sMeta(const std::string &name, const float value[4][4]); + + void setFileCompression(const int flags); + + void write(const std::string &filename) const; +}; + +#endif /* __OPENVDB_WRITER_H__ */ diff --git a/intern/openvdb/openvdb_capi.cpp b/intern/openvdb/openvdb_capi.cpp new file mode 100644 index 00000000000..64df4b94c3d --- /dev/null +++ b/intern/openvdb/openvdb_capi.cpp @@ -0,0 +1,248 @@ +/* + * ***** 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_primitive.h" +#include "openvdb_render.h" +#include "openvdb_util.h" + +using namespace openvdb; + +struct OpenVDBFloatGrid { int unused; }; +struct OpenVDBIntGrid { int unused; }; +struct OpenVDBVectorGrid { int unused; }; + +int OpenVDB_getVersionHex() +{ + return OPENVDB_LIBRARY_VERSION; +} + +void OpenVDB_get_grid_info(const char *filename, OpenVDBGridInfoCallback cb, void *userdata) +{ + int ret = OPENVDB_NO_ERROR; + initialize(); + + try { + io::File file(filename); + file.open(); + + GridPtrVecPtr grids = file.getGrids(); + int grid_num = grids->size(); + + for (size_t i = 0; i < grid_num; ++i) { + GridBase::ConstPtr grid = (*grids)[i]; + + std::string name = grid->getName(); + std::string value_type = grid->valueType(); + bool is_color = false; + if (grid->getMetadata< TypedMetadata<bool> >("is_color")) + is_color = grid->metaValue<bool>("is_color"); + + cb(userdata, name.c_str(), value_type.c_str(), is_color); + } + } + catch (...) { + catch_exception(ret); + } +} + +void OpenVDB_update_fluid_transform(const char *filename, + float matrix[4][4], + float matrix_high[4][4]) +{ + int ret = OPENVDB_NO_ERROR; + + try { + internal::OpenVDB_update_fluid_transform(filename, matrix, matrix_high); + } + catch (...) { + catch_exception(ret); + } +} + +OpenVDBFloatGrid *OpenVDB_export_grid_fl(OpenVDBWriter *writer, + const char *name, float *data, + const int res[3], float matrix[4][4], + OpenVDBFloatGrid *mask) +{ + OpenVDBFloatGrid *grid = + (OpenVDBFloatGrid *)internal::OpenVDB_export_grid<FloatGrid>(writer, name, data, res, matrix, (FloatGrid *)mask); + return grid; +} + +OpenVDBIntGrid *OpenVDB_export_grid_ch(OpenVDBWriter *writer, + const char *name, unsigned char *data, + const int res[3], float matrix[4][4], + OpenVDBFloatGrid *mask) +{ + OpenVDBIntGrid *grid = + (OpenVDBIntGrid *)internal::OpenVDB_export_grid<Int32Grid>(writer, name, data, res, matrix, (FloatGrid *)mask); + return 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) +{ + OpenVDBVectorGrid *grid = + (OpenVDBVectorGrid *)internal::OpenVDB_export_vector_grid(writer, name, + data_x, data_y, data_z, res, matrix, + static_cast<openvdb::VecType>(vec_type), + is_color, (FloatGrid *)mask); + return grid; +} + +OpenVDBPrimitive *OpenVDB_import_grid_fl(OpenVDBReader *reader, + const char *name, float **data, + const int res[3]) +{ + return internal::OpenVDB_import_grid<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<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]) +{ + 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; + writer = NULL; +} + +void OpenVDBWriter_set_compression(OpenVDBWriter *writer, const int flag) +{ + int compression_flags = io::COMPRESS_ACTIVE_MASK; + + if (flag == 0) { + compression_flags |= io::COMPRESS_ZIP; + } + else if (flag == 1) { + compression_flags |= io::COMPRESS_BLOSC; + } + else { + compression_flags = io::COMPRESS_NONE; + } + + writer->setFileCompression(compression_flags); +} + +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; + reader = NULL; +} + +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); +} + +void OpenVDB_draw_primitive(OpenVDBPrimitive *vdb_prim, + const bool draw_root, + const bool draw_level_1, + const bool draw_level_2, + const bool draw_leaves) +{ + internal::OpenVDBPrimitive_draw_tree(vdb_prim, draw_root, draw_level_1, draw_level_2, draw_leaves); +} diff --git a/intern/openvdb/openvdb_capi.h b/intern/openvdb/openvdb_capi.h new file mode 100644 index 00000000000..d0d4d80cb19 --- /dev/null +++ b/intern/openvdb/openvdb_capi.h @@ -0,0 +1,131 @@ +/* + * ***** 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 OpenVDBPrimitive; +struct OpenVDBVectorGrid; + +int OpenVDB_getVersionHex(void); + +typedef void (*OpenVDBGridInfoCallback)(void *userdata, const char *name, const char *value_type, bool is_color); +void OpenVDB_get_grid_info(const char *filename, OpenVDBGridInfoCallback cb, void *userdata); + +enum { + OPENVDB_NO_ERROR = 0, + OPENVDB_ARITHM_ERROR = 1, + OPENVDB_ILLEGAL_ERROR = 2, + OPENVDB_INDEX_ERROR = 3, + OPENVDB_IO_ERROR = 4, + OPENVDB_KEY_ERROR = 5, + OPENVDB_LOOKUP_ERROR = 6, + OPENVDB_IMPL_ERROR = 7, + OPENVDB_REF_ERROR = 8, + OPENVDB_TYPE_ERROR = 9, + OPENVDB_VALUE_ERROR = 10, + OPENVDB_UNKNOWN_ERROR = 11, +}; + +enum { + VEC_INVARIANT = 0, + VEC_COVARIANT = 1, + VEC_COVARIANT_NORMALIZE = 2, + VEC_CONTRAVARIANT_RELATIVE = 3, + VEC_CONTRAVARIANT_ABSOLUTE = 4, +}; + +void OpenVDB_update_fluid_transform(const char *filename, + float matrix[4][4], + float matrix_high[4][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); + +struct OpenVDBPrimitive *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_compression(struct OpenVDBWriter *writer, const int flag); +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]); + +void OpenVDB_draw_primitive(struct OpenVDBPrimitive *vdb_prim, + const bool draw_root, + const bool draw_level_1, + const bool draw_level_2, + const bool draw_leaves); + +#ifdef __cplusplus +} +#endif + +#endif /* __OPENVDB_CAPI_H__ */ diff --git a/intern/openvdb/openvdb_util.cpp b/intern/openvdb/openvdb_util.cpp new file mode 100644 index 00000000000..4caa62c6395 --- /dev/null +++ b/intern/openvdb/openvdb_util.cpp @@ -0,0 +1,80 @@ +/* + * ***** 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/Exceptions.h> + +#include "openvdb_capi.h" +#include "openvdb_util.h" + +void catch_exception(int &ret) +{ + try { + throw; + } + catch (const openvdb::ArithmeticError &e) { + std::cerr << e.what() << std::endl; + ret = OPENVDB_ARITHM_ERROR; + } + catch (const openvdb::IllegalValueException &e) { + std::cerr << e.what() << std::endl; + ret = OPENVDB_ILLEGAL_ERROR; + } + catch (const openvdb::IndexError &e) { + std::cerr << e.what() << std::endl; + ret = OPENVDB_INDEX_ERROR; + } + catch (const openvdb::IoError &e) { + std::cerr << e.what() << std::endl; + ret = OPENVDB_IO_ERROR; + } + catch (const openvdb::KeyError &e) { + std::cerr << e.what() << std::endl; + ret = OPENVDB_KEY_ERROR; + } + catch (const openvdb::LookupError &e) { + std::cerr << e.what() << std::endl; + ret = OPENVDB_LOOKUP_ERROR; + } + catch (const openvdb::NotImplementedError &e) { + std::cerr << e.what() << std::endl; + ret = OPENVDB_IMPL_ERROR; + } + catch (const openvdb::ReferenceError &e) { + std::cerr << e.what() << std::endl; + ret = OPENVDB_REF_ERROR; + } + catch (const openvdb::TypeError &e) { + std::cerr << e.what() << std::endl; + ret = OPENVDB_TYPE_ERROR; + } + catch (const openvdb::ValueError &e) { + std::cerr << e.what() << std::endl; + ret = OPENVDB_VALUE_ERROR; + } + catch (...) { + std::cerr << "Unknown error in OpenVDB library..." << std::endl; + ret = OPENVDB_UNKNOWN_ERROR; + } +} diff --git a/intern/openvdb/openvdb_util.h b/intern/openvdb/openvdb_util.h new file mode 100644 index 00000000000..56ed9ce412b --- /dev/null +++ b/intern/openvdb/openvdb_util.h @@ -0,0 +1,31 @@ +/* + * ***** 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__ + +void catch_exception(int &ret); + +#endif /* __OPENVDB_UTIL_H__ */ |