Blender V5.3
MOD_cast.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
10#include "BLI_math_vector_c.hh"
11#include "BLI_utildefines.hh"
12
13#include "BLT_translation.hh"
14
15#include "DNA_meshdata_types.h"
16#include "DNA_object_types.h"
17#include "DNA_screen_types.h"
18
19#include "BKE_deform.hh"
20#include "BKE_lib_query.hh"
21#include "BKE_modifier.hh"
22#include "BKE_object_types.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
35static void init_data(ModifierData *md)
36{
37 CastModifierData *cmd = reinterpret_cast<CastModifierData *>(md);
39}
40
41static bool is_disabled(const Scene * /*scene*/, ModifierData *md, bool /*use_render_params*/)
42{
43 CastModifierData *cmd = reinterpret_cast<CastModifierData *>(md);
44 short flag;
45
46 flag = cmd->flag & (MOD_CAST_X | MOD_CAST_Y | MOD_CAST_Z);
47
48 if ((cmd->fac == 0.0f) || flag == 0) {
49 return true;
50 }
51
52 return false;
53}
54
55static void required_data_mask(ModifierData *md, CustomData_MeshMasks *r_cddata_masks)
56{
57 CastModifierData *cmd = reinterpret_cast<CastModifierData *>(md);
58
59 /* Ask for vertex-groups if we need them. */
60 if (cmd->defgrp_name[0] != '\0') {
61 r_cddata_masks->vmask |= CD_MASK_MDEFORMVERT;
62 }
63}
64
65static void foreach_ID_link(ModifierData *md, Object *ob, IDWalkFunc walk, void *user_data)
66{
67 CastModifierData *cmd = reinterpret_cast<CastModifierData *>(md);
68
69 walk(user_data, ob, reinterpret_cast<ID **>(&cmd->object), IDWALK_CB_NOP);
70}
71
73{
74 CastModifierData *cmd = reinterpret_cast<CastModifierData *>(md);
75 if (cmd->object != nullptr) {
76 DEG_add_object_relation(ctx->node, cmd->object, DEG_OB_COMP_TRANSFORM, "Cast Modifier");
77 DEG_add_depends_on_transform_relation(ctx->node, "Cast Modifier");
78 }
79}
80
81static void sphere_do(CastModifierData *cmd,
82 const ModifierEvalContext * /*ctx*/,
83 Object *ob,
84 Mesh *mesh,
85 MutableSpan<float3> positions)
86{
87 const MDeformVert *dvert = nullptr;
88 const bool invert_vgroup = (cmd->flag & MOD_CAST_INVERT_VGROUP) != 0;
89
90 Object *ctrl_ob = nullptr;
91
92 int i, defgrp_index;
93 bool has_radius = false;
94 short flag, type;
95 float len = 0.0f;
96 float fac = cmd->fac;
97 float facm = 1.0f - fac;
98 const float fac_orig = fac;
99 float vec[3], center[3] = {0.0f, 0.0f, 0.0f};
100 float mat[4][4], imat[4][4];
101
102 flag = cmd->flag;
103 type = cmd->type; /* projection type: sphere or cylinder */
104
105 if (type == MOD_CAST_TYPE_CYLINDER) {
106 flag &= ~MOD_CAST_Z;
107 }
108
109 ctrl_ob = cmd->object;
110
111 /* The spheres center is {0, 0, 0} (the ob's own center in its local space),
112 * by default, but if the user defined a control object,
113 * we use its location, transformed to ob's local space. */
114 if (ctrl_ob) {
115 if (flag & MOD_CAST_USE_OB_TRANSFORM) {
116 invert_m4_m4(imat, ctrl_ob->object_to_world().ptr());
117 mul_m4_m4m4(mat, imat, ob->object_to_world().ptr());
118 invert_m4_m4(imat, mat);
119 }
120
121 invert_m4_m4(ob->runtime->world_to_object.ptr(), ob->object_to_world().ptr());
122 mul_v3_m4v3(center, ob->world_to_object().ptr(), ctrl_ob->object_to_world().location());
123 }
124
125 /* now we check which options the user wants */
126
127 /* 1) (flag was checked in the "if (ctrl_ob)" block above) */
128 /* 2) cmd->radius > 0.0f: only the vertices within this radius from
129 * the center of the effect should be deformed */
130 if (cmd->radius > FLT_EPSILON) {
131 has_radius = true;
132 }
133
134 /* 3) if we were given a vertex group name,
135 * only those vertices should be affected */
136 if (cmd->defgrp_name[0] != '\0') {
137 MOD_get_vgroup(ob, mesh, cmd->defgrp_name, &dvert, &defgrp_index);
138 }
139
140 if (flag & MOD_CAST_SIZE_FROM_RADIUS) {
141 len = cmd->radius;
142 }
143 else {
144 len = cmd->size;
145 }
146
147 if (len <= 0) {
148 for (i = 0; i < positions.size(); i++) {
149 len += len_v3v3(center, positions[i]);
150 }
151 len /= positions.size();
152
153 if (len == 0.0f) {
154 len = 10.0f;
155 }
156 }
157
158 for (i = 0; i < positions.size(); i++) {
159 float tmp_co[3];
160
161 copy_v3_v3(tmp_co, positions[i]);
162 if (ctrl_ob) {
163 if (flag & MOD_CAST_USE_OB_TRANSFORM) {
164 mul_m4_v3(mat, tmp_co);
165 }
166 else {
167 sub_v3_v3(tmp_co, center);
168 }
169 }
170
171 copy_v3_v3(vec, tmp_co);
172
173 if (type == MOD_CAST_TYPE_CYLINDER) {
174 vec[2] = 0.0f;
175 }
176
177 if (has_radius) {
178 if (len_v3(vec) > cmd->radius) {
179 continue;
180 }
181 }
182
183 if (dvert) {
184 const float weight = invert_vgroup ?
185 1.0f - BKE_defvert_find_weight(&dvert[i], defgrp_index) :
186 BKE_defvert_find_weight(&dvert[i], defgrp_index);
187
188 if (weight == 0.0f) {
189 continue;
190 }
191
192 fac = fac_orig * weight;
193 facm = 1.0f - fac;
194 }
195
196 normalize_v3(vec);
197
198 if (flag & MOD_CAST_X) {
199 tmp_co[0] = fac * vec[0] * len + facm * tmp_co[0];
200 }
201 if (flag & MOD_CAST_Y) {
202 tmp_co[1] = fac * vec[1] * len + facm * tmp_co[1];
203 }
204 if (flag & MOD_CAST_Z) {
205 tmp_co[2] = fac * vec[2] * len + facm * tmp_co[2];
206 }
207
208 if (ctrl_ob) {
209 if (flag & MOD_CAST_USE_OB_TRANSFORM) {
210 mul_m4_v3(imat, tmp_co);
211 }
212 else {
213 add_v3_v3(tmp_co, center);
214 }
215 }
216
217 copy_v3_v3(positions[i], tmp_co);
218 }
219}
220
222 const ModifierEvalContext * /*ctx*/,
223 Object *ob,
224 Mesh *mesh,
225 MutableSpan<float3> positions)
226{
227 const MDeformVert *dvert = nullptr;
228 int defgrp_index;
229 const bool invert_vgroup = (cmd->flag & MOD_CAST_INVERT_VGROUP) != 0;
230
231 Object *ctrl_ob = nullptr;
232
233 int i;
234 bool has_radius = false;
235 short flag;
236 float fac = cmd->fac;
237 float facm = 1.0f - fac;
238 const float fac_orig = fac;
239 float min[3], max[3], bb[8][3];
240 float center[3] = {0.0f, 0.0f, 0.0f};
241 float mat[4][4], imat[4][4];
242
243 flag = cmd->flag;
244
245 ctrl_ob = cmd->object;
246
247 /* now we check which options the user wants */
248
249 /* 1) (flag was checked in the "if (ctrl_ob)" block above) */
250 /* 2) cmd->radius > 0.0f: only the vertices within this radius from
251 * the center of the effect should be deformed */
252 if (cmd->radius > FLT_EPSILON) {
253 has_radius = true;
254 }
255
256 /* 3) if we were given a vertex group name,
257 * only those vertices should be affected */
258 if (cmd->defgrp_name[0] != '\0') {
259 MOD_get_vgroup(ob, mesh, cmd->defgrp_name, &dvert, &defgrp_index);
260 }
261
262 if (ctrl_ob) {
263 if (flag & MOD_CAST_USE_OB_TRANSFORM) {
264 invert_m4_m4(imat, ctrl_ob->object_to_world().ptr());
265 mul_m4_m4m4(mat, imat, ob->object_to_world().ptr());
266 invert_m4_m4(imat, mat);
267 }
268
269 invert_m4_m4(ob->runtime->world_to_object.ptr(), ob->object_to_world().ptr());
270 mul_v3_m4v3(center, ob->world_to_object().ptr(), ctrl_ob->object_to_world().location());
271 }
272
273 if ((flag & MOD_CAST_SIZE_FROM_RADIUS) && has_radius) {
274 for (i = 0; i < 3; i++) {
275 min[i] = -cmd->radius;
276 max[i] = cmd->radius;
277 }
278 }
279 else if (!(flag & MOD_CAST_SIZE_FROM_RADIUS) && cmd->size > 0) {
280 for (i = 0; i < 3; i++) {
281 min[i] = -cmd->size;
282 max[i] = cmd->size;
283 }
284 }
285 else {
286 /* get bound box */
287 /* We can't use the object's bound box because other modifiers
288 * may have changed the vertex data. */
290
291 /* Cast's center is the ob's own center in its local space,
292 * by default, but if the user defined a control object, we use
293 * its location, transformed to ob's local space. */
294 if (ctrl_ob) {
295 float vec[3];
296
297 /* let the center of the ctrl_ob be part of the bound box: */
298 minmax_v3v3_v3(min, max, center);
299
300 for (i = 0; i < positions.size(); i++) {
301 sub_v3_v3v3(vec, positions[i], center);
302 minmax_v3v3_v3(min, max, vec);
303 }
304 }
305 else {
306 for (i = 0; i < positions.size(); i++) {
307 minmax_v3v3_v3(min, max, positions[i]);
308 }
309 }
310
311 /* we want a symmetric bound box around the origin */
312 if (fabsf(min[0]) > fabsf(max[0])) {
313 max[0] = fabsf(min[0]);
314 }
315 if (fabsf(min[1]) > fabsf(max[1])) {
316 max[1] = fabsf(min[1]);
317 }
318 if (fabsf(min[2]) > fabsf(max[2])) {
319 max[2] = fabsf(min[2]);
320 }
321 min[0] = -max[0];
322 min[1] = -max[1];
323 min[2] = -max[2];
324 }
325
326 /* building our custom bounding box */
327 bb[0][0] = bb[2][0] = bb[4][0] = bb[6][0] = min[0];
328 bb[1][0] = bb[3][0] = bb[5][0] = bb[7][0] = max[0];
329 bb[0][1] = bb[1][1] = bb[4][1] = bb[5][1] = min[1];
330 bb[2][1] = bb[3][1] = bb[6][1] = bb[7][1] = max[1];
331 bb[0][2] = bb[1][2] = bb[2][2] = bb[3][2] = min[2];
332 bb[4][2] = bb[5][2] = bb[6][2] = bb[7][2] = max[2];
333
334 /* ready to apply the effect, one vertex at a time */
335 for (i = 0; i < positions.size(); i++) {
336 int octant, coord;
337 float d[3], dmax, apex[3], fbb;
338 float tmp_co[3];
339
340 copy_v3_v3(tmp_co, positions[i]);
341 if (ctrl_ob) {
342 if (flag & MOD_CAST_USE_OB_TRANSFORM) {
343 mul_m4_v3(mat, tmp_co);
344 }
345 else {
346 sub_v3_v3(tmp_co, center);
347 }
348 }
349
350 if (has_radius) {
351 if (fabsf(tmp_co[0]) > cmd->radius || fabsf(tmp_co[1]) > cmd->radius ||
352 fabsf(tmp_co[2]) > cmd->radius)
353 {
354 continue;
355 }
356 }
357
358 if (dvert) {
359 const float weight = invert_vgroup ?
360 1.0f - BKE_defvert_find_weight(&dvert[i], defgrp_index) :
361 BKE_defvert_find_weight(&dvert[i], defgrp_index);
362
363 if (weight == 0.0f) {
364 continue;
365 }
366
367 fac = fac_orig * weight;
368 facm = 1.0f - fac;
369 }
370
371 /* The algorithm used to project the vertices to their
372 * bounding box (bb) is pretty simple:
373 * for each vertex v:
374 * 1) find in which octant v is in;
375 * 2) find which outer "wall" of that octant is closer to v;
376 * 3) calculate factor (var fbb) to project v to that wall;
377 * 4) project. */
378
379 /* find in which octant this vertex is in */
380 octant = 0;
381 if (tmp_co[0] > 0.0f) {
382 octant += 1;
383 }
384 if (tmp_co[1] > 0.0f) {
385 octant += 2;
386 }
387 if (tmp_co[2] > 0.0f) {
388 octant += 4;
389 }
390
391 /* apex is the bb's vertex at the chosen octant */
392 copy_v3_v3(apex, bb[octant]);
393
394 /* find which bb plane is closest to this vertex ... */
395 d[0] = tmp_co[0] / apex[0];
396 d[1] = tmp_co[1] / apex[1];
397 d[2] = tmp_co[2] / apex[2];
398
399 /* ... (the closest has the higher (closer to 1) d value) */
400 dmax = d[0];
401 coord = 0;
402 if (d[1] > dmax) {
403 dmax = d[1];
404 coord = 1;
405 }
406 if (d[2] > dmax) {
407 // dmax = d[2]; /* commented, we don't need it */
408 coord = 2;
409 }
410
411 /* ok, now we know which coordinate of the vertex to use */
412
413 if (fabsf(tmp_co[coord]) < FLT_EPSILON) { /* avoid division by zero */
414 continue;
415 }
416
417 /* finally, this is the factor we wanted, to project the vertex
418 * to its bounding box (bb) */
419 fbb = apex[coord] / tmp_co[coord];
420
421 /* calculate the new vertex position */
422 if (flag & MOD_CAST_X) {
423 tmp_co[0] = facm * tmp_co[0] + fac * tmp_co[0] * fbb;
424 }
425 if (flag & MOD_CAST_Y) {
426 tmp_co[1] = facm * tmp_co[1] + fac * tmp_co[1] * fbb;
427 }
428 if (flag & MOD_CAST_Z) {
429 tmp_co[2] = facm * tmp_co[2] + fac * tmp_co[2] * fbb;
430 }
431
432 if (ctrl_ob) {
433 if (flag & MOD_CAST_USE_OB_TRANSFORM) {
434 mul_m4_v3(imat, tmp_co);
435 }
436 else {
437 add_v3_v3(tmp_co, center);
438 }
439 }
440
441 copy_v3_v3(positions[i], tmp_co);
442 }
443}
444
446 const ModifierEvalContext *ctx,
447 Mesh *mesh,
448 MutableSpan<float3> positions)
449{
450 CastModifierData *cmd = reinterpret_cast<CastModifierData *>(md);
451
452 if (cmd->type == MOD_CAST_TYPE_CUBOID) {
453 cuboid_do(cmd, ctx, ctx->object, mesh, positions);
454 }
455 else { /* MOD_CAST_TYPE_SPHERE or MOD_CAST_TYPE_CYLINDER */
456 sphere_do(cmd, ctx, ctx->object, mesh, positions);
457 }
458}
459
460static void panel_draw(const bContext * /*C*/, Panel *panel)
461{
462 ui::Layout &layout = *panel->layout;
464
465 PointerRNA ob_ptr;
467
468 PointerRNA cast_object_ptr = RNA_pointer_get(ptr, "object");
469
470 layout.use_property_split_set(true);
471
472 layout.prop(ptr, "cast_type", UI_ITEM_NONE, std::nullopt, ICON_NONE);
473
474 ui::Layout &row = layout.row(true, IFACE_("Axis"));
475 row.prop(ptr, "use_x", toggles_flag, std::nullopt, ICON_NONE);
476 row.prop(ptr, "use_y", toggles_flag, std::nullopt, ICON_NONE);
477 row.prop(ptr, "use_z", toggles_flag, std::nullopt, ICON_NONE);
478
479 layout.prop(ptr, "factor", UI_ITEM_NONE, std::nullopt, ICON_NONE);
480 layout.prop(ptr, "radius", UI_ITEM_NONE, std::nullopt, ICON_NONE);
481 layout.prop(ptr, "size", UI_ITEM_NONE, std::nullopt, ICON_NONE);
482 layout.prop(ptr, "use_radius_as_size", UI_ITEM_NONE, std::nullopt, ICON_NONE);
483
484 modifier_vgroup_ui(layout, ptr, &ob_ptr, "vertex_group", "invert_vertex_group", std::nullopt);
485
486 layout.prop(ptr, "object", UI_ITEM_NONE, std::nullopt, ICON_NONE);
487 if (cast_object_ptr) {
488 layout.prop(ptr, "use_transform", UI_ITEM_NONE, std::nullopt, ICON_NONE);
489 }
490
491 modifier_error_message_draw(layout, ptr);
492}
493
494static void panel_register(ARegionType *region_type)
495{
497}
498
500 /*idname*/ "Cast",
501 /*name*/ N_("Cast"),
502 /*struct_name*/ "CastModifierData",
503 /*struct_size*/ sizeof(CastModifierData),
504 /*srna*/ &RNA_CastModifier,
508 /*icon*/ ICON_MOD_CAST,
509
510 /*copy_data*/ BKE_modifier_copydata_generic,
511
512 /*deform_verts*/ deform_verts,
513 /*deform_matrices*/ nullptr,
514 /*deform_verts_EM*/ nullptr,
515 /*deform_matrices_EM*/ nullptr,
516 /*modify_mesh*/ nullptr,
517 /*modify_geometry_set*/ nullptr,
518
519 /*init_data*/ init_data,
520 /*required_data_mask*/ required_data_mask,
521 /*free_data*/ nullptr,
522 /*is_disabled*/ is_disabled,
523 /*update_depsgraph*/ update_depsgraph,
524 /*depends_on_time*/ nullptr,
525 /*depends_on_normals*/ nullptr,
526 /*foreach_ID_link*/ foreach_ID_link,
527 /*foreach_tex_link*/ nullptr,
528 /*free_runtime_data*/ nullptr,
529 /*panel_register*/ panel_register,
530 /*blend_write*/ nullptr,
531 /*blend_read*/ nullptr,
532 /*foreach_cache*/ nullptr,
533 /*foreach_working_space_color*/ nullptr,
534};
535
536} // namespace blender
support for deformation groups and hooks.
#define IFACE_(msgid)
Object is a sort of wrapper for general info.
#define UI_ITEM_NONE
#define INIT_MINMAX(min, max)
#define INIT_DEFAULT_STRUCT_AFTER(struct_dst, member)
#define CD_MASK_MDEFORMVERT
btScalar max
constexpr int64_t size() const
Definition BLI_span.hh:499
#define FLT_EPSILON
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
BMesh Iterators.
PointerRNA * modifier_panel_get_property_pointers(Panel *panel, PointerRNA *r_ob_ptr)
MINLINE void copy_v3_v3(float r[3], const float a[3])
ModifierTypeInfo modifierType_Cast
Definition MOD_cast.cc:499
void minmax_v3v3_v3(float min[3], float max[3], const float vec[3])
void mul_v3_m4v3(float r[3], const float mat[4][4], const float vec[3])
void MOD_get_vgroup(const Object *ob, const Mesh *mesh, const char *name, const MDeformVert **dvert, int *defgrp_index)
Definition MOD_util.cc:158
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
static void init_data(ModifierData *md)
void(*)(void *user_data, Object *ob, ID **idpoin, LibraryForeachIDCallbackFlag cb_flag) IDWalkFunc
static void update_depsgraph(tGraphSliderOp *gso)
static void foreach_ID_link(ModifierData *md, Object *ob, IDWalkFunc walk, void *user_data)
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
void DEG_add_object_relation(DepsNodeHandle *node_handle, Object *object, eDepsObjectComponentType component, const char *description)
void DEG_add_depends_on_transform_relation(DepsNodeHandle *node_handle, const char *description)
static void required_data_mask(ModifierData *, CustomData_MeshMasks *r_cddata_masks)
MINLINE void sub_v3_v3(float r[3], const float a[3])
void mul_m4_v3(const float M[4][4], float r[3])
void modifier_error_message_draw(ui::Layout &layout, PointerRNA *ptr)
float BKE_defvert_find_weight(const MDeformVert *dvert, int defgroup)
Definition deform.cc:779
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)
static void panel_register(ARegionType *region_type)
MINLINE float len_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT
@ eModifierTypeFlag_AcceptsCVs
@ eModifierTypeFlag_SupportsEditmode
@ eModifierTypeFlag_AcceptsVertexCosOnly
PointerRNA RNA_pointer_get(PointerRNA *ptr, const char *name)
MINLINE float normalize_v3(float n[3])
MINLINE void add_v3_v3(float r[3], const float a[3])
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
PanelType * modifier_panel_register(ARegionType *region_type, ModifierType type, PanelDrawFn draw)
static void panel_draw(const bContext *, Panel *panel)
static void cuboid_do(CastModifierData *cmd, const ModifierEvalContext *, Object *ob, Mesh *mesh, MutableSpan< float3 > positions)
Definition MOD_cast.cc:221
static void deform_verts(ModifierData *md, const ModifierEvalContext *ctx, Mesh *mesh, MutableSpan< float3 > positions)
static bool is_disabled(const Scene *, ModifierData *md, bool)
MINLINE float len_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
static void sphere_do(CastModifierData *cmd, const ModifierEvalContext *, Object *ob, Mesh *mesh, MutableSpan< float3 > positions)
Definition MOD_cast.cc:81
void BKE_modifier_copydata_generic(const ModifierData *md, ModifierData *md_dst, int flag)
#define fabsf
#define min(a, b)
Definition sort.cc:38
const c_style_mat & ptr() const
bke::ObjectRuntime * runtime
ui::Layout * layout
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)