Blender  V2.93
subdiv_deform.c
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) 2019 by Blender Foundation.
17  * All rights reserved.
18  */
19 
24 #include "BKE_subdiv_deform.h"
25 
26 #include <string.h>
27 
28 #include "DNA_mesh_types.h"
29 #include "DNA_meshdata_types.h"
30 
31 #include "BLI_math_vector.h"
32 #include "BLI_utildefines.h"
33 
34 #include "BKE_customdata.h"
35 #include "BKE_subdiv.h"
36 #include "BKE_subdiv_eval.h"
37 #include "BKE_subdiv_foreach.h"
38 #include "BKE_subdiv_mesh.h"
39 
40 #include "MEM_guardedalloc.h"
41 
42 /* -------------------------------------------------------------------- */
46 typedef struct SubdivDeformContext {
47  const Mesh *coarse_mesh;
49 
51  int num_verts;
52 
53  /* Accumulated values.
54  *
55  * Averaging is happening for vertices which correspond to the coarse ones.
56  * This is needed for displacement.
57  *
58  * Displacement is being accumulated to a vertices coordinates, since those
59  * are not needed during traversal of face-vertices vertices. */
60  /* Per-subdivided vertex counter of averaged values. */
62 
65 
66 static void subdiv_mesh_prepare_accumulator(SubdivDeformContext *ctx, int num_vertices)
67 {
68  if (!ctx->have_displacement) {
69  return;
70  }
72  sizeof(*ctx->accumulated_counters), num_vertices, "subdiv accumulated counters");
73 }
74 
76 {
78 }
79 
82 /* -------------------------------------------------------------------- */
87  const int ptex_face_index,
88  const float u,
89  const float v,
90  int vertex_index)
91 {
92  Subdiv *subdiv = ctx->subdiv;
93  float dummy_P[3], dPdu[3], dPdv[3], D[3];
94  BKE_subdiv_eval_limit_point_and_derivatives(subdiv, ptex_face_index, u, v, dummy_P, dPdu, dPdv);
95  /* Accumulate displacement if needed. */
96  if (ctx->have_displacement) {
97  BKE_subdiv_eval_displacement(subdiv, ptex_face_index, u, v, dPdu, dPdv, D);
98  /* NOTE: The storage for vertex coordinates is coming from an external world, not necessarily
99  * initialized to zeroes. */
100  if (ctx->accumulated_counters[vertex_index] == 0) {
101  copy_v3_v3(ctx->vertex_cos[vertex_index], D);
102  }
103  else {
104  add_v3_v3(ctx->vertex_cos[vertex_index], D);
105  }
106  }
107  ++ctx->accumulated_counters[vertex_index];
108 }
109 
112 /* -------------------------------------------------------------------- */
116 static bool subdiv_mesh_topology_info(const SubdivForeachContext *foreach_context,
117  const int UNUSED(num_vertices),
118  const int UNUSED(num_edges),
119  const int UNUSED(num_loops),
120  const int UNUSED(num_polygons))
121 {
122  SubdivDeformContext *subdiv_context = foreach_context->user_data;
123  subdiv_mesh_prepare_accumulator(subdiv_context, subdiv_context->coarse_mesh->totvert);
124  return true;
125 }
126 
127 static void subdiv_mesh_vertex_every_corner(const SubdivForeachContext *foreach_context,
128  void *UNUSED(tls),
129  const int ptex_face_index,
130  const float u,
131  const float v,
132  const int coarse_vertex_index,
133  const int UNUSED(coarse_poly_index),
134  const int UNUSED(coarse_corner),
135  const int UNUSED(subdiv_vertex_index))
136 {
137  SubdivDeformContext *ctx = foreach_context->user_data;
138  subdiv_accumulate_vertex_displacement(ctx, ptex_face_index, u, v, coarse_vertex_index);
139 }
140 
141 static void subdiv_mesh_vertex_corner(const SubdivForeachContext *foreach_context,
142  void *UNUSED(tls),
143  const int ptex_face_index,
144  const float u,
145  const float v,
146  const int coarse_vertex_index,
147  const int UNUSED(coarse_poly_index),
148  const int UNUSED(coarse_corner),
149  const int UNUSED(subdiv_vertex_index))
150 {
151  SubdivDeformContext *ctx = foreach_context->user_data;
152  BLI_assert(coarse_vertex_index != ORIGINDEX_NONE);
153  BLI_assert(coarse_vertex_index < ctx->num_verts);
154  float inv_num_accumulated = 1.0f;
155  if (ctx->accumulated_counters != NULL) {
156  inv_num_accumulated = 1.0f / ctx->accumulated_counters[coarse_vertex_index];
157  }
158  /* Displacement is accumulated in subdiv vertex position.
159  * Needs to be backed up before copying data from original vertex. */
160  float D[3] = {0.0f, 0.0f, 0.0f};
161  float *vertex_co = ctx->vertex_cos[coarse_vertex_index];
162  if (ctx->have_displacement) {
163  copy_v3_v3(D, vertex_co);
164  mul_v3_fl(D, inv_num_accumulated);
165  }
166  /* Copy custom data and evaluate position. */
167  BKE_subdiv_eval_limit_point(ctx->subdiv, ptex_face_index, u, v, vertex_co);
168  /* Apply displacement. */
169  add_v3_v3(vertex_co, D);
170 }
171 
174 /* -------------------------------------------------------------------- */
178 static void setup_foreach_callbacks(const SubdivDeformContext *subdiv_context,
179  SubdivForeachContext *foreach_context)
180 {
181  memset(foreach_context, 0, sizeof(*foreach_context));
182  /* General information. */
183  foreach_context->topology_info = subdiv_mesh_topology_info;
184  /* Every boundary geometry. Used for displacement and normals averaging. */
185  if (subdiv_context->have_displacement) {
187  }
188  foreach_context->vertex_corner = subdiv_mesh_vertex_corner;
189 }
190 
193 /* -------------------------------------------------------------------- */
198  const struct Mesh *coarse_mesh,
199  float (*vertex_cos)[3],
200  int num_verts)
201 {
203  /* Make sure evaluator is up to date with possible new topology, and that
204  * is refined for the new positions of coarse vertices. */
205  if (!BKE_subdiv_eval_begin_from_mesh(subdiv, coarse_mesh, vertex_cos)) {
206  /* This could happen in two situations:
207  * - OpenSubdiv is disabled.
208  * - Something totally bad happened, and OpenSubdiv rejected our
209  * topology.
210  * In either way, we can't safely continue. */
211  if (coarse_mesh->totpoly) {
213  return;
214  }
215  }
216 
217  /* Initialize subdivision mesh creation context. */
218  SubdivDeformContext subdiv_context = {0};
219  subdiv_context.coarse_mesh = coarse_mesh;
220  subdiv_context.subdiv = subdiv;
221  subdiv_context.vertex_cos = vertex_cos;
222  subdiv_context.num_verts = num_verts;
223  subdiv_context.have_displacement = (subdiv->displacement_evaluator != NULL);
224 
225  SubdivForeachContext foreach_context;
226  setup_foreach_callbacks(&subdiv_context, &foreach_context);
227  foreach_context.user_data = &subdiv_context;
228 
229  /* Dummy mesh rasterization settings. */
230  SubdivToMeshSettings mesh_settings;
231  mesh_settings.resolution = 1;
232  mesh_settings.use_optimal_display = false;
233 
234  /* Multi-threaded traversal/evaluation. */
236  BKE_subdiv_foreach_subdiv_geometry(subdiv, &foreach_context, &mesh_settings, coarse_mesh);
238 
239  // BKE_mesh_validate(result, true, true);
241 
242  /* Free used memory. */
243  subdiv_mesh_context_free(&subdiv_context);
244 }
245 
typedef float(TangentPoint)[2]
CustomData interface, see also DNA_customdata_types.h.
#define ORIGINDEX_NONE
@ SUBDIV_STATS_SUBDIV_TO_MESH_GEOMETRY
Definition: BKE_subdiv.h:94
@ SUBDIV_STATS_SUBDIV_TO_MESH
Definition: BKE_subdiv.h:93
void BKE_subdiv_stats_end(SubdivStats *stats, eSubdivStatsValue value)
Definition: subdiv_stats.c:47
void BKE_subdiv_stats_begin(SubdivStats *stats, eSubdivStatsValue value)
Definition: subdiv_stats.c:42
void BKE_subdiv_eval_limit_point(struct Subdiv *subdiv, const int ptex_face_index, const float u, const float v, float r_P[3])
Definition: subdiv_eval.c:171
void BKE_subdiv_eval_displacement(struct Subdiv *subdiv, const int ptex_face_index, const float u, const float v, const float dPdu[3], const float dPdv[3], float r_D[3])
Definition: subdiv_eval.c:247
bool BKE_subdiv_eval_begin_from_mesh(struct Subdiv *subdiv, const struct Mesh *mesh, const float(*coarse_vertex_cos)[3])
void BKE_subdiv_eval_limit_point_and_derivatives(struct Subdiv *subdiv, const int ptex_face_index, const float u, const float v, float r_P[3], float r_dPdu[3], float r_dPdv[3])
Definition: subdiv_eval.c:177
bool BKE_subdiv_foreach_subdiv_geometry(struct Subdiv *subdiv, const struct SubdivForeachContext *context, const struct SubdivToMeshSettings *mesh_settings, const struct Mesh *coarse_mesh)
#define BLI_assert(a)
Definition: BLI_assert.h:58
MINLINE void mul_v3_fl(float r[3], float f)
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void add_v3_v3(float r[3], const float a[3])
#define UNUSED(x)
Read Guarded memory(de)allocation.
#define MEM_SAFE_FREE(v)
ATTR_WARN_UNUSED_RESULT const BMVert * v
void *(* MEM_calloc_arrayN)(size_t len, size_t size, const char *str)
Definition: mallocn.c:46
int totvert
int totpoly
float(* vertex_cos)[3]
Definition: subdiv_deform.c:50
const Mesh * coarse_mesh
Definition: subdiv_deform.c:47
SubdivForeachTopologyInformationCb topology_info
SubdivForeachVertexFromCornerCb vertex_corner
SubdivForeachVertexFromCornerCb vertex_every_corner
SubdivStats stats
Definition: BKE_subdiv.h:186
struct SubdivDisplacement * displacement_evaluator
Definition: BKE_subdiv.h:184
static void subdiv_mesh_vertex_every_corner(const SubdivForeachContext *foreach_context, void *UNUSED(tls), const int ptex_face_index, const float u, const float v, const int coarse_vertex_index, const int UNUSED(coarse_poly_index), const int UNUSED(coarse_corner), const int UNUSED(subdiv_vertex_index))
void BKE_subdiv_deform_coarse_vertices(struct Subdiv *subdiv, const struct Mesh *coarse_mesh, float(*vertex_cos)[3], int num_verts)
static void subdiv_mesh_vertex_corner(const SubdivForeachContext *foreach_context, void *UNUSED(tls), const int ptex_face_index, const float u, const float v, const int coarse_vertex_index, const int UNUSED(coarse_poly_index), const int UNUSED(coarse_corner), const int UNUSED(subdiv_vertex_index))
static bool subdiv_mesh_topology_info(const SubdivForeachContext *foreach_context, const int UNUSED(num_vertices), const int UNUSED(num_edges), const int UNUSED(num_loops), const int UNUSED(num_polygons))
static void subdiv_mesh_prepare_accumulator(SubdivDeformContext *ctx, int num_vertices)
Definition: subdiv_deform.c:66
static void subdiv_accumulate_vertex_displacement(SubdivDeformContext *ctx, const int ptex_face_index, const float u, const float v, int vertex_index)
Definition: subdiv_deform.c:86
struct SubdivDeformContext SubdivDeformContext
static void setup_foreach_callbacks(const SubdivDeformContext *subdiv_context, SubdivForeachContext *foreach_context)
static void subdiv_mesh_context_free(SubdivDeformContext *ctx)
Definition: subdiv_deform.c:75
BLI_INLINE float D(const float *data, const int res[3], int x, int y, int z)
Definition: voxel.c:29