Blender  V2.93
openvdb_capi.cc
Go to the documentation of this file.
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_capi.h"
21 #include "openvdb_level_set.h"
22 #include "openvdb_transform.h"
23 #include "openvdb_util.h"
24 
26 {
27  return openvdb::OPENVDB_LIBRARY_VERSION;
28 }
29 
31 {
32  OpenVDBLevelSet *level_set = new OpenVDBLevelSet();
33  if (initGrid) {
34  openvdb::FloatGrid::Ptr grid = openvdb::FloatGrid::create();
35  grid->setGridClass(openvdb::GRID_LEVEL_SET);
36  if (xform) {
37  grid->setTransform(xform->get_transform());
38  }
39  level_set->set_grid(grid);
40  }
41  return level_set;
42 }
43 
45 {
46  return new OpenVDBTransform();
47 }
48 
50 {
51  delete transform;
52 }
53 
55 {
56  transform->create_linear_transform(voxel_size);
57 }
58 
60 {
61  delete level_set;
62 }
63 
65  const float *vertices,
66  const unsigned int *faces,
67  const unsigned int totvertices,
68  const unsigned int totfaces,
69  OpenVDBTransform *xform)
70 {
71  level_set->mesh_to_level_set(vertices, faces, totvertices, totfaces, xform->get_transform());
72 }
73 
75  const float *vertices,
76  const unsigned int *faces,
77  const unsigned int totvertices,
78  const unsigned int totfaces,
80 {
81  level_set->mesh_to_level_set(vertices, faces, totvertices, totfaces, transform->get_transform());
82 }
83 
86  const double isovalue,
87  const double adaptivity,
88  const bool relax_disoriented_triangles)
89 {
90  level_set->volume_to_mesh(mesh, isovalue, adaptivity, relax_disoriented_triangles);
91 }
92 
93 void OpenVDBLevelSet_filter(struct OpenVDBLevelSet *level_set,
94  OpenVDBLevelSet_FilterType filter_type,
95  int width,
96  float distance,
98 {
99  level_set->filter(filter_type, width, distance, bias);
100 }
101 
103  struct OpenVDBLevelSet *gridA,
104  struct OpenVDBLevelSet *gridB,
106 {
107  openvdb::FloatGrid::Ptr grid = out->CSG_operation_apply(
108  gridA->get_grid(), gridB->get_grid(), operation);
109  out->set_grid(grid);
110 }
111 
113  struct OpenVDBLevelSet *level_setB,
114  char sampler,
115  float isolevel)
116 {
117  openvdb::FloatGrid::Ptr sourceGrid = level_setA->get_grid();
118  openvdb::FloatGrid::Ptr targetGrid = level_setB->get_grid()->deepCopy();
119 
120  const openvdb::math::Transform &sourceXform = sourceGrid->transform(),
121  &targetXform = targetGrid->transform();
122 
123  // Compute a source grid to target grid transform.
124  // (For this example, we assume that both grids' transforms are linear,
125  // so that they can be represented as 4 x 4 matrices.)
126  openvdb::Mat4R xform = sourceXform.baseMap()->getAffineMap()->getMat4() *
127  targetXform.baseMap()->getAffineMap()->getMat4().inverse();
128 
129  // Create the transformer.
130  openvdb::tools::GridTransformer transformer(xform);
131 
132  switch (sampler) {
134  // Resample using nearest-neighbor interpolation.
135  transformer.transformGrid<openvdb::tools::PointSampler, openvdb::FloatGrid>(*sourceGrid,
136  *targetGrid);
137  // Prune the target tree for optimal sparsity.
138  targetGrid->tree().prune();
139  break;
140 
142  // Resample using trilinear interpolation.
143  transformer.transformGrid<openvdb::tools::BoxSampler, openvdb::FloatGrid>(*sourceGrid,
144  *targetGrid);
145  // Prune the target tree for optimal sparsity.
146  targetGrid->tree().prune();
147  break;
148 
150  // Resample using triquadratic interpolation.
151  transformer.transformGrid<openvdb::tools::QuadraticSampler, openvdb::FloatGrid>(*sourceGrid,
152  *targetGrid);
153  // Prune the target tree for optimal sparsity.
154  targetGrid->tree().prune();
155  break;
156 
158  break;
159  }
160 
161  targetGrid = openvdb::tools::levelSetRebuild(*targetGrid, isolevel, 1.0f);
162  openvdb::tools::pruneLevelSet(targetGrid->tree());
163 
164  OpenVDBLevelSet *level_set = OpenVDBLevelSet_create(false, NULL);
165  level_set->set_grid(targetGrid);
166 
167  return level_set;
168 }
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei width
SIMD_FORCE_INLINE btVector3 transform(const btVector3 &point) const
static char faces[256]
void OpenVDBTransform_free(OpenVDBTransform *transform)
Definition: openvdb_capi.cc:49
void OpenVDBLevelSet_volume_to_mesh(struct OpenVDBLevelSet *level_set, struct OpenVDBVolumeToMeshData *mesh, const double isovalue, const double adaptivity, const bool relax_disoriented_triangles)
Definition: openvdb_capi.cc:84
OpenVDBLevelSet * OpenVDBLevelSet_create(bool initGrid, OpenVDBTransform *xform)
Definition: openvdb_capi.cc:30
OpenVDBTransform * OpenVDBTransform_create()
Definition: openvdb_capi.cc:44
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, OpenVDBTransform *transform)
Definition: openvdb_capi.cc:74
void OpenVDBLevelSet_free(OpenVDBLevelSet *level_set)
Definition: openvdb_capi.cc:59
void OpenVDBLevelSet_filter(struct OpenVDBLevelSet *level_set, OpenVDBLevelSet_FilterType filter_type, int width, float distance, OpenVDBLevelSet_FilterBias bias)
Definition: openvdb_capi.cc:93
int OpenVDB_getVersionHex()
Definition: openvdb_capi.cc:25
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)
Definition: openvdb_capi.cc:64
OpenVDBLevelSet * OpenVDBLevelSet_transform_and_resample(struct OpenVDBLevelSet *level_setA, struct OpenVDBLevelSet *level_setB, char sampler, float isolevel)
void OpenVDBLevelSet_CSG_operation(struct OpenVDBLevelSet *out, struct OpenVDBLevelSet *gridA, struct OpenVDBLevelSet *gridB, OpenVDBLevelSet_CSGOperation operation)
void OpenVDBTransform_create_linear_transform(OpenVDBTransform *transform, double voxel_size)
Definition: openvdb_capi.cc:54
OpenVDBLevelSet_CSGOperation
Definition: openvdb_capi.h:48
OpenVDBLevelSet_FilterType
Definition: openvdb_capi.h:28
@ OPENVDB_LEVELSET_GRIDSAMPLER_QUADRATIC
Definition: openvdb_capi.h:58
@ OPENVDB_LEVELSET_GRIDSAMPLER_NONE
Definition: openvdb_capi.h:55
@ OPENVDB_LEVELSET_GRIDSAMPLER_BOX
Definition: openvdb_capi.h:57
@ OPENVDB_LEVELSET_GRIDSAMPLER_POINT
Definition: openvdb_capi.h:56
OpenVDBLevelSet_FilterBias
Definition: openvdb_capi.h:39
void set_grid(const openvdb::FloatGrid::Ptr &grid)
void volume_to_mesh(struct OpenVDBVolumeToMeshData *mesh, const double isovalue, const double adaptivity, const bool relax_disoriented_triangles)
const openvdb::FloatGrid::Ptr & get_grid()
void filter(OpenVDBLevelSet_FilterType filter_type, int width, float distance, OpenVDBLevelSet_FilterBias filter_bias)
openvdb::FloatGrid::Ptr CSG_operation_apply(const openvdb::FloatGrid::Ptr &gridA, const openvdb::FloatGrid::Ptr &gridB, OpenVDBLevelSet_CSGOperation operation)
void mesh_to_level_set(const float *vertices, const unsigned int *faces, const unsigned int totvertices, const unsigned int totfaces, const openvdb::math::Transform::Ptr &transform)
const openvdb::math::Transform::Ptr & get_transform()
ccl_device_inline float distance(const float2 &a, const float2 &b)
CCL_NAMESPACE_BEGIN struct Transform Transform