Blender  V3.3
subdiv_deform.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2019 Blender Foundation. All rights reserved. */
3 
8 #include "BKE_subdiv_deform.h"
9 
10 #include <string.h>
11 
12 #include "DNA_mesh_types.h"
13 #include "DNA_meshdata_types.h"
14 
15 #include "BLI_math_vector.h"
16 #include "BLI_utildefines.h"
17 
18 #include "BKE_customdata.h"
19 #include "BKE_subdiv.h"
20 #include "BKE_subdiv_eval.h"
21 #include "BKE_subdiv_foreach.h"
22 #include "BKE_subdiv_mesh.h"
23 
24 #include "MEM_guardedalloc.h"
25 
26 /* -------------------------------------------------------------------- */
30 typedef struct SubdivDeformContext {
31  const Mesh *coarse_mesh;
33 
35  int num_verts;
36 
37  /* Accumulated values.
38  *
39  * Averaging is happening for vertices which correspond to the coarse ones.
40  * This is needed for displacement.
41  *
42  * Displacement is being accumulated to a vertices coordinates, since those
43  * are not needed during traversal of face-vertices vertices. */
44  /* Per-subdivided vertex counter of averaged values. */
46 
49 
50 static void subdiv_mesh_prepare_accumulator(SubdivDeformContext *ctx, int num_vertices)
51 {
52  if (!ctx->have_displacement) {
53  return;
54  }
56  num_vertices, sizeof(*ctx->accumulated_counters), "subdiv accumulated counters");
57 }
58 
60 {
62 }
63 
66 /* -------------------------------------------------------------------- */
71  const int ptex_face_index,
72  const float u,
73  const float v,
74  int vertex_index)
75 {
76  Subdiv *subdiv = ctx->subdiv;
77  float dummy_P[3], dPdu[3], dPdv[3], D[3];
78  BKE_subdiv_eval_limit_point_and_derivatives(subdiv, ptex_face_index, u, v, dummy_P, dPdu, dPdv);
79  /* Accumulate displacement if needed. */
80  if (ctx->have_displacement) {
81  BKE_subdiv_eval_displacement(subdiv, ptex_face_index, u, v, dPdu, dPdv, D);
82  /* NOTE: The storage for vertex coordinates is coming from an external world, not necessarily
83  * initialized to zeroes. */
84  if (ctx->accumulated_counters[vertex_index] == 0) {
85  copy_v3_v3(ctx->vertex_cos[vertex_index], D);
86  }
87  else {
88  add_v3_v3(ctx->vertex_cos[vertex_index], D);
89  }
90  }
91  ++ctx->accumulated_counters[vertex_index];
92 }
93 
96 /* -------------------------------------------------------------------- */
100 static bool subdiv_mesh_topology_info(const SubdivForeachContext *foreach_context,
101  const int UNUSED(num_vertices),
102  const int UNUSED(num_edges),
103  const int UNUSED(num_loops),
104  const int UNUSED(num_polygons),
105  const int *UNUSED(subdiv_polygon_offset))
106 {
107  SubdivDeformContext *subdiv_context = foreach_context->user_data;
108  subdiv_mesh_prepare_accumulator(subdiv_context, subdiv_context->coarse_mesh->totvert);
109  return true;
110 }
111 
112 static void subdiv_mesh_vertex_every_corner(const SubdivForeachContext *foreach_context,
113  void *UNUSED(tls),
114  const int ptex_face_index,
115  const float u,
116  const float v,
117  const int coarse_vertex_index,
118  const int UNUSED(coarse_poly_index),
119  const int UNUSED(coarse_corner),
120  const int UNUSED(subdiv_vertex_index))
121 {
122  SubdivDeformContext *ctx = foreach_context->user_data;
123  subdiv_accumulate_vertex_displacement(ctx, ptex_face_index, u, v, coarse_vertex_index);
124 }
125 
126 static void subdiv_mesh_vertex_corner(const SubdivForeachContext *foreach_context,
127  void *UNUSED(tls),
128  const int ptex_face_index,
129  const float u,
130  const float v,
131  const int coarse_vertex_index,
132  const int UNUSED(coarse_poly_index),
133  const int UNUSED(coarse_corner),
134  const int UNUSED(subdiv_vertex_index))
135 {
136  SubdivDeformContext *ctx = foreach_context->user_data;
137  BLI_assert(coarse_vertex_index != ORIGINDEX_NONE);
138  BLI_assert(coarse_vertex_index < ctx->num_verts);
139  float inv_num_accumulated = 1.0f;
140  if (ctx->accumulated_counters != NULL) {
141  inv_num_accumulated = 1.0f / ctx->accumulated_counters[coarse_vertex_index];
142  }
143  /* Displacement is accumulated in subdiv vertex position.
144  * Needs to be backed up before copying data from original vertex. */
145  float D[3] = {0.0f, 0.0f, 0.0f};
146  float *vertex_co = ctx->vertex_cos[coarse_vertex_index];
147  if (ctx->have_displacement) {
148  copy_v3_v3(D, vertex_co);
149  mul_v3_fl(D, inv_num_accumulated);
150  }
151  /* Copy custom data and evaluate position. */
152  BKE_subdiv_eval_limit_point(ctx->subdiv, ptex_face_index, u, v, vertex_co);
153  /* Apply displacement. */
154  add_v3_v3(vertex_co, D);
155 }
156 
159 /* -------------------------------------------------------------------- */
163 static void setup_foreach_callbacks(const SubdivDeformContext *subdiv_context,
164  SubdivForeachContext *foreach_context)
165 {
166  memset(foreach_context, 0, sizeof(*foreach_context));
167  /* General information. */
168  foreach_context->topology_info = subdiv_mesh_topology_info;
169  /* Every boundary geometry. Used for displacement and normals averaging. */
170  if (subdiv_context->have_displacement) {
172  }
173  foreach_context->vertex_corner = subdiv_mesh_vertex_corner;
174 }
175 
178 /* -------------------------------------------------------------------- */
183  const struct Mesh *coarse_mesh,
184  float (*vertex_cos)[3],
185  int num_verts)
186 {
188  /* Make sure evaluator is up to date with possible new topology, and that
189  * is refined for the new positions of coarse vertices. */
191  subdiv, coarse_mesh, vertex_cos, SUBDIV_EVALUATOR_TYPE_CPU, NULL)) {
192  /* This could happen in two situations:
193  * - OpenSubdiv is disabled.
194  * - Something totally bad happened, and OpenSubdiv rejected our
195  * topology.
196  * In either way, we can't safely continue. */
197  if (coarse_mesh->totpoly) {
199  return;
200  }
201  }
202 
203  /* Initialize subdivision mesh creation context. */
204  SubdivDeformContext subdiv_context = {0};
205  subdiv_context.coarse_mesh = coarse_mesh;
206  subdiv_context.subdiv = subdiv;
207  subdiv_context.vertex_cos = vertex_cos;
208  subdiv_context.num_verts = num_verts;
209  subdiv_context.have_displacement = (subdiv->displacement_evaluator != NULL);
210 
211  SubdivForeachContext foreach_context;
212  setup_foreach_callbacks(&subdiv_context, &foreach_context);
213  foreach_context.user_data = &subdiv_context;
214 
215  /* Dummy mesh rasterization settings. */
216  SubdivToMeshSettings mesh_settings;
217  mesh_settings.resolution = 1;
218  mesh_settings.use_optimal_display = false;
219 
220  /* Multi-threaded traversal/evaluation. */
222  BKE_subdiv_foreach_subdiv_geometry(subdiv, &foreach_context, &mesh_settings, coarse_mesh);
224 
225  // BKE_mesh_validate(result, true, true);
227 
228  /* Free used memory. */
229  subdiv_mesh_context_free(&subdiv_context);
230 }
231 
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:78
@ SUBDIV_STATS_SUBDIV_TO_MESH
Definition: BKE_subdiv.h:77
void BKE_subdiv_stats_end(SubdivStats *stats, eSubdivStatsValue value)
Definition: subdiv_stats.c:31
void BKE_subdiv_stats_begin(SubdivStats *stats, eSubdivStatsValue value)
Definition: subdiv_stats.c:26
@ SUBDIV_EVALUATOR_TYPE_CPU
void BKE_subdiv_eval_displacement(struct Subdiv *subdiv, int ptex_face_index, float u, float v, const float dPdu[3], const float dPdv[3], float r_D[3])
Definition: subdiv_eval.c:350
void BKE_subdiv_eval_limit_point(struct Subdiv *subdiv, int ptex_face_index, float u, float v, float r_P[3])
Definition: subdiv_eval.c:280
bool BKE_subdiv_eval_begin_from_mesh(struct Subdiv *subdiv, const struct Mesh *mesh, const float(*coarse_vertex_cos)[3], eSubdivEvaluatorType evaluator_type, struct OpenSubdiv_EvaluatorCache *evaluator_cache)
void BKE_subdiv_eval_limit_point_and_derivatives(struct Subdiv *subdiv, int ptex_face_index, float u, float v, float r_P[3], float r_dPdu[3], float r_dPdv[3])
Definition: subdiv_eval.c:286
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:46
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:32
int totvert
int totpoly
float(* vertex_cos)[3]
Definition: subdiv_deform.c:34
const Mesh * coarse_mesh
Definition: subdiv_deform.c:31
SubdivForeachTopologyInformationCb topology_info
SubdivForeachVertexFromCornerCb vertex_corner
SubdivForeachVertexFromCornerCb vertex_every_corner
SubdivStats stats
Definition: BKE_subdiv.h:170
struct SubdivDisplacement * displacement_evaluator
Definition: BKE_subdiv.h:168
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), const int *UNUSED(subdiv_polygon_offset))
static void subdiv_mesh_prepare_accumulator(SubdivDeformContext *ctx, int num_vertices)
Definition: subdiv_deform.c:50
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:70
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:59
BLI_INLINE float D(const float *data, const int res[3], int x, int y, int z)
Definition: voxel.c:13