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