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 * Contributor(s): Kevin Dietrich
23 * ***** END GPL LICENSE BLOCK *****
26 #include "openvdb_writer.h"
27 #include "openvdb_util.h"
29 OpenVDBWriter::OpenVDBWriter()
30 : m_grids(new openvdb::GridPtrVec())
31 , m_meta_map(new openvdb::MetaMap())
32 , m_save_as_half(false)
34 m_meta_map->insertMeta("creator", openvdb::StringMetadata("Blender/Smoke"));
37 OpenVDBWriter::~OpenVDBWriter()
40 void OpenVDBWriter::insert(const openvdb::GridBase::Ptr &grid)
42 grid->setSaveFloatAsHalf(m_save_as_half);
43 m_grids->push_back(grid);
46 void OpenVDBWriter::insert(const openvdb::GridBase &grid)
48 m_grids->push_back(grid.copyGrid());
51 void OpenVDBWriter::insertFloatMeta(const openvdb::Name &name, const float value)
54 m_meta_map->insertMeta(name, openvdb::FloatMetadata(value));
59 void OpenVDBWriter::insertIntMeta(const openvdb::Name &name, const int value)
62 m_meta_map->insertMeta(name, openvdb::Int32Metadata(value));
67 void OpenVDBWriter::insertVec3sMeta(const openvdb::Name &name, const openvdb::Vec3s &value)
70 m_meta_map->insertMeta(name, openvdb::Vec3SMetadata(value));
75 void OpenVDBWriter::insertVec3IMeta(const openvdb::Name &name, const openvdb::Vec3I &value)
78 m_meta_map->insertMeta(name, openvdb::Vec3IMetadata(value));
83 void OpenVDBWriter::insertMat4sMeta(const openvdb::Name &name, const float value[4][4])
85 openvdb::Mat4s mat = openvdb::Mat4s(
86 value[0][0], value[0][1], value[0][2], value[0][3],
87 value[1][0], value[1][1], value[1][2], value[1][3],
88 value[2][0], value[2][1], value[2][2], value[2][3],
89 value[3][0], value[3][1], value[3][2], value[3][3]);
92 m_meta_map->insertMeta(name, openvdb::Mat4SMetadata(mat));
97 void OpenVDBWriter::setFlags(const int compression, const bool save_as_half)
99 m_compression_flags = compression;
100 m_save_as_half = save_as_half;
103 void OpenVDBWriter::write(const openvdb::Name &filename) const
106 openvdb::io::File file(filename);
107 file.setCompression(m_compression_flags);
108 file.write(*m_grids, *m_meta_map);
111 /* Should perhaps be an option at some point */
114 /* Mostly to catch exceptions related to Blosc not being supported. */
115 catch (const openvdb::IoError &e) {
116 std::cerr << e.what() << '\n';