Blender  V3.3
sculpt_pose.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2020 Blender Foundation. All rights reserved. */
3 
8 #include "MEM_guardedalloc.h"
9 
10 #include "BLI_blenlib.h"
11 #include "BLI_math.h"
12 #include "BLI_task.h"
13 
14 #include "DNA_brush_types.h"
15 #include "DNA_mesh_types.h"
16 #include "DNA_meshdata_types.h"
17 #include "DNA_object_types.h"
18 
19 #include "BKE_brush.h"
20 #include "BKE_ccg.h"
21 #include "BKE_colortools.h"
22 #include "BKE_context.h"
23 #include "BKE_mesh.h"
24 #include "BKE_multires.h"
25 #include "BKE_node.h"
26 #include "BKE_object.h"
27 #include "BKE_paint.h"
28 #include "BKE_pbvh.h"
29 #include "BKE_scene.h"
30 
31 #include "paint_intern.h"
32 #include "sculpt_intern.h"
33 
34 #include "bmesh.h"
35 
36 #include <math.h>
37 #include <stdlib.h>
38 
39 static void pose_solve_ik_chain(SculptPoseIKChain *ik_chain,
40  const float initial_target[3],
41  const bool use_anchor)
42 {
43  SculptPoseIKChainSegment *segments = ik_chain->segments;
44  int tot_segments = ik_chain->tot_segments;
45 
46  float target[3];
47 
48  /* Set the initial target. */
49  copy_v3_v3(target, initial_target);
50 
51  /* Solve the positions and rotations of all segments in the chain. */
52  for (int i = 0; i < tot_segments; i++) {
53  float initial_orientation[3];
54  float current_orientation[3];
55  float current_head_position[3];
56  float current_origin_position[3];
57 
58  /* Calculate the rotation to orientate the segment to the target from its initial state. */
59  sub_v3_v3v3(current_orientation, target, segments[i].orig);
60  normalize_v3(current_orientation);
61  sub_v3_v3v3(initial_orientation, segments[i].initial_head, segments[i].initial_orig);
62  normalize_v3(initial_orientation);
63  rotation_between_vecs_to_quat(segments[i].rot, initial_orientation, current_orientation);
64 
65  /* Rotate the segment by calculating a new head position. */
66  madd_v3_v3v3fl(current_head_position, segments[i].orig, current_orientation, segments[i].len);
67 
68  /* Move the origin of the segment towards the target. */
69  sub_v3_v3v3(current_origin_position, target, current_head_position);
70 
71  /* Store the new head and origin positions to the segment. */
72  copy_v3_v3(segments[i].head, current_head_position);
73  add_v3_v3(segments[i].orig, current_origin_position);
74 
75  /* Use the origin of this segment as target for the next segment in the chain. */
76  copy_v3_v3(target, segments[i].orig);
77  }
78 
79  /* Move back the whole chain to preserve the anchor point. */
80  if (use_anchor) {
81  float anchor_diff[3];
83  anchor_diff, segments[tot_segments - 1].initial_orig, segments[tot_segments - 1].orig);
84 
85  for (int i = 0; i < tot_segments; i++) {
86  add_v3_v3(segments[i].orig, anchor_diff);
87  add_v3_v3(segments[i].head, anchor_diff);
88  }
89  }
90 }
91 
93  const Brush *brush,
94  const float roll)
95 {
96  SculptPoseIKChainSegment *segments = ik_chain->segments;
97  int tot_segments = ik_chain->tot_segments;
98 
99  for (int i = 0; i < tot_segments; i++) {
100  float initial_orientation[3];
101  float initial_rotation[4];
102  float current_rotation[4];
103 
104  sub_v3_v3v3(initial_orientation, segments[i].initial_head, segments[i].initial_orig);
105  normalize_v3(initial_orientation);
106 
107  /* Calculate the current roll angle using the brush curve. */
108  float current_roll = roll * BKE_brush_curve_strength(brush, i, tot_segments);
109 
110  axis_angle_normalized_to_quat(initial_rotation, initial_orientation, 0.0f);
111  axis_angle_normalized_to_quat(current_rotation, initial_orientation, current_roll);
112 
113  /* Store the difference of the rotations in the segment rotation. */
114  rotation_between_quats_to_quat(segments[i].rot, current_rotation, initial_rotation);
115  }
116 }
117 
118 static void pose_solve_translate_chain(SculptPoseIKChain *ik_chain, const float delta[3])
119 {
120  SculptPoseIKChainSegment *segments = ik_chain->segments;
121  const int tot_segments = ik_chain->tot_segments;
122 
123  for (int i = 0; i < tot_segments; i++) {
124  /* Move the origin and head of each segment by delta. */
125  add_v3_v3v3(segments[i].head, segments[i].initial_head, delta);
126  add_v3_v3v3(segments[i].orig, segments[i].initial_orig, delta);
127 
128  /* Reset the segment rotation. */
129  unit_qt(segments[i].rot);
130  }
131 }
132 
133 static void pose_solve_scale_chain(SculptPoseIKChain *ik_chain, const float scale[3])
134 {
135  SculptPoseIKChainSegment *segments = ik_chain->segments;
136  const int tot_segments = ik_chain->tot_segments;
137 
138  for (int i = 0; i < tot_segments; i++) {
139  /* Assign the scale to each segment. */
140  copy_v3_v3(segments[i].scale, scale);
141  }
142 }
143 
144 static void do_pose_brush_task_cb_ex(void *__restrict userdata,
145  const int n,
146  const TaskParallelTLS *__restrict UNUSED(tls))
147 {
148  SculptThreadedTaskData *data = userdata;
149  SculptSession *ss = data->ob->sculpt;
150  SculptPoseIKChain *ik_chain = ss->cache->pose_ik_chain;
151  SculptPoseIKChainSegment *segments = ik_chain->segments;
152  const Brush *brush = data->brush;
153 
154  PBVHVertexIter vd;
155  float disp[3], new_co[3];
156  float final_pos[3];
157 
158  SculptOrigVertData orig_data;
159  SCULPT_orig_vert_data_init(&orig_data, data->ob, data->nodes[n], SCULPT_UNDO_COORDS);
160 
161  BKE_pbvh_vertex_iter_begin (ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE) {
162  SCULPT_orig_vert_data_update(&orig_data, &vd);
163 
164  float total_disp[3];
165  zero_v3(total_disp);
166 
167  ePaintSymmetryAreas symm_area = SCULPT_get_vertex_symm_area(orig_data.co);
168 
169  /* Calculate the displacement of each vertex for all the segments in the chain. */
170  for (int ik = 0; ik < ik_chain->tot_segments; ik++) {
171  copy_v3_v3(new_co, orig_data.co);
172 
173  /* Get the transform matrix for the vertex symmetry area to calculate a displacement in the
174  * vertex. */
175  mul_m4_v3(segments[ik].pivot_mat_inv[(int)symm_area], new_co);
176  mul_m4_v3(segments[ik].trans_mat[(int)symm_area], new_co);
177  mul_m4_v3(segments[ik].pivot_mat[(int)symm_area], new_co);
178 
179  /* Apply the segment weight of the vertex to the displacement. */
180  sub_v3_v3v3(disp, new_co, orig_data.co);
181  mul_v3_fl(disp, segments[ik].weights[vd.index]);
182 
183  /* Apply the vertex mask to the displacement. */
184  const float mask = vd.mask ? 1.0f - *vd.mask : 1.0f;
185  const float automask = SCULPT_automasking_factor_get(ss->cache->automasking, ss, vd.index);
186  mul_v3_fl(disp, mask * automask);
187 
188  /* Accumulate the displacement. */
189  add_v3_v3(total_disp, disp);
190  }
191 
192  /* Apply the accumulated displacement to the vertex. */
193  add_v3_v3v3(final_pos, orig_data.co, total_disp);
194 
195  float *target_co = SCULPT_brush_deform_target_vertex_co_get(ss, brush->deform_target, &vd);
196  copy_v3_v3(target_co, final_pos);
197 
198  if (vd.mvert) {
200  }
201  }
203 }
204 
205 typedef struct PoseGrowFactorTLSData {
206  float pos_avg[3];
209 
210 static void pose_brush_grow_factor_task_cb_ex(void *__restrict userdata,
211  const int n,
212  const TaskParallelTLS *__restrict tls)
213 {
214  SculptThreadedTaskData *data = userdata;
215  PoseGrowFactorTLSData *gftd = tls->userdata_chunk;
216  SculptSession *ss = data->ob->sculpt;
217  const char symm = SCULPT_mesh_symmetry_xyz_get(data->ob);
218  PBVHVertexIter vd;
219  BKE_pbvh_vertex_iter_begin (ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE) {
221  float max = 0.0f;
222 
223  /* Grow the factor. */
225  float vmask_f = data->prev_mask[ni.index];
226  max = MAX2(vmask_f, max);
227  }
229 
230  /* Keep the count of the vertices that where added to the factors in this grow iteration. */
231  if (max > data->prev_mask[vd.index]) {
232  data->pose_factor[vd.index] = max;
233  if (SCULPT_check_vertex_pivot_symmetry(vd.co, data->pose_initial_co, symm)) {
234  add_v3_v3(gftd->pos_avg, vd.co);
235  gftd->pos_count++;
236  }
237  }
238  }
239 
241 }
242 
243 static void pose_brush_grow_factor_reduce(const void *__restrict UNUSED(userdata),
244  void *__restrict chunk_join,
245  void *__restrict chunk)
246 {
247  PoseGrowFactorTLSData *join = chunk_join;
248  PoseGrowFactorTLSData *gftd = chunk;
249  add_v3_v3(join->pos_avg, gftd->pos_avg);
250  join->pos_count += gftd->pos_count;
251 }
252 
253 /* Grow the factor until its boundary is near to the offset pose origin or outside the target
254  * distance. */
256  Object *ob,
257  SculptSession *ss,
258  float pose_origin[3],
259  float pose_target[3],
260  float max_len,
261  float *r_pose_origin,
262  float *pose_factor)
263 {
264  PBVHNode **nodes;
265  PBVH *pbvh = ob->sculpt->pbvh;
266  int totnode;
267 
268  BKE_pbvh_search_gather(pbvh, NULL, NULL, &nodes, &totnode);
270  .sd = sd,
271  .ob = ob,
272  .nodes = nodes,
273  .totnode = totnode,
274  .pose_factor = pose_factor,
275  };
276 
277  data.pose_initial_co = pose_target;
278  TaskParallelSettings settings;
280  gftd.pos_count = 0;
281  zero_v3(gftd.pos_avg);
282  BKE_pbvh_parallel_range_settings(&settings, true, totnode);
284  settings.userdata_chunk = &gftd;
285  settings.userdata_chunk_size = sizeof(PoseGrowFactorTLSData);
286 
287  bool grow_next_iteration = true;
288  float prev_len = FLT_MAX;
289  data.prev_mask = MEM_mallocN(SCULPT_vertex_count_get(ss) * sizeof(float), "prev mask");
290  while (grow_next_iteration) {
291  zero_v3(gftd.pos_avg);
292  gftd.pos_count = 0;
293  memcpy(data.prev_mask, pose_factor, SCULPT_vertex_count_get(ss) * sizeof(float));
295 
296  if (gftd.pos_count != 0) {
297  mul_v3_fl(gftd.pos_avg, 1.0f / (float)gftd.pos_count);
298  if (pose_origin) {
299  /* Test with pose origin. Used when growing the factors to compensate the Origin Offset. */
300  /* Stop when the factor's avg_pos starts moving away from the origin instead of getting
301  * closer to it. */
302  float len = len_v3v3(gftd.pos_avg, pose_origin);
303  if (len < prev_len) {
304  prev_len = len;
305  grow_next_iteration = true;
306  }
307  else {
308  grow_next_iteration = false;
309  memcpy(pose_factor, data.prev_mask, SCULPT_vertex_count_get(ss) * sizeof(float));
310  }
311  }
312  else {
313  /* Test with length. Used to calculate the origin positions of the IK chain. */
314  /* Stops when the factors have grown enough to generate a new segment origin. */
315  float len = len_v3v3(gftd.pos_avg, pose_target);
316  if (len < max_len) {
317  prev_len = len;
318  grow_next_iteration = true;
319  }
320  else {
321  grow_next_iteration = false;
322  if (r_pose_origin) {
323  copy_v3_v3(r_pose_origin, gftd.pos_avg);
324  }
325  memcpy(pose_factor, data.prev_mask, SCULPT_vertex_count_get(ss) * sizeof(float));
326  }
327  }
328  }
329  else {
330  if (r_pose_origin) {
331  copy_v3_v3(r_pose_origin, pose_target);
332  }
333  grow_next_iteration = false;
334  }
335  }
336  MEM_freeN(data.prev_mask);
337 
338  MEM_SAFE_FREE(nodes);
339 }
340 
341 static bool sculpt_pose_brush_is_vertex_inside_brush_radius(const float vertex[3],
342  const float br_co[3],
343  float radius,
344  char symm)
345 {
346  for (char i = 0; i <= symm; ++i) {
347  if (SCULPT_is_symmetry_iteration_valid(i, symm)) {
348  float location[3];
349  flip_v3_v3(location, br_co, (char)i);
350  if (len_v3v3(location, vertex) < radius) {
351  return true;
352  }
353  }
354  }
355  return false;
356 }
357 
358 typedef struct PoseFloodFillData {
359  float pose_initial_co[3];
360  float radius;
361  int symm;
362 
363  float *pose_factor;
364  float pose_origin[3];
365  int tot_co;
366 
371 
373 
374  /* Store the visited face sets to avoid going back when calculating the chain. */
376 
377  /* In face sets origin mode, each vertex can only be assigned to one face set. */
379 
381 
382  /* In topology mode this stores the furthest point from the stroke origin for cases when a pose
383  * origin based on the brush radius can't be set. */
385 
386  /* Fallback origin. If we can't find any face set to continue, use the position of all vertices
387  * that have the current face set. */
388  float fallback_origin[3];
390 
391  /* Face Set FK mode. */
393  float *fk_weights;
399 
401  SculptSession *ss, int UNUSED(from_v), int to_v, bool is_duplicate, void *userdata)
402 {
403  PoseFloodFillData *data = userdata;
404  const float *co = SCULPT_vertex_co_get(ss, to_v);
405 
406  if (data->pose_factor) {
407  data->pose_factor[to_v] = 1.0f;
408  }
409 
410  if (len_squared_v3v3(data->pose_initial_co, data->fallback_floodfill_origin) <
411  len_squared_v3v3(data->pose_initial_co, co)) {
412  copy_v3_v3(data->fallback_floodfill_origin, co);
413  }
414 
416  co, data->pose_initial_co, data->radius, data->symm)) {
417  return true;
418  }
419  if (SCULPT_check_vertex_pivot_symmetry(co, data->pose_initial_co, data->symm)) {
420  if (!is_duplicate) {
421  add_v3_v3(data->pose_origin, co);
422  data->tot_co++;
423  }
424  }
425 
426  return false;
427 }
428 
430  SculptSession *ss, int UNUSED(from_v), int to_v, bool is_duplicate, void *userdata)
431 {
432  PoseFloodFillData *data = userdata;
433 
434  const int index = to_v;
435  bool visit_next = false;
436 
437  const float *co = SCULPT_vertex_co_get(ss, index);
438  const bool symmetry_check = SCULPT_check_vertex_pivot_symmetry(
439  co, data->pose_initial_co, data->symm) &&
440  !is_duplicate;
441 
442  /* First iteration. Continue expanding using topology until a vertex is outside the brush radius
443  * to determine the first face set. */
444  if (data->current_face_set == SCULPT_FACE_SET_NONE) {
445 
446  data->pose_factor[index] = 1.0f;
447  BLI_BITMAP_ENABLE(data->is_weighted, index);
448 
450  co, data->pose_initial_co, data->radius, data->symm)) {
451  const int visited_face_set = SCULPT_vertex_face_set_get(ss, index);
452  BLI_gset_add(data->visited_face_sets, POINTER_FROM_INT(visited_face_set));
453  }
454  else if (symmetry_check) {
455  data->current_face_set = SCULPT_vertex_face_set_get(ss, index);
456  BLI_gset_add(data->visited_face_sets, POINTER_FROM_INT(data->current_face_set));
457  }
458  return true;
459  }
460 
461  /* We already have a current face set, so we can start checking the face sets of the vertices. */
462  /* In the first iteration we need to check all face sets we already visited as the flood fill may
463  * still not be finished in some of them. */
464  bool is_vertex_valid = false;
465  if (data->is_first_iteration) {
466  GSetIterator gs_iter;
467  GSET_ITER (gs_iter, data->visited_face_sets) {
468  const int visited_face_set = POINTER_AS_INT(BLI_gsetIterator_getKey(&gs_iter));
469  is_vertex_valid |= SCULPT_vertex_has_face_set(ss, index, visited_face_set);
470  }
471  }
472  else {
473  is_vertex_valid = SCULPT_vertex_has_face_set(ss, index, data->current_face_set);
474  }
475 
476  if (!is_vertex_valid) {
477  return visit_next;
478  }
479 
480  if (!BLI_BITMAP_TEST(data->is_weighted, index)) {
481  data->pose_factor[index] = 1.0f;
482  BLI_BITMAP_ENABLE(data->is_weighted, index);
483  visit_next = true;
484  }
485 
486  /* Fallback origin accumulation. */
487  if (symmetry_check) {
488  add_v3_v3(data->fallback_origin, SCULPT_vertex_co_get(ss, index));
489  data->fallback_count++;
490  }
491 
492  if (!symmetry_check || SCULPT_vertex_has_unique_face_set(ss, index)) {
493  return visit_next;
494  }
495 
496  /* We only add coordinates for calculating the origin when it is possible to go from this
497  * vertex to another vertex in a valid face set for the next iteration. */
498  bool count_as_boundary = false;
499 
501  SCULPT_VERTEX_NEIGHBORS_ITER_BEGIN (ss, index, ni) {
502  int next_face_set_candidate = SCULPT_vertex_face_set_get(ss, ni.index);
503 
504  /* Check if we can get a valid face set for the next iteration from this neighbor. */
506  !BLI_gset_haskey(data->visited_face_sets, POINTER_FROM_INT(next_face_set_candidate))) {
507  if (!data->next_face_set_found) {
508  data->next_face_set = next_face_set_candidate;
509  data->next_vertex = ni.index;
510  data->next_face_set_found = true;
511  }
512  count_as_boundary = true;
513  }
514  }
516 
517  /* Origin accumulation. */
518  if (count_as_boundary) {
519  add_v3_v3(data->pose_origin, SCULPT_vertex_co_get(ss, index));
520  data->tot_co++;
521  }
522  return visit_next;
523 }
524 
525 /* Public functions. */
526 
528  Object *ob,
529  SculptSession *ss,
530  float initial_location[3],
531  float radius,
532  float pose_offset,
533  float *r_pose_origin,
534  float *r_pose_factor)
535 {
537 
538  /* Calculate the pose rotation point based on the boundaries of the brush factor. */
539  SculptFloodFill flood;
540  SCULPT_floodfill_init(ss, &flood);
541  SCULPT_floodfill_add_active(sd, ob, ss, &flood, (r_pose_factor) ? radius : 0.0f);
542 
543  PoseFloodFillData fdata = {
544  .radius = radius,
545  .symm = SCULPT_mesh_symmetry_xyz_get(ob),
546  .pose_factor = r_pose_factor,
547  .tot_co = 0,
548  };
549  zero_v3(fdata.pose_origin);
550  copy_v3_v3(fdata.pose_initial_co, initial_location);
551  copy_v3_v3(fdata.fallback_floodfill_origin, initial_location);
553  SCULPT_floodfill_free(&flood);
554 
555  if (fdata.tot_co > 0) {
556  mul_v3_fl(fdata.pose_origin, 1.0f / (float)fdata.tot_co);
557  }
558  else {
560  }
561 
562  /* Offset the pose origin. */
563  float pose_d[3];
564  sub_v3_v3v3(pose_d, fdata.pose_origin, fdata.pose_initial_co);
565  normalize_v3(pose_d);
566  madd_v3_v3fl(fdata.pose_origin, pose_d, radius * pose_offset);
567  copy_v3_v3(r_pose_origin, fdata.pose_origin);
568 
569  /* Do the initial grow of the factors to get the first segment of the chain with Origin Offset.
570  */
571  if (pose_offset != 0.0f && r_pose_factor) {
573  sd, ob, ss, fdata.pose_origin, fdata.pose_origin, 0, NULL, r_pose_factor);
574  }
575 }
576 
577 static void pose_brush_init_task_cb_ex(void *__restrict userdata,
578  const int n,
579  const TaskParallelTLS *__restrict UNUSED(tls))
580 {
581  SculptThreadedTaskData *data = userdata;
582  SculptSession *ss = data->ob->sculpt;
583  PBVHVertexIter vd;
584  BKE_pbvh_vertex_iter_begin (ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE) {
586  float avg = 0.0f;
587  int total = 0;
589  avg += data->pose_factor[ni.index];
590  total++;
591  }
593 
594  if (total > 0) {
595  data->pose_factor[vd.index] = avg / total;
596  }
597  }
599 }
600 
601 /* Init the IK chain with empty weights. */
602 static SculptPoseIKChain *pose_ik_chain_new(const int totsegments, const int totverts)
603 {
604  SculptPoseIKChain *ik_chain = MEM_callocN(sizeof(SculptPoseIKChain), "Pose IK Chain");
605  ik_chain->tot_segments = totsegments;
606  ik_chain->segments = MEM_callocN(totsegments * sizeof(SculptPoseIKChainSegment),
607  "Pose IK Chain Segments");
608  for (int i = 0; i < totsegments; i++) {
609  ik_chain->segments[i].weights = MEM_callocN(totverts * sizeof(float), "Pose IK weights");
610  }
611  return ik_chain;
612 }
613 
614 /* Init the origin/head pairs of all the segments from the calculated origins. */
616  const float initial_location[3])
617 {
618  float origin[3];
619  float head[3];
620  for (int i = 0; i < ik_chain->tot_segments; i++) {
621  if (i == 0) {
622  copy_v3_v3(head, initial_location);
623  copy_v3_v3(origin, ik_chain->segments[i].orig);
624  }
625  else {
626  copy_v3_v3(head, ik_chain->segments[i - 1].orig);
627  copy_v3_v3(origin, ik_chain->segments[i].orig);
628  }
629  copy_v3_v3(ik_chain->segments[i].orig, origin);
630  copy_v3_v3(ik_chain->segments[i].initial_orig, origin);
631  copy_v3_v3(ik_chain->segments[i].head, head);
632  copy_v3_v3(ik_chain->segments[i].initial_head, head);
633  ik_chain->segments[i].len = len_v3v3(head, origin);
634  copy_v3_fl(ik_chain->segments[i].scale, 1.0f);
635  }
636 }
637 
638 static int pose_brush_num_effective_segments(const Brush *brush)
639 {
640  /* Scaling multiple segments at the same time is not supported as the IK solver can't handle
641  * changes in the segment's length. It will also required a better weight distribution to avoid
642  * artifacts in the areas affected by multiple segments. */
643  if (ELEM(brush->pose_deform_type,
646  return 1;
647  }
648  return brush->pose_ik_segments;
649 }
650 
652  Object *ob,
653  SculptSession *ss,
654  Brush *br,
655  const float initial_location[3],
656  const float radius)
657 {
658 
659  const float chain_segment_len = radius * (1.0f + br->pose_offset);
660  float next_chain_segment_target[3];
661 
662  int totvert = SCULPT_vertex_count_get(ss);
663  int nearest_vertex_index = SCULPT_nearest_vertex_get(sd, ob, initial_location, FLT_MAX, true);
664 
665  /* Init the buffers used to keep track of the changes in the pose factors as more segments are
666  * added to the IK chain. */
667 
668  /* This stores the whole pose factors values as they grow through the mesh. */
669  float *pose_factor_grow = MEM_callocN(totvert * sizeof(float), "Pose Factor Grow");
670 
671  /* This stores the previous status of the factors when growing a new iteration. */
672  float *pose_factor_grow_prev = MEM_callocN(totvert * sizeof(float),
673  "Pose Factor Grow Prev Iteration");
674 
675  pose_factor_grow[nearest_vertex_index] = 1.0f;
676 
677  const int tot_segments = pose_brush_num_effective_segments(br);
678  SculptPoseIKChain *ik_chain = pose_ik_chain_new(tot_segments, totvert);
679 
680  /* Calculate the first segment in the chain using the brush radius and the pose origin offset. */
681  copy_v3_v3(next_chain_segment_target, initial_location);
683  ob,
684  ss,
685  next_chain_segment_target,
686  radius,
687  br->pose_offset,
688  ik_chain->segments[0].orig,
689  pose_factor_grow);
690 
691  copy_v3_v3(next_chain_segment_target, ik_chain->segments[0].orig);
692 
693  /* Init the weights of this segment and store the status of the pose factors to start calculating
694  * new segment origins. */
695  for (int j = 0; j < totvert; j++) {
696  ik_chain->segments[0].weights[j] = pose_factor_grow[j];
697  pose_factor_grow_prev[j] = pose_factor_grow[j];
698  }
699 
700  /* Calculate the next segments in the chain growing the pose factors. */
701  for (int i = 1; i < ik_chain->tot_segments; i++) {
702 
703  /* Grow the factors to get the new segment origin. */
705  ob,
706  ss,
707  NULL,
708  next_chain_segment_target,
709  chain_segment_len,
710  ik_chain->segments[i].orig,
711  pose_factor_grow);
712  copy_v3_v3(next_chain_segment_target, ik_chain->segments[i].orig);
713 
714  /* Create the weights for this segment from the difference between the previous grow factor
715  * iteration an the current iteration. */
716  for (int j = 0; j < totvert; j++) {
717  ik_chain->segments[i].weights[j] = pose_factor_grow[j] - pose_factor_grow_prev[j];
718  /* Store the current grow factor status for the next iteration. */
719  pose_factor_grow_prev[j] = pose_factor_grow[j];
720  }
721  }
722 
723  pose_ik_chain_origin_heads_init(ik_chain, initial_location);
724 
725  MEM_freeN(pose_factor_grow);
726  MEM_freeN(pose_factor_grow_prev);
727 
728  return ik_chain;
729 }
730 
732  Sculpt *sd, Object *ob, SculptSession *ss, Brush *br, const float radius)
733 {
734 
735  int totvert = SCULPT_vertex_count_get(ss);
736 
737  const int tot_segments = pose_brush_num_effective_segments(br);
738 
739  SculptPoseIKChain *ik_chain = pose_ik_chain_new(tot_segments, totvert);
740 
741  GSet *visited_face_sets = BLI_gset_int_new_ex("visited_face_sets", ik_chain->tot_segments);
742 
743  BLI_bitmap *is_weighted = BLI_BITMAP_NEW(totvert, "weighted");
744 
745  int current_face_set = SCULPT_FACE_SET_NONE;
746  int prev_face_set = SCULPT_FACE_SET_NONE;
747 
748  int current_vertex = SCULPT_active_vertex_get(ss);
749 
750  for (int s = 0; s < ik_chain->tot_segments; s++) {
751 
752  SculptFloodFill flood;
753  SCULPT_floodfill_init(ss, &flood);
754  SCULPT_floodfill_add_initial_with_symmetry(sd, ob, ss, &flood, current_vertex, FLT_MAX);
755 
756  BLI_gset_add(visited_face_sets, POINTER_FROM_INT(current_face_set));
757 
758  PoseFloodFillData fdata = {
759  .radius = radius,
760  .symm = SCULPT_mesh_symmetry_xyz_get(ob),
761  .pose_factor = ik_chain->segments[s].weights,
762  .tot_co = 0,
763  .fallback_count = 0,
764  .current_face_set = current_face_set,
765  .prev_face_set = prev_face_set,
766  .visited_face_sets = visited_face_sets,
767  .is_weighted = is_weighted,
768  .next_face_set_found = false,
769  .is_first_iteration = s == 0,
770  };
771  zero_v3(fdata.pose_origin);
772  zero_v3(fdata.fallback_origin);
773  copy_v3_v3(fdata.pose_initial_co, SCULPT_vertex_co_get(ss, current_vertex));
775  SCULPT_floodfill_free(&flood);
776 
777  if (fdata.tot_co > 0) {
778  mul_v3_fl(fdata.pose_origin, 1.0f / (float)fdata.tot_co);
779  copy_v3_v3(ik_chain->segments[s].orig, fdata.pose_origin);
780  }
781  else if (fdata.fallback_count > 0) {
782  mul_v3_fl(fdata.fallback_origin, 1.0f / (float)fdata.fallback_count);
783  copy_v3_v3(ik_chain->segments[s].orig, fdata.fallback_origin);
784  }
785  else {
786  zero_v3(ik_chain->segments[s].orig);
787  }
788 
789  prev_face_set = fdata.current_face_set;
790  current_face_set = fdata.next_face_set;
791  current_vertex = fdata.next_vertex;
792  }
793 
794  BLI_gset_free(visited_face_sets, NULL);
795 
797 
798  MEM_SAFE_FREE(is_weighted);
799 
800  return ik_chain;
801 }
802 
804  SculptSession *ss, int from_v, int to_v, bool is_duplicate, void *userdata)
805 {
806  PoseFloodFillData *data = userdata;
807 
808  if (!is_duplicate) {
809  data->floodfill_it[to_v] = data->floodfill_it[from_v] + 1;
810  }
811  else {
812  data->floodfill_it[to_v] = data->floodfill_it[from_v];
813  }
814 
815  const int to_face_set = SCULPT_vertex_face_set_get(ss, to_v);
816  if (!BLI_gset_haskey(data->visited_face_sets, POINTER_FROM_INT(to_face_set))) {
817  if (SCULPT_vertex_has_unique_face_set(ss, to_v) &&
818  !SCULPT_vertex_has_unique_face_set(ss, from_v) &&
819  SCULPT_vertex_has_face_set(ss, from_v, to_face_set)) {
820 
821  BLI_gset_add(data->visited_face_sets, POINTER_FROM_INT(to_face_set));
822 
823  if (data->floodfill_it[to_v] >= data->masked_face_set_it) {
824  data->masked_face_set = to_face_set;
825  data->masked_face_set_it = data->floodfill_it[to_v];
826  }
827 
828  if (data->target_face_set == SCULPT_FACE_SET_NONE) {
829  data->target_face_set = to_face_set;
830  }
831  }
832  }
833 
834  return SCULPT_vertex_has_face_set(ss, to_v, data->initial_face_set);
835 }
836 
838  SculptSession *ss, int UNUSED(from_v), int to_v, bool UNUSED(is_duplicate), void *userdata)
839 {
840  PoseFloodFillData *data = userdata;
841  data->fk_weights[to_v] = 1.0f;
842  return !SCULPT_vertex_has_face_set(ss, to_v, data->masked_face_set);
843 }
844 
846  Sculpt *sd, Object *ob, SculptSession *ss, const float radius, const float *initial_location)
847 {
848  const int totvert = SCULPT_vertex_count_get(ss);
849 
850  SculptPoseIKChain *ik_chain = pose_ik_chain_new(1, totvert);
851 
852  const int active_vertex = SCULPT_active_vertex_get(ss);
853  const int active_face_set = SCULPT_active_face_set_get(ss);
854 
855  SculptFloodFill flood;
856  SCULPT_floodfill_init(ss, &flood);
857  SCULPT_floodfill_add_initial(&flood, active_vertex);
858  PoseFloodFillData fdata;
859  fdata.floodfill_it = MEM_calloc_arrayN(totvert, sizeof(int), "floodfill iteration");
860  fdata.floodfill_it[active_vertex] = 1;
861  fdata.initial_face_set = active_face_set;
864  fdata.masked_face_set_it = 0;
865  fdata.visited_face_sets = BLI_gset_int_new_ex("visited_face_sets", 3);
867  SCULPT_floodfill_free(&flood);
869 
870  int origin_count = 0;
871  float origin_acc[3] = {0.0f};
872  for (int i = 0; i < totvert; i++) {
873  if (fdata.floodfill_it[i] != 0 && SCULPT_vertex_has_face_set(ss, i, fdata.initial_face_set) &&
875  add_v3_v3(origin_acc, SCULPT_vertex_co_get(ss, i));
876  origin_count++;
877  }
878  }
879 
880  int target_count = 0;
881  float target_acc[3] = {0.0f};
882  if (fdata.target_face_set != fdata.masked_face_set) {
883  for (int i = 0; i < totvert; i++) {
884  if (fdata.floodfill_it[i] != 0 &&
887  add_v3_v3(target_acc, SCULPT_vertex_co_get(ss, i));
888  target_count++;
889  }
890  }
891  }
892 
893  MEM_freeN(fdata.floodfill_it);
894 
895  if (origin_count > 0) {
896  copy_v3_v3(ik_chain->segments[0].orig, origin_acc);
897  mul_v3_fl(ik_chain->segments[0].orig, 1.0f / origin_count);
898  }
899  else {
900  zero_v3(ik_chain->segments[0].orig);
901  }
902 
903  if (target_count > 0) {
904  copy_v3_v3(ik_chain->segments[0].head, target_acc);
905  mul_v3_fl(ik_chain->segments[0].head, 1.0f / target_count);
906  sub_v3_v3v3(ik_chain->grab_delta_offset, ik_chain->segments[0].head, initial_location);
907  }
908  else {
909  copy_v3_v3(ik_chain->segments[0].head, initial_location);
910  }
911 
912  SCULPT_floodfill_init(ss, &flood);
913  SCULPT_floodfill_add_active(sd, ob, ss, &flood, radius);
914  fdata.fk_weights = ik_chain->segments[0].weights;
916  SCULPT_floodfill_free(&flood);
917 
918  pose_ik_chain_origin_heads_init(ik_chain, ik_chain->segments[0].head);
919  return ik_chain;
920 }
921 
923  Object *ob,
924  SculptSession *ss,
925  Brush *br,
926  const float initial_location[3],
927  const float radius)
928 {
929  SculptPoseIKChain *ik_chain = NULL;
930 
931  const bool use_fake_neighbors = !(br->flag2 & BRUSH_USE_CONNECTED_ONLY);
932 
933  if (use_fake_neighbors) {
936  }
937 
938  switch (br->pose_origin_type) {
940  ik_chain = pose_ik_chain_init_topology(sd, ob, ss, br, initial_location, radius);
941  break;
943  ik_chain = pose_ik_chain_init_face_sets(sd, ob, ss, br, radius);
944  break;
946  ik_chain = pose_ik_chain_init_face_sets_fk(sd, ob, ss, radius, initial_location);
947  break;
948  }
949 
950  if (use_fake_neighbors) {
952  }
953 
954  return ik_chain;
955 }
956 
958 {
959  PBVHNode **nodes;
960  PBVH *pbvh = ob->sculpt->pbvh;
961  int totnode;
962 
963  BKE_pbvh_search_gather(pbvh, NULL, NULL, &nodes, &totnode);
964 
966  .sd = sd,
967  .ob = ob,
968  .brush = br,
969  .nodes = nodes,
970  };
971 
972  /* Init the IK chain that is going to be used to deform the vertices. */
974  sd, ob, ss, br, ss->cache->true_location, ss->cache->radius);
975 
976  /* Smooth the weights of each segment for cleaner deformation. */
977  for (int ik = 0; ik < ss->cache->pose_ik_chain->tot_segments; ik++) {
978  data.pose_factor = ss->cache->pose_ik_chain->segments[ik].weights;
979  for (int i = 0; i < br->pose_smooth_iterations; i++) {
980  TaskParallelSettings settings;
981  BKE_pbvh_parallel_range_settings(&settings, true, totnode);
982  BLI_task_parallel_range(0, totnode, &data, pose_brush_init_task_cb_ex, &settings);
983  }
984  }
985 
986  MEM_SAFE_FREE(nodes);
987 }
988 
990 {
991  SculptPoseIKChain *ik_chain = ss->cache->pose_ik_chain;
994 }
995 
996 /* Calculate a scale factor based on the grab delta. */
997 static float sculpt_pose_get_scale_from_grab_delta(SculptSession *ss, const float ik_target[3])
998 {
999  SculptPoseIKChain *ik_chain = ss->cache->pose_ik_chain;
1000  float plane[4];
1001  float segment_dir[3];
1002  sub_v3_v3v3(segment_dir, ik_chain->segments[0].initial_head, ik_chain->segments[0].initial_orig);
1003  normalize_v3(segment_dir);
1004  plane_from_point_normal_v3(plane, ik_chain->segments[0].initial_head, segment_dir);
1005  const float segment_len = ik_chain->segments[0].len;
1006  return segment_len / (segment_len - dist_signed_to_plane_v3(ik_target, plane));
1007 }
1008 
1010 {
1011  float ik_target[3];
1012  SculptPoseIKChain *ik_chain = ss->cache->pose_ik_chain;
1013 
1014  copy_v3_v3(ik_target, ss->cache->true_location);
1015  add_v3_v3(ik_target, ss->cache->grab_delta);
1016 
1017  /* Solve the IK for the first segment to include rotation as part of scale if enabled. */
1018  if (!(brush->flag2 & BRUSH_POSE_USE_LOCK_ROTATION)) {
1019  pose_solve_ik_chain(ik_chain, ik_target, brush->flag2 & BRUSH_POSE_IK_ANCHORED);
1020  }
1021 
1022  float scale[3];
1023  copy_v3_fl(scale, sculpt_pose_get_scale_from_grab_delta(ss, ik_target));
1024 
1025  /* Write the scale into the segments. */
1026  pose_solve_scale_chain(ik_chain, scale);
1027 }
1028 
1030 {
1031  SculptPoseIKChain *ik_chain = ss->cache->pose_ik_chain;
1032 
1033  /* Calculate the maximum roll. 0.02 radians per pixel works fine. */
1034  float roll = (ss->cache->initial_mouse[0] - ss->cache->mouse[0]) * ss->cache->bstrength * 0.02f;
1035  BKE_curvemapping_init(brush->curve);
1036  pose_solve_roll_chain(ik_chain, brush, roll);
1037 }
1038 
1040 {
1041  float ik_target[3];
1042  SculptPoseIKChain *ik_chain = ss->cache->pose_ik_chain;
1043 
1044  /* Calculate the IK target. */
1045  copy_v3_v3(ik_target, ss->cache->true_location);
1046  add_v3_v3(ik_target, ss->cache->grab_delta);
1047  add_v3_v3(ik_target, ik_chain->grab_delta_offset);
1048 
1049  /* Solve the IK positions. */
1050  pose_solve_ik_chain(ik_chain, ik_target, brush->flag2 & BRUSH_POSE_IK_ANCHORED);
1051 }
1052 
1054 {
1055  if (ss->cache->invert) {
1056  sculpt_pose_do_twist_deform(ss, brush);
1057  }
1058  else {
1059  sculpt_pose_do_rotate_deform(ss, brush);
1060  }
1061 }
1062 
1064 {
1065  if (ss->cache->invert) {
1067  }
1068  else {
1069  sculpt_pose_do_scale_deform(ss, brush);
1070  }
1071 }
1072 
1074 {
1075  float ik_target[3];
1076  SculptPoseIKChain *ik_chain = ss->cache->pose_ik_chain;
1077 
1078  copy_v3_v3(ik_target, ss->cache->true_location);
1079  add_v3_v3(ik_target, ss->cache->grab_delta);
1080 
1081  float scale[3];
1082  scale[2] = sculpt_pose_get_scale_from_grab_delta(ss, ik_target);
1083  scale[0] = scale[1] = sqrtf(1.0f / scale[2]);
1084 
1085  /* Write the scale into the segments. */
1086  pose_solve_scale_chain(ik_chain, scale);
1087 }
1088 
1089 static void sculpt_pose_align_pivot_local_space(float r_mat[4][4],
1090  ePaintSymmetryFlags symm,
1091  ePaintSymmetryAreas symm_area,
1093  const float grab_location[3])
1094 {
1095  float segment_origin_head[3];
1096  float symm_head[3];
1097  float symm_orig[3];
1098 
1099  copy_v3_v3(symm_head, segment->head);
1100  copy_v3_v3(symm_orig, segment->orig);
1101 
1102  SCULPT_flip_v3_by_symm_area(symm_head, symm, symm_area, grab_location);
1103  SCULPT_flip_v3_by_symm_area(symm_orig, symm, symm_area, grab_location);
1104 
1105  sub_v3_v3v3(segment_origin_head, symm_head, symm_orig);
1106  normalize_v3(segment_origin_head);
1107 
1108  copy_v3_v3(r_mat[2], segment_origin_head);
1109  ortho_basis_v3v3_v3(r_mat[0], r_mat[1], r_mat[2]);
1110 }
1111 
1112 void SCULPT_do_pose_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
1113 {
1114  SculptSession *ss = ob->sculpt;
1115  Brush *brush = BKE_paint_brush(&sd->paint);
1117 
1118  /* The pose brush applies all enabled symmetry axis in a single iteration, so the rest can be
1119  * ignored. */
1120  if (ss->cache->mirror_symmetry_pass != 0) {
1121  return;
1122  }
1123 
1124  SculptPoseIKChain *ik_chain = ss->cache->pose_ik_chain;
1125 
1126  switch (brush->pose_deform_type) {
1129  break;
1132  break;
1135  break;
1136  }
1137 
1138  /* Flip the segment chain in all symmetry axis and calculate the transform matrices for each
1139  * possible combination. */
1140  /* This can be optimized by skipping the calculation of matrices where the symmetry is not
1141  * enabled. */
1142  for (int symm_it = 0; symm_it < PAINT_SYMM_AREAS; symm_it++) {
1143  for (int i = 0; i < ik_chain->tot_segments; i++) {
1144  float symm_rot[4];
1145  float symm_orig[3];
1146  float symm_initial_orig[3];
1147 
1148  ePaintSymmetryAreas symm_area = symm_it;
1149 
1150  copy_qt_qt(symm_rot, ik_chain->segments[i].rot);
1151  copy_v3_v3(symm_orig, ik_chain->segments[i].orig);
1152  copy_v3_v3(symm_initial_orig, ik_chain->segments[i].initial_orig);
1153 
1154  /* Flip the origins and rotation quats of each segment. */
1155  SCULPT_flip_quat_by_symm_area(symm_rot, symm, symm_area, ss->cache->orig_grab_location);
1156  SCULPT_flip_v3_by_symm_area(symm_orig, symm, symm_area, ss->cache->orig_grab_location);
1158  symm_initial_orig, symm, symm_area, ss->cache->orig_grab_location);
1159 
1160  float pivot_local_space[4][4];
1161  unit_m4(pivot_local_space);
1162 
1163  /* Align the segment pivot local space to the Z axis. */
1165  sculpt_pose_align_pivot_local_space(pivot_local_space,
1166  symm,
1167  symm_area,
1168  &ik_chain->segments[i],
1169  ss->cache->orig_grab_location);
1170  unit_m4(ik_chain->segments[i].trans_mat[symm_it]);
1171  }
1172  else {
1173  quat_to_mat4(ik_chain->segments[i].trans_mat[symm_it], symm_rot);
1174  }
1175 
1176  /* Apply segment scale to the transform. */
1177  for (int scale_i = 0; scale_i < 3; scale_i++) {
1178  mul_v3_fl(ik_chain->segments[i].trans_mat[symm_it][scale_i],
1179  ik_chain->segments[i].scale[scale_i]);
1180  }
1181 
1182  translate_m4(ik_chain->segments[i].trans_mat[symm_it],
1183  symm_orig[0] - symm_initial_orig[0],
1184  symm_orig[1] - symm_initial_orig[1],
1185  symm_orig[2] - symm_initial_orig[2]);
1186 
1187  unit_m4(ik_chain->segments[i].pivot_mat[symm_it]);
1188  translate_m4(
1189  ik_chain->segments[i].pivot_mat[symm_it], symm_orig[0], symm_orig[1], symm_orig[2]);
1190  mul_m4_m4_post(ik_chain->segments[i].pivot_mat[symm_it], pivot_local_space);
1191 
1192  invert_m4_m4(ik_chain->segments[i].pivot_mat_inv[symm_it],
1193  ik_chain->segments[i].pivot_mat[symm_it]);
1194  }
1195  }
1196 
1198  .sd = sd,
1199  .ob = ob,
1200  .brush = brush,
1201  .nodes = nodes,
1202  };
1203 
1204  TaskParallelSettings settings;
1205  BKE_pbvh_parallel_range_settings(&settings, true, totnode);
1206  BLI_task_parallel_range(0, totnode, &data, do_pose_brush_task_cb_ex, &settings);
1207 }
1208 
1210 {
1211  for (int i = 0; i < ik_chain->tot_segments; i++) {
1212  MEM_SAFE_FREE(ik_chain->segments[i].weights);
1213  }
1214  MEM_SAFE_FREE(ik_chain->segments);
1215  MEM_SAFE_FREE(ik_chain);
1216 }
float BKE_brush_curve_strength(const struct Brush *br, float p, float len)
void BKE_curvemapping_init(struct CurveMapping *cumap)
Definition: colortools.c:1235
General operations, lookup, etc. for blender objects.
#define PAINT_SYMM_AREAS
Definition: BKE_paint.h:118
ePaintSymmetryAreas
Definition: BKE_paint.h:112
struct Brush * BKE_paint_brush(struct Paint *paint)
Definition: paint.c:607
#define SCULPT_FACE_SET_NONE
Definition: BKE_paint.h:267
A BVH for high poly meshes.
#define BKE_pbvh_vertex_iter_begin(pbvh, node, vi, mode)
Definition: BKE_pbvh.h:439
#define BKE_pbvh_vertex_iter_end
Definition: BKE_pbvh.h:509
void BKE_pbvh_vert_tag_update_normal(PBVH *pbvh, int index)
Definition: pbvh.c:1967
#define PBVH_ITER_UNIQUE
Definition: BKE_pbvh.h:391
void BKE_pbvh_parallel_range_settings(struct TaskParallelSettings *settings, bool use_threading, int totnode)
Definition: pbvh.c:3211
void BKE_pbvh_search_gather(PBVH *pbvh, BKE_pbvh_SearchCallback scb, void *search_data, PBVHNode ***array, int *tot)
Definition: pbvh.c:838
#define BLI_BITMAP_NEW(_num, _alloc_string)
Definition: BLI_bitmap.h:40
#define BLI_BITMAP_TEST(_bitmap, _index)
Definition: BLI_bitmap.h:64
#define BLI_BITMAP_ENABLE(_bitmap, _index)
Definition: BLI_bitmap.h:81
unsigned int BLI_bitmap
Definition: BLI_bitmap.h:16
struct GSet GSet
Definition: BLI_ghash.h:340
GSet * BLI_gset_int_new_ex(const char *info, unsigned int nentries_reserve) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
bool BLI_gset_haskey(const GSet *gs, const void *key) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:1007
#define GSET_ITER(gs_iter_, gset_)
Definition: BLI_ghash.h:471
void BLI_gset_free(GSet *gs, GSetKeyFreeFP keyfreefp)
Definition: BLI_ghash.c:1037
BLI_INLINE void * BLI_gsetIterator_getKey(GSetIterator *gsi)
Definition: BLI_ghash.h:458
bool BLI_gset_add(GSet *gs, void *key)
Definition: BLI_ghash.c:969
void plane_from_point_normal_v3(float r_plane[4], const float plane_co[3], const float plane_no[3])
Definition: math_geom.c:209
float dist_signed_to_plane_v3(const float p[3], const float plane[4])
Definition: math_geom.c:461
void unit_m4(float m[4][4])
Definition: rct.c:1090
void translate_m4(float mat[4][4], float tx, float ty, float tz)
Definition: math_matrix.c:2318
bool invert_m4_m4(float R[4][4], const float A[4][4])
Definition: math_matrix.c:1287
void mul_m4_v3(const float M[4][4], float r[3])
Definition: math_matrix.c:729
void mul_m4_m4_post(float R[4][4], const float B[4][4])
Definition: math_matrix.c:380
void rotation_between_quats_to_quat(float q[4], const float q1[4], const float q2[4])
void rotation_between_vecs_to_quat(float q[4], const float v1[3], const float v2[3])
void unit_qt(float q[4])
Definition: math_rotation.c:27
void axis_angle_normalized_to_quat(float r[4], const float axis[3], float angle)
void copy_qt_qt(float q[4], const float a[4])
Definition: math_rotation.c:33
void quat_to_mat4(float mat[4][4], const float q[4])
MINLINE float len_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
MINLINE float normalize_v3(float r[3])
MINLINE float len_squared_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
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])
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
void ortho_basis_v3v3_v3(float r_n1[3], float r_n2[3], const float n[3])
Definition: math_vector.c:707
MINLINE void copy_v3_fl(float r[3], float f)
MINLINE void madd_v3_v3v3fl(float r[3], const float a[3], const float b[3], float f)
MINLINE void zero_v3(float r[3])
MINLINE void add_v3_v3(float r[3], const float a[3])
void BLI_task_parallel_range(int start, int stop, void *userdata, TaskParallelRangeFunc func, const TaskParallelSettings *settings)
Definition: task_range.cc:94
#define POINTER_FROM_INT(i)
#define UNUSED(x)
#define POINTER_AS_INT(i)
#define MAX2(a, b)
#define ELEM(...)
@ BRUSH_POSE_DEFORM_SQUASH_STRETCH
@ BRUSH_POSE_DEFORM_ROTATE_TWIST
@ BRUSH_POSE_DEFORM_SCALE_TRASLATE
@ BRUSH_USE_CONNECTED_ONLY
@ BRUSH_POSE_USE_LOCK_ROTATION
@ BRUSH_POSE_IK_ANCHORED
@ BRUSH_POSE_ORIGIN_FACE_SETS_FK
@ BRUSH_POSE_ORIGIN_TOPOLOGY
@ BRUSH_POSE_ORIGIN_FACE_SETS
Object is a sort of wrapper for general info.
ePaintSymmetryFlags
Read Guarded memory(de)allocation.
#define MEM_SAFE_FREE(v)
int len
Definition: draw_manager.c:108
#define rot(x, k)
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void *(* MEM_calloc_arrayN)(size_t len, size_t size, const char *str)
Definition: mallocn.c:32
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:31
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:33
ccl_device_inline float4 mask(const int4 &mask, const float4 &a)
Definition: math_float4.h:513
#define sqrtf(x)
Definition: metal/compat.h:243
Segment< FEdge *, Vec3r > segment
BLI_INLINE void flip_v3_v3(float out[3], const float in[3], const ePaintSymmetryFlags symm)
Definition: paint_intern.h:392
const float * SCULPT_vertex_co_get(SculptSession *ss, int index)
Definition: sculpt.c:125
int SCULPT_vertex_count_get(SculptSession *ss)
Definition: sculpt.c:111
void SCULPT_orig_vert_data_update(SculptOrigVertData *orig_data, PBVHVertexIter *iter)
Definition: sculpt.c:1300
void SCULPT_floodfill_add_initial_with_symmetry(Sculpt *sd, Object *ob, SculptSession *ss, SculptFloodFill *flood, int index, float radius)
Definition: sculpt.c:1092
void SCULPT_orig_vert_data_init(SculptOrigVertData *data, Object *ob, PBVHNode *node, SculptUndoType type)
Definition: sculpt.c:1290
int SCULPT_active_vertex_get(SculptSession *ss)
Definition: sculpt.c:271
int SCULPT_nearest_vertex_get(Sculpt *sd, Object *ob, const float co[3], float max_distance, bool use_original)
Definition: sculpt.c:983
void SCULPT_floodfill_init(SculptSession *ss, SculptFloodFill *flood)
Definition: sculpt.c:1072
void SCULPT_fake_neighbors_ensure(Sculpt *sd, Object *ob, const float max_dist)
Definition: sculpt.c:5879
bool SCULPT_vertex_has_face_set(SculptSession *ss, int index, int face_set)
Definition: sculpt.c:533
bool SCULPT_vertex_has_unique_face_set(SculptSession *ss, int index)
Definition: sculpt.c:662
void SCULPT_floodfill_add_initial(SculptFloodFill *flood, int index)
Definition: sculpt.c:1081
bool SCULPT_is_symmetry_iteration_valid(char i, char symm)
Definition: sculpt.c:1025
const float * SCULPT_active_vertex_co_get(SculptSession *ss)
Definition: sculpt.c:279
void SCULPT_vertex_random_access_ensure(SculptSession *ss)
Definition: sculpt.c:103
float * SCULPT_brush_deform_target_vertex_co_get(SculptSession *ss, const int deform_target, PBVHVertexIter *iter)
Definition: sculpt.c:304
void SCULPT_flip_v3_by_symm_area(float v[3], const ePaintSymmetryFlags symm, const ePaintSymmetryAreas symmarea, const float pivot[3])
Definition: sculpt.c:2863
void SCULPT_floodfill_free(SculptFloodFill *flood)
Definition: sculpt.c:1174
void SCULPT_fake_neighbors_enable(Object *ob)
Definition: sculpt.c:5909
int SCULPT_vertex_face_set_get(SculptSession *ss, int index)
Definition: sculpt.c:508
void SCULPT_fake_neighbors_disable(Object *ob)
Definition: sculpt.c:5916
char SCULPT_mesh_symmetry_xyz_get(Object *object)
Definition: sculpt.c:317
bool SCULPT_check_vertex_pivot_symmetry(const float vco[3], const float pco[3], const char symm)
Definition: sculpt.c:923
void SCULPT_floodfill_add_active(Sculpt *sd, Object *ob, SculptSession *ss, SculptFloodFill *flood, float radius)
Definition: sculpt.c:1118
ePaintSymmetryAreas SCULPT_get_vertex_symm_area(const float co[3])
Definition: sculpt.c:2848
int SCULPT_active_face_set_get(SculptSession *ss)
Definition: sculpt.c:325
void SCULPT_floodfill_execute(SculptSession *ss, SculptFloodFill *flood, bool(*func)(SculptSession *ss, int from_v, int to_v, bool is_duplicate, void *userdata), void *userdata)
Definition: sculpt.c:1143
void SCULPT_flip_quat_by_symm_area(float quat[4], const ePaintSymmetryFlags symm, const ePaintSymmetryAreas symmarea, const float pivot[3])
Definition: sculpt.c:2882
float SCULPT_automasking_factor_get(AutomaskingCache *automasking, SculptSession *ss, int vert)
#define SCULPT_VERTEX_NEIGHBORS_ITER_BEGIN(ss, v_index, neighbor_iterator)
#define SCULPT_VERTEX_NEIGHBORS_ITER_END(neighbor_iterator)
@ SCULPT_UNDO_COORDS
static void sculpt_pose_do_twist_deform(SculptSession *ss, Brush *brush)
Definition: sculpt_pose.c:1029
static void sculpt_pose_do_rotate_deform(SculptSession *ss, Brush *brush)
Definition: sculpt_pose.c:1039
static void sculpt_pose_do_scale_deform(SculptSession *ss, Brush *brush)
Definition: sculpt_pose.c:1009
static void pose_solve_translate_chain(SculptPoseIKChain *ik_chain, const float delta[3])
Definition: sculpt_pose.c:118
static void pose_solve_scale_chain(SculptPoseIKChain *ik_chain, const float scale[3])
Definition: sculpt_pose.c:133
static bool pose_face_sets_floodfill_cb(SculptSession *ss, int UNUSED(from_v), int to_v, bool is_duplicate, void *userdata)
Definition: sculpt_pose.c:429
static void sculpt_pose_do_translate_deform(SculptSession *ss, Brush *brush)
Definition: sculpt_pose.c:989
static int pose_brush_num_effective_segments(const Brush *brush)
Definition: sculpt_pose.c:638
static SculptPoseIKChain * pose_ik_chain_init_face_sets_fk(Sculpt *sd, Object *ob, SculptSession *ss, const float radius, const float *initial_location)
Definition: sculpt_pose.c:845
static void pose_brush_grow_factor_reduce(const void *__restrict UNUSED(userdata), void *__restrict chunk_join, void *__restrict chunk)
Definition: sculpt_pose.c:243
static void sculpt_pose_align_pivot_local_space(float r_mat[4][4], ePaintSymmetryFlags symm, ePaintSymmetryAreas symm_area, SculptPoseIKChainSegment *segment, const float grab_location[3])
Definition: sculpt_pose.c:1089
SculptPoseIKChain * SCULPT_pose_ik_chain_init(Sculpt *sd, Object *ob, SculptSession *ss, Brush *br, const float initial_location[3], const float radius)
Definition: sculpt_pose.c:922
static SculptPoseIKChain * pose_ik_chain_init_face_sets(Sculpt *sd, Object *ob, SculptSession *ss, Brush *br, const float radius)
Definition: sculpt_pose.c:731
static bool pose_topology_floodfill_cb(SculptSession *ss, int UNUSED(from_v), int to_v, bool is_duplicate, void *userdata)
Definition: sculpt_pose.c:400
struct PoseGrowFactorTLSData PoseGrowFactorTLSData
void SCULPT_pose_ik_chain_free(SculptPoseIKChain *ik_chain)
Definition: sculpt_pose.c:1209
void SCULPT_pose_brush_init(Sculpt *sd, Object *ob, SculptSession *ss, Brush *br)
Definition: sculpt_pose.c:957
static void pose_solve_roll_chain(SculptPoseIKChain *ik_chain, const Brush *brush, const float roll)
Definition: sculpt_pose.c:92
static void pose_ik_chain_origin_heads_init(SculptPoseIKChain *ik_chain, const float initial_location[3])
Definition: sculpt_pose.c:615
static void sculpt_pose_do_squash_stretch_deform(SculptSession *ss, Brush *UNUSED(brush))
Definition: sculpt_pose.c:1073
static SculptPoseIKChain * pose_ik_chain_init_topology(Sculpt *sd, Object *ob, SculptSession *ss, Brush *br, const float initial_location[3], const float radius)
Definition: sculpt_pose.c:651
void SCULPT_pose_calc_pose_data(Sculpt *sd, Object *ob, SculptSession *ss, float initial_location[3], float radius, float pose_offset, float *r_pose_origin, float *r_pose_factor)
Definition: sculpt_pose.c:527
static void pose_brush_init_task_cb_ex(void *__restrict userdata, const int n, const TaskParallelTLS *__restrict UNUSED(tls))
Definition: sculpt_pose.c:577
static bool pose_face_sets_fk_set_weights_floodfill_cb(SculptSession *ss, int UNUSED(from_v), int to_v, bool UNUSED(is_duplicate), void *userdata)
Definition: sculpt_pose.c:837
static void sculpt_pose_do_scale_translate_deform(SculptSession *ss, Brush *brush)
Definition: sculpt_pose.c:1063
struct PoseFloodFillData PoseFloodFillData
static float sculpt_pose_get_scale_from_grab_delta(SculptSession *ss, const float ik_target[3])
Definition: sculpt_pose.c:997
void SCULPT_do_pose_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
Definition: sculpt_pose.c:1112
static void sculpt_pose_grow_pose_factor(Sculpt *sd, Object *ob, SculptSession *ss, float pose_origin[3], float pose_target[3], float max_len, float *r_pose_origin, float *pose_factor)
Definition: sculpt_pose.c:255
static void do_pose_brush_task_cb_ex(void *__restrict userdata, const int n, const TaskParallelTLS *__restrict UNUSED(tls))
Definition: sculpt_pose.c:144
static void pose_brush_grow_factor_task_cb_ex(void *__restrict userdata, const int n, const TaskParallelTLS *__restrict tls)
Definition: sculpt_pose.c:210
static SculptPoseIKChain * pose_ik_chain_new(const int totsegments, const int totverts)
Definition: sculpt_pose.c:602
static void pose_solve_ik_chain(SculptPoseIKChain *ik_chain, const float initial_target[3], const bool use_anchor)
Definition: sculpt_pose.c:39
static void sculpt_pose_do_rotate_twist_deform(SculptSession *ss, Brush *brush)
Definition: sculpt_pose.c:1053
static bool pose_face_sets_fk_find_masked_floodfill_cb(SculptSession *ss, int from_v, int to_v, bool is_duplicate, void *userdata)
Definition: sculpt_pose.c:803
static bool sculpt_pose_brush_is_vertex_inside_brush_radius(const float vertex[3], const float br_co[3], float radius, char symm)
Definition: sculpt_pose.c:341
int pose_deform_type
int pose_smooth_iterations
struct CurveMapping * curve
int pose_ik_segments
float disconnected_distance_max
int deform_target
float pose_offset
int pose_origin_type
struct SculptSession * sculpt
struct MVert * mvert
Definition: BKE_pbvh.h:428
float * co
Definition: BKE_pbvh.h:430
float * mask
Definition: BKE_pbvh.h:433
float pose_initial_co[3]
Definition: sculpt_pose.c:359
float pose_origin[3]
Definition: sculpt_pose.c:364
float fallback_floodfill_origin[3]
Definition: sculpt_pose.c:384
GSet * visited_face_sets
Definition: sculpt_pose.c:375
float fallback_origin[3]
Definition: sculpt_pose.c:388
BLI_bitmap * is_weighted
Definition: sculpt_pose.c:378
const float * co
Definition: sculpt_intern.h:87
float pivot_mat[PAINT_SYMM_AREAS][4][4]
Definition: BKE_paint.h:292
float trans_mat[PAINT_SYMM_AREAS][4][4]
Definition: BKE_paint.h:291
float pivot_mat_inv[PAINT_SYMM_AREAS][4][4]
Definition: BKE_paint.h:293
SculptPoseIKChainSegment * segments
Definition: BKE_paint.h:297
float grab_delta_offset[3]
Definition: BKE_paint.h:299
struct StrokeCache * cache
Definition: BKE_paint.h:563
struct PBVH * pbvh
Definition: BKE_paint.h:550
Paint paint
float orig_grab_location[3]
float true_location[3]
float initial_mouse[2]
float mouse[2]
int mirror_symmetry_pass
struct SculptPoseIKChain * pose_ik_chain
AutomaskingCache * automasking
float grab_delta[3]
TaskParallelReduceFunc func_reduce
Definition: BLI_task.h:181
size_t userdata_chunk_size
Definition: BLI_task.h:169
float max