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