Blender  V2.93
openvdb_level_set.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_level_set.h"
21 #include "MEM_guardedalloc.h"
22 #include "openvdb/tools/Composite.h"
23 #include "openvdb_capi.h"
24 #include "openvdb_util.h"
25 
27 {
29 }
30 
32 {
33 }
34 
35 void OpenVDBLevelSet::mesh_to_level_set(const float *vertices,
36  const unsigned int *faces,
37  const unsigned int totvertices,
38  const unsigned int totfaces,
39  const openvdb::math::Transform::Ptr &xform)
40 {
41  std::vector<openvdb::Vec3s> points(totvertices);
42  std::vector<openvdb::Vec3I> triangles(totfaces);
43  std::vector<openvdb::Vec4I> quads;
44 
45  for (unsigned int i = 0; i < totvertices; i++) {
46  points[i] = openvdb::Vec3s(vertices[i * 3], vertices[i * 3 + 1], vertices[i * 3 + 2]);
47  }
48 
49  for (unsigned int i = 0; i < totfaces; i++) {
50  triangles[i] = openvdb::Vec3I(faces[i * 3], faces[i * 3 + 1], faces[i * 3 + 2]);
51  }
52 
53  this->grid = openvdb::tools::meshToLevelSet<openvdb::FloatGrid>(
54  *xform, points, triangles, quads, 1);
55 }
56 
58  const double isovalue,
59  const double adaptivity,
60  const bool relax_disoriented_triangles)
61 {
62  std::vector<openvdb::Vec3s> out_points;
63  std::vector<openvdb::Vec4I> out_quads;
64  std::vector<openvdb::Vec3I> out_tris;
65  openvdb::tools::volumeToMesh<openvdb::FloatGrid>(*this->grid,
66  out_points,
67  out_tris,
68  out_quads,
69  isovalue,
70  adaptivity,
71  relax_disoriented_triangles);
72  mesh->vertices = (float *)MEM_malloc_arrayN(
73  out_points.size(), 3 * sizeof(float), "openvdb remesher out verts");
74  mesh->quads = (unsigned int *)MEM_malloc_arrayN(
75  out_quads.size(), 4 * sizeof(unsigned int), "openvdb remesh out quads");
76  mesh->triangles = NULL;
77  if (out_tris.size() > 0) {
78  mesh->triangles = (unsigned int *)MEM_malloc_arrayN(
79  out_tris.size(), 3 * sizeof(unsigned int), "openvdb remesh out tris");
80  }
81 
82  mesh->totvertices = out_points.size();
83  mesh->tottriangles = out_tris.size();
84  mesh->totquads = out_quads.size();
85 
86  for (size_t i = 0; i < out_points.size(); i++) {
87  mesh->vertices[i * 3] = out_points[i].x();
88  mesh->vertices[i * 3 + 1] = out_points[i].y();
89  mesh->vertices[i * 3 + 2] = out_points[i].z();
90  }
91 
92  for (size_t i = 0; i < out_quads.size(); i++) {
93  mesh->quads[i * 4] = out_quads[i].x();
94  mesh->quads[i * 4 + 1] = out_quads[i].y();
95  mesh->quads[i * 4 + 2] = out_quads[i].z();
96  mesh->quads[i * 4 + 3] = out_quads[i].w();
97  }
98 
99  for (size_t i = 0; i < out_tris.size(); i++) {
100  mesh->triangles[i * 3] = out_tris[i].x();
101  mesh->triangles[i * 3 + 1] = out_tris[i].y();
102  mesh->triangles[i * 3 + 2] = out_tris[i].z();
103  }
104 }
105 
107  int width,
108  float distance,
109  OpenVDBLevelSet_FilterBias filter_bias)
110 {
111 
112  if (!this->grid) {
113  return;
114  }
115 
116  if (this->grid->getGridClass() != openvdb::GRID_LEVEL_SET) {
117  return;
118  }
119 
120  openvdb::tools::LevelSetFilter<openvdb::FloatGrid> filter(*this->grid);
121  filter.setSpatialScheme((openvdb::math::BiasedGradientScheme)filter_bias);
122  switch (filter_type) {
124  filter.gaussian(width);
125  break;
127  filter.median(width);
128  break;
130  filter.mean(width);
131  break;
133  filter.meanCurvature();
134  break;
136  filter.laplacian();
137  break;
139  filter.offset(distance);
140  break;
142  filter.offset(distance);
143  break;
145  break;
146  }
147 }
148 openvdb::FloatGrid::Ptr OpenVDBLevelSet::CSG_operation_apply(
149  const openvdb::FloatGrid::Ptr &gridA,
150  const openvdb::FloatGrid::Ptr &gridB,
152 {
153  switch (operation) {
155  openvdb::tools::csgUnion(*gridA, *gridB);
156  break;
158  openvdb::tools::csgDifference(*gridA, *gridB);
159  break;
161  openvdb::tools::csgIntersection(*gridA, *gridB);
162  break;
163  }
164 
165  return gridA;
166 }
167 
168 const openvdb::FloatGrid::Ptr &OpenVDBLevelSet::get_grid()
169 {
170  return this->grid;
171 }
172 
173 void OpenVDBLevelSet::set_grid(const openvdb::FloatGrid::Ptr &grid)
174 {
175  this->grid = grid;
176 }
typedef float(TangentPoint)[2]
_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
Read Guarded memory(de)allocation.
void *(* MEM_malloc_arrayN)(size_t len, size_t size, const char *str)
Definition: mallocn.c:48
static char faces[256]
OpenVDBLevelSet_CSGOperation
Definition: openvdb_capi.h:48
@ OPENVDB_LEVELSET_CSG_UNION
Definition: openvdb_capi.h:49
@ OPENVDB_LEVELSET_CSG_INTERSECTION
Definition: openvdb_capi.h:51
@ OPENVDB_LEVELSET_CSG_DIFFERENCE
Definition: openvdb_capi.h:50
OpenVDBLevelSet_FilterType
Definition: openvdb_capi.h:28
@ OPENVDB_LEVELSET_FILTER_DILATE
Definition: openvdb_capi.h:35
@ OPENVDB_LEVELSET_FILTER_MEAN_CURVATURE
Definition: openvdb_capi.h:33
@ OPENVDB_LEVELSET_FILTER_GAUSSIAN
Definition: openvdb_capi.h:30
@ OPENVDB_LEVELSET_FILTER_ERODE
Definition: openvdb_capi.h:36
@ OPENVDB_LEVELSET_FILTER_LAPLACIAN
Definition: openvdb_capi.h:34
@ OPENVDB_LEVELSET_FILTER_MEAN
Definition: openvdb_capi.h:31
@ OPENVDB_LEVELSET_FILTER_NONE
Definition: openvdb_capi.h:29
@ OPENVDB_LEVELSET_FILTER_MEDIAN
Definition: openvdb_capi.h:32
OpenVDBLevelSet_FilterBias
Definition: openvdb_capi.h:39
float size[3]
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)
static void initialize(SubdivDisplacement *displacement)
ccl_device_inline float distance(const float2 &a, const float2 &b)