Blender  V2.93
MOD_surface.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 "BLI_utildefines.h"
25 
26 #include "BLI_math.h"
27 
28 #include "BLT_translation.h"
29 
30 #include "DNA_defaults.h"
31 #include "DNA_mesh_types.h"
32 #include "DNA_meshdata_types.h"
33 #include "DNA_object_types.h"
34 #include "DNA_scene_types.h"
35 #include "DNA_screen_types.h"
36 
37 #include "BKE_bvhutils.h"
38 #include "BKE_context.h"
39 #include "BKE_lib_id.h"
40 #include "BKE_mesh.h"
41 #include "BKE_screen.h"
42 
43 #include "UI_interface.h"
44 #include "UI_resources.h"
45 
46 #include "RNA_access.h"
47 
48 #include "DEG_depsgraph.h"
49 #include "DEG_depsgraph_query.h"
50 
51 #include "MOD_modifiertypes.h"
52 #include "MOD_ui_common.h"
53 #include "MOD_util.h"
54 
55 #include "BLO_read_write.h"
56 
57 #include "MEM_guardedalloc.h"
58 
59 static void initData(ModifierData *md)
60 {
62 
63  BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(surmd, modifier));
64 
66 }
67 
68 static void copyData(const ModifierData *md_src, ModifierData *md_dst, const int flag)
69 {
70  SurfaceModifierData *surmd_dst = (SurfaceModifierData *)md_dst;
71 
72  BKE_modifier_copydata_generic(md_src, md_dst, flag);
73 
74  surmd_dst->bvhtree = NULL;
75  surmd_dst->mesh = NULL;
76  surmd_dst->x = NULL;
77  surmd_dst->v = NULL;
78 }
79 
80 static void freeData(ModifierData *md)
81 {
83 
84  if (surmd) {
85  if (surmd->bvhtree) {
87  MEM_SAFE_FREE(surmd->bvhtree);
88  }
89 
90  if (surmd->mesh) {
91  BKE_id_free(NULL, surmd->mesh);
92  surmd->mesh = NULL;
93  }
94 
95  MEM_SAFE_FREE(surmd->x);
96 
97  MEM_SAFE_FREE(surmd->v);
98  }
99 }
100 
102 {
103  return true;
104 }
105 
106 static void deformVerts(ModifierData *md,
107  const ModifierEvalContext *ctx,
108  Mesh *mesh,
109  float (*vertexCos)[3],
110  int numVerts)
111 {
113  const int cfra = (int)DEG_get_ctime(ctx->depsgraph);
114 
115  /* Free mesh and BVH cache. */
116  if (surmd->bvhtree) {
118  MEM_SAFE_FREE(surmd->bvhtree);
119  }
120 
121  if (surmd->mesh) {
122  BKE_id_free(NULL, surmd->mesh);
123  surmd->mesh = NULL;
124  }
125 
126  if (mesh) {
127  /* Not possible to use get_mesh() in this case as we'll modify its vertices
128  * and get_mesh() would return 'mesh' directly. */
130  }
131  else {
132  surmd->mesh = MOD_deform_mesh_eval_get(ctx->object, NULL, NULL, NULL, numVerts, false, false);
133  }
134 
135  if (!ctx->object->pd) {
136  printf("SurfaceModifier deformVerts: Should not happen!\n");
137  return;
138  }
139 
140  if (surmd->mesh) {
141  uint numverts = 0, i = 0;
142  int init = 0;
143  float *vec;
144  MVert *x, *v;
145 
146  BKE_mesh_vert_coords_apply(surmd->mesh, vertexCos);
147  BKE_mesh_calc_normals(surmd->mesh);
148 
149  numverts = surmd->mesh->totvert;
150 
151  if (numverts != surmd->numverts || surmd->x == NULL || surmd->v == NULL ||
152  cfra != surmd->cfra + 1) {
153  if (surmd->x) {
154  MEM_freeN(surmd->x);
155  surmd->x = NULL;
156  }
157  if (surmd->v) {
158  MEM_freeN(surmd->v);
159  surmd->v = NULL;
160  }
161 
162  surmd->x = MEM_calloc_arrayN(numverts, sizeof(MVert), "MVert");
163  surmd->v = MEM_calloc_arrayN(numverts, sizeof(MVert), "MVert");
164 
165  surmd->numverts = numverts;
166 
167  init = 1;
168  }
169 
170  /* convert to global coordinates and calculate velocity */
171  for (i = 0, x = surmd->x, v = surmd->v; i < numverts; i++, x++, v++) {
172  vec = surmd->mesh->mvert[i].co;
173  mul_m4_v3(ctx->object->obmat, vec);
174 
175  if (init) {
176  v->co[0] = v->co[1] = v->co[2] = 0.0f;
177  }
178  else {
179  sub_v3_v3v3(v->co, vec, x->co);
180  }
181 
182  copy_v3_v3(x->co, vec);
183  }
184 
185  surmd->cfra = cfra;
186 
187  surmd->bvhtree = MEM_callocN(sizeof(BVHTreeFromMesh), "BVHTreeFromMesh");
188 
189  if (surmd->mesh->totpoly) {
191  }
192  else {
194  }
195  }
196 }
197 
198 static void panel_draw(const bContext *UNUSED(C), Panel *panel)
199 {
200  uiLayout *layout = panel->layout;
201 
203 
204  uiItemL(layout, IFACE_("Settings are inside the Physics tab"), ICON_NONE);
205 
206  modifier_panel_end(layout, ptr);
207 }
208 
209 static void panelRegister(ARegionType *region_type)
210 {
212 }
213 
214 static void blendRead(BlendDataReader *UNUSED(reader), ModifierData *md)
215 {
217 
218  surmd->mesh = NULL;
219  surmd->bvhtree = NULL;
220  surmd->x = NULL;
221  surmd->v = NULL;
222  surmd->numverts = 0;
223 }
224 
226  /* name */ "Surface",
227  /* structName */ "SurfaceModifierData",
228  /* structSize */ sizeof(SurfaceModifierData),
229  /* srna */ &RNA_SurfaceModifier,
230  /* type */ eModifierTypeType_OnlyDeform,
233  /* icon */ ICON_MOD_PHYSICS,
234 
235  /* copyData */ copyData,
236 
237  /* deformVerts */ deformVerts,
238  /* deformMatrices */ NULL,
239  /* deformVertsEM */ NULL,
240  /* deformMatricesEM */ NULL,
241  /* modifyMesh */ NULL,
242  /* modifyHair */ NULL,
243  /* modifyGeometrySet */ NULL,
244  /* modifyVolume */ NULL,
245 
246  /* initData */ initData,
247  /* requiredDataMask */ NULL,
248  /* freeData */ freeData,
249  /* isDisabled */ NULL,
250  /* updateDepsgraph */ NULL,
251  /* dependsOnTime */ dependsOnTime,
252  /* dependsOnNormals */ NULL,
253  /* foreachIDLink */ NULL,
254  /* foreachTexLink */ NULL,
255  /* freeRuntimeData */ NULL,
256  /* panelRegister */ panelRegister,
257  /* blendWrite */ NULL,
258  /* blendRead */ blendRead,
259 };
BVHTree * BKE_bvhtree_from_mesh_get(struct BVHTreeFromMesh *data, struct Mesh *mesh, const BVHCacheType bvh_cache_type, const int tree_type)
Definition: bvhutils.c:1413
void free_bvhtree_from_mesh(struct BVHTreeFromMesh *data)
Definition: bvhutils.c:1701
@ BVHTREE_FROM_EDGES
Definition: BKE_bvhutils.h:91
@ BVHTREE_FROM_LOOPTRI
Definition: BKE_bvhutils.h:93
@ LIB_ID_COPY_LOCALIZE
Definition: BKE_lib_id.h:145
struct ID * BKE_id_copy_ex(struct Main *bmain, const struct ID *id, struct ID **r_newid, const 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.c:1755
void BKE_mesh_calc_normals(struct Mesh *me)
@ eModifierTypeFlag_NoUserAdd
Definition: BKE_modifier.h:110
@ 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_OnlyDeform
Definition: BKE_modifier.h:58
#define BLI_assert(a)
Definition: BLI_assert.h:58
void mul_m4_v3(const float M[4][4], float r[3])
Definition: math_matrix.c:732
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:83
#define UNUSED(x)
#define MEMCMP_STRUCT_AFTER_IS_ZERO(struct_var, member)
#define MEMCPY_STRUCT_AFTER(struct_dst, struct_src, member)
#define IFACE_(msgid)
float DEG_get_ctime(const Depsgraph *graph)
#define DNA_struct_default_get(struct_name)
Definition: DNA_defaults.h:44
@ 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 void blendRead(BlendDataReader *UNUSED(reader), ModifierData *md)
Definition: MOD_surface.c:214
static void copyData(const ModifierData *md_src, ModifierData *md_dst, const int flag)
Definition: MOD_surface.c:68
ModifierTypeInfo modifierType_Surface
Definition: MOD_surface.c:225
static void deformVerts(ModifierData *md, const ModifierEvalContext *ctx, Mesh *mesh, float(*vertexCos)[3], int numVerts)
Definition: MOD_surface.c:106
static bool dependsOnTime(ModifierData *UNUSED(md))
Definition: MOD_surface.c:101
static void panel_draw(const bContext *UNUSED(C), Panel *panel)
Definition: MOD_surface.c:198
static void initData(ModifierData *md)
Definition: MOD_surface.c:59
static void panelRegister(ARegionType *region_type)
Definition: MOD_surface.c:209
static void freeData(ModifierData *md)
Definition: MOD_surface.c:80
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)
Mesh * MOD_deform_mesh_eval_get(Object *ob, struct BMEditMesh *em, Mesh *mesh, const float(*vertexCos)[3], const int num_verts, const bool use_normals, const bool use_orco)
Definition: MOD_util.c:186
StructRNA RNA_SurfaceModifier
#define C
Definition: RandGen.cpp:39
void uiItemL(uiLayout *layout, const char *name, int icon)
ATTR_WARN_UNUSED_RESULT const BMVert * v
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_calloc_arrayN)(size_t len, size_t size, const char *str)
Definition: mallocn.c:46
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
float co[3]
Definition: bmesh_class.h:99
Definition: DNA_ID.h:273
float co[3]
struct MVert * mvert
int totvert
int totpoly
struct Depsgraph * depsgraph
Definition: BKE_modifier.h:153
struct Object * object
Definition: BKE_modifier.h:154
struct PartDeflect * pd
float obmat[4][4]
struct uiLayout * layout
struct BVHTreeFromMesh * bvhtree
PointerRNA * ptr
Definition: wm_files.c:3157