2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2015 Blender Foundation.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): Kevin Dietrich
25 * ***** END GPL LICENSE BLOCK *****
28 #ifndef __OPENVDB_DENSE_CONVERT_H__
29 #define __OPENVDB_DENSE_CONVERT_H__
31 #include "openvdb_reader.h"
32 #include "openvdb_writer.h"
34 #include <openvdb/tools/Clip.h>
35 #include <openvdb/tools/Dense.h>
39 #define TOLERANCE 1e-3f
43 openvdb::Mat4R convertMatrix(const float mat[4][4]);
45 template <typename GridType, typename T>
46 GridType *OpenVDB_export_grid(
47 OpenVDBWriter *writer,
48 const openvdb::Name &name,
51 float fluid_mat[4][4],
52 const openvdb::FloatGrid *mask)
54 using namespace openvdb;
56 math::CoordBBox bbox(Coord(0), Coord(res[0] - 1, res[1] - 1, res[2] - 1));
57 Mat4R mat = convertMatrix(fluid_mat);
58 math::Transform::Ptr transform = math::Transform::createLinearTransform(mat);
60 typename GridType::Ptr grid = GridType::create(T(0));
62 tools::Dense<const T, openvdb::tools::LayoutXYZ> dense_grid(bbox, data);
63 tools::copyFromDense(dense_grid, grid->tree(), (T)TOLERANCE);
65 grid->setTransform(transform);
68 grid = tools::clip(*grid, *mask);
72 grid->setIsInWorldSpace(false);
73 grid->setVectorType(openvdb::VEC_INVARIANT);
80 template <typename GridType, typename T>
81 void OpenVDB_import_grid(
82 OpenVDBReader *reader,
83 const openvdb::Name &name,
87 using namespace openvdb;
89 if (!reader->hasGrid(name)) {
90 std::fprintf(stderr, "OpenVDB grid %s not found in file!\n", name.c_str());
91 memset(*data, 0, sizeof(T) * res[0] * res[1] * res[2]);
95 typename GridType::Ptr grid = gridPtrCast<GridType>(reader->getGrid(name));
96 typename GridType::ConstAccessor acc = grid->getConstAccessor();
99 int &x = xyz[0], &y = xyz[1], &z = xyz[2];
102 for (z = 0; z < res[2]; ++z) {
103 for (y = 0; y < res[1]; ++y) {
104 for (x = 0; x < res[0]; ++x, ++index) {
105 (*data)[index] = acc.getValue(xyz);
111 openvdb::GridBase *OpenVDB_export_vector_grid(
112 OpenVDBWriter *writer,
113 const openvdb::Name &name,
114 const float *data_x, const float *data_y, const float *data_z,
116 float fluid_mat[4][4],
117 openvdb::VecType vec_type,
119 const openvdb::FloatGrid *mask);
122 void OpenVDB_import_grid_vector(
123 OpenVDBReader *reader,
124 const openvdb::Name &name,
125 float **data_x, float **data_y, float **data_z,
128 } /* namespace internal */
130 #endif /* __OPENVDB_DENSE_CONVERT_H__ */