1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
|
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2015 Blender Foundation.
* All rights reserved.
*/
#include "openvdb_capi.h"
#include "openvdb_dense_convert.h"
#include "openvdb_util.h"
#include "openvdb_level_set.h"
#include "openvdb_transform.h"
struct OpenVDBIntGrid {
int unused;
};
struct OpenVDBVectorGrid {
int unused;
};
int OpenVDB_getVersionHex()
{
return openvdb::OPENVDB_LIBRARY_VERSION;
}
OpenVDBFloatGrid *OpenVDB_export_grid_fl(OpenVDBWriter *writer,
const char *name,
float *data,
const int res[3],
float matrix[4][4],
const float clipping,
OpenVDBFloatGrid *mask)
{
Timer(__func__);
using openvdb::FloatGrid;
FloatGrid *mask_grid = reinterpret_cast<FloatGrid *>(mask);
FloatGrid *grid = internal::OpenVDB_export_grid<FloatGrid>(
writer, name, data, res, matrix, clipping, mask_grid);
return reinterpret_cast<OpenVDBFloatGrid *>(grid);
}
OpenVDBIntGrid *OpenVDB_export_grid_ch(OpenVDBWriter *writer,
const char *name,
unsigned char *data,
const int res[3],
float matrix[4][4],
const float clipping,
OpenVDBFloatGrid *mask)
{
Timer(__func__);
using openvdb::FloatGrid;
using openvdb::Int32Grid;
FloatGrid *mask_grid = reinterpret_cast<FloatGrid *>(mask);
Int32Grid *grid = internal::OpenVDB_export_grid<Int32Grid>(
writer, name, data, res, matrix, clipping, mask_grid);
return reinterpret_cast<OpenVDBIntGrid *>(grid);
}
OpenVDBVectorGrid *OpenVDB_export_grid_vec(struct OpenVDBWriter *writer,
const char *name,
const float *data_x,
const float *data_y,
const float *data_z,
const int res[3],
float matrix[4][4],
short vec_type,
const float clipping,
const bool is_color,
OpenVDBFloatGrid *mask)
{
Timer(__func__);
using openvdb::FloatGrid;
using openvdb::GridBase;
using openvdb::VecType;
FloatGrid *mask_grid = reinterpret_cast<FloatGrid *>(mask);
GridBase *grid = internal::OpenVDB_export_vector_grid(writer,
name,
data_x,
data_y,
data_z,
res,
matrix,
static_cast<VecType>(vec_type),
is_color,
clipping,
mask_grid);
return reinterpret_cast<OpenVDBVectorGrid *>(grid);
}
void OpenVDB_import_grid_fl(OpenVDBReader *reader,
const char *name,
float **data,
const int res[3])
{
Timer(__func__);
internal::OpenVDB_import_grid<openvdb::FloatGrid>(reader, name, data, res);
}
void OpenVDB_import_grid_ch(OpenVDBReader *reader,
const char *name,
unsigned char **data,
const int res[3])
{
internal::OpenVDB_import_grid<openvdb::Int32Grid>(reader, name, data, res);
}
void OpenVDB_import_grid_vec(struct OpenVDBReader *reader,
const char *name,
float **data_x,
float **data_y,
float **data_z,
const int res[3])
{
Timer(__func__);
internal::OpenVDB_import_grid_vector(reader, name, data_x, data_y, data_z, res);
}
OpenVDBWriter *OpenVDBWriter_create()
{
return new OpenVDBWriter();
}
void OpenVDBWriter_free(OpenVDBWriter *writer)
{
delete writer;
}
void OpenVDBWriter_set_flags(OpenVDBWriter *writer, const int flag, const bool half)
{
int compression_flags = openvdb::io::COMPRESS_ACTIVE_MASK;
#ifdef WITH_OPENVDB_BLOSC
if (flag == 0) {
compression_flags |= openvdb::io::COMPRESS_BLOSC;
}
else
#endif
if (flag == 1) {
compression_flags |= openvdb::io::COMPRESS_ZIP;
}
else {
compression_flags = openvdb::io::COMPRESS_NONE;
}
writer->setFlags(compression_flags, half);
}
void OpenVDBWriter_add_meta_fl(OpenVDBWriter *writer, const char *name, const float value)
{
writer->insertFloatMeta(name, value);
}
void OpenVDBWriter_add_meta_int(OpenVDBWriter *writer, const char *name, const int value)
{
writer->insertIntMeta(name, value);
}
void OpenVDBWriter_add_meta_v3(OpenVDBWriter *writer, const char *name, const float value[3])
{
writer->insertVec3sMeta(name, value);
}
void OpenVDBWriter_add_meta_v3_int(OpenVDBWriter *writer, const char *name, const int value[3])
{
writer->insertVec3IMeta(name, value);
}
void OpenVDBWriter_add_meta_mat4(OpenVDBWriter *writer, const char *name, float value[4][4])
{
writer->insertMat4sMeta(name, value);
}
void OpenVDBWriter_write(OpenVDBWriter *writer, const char *filename)
{
writer->write(filename);
}
OpenVDBReader *OpenVDBReader_create()
{
return new OpenVDBReader();
}
void OpenVDBReader_free(OpenVDBReader *reader)
{
delete reader;
}
void OpenVDBReader_open(OpenVDBReader *reader, const char *filename)
{
reader->open(filename);
}
void OpenVDBReader_get_meta_fl(OpenVDBReader *reader, const char *name, float *value)
{
reader->floatMeta(name, *value);
}
void OpenVDBReader_get_meta_int(OpenVDBReader *reader, const char *name, int *value)
{
reader->intMeta(name, *value);
}
void OpenVDBReader_get_meta_v3(OpenVDBReader *reader, const char *name, float value[3])
{
reader->vec3sMeta(name, value);
}
void OpenVDBReader_get_meta_v3_int(OpenVDBReader *reader, const char *name, int value[3])
{
reader->vec3IMeta(name, value);
}
void OpenVDBReader_get_meta_mat4(OpenVDBReader *reader, const char *name, float value[4][4])
{
reader->mat4sMeta(name, value);
}
OpenVDBLevelSet *OpenVDBLevelSet_create(bool initGrid, OpenVDBTransform *xform)
{
OpenVDBLevelSet *level_set = new OpenVDBLevelSet();
if (initGrid) {
openvdb::FloatGrid::Ptr grid = openvdb::FloatGrid::create();
grid->setGridClass(openvdb::GRID_LEVEL_SET);
if (xform) {
grid->setTransform(xform->OpenVDB_transform_get_transform());
}
level_set->OpenVDB_level_set_set_grid(grid);
}
return level_set;
}
OpenVDBTransform *OpenVDBTransform_create()
{
return new OpenVDBTransform();
}
void OpenVDBTransform_free(OpenVDBTransform *transform)
{
delete transform;
}
void OpenVDBTransform_create_linear_transform(OpenVDBTransform *transform, double voxel_size)
{
transform->OpenVDB_transform_create_linear_transform(voxel_size);
}
void OpenVDBLevelSet_free(OpenVDBLevelSet *level_set)
{
delete level_set;
}
void OpenVDBLevelSet_mesh_to_level_set(struct OpenVDBLevelSet *level_set,
const float *vertices,
const unsigned int *faces,
const unsigned int totvertices,
const unsigned int totfaces,
OpenVDBTransform *xform)
{
level_set->OpenVDB_mesh_to_level_set(
vertices, faces, totvertices, totfaces, xform->OpenVDB_transform_get_transform());
}
void OpenVDBLevelSet_mesh_to_level_set_transform(struct OpenVDBLevelSet *level_set,
const float *vertices,
const unsigned int *faces,
const unsigned int totvertices,
const unsigned int totfaces,
struct OpenVDBTransform *transform)
{
level_set->OpenVDB_mesh_to_level_set(
vertices, faces, totvertices, totfaces, transform->OpenVDB_transform_get_transform());
}
void OpenVDBLevelSet_volume_to_mesh(struct OpenVDBLevelSet *level_set,
struct OpenVDBVolumeToMeshData *mesh,
const double isovalue,
const double adaptivity,
const bool relax_disoriented_triangles)
{
level_set->OpenVDB_volume_to_mesh(mesh, isovalue, adaptivity, relax_disoriented_triangles);
}
void OpenVDBLevelSet_filter(struct OpenVDBLevelSet *level_set,
OpenVDBLevelSet_FilterType filter_type,
int width,
int iterations,
OpenVDBLevelSet_FilterBias bias)
{
level_set->OpenVDB_level_set_filter(filter_type, width, iterations, bias);
}
void OpenVDBLevelSet_CSG_operation(struct OpenVDBLevelSet *out,
struct OpenVDBLevelSet *gridA,
struct OpenVDBLevelSet *gridB,
OpenVDBLevelSet_CSGOperation operation)
{
openvdb::FloatGrid::Ptr grid = out->OpenVDB_CSG_operation(
gridA->OpenVDB_level_set_get_grid(), gridB->OpenVDB_level_set_get_grid(), operation);
out->OpenVDB_level_set_set_grid(grid);
}
OpenVDBLevelSet *OpenVDBLevelSet_transform_and_resample(struct OpenVDBLevelSet *level_setA,
struct OpenVDBLevelSet *level_setB,
char sampler,
float isolevel)
{
openvdb::FloatGrid::Ptr sourceGrid = level_setA->OpenVDB_level_set_get_grid();
openvdb::FloatGrid::Ptr targetGrid = level_setB->OpenVDB_level_set_get_grid()->deepCopy();
const openvdb::math::Transform &sourceXform = sourceGrid->transform(),
&targetXform = targetGrid->transform();
// Compute a source grid to target grid transform.
// (For this example, we assume that both grids' transforms are linear,
// so that they can be represented as 4 x 4 matrices.)
openvdb::Mat4R xform = sourceXform.baseMap()->getAffineMap()->getMat4() *
targetXform.baseMap()->getAffineMap()->getMat4().inverse();
// Create the transformer.
openvdb::tools::GridTransformer transformer(xform);
switch (sampler) {
case OPENVDB_LEVELSET_GRIDSAMPLER_POINT:
// Resample using nearest-neighbor interpolation.
transformer.transformGrid<openvdb::tools::PointSampler, openvdb::FloatGrid>(*sourceGrid,
*targetGrid);
// Prune the target tree for optimal sparsity.
targetGrid->tree().prune();
break;
case OPENVDB_LEVELSET_GRIDSAMPLER_BOX:
// Resample using trilinear interpolation.
transformer.transformGrid<openvdb::tools::BoxSampler, openvdb::FloatGrid>(*sourceGrid,
*targetGrid);
// Prune the target tree for optimal sparsity.
targetGrid->tree().prune();
break;
case OPENVDB_LEVELSET_GRIDSAMPLER_QUADRATIC:
// Resample using triquadratic interpolation.
transformer.transformGrid<openvdb::tools::QuadraticSampler, openvdb::FloatGrid>(*sourceGrid,
*targetGrid);
// Prune the target tree for optimal sparsity.
targetGrid->tree().prune();
break;
case OPENVDB_LEVELSET_GRIDSAMPLER_NONE:
// targetGrid = sourceGrid->deepCopy();
break;
}
targetGrid = openvdb::tools::levelSetRebuild(*targetGrid, isolevel, 1.0f);
openvdb::tools::pruneLevelSet(targetGrid->tree());
OpenVDBLevelSet *level_set = OpenVDBLevelSet_create(false, NULL);
level_set->OpenVDB_level_set_set_grid(targetGrid);
return level_set;
}
|