Blender V4.3
MOD_surface.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2005 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9#include "BLI_math_matrix.h"
10#include "BLI_math_vector.h"
11#include "BLI_utildefines.h"
12
13#include "BLT_translation.hh"
14
15#include "DNA_defaults.h"
16#include "DNA_mesh_types.h"
17#include "DNA_object_types.h"
18#include "DNA_screen_types.h"
19
20#include "BKE_bvhutils.hh"
21#include "BKE_lib_id.hh"
22#include "BKE_mesh.hh"
23
24#include "UI_interface.hh"
25#include "UI_resources.hh"
26
27#include "RNA_prototypes.hh"
28
29#include "DEG_depsgraph.hh"
31
32#include "MOD_modifiertypes.hh"
33#include "MOD_ui_common.hh"
34
35#include "MEM_guardedalloc.h"
36
45
46static void copy_data(const ModifierData *md_src, ModifierData *md_dst, const int flag)
47{
48 SurfaceModifierData *surmd_dst = (SurfaceModifierData *)md_dst;
49
50 BKE_modifier_copydata_generic(md_src, md_dst, flag);
51
52 memset(&surmd_dst->runtime, 0, sizeof(surmd_dst->runtime));
53}
54
55static void free_data(ModifierData *md)
56{
58
59 if (surmd) {
60 if (surmd->runtime.bvhtree) {
63 }
64
65 if (surmd->runtime.mesh) {
66 BKE_id_free(nullptr, surmd->runtime.mesh);
67 surmd->runtime.mesh = nullptr;
68 }
69
71
73 }
74}
75
76static bool depends_on_time(Scene * /*scene*/, ModifierData * /*md*/)
77{
78 return true;
79}
80
81static void deform_verts(ModifierData *md,
82 const ModifierEvalContext *ctx,
83 Mesh *mesh,
85{
87 const int cfra = int(DEG_get_ctime(ctx->depsgraph));
88
89 /* Free mesh and BVH cache. */
90 if (surmd->runtime.bvhtree) {
93 }
94
95 if (surmd->runtime.mesh) {
96 BKE_id_free(nullptr, surmd->runtime.mesh);
97 surmd->runtime.mesh = nullptr;
98 }
99
100 if (mesh) {
101 surmd->runtime.mesh = BKE_mesh_copy_for_eval(*mesh);
102 }
103
104 if (!ctx->object->pd) {
105 printf("SurfaceModifier deform_verts: Should not happen!\n");
106 return;
107 }
108
109 if (surmd->runtime.mesh) {
110 uint mesh_verts_num = 0, i = 0;
111 int init = 0;
112
113 surmd->runtime.mesh->vert_positions_for_write().copy_from(positions);
114 surmd->runtime.mesh->tag_positions_changed();
115
116 mesh_verts_num = surmd->runtime.mesh->verts_num;
117
118 if ((mesh_verts_num != surmd->runtime.verts_num) ||
119 (surmd->runtime.vert_positions_prev == nullptr) ||
120 (surmd->runtime.vert_velocities == nullptr) || (cfra != surmd->runtime.cfra_prev + 1))
121 {
122
125
126 surmd->runtime.vert_positions_prev = static_cast<float(*)[3]>(
127 MEM_calloc_arrayN(mesh_verts_num, sizeof(float[3]), __func__));
128 surmd->runtime.vert_velocities = static_cast<float(*)[3]>(
129 MEM_calloc_arrayN(mesh_verts_num, sizeof(float[3]), __func__));
130
131 surmd->runtime.verts_num = mesh_verts_num;
132
133 init = 1;
134 }
135
136 /* convert to global coordinates and calculate velocity */
138 surmd->runtime.mesh->vert_positions_for_write();
139 for (i = 0; i < mesh_verts_num; i++) {
140 float *vec = positions[i];
141 mul_m4_v3(ctx->object->object_to_world().ptr(), vec);
142
143 if (init) {
145 }
146 else {
148 }
149
150 copy_v3_v3(surmd->runtime.vert_positions_prev[i], vec);
151 }
152
153 surmd->runtime.cfra_prev = cfra;
154
155 const bool has_face = surmd->runtime.mesh->faces_num > 0;
156 const bool has_edge = surmd->runtime.mesh->edges_num > 0;
157 if (has_face || has_edge) {
158 surmd->runtime.bvhtree = static_cast<BVHTreeFromMesh *>(
159 MEM_callocN(sizeof(BVHTreeFromMesh), __func__));
160
161 if (has_face) {
164 }
165 else if (has_edge) {
167 surmd->runtime.bvhtree, surmd->runtime.mesh, BVHTREE_FROM_EDGES, 2);
168 }
169 }
170 }
171}
172
173static void panel_draw(const bContext * /*C*/, Panel *panel)
174{
175 uiLayout *layout = panel->layout;
176
178
179 uiItemL(layout, RPT_("Settings are inside the Physics tab"), ICON_NONE);
180
181 modifier_panel_end(layout, ptr);
182}
183
184static void panel_register(ARegionType *region_type)
185{
187}
188
189static void blend_read(BlendDataReader * /*reader*/, ModifierData *md)
190{
192
193 memset(&surmd->runtime, 0, sizeof(surmd->runtime));
194}
195
197 /*idname*/ "Surface",
198 /*name*/ N_("Surface"),
199 /*struct_name*/ "SurfaceModifierData",
200 /*struct_size*/ sizeof(SurfaceModifierData),
201 /*srna*/ &RNA_SurfaceModifier,
205 /*icon*/ ICON_MOD_PHYSICS,
206
207 /*copy_data*/ copy_data,
208
209 /*deform_verts*/ deform_verts,
210 /*deform_matrices*/ nullptr,
211 /*deform_verts_EM*/ nullptr,
212 /*deform_matrices_EM*/ nullptr,
213 /*modify_mesh*/ nullptr,
214 /*modify_geometry_set*/ nullptr,
215
216 /*init_data*/ init_data,
217 /*required_data_mask*/ nullptr,
218 /*free_data*/ free_data,
219 /*is_disabled*/ nullptr,
220 /*update_depsgraph*/ nullptr,
221 /*depends_on_time*/ depends_on_time,
222 /*depends_on_normals*/ nullptr,
223 /*foreach_ID_link*/ nullptr,
224 /*foreach_tex_link*/ nullptr,
225 /*free_runtime_data*/ nullptr,
226 /*panel_register*/ panel_register,
227 /*blend_write*/ nullptr,
228 /*blend_read*/ blend_read,
229 /*foreach_cache*/ nullptr,
230};
void free_bvhtree_from_mesh(BVHTreeFromMesh *data)
Definition bvhutils.cc:1160
BVHTree * BKE_bvhtree_from_mesh_get(BVHTreeFromMesh *data, const Mesh *mesh, BVHCacheType bvh_cache_type, int tree_type)
Definition bvhutils.cc:899
@ BVHTREE_FROM_CORNER_TRIS
@ BVHTREE_FROM_EDGES
void BKE_id_free(Main *bmain, void *idv)
Mesh * BKE_mesh_copy_for_eval(const Mesh &source)
void BKE_modifier_copydata_generic(const ModifierData *md, ModifierData *md_dst, int flag)
@ eModifierTypeFlag_NoUserAdd
@ eModifierTypeFlag_AcceptsCVs
@ eModifierTypeFlag_AcceptsMesh
#define BLI_assert(a)
Definition BLI_assert.h:50
void mul_m4_v3(const float M[4][4], float r[3])
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])
MINLINE void zero_v3(float r[3])
unsigned int uint
#define MEMCMP_STRUCT_AFTER_IS_ZERO(struct_var, member)
#define MEMCPY_STRUCT_AFTER(struct_dst, struct_src, member)
#define RPT_(msgid)
float DEG_get_ctime(const Depsgraph *graph)
#define DNA_struct_default_get(struct_name)
@ 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 init_data(ModifierData *md)
static void deform_verts(ModifierData *md, const ModifierEvalContext *ctx, Mesh *mesh, blender::MutableSpan< blender::float3 > positions)
static void panel_register(ARegionType *region_type)
ModifierTypeInfo modifierType_Surface
static void copy_data(const ModifierData *md_src, ModifierData *md_dst, const int flag)
static void blend_read(BlendDataReader *, ModifierData *md)
static void free_data(ModifierData *md)
static void panel_draw(const bContext *, Panel *panel)
static bool depends_on_time(Scene *, ModifierData *)
void modifier_panel_end(uiLayout *layout, PointerRNA *ptr)
PanelType * modifier_panel_register(ARegionType *region_type, ModifierType type, PanelDrawFn draw)
PointerRNA * modifier_panel_get_property_pointers(Panel *panel, PointerRNA *r_ob_ptr)
void uiItemL(uiLayout *layout, const char *name, int icon)
void init()
#define printf
draw_view push_constant(Type::INT, "radiance_src") .push_constant(Type capture_info_buf storage_buf(1, Qualifier::READ, "ObjectBounds", "bounds_buf[]") .push_constant(Type draw_view int
void *(* MEM_calloc_arrayN)(size_t len, size_t size, const char *str)
Definition mallocn.cc:43
void *(* MEM_callocN)(size_t len, const char *str)
Definition mallocn.cc:42
int edges_num
int faces_num
int verts_num
struct PartDeflect * pd
struct uiLayout * layout
struct BVHTreeFromMesh * bvhtree
SurfaceModifierData_Runtime runtime
#define N_(msgid)
PointerRNA * ptr
Definition wm_files.cc:4126
uint8_t flag
Definition wm_window.cc:138