Blender V4.3
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
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#include "BLI_time.h"
46#ifdef DEBUG_TIME
47# include "BLI_time_utildefines.h"
48#endif
49
50#include "BLI_strict_flags.h" /* 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 = static_cast<uint8_t *>(
131 MEM_calloc_arrayN(size_t(edges.size()), sizeof(*boundaries), __func__));
132
133 for (const int64_t i : faces.index_range()) {
134 for (const int edge : corner_edges.slice(faces[i])) {
135 uint8_t *e_value = &boundaries[edge];
136 *e_value |= uint8_t((*e_value) + 1);
137 }
138 }
139
140 for (const int64_t i : edges.index_range()) {
141 if (boundaries[i] == 1) {
142 smooth_weights[edges[i][0]] = 0.0f;
143 smooth_weights[edges[i][1]] = 0.0f;
144 }
145 }
146
147 MEM_freeN(boundaries);
148}
149
150/* -------------------------------------------------------------------- */
151/* Simple Weighted Smoothing
152 *
153 * (average of surrounding verts)
154 */
156 Mesh *mesh,
158 const float *smooth_weights,
159 uint iterations)
160{
161 const float lambda = csmd->lambda;
162 int i;
163
164 const int edges_num = mesh->edges_num;
165 const blender::Span<blender::int2> edges = mesh->edges();
166
167 struct SmoothingData_Simple {
168 float delta[3];
169 };
170 SmoothingData_Simple *smooth_data = MEM_cnew_array<SmoothingData_Simple>(
171 size_t(vertexCos.size()), __func__);
172
173 float *vertex_edge_count_div = static_cast<float *>(
174 MEM_calloc_arrayN(size_t(vertexCos.size()), sizeof(float), __func__));
175
176 /* calculate as floats to avoid int->float conversion in #smooth_iter */
177 for (i = 0; i < edges_num; i++) {
178 vertex_edge_count_div[edges[i][0]] += 1.0f;
179 vertex_edge_count_div[edges[i][1]] += 1.0f;
180 }
181
182 /* a little confusing, but we can include 'lambda' and smoothing weight
183 * here to avoid multiplying for every iteration */
184 if (smooth_weights == nullptr) {
185 for (i = 0; i < vertexCos.size(); i++) {
186 vertex_edge_count_div[i] = lambda * (vertex_edge_count_div[i] ?
187 (1.0f / vertex_edge_count_div[i]) :
188 1.0f);
189 }
190 }
191 else {
192 for (i = 0; i < vertexCos.size(); i++) {
193 vertex_edge_count_div[i] = smooth_weights[i] * lambda *
194 (vertex_edge_count_div[i] ? (1.0f / vertex_edge_count_div[i]) :
195 1.0f);
196 }
197 }
198
199 /* -------------------------------------------------------------------- */
200 /* Main Smoothing Loop */
201
202 while (iterations--) {
203 for (i = 0; i < edges_num; i++) {
204 SmoothingData_Simple *sd_v1;
205 SmoothingData_Simple *sd_v2;
206 float edge_dir[3];
207
208 sub_v3_v3v3(edge_dir, vertexCos[edges[i][1]], vertexCos[edges[i][0]]);
209
210 sd_v1 = &smooth_data[edges[i][0]];
211 sd_v2 = &smooth_data[edges[i][1]];
212
213 add_v3_v3(sd_v1->delta, edge_dir);
214 sub_v3_v3(sd_v2->delta, edge_dir);
215 }
216
217 for (i = 0; i < vertexCos.size(); i++) {
218 SmoothingData_Simple *sd = &smooth_data[i];
219 madd_v3_v3fl(vertexCos[i], sd->delta, vertex_edge_count_div[i]);
220 /* zero for the next iteration (saves memset on entire array) */
221 memset(sd, 0, sizeof(*sd));
222 }
223 }
224
225 MEM_freeN(vertex_edge_count_div);
226 MEM_freeN(smooth_data);
227}
228
229/* -------------------------------------------------------------------- */
230/* Edge-Length Weighted Smoothing
231 */
233 Mesh *mesh,
235 const float *smooth_weights,
236 uint iterations)
237{
238 const float eps = FLT_EPSILON * 10.0f;
239 const uint edges_num = uint(mesh->edges_num);
240 /* NOTE: the way this smoothing method works, its approx half as strong as the simple-smooth,
241 * and 2.0 rarely spikes, double the value for consistent behavior. */
242 const float lambda = csmd->lambda * 2.0f;
243 const blender::Span<blender::int2> edges = mesh->edges();
244 uint i;
245
246 struct SmoothingData_Weighted {
247 float delta[3];
248 float edge_length_sum;
249 };
250 SmoothingData_Weighted *smooth_data = MEM_cnew_array<SmoothingData_Weighted>(
251 size_t(vertexCos.size()), __func__);
252
253 /* calculate as floats to avoid int->float conversion in #smooth_iter */
254 float *vertex_edge_count = static_cast<float *>(
255 MEM_calloc_arrayN(size_t(vertexCos.size()), sizeof(float), __func__));
256 for (i = 0; i < edges_num; i++) {
257 vertex_edge_count[edges[i][0]] += 1.0f;
258 vertex_edge_count[edges[i][1]] += 1.0f;
259 }
260
261 /* -------------------------------------------------------------------- */
262 /* Main Smoothing Loop */
263
264 while (iterations--) {
265 for (i = 0; i < edges_num; i++) {
266 SmoothingData_Weighted *sd_v1;
267 SmoothingData_Weighted *sd_v2;
268 float edge_dir[3];
269 float edge_dist;
270
271 sub_v3_v3v3(edge_dir, vertexCos[edges[i][1]], vertexCos[edges[i][0]]);
272 edge_dist = len_v3(edge_dir);
273
274 /* weight by distance */
275 mul_v3_fl(edge_dir, edge_dist);
276
277 sd_v1 = &smooth_data[edges[i][0]];
278 sd_v2 = &smooth_data[edges[i][1]];
279
280 add_v3_v3(sd_v1->delta, edge_dir);
281 sub_v3_v3(sd_v2->delta, edge_dir);
282
283 sd_v1->edge_length_sum += edge_dist;
284 sd_v2->edge_length_sum += edge_dist;
285 }
286
287 if (smooth_weights == nullptr) {
288 /* fast-path */
289 for (i = 0; i < vertexCos.size(); i++) {
290 SmoothingData_Weighted *sd = &smooth_data[i];
291 /* Divide by sum of all neighbor distances (weighted) and amount of neighbors,
292 * (mean average). */
293 const float div = sd->edge_length_sum * vertex_edge_count[i];
294 if (div > eps) {
295#if 0
296 /* first calculate the new location */
297 mul_v3_fl(sd->delta, 1.0f / div);
298 /* then interpolate */
299 madd_v3_v3fl(vertexCos[i], sd->delta, lambda);
300#else
301 /* do this in one step */
302 madd_v3_v3fl(vertexCos[i], sd->delta, lambda / div);
303#endif
304 }
305 /* zero for the next iteration (saves memset on entire array) */
306 memset(sd, 0, sizeof(*sd));
307 }
308 }
309 else {
310 for (i = 0; i < vertexCos.size(); i++) {
311 SmoothingData_Weighted *sd = &smooth_data[i];
312 const float div = sd->edge_length_sum * vertex_edge_count[i];
313 if (div > eps) {
314 const float lambda_w = lambda * smooth_weights[i];
315 madd_v3_v3fl(vertexCos[i], sd->delta, lambda_w / div);
316 }
317
318 memset(sd, 0, sizeof(*sd));
319 }
320 }
321 }
322
323 MEM_freeN(vertex_edge_count);
324 MEM_freeN(smooth_data);
325}
326
328 Mesh *mesh,
330 const float *smooth_weights,
331 uint iterations)
332{
333 switch (csmd->smooth_type) {
335 smooth_iter__length_weight(csmd, mesh, vertexCos, smooth_weights, iterations);
336 break;
337
338 /* case MOD_CORRECTIVESMOOTH_SMOOTH_SIMPLE: */
339 default:
340 smooth_iter__simple(csmd, mesh, vertexCos, smooth_weights, iterations);
341 break;
342 }
343}
344
346 Mesh *mesh,
347 const MDeformVert *dvert,
348 const int defgrp_index,
350{
351 float *smooth_weights = nullptr;
352
353 if (dvert || (csmd->flag & MOD_CORRECTIVESMOOTH_PIN_BOUNDARY)) {
354
355 smooth_weights = static_cast<float *>(
356 MEM_malloc_arrayN(size_t(vertexCos.size()), sizeof(float), __func__));
357
358 if (dvert) {
359 mesh_get_weights(dvert,
360 defgrp_index,
361 uint(vertexCos.size()),
363 smooth_weights);
364 }
365 else {
366 copy_vn_fl(smooth_weights, int(vertexCos.size()), 1.0f);
367 }
368
370 mesh_get_boundaries(mesh, smooth_weights);
371 }
372 }
373
374 smooth_iter(csmd, mesh, vertexCos, smooth_weights, uint(csmd->repeat));
375
376 if (smooth_weights) {
377 MEM_freeN(smooth_weights);
378 }
379}
380
385static bool calc_tangent_loop(const float v_dir_prev[3],
386 const float v_dir_next[3],
387 float r_tspace[3][3])
388{
389 if (UNLIKELY(compare_v3v3(v_dir_prev, v_dir_next, FLT_EPSILON * 10.0f))) {
390 /* As there are no weights, the value doesn't matter just initialize it. */
391 unit_m3(r_tspace);
392 return false;
393 }
394
395 copy_v3_v3(r_tspace[0], v_dir_prev);
396 copy_v3_v3(r_tspace[1], v_dir_next);
397
398 cross_v3_v3v3(r_tspace[2], v_dir_prev, v_dir_next);
399 normalize_v3(r_tspace[2]);
400
401 /* Make orthogonal using `r_tspace[2]` as a basis.
402 *
403 * NOTE: while it seems more logical to use `v_dir_prev` & `v_dir_next` as separate X/Y axis
404 * (instead of combining them as is done here). It's not necessary as the directions of the
405 * axis aren't important as long as the difference between tangent matrices is equivalent.
406 * Some computations can be skipped by combining the two directions,
407 * using the cross product for the 3rd axes. */
408 add_v3_v3(r_tspace[0], r_tspace[1]);
409 normalize_v3(r_tspace[0]);
410 cross_v3_v3v3(r_tspace[1], r_tspace[2], r_tspace[0]);
411
412 return true;
413}
414
421static void calc_tangent_spaces(const Mesh *mesh,
423 float (*r_tangent_spaces)[3][3],
424 float *r_tangent_weights,
425 float *r_tangent_weights_per_vertex)
426{
427 const uint mvert_num = uint(mesh->verts_num);
428 const blender::OffsetIndices faces = mesh->faces();
429 blender::Span<int> corner_verts = mesh->corner_verts();
430
431 if (r_tangent_weights_per_vertex != nullptr) {
432 copy_vn_fl(r_tangent_weights_per_vertex, int(mvert_num), 0.0f);
433 }
434
435 for (const int64_t i : faces.index_range()) {
436 const blender::IndexRange face = faces[i];
437 int next_corner = int(face.start());
438 int term_corner = next_corner + int(face.size());
439 int prev_corner = term_corner - 2;
440 int curr_corner = term_corner - 1;
441
442 /* loop directions */
443 float v_dir_prev[3], v_dir_next[3];
444
445 /* needed entering the loop */
447 v_dir_prev, vertexCos[corner_verts[prev_corner]], vertexCos[corner_verts[curr_corner]]);
448 normalize_v3(v_dir_prev);
449
450 for (; next_corner != term_corner;
451 prev_corner = curr_corner, curr_corner = next_corner, next_corner++)
452 {
453 float(*ts)[3] = r_tangent_spaces[curr_corner];
454
455 /* re-use the previous value */
456#if 0
458 v_dir_prev, vertexCos[corner_verts[prev_corner]], vertexCos[corner_verts[curr_corner]]);
459 normalize_v3(v_dir_prev);
460#endif
462 v_dir_next, vertexCos[corner_verts[curr_corner]], vertexCos[corner_verts[next_corner]]);
463 normalize_v3(v_dir_next);
464
465 if (calc_tangent_loop(v_dir_prev, v_dir_next, ts)) {
466 if (r_tangent_weights != nullptr) {
467 const float weight = fabsf(
468 blender::math::safe_acos_approx(dot_v3v3(v_dir_next, v_dir_prev)));
469 r_tangent_weights[curr_corner] = weight;
470 r_tangent_weights_per_vertex[corner_verts[curr_corner]] += weight;
471 }
472 }
473 else {
474 if (r_tangent_weights != nullptr) {
475 r_tangent_weights[curr_corner] = 0;
476 }
477 }
478
479 copy_v3_v3(v_dir_prev, v_dir_next);
480 }
481 }
482}
483
485{
486 csmd->delta_cache.lambda = csmd->lambda;
487 csmd->delta_cache.repeat = csmd->repeat;
488 csmd->delta_cache.flag = csmd->flag;
489 csmd->delta_cache.smooth_type = csmd->smooth_type;
490 csmd->delta_cache.rest_source = csmd->rest_source;
491}
492
494{
495 return (csmd->delta_cache.lambda == csmd->lambda && csmd->delta_cache.repeat == csmd->repeat &&
496 csmd->delta_cache.flag == csmd->flag &&
497 csmd->delta_cache.smooth_type == csmd->smooth_type &&
498 csmd->delta_cache.rest_source == csmd->rest_source);
499}
500
506 Mesh *mesh,
507 const MDeformVert *dvert,
508 const int defgrp_index,
509 const blender::Span<blender::float3> rest_coords)
510{
511 const blender::Span<int> corner_verts = mesh->corner_verts();
512
513 blender::Array<blender::float3> smooth_vertex_coords(rest_coords);
514
515 uint l_index;
516
517 float(*tangent_spaces)[3][3] = static_cast<float(*)[3][3]>(
518 MEM_malloc_arrayN(size_t(corner_verts.size()), sizeof(float[3][3]), __func__));
519
520 if (csmd->delta_cache.deltas_num != uint(corner_verts.size())) {
522 }
523
524 /* allocate deltas if they have not yet been allocated, otherwise we will just write over them */
525 if (!csmd->delta_cache.deltas) {
526 csmd->delta_cache.deltas_num = uint(corner_verts.size());
527 csmd->delta_cache.deltas = static_cast<float(*)[3]>(
528 MEM_malloc_arrayN(size_t(corner_verts.size()), sizeof(float[3]), __func__));
529 }
530
531 smooth_verts(csmd, mesh, dvert, defgrp_index, smooth_vertex_coords);
532
533 calc_tangent_spaces(mesh, smooth_vertex_coords, tangent_spaces, nullptr, nullptr);
534
535 copy_vn_fl(&csmd->delta_cache.deltas[0][0], int(corner_verts.size()) * 3, 0.0f);
536
537 for (l_index = 0; l_index < corner_verts.size(); l_index++) {
538 const int v_index = corner_verts[l_index];
539 float delta[3];
540 sub_v3_v3v3(delta, rest_coords[v_index], smooth_vertex_coords[v_index]);
541
542 float imat[3][3];
543 if (UNLIKELY(!invert_m3_m3(imat, tangent_spaces[l_index]))) {
544 transpose_m3_m3(imat, tangent_spaces[l_index]);
545 }
546 mul_v3_m3v3(csmd->delta_cache.deltas[l_index], imat, delta);
547 }
548
549 MEM_SAFE_FREE(tangent_spaces);
550}
551
553 Depsgraph *depsgraph,
554 Object *ob,
555 Mesh *mesh,
557 BMEditMesh *em)
558{
560
561 const bool force_delta_cache_update =
562 /* XXX, take care! if mesh data itself changes we need to forcefully recalculate deltas */
563 !cache_settings_equal(csmd) ||
565 (((ID *)ob->data)->recalc & ID_RECALC_ALL));
566
567 blender::Span<int> corner_verts = mesh->corner_verts();
568
569 bool use_only_smooth = (csmd->flag & MOD_CORRECTIVESMOOTH_ONLY_SMOOTH) != 0;
570 const MDeformVert *dvert = nullptr;
571 int defgrp_index;
572
573 MOD_get_vgroup(ob, mesh, csmd->defgrp_name, &dvert, &defgrp_index);
574
575 /* if rest bind_coords not are defined, set them (only run during bind) */
577 /* signal to recalculate, whoever sets MUST also free bind coords */
578 (csmd->bind_coords_num == uint(-1)))
579 {
581 BLI_assert(csmd->bind_coords == nullptr);
582 csmd->bind_coords = static_cast<float(*)[3]>(
583 MEM_malloc_arrayN(size_t(vertexCos.size()), sizeof(float[3]), __func__));
584 memcpy(csmd->bind_coords, vertexCos.data(), size_t(vertexCos.size_in_bytes()));
585 csmd->bind_coords_num = uint(vertexCos.size());
586 BLI_assert(csmd->bind_coords != nullptr);
587 /* Copy bound data to the original modifier. */
590 csmd_orig->bind_coords = static_cast<float(*)[3]>(MEM_dupallocN(csmd->bind_coords));
591 csmd_orig->bind_coords_num = csmd->bind_coords_num;
592 }
593 else {
594 BKE_modifier_set_error(ob, md, "Attempt to bind from inactive dependency graph");
595 }
596 }
597
598 if (UNLIKELY(use_only_smooth)) {
599 smooth_verts(csmd, mesh, dvert, defgrp_index, vertexCos);
600 return;
601 }
602
604 (csmd->bind_coords == nullptr))
605 {
606 BKE_modifier_set_error(ob, md, "Bind data required");
607 goto error;
608 }
609
610 /* If the number of verts has changed, the bind is invalid, so we do nothing */
612 if (csmd->bind_coords_num != vertexCos.size()) {
614 md,
615 "Bind vertex count mismatch: %u to %u",
616 csmd->bind_coords_num,
617 uint(vertexCos.size()));
618 goto error;
619 }
620 }
621 else {
622 /* MOD_CORRECTIVESMOOTH_RESTSOURCE_ORCO */
623 if (ob->type != OB_MESH) {
624 BKE_modifier_set_error(ob, md, "Object is not a mesh");
625 goto error;
626 }
627 else {
628 const int me_numVerts = (em) ? em->bm->totvert : ((Mesh *)ob->data)->verts_num;
629
630 if (me_numVerts != vertexCos.size()) {
632 md,
633 "Original vertex count mismatch: %u to %u",
634 uint(me_numVerts),
635 uint(vertexCos.size()));
636 goto error;
637 }
638 }
639 }
640
641 /* check to see if our deltas are still valid */
642 if (!csmd->delta_cache.deltas || (csmd->delta_cache.deltas_num != corner_verts.size()) ||
643 force_delta_cache_update)
644 {
645 blender::Array<blender::float3> rest_coords_alloc;
647
649
651 /* caller needs to do sanity check here */
652 csmd->bind_coords_num = uint(vertexCos.size());
653 rest_coords = {reinterpret_cast<const blender::float3 *>(csmd->bind_coords),
654 csmd->bind_coords_num};
655 }
656 else {
657 if (em) {
658 rest_coords_alloc = BKE_editmesh_vert_coords_alloc_orco(em);
659 rest_coords = rest_coords_alloc;
660 }
661 else {
662 const Mesh *object_mesh = static_cast<const Mesh *>(ob->data);
663 rest_coords = object_mesh->vert_positions();
664 }
665 }
666
667#ifdef DEBUG_TIME
668 TIMEIT_START(corrective_smooth_deltas);
669#endif
670
671 calc_deltas(csmd, mesh, dvert, defgrp_index, rest_coords);
672
673#ifdef DEBUG_TIME
674 TIMEIT_END(corrective_smooth_deltas);
675#endif
676 }
677
679 /* this could be a check, but at this point it _must_ be valid */
680 BLI_assert(csmd->bind_coords_num == vertexCos.size() && csmd->delta_cache.deltas);
681 }
682
683#ifdef DEBUG_TIME
684 TIMEIT_START(corrective_smooth);
685#endif
686
687 /* do the actual delta mush */
688 smooth_verts(csmd, mesh, dvert, defgrp_index, vertexCos);
689
690 {
691
692 const float scale = csmd->scale;
693
694 float(*tangent_spaces)[3][3] = static_cast<float(*)[3][3]>(
695 MEM_malloc_arrayN(size_t(corner_verts.size()), sizeof(float[3][3]), __func__));
696 float *tangent_weights = static_cast<float *>(
697 MEM_malloc_arrayN(size_t(corner_verts.size()), sizeof(float), __func__));
698 float *tangent_weights_per_vertex = static_cast<float *>(
699 MEM_malloc_arrayN(size_t(vertexCos.size()), sizeof(float), __func__));
700
702 mesh, vertexCos, tangent_spaces, tangent_weights, tangent_weights_per_vertex);
703
704 for (const int64_t l_index : corner_verts.index_range()) {
705 const int v_index = corner_verts[l_index];
706 const float weight = tangent_weights[l_index] / tangent_weights_per_vertex[v_index];
707 if (UNLIKELY(!(weight > 0.0f))) {
708 /* Catches zero & divide by zero. */
709 continue;
710 }
711
712 float delta[3];
713 mul_v3_m3v3(delta, tangent_spaces[l_index], csmd->delta_cache.deltas[l_index]);
714 mul_v3_fl(delta, weight);
715 madd_v3_v3fl(vertexCos[v_index], delta, scale);
716 }
717
718 MEM_freeN(tangent_spaces);
719 MEM_freeN(tangent_weights);
720 MEM_freeN(tangent_weights_per_vertex);
721 }
722
723#ifdef DEBUG_TIME
724 TIMEIT_END(corrective_smooth);
725#endif
726
727 return;
728
729 /* when the modifier fails to execute */
730error:
732 csmd->delta_cache.deltas_num = 0;
733}
734
736 const ModifierEvalContext *ctx,
737 Mesh *mesh,
739{
740 correctivesmooth_modifier_do(md, ctx->depsgraph, ctx->object, mesh, positions, nullptr);
741}
742
743static void panel_draw(const bContext * /*C*/, Panel *panel)
744{
745 uiLayout *layout = panel->layout;
746
747 PointerRNA ob_ptr;
749
750 uiLayoutSetPropSep(layout, true);
751
752 uiItemR(layout, ptr, "factor", UI_ITEM_NONE, IFACE_("Factor"), ICON_NONE);
753 uiItemR(layout, ptr, "iterations", UI_ITEM_NONE, nullptr, ICON_NONE);
754 uiItemR(layout, ptr, "scale", UI_ITEM_NONE, nullptr, ICON_NONE);
755 uiItemR(layout, ptr, "smooth_type", UI_ITEM_NONE, nullptr, ICON_NONE);
756
757 modifier_vgroup_ui(layout, ptr, &ob_ptr, "vertex_group", "invert_vertex_group", nullptr);
758
759 uiItemR(layout, ptr, "use_only_smooth", UI_ITEM_NONE, nullptr, ICON_NONE);
760 uiItemR(layout, ptr, "use_pin_boundary", UI_ITEM_NONE, nullptr, ICON_NONE);
761
762 uiItemR(layout, ptr, "rest_source", UI_ITEM_NONE, nullptr, ICON_NONE);
764 uiItemO(layout,
765 (RNA_boolean_get(ptr, "is_bind") ? IFACE_("Unbind") : IFACE_("Bind")),
766 ICON_NONE,
767 "OBJECT_OT_correctivesmooth_bind");
768 }
769
770 modifier_panel_end(layout, ptr);
771}
772
777
778static void blend_write(BlendWriter *writer, const ID *id_owner, const ModifierData *md)
779{
781 const bool is_undo = BLO_write_is_undo(writer);
782
783 if (ID_IS_OVERRIDE_LIBRARY(id_owner) && !is_undo) {
784 BLI_assert(!ID_IS_LINKED(id_owner));
785 const bool is_local = (md->flag & eModifierFlag_OverrideLibrary_Local) != 0;
786 if (!is_local) {
787 /* Modifier coming from linked data cannot be bound from an override, so we can remove all
788 * binding data, can save a significant amount of memory. */
789 csmd.bind_coords_num = 0;
790 csmd.bind_coords = nullptr;
791 }
792 }
793
795
796 if (csmd.bind_coords != nullptr) {
797 BLO_write_float3_array(writer, csmd.bind_coords_num, (float *)csmd.bind_coords);
798 }
799}
800
801static void blend_read(BlendDataReader *reader, ModifierData *md)
802{
804
805 if (csmd->bind_coords) {
806 BLO_read_float3_array(reader, int(csmd->bind_coords_num), (float **)&csmd->bind_coords);
807 }
808
809 /* runtime only */
810 csmd->delta_cache.deltas = nullptr;
811 csmd->delta_cache.deltas_num = 0;
812}
813
815 /*idname*/ "CorrectiveSmooth",
816 /*name*/ N_("CorrectiveSmooth"),
817 /*struct_name*/ "CorrectiveSmoothModifierData",
818 /*struct_size*/ sizeof(CorrectiveSmoothModifierData),
819 /*srna*/ &RNA_CorrectiveSmoothModifier,
822 /*icon*/ ICON_MOD_SMOOTH,
823
824 /*copy_data*/ copy_data,
825
826 /*deform_verts*/ deform_verts,
827 /*deform_matrices*/ nullptr,
828 /*deform_verts_EM*/ nullptr,
829 /*deform_matrices_EM*/ nullptr,
830 /*modify_mesh*/ nullptr,
831 /*modify_geometry_set*/ nullptr,
832
833 /*init_data*/ init_data,
834 /*required_data_mask*/ required_data_mask,
835 /*free_data*/ free_data,
836 /*is_disabled*/ nullptr,
837 /*update_depsgraph*/ nullptr,
838 /*depends_on_time*/ nullptr,
839 /*depends_on_normals*/ nullptr,
840 /*foreach_ID_link*/ nullptr,
841 /*foreach_tex_link*/ nullptr,
842 /*free_runtime_data*/ nullptr,
843 /*panel_register*/ panel_register,
844 /*blend_write*/ blend_write,
845 /*blend_read*/ blend_read,
846 /*foreach_cache*/ nullptr,
847};
support for deformation groups and hooks.
float BKE_defvert_find_weight(const MDeformVert *dvert, int defgroup)
Definition deform.cc:770
blender::Array< blender::float3 > BKE_editmesh_vert_coords_alloc_orco(BMEditMesh *em)
Definition editmesh.cc:203
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:50
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, int array_size, float **ptr_p)
Definition readfile.cc:4977
void BLO_write_float3_array(BlendWriter *writer, uint 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:318
@ ID_RECALC_ALL
Definition DNA_ID.h:1155
#define ID_IS_LINKED(_id)
Definition DNA_ID.h:654
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition DNA_ID.h:683
#define CD_MASK_MDEFORMVERT
#define DNA_struct_default_get(struct_name)
@ eModifierFlag_OverrideLibrary_Local
@ MOD_CORRECTIVESMOOTH_ONLY_SMOOTH
@ MOD_CORRECTIVESMOOTH_PIN_BOUNDARY
@ MOD_CORRECTIVESMOOTH_INVERT_VGROUP
@ eModifierType_CorrectiveSmooth
@ MOD_CORRECTIVESMOOTH_RESTSOURCE_ORCO
@ MOD_CORRECTIVESMOOTH_RESTSOURCE_BIND
struct CorrectiveSmoothModifierData CorrectiveSmoothModifierData
@ MOD_CORRECTIVESMOOTH_SMOOTH_LENGTH_WEIGHT
Object is a sort of wrapper for general info.
@ OB_MESH
Read Guarded memory(de)allocation.
#define MEM_SAFE_FREE(v)
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_panel_end(uiLayout *layout, PointerRNA *ptr)
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_vgroup_ui(uiLayout *layout, PointerRNA *ptr, PointerRNA *ob_ptr, const char *vgroup_prop, const char *invert_vgroup_prop, const char *text)
void MOD_get_vgroup(const Object *ob, const Mesh *mesh, const char *name, const MDeformVert **dvert, int *defgrp_index)
Definition MOD_util.cc:159
void uiLayoutSetPropSep(uiLayout *layout, bool is_sep)
#define UI_ITEM_NONE
void uiItemO(uiLayout *layout, const char *name, int icon, const char *opname)
void uiItemR(uiLayout *layout, PointerRNA *ptr, const char *propname, eUI_Item_Flag flag, const char *name, int icon)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
constexpr int64_t size() const
Definition BLI_span.hh:494
constexpr T * data() const
Definition BLI_span.hh:540
constexpr int64_t size_in_bytes() const
Definition BLI_span.hh:502
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:138
constexpr int64_t size() const
Definition BLI_span.hh:253
constexpr IndexRange index_range() const
Definition BLI_span.hh:402
const Depsgraph * depsgraph
#define fabsf(x)
draw_view in_light_buf[] float
draw_view push_constant(Type::INT, "radiance_src") .push_constant(Type capture_info_buf storage_buf(1, Qualifier::READ, "ObjectBounds", "bounds_buf[]") .push_constant(Type draw_view int
void *(* MEM_malloc_arrayN)(size_t len, size_t size, const char *str)
Definition mallocn.cc:45
void *(* MEM_calloc_arrayN)(size_t len, size_t size, const char *str)
Definition mallocn.cc:43
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
void *(* MEM_dupallocN)(const void *vmemh)
Definition mallocn.cc:39
static void error(const char *str)
float safe_acos_approx(float x)
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)
__int64 int64_t
Definition stdint.h:89
unsigned char uint8_t
Definition stdint.h:78
int totvert
CorrectiveSmoothDeltaCache delta_cache
Definition DNA_ID.h:413
struct uiLayout * layout
#define N_(msgid)
PointerRNA * ptr
Definition wm_files.cc:4126
uint8_t flag
Definition wm_window.cc:138