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 #include "openvdb_dense_convert.h"
30 #include <openvdb/tools/ValueTransformer.h> /* for tools::foreach */
34 openvdb::Mat4R convertMatrix(const float mat[4][4])
36 return openvdb::Mat4R(
37 mat[0][0], mat[0][1], mat[0][2], mat[0][3],
38 mat[1][0], mat[1][1], mat[1][2], mat[1][3],
39 mat[2][0], mat[2][1], mat[2][2], mat[2][3],
40 mat[3][0], mat[3][1], mat[3][2], mat[3][3]);
44 class MergeScalarGrids {
45 typedef openvdb::FloatTree ScalarTree;
47 openvdb::tree::ValueAccessor<const ScalarTree> m_acc_x, m_acc_y, m_acc_z;
50 MergeScalarGrids(const ScalarTree *x_tree, const ScalarTree *y_tree, const ScalarTree *z_tree)
56 MergeScalarGrids(const MergeScalarGrids &other)
57 : m_acc_x(other.m_acc_x)
58 , m_acc_y(other.m_acc_y)
59 , m_acc_z(other.m_acc_z)
62 void operator()(const openvdb::Vec3STree::ValueOnIter &it) const
64 using namespace openvdb;
66 const math::Coord xyz = it.getCoord();
67 float x = m_acc_x.getValue(xyz);
68 float y = m_acc_y.getValue(xyz);
69 float z = m_acc_z.getValue(xyz);
71 it.setValue(math::Vec3s(x, y, z));
75 openvdb::GridBase *OpenVDB_export_vector_grid(
76 OpenVDBWriter *writer,
77 const openvdb::Name &name,
78 const float *data_x, const float *data_y, const float *data_z,
80 float fluid_mat[4][4],
81 openvdb::VecType vec_type,
83 const openvdb::FloatGrid *mask)
85 using namespace openvdb;
87 math::CoordBBox bbox(Coord(0), Coord(res[0] - 1, res[1] - 1, res[2] - 1));
88 Mat4R mat = convertMatrix(fluid_mat);
89 math::Transform::Ptr transform = math::Transform::createLinearTransform(mat);
91 FloatGrid::Ptr grid[3];
93 grid[0] = FloatGrid::create(0.0f);
94 tools::Dense<const float, tools::LayoutXYZ> dense_grid_x(bbox, data_x);
95 tools::copyFromDense(dense_grid_x, grid[0]->tree(), TOLERANCE);
97 grid[1] = FloatGrid::create(0.0f);
98 tools::Dense<const float, tools::LayoutXYZ> dense_grid_y(bbox, data_y);
99 tools::copyFromDense(dense_grid_y, grid[1]->tree(), TOLERANCE);
101 grid[2] = FloatGrid::create(0.0f);
102 tools::Dense<const float, tools::LayoutXYZ> dense_grid_z(bbox, data_z);
103 tools::copyFromDense(dense_grid_z, grid[2]->tree(), TOLERANCE);
105 Vec3SGrid::Ptr vecgrid = Vec3SGrid::create(Vec3s(0.0f));
107 /* Activate voxels in the vector grid based on the scalar grids to ensure
108 * thread safety later on */
109 for (int i = 0; i < 3; ++i) {
110 vecgrid->tree().topologyUnion(grid[i]->tree());
113 MergeScalarGrids op(&(grid[0]->tree()), &(grid[1]->tree()), &(grid[2]->tree()));
114 tools::foreach(vecgrid->beginValueOn(), op, true, false);
116 vecgrid->setTransform(transform);
119 vecgrid = tools::clip(*vecgrid, *mask);
122 vecgrid->setName(name);
123 vecgrid->setIsInWorldSpace(false);
124 vecgrid->setVectorType(vec_type);
125 vecgrid->insertMeta("is_color", BoolMetadata(is_color));
126 vecgrid->setGridClass(GRID_STAGGERED);
128 writer->insert(vecgrid);
130 return vecgrid.get();
133 void OpenVDB_import_grid_vector(
134 OpenVDBReader *reader,
135 const openvdb::Name &name,
136 float **data_x, float **data_y, float **data_z,
139 using namespace openvdb;
141 if (!reader->hasGrid(name)) {
142 std::fprintf(stderr, "OpenVDB grid %s not found in file!\n", name.c_str());
143 memset(*data_x, 0, sizeof(float) * res[0] * res[1] * res[2]);
144 memset(*data_y, 0, sizeof(float) * res[0] * res[1] * res[2]);
145 memset(*data_z, 0, sizeof(float) * res[0] * res[1] * res[2]);
149 Vec3SGrid::Ptr vgrid = gridPtrCast<Vec3SGrid>(reader->getGrid(name));
150 Vec3SGrid::ConstAccessor acc = vgrid->getConstAccessor();
152 int &x = xyz[0], &y = xyz[1], &z = xyz[2];
155 for (z = 0; z < res[2]; ++z) {
156 for (y = 0; y < res[1]; ++y) {
157 for (x = 0; x < res[0]; ++x, ++index) {
158 math::Vec3s value = acc.getValue(xyz);
159 (*data_x)[index] = value.x();
160 (*data_y)[index] = value.y();
161 (*data_z)[index] = value.z();
167 } /* namespace internal */