Blender V5.3
sculpt_pose.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10#include "BLI_math_geom_c.hh"
11#include "BLI_math_matrix.hh"
12#include "BLI_math_matrix_c.hh"
14#include "BLI_math_vector.hh"
15#include "BLI_math_vector_c.hh"
16
17#include "DNA_brush_types.h"
18#include "DNA_object_types.h"
19
20#include "BKE_brush.hh"
21#include "BKE_ccg.hh"
22#include "BKE_colortools.hh"
23#include "BKE_mesh.hh"
24#include "BKE_object_types.hh"
25#include "BKE_paint.hh"
26#include "BKE_paint_bvh.hh"
27
28#include "../paint_intern.hh"
29#include "mesh_brush_common.hh"
30#include "sculpt_automask.hh"
31#include "sculpt_cloth.hh"
32#include "sculpt_face_set.hh"
33#include "sculpt_flood_fill.hh"
34#include "sculpt_hide.hh"
35#include "sculpt_intern.hh"
36#include "sculpt_pose.hh"
37#include "sculpt_smooth.hh"
38
39#include "bmesh.hh"
40
41#include <cmath>
42
44
45static void solve_ik_chain(IKChain &ik_chain, const float3 &initial_target, const bool use_anchor)
46{
47 MutableSpan<IKChainSegment> segments = ik_chain.segments;
48
49 /* Set the initial target. */
50 float3 target = initial_target;
51
52 /* Solve the positions and rotations of all segments in the chain. */
53 for (const int i : segments.index_range()) {
54 /* Calculate the rotation to orientate the segment to the target from its initial state. */
55 float3 current_orientation = math::normalize(target - segments[i].orig);
56 float3 initial_orientation = math::normalize(segments[i].initial_head -
57 segments[i].initial_orig);
58 rotation_between_vecs_to_quat(segments[i].rot, initial_orientation, current_orientation);
59
60 /* Rotate the segment by calculating a new head position. */
61 float3 current_head_position = segments[i].orig + current_orientation * segments[i].len;
62
63 /* Move the origin of the segment towards the target. */
64 float3 current_origin_position = target - current_head_position;
65
66 /* Store the new head and origin positions to the segment. */
67 segments[i].head = current_head_position;
68 segments[i].orig += current_origin_position;
69
70 /* Use the origin of this segment as target for the next segment in the chain. */
71 target = segments[i].orig;
72 }
73
74 /* Move back the whole chain to preserve the anchor point. */
75 if (use_anchor) {
76 float3 anchor_diff = segments.last().initial_orig - segments.last().orig;
77 for (const int i : segments.index_range()) {
78 segments[i].orig += anchor_diff;
79 segments[i].head += anchor_diff;
80 }
81 }
82}
83
84static void solve_roll_chain(IKChain &ik_chain, const Brush &brush, const float roll)
85{
86 MutableSpan<IKChainSegment> segments = ik_chain.segments;
87
88 for (const int i : segments.index_range()) {
89 float3 initial_orientation = math::normalize(segments[i].initial_head -
90 segments[i].initial_orig);
91 float initial_rotation[4];
92 float current_rotation[4];
93
94 /* Calculate the current roll angle using the brush curve. */
95 float current_roll = roll * BKE_brush_curve_strength(&brush, i, segments.size());
96
97 axis_angle_normalized_to_quat(initial_rotation, initial_orientation, 0.0f);
98 axis_angle_normalized_to_quat(current_rotation, initial_orientation, current_roll);
99
100 /* Store the difference of the rotations in the segment rotation. */
101 rotation_between_quats_to_quat(segments[i].rot, current_rotation, initial_rotation);
102 }
103}
104
105static void solve_translate_chain(IKChain &ik_chain, const float delta[3])
106{
107 for (IKChainSegment &segment : ik_chain.segments) {
108 /* Move the origin and head of each segment by delta. */
109 add_v3_v3v3(segment.head, segment.initial_head, delta);
110 add_v3_v3v3(segment.orig, segment.initial_orig, delta);
111
112 /* Reset the segment rotation. */
113 unit_qt(segment.rot);
114 }
115}
116
117static void solve_scale_chain(IKChain &ik_chain, const float scale[3])
118{
119 for (IKChainSegment &segment : ik_chain.segments) {
120 /* Assign the scale to each segment. */
121 copy_v3_v3(segment.scale, scale);
122 }
123}
124
132
134 const IKChainSegment &segment,
135 const MutableSpan<float3> translations)
136{
138 BLI_assert(positions.size() == translations.size());
139 for (const int i : positions.index_range()) {
140 float3 position = positions[i];
142 position = math::transform_point(segment.pivot_mat_inv[int(symm_area)], position);
143 position = math::transform_point(segment.trans_mat[int(symm_area)], position);
144 position = math::transform_point(segment.pivot_mat[int(symm_area)], position);
145 translations[i] = position - positions[i];
146 }
147}
148
150{
152 BLI_assert(a.size() == b.size());
153 for (const int i : a.index_range()) {
154 a[i] += b[i];
155 }
156}
157
158static void calc_mesh(const Depsgraph &depsgraph,
159 const Sculpt &sd,
160 const Brush &brush,
161 const MeshAttributeData &attribute_data,
163 Object &object,
164 const PositionDeformData &position_data)
165{
166 SculptSession &ss = *object.runtime->sculpt_session;
167 const StrokeCache &cache = *ss.cache;
168
169 const Span<int> verts = node.verts();
171 gather_data_mesh(position_data.eval, verts, positions.as_mutable_span());
172 const OrigPositionData orig_data = orig_position_data_get_mesh(object, node);
173
175 fill_factor_from_hide_and_mask(attribute_data.hide_vert, attribute_data.mask, verts, factors);
176 auto_mask::calc_vert_factors(depsgraph, object, cache.automasking.get(), node, verts, factors);
177
179 translations.fill(float3(0));
180
181 Array<float, bke::pbvh::MESH_LEAF_LIMIT> segment_weights(verts.size());
182 Array<float3, bke::pbvh::MESH_LEAF_LIMIT> segment_translations(verts.size());
183
184 for (const IKChainSegment &segment : cache.pose_ik_chain->segments) {
185 calc_segment_translations(orig_data.positions, segment, segment_translations);
186 gather_data_mesh(segment.weights.as_span(), verts, segment_weights.as_mutable_span());
187 scale_translations(segment_translations, segment_weights);
188 add_arrays(translations, segment_translations);
189 }
190 scale_translations(translations, factors);
191
192 switch (eBrushDeformTarget(brush.deform_target)) {
194 reset_translations_to_original(translations, positions, orig_data.positions);
195 clip_and_lock_translations(sd, ss, position_data.eval, verts, translations);
196 position_data.deform(translations, verts);
197 break;
199 add_arrays(translations, orig_data.positions);
201 translations.as_span(), verts, cache.cloth_sim->deformation_pos.as_mutable_span());
202 break;
203 }
204}
205
206static void calc_grids(const Depsgraph &depsgraph,
207 const Sculpt &sd,
208 const Brush &brush,
210 Object &object,
211 BrushLocalData &tls)
212{
213 SculptSession &ss = *object.runtime->sculpt_session;
214 const StrokeCache &cache = *ss.cache;
215 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
216
217 const Span<int> grids = node.grids();
218 const Span<float3> positions = gather_grids_positions(subdiv_ccg, grids, tls.positions);
219 const OrigPositionData orig_data = orig_position_data_get_grids(object, node);
220
221 tls.factors.resize(positions.size());
222 const MutableSpan<float> factors = tls.factors;
223 fill_factor_from_hide_and_mask(subdiv_ccg, grids, factors);
224 auto_mask::calc_grids_factors(depsgraph, object, cache.automasking.get(), node, grids, factors);
225
226 tls.translations.resize(positions.size());
227 const MutableSpan<float3> translations = tls.translations;
228 translations.fill(float3(0));
229
230 tls.segment_weights.resize(positions.size());
231 tls.segment_translations.resize(positions.size());
232 const MutableSpan<float> segment_weights = tls.segment_weights;
233 const MutableSpan<float3> segment_translations = tls.segment_translations;
234
235 for (const IKChainSegment &segment : cache.pose_ik_chain->segments) {
236 calc_segment_translations(orig_data.positions, segment, segment_translations);
237 gather_data_grids(subdiv_ccg, segment.weights.as_span(), grids, segment_weights);
238 scale_translations(segment_translations, segment_weights);
239 add_arrays(translations, segment_translations);
240 }
241 scale_translations(translations, factors);
242
243 switch (eBrushDeformTarget(brush.deform_target)) {
245 reset_translations_to_original(translations, positions, orig_data.positions);
246 clip_and_lock_translations(sd, ss, orig_data.positions, translations);
247 apply_translations(translations, grids, subdiv_ccg);
248 break;
250 add_arrays(translations, orig_data.positions);
251 scatter_data_grids(subdiv_ccg,
252 translations.as_span(),
253 grids,
254 cache.cloth_sim->deformation_pos.as_mutable_span());
255 break;
256 }
257}
258
259static void calc_bmesh(const Depsgraph &depsgraph,
260 const Sculpt &sd,
261 const Brush &brush,
263 Object &object,
264 BrushLocalData &tls)
265{
266 SculptSession &ss = *object.runtime->sculpt_session;
267 const StrokeCache &cache = *ss.cache;
268
270 const Span<float3> positions = gather_bmesh_positions(verts, tls.positions);
271 Array<float3> orig_positions(verts.size());
272 Array<float3> orig_normals(verts.size());
273 orig_position_data_gather_bmesh(*ss.bm_log, verts, orig_positions, orig_normals);
274
275 tls.factors.resize(verts.size());
276 const MutableSpan<float> factors = tls.factors;
277 fill_factor_from_hide_and_mask(*ss.bm, verts, factors);
278 auto_mask::calc_vert_factors(depsgraph, object, cache.automasking.get(), node, verts, factors);
279
280 tls.translations.resize(verts.size());
281 const MutableSpan<float3> translations = tls.translations;
282 translations.fill(float3(0));
283
284 tls.segment_weights.resize(verts.size());
285 tls.segment_translations.resize(verts.size());
286 const MutableSpan<float> segment_weights = tls.segment_weights;
287 const MutableSpan<float3> segment_translations = tls.segment_translations;
288
289 for (const IKChainSegment &segment : cache.pose_ik_chain->segments) {
290 calc_segment_translations(orig_positions, segment, segment_translations);
291 gather_data_bmesh(segment.weights.as_span(), verts, segment_weights);
292 scale_translations(segment_translations, segment_weights);
293 add_arrays(translations, segment_translations);
294 }
295 scale_translations(translations, factors);
296
297 switch (eBrushDeformTarget(brush.deform_target)) {
299 reset_translations_to_original(translations, positions, orig_positions);
300 clip_and_lock_translations(sd, ss, orig_positions, translations);
301 apply_translations(translations, verts);
302 break;
304 add_arrays(translations, orig_positions);
306 translations.as_span(), verts, cache.cloth_sim->deformation_pos.as_mutable_span());
307 break;
308 }
309}
310
315 {
316 PoseGrowFactorData joined;
317 joined.pos_avg = a.pos_avg + b.pos_avg;
318 joined.pos_count = a.pos_count + b.pos_count;
319 return joined;
320 }
321};
322
329
330BLI_NOINLINE static void add_fake_neighbors(const Span<int> fake_neighbors,
331 const Span<int> verts,
332 const Span<int> orig_neighbor_data,
333 MutableSpan<int> neighbor_offsets,
334 Vector<int> &neighbor_data_with_fake)
335{
337 const OffsetIndices<int> offsets(neighbor_offsets);
338 for (const int i : verts.index_range()) {
339 const Span<int> orig_neighbors = orig_neighbor_data.slice(offsets[i]);
340
341 /* Modify the offsets in-place after using them to slice the current neighbor data. */
342 neighbor_offsets[i] = neighbor_data_with_fake.size();
343 neighbor_data_with_fake.extend(orig_neighbors);
344 const int neighbor = fake_neighbors[verts[i]];
345 if (neighbor != FAKE_NEIGHBOR_NONE) {
346 neighbor_data_with_fake.append(neighbor);
347 }
348 }
349 neighbor_offsets.last() = neighbor_data_with_fake.size();
350}
351
353 const float3 &pose_initial_position,
354 const Span<float3> vert_positions,
355 const OffsetIndices<int> faces,
356 const Span<int> corner_verts,
357 const GroupedSpan<int> vert_to_face_map,
358 const Span<bool> hide_vert,
359 const Span<bool> hide_poly,
360 const Span<int> fake_neighbors,
361 const Span<float> prev_mask,
364 const MutableSpan<float> pose_factor,
365 PoseGrowFactorData &gftd)
366{
368 const Span<int> verts = hide::node_visible_verts(node, hide_vert, index_data);
369
371 corner_verts,
372 vert_to_face_map,
373 hide_poly,
374 verts,
376 tls.neighbor_data);
377 if (!fake_neighbors.is_empty()) {
378 add_fake_neighbors(fake_neighbors,
379 verts,
380 tls.neighbor_data,
383 }
384 const GroupedSpan<int> neighbors(tls.neighbor_offsets.as_span(),
385 fake_neighbors.is_empty() ?
386 tls.neighbor_data.as_span() :
388
389 for (const int i : verts.index_range()) {
390 const int vert = verts[i];
391
392 float max = 0.0f;
393 for (const int neighbor : neighbors[i]) {
394 max = std::max(max, prev_mask[neighbor]);
395 }
396
397 if (max > prev_mask[vert]) {
398 const float3 &position = vert_positions[verts[i]];
399 pose_factor[vert] = max;
400 if (check_vertex_pivot_symmetry(position, pose_initial_position, symm)) {
401 gftd.pos_avg += position;
402 gftd.pos_count++;
403 }
404 }
405 }
406}
407
409 const float3 &pose_initial_position,
410 const SubdivCCG &subdiv_ccg,
411 const Span<int> fake_neighbors,
412 const Span<float> prev_mask,
414 const MutableSpan<float> pose_factor,
415 PoseGrowFactorData &gftd)
416{
417 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
418 const Span<float3> positions = subdiv_ccg.positions;
419 const BitGroupVector<> &grid_hidden = subdiv_ccg.grid_hidden;
420 const Span<int> grids = node.grids();
421
422 for (const int i : grids.index_range()) {
423 const int grid = grids[i];
424 const IndexRange grid_range = bke::ccg::grid_range(key, grid);
425 for (const short y : IndexRange(key.grid_size)) {
426 for (const short x : IndexRange(key.grid_size)) {
427 const int offset = CCG_grid_xy_to_index(key.grid_size, x, y);
428 if (!grid_hidden.is_empty() && grid_hidden[grid][offset]) {
429 continue;
430 }
431 const int vert = grid_range[offset];
432
433 SubdivCCGNeighbors neighbors;
435 subdiv_ccg, SubdivCCGCoord{grid, x, y}, false, neighbors);
436
437 float max = 0.0f;
438 for (const SubdivCCGCoord neighbor : neighbors.coords) {
439 max = std::max(max, prev_mask[neighbor.to_index(key)]);
440 }
441 if (!fake_neighbors.is_empty()) {
442 if (fake_neighbors[vert] != FAKE_NEIGHBOR_NONE) {
443 max = std::max(max, prev_mask[fake_neighbors[vert]]);
444 }
445 }
446
447 if (max > prev_mask[vert]) {
448 const float3 &position = positions[vert];
449 pose_factor[vert] = max;
450 if (check_vertex_pivot_symmetry(position, pose_initial_position, symm)) {
451 gftd.pos_avg += position;
452 gftd.pos_count++;
453 }
454 }
455 }
456 }
457 }
458}
459
461 const float3 &pose_initial_position,
462 const Span<int> fake_neighbors,
463 const Span<float> prev_mask,
465 const MutableSpan<float> pose_factor,
466 PoseGrowFactorData &gftd)
467{
469
470 BMeshNeighborVerts neighbors;
471
472 for (BMVert *bm_vert : verts) {
473 const int vert = BM_elem_index_get(bm_vert);
474
475 float max = 0.0f;
476 for (const BMVert *neighbor : vert_neighbors_get_bmesh(*bm_vert, neighbors)) {
477 max = std::max(max, prev_mask[BM_elem_index_get(neighbor)]);
478 }
479 if (!fake_neighbors.is_empty()) {
480 if (fake_neighbors[vert] != FAKE_NEIGHBOR_NONE) {
481 max = std::max(max, prev_mask[fake_neighbors[vert]]);
482 }
483 }
484
485 if (max > prev_mask[vert]) {
486 const float3 position = bm_vert->co;
487 pose_factor[vert] = max;
488 if (check_vertex_pivot_symmetry(position, pose_initial_position, symm)) {
489 gftd.pos_avg += position;
490 gftd.pos_count++;
491 }
492 }
493 }
494}
495
496/* Grow the factor until its boundary is near to the offset pose origin or outside the target
497 * distance. */
498static void grow_pose_factor(const Depsgraph &depsgraph,
499 Object &ob,
500 SculptSession &ss,
501 float pose_origin[3],
502 float pose_target[3],
503 float max_len,
504 float *r_pose_origin,
505 MutableSpan<float> pose_factor)
506{
510
511 IndexMaskMemory memory;
512 const IndexMask node_mask = bke::pbvh::all_leaf_nodes(pbvh, memory);
513 const Span<int> fake_neighbors = ss.fake_neighbors.fake_neighbor_index;
514
515 bool grow_next_iteration = true;
516 float prev_len = FLT_MAX;
517 Array<float> prev_mask(vertex_count_get(ob));
518 while (grow_next_iteration) {
519 prev_mask.as_mutable_span().copy_from(pose_factor);
520
523 switch (pbvh.type()) {
526 const Mesh &mesh = *id_cast<const Mesh *>(ob.data);
527 const Span<float3> vert_positions = bke::pbvh::vert_positions_eval(depsgraph, ob);
528 const OffsetIndices faces = mesh.faces();
529 const Span<int> corner_verts = mesh.corner_verts();
530 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
531 const bke::AttributeAccessor attributes = mesh.attributes();
532 const VArraySpan<bool> hide_vert = *attributes.lookup<bool>(".hide_vert",
534 const VArraySpan<bool> hide_poly = *attributes.lookup<bool>(".hide_poly",
537 node_mask.index_range(),
538 1,
540 [&](const IndexRange range, PoseGrowFactorData gftd) {
541 GrowFactorLocalData &tls = all_tls.local();
542 node_mask.slice(range).foreach_index([&](const int i) {
543 grow_factors_mesh(symm,
544 pose_target,
545 vert_positions,
546 faces,
547 corner_verts,
548 vert_to_face_map,
549 hide_vert,
550 hide_poly,
551 fake_neighbors,
552 prev_mask,
553 nodes[i],
554 tls,
555 pose_factor,
556 gftd);
557 });
558 return gftd;
559 },
561 break;
562 }
565 const SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
567 node_mask.index_range(),
568 1,
570 [&](const IndexRange range, PoseGrowFactorData gftd) {
571 node_mask.slice(range).foreach_index([&](const int i) {
572 grow_factors_grids(symm,
573 pose_target,
574 subdiv_ccg,
575 fake_neighbors,
576 prev_mask,
577 nodes[i],
578 pose_factor,
579 gftd);
580 });
581 return gftd;
582 },
584 break;
585 }
589 node_mask.index_range(),
590 1,
592 [&](const IndexRange range, PoseGrowFactorData gftd) {
593 node_mask.slice(range).foreach_index([&](const int i) {
594 grow_factors_bmesh(
595 symm, pose_target, fake_neighbors, prev_mask, nodes[i], pose_factor, gftd);
596 });
597 return gftd;
598 },
600 break;
601 }
602 }
603
604 if (gftd.pos_count != 0) {
605 gftd.pos_avg /= float(gftd.pos_count);
606 if (pose_origin) {
607 /* Test with pose origin. Used when growing the factors to compensate the Origin Offset. */
608 /* Stop when the factor's avg_pos starts moving away from the origin instead of getting
609 * closer to it. */
610 float len = math::distance(gftd.pos_avg, float3(pose_origin));
611 if (len < prev_len) {
612 prev_len = len;
613 grow_next_iteration = true;
614 }
615 else {
616 grow_next_iteration = false;
617 pose_factor.copy_from(prev_mask);
618 }
619 }
620 else {
621 /* Test with length. Used to calculate the origin positions of the IK chain. */
622 /* Stops when the factors have grown enough to generate a new segment origin. */
623 float len = math::distance(gftd.pos_avg, float3(pose_target));
624 if (len < max_len) {
625 prev_len = len;
626 grow_next_iteration = true;
627 }
628 else {
629 grow_next_iteration = false;
630 if (r_pose_origin) {
631 copy_v3_v3(r_pose_origin, gftd.pos_avg);
632 }
633 pose_factor.copy_from(prev_mask);
634 }
635 }
636 }
637 else {
638 if (r_pose_origin) {
639 copy_v3_v3(r_pose_origin, pose_target);
640 }
641 grow_next_iteration = false;
642 }
643 }
644}
645
647 const float3 &br_co,
648 float radius,
649 char symm)
650{
651 for (char i = 0; i <= symm; ++i) {
652 if (is_symmetry_iteration_valid(i, symm)) {
653 const float3 location = symmetry_flip(br_co, ePaintSymmetryFlags(i));
654 if (math::distance(location, vertex) < radius) {
655 return true;
656 }
657 }
658 }
659 return false;
660}
661
666static void calc_pose_origin_and_factor_mesh(const Depsgraph &depsgraph,
667 Object &object,
668 SculptSession &ss,
669 const float3 &initial_location,
670 float radius,
671 float3 &r_pose_origin,
672 MutableSpan<float> r_pose_factor)
673{
674 BLI_assert(!r_pose_factor.is_empty());
675
676 const Mesh &mesh = *id_cast<const Mesh *>(object.data);
677 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
678 const Span<float3> positions_eval = bke::pbvh::vert_positions_eval(depsgraph, object);
679
680 /* Calculate the pose rotation point based on the boundaries of the brush factor. */
682 flood.add_initial(find_symm_verts_mesh(depsgraph, object, ss.active_vert_index(), radius));
683
684 const int symm = mesh_symmetry_xyz_get(object);
685
686 int tot_co = 0;
687 float3 pose_origin(0);
688 float3 fallback_floodfill_origin = initial_location;
689 flood.execute(object, vert_to_face_map, [&](int /*from_v*/, int to_v) {
690 r_pose_factor[to_v] = 1.0f;
691
692 const float3 co = positions_eval[to_v];
693 if (math::distance_squared(initial_location, fallback_floodfill_origin) <
694 math::distance_squared(initial_location, co))
695 {
696 fallback_floodfill_origin = co;
697 }
698
699 if (vert_inside_brush_radius(co, initial_location, radius, symm)) {
700 return true;
701 }
702
703 if (check_vertex_pivot_symmetry(co, initial_location, symm)) {
704 pose_origin += co;
705 tot_co++;
706 }
707
708 return false;
709 });
710
711 if (tot_co > 0) {
712 r_pose_origin = pose_origin / float(tot_co);
713 }
714 else {
715 r_pose_origin = fallback_floodfill_origin;
716 }
717}
718
720 SculptSession &ss,
721 const float3 &initial_location,
722 float radius,
723 float3 &r_pose_origin,
724 MutableSpan<float> r_pose_factor)
725{
726 BLI_assert(!r_pose_factor.is_empty());
727
728 const SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
729
730 const Span<float3> positions = subdiv_ccg.positions;
731 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
732 /* Calculate the pose rotation point based on the boundaries of the brush factor. */
734 flood.add_initial(key, find_symm_verts_grids(object, ss.active_vert_index(), radius));
735
736 const int symm = mesh_symmetry_xyz_get(object);
737
738 int tot_co = 0;
739 float3 pose_origin(0);
740 float3 fallback_floodfill_origin = initial_location;
741 flood.execute(
742 object, subdiv_ccg, [&](SubdivCCGCoord /*from_v*/, SubdivCCGCoord to_v, bool is_duplicate) {
743 const int to_v_i = to_v.to_index(key);
744
745 r_pose_factor[to_v_i] = 1.0f;
746
747 const float3 &co = positions[to_v_i];
748 if (math::distance_squared(initial_location, fallback_floodfill_origin) <
749 math::distance_squared(initial_location, co))
750 {
751 fallback_floodfill_origin = co;
752 }
753
754 if (vert_inside_brush_radius(co, initial_location, radius, symm)) {
755 return true;
756 }
757
758 if (check_vertex_pivot_symmetry(co, initial_location, symm)) {
759 if (!is_duplicate) {
760 pose_origin += co;
761 tot_co++;
762 }
763 }
764
765 return false;
766 });
767
768 if (tot_co > 0) {
769 r_pose_origin = pose_origin / float(tot_co);
770 }
771 else {
772 r_pose_origin = fallback_floodfill_origin;
773 }
774}
775
777 SculptSession &ss,
778 const float3 &initial_location,
779 float radius,
780 float3 &r_pose_origin,
781 MutableSpan<float> r_pose_factor)
782{
783 BLI_assert(!r_pose_factor.is_empty());
785
786 /* Calculate the pose rotation point based on the boundaries of the brush factor. */
789 flood.add_initial(*ss.bm, find_symm_verts_bmesh(object, ss.active_vert_index(), radius));
790
791 const int symm = mesh_symmetry_xyz_get(object);
792
793 int tot_co = 0;
794 float3 pose_origin(0);
795 float3 fallback_floodfill_origin = initial_location;
796 flood.execute(object, [&](BMVert * /*from_v*/, BMVert *to_v) {
797 const int to_v_i = BM_elem_index_get(to_v);
798 r_pose_factor[to_v_i] = 1.0f;
799
800 const float3 co = to_v->co;
801 if (math::distance_squared(initial_location, fallback_floodfill_origin) <
802 math::distance_squared(initial_location, co))
803 {
804 fallback_floodfill_origin = co;
805 }
806
807 if (vert_inside_brush_radius(co, initial_location, radius, symm)) {
808 return true;
809 }
810
811 if (check_vertex_pivot_symmetry(co, initial_location, symm)) {
812 pose_origin += co;
813 tot_co++;
814 }
815
816 return false;
817 });
818
819 if (tot_co > 0) {
820 r_pose_origin = pose_origin / float(tot_co);
821 }
822 else {
823 r_pose_origin = fallback_floodfill_origin;
824 }
825}
826
827static void calc_pose_data(const Depsgraph &depsgraph,
828 Object &object,
829 SculptSession &ss,
830 const float3 &initial_location,
831 float radius,
832 float pose_offset,
833 float3 &r_pose_origin,
834 MutableSpan<float> r_pose_factor)
835{
837 BLI_assert(!r_pose_factor.is_empty());
838
839 float3 pose_origin;
840 switch (bke::object::pbvh_get(object)->type()) {
843 depsgraph, object, ss, initial_location, radius, pose_origin, r_pose_factor);
844 break;
847 object, ss, initial_location, radius, pose_origin, r_pose_factor);
848 break;
851 object, ss, initial_location, radius, pose_origin, r_pose_factor);
852 break;
853 }
854
855 /* Offset the pose origin. */
856 const float3 pose_dir = math::normalize(pose_origin - initial_location);
857 pose_origin += pose_dir * radius * pose_offset;
858 r_pose_origin = pose_origin;
859
860 /* Do the initial grow of the factors to get the first segment of the chain with Origin Offset.
861 */
862 if (pose_offset != 0.0f) {
863 grow_pose_factor(depsgraph, object, ss, pose_origin, pose_origin, 0, nullptr, r_pose_factor);
864 }
865}
866
867/* Init the IK chain with empty weights. */
868static std::unique_ptr<IKChain> ik_chain_new(const int totsegments, const int totverts)
869{
870 std::unique_ptr<IKChain> ik_chain = std::make_unique<IKChain>();
871 ik_chain->segments.reinitialize(totsegments);
872 for (IKChainSegment &segment : ik_chain->segments) {
873 segment.weights = Array<float>(totverts, 0.0f);
874 }
875 return ik_chain;
876}
877
878/* Init the origin/head pairs of all the segments from the calculated origins. */
879static void ik_chain_origin_heads_init(IKChain &ik_chain, const float3 &initial_location)
880{
881 float3 origin;
882 float3 head;
883 for (const int i : ik_chain.segments.index_range()) {
884 if (i == 0) {
885 head = initial_location;
886 origin = ik_chain.segments[i].orig;
887 }
888 else {
889 head = ik_chain.segments[i - 1].orig;
890 origin = ik_chain.segments[i].orig;
891 }
892 ik_chain.segments[i].orig = origin;
893 ik_chain.segments[i].initial_orig = origin;
894 ik_chain.segments[i].head = head;
895 ik_chain.segments[i].initial_head = head;
896 ik_chain.segments[i].len = math::distance(head, origin);
897 ik_chain.segments[i].scale = float3(1.0f);
898 }
899}
900
902{
903 /* Scaling multiple segments at the same time is not supported as the IK solver can't handle
904 * changes in the segment's length. It will also required a better weight distribution to avoid
905 * artifacts in the areas affected by multiple segments. */
906 if (ELEM(brush.pose_deform_type,
909 {
910 return 1;
911 }
912 return brush.pose_ik_segments;
913}
914
915static std::unique_ptr<IKChain> ik_chain_init_topology(const Depsgraph &depsgraph,
916 Object &object,
917 SculptSession &ss,
918 const Brush &brush,
919 const float3 &initial_location,
920 const float radius)
921{
922
923 const float chain_segment_len = radius * (1.0f + brush.pose_offset);
924
925 const int totvert = vertex_count_get(object);
927
928 int nearest_vertex_index = -1;
929 switch (pbvh.type()) {
931 const Mesh &mesh = *id_cast<const Mesh *>(object.data);
932 const Span<float3> vert_positions = bke::pbvh::vert_positions_eval(depsgraph, object);
933 const bke::AttributeAccessor attributes = mesh.attributes();
934 VArraySpan<bool> hide_vert = *attributes.lookup<bool>(".hide_vert", bke::AttrDomain::Point);
935 std::optional<int> nearest = nearest_vert_calc_mesh(pbvh,
936 vert_positions,
937 hide_vert,
938 initial_location,
939 std::numeric_limits<float>::max(),
940 true);
941 if (nearest) {
942 nearest_vertex_index = *nearest;
943 }
944 break;
945 }
947 const SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
948 const std::optional<SubdivCCGCoord> nearest = nearest_vert_calc_grids(
949 pbvh, subdiv_ccg, initial_location, std::numeric_limits<float>::max(), true);
950 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
951 if (nearest) {
952 nearest_vertex_index = nearest->to_index(key);
953 }
954 break;
955 }
957 const std::optional<BMVert *> nearest = nearest_vert_calc_bmesh(
958 pbvh, initial_location, std::numeric_limits<float>::max(), false);
959 if (nearest) {
960 nearest_vertex_index = BM_elem_index_get(*nearest);
961 }
962 break;
963 }
964 }
965
966 if (nearest_vertex_index == -1) {
967 return nullptr;
968 }
969
970 /* Init the buffers used to keep track of the changes in the pose factors as more segments are
971 * added to the IK chain. */
972
973 /* This stores the whole pose factors values as they grow through the mesh. */
974 Array<float> pose_factor_grow(totvert, 0.0f);
975
976 /* This stores the previous status of the factors when growing a new iteration. */
977 Array<float> pose_factor_grow_prev(totvert, 0.0f);
978
979 pose_factor_grow[nearest_vertex_index] = 1.0f;
980
981 const int tot_segments = brush_num_effective_segments(brush);
982 std::unique_ptr<IKChain> ik_chain = ik_chain_new(tot_segments, totvert);
983
984 /* Calculate the first segment in the chain using the brush radius and the pose origin offset. */
985 calc_pose_data(depsgraph,
986 object,
987 ss,
988 initial_location,
989 radius,
990 brush.pose_offset,
991 ik_chain->segments[0].orig,
992 pose_factor_grow);
993
994 float3 next_chain_segment_target = ik_chain->segments[0].orig;
995
996 /* Init the weights of this segment and store the status of the pose factors to start calculating
997 * new segment origins. */
998 for (int j = 0; j < totvert; j++) {
999 ik_chain->segments[0].weights[j] = pose_factor_grow[j];
1000 pose_factor_grow_prev[j] = pose_factor_grow[j];
1001 }
1002
1003 /* Calculate the next segments in the chain growing the pose factors. */
1004 for (const int i : ik_chain->segments.index_range().drop_front(1)) {
1005
1006 /* Grow the factors to get the new segment origin. */
1007 grow_pose_factor(depsgraph,
1008 object,
1009 ss,
1010 nullptr,
1011 next_chain_segment_target,
1012 chain_segment_len,
1013 ik_chain->segments[i].orig,
1014 pose_factor_grow);
1015 next_chain_segment_target = ik_chain->segments[i].orig;
1016
1017 /* Create the weights for this segment from the difference between the previous grow factor
1018 * iteration an the current iteration. */
1019 for (int j = 0; j < totvert; j++) {
1020 ik_chain->segments[i].weights[j] = pose_factor_grow[j] - pose_factor_grow_prev[j];
1021 /* Store the current grow factor status for the next iteration. */
1022 pose_factor_grow_prev[j] = pose_factor_grow[j];
1023 }
1024 }
1025
1026 ik_chain_origin_heads_init(*ik_chain, initial_location);
1027
1028 return ik_chain;
1029}
1030
1031static std::unique_ptr<IKChain> ik_chain_init_face_sets_mesh(const Depsgraph &depsgraph,
1032 Object &object,
1033 SculptSession &ss,
1034 const Brush &brush,
1035 const float radius)
1036{
1037 struct SegmentData {
1038 int vert;
1039 int face_set;
1040 };
1041
1042 Mesh &mesh = *id_cast<Mesh *>(object.data);
1043 Span<float3> vert_positions = bke::pbvh::vert_positions_eval(depsgraph, object);
1044 const OffsetIndices faces = mesh.faces();
1045 const Span<int> corner_verts = mesh.corner_verts();
1046 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
1047 const bke::AttributeAccessor attributes = mesh.attributes();
1048 const VArraySpan<bool> hide_vert = *attributes.lookup<bool>(".hide_vert",
1050 const VArraySpan<bool> hide_poly = *attributes.lookup<bool>(".hide_poly", bke::AttrDomain::Face);
1051 const VArraySpan face_sets = *attributes.lookup_or_default<int>(
1052 ".sculpt_face_set", bke::AttrDomain::Face, 0);
1053
1054 std::unique_ptr<IKChain> ik_chain = ik_chain_new(brush_num_effective_segments(brush),
1055 vert_positions.size());
1056
1057 /* Each vertex can only be assigned to one face set. */
1058 BitVector<> is_weighted(vert_positions.size());
1059 Set<int> visited_face_sets;
1060
1061 SegmentData current_data = {std::get<int>(ss.active_vert()), face_set_none_id};
1062
1063 const int symm = mesh_symmetry_xyz_get(object);
1064 Vector<int> neighbors;
1065 int num_valid_segments = 0;
1066 for (const int i : ik_chain->segments.index_range()) {
1067 num_valid_segments++;
1068 const bool is_first_iteration = i == 0;
1069
1070 flood_fill::FillDataMesh flood_fill(vert_positions.size(),
1072 flood_fill.add_initial(find_symm_verts_mesh(depsgraph, object, current_data.vert, radius));
1073
1074 visited_face_sets.add(current_data.face_set);
1075
1076 MutableSpan<float> pose_factor = ik_chain->segments[i].weights;
1077 std::optional<SegmentData> next_segment_data;
1078
1079 float3 face_set_boundary_accum(0);
1080 int face_set_boundary_count = 0;
1081
1082 float3 fallback_accum(0);
1083 int fallback_count = 0;
1084
1085 const float3 &pose_initial_co = vert_positions[current_data.vert];
1086 flood_fill.execute(object, vert_to_face_map, [&](int /*from_v*/, int to_v) {
1087 const float3 &to_v_position = vert_positions[to_v];
1088 const bool symmetry_check = check_vertex_pivot_symmetry(
1089 to_v_position, pose_initial_co, symm);
1090
1091 /* First iteration. Continue expanding using topology until a vertex is outside the brush
1092 * radius to determine the first face set. */
1093 if (current_data.face_set == face_set_none_id) {
1094
1095 pose_factor[to_v] = 1.0f;
1096 is_weighted[to_v].set();
1097
1098 if (vert_inside_brush_radius(to_v_position, pose_initial_co, radius, symm)) {
1099 const int visited_face_set = face_set::vert_face_set_max_get(
1100 vert_to_face_map, face_sets, to_v);
1101 visited_face_sets.add(visited_face_set);
1102 }
1103 else if (symmetry_check) {
1104 current_data.face_set = face_set::vert_face_set_max_get(
1105 vert_to_face_map, face_sets, to_v);
1106 visited_face_sets.add(current_data.face_set);
1107 }
1108 return true;
1109 }
1110
1111 /* We already have a current face set, so we can start checking the face sets of the
1112 * vertices. */
1113 /* In the first iteration we need to check all face sets we already visited as the flood
1114 * fill may still not be finished in some of them. */
1115 bool is_vertex_valid = false;
1116 if (is_first_iteration) {
1117 for (const int visited_face_set : visited_face_sets) {
1118 is_vertex_valid |= face_set::vert_has_face_set(
1119 vert_to_face_map, face_sets, to_v, visited_face_set);
1120 }
1121 }
1122 else {
1123 is_vertex_valid = face_set::vert_has_face_set(
1124 vert_to_face_map, face_sets, to_v, current_data.face_set);
1125 }
1126
1127 if (!is_vertex_valid) {
1128 return false;
1129 }
1130
1131 bool visit_next = false;
1132 if (!is_weighted[to_v]) {
1133 pose_factor[to_v] = 1.0f;
1134 is_weighted[to_v].set();
1135 visit_next = true;
1136 }
1137
1138 /* Fallback origin accumulation. */
1139 if (symmetry_check) {
1140 fallback_accum += to_v_position;
1141 fallback_count++;
1142 }
1143
1144 if (!symmetry_check || face_set::vert_has_unique_face_set(vert_to_face_map, face_sets, to_v))
1145 {
1146 return visit_next;
1147 }
1148
1149 /* We only add coordinates for calculating the origin when it is possible to go from this
1150 * vertex to another vertex in a valid face set for the next iteration. */
1151 bool count_as_boundary = false;
1152
1153 for (const int neighbor_idx : vert_neighbors_get_mesh(
1154 faces, corner_verts, vert_to_face_map, hide_poly, to_v, neighbors))
1155 {
1156 const int next_face_set_candidate = face_set::vert_face_set_max_get(
1157 vert_to_face_map, face_sets, neighbor_idx);
1158
1159 /* Check if we can get a valid face set for the next iteration from this neighbor. */
1160 if (face_set::vert_has_unique_face_set(vert_to_face_map, face_sets, neighbor_idx) &&
1161 !visited_face_sets.contains(next_face_set_candidate))
1162 {
1163 if (!next_segment_data) {
1164 next_segment_data = {neighbor_idx, next_face_set_candidate};
1165 }
1166 count_as_boundary = true;
1167 }
1168 }
1169
1170 /* Origin accumulation. */
1171 if (count_as_boundary) {
1172 face_set_boundary_accum += to_v_position;
1173 face_set_boundary_count++;
1174 }
1175 return visit_next;
1176 });
1177
1178 if (face_set_boundary_count > 0) {
1179 ik_chain->segments[i].orig = face_set_boundary_accum / float(face_set_boundary_count);
1180 }
1181 else if (fallback_count > 0) {
1182 ik_chain->segments[i].orig = fallback_accum / float(fallback_count);
1183 }
1184 else {
1185 ik_chain->segments[i].orig = float3(0);
1186 }
1187
1188 if (!next_segment_data) {
1189 /* It is possible that when traversing neighbors that we no longer have any vertices that
1190 * have not been assigned to a face set when trying to find the next segments starting
1191 * point. All further segments are invalid in this case. */
1192 break;
1193 }
1194
1195 current_data = *next_segment_data;
1196 }
1197
1198 ik_chain->segments.resize(num_valid_segments);
1199 ik_chain_origin_heads_init(*ik_chain, vert_positions[std::get<int>(ss.active_vert())]);
1200
1201 return ik_chain;
1202}
1203
1204static std::unique_ptr<IKChain> ik_chain_init_face_sets_grids(Object &object,
1205 SculptSession &ss,
1206 const Brush &brush,
1207 const float radius)
1208{
1209 struct SegmentData {
1210 int vert;
1211 int face_set;
1212 };
1213
1214 const Mesh &mesh = *id_cast<const Mesh *>(object.data);
1215 const OffsetIndices<int> faces = mesh.faces();
1216 const Span<int> corner_verts = mesh.corner_verts();
1217 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
1218 const bke::AttributeAccessor attributes = mesh.attributes();
1219 const VArraySpan face_sets = *attributes.lookup_or_default<int>(
1220 ".sculpt_face_set", bke::AttrDomain::Face, 0);
1221
1222 const SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
1223 const Span<float3> positions = subdiv_ccg.positions;
1224 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
1225 const int grids_num = ss.subdiv_ccg->grids_num * key.grid_area;
1226
1227 std::unique_ptr<IKChain> ik_chain = ik_chain_new(brush_num_effective_segments(brush), grids_num);
1228
1229 /* Each vertex can only be assigned to one face set. */
1230 BitVector<> is_weighted(grids_num);
1231 Set<int> visited_face_sets;
1232
1233 SegmentData current_data = {ss.active_vert_index(), face_set_none_id};
1234
1235 const int symm = mesh_symmetry_xyz_get(object);
1236 SubdivCCGNeighbors neighbors;
1237 int num_valid_segments = 0;
1238 for (const int i : ik_chain->segments.index_range()) {
1239 num_valid_segments++;
1240 const bool is_first_iteration = i == 0;
1241
1243 flood_fill.add_initial(key, find_symm_verts_grids(object, current_data.vert, radius));
1244
1245 visited_face_sets.add(current_data.face_set);
1246
1247 MutableSpan<float> pose_factor = ik_chain->segments[i].weights;
1248 std::optional<SegmentData> next_segment_data;
1249
1250 float3 face_set_boundary_accum(0);
1251 int face_set_boundary_count = 0;
1252
1253 float3 fallback_accum(0);
1254 int fallback_count = 0;
1255
1256 const float3 &pose_initial_co = positions[current_data.vert];
1257 flood_fill.execute(
1258 object,
1259 subdiv_ccg,
1260 [&](SubdivCCGCoord /*from_v*/, SubdivCCGCoord to_v, bool is_duplicate) {
1261 const int to_v_i = to_v.to_index(key);
1262
1263 const float3 to_v_position = positions[to_v_i];
1264 const bool symmetry_check = check_vertex_pivot_symmetry(
1265 to_v_position, pose_initial_co, symm) &&
1266 !is_duplicate;
1267
1268 /* First iteration. Continue expanding using topology until a vertex is outside the brush
1269 * radius to determine the first face set. */
1270 if (current_data.face_set == face_set_none_id) {
1271
1272 pose_factor[to_v_i] = 1.0f;
1273 is_weighted[to_v_i].set();
1274
1275 if (vert_inside_brush_radius(to_v_position, pose_initial_co, radius, symm)) {
1276 const int visited_face_set = face_set::vert_face_set_get(
1277 subdiv_ccg, face_sets, to_v.grid_index);
1278 visited_face_sets.add(visited_face_set);
1279 }
1280 else if (symmetry_check) {
1281 current_data.face_set = face_set::vert_face_set_get(
1282 subdiv_ccg, face_sets, to_v.grid_index);
1283 visited_face_sets.add(current_data.face_set);
1284 }
1285 return true;
1286 }
1287
1288 /* We already have a current face set, so we can start checking the face sets of the
1289 * vertices. */
1290 /* In the first iteration we need to check all face sets we already visited as the flood
1291 * fill may still not be finished in some of them. */
1292 bool is_vertex_valid = false;
1293 if (is_first_iteration) {
1294 for (const int visited_face_set : visited_face_sets) {
1295 is_vertex_valid |= face_set::vert_has_face_set(
1296 subdiv_ccg, face_sets, to_v.grid_index, visited_face_set);
1297 }
1298 }
1299 else {
1300 is_vertex_valid = face_set::vert_has_face_set(
1301 subdiv_ccg, face_sets, to_v.grid_index, current_data.face_set);
1302 }
1303
1304 if (!is_vertex_valid) {
1305 return false;
1306 }
1307
1308 bool visit_next = false;
1309 if (!is_weighted[to_v_i]) {
1310 pose_factor[to_v_i] = 1.0f;
1311 is_weighted[to_v_i].set();
1312 visit_next = true;
1313 }
1314
1315 /* Fallback origin accumulation. */
1316 if (symmetry_check) {
1317 fallback_accum += to_v_position;
1318 fallback_count++;
1319 }
1320
1321 if (!symmetry_check ||
1323 faces, corner_verts, vert_to_face_map, face_sets, subdiv_ccg, to_v))
1324 {
1325 return visit_next;
1326 }
1327
1328 /* We only add coordinates for calculating the origin when it is possible to go from this
1329 * vertex to another vertex in a valid face set for the next iteration. */
1330 bool count_as_boundary = false;
1331
1332 BKE_subdiv_ccg_neighbor_coords_get(subdiv_ccg, to_v, false, neighbors);
1333 for (const SubdivCCGCoord neighbor : neighbors.coords) {
1334 const int next_face_set_candidate = face_set::vert_face_set_get(
1335 subdiv_ccg, face_sets, neighbor.grid_index);
1336
1337 /* Check if we can get a valid face set for the next iteration from this neighbor. */
1339 faces, corner_verts, vert_to_face_map, face_sets, subdiv_ccg, neighbor) &&
1340 !visited_face_sets.contains(next_face_set_candidate))
1341 {
1342 if (!next_segment_data) {
1343 next_segment_data = {neighbor.to_index(key), next_face_set_candidate};
1344 }
1345 count_as_boundary = true;
1346 }
1347 }
1348
1349 /* Origin accumulation. */
1350 if (count_as_boundary) {
1351 face_set_boundary_accum += to_v_position;
1352 face_set_boundary_count++;
1353 }
1354 return visit_next;
1355 });
1356
1357 if (face_set_boundary_count > 0) {
1358 ik_chain->segments[i].orig = face_set_boundary_accum / float(face_set_boundary_count);
1359 }
1360 else if (fallback_count > 0) {
1361 ik_chain->segments[i].orig = fallback_accum / float(fallback_count);
1362 }
1363 else {
1364 ik_chain->segments[i].orig = float3(0);
1365 }
1366
1367 if (!next_segment_data) {
1368 /* It is possible that when traversing neighbors that we no longer have any vertices that
1369 * have not been assigned to a face set when trying to find the next segment's starting
1370 * point. All further segments are invalid in this case. */
1371 break;
1372 }
1373
1374 current_data = *next_segment_data;
1375 }
1376
1377 ik_chain->segments.resize(num_valid_segments);
1378 ik_chain_origin_heads_init(*ik_chain, positions[ss.active_vert_index()]);
1379
1380 return ik_chain;
1381}
1382
1383static std::unique_ptr<IKChain> ik_chain_init_face_sets_bmesh(Object &object,
1384 SculptSession &ss,
1385 const Brush &brush,
1386 const float radius)
1387{
1388 struct SegmentData {
1389 BMVert *vert;
1390 int face_set;
1391 };
1392
1393 const int verts_num = BM_mesh_elem_count(ss.bm, BM_VERT);
1394 const int face_set_offset = CustomData_get_offset_named(
1395 &ss.bm->pdata, CD_PROP_INT32, ".sculpt_face_set");
1396 std::unique_ptr<IKChain> ik_chain = ik_chain_new(brush_num_effective_segments(brush), verts_num);
1397
1398 /* Each vertex can only be assigned to one face set. */
1399 BitVector<> is_weighted(verts_num);
1400 Set<int> visited_face_sets;
1401
1402 SegmentData current_data = {std::get<BMVert *>(ss.active_vert()), face_set_none_id};
1403
1404 const int symm = mesh_symmetry_xyz_get(object);
1405 BMeshNeighborVerts neighbors;
1406 int num_valid_segments = 0;
1407 for (const int i : ik_chain->segments.index_range()) {
1408 num_valid_segments++;
1409 const bool is_first_iteration = i == 0;
1410
1412 flood_fill.add_initial(
1413 *ss.bm, find_symm_verts_bmesh(object, BM_elem_index_get(current_data.vert), radius));
1414
1415 visited_face_sets.add(current_data.face_set);
1416
1417 MutableSpan<float> pose_factor = ik_chain->segments[i].weights;
1418 std::optional<SegmentData> next_segment_data;
1419
1420 float3 face_set_boundary_accum(0);
1421 int face_set_boundary_count = 0;
1422
1423 float3 fallback_accum(0);
1424 int fallback_count = 0;
1425
1426 const float3 pose_initial_co = current_data.vert->co;
1427 flood_fill.execute(object, [&](BMVert * /*from_v*/, BMVert *to_v) {
1428 const int to_v_i = BM_elem_index_get(to_v);
1429
1430 const float3 to_v_position = to_v->co;
1431 const bool symmetry_check = check_vertex_pivot_symmetry(
1432 to_v_position, pose_initial_co, symm);
1433
1434 /* First iteration. Continue expanding using topology until a vertex is outside the brush
1435 * radius to determine the first face set. */
1436 if (current_data.face_set == face_set_none_id) {
1437
1438 pose_factor[to_v_i] = 1.0f;
1439 is_weighted[to_v_i].set();
1440
1441 if (vert_inside_brush_radius(to_v_position, pose_initial_co, radius, symm)) {
1442 const int visited_face_set = face_set::vert_face_set_max_get(face_set_offset, *to_v);
1443 visited_face_sets.add(visited_face_set);
1444 }
1445 else if (symmetry_check) {
1446 current_data.face_set = face_set::vert_face_set_max_get(face_set_offset, *to_v);
1447 visited_face_sets.add(current_data.face_set);
1448 }
1449 return true;
1450 }
1451
1452 /* We already have a current face set, so we can start checking the face sets of the
1453 * vertices. */
1454 /* In the first iteration we need to check all face sets we already visited as the flood
1455 * fill may still not be finished in some of them. */
1456 bool is_vertex_valid = false;
1457 if (is_first_iteration) {
1458 for (const int visited_face_set : visited_face_sets) {
1459 is_vertex_valid |= face_set::vert_has_face_set(face_set_offset, *to_v, visited_face_set);
1460 }
1461 }
1462 else {
1463 is_vertex_valid = face_set::vert_has_face_set(
1464 face_set_offset, *to_v, current_data.face_set);
1465 }
1466
1467 if (!is_vertex_valid) {
1468 return false;
1469 }
1470
1471 bool visit_next = false;
1472 if (!is_weighted[to_v_i]) {
1473 pose_factor[to_v_i] = 1.0f;
1474 is_weighted[to_v_i].set();
1475 visit_next = true;
1476 }
1477
1478 /* Fallback origin accumulation. */
1479 if (symmetry_check) {
1480 fallback_accum += to_v_position;
1481 fallback_count++;
1482 }
1483
1484 if (!symmetry_check || face_set::vert_has_unique_face_set(face_set_offset, *to_v)) {
1485 return visit_next;
1486 }
1487
1488 /* We only add coordinates for calculating the origin when it is possible to go from this
1489 * vertex to another vertex in a valid face set for the next iteration. */
1490 bool count_as_boundary = false;
1491
1492 for (BMVert *neighbor : vert_neighbors_get_bmesh(*to_v, neighbors)) {
1493 const int next_face_set_candidate = face_set::vert_face_set_max_get(face_set_offset,
1494 *neighbor);
1495
1496 /* Check if we can get a valid face set for the next iteration from this neighbor. */
1497 if (face_set::vert_has_unique_face_set(face_set_offset, *neighbor) &&
1498 !visited_face_sets.contains(next_face_set_candidate))
1499 {
1500 if (!next_segment_data) {
1501 next_segment_data = {neighbor, next_face_set_candidate};
1502 }
1503 count_as_boundary = true;
1504 }
1505 }
1506
1507 /* Origin accumulation. */
1508 if (count_as_boundary) {
1509 face_set_boundary_accum += to_v_position;
1510 face_set_boundary_count++;
1511 }
1512 return visit_next;
1513 });
1514
1515 if (face_set_boundary_count > 0) {
1516 ik_chain->segments[i].orig = face_set_boundary_accum / float(face_set_boundary_count);
1517 }
1518 else if (fallback_count > 0) {
1519 ik_chain->segments[i].orig = fallback_accum / float(fallback_count);
1520 }
1521 else {
1522 ik_chain->segments[i].orig = float3(0);
1523 }
1524
1525 if (!next_segment_data) {
1526 /* It is possible that when traversing neighbors that we no longer have any vertices that
1527 * have not been assigned to a face set when trying to find the next segment's starting
1528 * point. All further segments are invalid in this case. */
1529 break;
1530 }
1531
1532 current_data = *next_segment_data;
1533 }
1534
1535 ik_chain->segments.resize(num_valid_segments);
1536 ik_chain_origin_heads_init(*ik_chain, std::get<BMVert *>(ss.active_vert())->co);
1537
1538 return ik_chain;
1539}
1540
1541static std::unique_ptr<IKChain> ik_chain_init_face_sets(const Depsgraph &depsgraph,
1542 Object &object,
1543 SculptSession &ss,
1544 const Brush &brush,
1545 const float radius)
1546{
1548 switch (bke::object::pbvh_get(object)->type()) {
1550 return ik_chain_init_face_sets_mesh(depsgraph, object, ss, brush, radius);
1552 return ik_chain_init_face_sets_grids(object, ss, brush, radius);
1554 return ik_chain_init_face_sets_bmesh(object, ss, brush, radius);
1555 }
1556
1558 return nullptr;
1559}
1560
1561static std::optional<float3> calc_average_face_set_center(const Depsgraph &depsgraph,
1562 Object &object,
1563 const Span<int> floodfill_step,
1564 const int active_face_set,
1565 const int target_face_set)
1566{
1568 int count = 0;
1569 float3 sum(0.0f);
1570
1571 switch (bke::object::pbvh_get(object)->type()) {
1572 case bke::pbvh::Type::Mesh: {
1573 const Mesh &mesh = *id_cast<Mesh *>(object.data);
1574 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
1575 const Span<float3> vert_positions = bke::pbvh::vert_positions_eval(depsgraph, object);
1576 const bke::AttributeAccessor attributes = mesh.attributes();
1577 const VArraySpan face_sets = *attributes.lookup_or_default<int>(
1578 ".sculpt_face_set", bke::AttrDomain::Face, 0);
1579
1580 for (const int vert : vert_positions.index_range()) {
1581 if (floodfill_step[vert] != 0 &&
1582 face_set::vert_has_face_set(vert_to_face_map, face_sets, vert, active_face_set) &&
1583 face_set::vert_has_face_set(vert_to_face_map, face_sets, vert, target_face_set))
1584 {
1585 sum += vert_positions[vert];
1586 count++;
1587 }
1588 }
1589 break;
1590 }
1592 const SubdivCCG &subdiv_ccg = *object.runtime->sculpt_session->subdiv_ccg;
1593 const Span<float3> positions = subdiv_ccg.positions;
1594
1595 const Mesh &mesh = *id_cast<Mesh *>(object.data);
1596 const bke::AttributeAccessor attributes = mesh.attributes();
1597 const VArraySpan face_sets = *attributes.lookup_or_default<int>(
1598 ".sculpt_face_set", bke::AttrDomain::Face, 0);
1599
1600 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
1601 for (const int grid : IndexRange(subdiv_ccg.grids_num)) {
1602 for (const int index : bke::ccg::grid_range(key, grid)) {
1603 if (floodfill_step[index] != 0 &&
1604 face_set::vert_has_face_set(subdiv_ccg, face_sets, grid, active_face_set) &&
1605 face_set::vert_has_face_set(subdiv_ccg, face_sets, grid, target_face_set))
1606 {
1607 sum += positions[index];
1608 count++;
1609 }
1610 }
1611 }
1612 break;
1613 }
1616 BMesh &bm = *object.runtime->sculpt_session->bm;
1617 const int face_set_offset = CustomData_get_offset_named(
1618 &bm.pdata, CD_PROP_INT32, ".sculpt_face_set");
1619 for (const int vert : IndexRange(BM_mesh_elem_count(&bm, BM_VERT))) {
1620 const BMVert *bm_vert = BM_vert_at_index(&bm, vert);
1621 if (floodfill_step[vert] != 0 &&
1622 face_set::vert_has_face_set(face_set_offset, *bm_vert, active_face_set) &&
1623 face_set::vert_has_face_set(face_set_offset, *bm_vert, target_face_set))
1624 {
1625 sum += bm_vert->co;
1626 count++;
1627 }
1628 }
1629
1630 break;
1631 }
1632 }
1633
1634 if (count != 0) {
1635 return sum / float(count);
1636 }
1637
1638 return std::nullopt;
1639}
1640
1641static std::unique_ptr<IKChain> ik_chain_init_face_sets_fk_mesh(const Depsgraph &depsgraph,
1642 Object &object,
1643 SculptSession &ss,
1644 const float radius,
1645 const float3 &initial_location)
1646{
1647 const Mesh &mesh = *id_cast<Mesh *>(object.data);
1648 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
1649 const bke::AttributeAccessor attributes = mesh.attributes();
1650 const VArraySpan face_sets = *attributes.lookup_or_default<int>(
1651 ".sculpt_face_set", bke::AttrDomain::Face, 0);
1652
1653 std::unique_ptr<IKChain> ik_chain = ik_chain_new(1, mesh.verts_num);
1654
1655 const int active_vert = std::get<int>(ss.active_vert());
1656
1657 const int active_face_set = face_set::active_face_set_get(object);
1658
1659 Set<int> visited_face_sets;
1660 Array<int> floodfill_step(mesh.verts_num);
1661 floodfill_step[active_vert] = 1;
1662
1663 int masked_face_set = face_set_none_id;
1664 int target_face_set = face_set_none_id;
1665 int masked_face_set_it = 0;
1667 step_floodfill.add_initial(active_vert);
1668 step_floodfill.execute(object, vert_to_face_map, [&](int from_v, int to_v) {
1669 floodfill_step[to_v] = floodfill_step[from_v] + 1;
1670
1671 const int to_face_set = face_set::vert_face_set_max_get(vert_to_face_map, face_sets, to_v);
1672 if (!visited_face_sets.contains(to_face_set)) {
1673 if (face_set::vert_has_unique_face_set(vert_to_face_map, face_sets, to_v) &&
1674 !face_set::vert_has_unique_face_set(vert_to_face_map, face_sets, from_v) &&
1675 face_set::vert_has_face_set(vert_to_face_map, face_sets, from_v, to_face_set))
1676 {
1677
1678 visited_face_sets.add(to_face_set);
1679
1680 if (floodfill_step[to_v] >= masked_face_set_it) {
1681 masked_face_set = to_face_set;
1682 masked_face_set_it = floodfill_step[to_v];
1683 }
1684
1685 if (target_face_set == face_set_none_id) {
1686 target_face_set = to_face_set;
1687 }
1688 }
1689 }
1690
1691 return face_set::vert_has_face_set(vert_to_face_map, face_sets, to_v, active_face_set);
1692 });
1693
1694 const std::optional<float3> origin = calc_average_face_set_center(
1695 depsgraph, object, floodfill_step, active_face_set, masked_face_set);
1696 ik_chain->segments[0].orig = origin.value_or(float3(0));
1697
1698 std::optional<float3> head = std::nullopt;
1699 if (target_face_set != masked_face_set) {
1701 depsgraph, object, floodfill_step, active_face_set, target_face_set);
1702 }
1703
1704 ik_chain->segments[0].head = head.value_or(initial_location);
1705 ik_chain->grab_delta_offset = ik_chain->segments[0].head - initial_location;
1706
1707 flood_fill::FillDataMesh weight_floodfill(mesh.verts_num, ss.fake_neighbors.fake_neighbor_index);
1708 weight_floodfill.add_initial(find_symm_verts_mesh(depsgraph, object, active_vert, radius));
1709 MutableSpan<float> fk_weights = ik_chain->segments[0].weights;
1710 weight_floodfill.execute(object, vert_to_face_map, [&](int /*from_v*/, int to_v) {
1711 fk_weights[to_v] = 1.0f;
1712 return !face_set::vert_has_face_set(vert_to_face_map, face_sets, to_v, masked_face_set);
1713 });
1714
1715 ik_chain_origin_heads_init(*ik_chain, ik_chain->segments[0].head);
1716 return ik_chain;
1717}
1718
1719static std::unique_ptr<IKChain> ik_chain_init_face_sets_fk_grids(const Depsgraph &depsgraph,
1720 Object &object,
1721 SculptSession &ss,
1722 const float radius,
1723 const float3 &initial_location)
1724{
1725 const Mesh &mesh = *id_cast<const Mesh *>(object.data);
1726 const OffsetIndices<int> faces = mesh.faces();
1727 const Span<int> corner_verts = mesh.corner_verts();
1728 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
1729 const bke::AttributeAccessor attributes = mesh.attributes();
1730 const VArraySpan face_sets = *attributes.lookup_or_default<int>(
1731 ".sculpt_face_set", bke::AttrDomain::Face, 0);
1732
1733 const SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
1734 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
1735 const Span<int> grid_to_face_map = subdiv_ccg.grid_to_face_map;
1736 const int grids_num = ss.subdiv_ccg->grids_num * key.grid_area;
1737
1738 std::unique_ptr<IKChain> ik_chain = ik_chain_new(1, grids_num);
1739
1740 const int active_vert_index = ss.active_vert_index();
1741
1742 const int active_face_set = face_set::active_face_set_get(object);
1743
1744 Set<int> visited_face_sets;
1745 Array<int> floodfill_step(grids_num);
1746 floodfill_step[active_vert_index] = 1;
1747
1748 int masked_face_set = face_set_none_id;
1749 int target_face_set = face_set_none_id;
1750 int masked_face_set_it = 0;
1751 flood_fill::FillDataGrids step_floodfill(grids_num, ss.fake_neighbors.fake_neighbor_index);
1752 step_floodfill.add_initial(SubdivCCGCoord::from_index(key, active_vert_index));
1753 step_floodfill.execute(
1754 object, subdiv_ccg, [&](SubdivCCGCoord from_v, SubdivCCGCoord to_v, bool is_duplicate) {
1755 const int from_v_i = from_v.to_index(key);
1756 const int to_v_i = to_v.to_index(key);
1757
1758 if (!is_duplicate) {
1759 floodfill_step[to_v_i] = floodfill_step[from_v_i] + 1;
1760 }
1761 else {
1762 floodfill_step[to_v_i] = floodfill_step[from_v_i];
1763 }
1764
1765 const int to_face_set = face_sets[grid_to_face_map[to_v.grid_index]];
1766 if (!visited_face_sets.contains(to_face_set)) {
1768 faces, corner_verts, vert_to_face_map, face_sets, subdiv_ccg, to_v) &&
1770 faces, corner_verts, vert_to_face_map, face_sets, subdiv_ccg, from_v) &&
1771 face_set::vert_has_face_set(subdiv_ccg, face_sets, from_v.grid_index, to_face_set))
1772 {
1773
1774 visited_face_sets.add(to_face_set);
1775
1776 if (floodfill_step[to_v_i] >= masked_face_set_it) {
1777 masked_face_set = to_face_set;
1778 masked_face_set_it = floodfill_step[to_v_i];
1779 }
1780
1781 if (target_face_set == face_set_none_id) {
1782 target_face_set = to_face_set;
1783 }
1784 }
1785 }
1786
1788 subdiv_ccg, face_sets, to_v.grid_index, active_face_set);
1789 });
1790
1791 const std::optional<float3> origin = calc_average_face_set_center(
1792 depsgraph, object, floodfill_step, active_face_set, masked_face_set);
1793 ik_chain->segments[0].orig = origin.value_or(float3(0));
1794
1795 std::optional<float3> head = std::nullopt;
1796 if (target_face_set != masked_face_set) {
1798 depsgraph, object, floodfill_step, active_face_set, target_face_set);
1799 }
1800
1801 ik_chain->segments[0].head = head.value_or(initial_location);
1802 ik_chain->grab_delta_offset = ik_chain->segments[0].head - initial_location;
1803
1804 flood_fill::FillDataGrids weight_floodfill(grids_num, ss.fake_neighbors.fake_neighbor_index);
1805 weight_floodfill.add_initial(key, find_symm_verts_grids(object, active_vert_index, radius));
1806 MutableSpan<float> fk_weights = ik_chain->segments[0].weights;
1807 weight_floodfill.execute(
1808 object,
1809 subdiv_ccg,
1810 [&](SubdivCCGCoord /*from_v*/, SubdivCCGCoord to_v, bool /*is_duplicate*/) {
1811 int to_v_i = to_v.to_index(key);
1812
1813 fk_weights[to_v_i] = 1.0f;
1815 subdiv_ccg, face_sets, to_v.grid_index, masked_face_set);
1816 });
1817
1818 ik_chain_origin_heads_init(*ik_chain, ik_chain->segments[0].head);
1819 return ik_chain;
1820}
1821
1822static std::unique_ptr<IKChain> ik_chain_init_face_sets_fk_bmesh(const Depsgraph &depsgraph,
1823 Object &object,
1824 SculptSession &ss,
1825 const float radius,
1826 const float3 &initial_location)
1827{
1829
1830 BMesh &bm = *ss.bm;
1831 const int face_set_offset = CustomData_get_offset_named(
1832 &bm.pdata, CD_PROP_INT32, ".sculpt_face_set");
1833 const int verts_num = BM_mesh_elem_count(&bm, BM_VERT);
1834
1835 std::unique_ptr<IKChain> ik_chain = ik_chain_new(1, verts_num);
1836
1837 BMVert *active_vert = std::get<BMVert *>(ss.active_vert());
1838 const int active_vert_index = BM_elem_index_get(active_vert);
1839
1840 const int active_face_set = face_set::active_face_set_get(object);
1841
1842 Set<int> visited_face_sets;
1843 Array<int> floodfill_step(verts_num);
1844 floodfill_step[active_vert_index] = 1;
1845
1846 int masked_face_set = face_set_none_id;
1847 int target_face_set = face_set_none_id;
1848 int masked_face_set_it = 0;
1849 flood_fill::FillDataBMesh step_floodfill(verts_num, ss.fake_neighbors.fake_neighbor_index);
1850 step_floodfill.add_initial(active_vert);
1851 step_floodfill.execute(object, [&](BMVert *from_v, BMVert *to_v) {
1852 const int from_v_i = BM_elem_index_get(from_v);
1853 const int to_v_i = BM_elem_index_get(to_v);
1854
1855 floodfill_step[to_v_i] = floodfill_step[from_v_i] + 1;
1856
1857 const int to_face_set = face_set::vert_face_set_max_get(face_set_offset, *to_v);
1858 if (!visited_face_sets.contains(to_face_set)) {
1859 if (face_set::vert_has_unique_face_set(face_set_offset, *to_v) &&
1860 !face_set::vert_has_unique_face_set(face_set_offset, *from_v) &&
1861 face_set::vert_has_face_set(face_set_offset, *from_v, to_face_set))
1862 {
1863
1864 visited_face_sets.add(to_face_set);
1865
1866 if (floodfill_step[to_v_i] >= masked_face_set_it) {
1867 masked_face_set = to_face_set;
1868 masked_face_set_it = floodfill_step[to_v_i];
1869 }
1870
1871 if (target_face_set == face_set_none_id) {
1872 target_face_set = to_face_set;
1873 }
1874 }
1875 }
1876
1877 return face_set::vert_has_face_set(face_set_offset, *to_v, active_face_set);
1878 });
1879
1880 const std::optional<float3> origin = calc_average_face_set_center(
1881 depsgraph, object, floodfill_step, active_face_set, masked_face_set);
1882 ik_chain->segments[0].orig = origin.value_or(float3(0));
1883
1884 std::optional<float3> head = std::nullopt;
1885 if (target_face_set != masked_face_set) {
1887 depsgraph, object, floodfill_step, active_face_set, target_face_set);
1888 }
1889
1890 ik_chain->segments[0].head = head.value_or(initial_location);
1891 ik_chain->grab_delta_offset = ik_chain->segments[0].head - initial_location;
1892
1893 flood_fill::FillDataBMesh weight_floodfill(verts_num, ss.fake_neighbors.fake_neighbor_index);
1894 weight_floodfill.add_initial(
1895 *ss.bm, find_symm_verts_bmesh(object, BM_elem_index_get(active_vert), radius));
1896 MutableSpan<float> fk_weights = ik_chain->segments[0].weights;
1897 weight_floodfill.execute(object, [&](BMVert * /*from_v*/, BMVert *to_v) {
1898 int to_v_i = BM_elem_index_get(to_v);
1899
1900 fk_weights[to_v_i] = 1.0f;
1901 return !face_set::vert_has_face_set(face_set_offset, *to_v, masked_face_set);
1902 });
1903
1904 ik_chain_origin_heads_init(*ik_chain, ik_chain->segments[0].head);
1905 return ik_chain;
1906}
1907
1908static std::unique_ptr<IKChain> ik_chain_init_face_sets_fk(const Depsgraph &depsgraph,
1909 Object &object,
1910 SculptSession &ss,
1911 const float radius,
1912 const float3 &initial_location)
1913{
1915 switch (bke::object::pbvh_get(object)->type()) {
1917 return ik_chain_init_face_sets_fk_mesh(depsgraph, object, ss, radius, initial_location);
1919 return ik_chain_init_face_sets_fk_grids(depsgraph, object, ss, radius, initial_location);
1921 return ik_chain_init_face_sets_fk_bmesh(depsgraph, object, ss, radius, initial_location);
1922 }
1924 return nullptr;
1925}
1926
1927static std::unique_ptr<IKChain> ik_chain_init(const Depsgraph &depsgraph,
1928 Object &ob,
1929 SculptSession &ss,
1930 const Brush &brush,
1931 const float3 &initial_location,
1932 const float radius)
1933{
1934 std::unique_ptr<IKChain> ik_chain;
1935
1936 const bool use_fake_neighbors = !(brush.flag2 & BRUSH_USE_CONNECTED_ONLY);
1937
1938 if (use_fake_neighbors) {
1939 fake_neighbors_ensure(depsgraph, ob, brush.disconnected_distance_max);
1940 }
1941 else {
1943 }
1944
1945 switch (brush.pose_origin_type) {
1947 ik_chain = ik_chain_init_topology(depsgraph, ob, ss, brush, initial_location, radius);
1948 break;
1950 ik_chain = ik_chain_init_face_sets(depsgraph, ob, ss, brush, radius);
1951 break;
1953 ik_chain = ik_chain_init_face_sets_fk(depsgraph, ob, ss, radius, initial_location);
1954 break;
1955 }
1956
1957 return ik_chain;
1958}
1959
1960static bool pose_brush_init(const Depsgraph &depsgraph,
1961 Object &ob,
1962 SculptSession &ss,
1963 const Brush &brush)
1964{
1965 /* Init the IK chain that is going to be used to deform the vertices. */
1967 depsgraph, ob, ss, brush, ss.cache->location, ss.cache->radius);
1968
1969 if (!ss.cache->pose_ik_chain) {
1970 return false;
1971 }
1972
1973 /* Smooth the weights of each segment for cleaner deformation. */
1974 for (IKChainSegment &segment : ss.cache->pose_ik_chain->segments) {
1975 smooth::blur_geometry_data_array(ob, brush.pose_smooth_iterations, segment.weights);
1976 }
1977
1978 return true;
1979}
1980
1981std::unique_ptr<SculptPoseIKChainPreview> preview_ik_chain_init(const Depsgraph &depsgraph,
1982 Object &ob,
1983 SculptSession &ss,
1984 const Brush &brush,
1985 const float3 &initial_location,
1986 const float radius)
1987{
1988 const std::unique_ptr<IKChain> chain = ik_chain_init(
1989 depsgraph, ob, ss, brush, initial_location, radius);
1990 if (!chain) {
1991 return nullptr;
1992 }
1993
1994 std::unique_ptr<SculptPoseIKChainPreview> preview = std::make_unique<SculptPoseIKChainPreview>();
1995
1996 preview->initial_head_coords.reinitialize(chain->segments.size());
1997 preview->initial_orig_coords.reinitialize(chain->segments.size());
1998 for (const int i : chain->segments.index_range()) {
1999 preview->initial_head_coords[i] = chain->segments[i].initial_head;
2000 preview->initial_orig_coords[i] = chain->segments[i].initial_orig;
2001 }
2002
2003 return preview;
2004}
2005
2007{
2008 IKChain &ik_chain = *ss.cache->pose_ik_chain;
2009 BKE_curvemapping_init(brush.curve_distance_falloff);
2011}
2012
2013/* Calculate a scale factor based on the grab delta. */
2014static float calc_scale_from_grab_delta(SculptSession &ss, const float3 &ik_target)
2015{
2016 IKChain &ik_chain = *ss.cache->pose_ik_chain;
2017 const float3 segment_dir = math::normalize(ik_chain.segments[0].initial_head -
2018 ik_chain.segments[0].initial_orig);
2019 float4 plane;
2020 plane_from_point_normal_v3(plane, ik_chain.segments[0].initial_head, segment_dir);
2021 const float segment_len = ik_chain.segments[0].len;
2022 return segment_len / (segment_len - dist_signed_to_plane_v3(ik_target, plane));
2023}
2024
2026{
2027 IKChain &ik_chain = *ss.cache->pose_ik_chain;
2028
2029 float3 ik_target = ss.cache->location + (ss.cache->grab_delta * ss.cache->bstrength);
2030
2031 /* Solve the IK for the first segment to include rotation as part of scale if enabled. */
2032 if (!(brush.flag2 & BRUSH_POSE_USE_LOCK_ROTATION)) {
2033 solve_ik_chain(ik_chain, ik_target, brush.flag2 & BRUSH_POSE_IK_ANCHORED);
2034 }
2035
2036 float3 scale(calc_scale_from_grab_delta(ss, ik_target));
2037
2038 /* Write the scale into the segments. */
2039 solve_scale_chain(ik_chain, scale);
2040}
2041
2043{
2044 IKChain &ik_chain = *ss.cache->pose_ik_chain;
2045
2046 /* Calculate the maximum roll. 0.02 radians per pixel works fine. */
2047 float roll = (ss.cache->initial_mouse[0] - ss.cache->mouse[0]) * ss.cache->bstrength * 0.02f;
2048 BKE_curvemapping_init(brush.curve_distance_falloff);
2049 solve_roll_chain(ik_chain, brush, roll);
2050}
2051
2053{
2054 IKChain &ik_chain = *ss.cache->pose_ik_chain;
2055
2056 /* Calculate the IK target. */
2057 float3 ik_target = ss.cache->location + (ss.cache->grab_delta * ss.cache->bstrength) +
2058 ik_chain.grab_delta_offset;
2059
2060 /* Solve the IK positions. */
2061 solve_ik_chain(ik_chain, ik_target, brush.flag2 & BRUSH_POSE_IK_ANCHORED);
2062}
2063
2065{
2066 if (ss.cache->toggle_settings.invert) {
2068 }
2069 else {
2071 }
2072}
2073
2075{
2076 if (ss.cache->toggle_settings.invert) {
2078 }
2079 else {
2081 }
2082}
2083
2084static void calc_squash_stretch_deform(SculptSession &ss, const Brush & /*brush*/)
2085{
2086 IKChain &ik_chain = *ss.cache->pose_ik_chain;
2087
2088 float3 ik_target = ss.cache->location + (ss.cache->grab_delta * ss.cache->bstrength);
2089
2090 float3 scale;
2091 scale.z = calc_scale_from_grab_delta(ss, ik_target);
2092 if (math::abs(scale.z) < 1e-5f) {
2093 scale = float3(0.0f);
2094 }
2095 else {
2096 const float signed_scale = math::sqrt(1.0f / math::abs(scale.z)) * math::sign(scale.z);
2097
2098 scale.x = signed_scale;
2099 scale.y = signed_scale;
2100 }
2101
2102 BLI_assert(std::isfinite(scale.x) && std::isfinite(scale.y) && std::isfinite(scale.z));
2103
2104 /* Write the scale into the segments. */
2105 solve_scale_chain(ik_chain, scale);
2106}
2107
2108static void align_pivot_local_space(float r_mat[4][4],
2110 ePaintSymmetryAreas symm_area,
2111 IKChainSegment *segment,
2112 const float3 &grab_location)
2113{
2114 const float3 symm_head = flip_v3_by_symm_area(segment->head, symm, symm_area, grab_location);
2115 const float3 symm_orig = flip_v3_by_symm_area(segment->orig, symm, symm_area, grab_location);
2116
2117 float3 segment_origin_head = math::normalize(symm_head - symm_orig);
2118
2119 copy_v3_v3(r_mat[2], segment_origin_head);
2120 ortho_basis_v3v3_v3(r_mat[0], r_mat[1], r_mat[2]);
2121}
2122
2123void do_pose_brush(const Depsgraph &depsgraph,
2124 const Sculpt &sd,
2125 Object &ob,
2126 const IndexMask &node_mask)
2127{
2133
2134 if (!ss.cache->pose_ik_chain) {
2135 if (!pose_brush_init(depsgraph, ob, ss, brush)) {
2136 return;
2137 }
2138 }
2139
2140 /* The pose brush applies all enabled symmetry axis in a single iteration, so the rest can be
2141 * ignored. */
2142 if (ss.cache->mirror_symmetry_pass != 0) {
2143 return;
2144 }
2145
2146 IKChain &ik_chain = *ss.cache->pose_ik_chain;
2147
2148 switch (brush.pose_deform_type) {
2151 break;
2154 break;
2157 break;
2158 }
2159
2160 /* Flip the segment chain in all symmetry axis and calculate the transform matrices for each
2161 * possible combination. */
2162 /* This can be optimized by skipping the calculation of matrices where the symmetry is not
2163 * enabled. */
2164 for (int symm_it = 0; symm_it < PAINT_SYMM_AREAS; symm_it++) {
2165 for (const int i : ik_chain.segments.index_range()) {
2166 ePaintSymmetryAreas symm_area = ePaintSymmetryAreas(symm_it);
2167
2168 float symm_rot[4];
2169 copy_qt_qt(symm_rot, ik_chain.segments[i].rot);
2170
2171 /* Flip the origins and rotation quats of each segment. */
2172 flip_quat_by_symm_area(symm_rot, symm, symm_area, ss.cache->orig_grab_location);
2173 float3 symm_orig = flip_v3_by_symm_area(
2174 ik_chain.segments[i].orig, symm, symm_area, ss.cache->orig_grab_location);
2175 float3 symm_initial_orig = flip_v3_by_symm_area(
2176 ik_chain.segments[i].initial_orig, symm, symm_area, ss.cache->orig_grab_location);
2177
2178 float pivot_local_space[4][4];
2179 unit_m4(pivot_local_space);
2180
2181 /* Align the segment pivot local space to the Z axis. */
2182 if (brush.pose_deform_type == BRUSH_POSE_DEFORM_SQUASH_STRETCH) {
2183 align_pivot_local_space(pivot_local_space,
2184 symm,
2185 symm_area,
2186 &ik_chain.segments[i],
2188 unit_m4(ik_chain.segments[i].trans_mat[symm_it].ptr());
2189 }
2190 else {
2191 quat_to_mat4(ik_chain.segments[i].trans_mat[symm_it].ptr(), symm_rot);
2192 }
2193
2194 /* Apply segment scale to the transform. */
2195 for (int scale_i = 0; scale_i < 3; scale_i++) {
2196 mul_v3_fl(ik_chain.segments[i].trans_mat[symm_it][scale_i],
2197 ik_chain.segments[i].scale[scale_i]);
2198 }
2199
2200 translate_m4(ik_chain.segments[i].trans_mat[symm_it].ptr(),
2201 symm_orig[0] - symm_initial_orig[0],
2202 symm_orig[1] - symm_initial_orig[1],
2203 symm_orig[2] - symm_initial_orig[2]);
2204
2205 unit_m4(ik_chain.segments[i].pivot_mat[symm_it].ptr());
2207 ik_chain.segments[i].pivot_mat[symm_it].ptr(), symm_orig[0], symm_orig[1], symm_orig[2]);
2208 mul_m4_m4_post(ik_chain.segments[i].pivot_mat[symm_it].ptr(), pivot_local_space);
2209
2210 invert_m4_m4(ik_chain.segments[i].pivot_mat_inv[symm_it].ptr(),
2211 ik_chain.segments[i].pivot_mat[symm_it].ptr());
2212 }
2213 }
2214
2216 switch (pbvh.type()) {
2217 case bke::pbvh::Type::Mesh: {
2218 Mesh &mesh = *id_cast<Mesh *>(ob.data);
2219 const MeshAttributeData attribute_data(mesh);
2221 const PositionDeformData position_data(depsgraph, ob);
2222 node_mask.foreach_index(
2223 [&](const int i) {
2224 calc_mesh(depsgraph, sd, brush, attribute_data, nodes[i], ob, position_data);
2225 bke::pbvh::update_node_bounds_mesh(position_data.eval, nodes[i]);
2226 },
2228 break;
2229 }
2231 SubdivCCG &subdiv_ccg = *ob.runtime->sculpt_session->subdiv_ccg;
2232 MutableSpan<float3> positions = subdiv_ccg.positions;
2234 node_mask.foreach_index(
2235 [&](const int i) {
2236 BrushLocalData &tls = all_tls.local();
2237 calc_grids(depsgraph, sd, brush, nodes[i], ob, tls);
2238 bke::pbvh::update_node_bounds_grids(subdiv_ccg.grid_area, positions, nodes[i]);
2239 },
2241 break;
2242 }
2245 node_mask.foreach_index(
2246 [&](const int i) {
2247 BrushLocalData &tls = all_tls.local();
2248 calc_bmesh(depsgraph, sd, brush, nodes[i], ob, tls);
2250 },
2252 break;
2253 }
2254 }
2255 pbvh.tag_positions_changed(node_mask);
2256 pbvh.flush_bounds_to_parents();
2257}
2258
2259} // namespace blender::ed::sculpt_paint::pose
#define PAINT_SYMM_AREAS
Definition BKE_paint.hh:109
A BVH for high poly meshes.
Object is a sort of wrapper for general info.
#define PRF_scope(category)
#define BLI_assert_unreachable()
#define BLI_NOINLINE
#define ELEM(...)
#define BM_elem_index_get(ele)
btScalar max
Span< T > as_span() const
Definition BLI_array.hh:254
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
void fill(const T &value) const
Definition BLI_array.hh:283
constexpr IndexRange()=default
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr bool is_empty() const
Definition BLI_span.hh:515
constexpr void fill(const T &value) const
Definition BLI_span.hh:523
constexpr Span< T > as_span() const
Definition BLI_span.hh:667
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
constexpr T & last(const int64_t n=0) const
Definition BLI_span.hh:695
int64_t size() const
Definition BLI_set.hh:606
bool contains(const Key &key) const
Definition BLI_set.hh:310
bool add(const Key &key)
Definition BLI_set.hh:248
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:141
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
constexpr bool is_empty() const
Definition BLI_span.hh:264
int64_t size() const
void append(const T &value)
void resize(const int64_t new_size)
void extend(Span< T > array)
Span< T > as_span() const
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
GAttributeReader lookup(const StringRef name) const
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
#define rot(x, k)
#define FLT_MAX
#define position
#define vertex
float3x3 scale(float3x3 mat, float2 scale)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
ccl_device_inline void copy_v3_v3(ccl_private float *r, const float3 val)
#define FAKE_NEIGHBOR_NONE
IndexRange grid_range(const int grid_area, const int grid)
pbvh::Tree * pbvh_get(Object &object)
IndexMask all_leaf_nodes(const Tree &pbvh, IndexMaskMemory &memory)
Definition pbvh.cc:2522
void update_node_bounds_bmesh(BMeshNode &node)
Definition pbvh.cc:1216
void update_node_bounds_mesh(Span< float3 > positions, MeshNode &node)
Definition pbvh.cc:1196
void update_node_bounds_grids(int grid_area, Span< float3 > positions, GridsNode &node)
Definition pbvh.cc:1205
Span< float3 > vert_positions_eval(const Depsgraph &depsgraph, const Object &object_orig)
Definition pbvh.cc:933
void calc_vert_factors(const Depsgraph &depsgraph, const Object &object, const Cache &automasking, const bke::pbvh::MeshNode &node, Span< int > verts, MutableSpan< float > factors)
void calc_grids_factors(const Depsgraph &depsgraph, const Object &object, const Cache &automasking, const bke::pbvh::GridsNode &node, Span< int > grids, MutableSpan< float > factors)
bool vert_has_unique_face_set(const GroupedSpan< int > vert_to_face_map, const Span< int > face_sets, int vert)
Definition sculpt.cc:324
bool vert_has_face_set(const GroupedSpan< int > vert_to_face_map, const Span< int > face_sets, const int vert, const int face_set)
Definition sculpt.cc:268
int vert_face_set_max_get(const GroupedSpan< int > vert_to_face_map, const Span< int > face_sets, const int vert)
Definition sculpt.cc:232
int vert_face_set_get(const SubdivCCG &subdiv_ccg, const Span< int > face_sets, const int grid)
Definition sculpt.cc:243
int active_face_set_get(const Object &object)
Definition sculpt.cc:202
Span< int > node_visible_verts(const bke::pbvh::MeshNode &node, const Span< bool > hide_vert, Vector< int, bke::pbvh::MESH_LEAF_LIMIT > &indices)
void do_pose_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &ob, const IndexMask &node_mask)
static void grow_factors_bmesh(const ePaintSymmetryFlags symm, const float3 &pose_initial_position, const Span< int > fake_neighbors, const Span< float > prev_mask, bke::pbvh::BMeshNode &node, const MutableSpan< float > pose_factor, PoseGrowFactorData &gftd)
static std::unique_ptr< IKChain > ik_chain_init(const Depsgraph &depsgraph, Object &ob, SculptSession &ss, const Brush &brush, const float3 &initial_location, const float radius)
static BLI_NOINLINE void add_fake_neighbors(const Span< int > fake_neighbors, const Span< int > verts, const Span< int > orig_neighbor_data, MutableSpan< int > neighbor_offsets, Vector< int > &neighbor_data_with_fake)
static void solve_roll_chain(IKChain &ik_chain, const Brush &brush, const float roll)
static void calc_squash_stretch_deform(SculptSession &ss, const Brush &)
static std::unique_ptr< IKChain > ik_chain_init_face_sets(const Depsgraph &depsgraph, Object &object, SculptSession &ss, const Brush &brush, const float radius)
static void calc_mesh(const Depsgraph &depsgraph, const Sculpt &sd, const Brush &brush, const MeshAttributeData &attribute_data, const bke::pbvh::MeshNode &node, Object &object, const PositionDeformData &position_data)
static void solve_translate_chain(IKChain &ik_chain, const float delta[3])
static void grow_factors_grids(const ePaintSymmetryFlags symm, const float3 &pose_initial_position, const SubdivCCG &subdiv_ccg, const Span< int > fake_neighbors, const Span< float > prev_mask, const bke::pbvh::GridsNode &node, const MutableSpan< float > pose_factor, PoseGrowFactorData &gftd)
static BLI_NOINLINE void calc_segment_translations(const Span< float3 > positions, const IKChainSegment &segment, const MutableSpan< float3 > translations)
static void calc_rotate_deform(SculptSession &ss, const Brush &brush)
static void calc_grids(const Depsgraph &depsgraph, const Sculpt &sd, const Brush &brush, const bke::pbvh::GridsNode &node, Object &object, BrushLocalData &tls)
static bool vert_inside_brush_radius(const float3 &vertex, const float3 &br_co, float radius, char symm)
static int brush_num_effective_segments(const Brush &brush)
static std::unique_ptr< IKChain > ik_chain_init_face_sets_grids(Object &object, SculptSession &ss, const Brush &brush, const float radius)
static std::unique_ptr< IKChain > ik_chain_init_face_sets_fk_bmesh(const Depsgraph &depsgraph, Object &object, SculptSession &ss, const float radius, const float3 &initial_location)
static std::unique_ptr< IKChain > ik_chain_init_face_sets_fk(const Depsgraph &depsgraph, Object &object, SculptSession &ss, const float radius, const float3 &initial_location)
static float calc_scale_from_grab_delta(SculptSession &ss, const float3 &ik_target)
static void sculpt_pose_do_translate_deform(SculptSession &ss, const Brush &brush)
static void calc_scale_translate_deform(SculptSession &ss, const Brush &brush)
static void ik_chain_origin_heads_init(IKChain &ik_chain, const float3 &initial_location)
static std::unique_ptr< IKChain > ik_chain_init_face_sets_fk_mesh(const Depsgraph &depsgraph, Object &object, SculptSession &ss, const float radius, const float3 &initial_location)
static void calc_pose_origin_and_factor_bmesh(Object &object, SculptSession &ss, const float3 &initial_location, float radius, float3 &r_pose_origin, MutableSpan< float > r_pose_factor)
static bool pose_brush_init(const Depsgraph &depsgraph, Object &ob, SculptSession &ss, const Brush &brush)
static void calc_twist_deform(SculptSession &ss, const Brush &brush)
static void calc_scale_deform(SculptSession &ss, const Brush &brush)
static std::unique_ptr< IKChain > ik_chain_init_face_sets_fk_grids(const Depsgraph &depsgraph, Object &object, SculptSession &ss, const float radius, const float3 &initial_location)
static std::unique_ptr< IKChain > ik_chain_new(const int totsegments, const int totverts)
static void calc_pose_origin_and_factor_mesh(const Depsgraph &depsgraph, Object &object, SculptSession &ss, const float3 &initial_location, float radius, float3 &r_pose_origin, MutableSpan< float > r_pose_factor)
static std::optional< float3 > calc_average_face_set_center(const Depsgraph &depsgraph, Object &object, const Span< int > floodfill_step, const int active_face_set, const int target_face_set)
static std::unique_ptr< IKChain > ik_chain_init_face_sets_bmesh(Object &object, SculptSession &ss, const Brush &brush, const float radius)
static BLI_NOINLINE void add_arrays(const MutableSpan< float3 > a, const Span< float3 > b)
static void calc_rotate_twist_deform(SculptSession &ss, const Brush &brush)
static void solve_scale_chain(IKChain &ik_chain, const float scale[3])
static std::unique_ptr< IKChain > ik_chain_init_face_sets_mesh(const Depsgraph &depsgraph, Object &object, SculptSession &ss, const Brush &brush, const float radius)
static void align_pivot_local_space(float r_mat[4][4], ePaintSymmetryFlags symm, ePaintSymmetryAreas symm_area, IKChainSegment *segment, const float3 &grab_location)
static void grow_factors_mesh(const ePaintSymmetryFlags symm, const float3 &pose_initial_position, const Span< float3 > vert_positions, const OffsetIndices< int > faces, const Span< int > corner_verts, const GroupedSpan< int > vert_to_face_map, const Span< bool > hide_vert, const Span< bool > hide_poly, const Span< int > fake_neighbors, const Span< float > prev_mask, const bke::pbvh::MeshNode &node, GrowFactorLocalData &tls, const MutableSpan< float > pose_factor, PoseGrowFactorData &gftd)
static void solve_ik_chain(IKChain &ik_chain, const float3 &initial_target, const bool use_anchor)
static void grow_pose_factor(const Depsgraph &depsgraph, Object &ob, SculptSession &ss, float pose_origin[3], float pose_target[3], float max_len, float *r_pose_origin, MutableSpan< float > pose_factor)
static std::unique_ptr< IKChain > ik_chain_init_topology(const Depsgraph &depsgraph, Object &object, SculptSession &ss, const Brush &brush, const float3 &initial_location, const float radius)
static void calc_pose_origin_and_factor_grids(Object &object, SculptSession &ss, const float3 &initial_location, float radius, float3 &r_pose_origin, MutableSpan< float > r_pose_factor)
static void calc_bmesh(const Depsgraph &depsgraph, const Sculpt &sd, const Brush &brush, bke::pbvh::BMeshNode &node, Object &object, BrushLocalData &tls)
static void calc_pose_data(const Depsgraph &depsgraph, Object &object, SculptSession &ss, const float3 &initial_location, float radius, float pose_offset, float3 &r_pose_origin, MutableSpan< float > r_pose_factor)
std::unique_ptr< SculptPoseIKChainPreview > preview_ik_chain_init(const Depsgraph &depsgraph, Object &ob, SculptSession &ss, const Brush &brush, const float3 &initial_location, const float radius)
void blur_geometry_data_array(const Object &object, const int iterations, const MutableSpan< float > data)
void fill_factor_from_hide_and_mask(Span< bool > hide_vert, Span< float > mask, Span< int > verts, MutableSpan< float > r_factors)
Definition sculpt.cc:7128
MutableSpan< float3 > gather_grids_positions(const SubdivCCG &subdiv_ccg, const Span< int > grids, Vector< float3 > &positions)
void scatter_data_bmesh(Span< T > node_data, const Set< BMVert *, 0 > &verts, MutableSpan< T > dst)
Definition sculpt.cc:6577
void gather_bmesh_positions(const Set< BMVert *, 0 > &verts, MutableSpan< float3 > positions)
Definition sculpt.cc:6497
bool check_vertex_pivot_symmetry(const float vco[3], const float pco[3], const char symm)
Definition sculpt.cc:672
void gather_data_grids(const SubdivCCG &subdiv_ccg, Span< T > src, Span< int > grids, MutableSpan< T > node_data)
Definition sculpt.cc:6528
void vert_random_access_ensure(Object &object)
Definition sculpt.cc:154
std::optional< int > nearest_vert_calc_mesh(const bke::pbvh::Tree &pbvh, const Span< float3 > vert_positions, const Span< bool > hide_vert, const float3 &location, const float max_distance, const bool use_original)
Definition sculpt.cc:730
Vector< int > find_symm_verts_grids(const Object &object, const int original_vert, const float max_distance)
GroupedSpan< int > calc_vert_neighbors(OffsetIndices< int > faces, Span< int > corner_verts, GroupedSpan< int > vert_to_face, Span< bool > hide_poly, Span< int > verts, Vector< int > &r_offset_data, Vector< int > &r_data)
Definition sculpt.cc:7949
Vector< BMVert *, 64 > BMeshNeighborVerts
int vertex_count_get(const Object &object)
Definition sculpt.cc:163
ePaintSymmetryAreas get_vertex_symm_area(const float co[3])
Definition sculpt.cc:2979
void gather_data_bmesh(Span< T > src, const Set< BMVert *, 0 > &verts, MutableSpan< T > node_data)
Definition sculpt.cc:6545
void reset_translations_to_original(MutableSpan< float3 > translations, Span< float3 > positions, Span< float3 > orig_positions)
Definition sculpt.cc:7572
float3 symmetry_flip(const float3 &src, const ePaintSymmetryFlags symm)
void orig_position_data_gather_bmesh(const BMLog &bm_log, const Set< BMVert *, 0 > &verts, MutableSpan< float3 > positions, MutableSpan< float3 > normals)
void scale_translations(MutableSpan< float3 > translations, Span< float > factors)
Definition sculpt.cc:7837
ePaintSymmetryFlags mesh_symmetry_xyz_get(const Object &object)
Definition sculpt.cc:192
Vector< int > find_symm_verts_mesh(const Depsgraph &depsgraph, const Object &object, const int original_vert, const float max_distance)
void fake_neighbors_free(Object &ob)
Definition sculpt.cc:6249
float3 flip_v3_by_symm_area(const float3 &vector, const ePaintSymmetryFlags symm, const ePaintSymmetryAreas symmarea, const float3 &pivot)
Definition sculpt.cc:3022
void clip_and_lock_translations(const Sculpt &sd, const SculptSession &ss, Span< float3 > positions, Span< int > verts, MutableSpan< float3 > translations)
Definition sculpt.cc:7664
void scatter_data_mesh(Span< T > src, Span< int > indices, MutableSpan< T > dst)
Span< BMVert * > vert_neighbors_get_bmesh(BMVert &vert, BMeshNeighborVerts &r_neighbors)
Definition sculpt.cc:490
void apply_translations(Span< float3 > translations, Span< int > verts, MutableSpan< float3 > positions)
Definition sculpt.cc:7592
OrigPositionData orig_position_data_get_mesh(const Object &object, const bke::pbvh::MeshNode &node)
void gather_data_mesh(Span< T > src, Span< int > indices, MutableSpan< T > dst)
OrigPositionData orig_position_data_get_grids(const Object &object, const bke::pbvh::GridsNode &node)
std::optional< BMVert * > nearest_vert_calc_bmesh(const bke::pbvh::Tree &pbvh, const float3 &location, const float max_distance, const bool use_original)
Definition sculpt.cc:831
Span< int > fake_neighbors_ensure(const Depsgraph &depsgraph, Object &ob, const float max_dist)
Definition sculpt.cc:6230
void flip_quat_by_symm_area(float quat[4], const ePaintSymmetryFlags symm, const ePaintSymmetryAreas symmarea, const float pivot[3])
Definition sculpt.cc:3043
std::optional< SubdivCCGCoord > nearest_vert_calc_grids(const bke::pbvh::Tree &pbvh, const SubdivCCG &subdiv_ccg, const float3 &location, const float max_distance, const bool use_original)
Definition sculpt.cc:777
Span< int > vert_neighbors_get_mesh(const OffsetIndices< int > faces, const Span< int > corner_verts, const GroupedSpan< int > vert_to_face, const Span< bool > hide_poly, const int vert, Vector< int > &r_neighbors)
Definition sculpt.cc:534
void scatter_data_grids(const SubdivCCG &subdiv_ccg, Span< T > node_data, Span< int > grids, MutableSpan< T > dst)
Definition sculpt.cc:6560
Vector< int > find_symm_verts_bmesh(const Object &object, const int original_vert, const float max_distance)
bool is_symmetry_iteration_valid(const char i, const char symm)
constexpr ParallelGrainSize grain_size(int grain_size)
T sqrt(const T &a)
T sign(const T &a)
T distance(const T &a, const T &b)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
T distance_squared(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
T abs(const T &a)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
Value parallel_reduce(IndexRange range, int64_t grain_size, const Value &identity, const Function &function, const Reduction &reduction)
Definition BLI_task.hh:127
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void rotation_between_quats_to_quat(float q[4], const float q1[4], const float q2[4])
MINLINE void copy_v3_v3(float r[3], const float a[3])
void axis_angle_normalized_to_quat(float r[4], const float axis[3], float angle)
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
ePaintSymmetryAreas
Definition BKE_paint.hh:102
float dist_signed_to_plane_v3(const float p[3], const float plane[4])
Definition math_geom.cc:511
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
void rotation_between_vecs_to_quat(float q[4], const float v1[3], const float v2[3])
void translate_m4(float mat[4][4], float Tx, float Ty, float Tz)
@ BRUSH_POSE_IK_ANCHORED
@ BRUSH_USE_CONNECTED_ONLY
@ BRUSH_POSE_USE_LOCK_ROTATION
void plane_from_point_normal_v3(float r_plane[4], const float plane_co[3], const float plane_no[3])
Definition math_geom.cc:225
MINLINE void mul_v3_fl(float r[3], float f)
CCGKey BKE_subdiv_ccg_key_top_level(const SubdivCCG &subdiv_ccg)
@ BRUSH_POSE_DEFORM_SQUASH_STRETCH
@ BRUSH_POSE_DEFORM_SCALE_TRANSLATE
@ BRUSH_POSE_DEFORM_ROTATE_TWIST
VecBase< float, 4 > float4
@ BRUSH_POSE_ORIGIN_TOPOLOGY
@ BRUSH_POSE_ORIGIN_FACE_SETS_FK
@ BRUSH_POSE_ORIGIN_FACE_SETS
void unit_qt(float q[4])
void BKE_subdiv_ccg_neighbor_coords_get(const SubdivCCG &subdiv_ccg, const SubdivCCGCoord &coord, bool include_duplicates, SubdivCCGNeighbors &r_neighbors)
void BKE_curvemapping_init(CurveMapping *cumap)
void copy_qt_qt(float q[4], const float a[4])
BMesh const char void * data
int CustomData_get_offset_named(const CustomData *data, eCustomDataType type, StringRef name)
int BM_mesh_elem_count(BMesh *bm, const char htype)
float BKE_brush_curve_strength(eBrushCurvePreset preset, const CurveMapping *cumap, float distance, float brush_radius)
Definition brush.cc:1691
void unit_m4(float m[4][4])
BLI_INLINE BMVert * BM_vert_at_index(BMesh *bm, const int index)
int CCG_grid_xy_to_index(const int grid_size, const int x, const int y)
Definition BKE_ccg.hh:75
const Brush * BKE_paint_brush_for_read(const Paint *paint)
void mul_m4_m4_post(float R[4][4], const float B[4][4])
@ BRUSH_DEFORM_TARGET_CLOTH_SIM
@ BRUSH_DEFORM_TARGET_GEOMETRY
VecBase< float, 3 > float3
void ortho_basis_v3v3_v3(float r_n1[3], float r_n2[3], const float n[3])
const Set< BMVert *, 0 > & BKE_pbvh_bmesh_node_unique_verts(bke::pbvh::BMeshNode *node)
void quat_to_mat4(float m[4][4], const float q[4])
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
CustomData pdata
bke::ObjectRuntime * runtime
ed::sculpt_paint::StrokeCache * cache
Definition BKE_paint.hh:409
SculptFakeNeighbors fake_neighbors
Definition BKE_paint.hh:459
static SubdivCCGCoord from_index(const CCGKey &key, int index)
int to_index(const CCGKey &key) const
SubdivCCGNeighborCoords coords
Array< float3 > positions
BitGroupVector grid_hidden
std::unique_ptr< pose::IKChain > pose_ik_chain
std::unique_ptr< auto_mask::Cache > automasking
std::unique_ptr< cloth::SimulationData > cloth_sim
void execute(Object &object, FunctionRef< bool(BMVert *from_v, BMVert *to_v)> func)
void execute(Object &object, const SubdivCCG &subdiv_ccg, FunctionRef< bool(SubdivCCGCoord from_v, SubdivCCGCoord to_v, bool is_duplicate)> func)
void execute(Object &object, GroupedSpan< int > vert_to_face_map, FunctionRef< bool(int from_v, int to_v)> func)
static PoseGrowFactorData join(const PoseGrowFactorData &a, const PoseGrowFactorData &b)
void resize(int new_size)
Definition vector.hh:94