Blender  V3.3
MOD_remesh.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2011 by Nicholas Bishop. */
3 
8 #include "MEM_guardedalloc.h"
9 
10 #include "BLI_utildefines.h"
11 
12 #include "BLI_math_base.h"
13 #include "BLI_threads.h"
14 
15 #include "BLT_translation.h"
16 
17 #include "DNA_defaults.h"
18 #include "DNA_mesh_types.h"
19 #include "DNA_meshdata_types.h"
20 #include "DNA_modifier_types.h"
21 #include "DNA_object_types.h"
22 #include "DNA_screen_types.h"
23 
24 #include "BKE_context.h"
25 #include "BKE_mesh.h"
26 #include "BKE_mesh_remesh_voxel.h"
27 #include "BKE_mesh_runtime.h"
28 #include "BKE_screen.h"
29 
30 #include "UI_interface.h"
31 #include "UI_resources.h"
32 
33 #include "RNA_access.h"
34 #include "RNA_prototypes.h"
35 
36 #include "MOD_modifiertypes.h"
37 #include "MOD_ui_common.h"
38 
39 #include <stdlib.h>
40 #include <string.h>
41 
42 #ifdef WITH_MOD_REMESH
43 # include "BLI_math_vector.h"
44 
45 # include "dualcon.h"
46 #endif
47 
48 static void initData(ModifierData *md)
49 {
51 
53 
55 }
56 
57 #ifdef WITH_MOD_REMESH
58 
59 static void init_dualcon_mesh(DualConInput *input, Mesh *mesh)
60 {
61  memset(input, 0, sizeof(DualConInput));
62 
63  input->co = (void *)mesh->mvert;
64  input->co_stride = sizeof(MVert);
65  input->totco = mesh->totvert;
66 
67  input->mloop = (void *)mesh->mloop;
68  input->loop_stride = sizeof(MLoop);
69 
71  input->looptri = (void *)mesh->runtime.looptris.array;
72  input->tri_stride = sizeof(MLoopTri);
73  input->tottri = mesh->runtime.looptris.len;
74 
75  INIT_MINMAX(input->min, input->max);
76  BKE_mesh_minmax(mesh, input->min, input->max);
77 }
78 
79 /* simple structure to hold the output: a CDDM and two counters to
80  * keep track of the current elements */
81 typedef struct {
82  Mesh *mesh;
83  int curvert, curface;
84 } DualConOutput;
85 
86 /* allocate and initialize a DualConOutput */
87 static void *dualcon_alloc_output(int totvert, int totquad)
88 {
89  DualConOutput *output;
90 
91  if (!(output = MEM_callocN(sizeof(DualConOutput), "DualConOutput"))) {
92  return NULL;
93  }
94 
95  output->mesh = BKE_mesh_new_nomain(totvert, 0, 0, 4 * totquad, totquad);
96  return output;
97 }
98 
99 static void dualcon_add_vert(void *output_v, const float co[3])
100 {
101  DualConOutput *output = output_v;
102  Mesh *mesh = output->mesh;
103 
104  BLI_assert(output->curvert < mesh->totvert);
105 
106  copy_v3_v3(mesh->mvert[output->curvert].co, co);
107  output->curvert++;
108 }
109 
110 static void dualcon_add_quad(void *output_v, const int vert_indices[4])
111 {
112  DualConOutput *output = output_v;
113  Mesh *mesh = output->mesh;
114  MLoop *mloop;
115  MPoly *cur_poly;
116  int i;
117 
118  BLI_assert(output->curface < mesh->totpoly);
119 
120  mloop = mesh->mloop;
121  cur_poly = &mesh->mpoly[output->curface];
122 
123  cur_poly->loopstart = output->curface * 4;
124  cur_poly->totloop = 4;
125  for (i = 0; i < 4; i++) {
126  mloop[output->curface * 4 + i].v = vert_indices[i];
127  }
128 
129  output->curface++;
130 }
131 
132 static Mesh *modifyMesh(ModifierData *md, const ModifierEvalContext *UNUSED(ctx), Mesh *mesh)
133 {
134  RemeshModifierData *rmd;
135  DualConOutput *output;
137  Mesh *result;
138  DualConFlags flags = 0;
139  DualConMode mode = 0;
140 
141  rmd = (RemeshModifierData *)md;
142 
143  if (rmd->mode == MOD_REMESH_VOXEL) {
144  /* OpenVDB modes. */
145  if (rmd->voxel_size == 0.0f) {
146  return NULL;
147  }
149  if (result == NULL) {
150  return NULL;
151  }
152  }
153  else {
154  /* Dualcon modes. */
155  init_dualcon_mesh(&input, mesh);
156 
157  if (rmd->flag & MOD_REMESH_FLOOD_FILL) {
158  flags |= DUALCON_FLOOD_FILL;
159  }
160 
161  switch (rmd->mode) {
162  case MOD_REMESH_CENTROID:
163  mode = DUALCON_CENTROID;
164  break;
166  mode = DUALCON_MASS_POINT;
167  break;
169  mode = DUALCON_SHARP_FEATURES;
170  break;
171  case MOD_REMESH_VOXEL:
172  /* Should have been processed before as an OpenVDB operation. */
173  BLI_assert(false);
174  break;
175  }
176  /* TODO(jbakker): Dualcon crashes when run in parallel. Could be related to incorrect
177  * input data or that the library isn't thread safe.
178  * This was identified when changing the task isolation's during T76553. */
179  static ThreadMutex dualcon_mutex = BLI_MUTEX_INITIALIZER;
180  BLI_mutex_lock(&dualcon_mutex);
181  output = dualcon(&input,
182  dualcon_alloc_output,
183  dualcon_add_vert,
184  dualcon_add_quad,
185  flags,
186  mode,
187  rmd->threshold,
188  rmd->hermite_num,
189  rmd->scale,
190  rmd->depth);
191  BLI_mutex_unlock(&dualcon_mutex);
192 
193  result = output->mesh;
194  MEM_freeN(output);
195  }
196 
197  if (rmd->flag & MOD_REMESH_SMOOTH_SHADING) {
198  MPoly *mpoly = result->mpoly;
199  int i, totpoly = result->totpoly;
200 
201  /* Apply smooth shading to output faces */
202  for (i = 0; i < totpoly; i++) {
203  mpoly[i].flag |= ME_SMOOTH;
204  }
205  }
206 
208  BKE_mesh_calc_edges(result, true, false);
209  return result;
210 }
211 
212 #else /* !WITH_MOD_REMESH */
213 
215  const ModifierEvalContext *UNUSED(ctx),
216  Mesh *mesh)
217 {
218  return mesh;
219 }
220 
221 #endif /* !WITH_MOD_REMESH */
222 
223 static void panel_draw(const bContext *UNUSED(C), Panel *panel)
224 {
225  uiLayout *layout = panel->layout;
226 #ifdef WITH_MOD_REMESH
227  uiLayout *row, *col;
228 
229  PointerRNA ob_ptr;
231 
232  int mode = RNA_enum_get(ptr, "mode");
233 
234  uiItemR(layout, ptr, "mode", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
235 
236  uiLayoutSetPropSep(layout, true);
237 
238  col = uiLayoutColumn(layout, false);
239  if (mode == MOD_REMESH_VOXEL) {
240  uiItemR(col, ptr, "voxel_size", 0, NULL, ICON_NONE);
241  uiItemR(col, ptr, "adaptivity", 0, NULL, ICON_NONE);
242  }
243  else {
244  uiItemR(col, ptr, "octree_depth", 0, NULL, ICON_NONE);
245  uiItemR(col, ptr, "scale", 0, NULL, ICON_NONE);
246 
247  if (mode == MOD_REMESH_SHARP_FEATURES) {
248  uiItemR(col, ptr, "sharpness", 0, NULL, ICON_NONE);
249  }
250 
251  uiItemR(layout, ptr, "use_remove_disconnected", 0, NULL, ICON_NONE);
252  row = uiLayoutRow(layout, false);
253  uiLayoutSetActive(row, RNA_boolean_get(ptr, "use_remove_disconnected"));
254  uiItemR(layout, ptr, "threshold", 0, NULL, ICON_NONE);
255  }
256  uiItemR(layout, ptr, "use_smooth_shade", 0, NULL, ICON_NONE);
257 
258  modifier_panel_end(layout, ptr);
259 
260 #else /* WITH_MOD_REMESH */
261  uiItemL(layout, TIP_("Built without Remesh modifier"), ICON_NONE);
262 #endif /* WITH_MOD_REMESH */
263 }
264 
265 static void panelRegister(ARegionType *region_type)
266 {
268 }
269 
271  /* name */ N_("Remesh"),
272  /* structName */ "RemeshModifierData",
273  /* structSize */ sizeof(RemeshModifierData),
274  /* srna */ &RNA_RemeshModifier,
278  /* icon */ ICON_MOD_REMESH,
279 
280  /* copyData */ BKE_modifier_copydata_generic,
281 
282  /* deformVerts */ NULL,
283  /* deformMatrices */ NULL,
284  /* deformVertsEM */ NULL,
285  /* deformMatricesEM */ NULL,
286  /* modifyMesh */ modifyMesh,
287  /* modifyGeometrySet */ NULL,
288 
289  /* initData */ initData,
290  /* requiredDataMask */ NULL,
291  /* freeData */ NULL,
292  /* isDisabled */ NULL,
293  /* updateDepsgraph */ NULL,
294  /* dependsOnTime */ NULL,
295  /* dependsOnNormals */ NULL,
296  /* foreachIDLink */ NULL,
297  /* foreachTexLink */ NULL,
298  /* freeRuntimeData */ NULL,
299  /* panelRegister */ panelRegister,
300  /* blendWrite */ NULL,
301  /* blendRead */ NULL,
302 };
void BKE_mesh_copy_parameters_for_eval(struct Mesh *me_dst, const struct Mesh *me_src)
struct Mesh * BKE_mesh_new_nomain(int verts_len, int edges_len, int tessface_len, int loops_len, int polys_len)
Definition: mesh.cc:991
bool BKE_mesh_minmax(const struct Mesh *me, float r_min[3], float r_max[3])
void BKE_mesh_calc_edges(struct Mesh *mesh, bool keep_existing_edges, bool select_new_edges)
struct Mesh * BKE_mesh_remesh_voxel(const struct Mesh *mesh, float voxel_size, float adaptivity, float isovalue)
const struct MLoopTri * BKE_mesh_runtime_looptri_ensure(const struct Mesh *mesh)
@ eModifierTypeFlag_AcceptsCVs
Definition: BKE_modifier.h:67
@ eModifierTypeFlag_SupportsEditmode
Definition: BKE_modifier.h:69
@ eModifierTypeFlag_AcceptsMesh
Definition: BKE_modifier.h:66
void BKE_modifier_copydata_generic(const struct ModifierData *md, struct ModifierData *md_dst, int flag)
@ eModifierTypeType_Nonconstructive
Definition: BKE_modifier.h:49
#define BLI_assert(a)
Definition: BLI_assert.h:46
MINLINE void copy_v3_v3(float r[3], const float a[3])
#define BLI_MUTEX_INITIALIZER
Definition: BLI_threads.h:83
void BLI_mutex_lock(ThreadMutex *mutex)
Definition: threads.cc:373
void BLI_mutex_unlock(ThreadMutex *mutex)
Definition: threads.cc:378
pthread_mutex_t ThreadMutex
Definition: BLI_threads.h:82
#define INIT_MINMAX(min, max)
#define UNUSED(x)
#define MEMCMP_STRUCT_AFTER_IS_ZERO(struct_var, member)
#define MEMCPY_STRUCT_AFTER(struct_dst, struct_src, member)
#define TIP_(msgid)
#define DNA_struct_default_get(struct_name)
Definition: DNA_defaults.h:29
@ ME_SMOOTH
@ MOD_REMESH_FLOOD_FILL
@ MOD_REMESH_SMOOTH_SHADING
@ MOD_REMESH_VOXEL
@ MOD_REMESH_MASS_POINT
@ MOD_REMESH_SHARP_FEATURES
@ MOD_REMESH_CENTROID
@ eModifierType_Remesh
struct RemeshModifierData RemeshModifierData
Object is a sort of wrapper for general info.
Read Guarded memory(de)allocation.
static void panel_draw(const bContext *UNUSED(C), Panel *panel)
Definition: MOD_remesh.c:223
static void initData(ModifierData *md)
Definition: MOD_remesh.c:48
static void panelRegister(ARegionType *region_type)
Definition: MOD_remesh.c:265
static Mesh * modifyMesh(ModifierData *UNUSED(md), const ModifierEvalContext *UNUSED(ctx), Mesh *mesh)
Definition: MOD_remesh.c:214
ModifierTypeInfo modifierType_Remesh
Definition: MOD_remesh.c:270
PointerRNA * modifier_panel_get_property_pointers(Panel *panel, PointerRNA *r_ob_ptr)
void modifier_panel_end(uiLayout *layout, PointerRNA *ptr)
Definition: MOD_ui_common.c:91
PanelType * modifier_panel_register(ARegionType *region_type, ModifierType type, PanelDrawFn draw)
#define C
Definition: RandGen.cpp:25
void uiLayoutSetActive(uiLayout *layout, bool active)
uiLayout * uiLayoutColumn(uiLayout *layout, bool align)
void uiItemL(uiLayout *layout, const char *name, int icon)
void uiLayoutSetPropSep(uiLayout *layout, bool is_sep)
uiLayout * uiLayoutRow(uiLayout *layout, bool align)
@ UI_ITEM_R_EXPAND
void uiItemR(uiLayout *layout, struct PointerRNA *ptr, const char *propname, int flag, const char *name, int icon)
DualConMode
Definition: dualcon.h:47
@ DUALCON_SHARP_FEATURES
Definition: dualcon.h:53
@ DUALCON_CENTROID
Definition: dualcon.h:49
@ DUALCON_MASS_POINT
Definition: dualcon.h:51
void * dualcon(const DualConInput *input_mesh, DualConAllocOutput alloc_output, DualConAddVert add_vert, DualConAddQuad add_quad, DualConFlags flags, DualConMode mode, float threshold, float hermite_num, float scale, int depth)
DualConFlags
Definition: dualcon.h:43
@ DUALCON_FLOOD_FILL
Definition: dualcon.h:44
uint col
ccl_global KernelShaderEvalInput ccl_global float * output
ccl_global KernelShaderEvalInput * input
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:31
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:4863
int RNA_enum_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:5004
struct MLoopTri * array
unsigned int v
float co[3]
struct MLoopTri_Store looptris
struct MVert * mvert
int totvert
struct MLoop * mloop
Mesh_Runtime runtime
int totpoly
struct MPoly * mpoly
struct uiLayout * layout
#define N_(msgid)
PointerRNA * ptr
Definition: wm_files.c:3480