Blender V5.3
MOD_simpledeform.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 <algorithm>
10
11#include "BLI_math_matrix_c.hh"
12#include "BLI_math_vector_c.hh"
13#include "BLI_task_c.hh"
14#include "BLI_utildefines.hh"
15#include "BLT_translation.hh"
16
17#include "DNA_object_types.h"
18#include "DNA_screen_types.h"
19
20#include "BKE_deform.hh"
21#include "BKE_lib_query.hh"
22#include "BKE_modifier.hh"
23
25#include "UI_resources.hh"
26
27#include "RNA_access.hh"
28#include "RNA_prototypes.hh"
29
30#include "MOD_ui_common.hh"
31#include "MOD_util.hh"
32
33namespace blender {
34
35#define BEND_EPS 0.000001f
36
50
51/* Re-maps the indices for X Y Z by shifting them up and wrapping, such that
52 * X = Y, Y = Z, Z = X (for X axis), and X = Z, Y = X, Z = Y (for Y axis). This
53 * exists because the deformations (excluding bend) are based on the Z axis.
54 * Having this helps avoid long, drawn out switches. */
55static const uint axis_map_table[3][3] = {
56 {1, 2, 0},
57 {2, 0, 1},
58 {0, 1, 2},
59};
60
61BLI_INLINE void copy_v3_v3_map(float a[3], const float b[3], const uint map[3])
62{
63 a[0] = b[map[0]];
64 a[1] = b[map[1]];
65 a[2] = b[map[2]];
66}
67
68BLI_INLINE void copy_v3_v3_unmap(float a[3], const float b[3], const uint map[3])
69{
70 a[map[0]] = b[0];
71 a[map[1]] = b[1];
72 a[map[2]] = b[2];
73}
74
79static void axis_limit(const int axis, const float limits[2], float co[3], float dcut[3])
80{
81 float val = co[axis];
82 val = std::max(limits[0], val);
83 val = std::min(limits[1], val);
84
85 dcut[axis] = co[axis] - val;
86 co[axis] = val;
87}
88
89static void simpleDeform_taper(const float factor,
90 const int /*axis*/,
91 const float dcut[3],
92 float r_co[3])
93{
94 float x = r_co[0], y = r_co[1], z = r_co[2];
95 float scale = z * factor;
96
97 r_co[0] = x + x * scale;
98 r_co[1] = y + y * scale;
99 r_co[2] = z;
100
101 add_v3_v3(r_co, dcut);
102}
103
104static void simpleDeform_stretch(const float factor,
105 const int /*axis*/,
106 const float dcut[3],
107 float r_co[3])
108{
109 float x = r_co[0], y = r_co[1], z = r_co[2];
110 float scale;
111
112 scale = (z * z * factor - factor + 1.0f);
113
114 r_co[0] = x * scale;
115 r_co[1] = y * scale;
116 r_co[2] = z * (1.0f + factor);
117
118 add_v3_v3(r_co, dcut);
119}
120
121static void simpleDeform_twist(const float factor,
122 const int /*axis*/,
123 const float *dcut,
124 float r_co[3])
125{
126 float x = r_co[0], y = r_co[1], z = r_co[2];
127 float theta, sint, cost;
128
129 theta = z * factor;
130 sint = sinf(theta);
131 cost = cosf(theta);
132
133 r_co[0] = x * cost - y * sint;
134 r_co[1] = x * sint + y * cost;
135 r_co[2] = z;
136
137 add_v3_v3(r_co, dcut);
138}
139
140static void simpleDeform_bend(const float factor,
141 const int axis,
142 const float dcut[3],
143 float r_co[3])
144{
145 float x = r_co[0], y = r_co[1], z = r_co[2];
146 float theta, sint, cost;
147
148 BLI_assert(!(fabsf(factor) < BEND_EPS));
149
150 switch (axis) {
151 case 0:
153 case 1:
154 theta = z * factor;
155 break;
156 default:
157 theta = x * factor;
158 }
159 sint = sinf(theta);
160 cost = cosf(theta);
161
162 /* NOTE: the operations below a susceptible to float precision errors
163 * regarding the order of operations, take care when changing, see: #85470 */
164 switch (axis) {
165 case 0:
166 r_co[0] = x;
167 r_co[1] = y * cost + (1.0f - cost) / factor;
168 r_co[2] = -(y - 1.0f / factor) * sint;
169 {
170 r_co[0] += dcut[0];
171 r_co[1] += sint * dcut[2];
172 r_co[2] += cost * dcut[2];
173 }
174 break;
175 case 1:
176 r_co[0] = x * cost + (1.0f - cost) / factor;
177 r_co[1] = y;
178 r_co[2] = -(x - 1.0f / factor) * sint;
179 {
180 r_co[0] += sint * dcut[2];
181 r_co[1] += dcut[1];
182 r_co[2] += cost * dcut[2];
183 }
184 break;
185 default:
186 r_co[0] = -(y - 1.0f / factor) * sint;
187 r_co[1] = y * cost + (1.0f - cost) / factor;
188 r_co[2] = z;
189 {
190 r_co[0] += cost * dcut[0];
191 r_co[1] += sint * dcut[0];
192 r_co[2] += dcut[2];
193 }
194 }
195}
196
197static void simple_helper(void *__restrict userdata,
198 const int iter,
199 const TaskParallelTLS *__restrict /*tls*/)
200{
201 const DeformUserData *curr_deform_data = static_cast<const DeformUserData *>(userdata);
203 curr_deform_data->dvert, iter, curr_deform_data->vgroup, curr_deform_data->invert_vgroup);
204 const uint *axis_map = axis_map_table[(curr_deform_data->mode != MOD_SIMPLEDEFORM_MODE_BEND) ?
205 curr_deform_data->deform_axis :
206 2];
207 const float base_limit[2] = {0.0f, 0.0f};
208
209 if (weight != 0.0f) {
210 float co[3], dcut[3] = {0.0f, 0.0f, 0.0f};
211
212 if (curr_deform_data->transf) {
213 BLI_space_transform_apply(curr_deform_data->transf, curr_deform_data->vertexCos[iter]);
214 }
215
216 copy_v3_v3(co, curr_deform_data->vertexCos[iter]);
217
218 /* Apply axis limits, and axis mappings */
219 if (curr_deform_data->lock_axis & MOD_SIMPLEDEFORM_LOCK_AXIS_X) {
220 axis_limit(0, base_limit, co, dcut);
221 }
222 if (curr_deform_data->lock_axis & MOD_SIMPLEDEFORM_LOCK_AXIS_Y) {
223 axis_limit(1, base_limit, co, dcut);
224 }
225 if (curr_deform_data->lock_axis & MOD_SIMPLEDEFORM_LOCK_AXIS_Z) {
226 axis_limit(2, base_limit, co, dcut);
227 }
228 axis_limit(curr_deform_data->limit_axis, curr_deform_data->smd_limit, co, dcut);
229
230 /* apply the deform to a mapped copy of the vertex, and then re-map it back. */
231 float co_remap[3];
232 float dcut_remap[3];
233 copy_v3_v3_map(co_remap, co, axis_map);
234 copy_v3_v3_map(dcut_remap, dcut, axis_map);
235 switch (curr_deform_data->mode) {
237 /* Apply deform. */
239 curr_deform_data->smd_factor, curr_deform_data->deform_axis, dcut_remap, co_remap);
240 break;
242 /* Apply deform. */
244 curr_deform_data->smd_factor, curr_deform_data->deform_axis, dcut_remap, co_remap);
245 break;
247 /* Apply deform. */
249 curr_deform_data->smd_factor, curr_deform_data->deform_axis, dcut_remap, co_remap);
250 break;
252 /* Apply deform. */
254 curr_deform_data->smd_factor, curr_deform_data->deform_axis, dcut_remap, co_remap);
255 break;
256 default:
257 return; /* No simple-deform mode? */
258 }
259 copy_v3_v3_unmap(co, co_remap, axis_map);
260
261 /* Use vertex weight coefficient of the linear interpolation. */
263 curr_deform_data->vertexCos[iter], curr_deform_data->vertexCos[iter], co, weight);
264
265 if (curr_deform_data->transf) {
266 BLI_space_transform_invert(curr_deform_data->transf, curr_deform_data->vertexCos[iter]);
267 }
268 }
269}
270
271/* simple deform modifier */
273 const ModifierEvalContext * /*ctx*/,
274 Object *ob,
275 Mesh *mesh,
276 float (*vertexCos)[3],
277 int verts_num)
278{
279 int i;
280 float smd_limit[2], smd_factor;
281 SpaceTransform *transf = nullptr, tmp_transf;
282 int vgroup;
283 const MDeformVert *dvert;
284
285 /* This is historically the lock axis, _not_ the deform axis as the name would imply */
286 const int deform_axis = std::clamp(int(smd->deform_axis), 0, 2);
287 int lock_axis = smd->axis;
288 if (smd->mode == MOD_SIMPLEDEFORM_MODE_BEND) { /* Bend mode shouldn't have any lock axis */
289 lock_axis = 0;
290 }
291 else {
292 /* Don't lock axis if it is the chosen deform axis, as this flattens
293 * the geometry */
294 if (deform_axis == 0) {
296 }
297 if (deform_axis == 1) {
299 }
300 if (deform_axis == 2) {
302 }
303 }
304
305 /* Safe-check */
306 if (smd->origin == ob) {
307 smd->origin = nullptr; /* No self references */
308 }
309
310 smd->limit[0] = std::max(smd->limit[0], 0.0f);
311 smd->limit[0] = std::min(smd->limit[0], 1.0f);
312
313 smd->limit[0] = min_ff(smd->limit[0], smd->limit[1]); /* Upper limit >= than lower limit */
314
315 /* Calculate matrix to convert between coordinate spaces. */
316 if (smd->origin != nullptr) {
317 transf = &tmp_transf;
318 BLI_SPACE_TRANSFORM_SETUP(transf, ob, smd->origin);
319 }
320
321 /* Update limits if needed */
322 int limit_axis = deform_axis;
323 if (smd->mode == MOD_SIMPLEDEFORM_MODE_BEND) {
324 /* Bend is a special case. */
325 switch (deform_axis) {
326 case 0:
328 case 1:
329 limit_axis = 2;
330 break;
331 default:
332 limit_axis = 0;
333 }
334 }
335
336 {
337 float lower = FLT_MAX;
338 float upper = -FLT_MAX;
339
340 for (i = 0; i < verts_num; i++) {
341 float tmp[3];
342 copy_v3_v3(tmp, vertexCos[i]);
343
344 if (transf) {
345 BLI_space_transform_apply(transf, tmp);
346 }
347
348 lower = min_ff(lower, tmp[limit_axis]);
349 upper = max_ff(upper, tmp[limit_axis]);
350 }
351
352 /* SMD values are normalized to the BV, calculate the absolute values */
353 smd_limit[1] = lower + (upper - lower) * smd->limit[1];
354 smd_limit[0] = lower + (upper - lower) * smd->limit[0];
355
356 smd_factor = smd->factor / max_ff(FLT_EPSILON, smd_limit[1] - smd_limit[0]);
357 }
358
359 if (smd->mode == MOD_SIMPLEDEFORM_MODE_BEND) {
360 if (fabsf(smd_factor) < BEND_EPS) {
361 return;
362 }
363 }
364
365 MOD_get_vgroup(ob, mesh, smd->vgroup_name, &dvert, &vgroup);
366 const bool invert_vgroup = (smd->flag & MOD_SIMPLEDEFORM_FLAG_INVERT_VGROUP) != 0;
367
368 /* Build our data. */
369 DeformUserData deform_pool_data{};
370 deform_pool_data.mode = smd->mode;
371 deform_pool_data.smd_factor = smd_factor;
372 deform_pool_data.deform_axis = deform_axis;
373 deform_pool_data.transf = transf;
374 deform_pool_data.vertexCos = vertexCos;
375 deform_pool_data.invert_vgroup = invert_vgroup;
376 deform_pool_data.lock_axis = lock_axis;
377 deform_pool_data.vgroup = vgroup;
378 deform_pool_data.smd_limit[0] = smd_limit[0];
379 deform_pool_data.smd_limit[1] = smd_limit[1];
380 deform_pool_data.dvert = dvert;
381 deform_pool_data.limit_axis = limit_axis;
382
383 /* Do deformation. */
384 TaskParallelSettings settings;
387 0, verts_num, static_cast<void *>(&deform_pool_data), simple_helper, &settings);
388}
389
390/* SimpleDeform */
391static void init_data(ModifierData *md)
392{
393 SimpleDeformModifierData *smd = reinterpret_cast<SimpleDeformModifierData *>(md);
395}
396
397static void required_data_mask(ModifierData *md, CustomData_MeshMasks *r_cddata_masks)
398{
399 SimpleDeformModifierData *smd = reinterpret_cast<SimpleDeformModifierData *>(md);
400
401 /* Ask for vertex-groups if we need them. */
402 if (smd->vgroup_name[0] != '\0') {
403 r_cddata_masks->vmask |= CD_MASK_MDEFORMVERT;
404 }
405}
406
407static void foreach_ID_link(ModifierData *md, Object *ob, IDWalkFunc walk, void *user_data)
408{
409 SimpleDeformModifierData *smd = reinterpret_cast<SimpleDeformModifierData *>(md);
410 walk(user_data, ob, reinterpret_cast<ID **>(&smd->origin), IDWALK_CB_NOP);
411}
412
414{
415 SimpleDeformModifierData *smd = reinterpret_cast<SimpleDeformModifierData *>(md);
416 if (smd->origin != nullptr) {
418 ctx->node, smd->origin, DEG_OB_COMP_TRANSFORM, "SimpleDeform Modifier");
419 DEG_add_depends_on_transform_relation(ctx->node, "SimpleDeform Modifier");
420 }
421}
422
424 const ModifierEvalContext *ctx,
425 Mesh *mesh,
426 MutableSpan<float3> positions)
427{
428 SimpleDeformModifierData *sdmd = reinterpret_cast<SimpleDeformModifierData *>(md);
430 ctx,
431 ctx->object,
432 mesh,
433 reinterpret_cast<float (*)[3]>(positions.data()),
434 positions.size());
435}
436
437static void panel_draw(const bContext * /*C*/, Panel *panel)
438{
439 ui::Layout &layout = *panel->layout;
440
441 PointerRNA ob_ptr;
443
444 int deform_method = RNA_enum_get(ptr, "deform_method");
445
446 ui::Layout &row = layout.row(false);
447 row.prop(ptr, "deform_method", ui::ITEM_R_EXPAND, std::nullopt, ICON_NONE);
448
449 layout.use_property_split_set(true);
450
452 layout.prop(ptr, "factor", UI_ITEM_NONE, std::nullopt, ICON_NONE);
453 }
454 else {
455 layout.prop(ptr, "angle", UI_ITEM_NONE, std::nullopt, ICON_NONE);
456 }
457
458 layout.prop(ptr, "origin", UI_ITEM_NONE, std::nullopt, ICON_NONE);
459 layout.prop(ptr, "deform_axis", ui::ITEM_R_EXPAND, std::nullopt, ICON_NONE);
460
461 modifier_error_message_draw(layout, ptr);
462}
463
464static void restrictions_panel_draw(const bContext * /*C*/, Panel *panel)
465{
466 ui::Layout &layout = *panel->layout;
468
469 PointerRNA ob_ptr;
471
472 int deform_method = RNA_enum_get(ptr, "deform_method");
473
474 layout.use_property_split_set(true);
475
476 layout.prop(ptr, "limits", ui::ITEM_R_SLIDER, std::nullopt, ICON_NONE);
477
478 if (ELEM(deform_method,
482 {
483 int deform_axis = RNA_enum_get(ptr, "deform_axis");
484
485 ui::Layout &row = layout.row(true, IFACE_("Lock"));
486 if (deform_axis != 0) {
487 row.prop(ptr, "lock_x", toggles_flag, std::nullopt, ICON_NONE);
488 }
489 if (deform_axis != 1) {
490 row.prop(ptr, "lock_y", toggles_flag, std::nullopt, ICON_NONE);
491 }
492 if (deform_axis != 2) {
493 row.prop(ptr, "lock_z", toggles_flag, std::nullopt, ICON_NONE);
494 }
495 }
496
497 modifier_vgroup_ui(layout, ptr, &ob_ptr, "vertex_group", "invert_vertex_group", std::nullopt);
498}
499
500static void panel_register(ARegionType *region_type)
501{
505 region_type, "restrictions", "Restrictions", nullptr, restrictions_panel_draw, panel_type);
506}
507
509 /*idname*/ "SimpleDeform",
510 /*name*/ N_("SimpleDeform"),
511 /*struct_name*/ "SimpleDeformModifierData",
512 /*struct_size*/ sizeof(SimpleDeformModifierData),
513 /*srna*/ &RNA_SimpleDeformModifier,
515
519 /*icon*/ ICON_MOD_SIMPLEDEFORM,
520
521 /*copy_data*/ BKE_modifier_copydata_generic,
522
523 /*deform_verts*/ deform_verts,
524 /*deform_matrices*/ nullptr,
525 /*deform_verts_EM*/ nullptr,
526 /*deform_matrices_EM*/ nullptr,
527 /*modify_mesh*/ nullptr,
528 /*modify_geometry_set*/ nullptr,
529
530 /*init_data*/ init_data,
531 /*required_data_mask*/ required_data_mask,
532 /*free_data*/ nullptr,
533 /*is_disabled*/ nullptr,
534 /*update_depsgraph*/ update_depsgraph,
535 /*depends_on_time*/ nullptr,
536 /*depends_on_normals*/ nullptr,
537 /*foreach_ID_link*/ foreach_ID_link,
538 /*foreach_tex_link*/ nullptr,
539 /*free_runtime_data*/ nullptr,
540 /*panel_register*/ panel_register,
541 /*blend_write*/ nullptr,
542 /*blend_read*/ nullptr,
543 /*foreach_cache*/ nullptr,
544 /*foreach_working_space_color*/ nullptr,
545};
546
547} // namespace blender
support for deformation groups and hooks.
#define ATTR_FALLTHROUGH
#define BLI_ALIGN_STRUCT
#define BLI_SPACE_TRANSFORM_SETUP(data, local, target)
#define IFACE_(msgid)
Object is a sort of wrapper for general info.
#define BEND_EPS
#define UI_ITEM_NONE
#define BLI_INLINE
#define INIT_DEFAULT_STRUCT_AFTER(struct_dst, member)
#define ELEM(...)
#define CD_MASK_MDEFORMVERT
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr T * data() const
Definition BLI_span.hh:545
#define FLT_MAX
#define FLT_EPSILON
float3x3 scale(float3x3 mat, float2 scale)
BMesh Iterators.
MINLINE float min_ff(float a, float b)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static void simpleDeform_taper(const float factor, const int, const float dcut[3], float r_co[3])
int RNA_enum_get(PointerRNA *ptr, const char *name)
PointerRNA * modifier_panel_get_property_pointers(Panel *panel, PointerRNA *r_ob_ptr)
BLI_INLINE void BLI_parallel_range_settings_defaults(TaskParallelSettings *settings)
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
void MOD_get_vgroup(const Object *ob, const Mesh *mesh, const char *name, const MDeformVert **dvert, int *defgrp_index)
Definition MOD_util.cc:158
@ eModifierType_SimpleDeform
static void init_data(ModifierData *md)
void(*)(void *user_data, Object *ob, ID **idpoin, LibraryForeachIDCallbackFlag cb_flag) IDWalkFunc
static void update_depsgraph(tGraphSliderOp *gso)
void BLI_space_transform_invert(const struct SpaceTransform *data, float co[3])
static void foreach_ID_link(ModifierData *md, Object *ob, IDWalkFunc walk, void *user_data)
void BLI_task_parallel_range(int start, int stop, void *userdata, TaskParallelRangeFunc func, const TaskParallelSettings *settings)
void DEG_add_object_relation(DepsNodeHandle *node_handle, Object *object, eDepsObjectComponentType component, const char *description)
static void restrictions_panel_draw(const bContext *, Panel *panel)
void DEG_add_depends_on_transform_relation(DepsNodeHandle *node_handle, const char *description)
static void required_data_mask(ModifierData *, CustomData_MeshMasks *r_cddata_masks)
ModifierTypeInfo modifierType_SimpleDeform
void modifier_error_message_draw(ui::Layout &layout, PointerRNA *ptr)
void modifier_vgroup_ui(ui::Layout &layout, PointerRNA *ptr, PointerRNA *ob_ptr, const StringRefNull vgroup_prop, const std::optional< StringRefNull > invert_vgroup_prop, const std::optional< StringRefNull > text)
BLI_INLINE void copy_v3_v3_map(float a[3], const float b[3], const uint map[3])
static void panel_register(ARegionType *region_type)
float BKE_defvert_array_find_weight_safe(const MDeformVert *dvert, int index, int defgroup, bool invert)
Definition deform.cc:785
static void simpleDeform_twist(const float factor, const int, const float *dcut, float r_co[3])
void interp_v3_v3v3(float r[3], const float a[3], const float b[3], float t)
@ MOD_SIMPLEDEFORM_FLAG_INVERT_VGROUP
@ MOD_SIMPLEDEFORM_MODE_STRETCH
@ eModifierTypeFlag_AcceptsCVs
@ eModifierTypeFlag_EnableInEditmode
@ eModifierTypeFlag_SupportsEditmode
@ eModifierTypeFlag_AcceptsMesh
@ eModifierTypeFlag_AcceptsVertexCosOnly
MINLINE void add_v3_v3(float r[3], const float a[3])
static void SimpleDeformModifier_do(SimpleDeformModifierData *smd, const ModifierEvalContext *, Object *ob, Mesh *mesh, float(*vertexCos)[3], int verts_num)
BLI_INLINE void copy_v3_v3_unmap(float a[3], const float b[3], const uint map[3])
MINLINE float max_ff(float a, float b)
PanelType * modifier_panel_register(ARegionType *region_type, ModifierType type, PanelDrawFn draw)
static const uint axis_map_table[3][3]
static void simple_helper(void *__restrict userdata, const int iter, const TaskParallelTLS *__restrict)
static void simpleDeform_stretch(const float factor, const int, const float dcut[3], float r_co[3])
static void panel_draw(const bContext *, Panel *panel)
PanelType * modifier_subpanel_register(ARegionType *region_type, const char *name, const char *label, PanelDrawFn draw_header, PanelDrawFn draw, PanelType *parent)
static void deform_verts(ModifierData *md, const ModifierEvalContext *ctx, Mesh *mesh, MutableSpan< float3 > positions)
void BLI_space_transform_apply(const struct SpaceTransform *data, float co[3])
static void simpleDeform_bend(const float factor, const int axis, const float dcut[3], float r_co[3])
static void axis_limit(const int axis, const float limits[2], float co[3], float dcut[3])
void BKE_modifier_copydata_generic(const ModifierData *md, ModifierData *md_dst, int flag)
#define fabsf
#define sinf
#define cosf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
const MDeformVert * dvert
const SpaceTransform * transf
ui::Layout * layout
SimpleDeformModifierLockAxis deform_axis
void use_property_split_set(bool is_sep)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)
Layout & row(bool align)
#define N_(msgid)