Blender  V2.93
MOD_build.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) 2005 by the Blender Foundation.
17  * All rights reserved.
18  */
19 
24 #include "MEM_guardedalloc.h"
25 
26 #include "BLI_utildefines.h"
27 
28 #include "BLI_ghash.h"
29 #include "BLI_math_vector.h"
30 #include "BLI_rand.h"
31 
32 #include "DNA_defaults.h"
33 #include "DNA_mesh_types.h"
34 #include "DNA_meshdata_types.h"
35 #include "DNA_object_types.h"
36 #include "DNA_screen_types.h"
37 
38 #include "DEG_depsgraph_query.h"
39 
40 #include "BKE_context.h"
41 #include "BKE_mesh.h"
42 #include "BKE_modifier.h"
43 #include "BKE_particle.h"
44 #include "BKE_scene.h"
45 #include "BKE_screen.h"
46 
47 #include "UI_interface.h"
48 #include "UI_resources.h"
49 
50 #include "RNA_access.h"
51 
52 #include "MOD_modifiertypes.h"
53 #include "MOD_ui_common.h"
54 
55 static void initData(ModifierData *md)
56 {
58 
60 
62 }
63 
65 {
66  return true;
67 }
68 
69 static Mesh *modifyMesh(ModifierData *md, const ModifierEvalContext *ctx, struct Mesh *mesh)
70 {
71  Mesh *result;
73  int i, j, k;
74  int numFaces_dst, numEdges_dst, numLoops_dst = 0;
75  int *vertMap, *edgeMap, *faceMap;
76  float frac;
77  MPoly *mpoly_dst;
78  MLoop *ml_dst, *ml_src /*, *mloop_dst */;
79  GHashIterator gh_iter;
80  /* maps vert indices in old mesh to indices in new mesh */
81  GHash *vertHash = BLI_ghash_int_new("build ve apply gh");
82  /* maps edge indices in new mesh to indices in old mesh */
83  GHash *edgeHash = BLI_ghash_int_new("build ed apply gh");
84  /* maps edge indices in old mesh to indices in new mesh */
85  GHash *edgeHash2 = BLI_ghash_int_new("build ed apply gh");
86 
87  const int numVert_src = mesh->totvert;
88  const int numEdge_src = mesh->totedge;
89  const int numPoly_src = mesh->totpoly;
90  MPoly *mpoly_src = mesh->mpoly;
91  MLoop *mloop_src = mesh->mloop;
92  MEdge *medge_src = mesh->medge;
93  MVert *mvert_src = mesh->mvert;
94 
95  vertMap = MEM_malloc_arrayN(numVert_src, sizeof(*vertMap), "build modifier vertMap");
96  edgeMap = MEM_malloc_arrayN(numEdge_src, sizeof(*edgeMap), "build modifier edgeMap");
97  faceMap = MEM_malloc_arrayN(numPoly_src, sizeof(*faceMap), "build modifier faceMap");
98 
99  range_vn_i(vertMap, numVert_src, 0);
100  range_vn_i(edgeMap, numEdge_src, 0);
101  range_vn_i(faceMap, numPoly_src, 0);
102 
103  struct Scene *scene = DEG_get_input_scene(ctx->depsgraph);
104  frac = (BKE_scene_frame_get(scene) - bmd->start) / bmd->length;
105  CLAMP(frac, 0.0f, 1.0f);
106  if (bmd->flag & MOD_BUILD_FLAG_REVERSE) {
107  frac = 1.0f - frac;
108  }
109 
110  numFaces_dst = numPoly_src * frac;
111  numEdges_dst = numEdge_src * frac;
112 
113  /* if there's at least one face, build based on faces */
114  if (numFaces_dst) {
115  MPoly *mpoly, *mp;
116  MLoop *ml, *mloop;
117  uintptr_t hash_num, hash_num_alt;
118 
119  if (bmd->flag & MOD_BUILD_FLAG_RANDOMIZE) {
120  BLI_array_randomize(faceMap, sizeof(*faceMap), numPoly_src, bmd->seed);
121  }
122 
123  /* get the set of all vert indices that will be in the final mesh,
124  * mapped to the new indices
125  */
126  mpoly = mpoly_src;
127  mloop = mloop_src;
128  hash_num = 0;
129  for (i = 0; i < numFaces_dst; i++) {
130  mp = mpoly + faceMap[i];
131  ml = mloop + mp->loopstart;
132 
133  for (j = 0; j < mp->totloop; j++, ml++) {
134  void **val_p;
135  if (!BLI_ghash_ensure_p(vertHash, POINTER_FROM_INT(ml->v), &val_p)) {
136  *val_p = (void *)hash_num;
137  hash_num++;
138  }
139  }
140 
141  numLoops_dst += mp->totloop;
142  }
143  BLI_assert(hash_num == BLI_ghash_len(vertHash));
144 
145  /* get the set of edges that will be in the new mesh (i.e. all edges
146  * that have both verts in the new mesh)
147  */
148  hash_num = 0;
149  hash_num_alt = 0;
150  for (i = 0; i < numEdge_src; i++, hash_num_alt++) {
151  MEdge *me = medge_src + i;
152 
153  if (BLI_ghash_haskey(vertHash, POINTER_FROM_INT(me->v1)) &&
154  BLI_ghash_haskey(vertHash, POINTER_FROM_INT(me->v2))) {
155  BLI_ghash_insert(edgeHash, (void *)hash_num, (void *)hash_num_alt);
156  BLI_ghash_insert(edgeHash2, (void *)hash_num_alt, (void *)hash_num);
157  hash_num++;
158  }
159  }
160  BLI_assert(hash_num == BLI_ghash_len(edgeHash));
161  }
162  else if (numEdges_dst) {
163  MEdge *medge, *me;
164  uintptr_t hash_num;
165 
166  if (bmd->flag & MOD_BUILD_FLAG_RANDOMIZE) {
167  BLI_array_randomize(edgeMap, sizeof(*edgeMap), numEdge_src, bmd->seed);
168  }
169 
170  /* get the set of all vert indices that will be in the final mesh,
171  * mapped to the new indices
172  */
173  medge = medge_src;
174  hash_num = 0;
175  BLI_assert(hash_num == BLI_ghash_len(vertHash));
176  for (i = 0; i < numEdges_dst; i++) {
177  void **val_p;
178  me = medge + edgeMap[i];
179 
180  if (!BLI_ghash_ensure_p(vertHash, POINTER_FROM_INT(me->v1), &val_p)) {
181  *val_p = (void *)hash_num;
182  hash_num++;
183  }
184  if (!BLI_ghash_ensure_p(vertHash, POINTER_FROM_INT(me->v2), &val_p)) {
185  *val_p = (void *)hash_num;
186  hash_num++;
187  }
188  }
189  BLI_assert(hash_num == BLI_ghash_len(vertHash));
190 
191  /* get the set of edges that will be in the new mesh */
192  for (i = 0; i < numEdges_dst; i++) {
193  j = BLI_ghash_len(edgeHash);
194 
195  BLI_ghash_insert(edgeHash, POINTER_FROM_INT(j), POINTER_FROM_INT(edgeMap[i]));
196  BLI_ghash_insert(edgeHash2, POINTER_FROM_INT(edgeMap[i]), POINTER_FROM_INT(j));
197  }
198  }
199  else {
200  int numVerts = numVert_src * frac;
201 
202  if (bmd->flag & MOD_BUILD_FLAG_RANDOMIZE) {
203  BLI_array_randomize(vertMap, sizeof(*vertMap), numVert_src, bmd->seed);
204  }
205 
206  /* get the set of all vert indices that will be in the final mesh,
207  * mapped to the new indices
208  */
209  for (i = 0; i < numVerts; i++) {
210  BLI_ghash_insert(vertHash, POINTER_FROM_INT(vertMap[i]), POINTER_FROM_INT(i));
211  }
212  }
213 
214  /* now we know the number of verts, edges and faces, we can create the mesh. */
216  mesh, BLI_ghash_len(vertHash), BLI_ghash_len(edgeHash), 0, numLoops_dst, numFaces_dst);
217 
218  /* copy the vertices across */
219  GHASH_ITER (gh_iter, vertHash) {
220  MVert source;
221  MVert *dest;
222  int oldIndex = POINTER_AS_INT(BLI_ghashIterator_getKey(&gh_iter));
223  int newIndex = POINTER_AS_INT(BLI_ghashIterator_getValue(&gh_iter));
224 
225  source = mvert_src[oldIndex];
226  dest = &result->mvert[newIndex];
227 
228  CustomData_copy_data(&mesh->vdata, &result->vdata, oldIndex, newIndex, 1);
229  *dest = source;
230  }
231 
232  /* copy the edges across, remapping indices */
233  for (i = 0; i < BLI_ghash_len(edgeHash); i++) {
234  MEdge source;
235  MEdge *dest;
236  int oldIndex = POINTER_AS_INT(BLI_ghash_lookup(edgeHash, POINTER_FROM_INT(i)));
237 
238  source = medge_src[oldIndex];
239  dest = &result->medge[i];
240 
241  source.v1 = POINTER_AS_INT(BLI_ghash_lookup(vertHash, POINTER_FROM_INT(source.v1)));
242  source.v2 = POINTER_AS_INT(BLI_ghash_lookup(vertHash, POINTER_FROM_INT(source.v2)));
243 
244  CustomData_copy_data(&mesh->edata, &result->edata, oldIndex, i, 1);
245  *dest = source;
246  }
247 
248  mpoly_dst = result->mpoly;
249  ml_dst = result->mloop;
250 
251  /* copy the faces across, remapping indices */
252  k = 0;
253  for (i = 0; i < numFaces_dst; i++) {
254  MPoly *source;
255  MPoly *dest;
256 
257  source = mpoly_src + faceMap[i];
258  dest = mpoly_dst + i;
259  CustomData_copy_data(&mesh->pdata, &result->pdata, faceMap[i], i, 1);
260 
261  *dest = *source;
262  dest->loopstart = k;
264  &mesh->ldata, &result->ldata, source->loopstart, dest->loopstart, dest->totloop);
265 
266  ml_src = mloop_src + source->loopstart;
267  for (j = 0; j < source->totloop; j++, k++, ml_src++, ml_dst++) {
268  ml_dst->v = POINTER_AS_INT(BLI_ghash_lookup(vertHash, POINTER_FROM_INT(ml_src->v)));
269  ml_dst->e = POINTER_AS_INT(BLI_ghash_lookup(edgeHash2, POINTER_FROM_INT(ml_src->e)));
270  }
271  }
272 
273  BLI_ghash_free(vertHash, NULL, NULL);
274  BLI_ghash_free(edgeHash, NULL, NULL);
275  BLI_ghash_free(edgeHash2, NULL, NULL);
276 
277  MEM_freeN(vertMap);
278  MEM_freeN(edgeMap);
280 
282  result->runtime.cd_dirty_vert |= CD_MASK_NORMAL;
283  }
284 
285  /* TODO(sybren): also copy flags & tags? */
286  return result;
287 }
288 
289 static void panel_draw(const bContext *UNUSED(C), Panel *panel)
290 {
291  uiLayout *layout = panel->layout;
292 
294 
295  uiLayoutSetPropSep(layout, true);
296 
297  uiItemR(layout, ptr, "frame_start", 0, NULL, ICON_NONE);
298  uiItemR(layout, ptr, "frame_duration", 0, NULL, ICON_NONE);
299  uiItemR(layout, ptr, "use_reverse", 0, NULL, ICON_NONE);
300 
301  modifier_panel_end(layout, ptr);
302 }
303 
304 static void random_panel_header_draw(const bContext *UNUSED(C), Panel *panel)
305 {
306  uiLayout *layout = panel->layout;
307 
309 
310  uiItemR(layout, ptr, "use_random_order", 0, NULL, ICON_NONE);
311 }
312 
313 static void random_panel_draw(const bContext *UNUSED(C), Panel *panel)
314 {
315  uiLayout *layout = panel->layout;
316 
318 
319  uiLayoutSetPropSep(layout, true);
320 
321  uiLayoutSetActive(layout, RNA_boolean_get(ptr, "use_random_order"));
322  uiItemR(layout, ptr, "seed", 0, NULL, ICON_NONE);
323 }
324 
325 static void panelRegister(ARegionType *region_type)
326 {
329  region_type, "randomize", "", random_panel_header_draw, random_panel_draw, panel_type);
330 }
331 
333  /* name */ "Build",
334  /* structName */ "BuildModifierData",
335  /* structSize */ sizeof(BuildModifierData),
336  /* srna */ &RNA_BuildModifier,
339  /* icon */ ICON_MOD_BUILD,
340 
341  /* copyData */ BKE_modifier_copydata_generic,
342 
343  /* deformVerts */ NULL,
344  /* deformMatrices */ NULL,
345  /* deformVertsEM */ NULL,
346  /* deformMatricesEM */ NULL,
347  /* modifyMesh */ modifyMesh,
348  /* modifyHair */ NULL,
349  /* modifyGeometrySet */ NULL,
350  /* modifyVolume */ NULL,
351 
352  /* initData */ initData,
353  /* requiredDataMask */ NULL,
354  /* freeData */ NULL,
355  /* isDisabled */ NULL,
356  /* updateDepsgraph */ NULL,
357  /* dependsOnTime */ dependsOnTime,
358  /* dependsOnNormals */ NULL,
359  /* foreachIDLink */ NULL,
360  /* foreachTexLink */ NULL,
361  /* freeRuntimeData */ NULL,
362  /* panelRegister */ panelRegister,
363  /* blendWrite */ NULL,
364  /* blendRead */ NULL,
365 };
void CustomData_copy_data(const struct CustomData *source, struct CustomData *dest, int source_index, int dest_index, int count)
struct Mesh * BKE_mesh_new_nomain_from_template(const struct Mesh *me_src, int verts_len, int edges_len, int tessface_len, int loops_len, int polys_len)
@ eModifierTypeFlag_AcceptsCVs
Definition: BKE_modifier.h:81
@ 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
float BKE_scene_frame_get(const struct Scene *scene)
#define BLI_assert(a)
Definition: BLI_assert.h:58
BLI_INLINE void * BLI_ghashIterator_getKey(GHashIterator *ghi) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.h:146
BLI_INLINE void * BLI_ghashIterator_getValue(GHashIterator *ghi) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.h:150
#define GHASH_ITER(gh_iter_, ghash_)
Definition: BLI_ghash.h:169
unsigned int BLI_ghash_len(GHash *gh) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:744
bool BLI_ghash_haskey(GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:941
GHash * BLI_ghash_int_new(const char *info) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
void BLI_ghash_insert(GHash *gh, void *key, void *val)
Definition: BLI_ghash.c:756
void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition: BLI_ghash.c:1008
bool BLI_ghash_ensure_p(GHash *gh, void *key, void ***r_val) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:851
void * BLI_ghash_lookup(GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:803
void range_vn_i(int *array_tar, const int size, const int start)
Definition: math_vector.c:1172
Random number functions.
void BLI_array_randomize(void *data, unsigned int elem_size, unsigned int elem_tot, unsigned int seed)
Definition: rand.cc:208
#define POINTER_FROM_INT(i)
#define UNUSED(x)
#define POINTER_AS_INT(i)
#define MEMCMP_STRUCT_AFTER_IS_ZERO(struct_var, member)
#define MEMCPY_STRUCT_AFTER(struct_dst, struct_src, member)
struct Scene * DEG_get_input_scene(const Depsgraph *graph)
#define CD_MASK_NORMAL
#define DNA_struct_default_get(struct_name)
Definition: DNA_defaults.h:44
@ eModifierType_Build
struct BuildModifierData BuildModifierData
@ MOD_BUILD_FLAG_REVERSE
@ MOD_BUILD_FLAG_RANDOMIZE
Object is a sort of wrapper for general info.
Read Guarded memory(de)allocation.
static Mesh * modifyMesh(ModifierData *md, const ModifierEvalContext *ctx, struct Mesh *mesh)
Definition: MOD_build.c:69
static void random_panel_header_draw(const bContext *UNUSED(C), Panel *panel)
Definition: MOD_build.c:304
static void random_panel_draw(const bContext *UNUSED(C), Panel *panel)
Definition: MOD_build.c:313
ModifierTypeInfo modifierType_Build
Definition: MOD_build.c:332
static bool dependsOnTime(ModifierData *UNUSED(md))
Definition: MOD_build.c:64
static void panel_draw(const bContext *UNUSED(C), Panel *panel)
Definition: MOD_build.c:289
static void initData(ModifierData *md)
Definition: MOD_build.c:55
static void panelRegister(ARegionType *region_type)
Definition: MOD_build.c:325
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)
PanelType * modifier_subpanel_register(ARegionType *region_type, const char *name, const char *label, PanelDrawFn draw_header, PanelDrawFn draw, PanelType *parent)
Group RGB to Bright Vector Camera CLAMP
StructRNA RNA_BuildModifier
#define C
Definition: RandGen.cpp:39
void uiLayoutSetActive(uiLayout *layout, bool active)
void uiLayoutSetPropSep(uiLayout *layout, bool is_sep)
void uiItemR(uiLayout *layout, struct PointerRNA *ptr, const char *propname, int flag, const char *name, int icon)
Scene scene
void *(* MEM_malloc_arrayN)(size_t len, size_t size, const char *str)
Definition: mallocn.c:48
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
ccl_device_inline float frac(float x, int *ix)
const int faceMap[6][4]
Definition: octree.cpp:2827
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:6261
_W64 unsigned int uintptr_t
Definition: stdint.h:122
unsigned int v1
unsigned int v2
unsigned int e
unsigned int v
int64_t cd_dirty_vert
struct MEdge * medge
struct CustomData pdata ldata
struct MVert * mvert
int totedge
int totvert
struct MLoop * mloop
Mesh_Runtime runtime
int totpoly
struct MPoly * mpoly
struct Depsgraph * depsgraph
Definition: BKE_modifier.h:153
struct uiLayout * layout
PointerRNA * ptr
Definition: wm_files.c:3157