Blender V4.5
MOD_correctivesmooth.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
10
11#include "BLI_math_base.hh"
12#include "BLI_math_matrix.h"
13#include "BLI_math_vector.h"
14#include "BLI_utildefines.h"
15
16#include "BLT_translation.hh"
17
18#include "DNA_defaults.h"
19#include "DNA_mesh_types.h"
20#include "DNA_meshdata_types.h"
21#include "DNA_object_types.h"
22#include "DNA_screen_types.h"
23
24#include "MEM_guardedalloc.h"
25
26#include "BKE_deform.hh"
27#include "BKE_editmesh.hh"
28
29#include "UI_interface.hh"
30#include "UI_resources.hh"
31
32#include "RNA_access.hh"
33#include "RNA_prototypes.hh"
34
35#include "MOD_modifiertypes.hh"
36#include "MOD_ui_common.hh"
37#include "MOD_util.hh"
38
39#include "BLO_read_write.hh"
40
42
43// #define DEBUG_TIME
44
45#ifdef DEBUG_TIME
46# include "BLI_time.h"
47# include "BLI_time_utildefines.h"
48#endif
49
50#include "BLI_strict_flags.h" /* IWYU pragma: keep. Keep last. */
51
62
63static void copy_data(const ModifierData *md, ModifierData *target, const int flag)
64{
67
69
70 if (csmd->bind_coords) {
71 tcsmd->bind_coords = static_cast<float(*)[3]>(MEM_dupallocN(csmd->bind_coords));
72 }
73
74 tcsmd->delta_cache.deltas = nullptr;
75 tcsmd->delta_cache.deltas_num = 0;
76}
77
79{
82
83 csmd->bind_coords_num = 0;
84}
85
91
92static void required_data_mask(ModifierData *md, CustomData_MeshMasks *r_cddata_masks)
93{
95
96 /* ask for vertex groups if we need them */
97 if (csmd->defgrp_name[0] != '\0') {
98 r_cddata_masks->vmask |= CD_MASK_MDEFORMVERT;
99 }
100}
101
102/* check individual weights for changes and cache values */
103static void mesh_get_weights(const MDeformVert *dvert,
104 const int defgrp_index,
105 const uint verts_num,
106 const bool use_invert_vgroup,
107 float *smooth_weights)
108{
109 uint i;
110
111 for (i = 0; i < verts_num; i++, dvert++) {
112 const float w = BKE_defvert_find_weight(dvert, defgrp_index);
113
114 if (use_invert_vgroup == false) {
115 smooth_weights[i] = w;
116 }
117 else {
118 smooth_weights[i] = 1.0f - w;
119 }
120 }
121}
122
123static void mesh_get_boundaries(Mesh *mesh, float *smooth_weights)
124{
125 const blender::Span<blender::int2> edges = mesh->edges();
126 const blender::OffsetIndices faces = mesh->faces();
127 const blender::Span<int> corner_edges = mesh->corner_edges();
128
129 /* Flag boundary edges so only boundaries are set to 1. */
130 uint8_t *boundaries = MEM_calloc_arrayN<uint8_t>(size_t(edges.size()), __func__);
131
132 for (const int64_t i : faces.index_range()) {
133 for (const int edge : corner_edges.slice(faces[i])) {
134 uint8_t *e_value = &boundaries[edge];
135 *e_value |= uint8_t((*e_value) + 1);
136 }
137 }
138
139 for (const int64_t i : edges.index_range()) {
140 if (boundaries[i] == 1) {
141 smooth_weights[edges[i][0]] = 0.0f;
142 smooth_weights[edges[i][1]] = 0.0f;
143 }
144 }
145
146 MEM_freeN(boundaries);
147}
148
149/* -------------------------------------------------------------------- */
150/* Simple Weighted Smoothing
151 *
152 * (average of surrounding verts)
153 */
155 Mesh *mesh,
157 const float *smooth_weights,
158 uint iterations)
159{
160 const float lambda = csmd->lambda;
161 int i;
162
163 const int edges_num = mesh->edges_num;
164 const blender::Span<blender::int2> edges = mesh->edges();
165
166 struct SmoothingData_Simple {
167 float delta[3];
168 };
169 SmoothingData_Simple *smooth_data = MEM_calloc_arrayN<SmoothingData_Simple>(
170 size_t(vertexCos.size()), __func__);
171
172 float *vertex_edge_count_div = MEM_calloc_arrayN<float>(size_t(vertexCos.size()), __func__);
173
174 /* calculate as floats to avoid int->float conversion in #smooth_iter */
175 for (i = 0; i < edges_num; i++) {
176 vertex_edge_count_div[edges[i][0]] += 1.0f;
177 vertex_edge_count_div[edges[i][1]] += 1.0f;
178 }
179
180 /* a little confusing, but we can include 'lambda' and smoothing weight
181 * here to avoid multiplying for every iteration */
182 if (smooth_weights == nullptr) {
183 for (i = 0; i < vertexCos.size(); i++) {
184 vertex_edge_count_div[i] = lambda * (vertex_edge_count_div[i] ?
185 (1.0f / vertex_edge_count_div[i]) :
186 1.0f);
187 }
188 }
189 else {
190 for (i = 0; i < vertexCos.size(); i++) {
191 vertex_edge_count_div[i] = smooth_weights[i] * lambda *
192 (vertex_edge_count_div[i] ? (1.0f / vertex_edge_count_div[i]) :
193 1.0f);
194 }
195 }
196
197 /* -------------------------------------------------------------------- */
198 /* Main Smoothing Loop */
199
200 while (iterations--) {
201 for (i = 0; i < edges_num; i++) {
202 SmoothingData_Simple *sd_v1;
203 SmoothingData_Simple *sd_v2;
204 float edge_dir[3];
205
206 sub_v3_v3v3(edge_dir, vertexCos[edges[i][1]], vertexCos[edges[i][0]]);
207
208 sd_v1 = &smooth_data[edges[i][0]];
209 sd_v2 = &smooth_data[edges[i][1]];
210
211 add_v3_v3(sd_v1->delta, edge_dir);
212 sub_v3_v3(sd_v2->delta, edge_dir);
213 }
214
215 for (i = 0; i < vertexCos.size(); i++) {
216 SmoothingData_Simple *sd = &smooth_data[i];
217 madd_v3_v3fl(vertexCos[i], sd->delta, vertex_edge_count_div[i]);
218 /* zero for the next iteration (saves memset on entire array) */
219 memset(sd, 0, sizeof(*sd));
220 }
221 }
222
223 MEM_freeN(vertex_edge_count_div);
224 MEM_freeN(smooth_data);
225}
226
227/* -------------------------------------------------------------------- */
228/* Edge-Length Weighted Smoothing
229 */
231 Mesh *mesh,
233 const float *smooth_weights,
234 uint iterations)
235{
236 const float eps = FLT_EPSILON * 10.0f;
237 const uint edges_num = uint(mesh->edges_num);
238 /* NOTE: the way this smoothing method works, its approx half as strong as the simple-smooth,
239 * and 2.0 rarely spikes, double the value for consistent behavior. */
240 const float lambda = csmd->lambda * 2.0f;
241 const blender::Span<blender::int2> edges = mesh->edges();
242 uint i;
243
244 struct SmoothingData_Weighted {
245 float delta[3];
246 float edge_length_sum;
247 };
248 SmoothingData_Weighted *smooth_data = MEM_calloc_arrayN<SmoothingData_Weighted>(
249 size_t(vertexCos.size()), __func__);
250
251 /* calculate as floats to avoid int->float conversion in #smooth_iter */
252 float *vertex_edge_count = MEM_calloc_arrayN<float>(size_t(vertexCos.size()), __func__);
253 for (i = 0; i < edges_num; i++) {
254 vertex_edge_count[edges[i][0]] += 1.0f;
255 vertex_edge_count[edges[i][1]] += 1.0f;
256 }
257
258 /* -------------------------------------------------------------------- */
259 /* Main Smoothing Loop */
260
261 while (iterations--) {
262 for (i = 0; i < edges_num; i++) {
263 SmoothingData_Weighted *sd_v1;
264 SmoothingData_Weighted *sd_v2;
265 float edge_dir[3];
266 float edge_dist;
267
268 sub_v3_v3v3(edge_dir, vertexCos[edges[i][1]], vertexCos[edges[i][0]]);
269 edge_dist = len_v3(edge_dir);
270
271 /* weight by distance */
272 mul_v3_fl(edge_dir, edge_dist);
273
274 sd_v1 = &smooth_data[edges[i][0]];
275 sd_v2 = &smooth_data[edges[i][1]];
276
277 add_v3_v3(sd_v1->delta, edge_dir);
278 sub_v3_v3(sd_v2->delta, edge_dir);
279
280 sd_v1->edge_length_sum += edge_dist;
281 sd_v2->edge_length_sum += edge_dist;
282 }
283
284 if (smooth_weights == nullptr) {
285 /* fast-path */
286 for (i = 0; i < vertexCos.size(); i++) {
287 SmoothingData_Weighted *sd = &smooth_data[i];
288 /* Divide by sum of all neighbor distances (weighted) and amount of neighbors,
289 * (mean average). */
290 const float div = sd->edge_length_sum * vertex_edge_count[i];
291 if (div > eps) {
292#if 0
293 /* first calculate the new location */
294 mul_v3_fl(sd->delta, 1.0f / div);
295 /* then interpolate */
296 madd_v3_v3fl(vertexCos[i], sd->delta, lambda);
297#else
298 /* do this in one step */
299 madd_v3_v3fl(vertexCos[i], sd->delta, lambda / div);
300#endif
301 }
302 /* zero for the next iteration (saves memset on entire array) */
303 memset(sd, 0, sizeof(*sd));
304 }
305 }
306 else {
307 for (i = 0; i < vertexCos.size(); i++) {
308 SmoothingData_Weighted *sd = &smooth_data[i];
309 const float div = sd->edge_length_sum * vertex_edge_count[i];
310 if (div > eps) {
311 const float lambda_w = lambda * smooth_weights[i];
312 madd_v3_v3fl(vertexCos[i], sd->delta, lambda_w / div);
313 }
314
315 memset(sd, 0, sizeof(*sd));
316 }
317 }
318 }
319
320 MEM_freeN(vertex_edge_count);
321 MEM_freeN(smooth_data);
322}
323
325 Mesh *mesh,
327 const float *smooth_weights,
328 uint iterations)
329{
330 switch (csmd->smooth_type) {
332 smooth_iter__length_weight(csmd, mesh, vertexCos, smooth_weights, iterations);
333 break;
334
335 /* case MOD_CORRECTIVESMOOTH_SMOOTH_SIMPLE: */
336 default:
337 smooth_iter__simple(csmd, mesh, vertexCos, smooth_weights, iterations);
338 break;
339 }
340}
341
343 Mesh *mesh,
344 const MDeformVert *dvert,
345 const int defgrp_index,
347{
348 float *smooth_weights = nullptr;
349
350 if (dvert || (csmd->flag & MOD_CORRECTIVESMOOTH_PIN_BOUNDARY)) {
351
352 smooth_weights = MEM_malloc_arrayN<float>(size_t(vertexCos.size()), __func__);
353
354 if (dvert) {
355 mesh_get_weights(dvert,
356 defgrp_index,
357 uint(vertexCos.size()),
359 smooth_weights);
360 }
361 else {
362 copy_vn_fl(smooth_weights, int(vertexCos.size()), 1.0f);
363 }
364
366 mesh_get_boundaries(mesh, smooth_weights);
367 }
368 }
369
370 smooth_iter(csmd, mesh, vertexCos, smooth_weights, uint(csmd->repeat));
371
372 if (smooth_weights) {
373 MEM_freeN(smooth_weights);
374 }
375}
376
381static bool calc_tangent_loop(const float v_dir_prev[3],
382 const float v_dir_next[3],
383 float r_tspace[3][3])
384{
385 if (UNLIKELY(compare_v3v3(v_dir_prev, v_dir_next, FLT_EPSILON * 10.0f))) {
386 /* As there are no weights, the value doesn't matter just initialize it. */
387 unit_m3(r_tspace);
388 return false;
389 }
390
391 copy_v3_v3(r_tspace[0], v_dir_prev);
392 copy_v3_v3(r_tspace[1], v_dir_next);
393
394 cross_v3_v3v3(r_tspace[2], v_dir_prev, v_dir_next);
395 normalize_v3(r_tspace[2]);
396
397 /* Make orthogonal using `r_tspace[2]` as a basis.
398 *
399 * NOTE: while it seems more logical to use `v_dir_prev` & `v_dir_next` as separate X/Y axis
400 * (instead of combining them as is done here). It's not necessary as the directions of the
401 * axis aren't important as long as the difference between tangent matrices is equivalent.
402 * Some computations can be skipped by combining the two directions,
403 * using the cross product for the 3rd axes. */
404 add_v3_v3(r_tspace[0], r_tspace[1]);
405 normalize_v3(r_tspace[0]);
406 cross_v3_v3v3(r_tspace[1], r_tspace[2], r_tspace[0]);
407
408 return true;
409}
410
417static void calc_tangent_spaces(const Mesh *mesh,
419 float (*r_tangent_spaces)[3][3],
420 float *r_tangent_weights,
421 float *r_tangent_weights_per_vertex)
422{
423 const uint mvert_num = uint(mesh->verts_num);
424 const blender::OffsetIndices faces = mesh->faces();
425 blender::Span<int> corner_verts = mesh->corner_verts();
426
427 if (r_tangent_weights_per_vertex != nullptr) {
428 copy_vn_fl(r_tangent_weights_per_vertex, int(mvert_num), 0.0f);
429 }
430
431 for (const int64_t i : faces.index_range()) {
432 const blender::IndexRange face = faces[i];
433 int next_corner = int(face.start());
434 int term_corner = next_corner + int(face.size());
435 int prev_corner = term_corner - 2;
436 int curr_corner = term_corner - 1;
437
438 /* loop directions */
439 float v_dir_prev[3], v_dir_next[3];
440
441 /* needed entering the loop */
443 v_dir_prev, vertexCos[corner_verts[prev_corner]], vertexCos[corner_verts[curr_corner]]);
444 normalize_v3(v_dir_prev);
445
446 for (; next_corner != term_corner;
447 prev_corner = curr_corner, curr_corner = next_corner, next_corner++)
448 {
449 float(*ts)[3] = r_tangent_spaces[curr_corner];
450
451 /* re-use the previous value */
452#if 0
454 v_dir_prev, vertexCos[corner_verts[prev_corner]], vertexCos[corner_verts[curr_corner]]);
455 normalize_v3(v_dir_prev);
456#endif
458 v_dir_next, vertexCos[corner_verts[curr_corner]], vertexCos[corner_verts[next_corner]]);
459 normalize_v3(v_dir_next);
460
461 if (calc_tangent_loop(v_dir_prev, v_dir_next, ts)) {
462 if (r_tangent_weights != nullptr) {
463 const float weight = fabsf(
464 blender::math::safe_acos_approx(dot_v3v3(v_dir_next, v_dir_prev)));
465 r_tangent_weights[curr_corner] = weight;
466 r_tangent_weights_per_vertex[corner_verts[curr_corner]] += weight;
467 }
468 }
469 else {
470 if (r_tangent_weights != nullptr) {
471 r_tangent_weights[curr_corner] = 0;
472 }
473 }
474
475 copy_v3_v3(v_dir_prev, v_dir_next);
476 }
477 }
478}
479
481{
482 csmd->delta_cache.lambda = csmd->lambda;
483 csmd->delta_cache.repeat = csmd->repeat;
484 csmd->delta_cache.flag = csmd->flag;
485 csmd->delta_cache.smooth_type = csmd->smooth_type;
486 csmd->delta_cache.rest_source = csmd->rest_source;
487}
488
490{
491 return (csmd->delta_cache.lambda == csmd->lambda && csmd->delta_cache.repeat == csmd->repeat &&
492 csmd->delta_cache.flag == csmd->flag &&
493 csmd->delta_cache.smooth_type == csmd->smooth_type &&
494 csmd->delta_cache.rest_source == csmd->rest_source);
495}
496
502 Mesh *mesh,
503 const MDeformVert *dvert,
504 const int defgrp_index,
505 const blender::Span<blender::float3> rest_coords)
506{
507 const blender::Span<int> corner_verts = mesh->corner_verts();
508
509 blender::Array<blender::float3> smooth_vertex_coords(rest_coords);
510
511 uint l_index;
512
513 float(*tangent_spaces)[3][3] = MEM_malloc_arrayN<float[3][3]>(size_t(corner_verts.size()),
514 __func__);
515
516 if (csmd->delta_cache.deltas_num != uint(corner_verts.size())) {
518 }
519
520 /* allocate deltas if they have not yet been allocated, otherwise we will just write over them */
521 if (!csmd->delta_cache.deltas) {
522 csmd->delta_cache.deltas_num = uint(corner_verts.size());
523 csmd->delta_cache.deltas = MEM_malloc_arrayN<float[3]>(size_t(corner_verts.size()), __func__);
524 }
525
526 smooth_verts(csmd, mesh, dvert, defgrp_index, smooth_vertex_coords);
527
528 calc_tangent_spaces(mesh, smooth_vertex_coords, tangent_spaces, nullptr, nullptr);
529
530 copy_vn_fl(&csmd->delta_cache.deltas[0][0], int(corner_verts.size()) * 3, 0.0f);
531
532 for (l_index = 0; l_index < corner_verts.size(); l_index++) {
533 const int v_index = corner_verts[l_index];
534 float delta[3];
535 sub_v3_v3v3(delta, rest_coords[v_index], smooth_vertex_coords[v_index]);
536
537 float imat[3][3];
538 if (UNLIKELY(!invert_m3_m3(imat, tangent_spaces[l_index]))) {
539 transpose_m3_m3(imat, tangent_spaces[l_index]);
540 }
541 mul_v3_m3v3(csmd->delta_cache.deltas[l_index], imat, delta);
542 }
543
544 MEM_SAFE_FREE(tangent_spaces);
545}
546
548 Depsgraph *depsgraph,
549 Object *ob,
550 Mesh *mesh,
552 BMEditMesh *em)
553{
555
556 const bool force_delta_cache_update =
557 /* XXX, take care! if mesh data itself changes we need to forcefully recalculate deltas */
558 !cache_settings_equal(csmd) ||
560 (((ID *)ob->data)->recalc & ID_RECALC_ALL));
561
562 blender::Span<int> corner_verts = mesh->corner_verts();
563
564 bool use_only_smooth = (csmd->flag & MOD_CORRECTIVESMOOTH_ONLY_SMOOTH) != 0;
565 const MDeformVert *dvert = nullptr;
566 int defgrp_index;
567
568 MOD_get_vgroup(ob, mesh, csmd->defgrp_name, &dvert, &defgrp_index);
569
570 /* if rest bind_coords not are defined, set them (only run during bind) */
572 /* signal to recalculate, whoever sets MUST also free bind coords */
573 (csmd->bind_coords_num == uint(-1)))
574 {
576 BLI_assert(csmd->bind_coords == nullptr);
577 csmd->bind_coords = MEM_malloc_arrayN<float[3]>(size_t(vertexCos.size()), __func__);
578 memcpy(csmd->bind_coords, vertexCos.data(), size_t(vertexCos.size_in_bytes()));
579 csmd->bind_coords_num = uint(vertexCos.size());
580 BLI_assert(csmd->bind_coords != nullptr);
581 /* Copy bound data to the original modifier. */
584 csmd_orig->bind_coords = static_cast<float(*)[3]>(MEM_dupallocN(csmd->bind_coords));
585 csmd_orig->bind_coords_num = csmd->bind_coords_num;
586 }
587 else {
588 BKE_modifier_set_error(ob, md, "Attempt to bind from inactive dependency graph");
589 }
590 }
591
592 if (UNLIKELY(use_only_smooth)) {
593 smooth_verts(csmd, mesh, dvert, defgrp_index, vertexCos);
594 return;
595 }
596
598 (csmd->bind_coords == nullptr))
599 {
600 BKE_modifier_set_error(ob, md, "Bind data required");
601 goto error;
602 }
603
604 /* If the number of verts has changed, the bind is invalid, so we do nothing */
606 if (csmd->bind_coords_num != vertexCos.size()) {
608 md,
609 "Bind vertex count mismatch: %u to %u",
610 csmd->bind_coords_num,
611 uint(vertexCos.size()));
612 goto error;
613 }
614 }
615 else {
616 /* MOD_CORRECTIVESMOOTH_RESTSOURCE_ORCO */
617 if (ob->type != OB_MESH) {
618 BKE_modifier_set_error(ob, md, "Object is not a mesh");
619 goto error;
620 }
621 else {
622 const int me_numVerts = (em) ? em->bm->totvert : ((Mesh *)ob->data)->verts_num;
623
624 if (me_numVerts != vertexCos.size()) {
626 md,
627 "Original vertex count mismatch: %u to %u",
628 uint(me_numVerts),
629 uint(vertexCos.size()));
630 goto error;
631 }
632 }
633 }
634
635 /* check to see if our deltas are still valid */
636 if (!csmd->delta_cache.deltas || (csmd->delta_cache.deltas_num != corner_verts.size()) ||
637 force_delta_cache_update)
638 {
639 blender::Array<blender::float3> rest_coords_alloc;
641
643
645 /* caller needs to do sanity check here */
646 csmd->bind_coords_num = uint(vertexCos.size());
647 rest_coords = {reinterpret_cast<const blender::float3 *>(csmd->bind_coords),
648 csmd->bind_coords_num};
649 }
650 else {
651 if (em) {
652 rest_coords_alloc = BKE_editmesh_vert_coords_alloc_orco(em);
653 rest_coords = rest_coords_alloc;
654 }
655 else {
656 const Mesh *object_mesh = static_cast<const Mesh *>(ob->data);
657 rest_coords = object_mesh->vert_positions();
658 }
659 }
660
661#ifdef DEBUG_TIME
662 TIMEIT_START(corrective_smooth_deltas);
663#endif
664
665 calc_deltas(csmd, mesh, dvert, defgrp_index, rest_coords);
666
667#ifdef DEBUG_TIME
668 TIMEIT_END(corrective_smooth_deltas);
669#endif
670 }
671
673 /* this could be a check, but at this point it _must_ be valid */
674 BLI_assert(csmd->bind_coords_num == vertexCos.size() && csmd->delta_cache.deltas);
675 }
676
677#ifdef DEBUG_TIME
678 TIMEIT_START(corrective_smooth);
679#endif
680
681 /* do the actual delta mush */
682 smooth_verts(csmd, mesh, dvert, defgrp_index, vertexCos);
683
684 {
685
686 const float scale = csmd->scale;
687
688 float(*tangent_spaces)[3][3] = MEM_malloc_arrayN<float[3][3]>(size_t(corner_verts.size()),
689 __func__);
690 float *tangent_weights = MEM_malloc_arrayN<float>(size_t(corner_verts.size()), __func__);
691 float *tangent_weights_per_vertex = MEM_malloc_arrayN<float>(size_t(vertexCos.size()),
692 __func__);
693
695 mesh, vertexCos, tangent_spaces, tangent_weights, tangent_weights_per_vertex);
696
697 for (const int64_t l_index : corner_verts.index_range()) {
698 const int v_index = corner_verts[l_index];
699 const float weight = tangent_weights[l_index] / tangent_weights_per_vertex[v_index];
700 if (UNLIKELY(!(weight > 0.0f))) {
701 /* Catches zero & divide by zero. */
702 continue;
703 }
704
705 float delta[3];
706 mul_v3_m3v3(delta, tangent_spaces[l_index], csmd->delta_cache.deltas[l_index]);
707 mul_v3_fl(delta, weight);
708 madd_v3_v3fl(vertexCos[v_index], delta, scale);
709 }
710
711 MEM_freeN(tangent_spaces);
712 MEM_freeN(tangent_weights);
713 MEM_freeN(tangent_weights_per_vertex);
714 }
715
716#ifdef DEBUG_TIME
717 TIMEIT_END(corrective_smooth);
718#endif
719
720 return;
721
722 /* when the modifier fails to execute */
723error:
725 csmd->delta_cache.deltas_num = 0;
726}
727
729 const ModifierEvalContext *ctx,
730 Mesh *mesh,
732{
733 correctivesmooth_modifier_do(md, ctx->depsgraph, ctx->object, mesh, positions, nullptr);
734}
735
736static void panel_draw(const bContext * /*C*/, Panel *panel)
737{
738 uiLayout *layout = panel->layout;
739
740 PointerRNA ob_ptr;
742
743 uiLayoutSetPropSep(layout, true);
744
745 layout->prop(ptr, "factor", UI_ITEM_NONE, IFACE_("Factor"), ICON_NONE);
746 layout->prop(ptr, "iterations", UI_ITEM_NONE, std::nullopt, ICON_NONE);
747 layout->prop(ptr, "scale", UI_ITEM_NONE, std::nullopt, ICON_NONE);
748 layout->prop(ptr, "smooth_type", UI_ITEM_NONE, std::nullopt, ICON_NONE);
749
750 modifier_vgroup_ui(layout, ptr, &ob_ptr, "vertex_group", "invert_vertex_group", std::nullopt);
751
752 layout->prop(ptr, "use_only_smooth", UI_ITEM_NONE, std::nullopt, ICON_NONE);
753 layout->prop(ptr, "use_pin_boundary", UI_ITEM_NONE, std::nullopt, ICON_NONE);
754
755 layout->prop(ptr, "rest_source", UI_ITEM_NONE, std::nullopt, ICON_NONE);
757 layout->op("OBJECT_OT_correctivesmooth_bind",
758 (RNA_boolean_get(ptr, "is_bind") ? IFACE_("Unbind") : IFACE_("Bind")),
759 ICON_NONE);
760 }
761
763}
764
769
770static void blend_write(BlendWriter *writer, const ID *id_owner, const ModifierData *md)
771{
773 const bool is_undo = BLO_write_is_undo(writer);
774
775 if (ID_IS_OVERRIDE_LIBRARY(id_owner) && !is_undo) {
776 BLI_assert(!ID_IS_LINKED(id_owner));
777 const bool is_local = (md->flag & eModifierFlag_OverrideLibrary_Local) != 0;
778 if (!is_local) {
779 /* Modifier coming from linked data cannot be bound from an override, so we can remove all
780 * binding data, can save a significant amount of memory. */
781 csmd.bind_coords_num = 0;
782 csmd.bind_coords = nullptr;
783 }
784 }
785
787
788 if (csmd.bind_coords != nullptr) {
789 BLO_write_float3_array(writer, csmd.bind_coords_num, (float *)csmd.bind_coords);
790 }
791}
792
793static void blend_read(BlendDataReader *reader, ModifierData *md)
794{
796
797 if (csmd->bind_coords) {
798 BLO_read_float3_array(reader, int(csmd->bind_coords_num), (float **)&csmd->bind_coords);
799 }
800
801 /* runtime only */
802 csmd->delta_cache.deltas = nullptr;
803 csmd->delta_cache.deltas_num = 0;
804}
805
807 /*idname*/ "CorrectiveSmooth",
808 /*name*/ N_("CorrectiveSmooth"),
809 /*struct_name*/ "CorrectiveSmoothModifierData",
810 /*struct_size*/ sizeof(CorrectiveSmoothModifierData),
811 /*srna*/ &RNA_CorrectiveSmoothModifier,
814 /*icon*/ ICON_MOD_SMOOTH,
815
816 /*copy_data*/ copy_data,
817
818 /*deform_verts*/ deform_verts,
819 /*deform_matrices*/ nullptr,
820 /*deform_verts_EM*/ nullptr,
821 /*deform_matrices_EM*/ nullptr,
822 /*modify_mesh*/ nullptr,
823 /*modify_geometry_set*/ nullptr,
824
825 /*init_data*/ init_data,
826 /*required_data_mask*/ required_data_mask,
827 /*free_data*/ free_data,
828 /*is_disabled*/ nullptr,
829 /*update_depsgraph*/ nullptr,
830 /*depends_on_time*/ nullptr,
831 /*depends_on_normals*/ nullptr,
832 /*foreach_ID_link*/ nullptr,
833 /*foreach_tex_link*/ nullptr,
834 /*free_runtime_data*/ nullptr,
835 /*panel_register*/ panel_register,
836 /*blend_write*/ blend_write,
837 /*blend_read*/ blend_read,
838 /*foreach_cache*/ nullptr,
839};
support for deformation groups and hooks.
float BKE_defvert_find_weight(const MDeformVert *dvert, int defgroup)
Definition deform.cc:763
blender::Array< blender::float3 > BKE_editmesh_vert_coords_alloc_orco(BMEditMesh *em)
Definition editmesh.cc:215
void BKE_modifier_copydata_generic(const ModifierData *md, ModifierData *md_dst, int flag)
@ eModifierTypeFlag_SupportsEditmode
@ eModifierTypeFlag_AcceptsMesh
ModifierData * BKE_modifier_get_original(const Object *object, ModifierData *md)
void BKE_modifier_set_error(const Object *ob, ModifierData *md, const char *format,...) ATTR_PRINTF_FORMAT(3
#define BLI_assert(a)
Definition BLI_assert.h:46
void unit_m3(float m[3][3])
bool invert_m3_m3(float inverse[3][3], const float mat[3][3])
void mul_v3_m3v3(float r[3], const float M[3][3], const float a[3])
void transpose_m3_m3(float R[3][3], const float M[3][3])
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
MINLINE void sub_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])
MINLINE void mul_v3_fl(float r[3], float f)
MINLINE void copy_v3_v3(float r[3], const float a[3])
void copy_vn_fl(float *array_tar, int size, float val)
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void cross_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE bool compare_v3v3(const float v1[3], const float v2[3], float limit) ATTR_WARN_UNUSED_RESULT
MINLINE void add_v3_v3(float r[3], const float a[3])
MINLINE float normalize_v3(float n[3])
MINLINE float len_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT
unsigned int uint
Platform independent time functions.
Utility defines for timing/benchmarks.
#define TIMEIT_START(var)
#define TIMEIT_END(var)
#define UNLIKELY(x)
#define MEMCMP_STRUCT_AFTER_IS_ZERO(struct_var, member)
#define MEMCPY_STRUCT_AFTER(struct_dst, struct_src, member)
void BLO_read_float3_array(BlendDataReader *reader, int64_t array_size, float **ptr_p)
Definition readfile.cc:5336
void BLO_write_float3_array(BlendWriter *writer, int64_t num, const float *data_ptr)
bool BLO_write_is_undo(BlendWriter *writer)
#define BLO_write_struct_at_address(writer, struct_name, address, data_ptr)
#define IFACE_(msgid)
bool DEG_is_active(const Depsgraph *depsgraph)
Definition depsgraph.cc:323
@ ID_RECALC_ALL
Definition DNA_ID.h:1096
#define DNA_struct_default_get(struct_name)
@ eModifierFlag_OverrideLibrary_Local
@ MOD_CORRECTIVESMOOTH_RESTSOURCE_ORCO
@ MOD_CORRECTIVESMOOTH_RESTSOURCE_BIND
@ eModifierType_CorrectiveSmooth
@ MOD_CORRECTIVESMOOTH_SMOOTH_LENGTH_WEIGHT
@ MOD_CORRECTIVESMOOTH_ONLY_SMOOTH
@ MOD_CORRECTIVESMOOTH_PIN_BOUNDARY
@ MOD_CORRECTIVESMOOTH_INVERT_VGROUP
Object is a sort of wrapper for general info.
@ OB_MESH
Read Guarded memory(de)allocation.
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 blend_read(BlendDataReader *, ModifierData *md)
static void panel_draw(const bContext *, Panel *panel)
static void copy_data(const ModifierData *md, ModifierData *target, const int flag)
static void free_data(ModifierData *md)
Definition MOD_bevel.cc:271
static void blend_write(BlendWriter *writer, const ID *, const ModifierData *md)
Definition MOD_bevel.cc:433
static void init_data(ModifierData *md)
static void freeBind(CorrectiveSmoothModifierData *csmd)
static void deform_verts(ModifierData *md, const ModifierEvalContext *ctx, Mesh *mesh, blender::MutableSpan< blender::float3 > positions)
static void store_cache_settings(CorrectiveSmoothModifierData *csmd)
static void panel_register(ARegionType *region_type)
static void smooth_iter__length_weight(CorrectiveSmoothModifierData *csmd, Mesh *mesh, blender::MutableSpan< blender::float3 > vertexCos, const float *smooth_weights, uint iterations)
ModifierTypeInfo modifierType_CorrectiveSmooth
static void smooth_iter__simple(CorrectiveSmoothModifierData *csmd, Mesh *mesh, blender::MutableSpan< blender::float3 > vertexCos, const float *smooth_weights, uint iterations)
static void calc_deltas(CorrectiveSmoothModifierData *csmd, Mesh *mesh, const MDeformVert *dvert, const int defgrp_index, const blender::Span< blender::float3 > rest_coords)
static void free_data(ModifierData *md)
static void blend_read(BlendDataReader *reader, ModifierData *md)
static void calc_tangent_spaces(const Mesh *mesh, blender::Span< blender::float3 > vertexCos, float(*r_tangent_spaces)[3][3], float *r_tangent_weights, float *r_tangent_weights_per_vertex)
static void correctivesmooth_modifier_do(ModifierData *md, Depsgraph *depsgraph, Object *ob, Mesh *mesh, blender::MutableSpan< blender::float3 > vertexCos, BMEditMesh *em)
static void panel_draw(const bContext *, Panel *panel)
static bool calc_tangent_loop(const float v_dir_prev[3], const float v_dir_next[3], float r_tspace[3][3])
static void required_data_mask(ModifierData *md, CustomData_MeshMasks *r_cddata_masks)
static void blend_write(BlendWriter *writer, const ID *id_owner, const ModifierData *md)
static void smooth_verts(CorrectiveSmoothModifierData *csmd, Mesh *mesh, const MDeformVert *dvert, const int defgrp_index, blender::MutableSpan< blender::float3 > vertexCos)
static void mesh_get_weights(const MDeformVert *dvert, const int defgrp_index, const uint verts_num, const bool use_invert_vgroup, float *smooth_weights)
static void smooth_iter(CorrectiveSmoothModifierData *csmd, Mesh *mesh, blender::MutableSpan< blender::float3 > vertexCos, const float *smooth_weights, uint iterations)
static bool cache_settings_equal(CorrectiveSmoothModifierData *csmd)
static void copy_data(const ModifierData *md, ModifierData *target, const int flag)
static void mesh_get_boundaries(Mesh *mesh, float *smooth_weights)
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_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
void uiLayoutSetPropSep(uiLayout *layout, bool is_sep)
#define UI_ITEM_NONE
BPy_StructRNA * depsgraph
long long int int64_t
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
constexpr int64_t size() const
constexpr int64_t start() const
constexpr int64_t size() const
Definition BLI_span.hh:493
constexpr T * data() const
Definition BLI_span.hh:539
constexpr int64_t size_in_bytes() const
Definition BLI_span.hh:501
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:137
constexpr int64_t size() const
Definition BLI_span.hh:252
constexpr IndexRange index_range() const
Definition BLI_span.hh:401
#define fabsf(x)
#define CD_MASK_MDEFORMVERT
#define MEM_SAFE_FREE(v)
#define ID_IS_LINKED(_id)
#define ID_IS_OVERRIDE_LIBRARY(_id)
void * MEM_calloc_arrayN(size_t len, size_t size, const char *str)
Definition mallocn.cc:123
void * MEM_malloc_arrayN(size_t len, size_t size, const char *str)
Definition mallocn.cc:133
void * MEM_dupallocN(const void *vmemh)
Definition mallocn.cc:143
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
static char faces[256]
static void error(const char *str)
float safe_acos_approx(float x)
VecBase< float, 3 > float3
const btScalar eps
Definition poly34.cpp:11
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
int RNA_enum_get(PointerRNA *ptr, const char *name)
int totvert
CorrectiveSmoothDeltaCache delta_cache
Definition DNA_ID.h:404
int edges_num
int verts_num
struct uiLayout * layout
PointerRNA op(wmOperatorType *ot, std::optional< blender::StringRef > name, int icon, wmOperatorCallContext context, eUI_Item_Flag flag)
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:4226
uint8_t flag
Definition wm_window.cc:139