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