ClangFormat: apply to source, most of intern
[blender.git] / intern / openvdb / intern / openvdb_writer.cc
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2015 Blender Foundation.
17  * All rights reserved.
18  */
19
20 #include "openvdb_writer.h"
21 #include "openvdb_util.h"
22
23 OpenVDBWriter::OpenVDBWriter()
24     : m_grids(new openvdb::GridPtrVec()), m_meta_map(new openvdb::MetaMap()), m_save_as_half(false)
25 {
26   m_meta_map->insertMeta("creator", openvdb::StringMetadata("Blender/Smoke"));
27 }
28
29 OpenVDBWriter::~OpenVDBWriter()
30 {
31 }
32
33 void OpenVDBWriter::insert(const openvdb::GridBase::Ptr &grid)
34 {
35   grid->setSaveFloatAsHalf(m_save_as_half);
36   m_grids->push_back(grid);
37 }
38
39 void OpenVDBWriter::insert(const openvdb::GridBase &grid)
40 {
41 #if (OPENVDB_LIBRARY_MAJOR_VERSION_NUMBER <= 3) || defined(OPENVDB_3_ABI_COMPATIBLE)
42   m_grids->push_back(grid.copyGrid());
43 #else
44   m_grids->push_back(grid.copyGridWithNewTree());
45 #endif
46 }
47
48 void OpenVDBWriter::insertFloatMeta(const openvdb::Name &name, const float value)
49 {
50   try {
51     m_meta_map->insertMeta(name, openvdb::FloatMetadata(value));
52   }
53   CATCH_KEYERROR;
54 }
55
56 void OpenVDBWriter::insertIntMeta(const openvdb::Name &name, const int value)
57 {
58   try {
59     m_meta_map->insertMeta(name, openvdb::Int32Metadata(value));
60   }
61   CATCH_KEYERROR;
62 }
63
64 void OpenVDBWriter::insertVec3sMeta(const openvdb::Name &name, const openvdb::Vec3s &value)
65 {
66   try {
67     m_meta_map->insertMeta(name, openvdb::Vec3SMetadata(value));
68   }
69   CATCH_KEYERROR;
70 }
71
72 void OpenVDBWriter::insertVec3IMeta(const openvdb::Name &name, const openvdb::Vec3I &value)
73 {
74   try {
75     m_meta_map->insertMeta(name, openvdb::Vec3IMetadata(value));
76   }
77   CATCH_KEYERROR;
78 }
79
80 void OpenVDBWriter::insertMat4sMeta(const openvdb::Name &name, const float value[4][4])
81 {
82   openvdb::Mat4s mat = openvdb::Mat4s(value[0][0],
83                                       value[0][1],
84                                       value[0][2],
85                                       value[0][3],
86                                       value[1][0],
87                                       value[1][1],
88                                       value[1][2],
89                                       value[1][3],
90                                       value[2][0],
91                                       value[2][1],
92                                       value[2][2],
93                                       value[2][3],
94                                       value[3][0],
95                                       value[3][1],
96                                       value[3][2],
97                                       value[3][3]);
98
99   try {
100     m_meta_map->insertMeta(name, openvdb::Mat4SMetadata(mat));
101   }
102   CATCH_KEYERROR;
103 }
104
105 void OpenVDBWriter::setFlags(const int compression, const bool save_as_half)
106 {
107   m_compression_flags = compression;
108   m_save_as_half = save_as_half;
109 }
110
111 void OpenVDBWriter::write(const openvdb::Name &filename) const
112 {
113   try {
114     openvdb::io::File file(filename);
115     file.setCompression(m_compression_flags);
116     file.write(*m_grids, *m_meta_map);
117     file.close();
118
119     /* Should perhaps be an option at some point */
120     m_grids->clear();
121   }
122   /* Mostly to catch exceptions related to Blosc not being supported. */
123   catch (const openvdb::IoError &e) {
124     std::cerr << e.what() << '\n';
125   }
126 }