Blender  V3.3
MOD_surface.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2005 Blender Foundation. All rights reserved. */
3 
8 #include "BLI_utildefines.h"
9 
10 #include "BLI_math.h"
11 
12 #include "BLT_translation.h"
13 
14 #include "DNA_defaults.h"
15 #include "DNA_mesh_types.h"
16 #include "DNA_meshdata_types.h"
17 #include "DNA_object_types.h"
18 #include "DNA_scene_types.h"
19 #include "DNA_screen_types.h"
20 
21 #include "BKE_bvhutils.h"
22 #include "BKE_context.h"
23 #include "BKE_lib_id.h"
24 #include "BKE_mesh.h"
25 #include "BKE_screen.h"
26 
27 #include "UI_interface.h"
28 #include "UI_resources.h"
29 
30 #include "RNA_access.h"
31 #include "RNA_prototypes.h"
32 
33 #include "DEG_depsgraph.h"
34 #include "DEG_depsgraph_query.h"
35 
36 #include "MOD_modifiertypes.h"
37 #include "MOD_ui_common.h"
38 #include "MOD_util.h"
39 
40 #include "BLO_read_write.h"
41 
42 #include "MEM_guardedalloc.h"
43 
44 static void initData(ModifierData *md)
45 {
47 
48  BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(surmd, modifier));
49 
51 }
52 
53 static void copyData(const ModifierData *md_src, ModifierData *md_dst, const int flag)
54 {
55  SurfaceModifierData *surmd_dst = (SurfaceModifierData *)md_dst;
56 
57  BKE_modifier_copydata_generic(md_src, md_dst, flag);
58 
59  surmd_dst->bvhtree = NULL;
60  surmd_dst->mesh = NULL;
61  surmd_dst->x = NULL;
62  surmd_dst->v = NULL;
63 }
64 
65 static void freeData(ModifierData *md)
66 {
68 
69  if (surmd) {
70  if (surmd->bvhtree) {
72  MEM_SAFE_FREE(surmd->bvhtree);
73  }
74 
75  if (surmd->mesh) {
76  BKE_id_free(NULL, surmd->mesh);
77  surmd->mesh = NULL;
78  }
79 
80  MEM_SAFE_FREE(surmd->x);
81 
82  MEM_SAFE_FREE(surmd->v);
83  }
84 }
85 
86 static bool dependsOnTime(struct Scene *UNUSED(scene), ModifierData *UNUSED(md))
87 {
88  return true;
89 }
90 
91 static void deformVerts(ModifierData *md,
92  const ModifierEvalContext *ctx,
93  Mesh *mesh,
94  float (*vertexCos)[3],
95  int verts_num)
96 {
98  const int cfra = (int)DEG_get_ctime(ctx->depsgraph);
99 
100  /* Free mesh and BVH cache. */
101  if (surmd->bvhtree) {
103  MEM_SAFE_FREE(surmd->bvhtree);
104  }
105 
106  if (surmd->mesh) {
107  BKE_id_free(NULL, surmd->mesh);
108  surmd->mesh = NULL;
109  }
110 
111  if (mesh) {
112  /* Not possible to use get_mesh() in this case as we'll modify its vertices
113  * and get_mesh() would return 'mesh' directly. */
115  }
116  else {
117  surmd->mesh = MOD_deform_mesh_eval_get(ctx->object, NULL, NULL, NULL, verts_num, false, false);
118  }
119 
120  if (!ctx->object->pd) {
121  printf("SurfaceModifier deformVerts: Should not happen!\n");
122  return;
123  }
124 
125  if (surmd->mesh) {
126  uint mesh_verts_num = 0, i = 0;
127  int init = 0;
128  float *vec;
129  MVert *x, *v;
130 
131  BKE_mesh_vert_coords_apply(surmd->mesh, vertexCos);
132 
133  mesh_verts_num = surmd->mesh->totvert;
134 
135  if (mesh_verts_num != surmd->verts_num || surmd->x == NULL || surmd->v == NULL ||
136  cfra != surmd->cfra + 1) {
137  if (surmd->x) {
138  MEM_freeN(surmd->x);
139  surmd->x = NULL;
140  }
141  if (surmd->v) {
142  MEM_freeN(surmd->v);
143  surmd->v = NULL;
144  }
145 
146  surmd->x = MEM_calloc_arrayN(mesh_verts_num, sizeof(MVert), "MVert");
147  surmd->v = MEM_calloc_arrayN(mesh_verts_num, sizeof(MVert), "MVert");
148 
149  surmd->verts_num = mesh_verts_num;
150 
151  init = 1;
152  }
153 
154  /* convert to global coordinates and calculate velocity */
155  for (i = 0, x = surmd->x, v = surmd->v; i < mesh_verts_num; i++, x++, v++) {
156  vec = surmd->mesh->mvert[i].co;
157  mul_m4_v3(ctx->object->obmat, vec);
158 
159  if (init) {
160  v->co[0] = v->co[1] = v->co[2] = 0.0f;
161  }
162  else {
163  sub_v3_v3v3(v->co, vec, x->co);
164  }
165 
166  copy_v3_v3(x->co, vec);
167  }
168 
169  surmd->cfra = cfra;
170 
171  const bool has_poly = surmd->mesh->totpoly > 0;
172  const bool has_edge = surmd->mesh->totedge > 0;
173  if (has_poly || has_edge) {
174  surmd->bvhtree = MEM_callocN(sizeof(BVHTreeFromMesh), "BVHTreeFromMesh");
175 
176  if (has_poly) {
178  }
179  else if (has_edge) {
181  }
182  }
183  }
184 }
185 
186 static void panel_draw(const bContext *UNUSED(C), Panel *panel)
187 {
188  uiLayout *layout = panel->layout;
189 
191 
192  uiItemL(layout, TIP_("Settings are inside the Physics tab"), ICON_NONE);
193 
194  modifier_panel_end(layout, ptr);
195 }
196 
197 static void panelRegister(ARegionType *region_type)
198 {
200 }
201 
202 static void blendRead(BlendDataReader *UNUSED(reader), ModifierData *md)
203 {
205 
206  surmd->mesh = NULL;
207  surmd->bvhtree = NULL;
208  surmd->x = NULL;
209  surmd->v = NULL;
210  surmd->verts_num = 0;
211 }
212 
214  /* name */ N_("Surface"),
215  /* structName */ "SurfaceModifierData",
216  /* structSize */ sizeof(SurfaceModifierData),
217  /* srna */ &RNA_SurfaceModifier,
218  /* type */ eModifierTypeType_OnlyDeform,
221  /* icon */ ICON_MOD_PHYSICS,
222 
223  /* copyData */ copyData,
224 
225  /* deformVerts */ deformVerts,
226  /* deformMatrices */ NULL,
227  /* deformVertsEM */ NULL,
228  /* deformMatricesEM */ NULL,
229  /* modifyMesh */ NULL,
230  /* modifyGeometrySet */ NULL,
231 
232  /* initData */ initData,
233  /* requiredDataMask */ NULL,
234  /* freeData */ freeData,
235  /* isDisabled */ NULL,
236  /* updateDepsgraph */ NULL,
237  /* dependsOnTime */ dependsOnTime,
238  /* dependsOnNormals */ NULL,
239  /* foreachIDLink */ NULL,
240  /* foreachTexLink */ NULL,
241  /* freeRuntimeData */ NULL,
242  /* panelRegister */ panelRegister,
243  /* blendWrite */ NULL,
244  /* blendRead */ blendRead,
245 };
void free_bvhtree_from_mesh(struct BVHTreeFromMesh *data)
Definition: bvhutils.cc:1410
@ BVHTREE_FROM_EDGES
Definition: BKE_bvhutils.h:71
@ BVHTREE_FROM_LOOPTRI
Definition: BKE_bvhutils.h:73
BVHTree * BKE_bvhtree_from_mesh_get(struct BVHTreeFromMesh *data, const struct Mesh *mesh, BVHCacheType bvh_cache_type, int tree_type)
Definition: bvhutils.cc:1213
@ LIB_ID_COPY_LOCALIZE
Definition: BKE_lib_id.h:187
struct ID * BKE_id_copy_ex(struct Main *bmain, const struct ID *id, struct ID **r_newid, int flag)
void BKE_id_free(struct Main *bmain, void *idv)
void BKE_mesh_vert_coords_apply(struct Mesh *mesh, const float(*vert_coords)[3])
Definition: mesh.cc:1834
@ eModifierTypeFlag_NoUserAdd
Definition: BKE_modifier.h:96
@ eModifierTypeFlag_AcceptsCVs
Definition: BKE_modifier.h:67
@ eModifierTypeFlag_AcceptsMesh
Definition: BKE_modifier.h:66
void BKE_modifier_copydata_generic(const struct ModifierData *md, struct ModifierData *md_dst, int flag)
@ eModifierTypeType_OnlyDeform
Definition: BKE_modifier.h:44
#define BLI_assert(a)
Definition: BLI_assert.h:46
void mul_m4_v3(const float M[4][4], float r[3])
Definition: math_matrix.c:729
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition: BLI_sys_types.h:67
#define UNUSED(x)
#define MEMCMP_STRUCT_AFTER_IS_ZERO(struct_var, member)
#define MEMCPY_STRUCT_AFTER(struct_dst, struct_src, member)
#define TIP_(msgid)
float DEG_get_ctime(const Depsgraph *graph)
#define DNA_struct_default_get(struct_name)
Definition: DNA_defaults.h:29
@ eModifierType_Surface
struct SurfaceModifierData SurfaceModifierData
Object is a sort of wrapper for general info.
Read Guarded memory(de)allocation.
#define MEM_SAFE_FREE(v)
static bool dependsOnTime(struct Scene *UNUSED(scene), ModifierData *UNUSED(md))
Definition: MOD_surface.c:86
static void blendRead(BlendDataReader *UNUSED(reader), ModifierData *md)
Definition: MOD_surface.c:202
static void copyData(const ModifierData *md_src, ModifierData *md_dst, const int flag)
Definition: MOD_surface.c:53
static void deformVerts(ModifierData *md, const ModifierEvalContext *ctx, Mesh *mesh, float(*vertexCos)[3], int verts_num)
Definition: MOD_surface.c:91
ModifierTypeInfo modifierType_Surface
Definition: MOD_surface.c:213
static void panel_draw(const bContext *UNUSED(C), Panel *panel)
Definition: MOD_surface.c:186
static void initData(ModifierData *md)
Definition: MOD_surface.c:44
static void panelRegister(ARegionType *region_type)
Definition: MOD_surface.c:197
static void freeData(ModifierData *md)
Definition: MOD_surface.c:65
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)
Mesh * MOD_deform_mesh_eval_get(Object *ob, struct BMEditMesh *em, Mesh *mesh, const float(*vertexCos)[3], const int verts_num, const bool use_normals, const bool use_orco)
Definition: MOD_util.c:167
#define C
Definition: RandGen.cpp:25
void uiItemL(uiLayout *layout, const char *name, int icon)
ATTR_WARN_UNUSED_RESULT const BMVert * v
Scene scene
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void *(* MEM_calloc_arrayN)(size_t len, size_t size, const char *str)
Definition: mallocn.c:32
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:31
float co[3]
Definition: bmesh_class.h:87
Definition: DNA_ID.h:368
float co[3]
struct MVert * mvert
int totedge
int totvert
int totpoly
struct Depsgraph * depsgraph
Definition: BKE_modifier.h:140
struct Object * object
Definition: BKE_modifier.h:141
struct PartDeflect * pd
float obmat[4][4]
struct uiLayout * layout
struct BVHTreeFromMesh * bvhtree
#define N_(msgid)
PointerRNA * ptr
Definition: wm_files.c:3480