Merge branch 'master' into blender2.8
[blender.git] / intern / openvdb / openvdb_capi.cc
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
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.
8  *
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.
13  *
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.
17  *
18  * The Original Code is Copyright (C) 2015 Blender Foundation.
19  * All rights reserved.
20  *
21  * Contributor(s): Kevin Dietrich
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 #include "openvdb_capi.h"
27 #include "openvdb_dense_convert.h"
28 #include "openvdb_util.h"
29
30 struct OpenVDBFloatGrid { int unused; };
31 struct OpenVDBIntGrid { int unused; };
32 struct OpenVDBVectorGrid { int unused; };
33
34 int OpenVDB_getVersionHex()
35 {
36         return openvdb::OPENVDB_LIBRARY_VERSION;
37 }
38
39 OpenVDBFloatGrid *OpenVDB_export_grid_fl(
40         OpenVDBWriter *writer,
41         const char *name, float *data,
42         const int res[3], float matrix[4][4],
43         OpenVDBFloatGrid *mask)
44 {
45         Timer(__func__);
46
47         using openvdb::FloatGrid;
48
49         FloatGrid *mask_grid = reinterpret_cast<FloatGrid *>(mask);
50         FloatGrid *grid = internal::OpenVDB_export_grid<FloatGrid>(
51                 writer,
52                 name,
53                 data,
54                 res,
55                 matrix,
56                 mask_grid);
57
58         return reinterpret_cast<OpenVDBFloatGrid *>(grid);
59 }
60
61 OpenVDBIntGrid *OpenVDB_export_grid_ch(
62         OpenVDBWriter *writer,
63         const char *name, unsigned char *data,
64         const int res[3], float matrix[4][4],
65         OpenVDBFloatGrid *mask)
66 {
67         Timer(__func__);
68
69         using openvdb::FloatGrid;
70         using openvdb::Int32Grid;
71
72         FloatGrid *mask_grid = reinterpret_cast<FloatGrid *>(mask);
73         Int32Grid *grid = internal::OpenVDB_export_grid<Int32Grid>(
74                 writer,
75                 name,
76                 data,
77                 res,
78                 matrix,
79                 mask_grid);
80
81         return reinterpret_cast<OpenVDBIntGrid *>(grid);
82 }
83
84 OpenVDBVectorGrid *OpenVDB_export_grid_vec(
85         struct OpenVDBWriter *writer,
86         const char *name,
87         const float *data_x, const float *data_y, const float *data_z,
88         const int res[3], float matrix[4][4], short vec_type,
89         const bool is_color, OpenVDBFloatGrid *mask)
90 {
91         Timer(__func__);
92
93         using openvdb::GridBase;
94         using openvdb::FloatGrid;
95         using openvdb::VecType;
96
97         FloatGrid *mask_grid = reinterpret_cast<FloatGrid *>(mask);
98         GridBase *grid = internal::OpenVDB_export_vector_grid(
99                 writer,
100                 name,
101                 data_x,
102                 data_y,
103                 data_z,
104                 res,
105                 matrix,
106                 static_cast<VecType>(vec_type),
107                 is_color,
108                 mask_grid);
109
110         return reinterpret_cast<OpenVDBVectorGrid *>(grid);
111 }
112
113 void OpenVDB_import_grid_fl(
114         OpenVDBReader *reader,
115         const char *name, float **data,
116         const int res[3])
117 {
118         Timer(__func__);
119
120         internal::OpenVDB_import_grid<openvdb::FloatGrid>(reader, name, data, res);
121 }
122
123 void OpenVDB_import_grid_ch(
124         OpenVDBReader *reader,
125         const char *name, unsigned char **data,
126         const int res[3])
127 {
128         internal::OpenVDB_import_grid<openvdb::Int32Grid>(reader, name, data, res);
129 }
130
131 void OpenVDB_import_grid_vec(
132         struct OpenVDBReader *reader,
133         const char *name,
134         float **data_x, float **data_y, float **data_z,
135         const int res[3])
136 {
137         Timer(__func__);
138
139         internal::OpenVDB_import_grid_vector(reader, name, data_x, data_y, data_z, res);
140 }
141
142 OpenVDBWriter *OpenVDBWriter_create()
143 {
144         return new OpenVDBWriter();
145 }
146
147 void OpenVDBWriter_free(OpenVDBWriter *writer)
148 {
149         delete writer;
150 }
151
152 void OpenVDBWriter_set_flags(OpenVDBWriter *writer, const int flag, const bool half)
153 {
154         int compression_flags = openvdb::io::COMPRESS_ACTIVE_MASK;
155
156 #ifdef WITH_OPENVDB_BLOSC
157         if (flag == 0) {
158                 compression_flags |= openvdb::io::COMPRESS_BLOSC;
159         }
160         else
161 #endif
162         if (flag == 1) {
163                 compression_flags |= openvdb::io::COMPRESS_ZIP;
164         }
165         else {
166                 compression_flags = openvdb::io::COMPRESS_NONE;
167         }
168
169         writer->setFlags(compression_flags, half);
170 }
171
172 void OpenVDBWriter_add_meta_fl(OpenVDBWriter *writer, const char *name, const float value)
173 {
174         writer->insertFloatMeta(name, value);
175 }
176
177 void OpenVDBWriter_add_meta_int(OpenVDBWriter *writer, const char *name, const int value)
178 {
179         writer->insertIntMeta(name, value);
180 }
181
182 void OpenVDBWriter_add_meta_v3(OpenVDBWriter *writer, const char *name, const float value[3])
183 {
184         writer->insertVec3sMeta(name, value);
185 }
186
187 void OpenVDBWriter_add_meta_v3_int(OpenVDBWriter *writer, const char *name, const int value[3])
188 {
189         writer->insertVec3IMeta(name, value);
190 }
191
192 void OpenVDBWriter_add_meta_mat4(OpenVDBWriter *writer, const char *name, float value[4][4])
193 {
194         writer->insertMat4sMeta(name, value);
195 }
196
197 void OpenVDBWriter_write(OpenVDBWriter *writer, const char *filename)
198 {
199         writer->write(filename);
200 }
201
202 OpenVDBReader *OpenVDBReader_create()
203 {
204         return new OpenVDBReader();
205 }
206
207 void OpenVDBReader_free(OpenVDBReader *reader)
208 {
209         delete reader;
210 }
211
212 void OpenVDBReader_open(OpenVDBReader *reader, const char *filename)
213 {
214         reader->open(filename);
215 }
216
217 void OpenVDBReader_get_meta_fl(OpenVDBReader *reader, const char *name, float *value)
218 {
219         reader->floatMeta(name, *value);
220 }
221
222 void OpenVDBReader_get_meta_int(OpenVDBReader *reader, const char *name, int *value)
223 {
224         reader->intMeta(name, *value);
225 }
226
227 void OpenVDBReader_get_meta_v3(OpenVDBReader *reader, const char *name, float value[3])
228 {
229         reader->vec3sMeta(name, value);
230 }
231
232 void OpenVDBReader_get_meta_v3_int(OpenVDBReader *reader, const char *name, int value[3])
233 {
234         reader->vec3IMeta(name, value);
235 }
236
237 void OpenVDBReader_get_meta_mat4(OpenVDBReader *reader, const char *name, float value[4][4])
238 {
239         reader->mat4sMeta(name, value);
240 }