Blender V5.3
MOD_collision.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
8
9#include "BLI_utildefines.hh"
10
11#include "BLI_kdopbvh.hh"
12#include "BLI_math_matrix_c.hh"
13#include "BLI_math_vector_c.hh"
14
15#include "BLT_translation.hh"
16
18#include "DNA_object_types.h"
19#include "DNA_screen_types.h"
20
21#include "MEM_guardedalloc.h"
22
23#include "BKE_collision.h"
24#include "BKE_global.hh"
25#include "BKE_mesh.hh"
26#include "BKE_modifier.hh"
27
29#include "UI_resources.hh"
30
31#include "RNA_prototypes.hh"
32
33#include "MOD_ui_common.hh"
34
36
37namespace blender {
38
39static void init_data(ModifierData *md)
40{
41 CollisionModifierData *collmd = reinterpret_cast<CollisionModifierData *>(md);
43}
44
45static void free_data(ModifierData *md)
46{
47 CollisionModifierData *collmd = reinterpret_cast<CollisionModifierData *>(md);
48
49 if (collmd) { /* Seriously? */
50 if (collmd->bvhtree) {
52 collmd->bvhtree = nullptr;
53 }
54
55 MEM_SAFE_DELETE(collmd->x);
56 MEM_SAFE_DELETE(collmd->xnew);
57 MEM_SAFE_DELETE(collmd->current_x);
58 MEM_SAFE_DELETE(collmd->current_xnew);
59 MEM_SAFE_DELETE(collmd->current_v);
60
61 MEM_SAFE_DELETE(collmd->vert_tris);
62
63 collmd->time_x = collmd->time_xnew = -1000;
64 collmd->mvert_num = 0;
65 collmd->tri_num = 0;
66 collmd->is_static = false;
67 }
68}
69
70static bool depends_on_time(Scene * /*scene*/, ModifierData * /*md*/)
71{
72 return true;
73}
74
75static void deform_verts(ModifierData *md,
76 const ModifierEvalContext *ctx,
77 Mesh *mesh,
78 MutableSpan<float3> positions)
79{
80 CollisionModifierData *collmd = reinterpret_cast<CollisionModifierData *>(md);
81 Object *ob = ctx->object;
82
83 /* If collision is disabled, free the stale data and exit. */
84 if (!ob->pd || !ob->pd->deflect) {
85 if (!ob->pd) {
86 printf("CollisionModifier: collision settings are missing!\n");
87 }
88
89 free_data(md);
90 return;
91 }
92
93 if (mesh) {
94 float current_time = 0;
95 int mvert_num = 0;
96
97 mesh->vert_positions_for_write().copy_from(positions);
98 mesh->tag_positions_changed();
99
100 current_time = DEG_get_ctime(ctx->depsgraph);
101
102 if (G.debug & G_DEBUG_SIMDATA) {
103 printf("current_time %f, collmd->time_xnew %f\n", current_time, collmd->time_xnew);
104 }
105
106 mvert_num = mesh->verts_num;
107
108 if (current_time < collmd->time_xnew) {
109 free_data(reinterpret_cast<ModifierData *>(collmd));
110 }
111 else if (current_time == collmd->time_xnew) {
112 if (mvert_num != collmd->mvert_num) {
113 free_data(reinterpret_cast<ModifierData *>(collmd));
114 }
115 }
116
117 /* check if mesh has changed */
118 if (collmd->x && (mvert_num != collmd->mvert_num)) {
119 free_data(reinterpret_cast<ModifierData *>(collmd));
120 }
121
122 if (collmd->time_xnew == -1000) { /* first time */
123
124 mvert_num = mesh->verts_num;
125 collmd->x = MEM_new_array_uninitialized<float[3]>(size_t(mvert_num), __func__);
126 MutableSpan(reinterpret_cast<float3 *>(collmd->x), mvert_num)
127 .copy_from(mesh->vert_positions());
128
129 for (uint i = 0; i < mvert_num; i++) {
130 /* we save global positions */
131 mul_m4_v3(ob->object_to_world().ptr(), collmd->x[i]);
132 }
133
134 collmd->xnew = MEM_dupalloc(collmd->x); /* Frame end position. */
135 collmd->current_x = MEM_dupalloc(collmd->x); /* Inter-frame. */
136 collmd->current_xnew = MEM_dupalloc(collmd->x); /* Inter-frame. */
137 collmd->current_v = MEM_dupalloc(collmd->x); /* Inter-frame. */
138
139 collmd->mvert_num = mvert_num;
140
141 {
142 const Span<int3> corner_tris = mesh->corner_tris();
143 collmd->tri_num = corner_tris.size();
144 int (*vert_tris)[3] = MEM_new_array_uninitialized<int[3]>(collmd->tri_num, __func__);
146 mesh->corner_verts(),
147 corner_tris,
148 {reinterpret_cast<int3 *>(vert_tris), collmd->tri_num});
149 collmd->vert_tris = vert_tris;
150 }
151
152 /* create bounding box hierarchy */
153 collmd->bvhtree = bvhtree_build_from_mvert(collmd->x,
154 reinterpret_cast<int3 *>(collmd->vert_tris),
155 collmd->tri_num,
156 ob->pd->pdef_sboft);
157
158 collmd->time_x = collmd->time_xnew = current_time;
159 collmd->is_static = true;
160 }
161 else if (mvert_num == collmd->mvert_num) {
162 /* put positions to old positions */
163 float (*temp)[3] = collmd->x;
164 collmd->x = collmd->xnew;
165 collmd->xnew = temp;
166 collmd->time_x = collmd->time_xnew;
167
168 memcpy(collmd->xnew, mesh->vert_positions().data(), mvert_num * sizeof(float[3]));
169
170 bool is_static = true;
171
172 for (uint i = 0; i < mvert_num; i++) {
173 /* we save global positions */
174 mul_m4_v3(ob->object_to_world().ptr(), collmd->xnew[i]);
175
176 /* detect motion */
177 is_static = is_static && equals_v3v3(collmd->x[i], collmd->xnew[i]);
178 }
179
180 memcpy(collmd->current_xnew, collmd->x, mvert_num * sizeof(float[3]));
181 memcpy(collmd->current_x, collmd->x, mvert_num * sizeof(float[3]));
182
183 /* check if GUI setting has changed for bvh */
184 if (collmd->bvhtree) {
185 if (ob->pd->pdef_sboft != BLI_bvhtree_get_epsilon(collmd->bvhtree)) {
186 BLI_bvhtree_free(collmd->bvhtree);
188 collmd->current_x,
189 reinterpret_cast<const int3 *>(collmd->vert_tris),
190 collmd->tri_num,
191 ob->pd->pdef_sboft);
192 }
193 }
194
195 /* Happens on file load (ONLY when I un-comment changes in `readfile.cc`). */
196 if (!collmd->bvhtree) {
198 collmd->current_x,
199 reinterpret_cast<const int3 *>(collmd->vert_tris),
200 collmd->tri_num,
201 ob->pd->pdef_sboft);
202 }
203 else if (!collmd->is_static || !is_static) {
204 /* recalc static bounding boxes */
206 collmd->current_x,
207 collmd->current_xnew,
208 reinterpret_cast<const int3 *>(collmd->vert_tris),
209 collmd->tri_num,
210 true);
211 }
212
213 collmd->is_static = is_static;
214 collmd->time_xnew = current_time;
215 }
216 else if (mvert_num != collmd->mvert_num) {
217 free_data(reinterpret_cast<ModifierData *>(collmd));
218 }
219 }
220}
221
223{
224 DEG_add_depends_on_transform_relation(ctx->node, "Collision Modifier");
225}
226
227static void panel_draw(const bContext * /*C*/, Panel *panel)
228{
229 ui::Layout &layout = *panel->layout;
230
232
233 layout.label(RPT_("Settings are inside the Physics tab"), ICON_NONE);
234
235 modifier_error_message_draw(layout, ptr);
236}
237
238static void panel_register(ARegionType *region_type)
239{
241}
242
243static void blend_read(BlendDataReader * /*reader*/, ModifierData *md)
244{
245 CollisionModifierData *collmd = reinterpret_cast<CollisionModifierData *>(md);
246#if 0
247 /* TODO: #CollisionModifier should use point-cache
248 * + have proper reset events before enabling this. */
249 collmd->x = newdataadr(fd, collmd->x);
250 collmd->xnew = newdataadr(fd, collmd->xnew);
251 collmd->mfaces = newdataadr(fd, collmd->mfaces);
252
253 collmd->current_x = MEM_new_array_zeroed<float[3]>(collmd->mvert_num, "current_x");
254 collmd->current_xnew = MEM_new_array_zeroed<float[3]>(collmd->mvert_num, "current_xnew");
255 collmd->current_v = MEM_new_array_zeroed<float[3]>(collmd->mvert_num, "current_v");
256#endif
257
258 collmd->x = nullptr;
259 collmd->xnew = nullptr;
260 collmd->current_x = nullptr;
261 collmd->current_xnew = nullptr;
262 collmd->current_v = nullptr;
263 collmd->time_x = collmd->time_xnew = -1000;
264 collmd->mvert_num = 0;
265 collmd->tri_num = 0;
266 collmd->is_static = false;
267 collmd->bvhtree = nullptr;
268 collmd->vert_tris = nullptr;
269}
270
272 /*idname*/ "Collision",
273 /*name*/ N_("Collision"),
274 /*struct_name*/ "CollisionModifierData",
275 /*struct_size*/ sizeof(CollisionModifierData),
276 /*srna*/ &RNA_CollisionModifier,
279 /*icon*/ ICON_MOD_PHYSICS,
280
281 /*copy_data*/ nullptr,
282
283 /*deform_verts*/ deform_verts,
284 /*deform_matrices*/ nullptr,
285 /*deform_verts_EM*/ nullptr,
286 /*deform_matrices_EM*/ nullptr,
287 /*modify_mesh*/ nullptr,
288 /*modify_geometry_set*/ nullptr,
289
290 /*init_data*/ init_data,
291 /*required_data_mask*/ nullptr,
292 /*free_data*/ free_data,
293 /*is_disabled*/ nullptr,
294 /*update_depsgraph*/ update_depsgraph,
295 /*depends_on_time*/ depends_on_time,
296 /*depends_on_normals*/ nullptr,
297 /*foreach_ID_link*/ nullptr,
298 /*foreach_tex_link*/ nullptr,
299 /*free_runtime_data*/ nullptr,
300 /*panel_register*/ panel_register,
301 /*blend_write*/ nullptr,
302 /*blend_read*/ blend_read,
303 /*foreach_cache*/ nullptr,
304 /*foreach_working_space_color*/ nullptr,
305};
306
307} // namespace blender
#define RPT_(msgid)
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define INIT_DEFAULT_STRUCT_AFTER(struct_dst, member)
constexpr MutableSpan()=default
constexpr int64_t size() const
Definition BLI_span.hh:256
void vert_tris_from_corner_tris(Span< int > corner_verts, Span< int3 > corner_tris, MutableSpan< int3 > vert_tris)
BMesh Iterators.
PointerRNA * modifier_panel_get_property_pointers(Panel *panel, PointerRNA *r_ob_ptr)
unsigned int uint
Definition BKE_text.h:55
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
float BLI_bvhtree_get_epsilon(const BVHTree *tree)
static bool depends_on_time(Scene *, ModifierData *)
Definition MOD_build.cc:46
void BLI_bvhtree_free(BVHTree *tree)
static void init_data(ModifierData *md)
static void update_depsgraph(tGraphSliderOp *gso)
static void * newdataadr(FileData *fd, const void *adr, int64_t *r_alloc_len=nullptr)
Definition readfile.cc:1503
void DEG_add_depends_on_transform_relation(DepsNodeHandle *node_handle, const char *description)
@ G_DEBUG_SIMDATA
Global G
Definition blender.cc:57
void mul_m4_v3(const float M[4][4], float r[3])
MINLINE bool equals_v3v3(const float v1[3], const float v2[3]) ATTR_WARN_UNUSED_RESULT
void modifier_error_message_draw(ui::Layout &layout, PointerRNA *ptr)
struct BVHTree * bvhtree_build_from_mvert(const float(*positions)[3], const int3 *vert_tris, int tri_num, float epsilon)
Definition collision.cc:92
VecBase< int32_t, 3 > int3
static void free_data(ModifierData *md)
static void panel_register(ARegionType *region_type)
@ eModifierTypeFlag_AcceptsMesh
@ eModifierTypeFlag_Single
static void blend_read(BlendDataReader *, ModifierData *md)
ModifierTypeInfo modifierType_Collision
void bvhtree_update_from_mvert(struct BVHTree *bvhtree, const float(*positions)[3], const float(*positions_moving)[3], const int3 *vert_tris, int tri_num, bool moving)
Definition collision.cc:116
PanelType * modifier_panel_register(ARegionType *region_type, ModifierType type, PanelDrawFn draw)
static void panel_draw(const bContext *, Panel *panel)
static void deform_verts(ModifierData *md, const ModifierEvalContext *ctx, Mesh *mesh, MutableSpan< float3 > positions)
VecBase< float, 3 > float3
float DEG_get_ctime(const Depsgraph *graph)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
struct PartDeflect * pd
ui::Layout * layout
void label(StringRef name, int icon)
#define N_(msgid)