Cleanup: remove redundant, invalid info from headers
[blender.git] / source / blender / editors / sculpt_paint / sculpt.c
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) 2006 by Nicholas Bishop
17  * All rights reserved.
18  * Implements the Sculpt Mode tools
19  */
20
21 /** \file blender/editors/sculpt_paint/sculpt.c
22  *  \ingroup edsculpt
23  */
24
25
26 #include "MEM_guardedalloc.h"
27
28 #include "BLI_math.h"
29 #include "BLI_blenlib.h"
30 #include "BLI_dial_2d.h"
31 #include "BLI_task.h"
32 #include "BLI_utildefines.h"
33 #include "BLI_ghash.h"
34
35 #include "BLT_translation.h"
36
37 #include "DNA_customdata_types.h"
38 #include "DNA_mesh_types.h"
39 #include "DNA_meshdata_types.h"
40 #include "DNA_node_types.h"
41 #include "DNA_object_types.h"
42 #include "DNA_scene_types.h"
43 #include "DNA_brush_types.h"
44
45 #include "BKE_pbvh.h"
46 #include "BKE_brush.h"
47 #include "BKE_ccg.h"
48 #include "BKE_context.h"
49 #include "BKE_depsgraph.h"
50 #include "BKE_global.h"
51 #include "BKE_image.h"
52 #include "BKE_key.h"
53 #include "BKE_library.h"
54 #include "BKE_main.h"
55 #include "BKE_mesh.h"
56 #include "BKE_mesh_mapping.h"
57 #include "BKE_modifier.h"
58 #include "BKE_multires.h"
59 #include "BKE_paint.h"
60 #include "BKE_report.h"
61 #include "BKE_node.h"
62 #include "BKE_object.h"
63 #include "BKE_subsurf.h"
64 #include "BKE_colortools.h"
65
66 #include "WM_api.h"
67 #include "WM_types.h"
68
69 #include "ED_sculpt.h"
70 #include "ED_object.h"
71 #include "ED_screen.h"
72 #include "ED_view3d.h"
73 #include "paint_intern.h"
74 #include "sculpt_intern.h"
75
76 #include "RNA_access.h"
77 #include "RNA_define.h"
78
79 #include "GPU_buffers.h"
80 #include "GPU_extensions.h"
81
82 #include "UI_interface.h"
83 #include "UI_resources.h"
84
85 #include "bmesh.h"
86 #include "bmesh_tools.h"
87
88 #include <math.h>
89 #include <stdlib.h>
90 #include <string.h>
91
92 /** \name Tool Capabilities
93  *
94  * Avoid duplicate checks, internal logic only,
95  * share logic with #rna_def_sculpt_capabilities where possible.
96  *
97  * \{ */
98
99 /* Check if there are any active modifiers in stack
100  * (used for flushing updates at enter/exit sculpt mode) */
101 static bool sculpt_has_active_modifiers(Scene *scene, Object *ob)
102 {
103         ModifierData *md;
104         VirtualModifierData virtualModifierData;
105
106         md = modifiers_getVirtualModifierList(ob, &virtualModifierData);
107
108         /* exception for shape keys because we can edit those */
109         for (; md; md = md->next) {
110                 if (modifier_isEnabled(scene, md, eModifierMode_Realtime))
111                         return 1;
112         }
113
114         return 0;
115 }
116
117 static bool sculpt_tool_needs_original(const char sculpt_tool)
118 {
119         return ELEM(sculpt_tool,
120                     SCULPT_TOOL_GRAB,
121                     SCULPT_TOOL_ROTATE,
122                     SCULPT_TOOL_THUMB,
123                     SCULPT_TOOL_LAYER);
124 }
125
126 static bool sculpt_tool_is_proxy_used(const char sculpt_tool)
127 {
128         return ELEM(sculpt_tool,
129                     SCULPT_TOOL_SMOOTH,
130                     SCULPT_TOOL_LAYER);
131 }
132
133 /**
134  * Test whether the #StrokeCache.sculpt_normal needs update in #do_brush_action
135  */
136 static int sculpt_brush_needs_normal(const Brush *brush, float normal_weight)
137 {
138         return ((SCULPT_TOOL_HAS_NORMAL_WEIGHT(brush->sculpt_tool) &&
139                  (normal_weight > 0.0f)) ||
140
141                 ELEM(brush->sculpt_tool,
142                      SCULPT_TOOL_BLOB,
143                      SCULPT_TOOL_CREASE,
144                      SCULPT_TOOL_DRAW,
145                      SCULPT_TOOL_LAYER,
146                      SCULPT_TOOL_NUDGE,
147                      SCULPT_TOOL_ROTATE,
148                      SCULPT_TOOL_THUMB) ||
149
150                 (brush->mtex.brush_map_mode == MTEX_MAP_MODE_AREA));
151 }
152 /** \} */
153
154 static bool sculpt_brush_needs_rake_rotation(const Brush *brush)
155 {
156         return SCULPT_TOOL_HAS_RAKE(brush->sculpt_tool) && (brush->rake_factor != 0.0f);
157 }
158
159 typedef enum StrokeFlags {
160         CLIP_X = 1,
161         CLIP_Y = 2,
162         CLIP_Z = 4,
163 } StrokeFlags;
164
165 /************** Access to original unmodified vertex data *************/
166
167 typedef struct {
168         BMLog *bm_log;
169
170         SculptUndoNode *unode;
171         float (*coords)[3];
172         short (*normals)[3];
173         const float *vmasks;
174
175         /* Original coordinate, normal, and mask */
176         const float *co;
177         const short *no;
178         float mask;
179 } SculptOrigVertData;
180
181
182 /* Initialize a SculptOrigVertData for accessing original vertex data;
183  * handles BMesh, mesh, and multires */
184 static void sculpt_orig_vert_data_unode_init(SculptOrigVertData *data,
185                                              Object *ob,
186                                              SculptUndoNode *unode)
187 {
188         SculptSession *ss = ob->sculpt;
189         BMesh *bm = ss->bm;
190
191         memset(data, 0, sizeof(*data));
192         data->unode = unode;
193
194         if (bm) {
195                 data->bm_log = ss->bm_log;
196         }
197         else {
198                 data->coords = data->unode->co;
199                 data->normals = data->unode->no;
200                 data->vmasks = data->unode->mask;
201         }
202 }
203
204 /* Initialize a SculptOrigVertData for accessing original vertex data;
205  * handles BMesh, mesh, and multires */
206 static void sculpt_orig_vert_data_init(SculptOrigVertData *data,
207                                        Object *ob,
208                                        PBVHNode *node)
209 {
210         SculptUndoNode *unode;
211         unode = sculpt_undo_push_node(ob, node, SCULPT_UNDO_COORDS);
212         sculpt_orig_vert_data_unode_init(data, ob, unode);
213 }
214
215 /* Update a SculptOrigVertData for a particular vertex from the PBVH
216  * iterator */
217 static void sculpt_orig_vert_data_update(SculptOrigVertData *orig_data,
218                                          PBVHVertexIter *iter)
219 {
220         if (orig_data->unode->type == SCULPT_UNDO_COORDS) {
221                 if (orig_data->bm_log) {
222                         BM_log_original_vert_data(
223                                 orig_data->bm_log, iter->bm_vert,
224                                 &orig_data->co, &orig_data->no);
225                 }
226                 else {
227                         orig_data->co = orig_data->coords[iter->i];
228                         orig_data->no = orig_data->normals[iter->i];
229                 }
230         }
231         else if (orig_data->unode->type == SCULPT_UNDO_MASK) {
232                 if (orig_data->bm_log) {
233                         orig_data->mask = BM_log_original_mask(orig_data->bm_log, iter->bm_vert);
234                 }
235                 else {
236                         orig_data->mask = orig_data->vmasks[iter->i];
237                 }
238         }
239 }
240
241 static void sculpt_rake_data_update(struct SculptRakeData *srd, const float co[3])
242 {
243         float rake_dist = len_v3v3(srd->follow_co, co);
244         if (rake_dist > srd->follow_dist) {
245                 interp_v3_v3v3(srd->follow_co, srd->follow_co, co, rake_dist - srd->follow_dist);
246         }
247 }
248
249
250 static void sculpt_rake_rotate(
251         const SculptSession *ss, const float sculpt_co[3], const float v_co[3], float factor, float r_delta[3])
252 {
253         float vec_rot[3];
254
255 #if 0
256         /* lerp */
257         sub_v3_v3v3(vec_rot, v_co, sculpt_co);
258         mul_qt_v3(ss->cache->rake_rotation_symmetry, vec_rot);
259         add_v3_v3(vec_rot, sculpt_co);
260         sub_v3_v3v3(r_delta, vec_rot, v_co);
261         mul_v3_fl(r_delta, factor);
262 #else
263         /* slerp */
264         float q_interp[4];
265         sub_v3_v3v3(vec_rot, v_co, sculpt_co);
266
267         copy_qt_qt(q_interp, ss->cache->rake_rotation_symmetry);
268         mul_fac_qt_fl(q_interp, factor);
269         mul_qt_v3(q_interp, vec_rot);
270
271         add_v3_v3(vec_rot, sculpt_co);
272         sub_v3_v3v3(r_delta, vec_rot, v_co);
273 #endif
274
275 }
276
277 /**
278  * Align the grab delta to the brush normal.
279  *
280  * \param grab_delta: Typically from `ss->cache->grab_delta_symmetry`.
281  */
282 static void sculpt_project_v3_normal_align(SculptSession *ss, const float normal_weight, float grab_delta[3])
283 {
284         /* signed to support grabbing in (to make a hole) as well as out. */
285         const float len_signed = dot_v3v3(ss->cache->sculpt_normal_symm, grab_delta);
286
287         /* this scale effectively projects the offset so dragging follows the cursor,
288          * as the normal points towards the view, the scale increases. */
289         float len_view_scale;
290         {
291                 float view_aligned_normal[3];
292                 project_plane_v3_v3v3(view_aligned_normal, ss->cache->sculpt_normal_symm, ss->cache->view_normal);
293                 len_view_scale = fabsf(dot_v3v3(view_aligned_normal, ss->cache->sculpt_normal_symm));
294                 len_view_scale = (len_view_scale > FLT_EPSILON) ? 1.0f / len_view_scale : 1.0f;
295         }
296
297         mul_v3_fl(grab_delta, 1.0f - normal_weight);
298         madd_v3_v3fl(grab_delta, ss->cache->sculpt_normal_symm, (len_signed * normal_weight) * len_view_scale);
299 }
300
301
302 /** \name SculptProjectVector
303  *
304  * Fast-path for #project_plane_v3_v3v3
305  *
306  * \{ */
307
308 typedef struct SculptProjectVector {
309         float plane[3];
310         float len_sq;
311         float len_sq_inv_neg;
312         bool  is_valid;
313
314 } SculptProjectVector;
315
316 /**
317  * \param plane: Direction, can be any length.
318  */
319 static void sculpt_project_v3_cache_init(
320         SculptProjectVector *spvc, const float plane[3])
321 {
322         copy_v3_v3(spvc->plane, plane);
323         spvc->len_sq = len_squared_v3(spvc->plane);
324         spvc->is_valid = (spvc->len_sq > FLT_EPSILON);
325         spvc->len_sq_inv_neg = (spvc->is_valid) ? -1.0f / spvc->len_sq : 0.0f;
326 }
327
328 /**
329  * Calculate the projection.
330  */
331 static void sculpt_project_v3(
332         const SculptProjectVector *spvc, const float vec[3],
333         float r_vec[3])
334 {
335 #if 0
336         project_plane_v3_v3v3(r_vec, vec, spvc->plane);
337 #else
338         /* inline the projection, cache `-1.0 / dot_v3_v3(v_proj, v_proj)` */
339         madd_v3_v3fl(r_vec, spvc->plane, dot_v3v3(vec, spvc->plane) * spvc->len_sq_inv_neg);
340 #endif
341 }
342
343 /** \} */
344
345
346 /**********************************************************************/
347
348 /* Returns true if the stroke will use dynamic topology, false
349  * otherwise.
350  *
351  * Factors: some brushes like grab cannot do dynamic topology.
352  * Others, like smooth, are better without. Same goes for alt-
353  * key smoothing. */
354 static bool sculpt_stroke_is_dynamic_topology(
355         const SculptSession *ss, const Brush *brush)
356 {
357         return ((BKE_pbvh_type(ss->pbvh) == PBVH_BMESH) &&
358
359                 (!ss->cache || (!ss->cache->alt_smooth)) &&
360
361                 /* Requires mesh restore, which doesn't work with
362                  * dynamic-topology */
363                 !(brush->flag & BRUSH_ANCHORED) &&
364                 !(brush->flag & BRUSH_DRAG_DOT) &&
365
366                 SCULPT_TOOL_HAS_DYNTOPO(brush->sculpt_tool));
367 }
368
369 /*** paint mesh ***/
370
371 static void paint_mesh_restore_co_task_cb(
372         void *__restrict userdata,
373         const int n,
374         const ParallelRangeTLS *__restrict UNUSED(tls))
375 {
376         SculptThreadedTaskData *data = userdata;
377         SculptSession *ss = data->ob->sculpt;
378
379         SculptUndoNode *unode;
380         SculptUndoType type = (data->brush->sculpt_tool == SCULPT_TOOL_MASK ? SCULPT_UNDO_MASK : SCULPT_UNDO_COORDS);
381
382         if (ss->bm) {
383                 unode = sculpt_undo_push_node(data->ob, data->nodes[n], type);
384         }
385         else {
386                 unode = sculpt_undo_get_node(data->nodes[n]);
387         }
388
389         if (unode) {
390                 PBVHVertexIter vd;
391                 SculptOrigVertData orig_data;
392
393                 sculpt_orig_vert_data_unode_init(&orig_data, data->ob, unode);
394
395                 BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
396                 {
397                         sculpt_orig_vert_data_update(&orig_data, &vd);
398
399                         if (orig_data.unode->type == SCULPT_UNDO_COORDS) {
400                                 copy_v3_v3(vd.co, orig_data.co);
401                                 if (vd.no)
402                                         copy_v3_v3_short(vd.no, orig_data.no);
403                                 else
404                                         normal_short_to_float_v3(vd.fno, orig_data.no);
405                         }
406                         else if (orig_data.unode->type == SCULPT_UNDO_MASK) {
407                                 *vd.mask = orig_data.mask;
408                         }
409
410                         if (vd.mvert)
411                                 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
412                 }
413                 BKE_pbvh_vertex_iter_end;
414
415                 BKE_pbvh_node_mark_update(data->nodes[n]);
416         }
417 }
418
419 static void paint_mesh_restore_co(Sculpt *sd, Object *ob)
420 {
421         SculptSession *ss = ob->sculpt;
422         Brush *brush = BKE_paint_brush(&sd->paint);
423
424         PBVHNode **nodes;
425         int totnode;
426
427         BKE_pbvh_search_gather(ss->pbvh, NULL, NULL, &nodes, &totnode);
428
429         /* Disable OpenMP when dynamic-topology is enabled. Otherwise, new entries might be inserted by
430          * sculpt_undo_push_node() into the GHash used internally by BM_log_original_vert_co() by a different thread.
431          * See T33787. */
432         SculptThreadedTaskData data = {
433                 .sd = sd, .ob = ob, .brush = brush, .nodes = nodes,
434         };
435
436         ParallelRangeSettings settings;
437         BLI_parallel_range_settings_defaults(&settings);
438         settings.use_threading = ((sd->flags & SCULPT_USE_OPENMP) && !ss->bm && totnode > SCULPT_THREADED_LIMIT);
439         BLI_task_parallel_range(
440                     0, totnode,
441                     &data,
442                     paint_mesh_restore_co_task_cb,
443                     &settings);
444
445         if (nodes)
446                 MEM_freeN(nodes);
447 }
448
449 /*** BVH Tree ***/
450
451 static void sculpt_extend_redraw_rect_previous(Object *ob, rcti *rect)
452 {
453         /* expand redraw rect with redraw rect from previous step to
454          * prevent partial-redraw issues caused by fast strokes. This is
455          * needed here (not in sculpt_flush_update) as it was before
456          * because redraw rectangle should be the same in both of
457          * optimized PBVH draw function and 3d view redraw (if not -- some
458          * mesh parts could disappear from screen (sergey) */
459         SculptSession *ss = ob->sculpt;
460
461         if (ss->cache) {
462                 if (!BLI_rcti_is_empty(&ss->cache->previous_r))
463                         BLI_rcti_union(rect, &ss->cache->previous_r);
464         }
465 }
466
467 /* Get a screen-space rectangle of the modified area */
468 bool sculpt_get_redraw_rect(ARegion *ar, RegionView3D *rv3d,
469                                    Object *ob, rcti *rect)
470 {
471         PBVH *pbvh = ob->sculpt->pbvh;
472         float bb_min[3], bb_max[3];
473
474         if (!pbvh)
475                 return 0;
476
477         BKE_pbvh_redraw_BB(pbvh, bb_min, bb_max);
478
479         /* convert 3D bounding box to screen space */
480         if (!paint_convert_bb_to_rect(rect,
481                                       bb_min,
482                                       bb_max,
483                                       ar,
484                                       rv3d,
485                                       ob))
486         {
487                 return 0;
488         }
489
490
491         return 1;
492 }
493
494 void ED_sculpt_redraw_planes_get(float planes[4][4], ARegion *ar,
495                                  RegionView3D *rv3d, Object *ob)
496 {
497         PBVH *pbvh = ob->sculpt->pbvh;
498         /* copy here, original will be used below */
499         rcti rect = ob->sculpt->cache->current_r;
500
501         sculpt_extend_redraw_rect_previous(ob, &rect);
502
503         paint_calc_redraw_planes(planes, ar, rv3d, ob, &rect);
504
505         /* we will draw this rect, so now we can set it as the previous partial rect.
506          * Note that we don't update with the union of previous/current (rect), only with
507          * the current. Thus we avoid the rectangle needlessly growing to include
508          * all the stroke area */
509         ob->sculpt->cache->previous_r = ob->sculpt->cache->current_r;
510
511         /* clear redraw flag from nodes */
512         if (pbvh)
513                 BKE_pbvh_update(pbvh, PBVH_UpdateRedraw, NULL);
514 }
515
516 /************************ Brush Testing *******************/
517
518 void sculpt_brush_test_init(SculptSession *ss, SculptBrushTest *test)
519 {
520         RegionView3D *rv3d = ss->cache->vc->rv3d;
521
522         test->radius_squared = ss->cache->radius_squared;
523         copy_v3_v3(test->location, ss->cache->location);
524         test->dist = 0.0f;   /* just for initialize */
525
526         /* Only for 2D projection. */
527         zero_v4(test->plane_view);
528         zero_v4(test->plane_tool);
529
530         test->mirror_symmetry_pass = ss->cache->mirror_symmetry_pass;
531
532         if (rv3d->rflag & RV3D_CLIPPING) {
533                 test->clip_rv3d = rv3d;
534         }
535         else {
536                 test->clip_rv3d = NULL;
537         }
538 }
539
540 BLI_INLINE bool sculpt_brush_test_clipping(const SculptBrushTest *test, const float co[3])
541 {
542         RegionView3D *rv3d = test->clip_rv3d;
543         if (!rv3d) {
544                 return false;
545         }
546         float symm_co[3];
547         flip_v3_v3(symm_co, co, test->mirror_symmetry_pass);
548         return ED_view3d_clipping_test(rv3d, symm_co, true);
549 }
550
551 bool sculpt_brush_test_sphere(SculptBrushTest *test, const float co[3])
552 {
553         float distsq = len_squared_v3v3(co, test->location);
554
555         if (distsq <= test->radius_squared) {
556                 if (sculpt_brush_test_clipping(test, co)) {
557                         return 0;
558                 }
559                 test->dist = sqrtf(distsq);
560                 return 1;
561         }
562         else {
563                 return 0;
564         }
565 }
566
567 bool sculpt_brush_test_sphere_sq(SculptBrushTest *test, const float co[3])
568 {
569         float distsq = len_squared_v3v3(co, test->location);
570
571         if (distsq <= test->radius_squared) {
572                 if (sculpt_brush_test_clipping(test, co)) {
573                         return 0;
574                 }
575                 test->dist = distsq;
576                 return 1;
577         }
578         else {
579                 return 0;
580         }
581 }
582
583 bool sculpt_brush_test_sphere_fast(const SculptBrushTest *test, const float co[3])
584 {
585         if (sculpt_brush_test_clipping(test, co)) {
586                 return 0;
587         }
588         return len_squared_v3v3(co, test->location) <= test->radius_squared;
589 }
590
591 bool sculpt_brush_test_circle_sq(SculptBrushTest *test, const float co[3])
592 {
593         float co_proj[3];
594         closest_to_plane_normalized_v3(co_proj, test->plane_view, co);
595         float distsq = len_squared_v3v3(co_proj, test->location);
596
597         if (distsq <= test->radius_squared) {
598                 if (sculpt_brush_test_clipping(test, co)) {
599                         return 0;
600                 }
601                 test->dist = distsq;
602                 return 1;
603         }
604         else {
605                 return 0;
606         }
607 }
608
609 bool sculpt_brush_test_cube(SculptBrushTest *test, const float co[3], float local[4][4])
610 {
611         float side = M_SQRT1_2;
612         float local_co[3];
613
614         if (sculpt_brush_test_clipping(test, co)) {
615                 return 0;
616         }
617
618         mul_v3_m4v3(local_co, local, co);
619
620         local_co[0] = fabsf(local_co[0]);
621         local_co[1] = fabsf(local_co[1]);
622         local_co[2] = fabsf(local_co[2]);
623
624         if (local_co[0] <= side && local_co[1] <= side && local_co[2] <= side) {
625                 float p = 4.0f;
626
627                 test->dist = ((powf(local_co[0], p) +
628                                powf(local_co[1], p) +
629                                powf(local_co[2], p)) / powf(side, p));
630
631                 return 1;
632         }
633         else {
634                 return 0;
635         }
636 }
637
638 SculptBrushTestFn sculpt_brush_test_init_with_falloff_shape(
639         SculptSession *ss, SculptBrushTest *test, char falloff_shape)
640 {
641         sculpt_brush_test_init(ss, test);
642         SculptBrushTestFn sculpt_brush_test_sq_fn;
643         if (falloff_shape == PAINT_FALLOFF_SHAPE_SPHERE) {
644                 sculpt_brush_test_sq_fn = sculpt_brush_test_sphere_sq;
645         }
646         else {
647                 /* PAINT_FALLOFF_SHAPE_TUBE */
648                 plane_from_point_normal_v3(test->plane_view, test->location, ss->cache->view_normal);
649                 sculpt_brush_test_sq_fn = sculpt_brush_test_circle_sq;
650         }
651         return sculpt_brush_test_sq_fn;
652 }
653
654 const float *sculpt_brush_frontface_normal_from_falloff_shape(
655         SculptSession *ss, char falloff_shape)
656 {
657         if (falloff_shape == PAINT_FALLOFF_SHAPE_SPHERE) {
658                 return ss->cache->sculpt_normal_symm;
659         }
660         else {
661                 /* PAINT_FALLOFF_SHAPE_TUBE */
662                 return ss->cache->view_normal;
663         }
664 }
665
666
667 static float frontface(const Brush *br, const float sculpt_normal[3],
668                        const short no[3], const float fno[3])
669 {
670         if (br->flag & BRUSH_FRONTFACE) {
671                 float dot;
672
673                 if (no) {
674                         float tmp[3];
675
676                         normal_short_to_float_v3(tmp, no);
677                         dot = dot_v3v3(tmp, sculpt_normal);
678                 }
679                 else {
680                         dot = dot_v3v3(fno, sculpt_normal);
681                 }
682                 return dot > 0 ? dot : 0;
683         }
684         else {
685                 return 1;
686         }
687 }
688
689 #if 0
690
691 static bool sculpt_brush_test_cyl(SculptBrushTest *test, float co[3], float location[3], const float area_no[3])
692 {
693         if (sculpt_brush_test_sphere_fast(test, co)) {
694                 float t1[3], t2[3], t3[3], dist;
695
696                 sub_v3_v3v3(t1, location, co);
697                 sub_v3_v3v3(t2, x2, location);
698
699                 cross_v3_v3v3(t3, area_no, t1);
700
701                 dist = len_v3(t3) / len_v3(t2);
702
703                 test->dist = dist;
704
705                 return 1;
706         }
707
708         return 0;
709 }
710
711 #endif
712
713 /* ===== Sculpting =====
714  */
715 static void flip_v3(float v[3], const char symm)
716 {
717         flip_v3_v3(v, v, symm);
718 }
719
720 static float calc_overlap(StrokeCache *cache, const char symm, const char axis, const float angle)
721 {
722         float mirror[3];
723         float distsq;
724
725         /* flip_v3_v3(mirror, cache->traced_location, symm); */
726         flip_v3_v3(mirror, cache->true_location, symm);
727
728         if (axis != 0) {
729                 float mat[3][3];
730                 axis_angle_to_mat3_single(mat, axis, angle);
731                 mul_m3_v3(mat, mirror);
732         }
733
734         /* distsq = len_squared_v3v3(mirror, cache->traced_location); */
735         distsq = len_squared_v3v3(mirror, cache->true_location);
736
737         if (distsq <= 4.0f * (cache->radius_squared))
738                 return (2.0f * (cache->radius) - sqrtf(distsq)) / (2.0f * (cache->radius));
739         else
740                 return 0;
741 }
742
743 static float calc_radial_symmetry_feather(Sculpt *sd, StrokeCache *cache, const char symm, const char axis)
744 {
745         int i;
746         float overlap;
747
748         overlap = 0;
749         for (i = 1; i < sd->radial_symm[axis - 'X']; ++i) {
750                 const float angle = 2 * M_PI * i / sd->radial_symm[axis - 'X'];
751                 overlap += calc_overlap(cache, symm, axis, angle);
752         }
753
754         return overlap;
755 }
756
757 static float calc_symmetry_feather(Sculpt *sd, StrokeCache *cache)
758 {
759         if (sd->paint.symmetry_flags & PAINT_SYMMETRY_FEATHER) {
760                 float overlap;
761                 int symm = cache->symmetry;
762                 int i;
763
764                 overlap = 0;
765                 for (i = 0; i <= symm; i++) {
766                         if (i == 0 || (symm & i && (symm != 5 || i != 3) && (symm != 6 || (i != 3 && i != 5)))) {
767
768                                 overlap += calc_overlap(cache, i, 0, 0);
769
770                                 overlap += calc_radial_symmetry_feather(sd, cache, i, 'X');
771                                 overlap += calc_radial_symmetry_feather(sd, cache, i, 'Y');
772                                 overlap += calc_radial_symmetry_feather(sd, cache, i, 'Z');
773                         }
774                 }
775
776                 return 1 / overlap;
777         }
778         else {
779                 return 1;
780         }
781 }
782
783 /** \name Calculate Normal and Center
784  *
785  * Calculate geometry surrounding the brush center.
786  * (optionally using original coordinates).
787  *
788  * Functions are:
789  * - #calc_area_center
790  * - #calc_area_normal
791  * - #calc_area_normal_and_center
792  *
793  * \note These are all _very_ similar, when changing one, check others.
794  * \{ */
795
796 static void calc_area_normal_and_center_task_cb(
797         void *__restrict userdata,
798         const int n,
799         const ParallelRangeTLS *__restrict UNUSED(tls))
800 {
801         SculptThreadedTaskData *data = userdata;
802         SculptSession *ss = data->ob->sculpt;
803         float (*area_nos)[3] = data->area_nos;
804         float (*area_cos)[3] = data->area_cos;
805
806         PBVHVertexIter vd;
807         SculptUndoNode *unode = NULL;
808
809         float private_co[2][3] = {{0.0f}};
810         float private_no[2][3] = {{0.0f}};
811         int   private_count[2] = {0};
812         bool use_original = false;
813
814         if (ss->cache->original) {
815                 unode = sculpt_undo_push_node(data->ob, data->nodes[n], SCULPT_UNDO_COORDS);
816                 use_original = (unode->co || unode->bm_entry);
817         }
818
819         SculptBrushTest test;
820         SculptBrushTestFn sculpt_brush_test_sq_fn =
821                 sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape);
822
823
824         /* when the mesh is edited we can't rely on original coords
825          * (original mesh may not even have verts in brush radius) */
826         if (use_original && data->has_bm_orco) {
827                 float (*orco_coords)[3];
828                 int   (*orco_tris)[3];
829                 int     orco_tris_num;
830                 int i;
831
832                 BKE_pbvh_node_get_bm_orco_data(data->nodes[n], &orco_tris, &orco_tris_num, &orco_coords);
833
834                 for (i = 0; i < orco_tris_num; i++) {
835                         const float *co_tri[3] = {
836                             orco_coords[orco_tris[i][0]],
837                             orco_coords[orco_tris[i][1]],
838                             orco_coords[orco_tris[i][2]],
839                         };
840                         float co[3];
841
842                         closest_on_tri_to_point_v3(co, test.location, UNPACK3(co_tri));
843
844                         if (sculpt_brush_test_sq_fn(&test, co)) {
845                                 float no[3];
846                                 int flip_index;
847
848                                 normal_tri_v3(no, UNPACK3(co_tri));
849
850                                 flip_index = (dot_v3v3(ss->cache->view_normal, no) <= 0.0f);
851                                 if (area_cos)
852                                         add_v3_v3(private_co[flip_index], co);
853                                 if (area_nos)
854                                         add_v3_v3(private_no[flip_index], no);
855                                 private_count[flip_index] += 1;
856                         }
857                 }
858         }
859         else {
860                 BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
861                 {
862                         const float *co;
863                         const short *no_s;  /* bm_vert only */
864
865                         if (use_original) {
866                                 if (unode->bm_entry) {
867                                         BM_log_original_vert_data(ss->bm_log, vd.bm_vert, &co, &no_s);
868                                 }
869                                 else {
870                                         co = unode->co[vd.i];
871                                         no_s = unode->no[vd.i];
872                                 }
873                         }
874                         else {
875                                 co = vd.co;
876                         }
877
878                         if (sculpt_brush_test_sq_fn(&test, co)) {
879                                 float no_buf[3];
880                                 const float *no;
881                                 int flip_index;
882
883                                 if (use_original) {
884                                         normal_short_to_float_v3(no_buf, no_s);
885                                         no = no_buf;
886                                 }
887                                 else {
888                                         if (vd.no) {
889                                                 normal_short_to_float_v3(no_buf, vd.no);
890                                                 no = no_buf;
891                                         }
892                                         else {
893                                                 no = vd.fno;
894                                         }
895                                 }
896
897                                 flip_index = (dot_v3v3(ss->cache->view_normal, no) <= 0.0f);
898                                 if (area_cos)
899                                         add_v3_v3(private_co[flip_index], co);
900                                 if (area_nos)
901                                         add_v3_v3(private_no[flip_index], no);
902                                 private_count[flip_index] += 1;
903                         }
904                 }
905                 BKE_pbvh_vertex_iter_end;
906         }
907
908         BLI_mutex_lock(&data->mutex);
909
910         /* for flatten center */
911         if (area_cos) {
912                 add_v3_v3(area_cos[0], private_co[0]);
913                 add_v3_v3(area_cos[1], private_co[1]);
914         }
915
916         /* for area normal */
917         if (area_nos) {
918                 add_v3_v3(area_nos[0], private_no[0]);
919                 add_v3_v3(area_nos[1], private_no[1]);
920         }
921
922         /* weights */
923         data->count[0] += private_count[0];
924         data->count[1] += private_count[1];
925
926         BLI_mutex_unlock(&data->mutex);
927 }
928
929 static void calc_area_center(
930         Sculpt *sd, Object *ob,
931         PBVHNode **nodes, int totnode,
932         float r_area_co[3])
933 {
934         const Brush *brush = BKE_paint_brush(&sd->paint);
935         SculptSession *ss = ob->sculpt;
936         const bool has_bm_orco = ss->bm && sculpt_stroke_is_dynamic_topology(ss, brush);
937         int n;
938
939         /* 0=towards view, 1=flipped */
940         float area_cos[2][3] = {{0.0f}};
941
942         int count[2] = {0};
943
944         /* Intentionally set 'sd' to NULL since we share logic with vertex paint. */
945         SculptThreadedTaskData data = {
946                 .sd = NULL, .ob = ob, .brush = brush, .nodes = nodes, .totnode = totnode,
947                 .has_bm_orco = has_bm_orco, .area_cos = area_cos, .area_nos = NULL, .count = count,
948         };
949         BLI_mutex_init(&data.mutex);
950
951         ParallelRangeSettings settings;
952         BLI_parallel_range_settings_defaults(&settings);
953         settings.use_threading = ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT);
954         BLI_task_parallel_range(
955                     0, totnode,
956                     &data,
957                     calc_area_normal_and_center_task_cb,
958                     &settings);
959
960         BLI_mutex_end(&data.mutex);
961
962         /* for flatten center */
963         for (n = 0; n < ARRAY_SIZE(area_cos); n++) {
964                 if (count[n] != 0) {
965                         mul_v3_v3fl(r_area_co, area_cos[n], 1.0f / count[n]);
966                         break;
967                 }
968         }
969         if (n == 2) {
970                 zero_v3(r_area_co);
971         }
972 }
973
974 static void calc_area_normal(
975         Sculpt *sd, Object *ob,
976         PBVHNode **nodes, int totnode,
977         float r_area_no[3])
978 {
979         const Brush *brush = BKE_paint_brush(&sd->paint);
980         bool use_threading = (sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT;
981         sculpt_pbvh_calc_area_normal(brush, ob, nodes, totnode, use_threading, r_area_no);
982 }
983
984 /* expose 'calc_area_normal' externally. */
985 void sculpt_pbvh_calc_area_normal(
986         const Brush *brush, Object *ob,
987         PBVHNode **nodes, int totnode,
988         bool use_threading,
989         float r_area_no[3])
990 {
991         SculptSession *ss = ob->sculpt;
992         const bool has_bm_orco = ss->bm && sculpt_stroke_is_dynamic_topology(ss, brush);
993
994         /* 0=towards view, 1=flipped */
995         float area_nos[2][3] = {{0.0f}};
996
997         int count[2] = {0};
998
999         /* Intentionally set 'sd' to NULL since this is used for vertex paint too. */
1000         SculptThreadedTaskData data = {
1001                 .sd = NULL, .ob = ob, .brush = brush, .nodes = nodes, .totnode = totnode,
1002                 .has_bm_orco = has_bm_orco, .area_cos = NULL, .area_nos = area_nos, .count = count,
1003         };
1004         BLI_mutex_init(&data.mutex);
1005
1006         ParallelRangeSettings settings;
1007         BLI_parallel_range_settings_defaults(&settings);
1008         settings.use_threading = use_threading;
1009         BLI_task_parallel_range(
1010                     0, totnode,
1011                     &data,
1012                     calc_area_normal_and_center_task_cb,
1013                     &settings);
1014
1015         BLI_mutex_end(&data.mutex);
1016
1017         /* for area normal */
1018         for (int i = 0; i < ARRAY_SIZE(area_nos); i++) {
1019                 if (normalize_v3_v3(r_area_no, area_nos[i]) != 0.0f) {
1020                         break;
1021                 }
1022         }
1023 }
1024
1025
1026 /* this calculates flatten center and area normal together,
1027  * amortizing the memory bandwidth and loop overhead to calculate both at the same time */
1028 static void calc_area_normal_and_center(
1029         Sculpt *sd, Object *ob,
1030         PBVHNode **nodes, int totnode,
1031         float r_area_no[3], float r_area_co[3])
1032 {
1033         const Brush *brush = BKE_paint_brush(&sd->paint);
1034         SculptSession *ss = ob->sculpt;
1035         const bool has_bm_orco = ss->bm && sculpt_stroke_is_dynamic_topology(ss, brush);
1036         int n;
1037
1038         /* 0=towards view, 1=flipped */
1039         float area_cos[2][3] = {{0.0f}};
1040         float area_nos[2][3] = {{0.0f}};
1041
1042         int count[2] = {0};
1043
1044         /* Intentionally set 'sd' to NULL since this is used for vertex paint too. */
1045         SculptThreadedTaskData data = {
1046                 .sd = NULL, .ob = ob, .brush = brush, .nodes = nodes, .totnode = totnode,
1047                 .has_bm_orco = has_bm_orco, .area_cos = area_cos, .area_nos = area_nos, .count = count,
1048         };
1049         BLI_mutex_init(&data.mutex);
1050
1051         ParallelRangeSettings settings;
1052         BLI_parallel_range_settings_defaults(&settings);
1053         settings.use_threading = ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT);
1054         BLI_task_parallel_range(
1055                     0, totnode,
1056                     &data,
1057                     calc_area_normal_and_center_task_cb,
1058                     &settings);
1059
1060         BLI_mutex_end(&data.mutex);
1061
1062         /* for flatten center */
1063         for (n = 0; n < ARRAY_SIZE(area_cos); n++) {
1064                 if (count[n] != 0) {
1065                         mul_v3_v3fl(r_area_co, area_cos[n], 1.0f / count[n]);
1066                         break;
1067                 }
1068         }
1069         if (n == 2) {
1070                 zero_v3(r_area_co);
1071         }
1072
1073         /* for area normal */
1074         for (n = 0; n < ARRAY_SIZE(area_nos); n++) {
1075                 if (normalize_v3_v3(r_area_no, area_nos[n]) != 0.0f) {
1076                         break;
1077                 }
1078         }
1079 }
1080
1081 /** \} */
1082
1083
1084 /* Return modified brush strength. Includes the direction of the brush, positive
1085  * values pull vertices, negative values push. Uses tablet pressure and a
1086  * special multiplier found experimentally to scale the strength factor. */
1087 static float brush_strength(
1088         const Sculpt *sd, const StrokeCache *cache,
1089         const float feather, const UnifiedPaintSettings *ups)
1090 {
1091         const Scene *scene = cache->vc->scene;
1092         const Brush *brush = BKE_paint_brush((Paint *)&sd->paint);
1093
1094         /* Primary strength input; square it to make lower values more sensitive */
1095         const float root_alpha = BKE_brush_alpha_get(scene, brush);
1096         float alpha        = root_alpha * root_alpha;
1097         float dir          = (brush->flag & BRUSH_DIR_IN) ? -1 : 1;
1098         float pressure     = BKE_brush_use_alpha_pressure(scene, brush) ? cache->pressure : 1;
1099         float pen_flip     = cache->pen_flip ? -1 : 1;
1100         float invert       = cache->invert ? -1 : 1;
1101         float overlap       = ups->overlap_factor;
1102         /* spacing is integer percentage of radius, divide by 50 to get
1103          * normalized diameter */
1104
1105         float flip         = dir * invert * pen_flip;
1106
1107         switch (brush->sculpt_tool) {
1108                 case SCULPT_TOOL_CLAY:
1109                 case SCULPT_TOOL_CLAY_STRIPS:
1110                 case SCULPT_TOOL_DRAW:
1111                 case SCULPT_TOOL_LAYER:
1112                         return alpha * flip * pressure * overlap * feather;
1113
1114                 case SCULPT_TOOL_MASK:
1115                         overlap = (1 + overlap) / 2;
1116                         switch ((BrushMaskTool)brush->mask_tool) {
1117                                 case BRUSH_MASK_DRAW:
1118                                         return alpha * flip * pressure * overlap * feather;
1119                                 case BRUSH_MASK_SMOOTH:
1120                                         return alpha * pressure * feather;
1121                         }
1122                         BLI_assert(!"Not supposed to happen");
1123                         return 0.0f;
1124
1125                 case SCULPT_TOOL_CREASE:
1126                 case SCULPT_TOOL_BLOB:
1127                         return alpha * flip * pressure * overlap * feather;
1128
1129                 case SCULPT_TOOL_INFLATE:
1130                         if (flip > 0) {
1131                                 return 0.250f * alpha * flip * pressure * overlap * feather;
1132                         }
1133                         else {
1134                                 return 0.125f * alpha * flip * pressure * overlap * feather;
1135                         }
1136
1137                 case SCULPT_TOOL_FILL:
1138                 case SCULPT_TOOL_SCRAPE:
1139                 case SCULPT_TOOL_FLATTEN:
1140                         if (flip > 0) {
1141                                 overlap = (1 + overlap) / 2;
1142                                 return alpha * flip * pressure * overlap * feather;
1143                         }
1144                         else {
1145                                 /* reduce strength for DEEPEN, PEAKS, and CONTRAST */
1146                                 return 0.5f * alpha * flip * pressure * overlap * feather;
1147                         }
1148
1149                 case SCULPT_TOOL_SMOOTH:
1150                         return alpha * pressure * feather;
1151
1152                 case SCULPT_TOOL_PINCH:
1153                         if (flip > 0) {
1154                                 return alpha * flip * pressure * overlap * feather;
1155                         }
1156                         else {
1157                                 return 0.25f * alpha * flip * pressure * overlap * feather;
1158                         }
1159
1160                 case SCULPT_TOOL_NUDGE:
1161                         overlap = (1 + overlap) / 2;
1162                         return alpha * pressure * overlap * feather;
1163
1164                 case SCULPT_TOOL_THUMB:
1165                         return alpha * pressure * feather;
1166
1167                 case SCULPT_TOOL_SNAKE_HOOK:
1168                         return root_alpha * feather;
1169
1170                 case SCULPT_TOOL_GRAB:
1171                         return root_alpha * feather;
1172
1173                 case SCULPT_TOOL_ROTATE:
1174                         return alpha * pressure * feather;
1175
1176                 default:
1177                         return 0;
1178         }
1179 }
1180
1181 /* Return a multiplier for brush strength on a particular vertex. */
1182 float tex_strength(SculptSession *ss, const Brush *br,
1183                    const float brush_point[3],
1184                    const float len,
1185                    const short vno[3],
1186                    const float fno[3],
1187                    const float mask,
1188                    const int thread_id)
1189 {
1190         StrokeCache *cache = ss->cache;
1191         const Scene *scene = cache->vc->scene;
1192         const MTex *mtex = &br->mtex;
1193         float avg = 1;
1194         float rgba[4];
1195         float point[3];
1196
1197         sub_v3_v3v3(point, brush_point, cache->plane_offset);
1198
1199         if (!mtex->tex) {
1200                 avg = 1;
1201         }
1202         else if (mtex->brush_map_mode == MTEX_MAP_MODE_3D) {
1203                 /* Get strength by feeding the vertex
1204                  * location directly into a texture */
1205                 avg = BKE_brush_sample_tex_3d(scene, br, point, rgba, 0, ss->tex_pool);
1206         }
1207         else if (ss->texcache) {
1208                 float symm_point[3], point_2d[2];
1209                 float x = 0.0f, y = 0.0f; /* Quite warnings */
1210
1211                 /* if the active area is being applied for symmetry, flip it
1212                  * across the symmetry axis and rotate it back to the original
1213                  * position in order to project it. This insures that the
1214                  * brush texture will be oriented correctly. */
1215
1216                 flip_v3_v3(symm_point, point, cache->mirror_symmetry_pass);
1217
1218                 if (cache->radial_symmetry_pass)
1219                         mul_m4_v3(cache->symm_rot_mat_inv, symm_point);
1220
1221                 ED_view3d_project_float_v2_m4(cache->vc->ar, symm_point, point_2d, cache->projection_mat);
1222
1223                 /* still no symmetry supported for other paint modes.
1224                  * Sculpt does it DIY */
1225                 if (mtex->brush_map_mode == MTEX_MAP_MODE_AREA) {
1226                         /* Similar to fixed mode, but projects from brush angle
1227                          * rather than view direction */
1228
1229                         mul_m4_v3(cache->brush_local_mat, symm_point);
1230
1231                         x = symm_point[0];
1232                         y = symm_point[1];
1233
1234                         x *= br->mtex.size[0];
1235                         y *= br->mtex.size[1];
1236
1237                         x += br->mtex.ofs[0];
1238                         y += br->mtex.ofs[1];
1239
1240                         avg = paint_get_tex_pixel(&br->mtex, x, y, ss->tex_pool, thread_id);
1241
1242                         avg += br->texture_sample_bias;
1243                 }
1244                 else {
1245                         const float point_3d[3] = {point_2d[0], point_2d[1], 0.0f};
1246                         avg = BKE_brush_sample_tex_3d(scene, br, point_3d, rgba, 0, ss->tex_pool);
1247                 }
1248         }
1249
1250         /* Falloff curve */
1251         avg *= BKE_brush_curve_strength(br, len, cache->radius);
1252
1253         avg *= frontface(br, cache->view_normal, vno, fno);
1254
1255         /* Paint mask */
1256         avg *= 1.0f - mask;
1257
1258         return avg;
1259 }
1260
1261 /* Test AABB against sphere */
1262 bool sculpt_search_sphere_cb(PBVHNode *node, void *data_v)
1263 {
1264         SculptSearchSphereData *data = data_v;
1265         float *center = data->ss->cache->location, nearest[3];
1266         float t[3], bb_min[3], bb_max[3];
1267         int i;
1268
1269         if (data->original)
1270                 BKE_pbvh_node_get_original_BB(node, bb_min, bb_max);
1271         else
1272                 BKE_pbvh_node_get_BB(node, bb_min, bb_max);
1273
1274         for (i = 0; i < 3; ++i) {
1275                 if (bb_min[i] > center[i])
1276                         nearest[i] = bb_min[i];
1277                 else if (bb_max[i] < center[i])
1278                         nearest[i] = bb_max[i];
1279                 else
1280                         nearest[i] = center[i];
1281         }
1282
1283         sub_v3_v3v3(t, center, nearest);
1284
1285         return len_squared_v3(t) < data->radius_squared;
1286 }
1287
1288 /* 2D projection (distance to line). */
1289 bool sculpt_search_circle_cb(PBVHNode *node, void *data_v)
1290 {
1291         SculptSearchCircleData *data = data_v;
1292         float bb_min[3], bb_max[3];
1293
1294         if (data->original)
1295                 BKE_pbvh_node_get_original_BB(node, bb_min, bb_max);
1296         else
1297                 BKE_pbvh_node_get_BB(node, bb_min, bb_min);
1298
1299         float dummy_co[3], dummy_depth;
1300         const float dist_sq = dist_squared_ray_to_aabb_v3(
1301                 data->dist_ray_to_aabb_precalc, bb_min, bb_max, dummy_co, &dummy_depth);
1302
1303         return dist_sq < data->radius_squared || 1;
1304 }
1305
1306 /* Handles clipping against a mirror modifier and SCULPT_LOCK axis flags */
1307 static void sculpt_clip(Sculpt *sd, SculptSession *ss, float co[3], const float val[3])
1308 {
1309         int i;
1310
1311         for (i = 0; i < 3; ++i) {
1312                 if (sd->flags & (SCULPT_LOCK_X << i))
1313                         continue;
1314
1315                 if ((ss->cache->flag & (CLIP_X << i)) && (fabsf(co[i]) <= ss->cache->clip_tolerance[i]))
1316                         co[i] = 0.0f;
1317                 else
1318                         co[i] = val[i];
1319         }
1320 }
1321
1322 static PBVHNode **sculpt_pbvh_gather_generic(
1323         Object *ob, Sculpt *sd, const Brush *brush, bool use_original, float radius_scale, int *r_totnode)
1324 {
1325         SculptSession *ss = ob->sculpt;
1326         PBVHNode **nodes = NULL;
1327
1328         /* Build a list of all nodes that are potentially within the brush's area of influence */
1329         if (brush->falloff_shape == PAINT_FALLOFF_SHAPE_SPHERE) {
1330                 SculptSearchSphereData data = {
1331                         .ss = ss,
1332                         .sd = sd,
1333                         .radius_squared = SQUARE(ss->cache->radius * radius_scale),
1334                         .original = use_original,
1335                 };
1336                 BKE_pbvh_search_gather(ss->pbvh, sculpt_search_sphere_cb, &data, &nodes, r_totnode);
1337         }
1338         else {
1339                 struct DistRayAABB_Precalc dist_ray_to_aabb_precalc;
1340                 dist_squared_ray_to_aabb_v3_precalc(&dist_ray_to_aabb_precalc, ss->cache->location, ss->cache->view_normal);
1341                 SculptSearchCircleData data = {
1342                         .ss = ss,
1343                         .sd = sd,
1344                         .radius_squared = SQUARE(ss->cache->radius * radius_scale),
1345                         .original = use_original,
1346                         .dist_ray_to_aabb_precalc = &dist_ray_to_aabb_precalc,
1347                 };
1348                 BKE_pbvh_search_gather(ss->pbvh, sculpt_search_circle_cb, &data, &nodes, r_totnode);
1349         }
1350         return nodes;
1351 }
1352
1353 /* Calculate primary direction of movement for many brushes */
1354 static void calc_sculpt_normal(
1355         Sculpt *sd, Object *ob,
1356         PBVHNode **nodes, int totnode,
1357         float r_area_no[3])
1358 {
1359         const Brush *brush = BKE_paint_brush(&sd->paint);
1360         const SculptSession *ss = ob->sculpt;
1361
1362         switch (brush->sculpt_plane) {
1363                 case SCULPT_DISP_DIR_VIEW:
1364                         copy_v3_v3(r_area_no, ss->cache->true_view_normal);
1365                         break;
1366
1367                 case SCULPT_DISP_DIR_X:
1368                         ARRAY_SET_ITEMS(r_area_no, 1, 0, 0);
1369                         break;
1370
1371                 case SCULPT_DISP_DIR_Y:
1372                         ARRAY_SET_ITEMS(r_area_no, 0, 1, 0);
1373                         break;
1374
1375                 case SCULPT_DISP_DIR_Z:
1376                         ARRAY_SET_ITEMS(r_area_no, 0, 0, 1);
1377                         break;
1378
1379                 case SCULPT_DISP_DIR_AREA:
1380                         calc_area_normal(sd, ob, nodes, totnode, r_area_no);
1381                         break;
1382
1383                 default:
1384                         break;
1385         }
1386 }
1387
1388 static void update_sculpt_normal(Sculpt *sd, Object *ob,
1389                                  PBVHNode **nodes, int totnode)
1390 {
1391         const Brush *brush = BKE_paint_brush(&sd->paint);
1392         StrokeCache *cache = ob->sculpt->cache;
1393
1394         if (cache->mirror_symmetry_pass == 0 &&
1395             cache->radial_symmetry_pass == 0 &&
1396             (cache->first_time || !(brush->flag & BRUSH_ORIGINAL_NORMAL)))
1397         {
1398                 calc_sculpt_normal(sd, ob, nodes, totnode, cache->sculpt_normal);
1399                 if (brush->falloff_shape == PAINT_FALLOFF_SHAPE_TUBE) {
1400                         project_plane_v3_v3v3(cache->sculpt_normal, cache->sculpt_normal, cache->view_normal);
1401                         normalize_v3(cache->sculpt_normal);
1402                 }
1403                 copy_v3_v3(cache->sculpt_normal_symm, cache->sculpt_normal);
1404         }
1405         else {
1406                 copy_v3_v3(cache->sculpt_normal_symm, cache->sculpt_normal);
1407                 flip_v3(cache->sculpt_normal_symm, cache->mirror_symmetry_pass);
1408                 mul_m4_v3(cache->symm_rot_mat, cache->sculpt_normal_symm);
1409         }
1410 }
1411
1412 static void calc_local_y(ViewContext *vc, const float center[3], float y[3])
1413 {
1414         Object *ob = vc->obact;
1415         float loc[3], mval_f[2] = {0.0f, 1.0f};
1416         float zfac;
1417
1418         mul_v3_m4v3(loc, ob->imat, center);
1419         zfac = ED_view3d_calc_zfac(vc->rv3d, loc, NULL);
1420
1421         ED_view3d_win_to_delta(vc->ar, mval_f, y, zfac);
1422         normalize_v3(y);
1423
1424         add_v3_v3(y, ob->loc);
1425         mul_m4_v3(ob->imat, y);
1426 }
1427
1428 static void calc_brush_local_mat(const Brush *brush, Object *ob,
1429                                  float local_mat[4][4])
1430 {
1431         const StrokeCache *cache = ob->sculpt->cache;
1432         float tmat[4][4];
1433         float mat[4][4];
1434         float scale[4][4];
1435         float angle, v[3];
1436         float up[3];
1437
1438         /* Ensure ob->imat is up to date */
1439         invert_m4_m4(ob->imat, ob->obmat);
1440
1441         /* Initialize last column of matrix */
1442         mat[0][3] = 0;
1443         mat[1][3] = 0;
1444         mat[2][3] = 0;
1445         mat[3][3] = 1;
1446
1447         /* Get view's up vector in object-space */
1448         calc_local_y(cache->vc, cache->location, up);
1449
1450         /* Calculate the X axis of the local matrix */
1451         cross_v3_v3v3(v, up, cache->sculpt_normal);
1452         /* Apply rotation (user angle, rake, etc.) to X axis */
1453         angle = brush->mtex.rot - cache->special_rotation;
1454         rotate_v3_v3v3fl(mat[0], v, cache->sculpt_normal, angle);
1455
1456         /* Get other axes */
1457         cross_v3_v3v3(mat[1], cache->sculpt_normal, mat[0]);
1458         copy_v3_v3(mat[2], cache->sculpt_normal);
1459
1460         /* Set location */
1461         copy_v3_v3(mat[3], cache->location);
1462
1463         /* Scale by brush radius */
1464         normalize_m4(mat);
1465         scale_m4_fl(scale, cache->radius);
1466         mul_m4_m4m4(tmat, mat, scale);
1467
1468         /* Return inverse (for converting from modelspace coords to local
1469          * area coords) */
1470         invert_m4_m4(local_mat, tmat);
1471 }
1472
1473 static void update_brush_local_mat(Sculpt *sd, Object *ob)
1474 {
1475         StrokeCache *cache = ob->sculpt->cache;
1476
1477         if (cache->mirror_symmetry_pass == 0 &&
1478             cache->radial_symmetry_pass == 0)
1479         {
1480                 calc_brush_local_mat(BKE_paint_brush(&sd->paint), ob,
1481                                      cache->brush_local_mat);
1482         }
1483 }
1484
1485 /* For the smooth brush, uses the neighboring vertices around vert to calculate
1486  * a smoothed location for vert. Skips corner vertices (used by only one
1487  * polygon.) */
1488 static void neighbor_average(SculptSession *ss, float avg[3], unsigned vert)
1489 {
1490         const MeshElemMap *vert_map = &ss->pmap[vert];
1491         const MVert *mvert = ss->mvert;
1492         float (*deform_co)[3] = ss->deform_cos;
1493
1494         /* Don't modify corner vertices */
1495         if (vert_map->count > 1) {
1496                 int i, total = 0;
1497
1498                 zero_v3(avg);
1499
1500                 for (i = 0; i < vert_map->count; i++) {
1501                         const MPoly *p = &ss->mpoly[vert_map->indices[i]];
1502                         unsigned f_adj_v[2];
1503
1504                         if (poly_get_adj_loops_from_vert(p, ss->mloop, vert, f_adj_v) != -1) {
1505                                 int j;
1506                                 for (j = 0; j < ARRAY_SIZE(f_adj_v); j += 1) {
1507                                         if (vert_map->count != 2 || ss->pmap[f_adj_v[j]].count <= 2) {
1508                                                 add_v3_v3(avg, deform_co ? deform_co[f_adj_v[j]] :
1509                                                           mvert[f_adj_v[j]].co);
1510
1511                                                 total++;
1512                                         }
1513                                 }
1514                         }
1515                 }
1516
1517                 if (total > 0) {
1518                         mul_v3_fl(avg, 1.0f / total);
1519                         return;
1520                 }
1521         }
1522
1523         copy_v3_v3(avg, deform_co ? deform_co[vert] : mvert[vert].co);
1524 }
1525
1526 /* Similar to neighbor_average(), but returns an averaged mask value
1527  * instead of coordinate. Also does not restrict based on border or
1528  * corner vertices. */
1529 static float neighbor_average_mask(SculptSession *ss, unsigned vert)
1530 {
1531         const float *vmask = ss->vmask;
1532         float avg = 0;
1533         int i, total = 0;
1534
1535         for (i = 0; i < ss->pmap[vert].count; i++) {
1536                 const MPoly *p = &ss->mpoly[ss->pmap[vert].indices[i]];
1537                 unsigned f_adj_v[2];
1538
1539                 if (poly_get_adj_loops_from_vert(p, ss->mloop, vert, f_adj_v) != -1) {
1540                         int j;
1541                         for (j = 0; j < ARRAY_SIZE(f_adj_v); j += 1) {
1542                                 avg += vmask[f_adj_v[j]];
1543                                 total++;
1544                         }
1545                 }
1546         }
1547
1548         if (total > 0)
1549                 return avg / (float)total;
1550         else
1551                 return vmask[vert];
1552 }
1553
1554 /* Same logic as neighbor_average(), but for bmesh rather than mesh */
1555 static void bmesh_neighbor_average(float avg[3], BMVert *v)
1556 {
1557         /* logic for 3 or more is identical */
1558         const int vfcount = BM_vert_face_count_at_most(v, 3);
1559
1560         /* Don't modify corner vertices */
1561         if (vfcount > 1) {
1562                 BMIter liter;
1563                 BMLoop *l;
1564                 int i, total = 0;
1565
1566                 zero_v3(avg);
1567
1568                 BM_ITER_ELEM (l, &liter, v, BM_LOOPS_OF_VERT) {
1569                         const BMVert *adj_v[2] = {l->prev->v, l->next->v};
1570
1571                         for (i = 0; i < ARRAY_SIZE(adj_v); i++) {
1572                                 const BMVert *v_other = adj_v[i];
1573                                 if (vfcount != 2 || BM_vert_face_count_at_most(v_other, 2) <= 2) {
1574                                         add_v3_v3(avg, v_other->co);
1575                                         total++;
1576                                 }
1577                         }
1578                 }
1579
1580                 if (total > 0) {
1581                         mul_v3_fl(avg, 1.0f / total);
1582                         return;
1583                 }
1584         }
1585
1586         copy_v3_v3(avg, v->co);
1587 }
1588
1589 /* Same logic as neighbor_average_mask(), but for bmesh rather than mesh */
1590 static float bmesh_neighbor_average_mask(BMVert *v, const int cd_vert_mask_offset)
1591 {
1592         BMIter liter;
1593         BMLoop *l;
1594         float avg = 0;
1595         int i, total = 0;
1596
1597         BM_ITER_ELEM (l, &liter, v, BM_LOOPS_OF_VERT) {
1598                 /* skip this vertex */
1599                 const BMVert *adj_v[2] = {l->prev->v, l->next->v};
1600
1601                 for (i = 0; i < ARRAY_SIZE(adj_v); i++) {
1602                         const BMVert *v_other = adj_v[i];
1603                         const float *vmask = BM_ELEM_CD_GET_VOID_P(v_other, cd_vert_mask_offset);
1604                         avg += (*vmask);
1605                         total++;
1606                 }
1607         }
1608
1609         if (total > 0) {
1610                 return avg / (float)total;
1611         }
1612         else {
1613                 const float *vmask = BM_ELEM_CD_GET_VOID_P(v, cd_vert_mask_offset);
1614                 return (*vmask);
1615         }
1616 }
1617
1618 /* Note: uses after-struct allocated mem to store actual cache... */
1619 typedef struct SculptDoBrushSmoothGridDataChunk {
1620         size_t tmpgrid_size;
1621 } SculptDoBrushSmoothGridDataChunk;
1622
1623 typedef struct {
1624         SculptSession *ss;
1625         const float *ray_start, *ray_normal;
1626         bool hit;
1627         float depth;
1628         bool original;
1629 } SculptRaycastData;
1630
1631 typedef struct {
1632         const float *ray_start, *ray_normal;
1633         bool hit;
1634         float depth;
1635         float detail;
1636 } SculptDetailRaycastData;
1637
1638 typedef struct {
1639         SculptSession *ss;
1640         const float *ray_start, *ray_normal;
1641         bool hit;
1642         float depth;
1643         float dist_sq_to_ray;
1644         bool original;
1645 } SculptFindNearestToRayData;
1646
1647 static void do_smooth_brush_mesh_task_cb_ex(
1648         void *__restrict userdata,
1649         const int n,
1650         const ParallelRangeTLS *__restrict tls)
1651 {
1652         SculptThreadedTaskData *data = userdata;
1653         SculptSession *ss = data->ob->sculpt;
1654         Sculpt *sd = data->sd;
1655         const Brush *brush = data->brush;
1656         const bool smooth_mask = data->smooth_mask;
1657         float bstrength = data->strength;
1658
1659         PBVHVertexIter vd;
1660
1661         CLAMP(bstrength, 0.0f, 1.0f);
1662
1663         SculptBrushTest test;
1664         SculptBrushTestFn sculpt_brush_test_sq_fn =
1665                 sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape);
1666
1667         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
1668         {
1669                 if (sculpt_brush_test_sq_fn(&test, vd.co)) {
1670                         const float fade = bstrength * tex_strength(
1671                                 ss, brush, vd.co, sqrtf(test.dist),
1672                                 vd.no, vd.fno, smooth_mask ? 0.0f : (vd.mask ? *vd.mask : 0.0f), tls->thread_id);
1673                         if (smooth_mask) {
1674                                 float val = neighbor_average_mask(ss, vd.vert_indices[vd.i]) - *vd.mask;
1675                                 val *= fade * bstrength;
1676                                 *vd.mask += val;
1677                                 CLAMP(*vd.mask, 0.0f, 1.0f);
1678                         }
1679                         else {
1680                                 float avg[3], val[3];
1681
1682                                 neighbor_average(ss, avg, vd.vert_indices[vd.i]);
1683                                 sub_v3_v3v3(val, avg, vd.co);
1684
1685                                 madd_v3_v3v3fl(val, vd.co, val, fade);
1686
1687                                 sculpt_clip(sd, ss, vd.co, val);
1688                         }
1689
1690                         if (vd.mvert)
1691                                 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
1692                 }
1693         }
1694         BKE_pbvh_vertex_iter_end;
1695 }
1696
1697 static void do_smooth_brush_bmesh_task_cb_ex(
1698         void *__restrict userdata,
1699         const int n,
1700         const ParallelRangeTLS *__restrict tls)
1701 {
1702         SculptThreadedTaskData *data = userdata;
1703         SculptSession *ss = data->ob->sculpt;
1704         Sculpt *sd = data->sd;
1705         const Brush *brush = data->brush;
1706         const bool smooth_mask = data->smooth_mask;
1707         float bstrength = data->strength;
1708
1709         PBVHVertexIter vd;
1710
1711         CLAMP(bstrength, 0.0f, 1.0f);
1712
1713         SculptBrushTest test;
1714         SculptBrushTestFn sculpt_brush_test_sq_fn =
1715                 sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape);
1716
1717         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
1718         {
1719                 if (sculpt_brush_test_sq_fn(&test, vd.co)) {
1720                         const float fade = bstrength * tex_strength(
1721                                 ss, brush, vd.co, sqrtf(test.dist),
1722                                 vd.no, vd.fno, smooth_mask ? 0.0f : *vd.mask, tls->thread_id);
1723                         if (smooth_mask) {
1724                                 float val = bmesh_neighbor_average_mask(vd.bm_vert, vd.cd_vert_mask_offset) - *vd.mask;
1725                                 val *= fade * bstrength;
1726                                 *vd.mask += val;
1727                                 CLAMP(*vd.mask, 0.0f, 1.0f);
1728                         }
1729                         else {
1730                                 float avg[3], val[3];
1731
1732                                 bmesh_neighbor_average(avg, vd.bm_vert);
1733                                 sub_v3_v3v3(val, avg, vd.co);
1734
1735                                 madd_v3_v3v3fl(val, vd.co, val, fade);
1736
1737                                 sculpt_clip(sd, ss, vd.co, val);
1738                         }
1739
1740                         if (vd.mvert)
1741                                 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
1742                 }
1743         }
1744         BKE_pbvh_vertex_iter_end;
1745 }
1746
1747 static void do_smooth_brush_multires_task_cb_ex(
1748         void *__restrict userdata,
1749         const int n,
1750         const ParallelRangeTLS *__restrict tls)
1751 {
1752         SculptThreadedTaskData *data = userdata;
1753         SculptDoBrushSmoothGridDataChunk *data_chunk = tls->userdata_chunk;
1754         SculptSession *ss = data->ob->sculpt;
1755         Sculpt *sd = data->sd;
1756         const Brush *brush = data->brush;
1757         const bool smooth_mask = data->smooth_mask;
1758         float bstrength = data->strength;
1759
1760         CCGElem **griddata, *gddata;
1761         CCGKey key;
1762
1763         float (*tmpgrid_co)[3] = NULL;
1764         float tmprow_co[2][3];
1765         float *tmpgrid_mask = NULL;
1766         float tmprow_mask[2];
1767
1768         BLI_bitmap * const *grid_hidden;
1769         int *grid_indices, totgrid, gridsize;
1770         int i, x, y;
1771
1772         SculptBrushTest test;
1773         SculptBrushTestFn sculpt_brush_test_sq_fn =
1774                 sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape);
1775
1776         CLAMP(bstrength, 0.0f, 1.0f);
1777
1778         BKE_pbvh_node_get_grids(ss->pbvh, data->nodes[n], &grid_indices, &totgrid, NULL, &gridsize, &griddata);
1779         BKE_pbvh_get_grid_key(ss->pbvh, &key);
1780
1781         grid_hidden = BKE_pbvh_grid_hidden(ss->pbvh);
1782
1783         if (smooth_mask)
1784                 tmpgrid_mask = (void *)(data_chunk + 1);
1785         else
1786                 tmpgrid_co = (void *)(data_chunk + 1);
1787
1788         for (i = 0; i < totgrid; i++) {
1789                 int gi = grid_indices[i];
1790                 const BLI_bitmap *gh = grid_hidden[gi];
1791                 gddata = griddata[gi];
1792
1793                 if (smooth_mask)
1794                         memset(tmpgrid_mask, 0, data_chunk->tmpgrid_size);
1795                 else
1796                         memset(tmpgrid_co, 0, data_chunk->tmpgrid_size);
1797
1798                 for (y = 0; y < gridsize - 1; y++) {
1799                         const int v = y * gridsize;
1800                         if (smooth_mask) {
1801                                 tmprow_mask[0] = (*CCG_elem_offset_mask(&key, gddata, v) +
1802                                                   *CCG_elem_offset_mask(&key, gddata, v + gridsize));
1803                         }
1804                         else {
1805                                 add_v3_v3v3(tmprow_co[0],
1806                                             CCG_elem_offset_co(&key, gddata, v),
1807                                             CCG_elem_offset_co(&key, gddata, v + gridsize));
1808                         }
1809
1810                         for (x = 0; x < gridsize - 1; x++) {
1811                                 const int v1 = x + y * gridsize;
1812                                 const int v2 = v1 + 1;
1813                                 const int v3 = v1 + gridsize;
1814                                 const int v4 = v3 + 1;
1815
1816                                 if (smooth_mask) {
1817                                         float tmp;
1818
1819                                         tmprow_mask[(x + 1) % 2] = (*CCG_elem_offset_mask(&key, gddata, v2) +
1820                                                                     *CCG_elem_offset_mask(&key, gddata, v4));
1821                                         tmp = tmprow_mask[(x + 1) % 2] + tmprow_mask[x % 2];
1822
1823                                         tmpgrid_mask[v1] += tmp;
1824                                         tmpgrid_mask[v2] += tmp;
1825                                         tmpgrid_mask[v3] += tmp;
1826                                         tmpgrid_mask[v4] += tmp;
1827                                 }
1828                                 else {
1829                                         float tmp[3];
1830
1831                                         add_v3_v3v3(tmprow_co[(x + 1) % 2],
1832                                                     CCG_elem_offset_co(&key, gddata, v2),
1833                                                     CCG_elem_offset_co(&key, gddata, v4));
1834                                         add_v3_v3v3(tmp, tmprow_co[(x + 1) % 2], tmprow_co[x % 2]);
1835
1836                                         add_v3_v3(tmpgrid_co[v1], tmp);
1837                                         add_v3_v3(tmpgrid_co[v2], tmp);
1838                                         add_v3_v3(tmpgrid_co[v3], tmp);
1839                                         add_v3_v3(tmpgrid_co[v4], tmp);
1840                                 }
1841                         }
1842                 }
1843
1844                 /* blend with existing coordinates */
1845                 for (y = 0; y < gridsize; y++) {
1846                         for (x = 0; x < gridsize; x++) {
1847                                 float *co;
1848                                 const float *fno;
1849                                 float *mask;
1850                                 const int index = y * gridsize + x;
1851
1852                                 if (gh) {
1853                                         if (BLI_BITMAP_TEST(gh, index))
1854                                                 continue;
1855                                 }
1856
1857                                 co = CCG_elem_offset_co(&key, gddata, index);
1858                                 fno = CCG_elem_offset_no(&key, gddata, index);
1859                                 mask = CCG_elem_offset_mask(&key, gddata, index);
1860
1861                                 if (sculpt_brush_test_sq_fn(&test, co)) {
1862                                         const float strength_mask = (smooth_mask ? 0.0f : *mask);
1863                                         const float fade = bstrength * tex_strength(
1864                                                 ss, brush, co, sqrtf(test.dist),
1865                                                 NULL, fno, strength_mask, tls->thread_id);
1866                                         float f = 1.0f / 16.0f;
1867
1868                                         if (x == 0 || x == gridsize - 1)
1869                                                 f *= 2.0f;
1870
1871                                         if (y == 0 || y == gridsize - 1)
1872                                                 f *= 2.0f;
1873
1874                                         if (smooth_mask) {
1875                                                 *mask += ((tmpgrid_mask[index] * f) - *mask) * fade;
1876                                         }
1877                                         else {
1878                                                 float *avg = tmpgrid_co[index];
1879                                                 float val[3];
1880
1881                                                 mul_v3_fl(avg, f);
1882                                                 sub_v3_v3v3(val, avg, co);
1883                                                 madd_v3_v3v3fl(val, co, val, fade);
1884
1885                                                 sculpt_clip(sd, ss, co, val);
1886                                         }
1887                                 }
1888                         }
1889                 }
1890         }
1891 }
1892
1893 static void smooth(
1894         Sculpt *sd, Object *ob, PBVHNode **nodes, const int totnode, float bstrength, const bool smooth_mask)
1895 {
1896         SculptSession *ss = ob->sculpt;
1897         Brush *brush = BKE_paint_brush(&sd->paint);
1898
1899         const int max_iterations = 4;
1900         const float fract = 1.0f / max_iterations;
1901         PBVHType type = BKE_pbvh_type(ss->pbvh);
1902         int iteration, count;
1903         float last;
1904
1905         CLAMP(bstrength, 0.0f, 1.0f);
1906
1907         count = (int)(bstrength * max_iterations);
1908         last  = max_iterations * (bstrength - count * fract);
1909
1910         if (type == PBVH_FACES && !ss->pmap) {
1911                 BLI_assert(!"sculpt smooth: pmap missing");
1912                 return;
1913         }
1914
1915         for (iteration = 0; iteration <= count; ++iteration) {
1916                 const float strength = (iteration != count) ? 1.0f : last;
1917
1918                 SculptThreadedTaskData data = {
1919                     .sd = sd, .ob = ob, .brush = brush, .nodes = nodes,
1920                     .smooth_mask = smooth_mask, .strength = strength,
1921                 };
1922
1923                 ParallelRangeSettings settings;
1924                 BLI_parallel_range_settings_defaults(&settings);
1925                 settings.use_threading = ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT);
1926
1927                 switch (type) {
1928                         case PBVH_GRIDS:
1929                         {
1930                                 int gridsize;
1931                                 size_t size;
1932                                 SculptDoBrushSmoothGridDataChunk *data_chunk;
1933
1934                                 BKE_pbvh_node_get_grids(ss->pbvh, NULL, NULL, NULL, NULL, &gridsize, NULL);
1935                                 size = (size_t)gridsize;
1936                                 size = sizeof(float) * size * size * (smooth_mask ? 1 : 3);
1937                                 data_chunk = MEM_mallocN(sizeof(*data_chunk) + size, __func__);
1938                                 data_chunk->tmpgrid_size = size;
1939                                 size += sizeof(*data_chunk);
1940
1941                                 settings.userdata_chunk = data_chunk;
1942                                 settings.userdata_chunk_size = size;
1943                                 BLI_task_parallel_range(
1944                                             0, totnode,
1945                                             &data,
1946                                             do_smooth_brush_multires_task_cb_ex,
1947                                             &settings);
1948
1949                                 MEM_freeN(data_chunk);
1950                                 break;
1951                         }
1952                         case PBVH_FACES:
1953                                 BLI_task_parallel_range(
1954                                             0, totnode,
1955                                             &data,
1956                                             do_smooth_brush_mesh_task_cb_ex,
1957                                             &settings);
1958                                 break;
1959                         case PBVH_BMESH:
1960                                 BLI_task_parallel_range(
1961                                             0, totnode,
1962                                             &data,
1963                                             do_smooth_brush_bmesh_task_cb_ex,
1964                                             &settings);
1965                                 break;
1966                 }
1967
1968                 if (ss->multires)
1969                         multires_stitch_grids(ob);
1970         }
1971 }
1972
1973 static void do_smooth_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
1974 {
1975         SculptSession *ss = ob->sculpt;
1976         smooth(sd, ob, nodes, totnode, ss->cache->bstrength, false);
1977 }
1978
1979 static void do_mask_brush_draw_task_cb_ex(
1980         void *__restrict userdata,
1981         const int n,
1982         const ParallelRangeTLS *__restrict tls)
1983 {
1984         SculptThreadedTaskData *data = userdata;
1985         SculptSession *ss = data->ob->sculpt;
1986         const Brush *brush = data->brush;
1987         const float bstrength = ss->cache->bstrength;
1988
1989         PBVHVertexIter vd;
1990
1991         SculptBrushTest test;
1992         SculptBrushTestFn sculpt_brush_test_sq_fn =
1993                 sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape);
1994
1995         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
1996         {
1997                 if (sculpt_brush_test_sq_fn(&test, vd.co)) {
1998                         const float fade = tex_strength(
1999                                 ss, brush, vd.co, sqrtf(test.dist),
2000                                 vd.no, vd.fno, 0.0f, tls->thread_id);
2001
2002                         (*vd.mask) += fade * bstrength;
2003                         CLAMP(*vd.mask, 0, 1);
2004
2005                         if (vd.mvert)
2006                                 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
2007                 }
2008                 BKE_pbvh_vertex_iter_end;
2009         }
2010 }
2011
2012 static void do_mask_brush_draw(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
2013 {
2014         Brush *brush = BKE_paint_brush(&sd->paint);
2015
2016         /* threaded loop over nodes */
2017         SculptThreadedTaskData data = {
2018             .sd = sd, .ob = ob, .brush = brush, .nodes = nodes,
2019         };
2020
2021         ParallelRangeSettings settings;
2022         BLI_parallel_range_settings_defaults(&settings);
2023         settings.use_threading = ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT);
2024         BLI_task_parallel_range(
2025                     0, totnode,
2026                     &data,
2027                     do_mask_brush_draw_task_cb_ex,
2028                     &settings);
2029 }
2030
2031 static void do_mask_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
2032 {
2033         SculptSession *ss = ob->sculpt;
2034         Brush *brush = BKE_paint_brush(&sd->paint);
2035
2036         switch ((BrushMaskTool)brush->mask_tool) {
2037                 case BRUSH_MASK_DRAW:
2038                         do_mask_brush_draw(sd, ob, nodes, totnode);
2039                         break;
2040                 case BRUSH_MASK_SMOOTH:
2041                         smooth(sd, ob, nodes, totnode, ss->cache->bstrength, true);
2042                         break;
2043         }
2044 }
2045
2046 static void do_draw_brush_task_cb_ex(
2047         void *__restrict userdata,
2048         const int n,
2049         const ParallelRangeTLS *__restrict tls)
2050 {
2051         SculptThreadedTaskData *data = userdata;
2052         SculptSession *ss = data->ob->sculpt;
2053         const Brush *brush = data->brush;
2054         const float *offset = data->offset;
2055
2056         PBVHVertexIter vd;
2057         float (*proxy)[3];
2058
2059         proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co;
2060
2061         SculptBrushTest test;
2062         SculptBrushTestFn sculpt_brush_test_sq_fn =
2063                 sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape);
2064
2065         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
2066         {
2067                 if (sculpt_brush_test_sq_fn(&test, vd.co)) {
2068                         /* offset vertex */
2069                         const float fade = tex_strength(
2070                                 ss, brush, vd.co, sqrtf(test.dist),
2071                                 vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, tls->thread_id);
2072
2073                         mul_v3_v3fl(proxy[vd.i], offset, fade);
2074
2075                         if (vd.mvert)
2076                                 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
2077                 }
2078         }
2079         BKE_pbvh_vertex_iter_end;
2080 }
2081
2082 static void do_draw_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
2083 {
2084         SculptSession *ss = ob->sculpt;
2085         Brush *brush = BKE_paint_brush(&sd->paint);
2086         float offset[3];
2087         const float bstrength = ss->cache->bstrength;
2088
2089         /* offset with as much as possible factored in already */
2090         mul_v3_v3fl(offset, ss->cache->sculpt_normal_symm, ss->cache->radius);
2091         mul_v3_v3(offset, ss->cache->scale);
2092         mul_v3_fl(offset, bstrength);
2093
2094         /* threaded loop over nodes */
2095         SculptThreadedTaskData data = {
2096             .sd = sd, .ob = ob, .brush = brush, .nodes = nodes,
2097             .offset = offset,
2098         };
2099
2100         ParallelRangeSettings settings;
2101         BLI_parallel_range_settings_defaults(&settings);
2102         settings.use_threading = ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT);
2103         BLI_task_parallel_range(
2104                     0, totnode,
2105                     &data,
2106                     do_draw_brush_task_cb_ex,
2107                     &settings);
2108 }
2109
2110 /**
2111  * Used for 'SCULPT_TOOL_CREASE' and 'SCULPT_TOOL_BLOB'
2112  */
2113 static void do_crease_brush_task_cb_ex(
2114         void *__restrict userdata,
2115         const int n,
2116         const ParallelRangeTLS *__restrict tls)
2117 {
2118         SculptThreadedTaskData *data = userdata;
2119         SculptSession *ss = data->ob->sculpt;
2120         const Brush *brush = data->brush;
2121         SculptProjectVector *spvc = data->spvc;
2122         const float flippedbstrength = data->flippedbstrength;
2123         const float *offset = data->offset;
2124
2125         PBVHVertexIter vd;
2126         float (*proxy)[3];
2127
2128         proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co;
2129
2130         SculptBrushTest test;
2131         SculptBrushTestFn sculpt_brush_test_sq_fn =
2132                 sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape);
2133
2134         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
2135         {
2136                 if (sculpt_brush_test_sq_fn(&test, vd.co)) {
2137                         /* offset vertex */
2138                         const float fade = tex_strength(
2139                                 ss, brush, vd.co, sqrtf(test.dist),
2140                                 vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, tls->thread_id);
2141                         float val1[3];
2142                         float val2[3];
2143
2144                         /* first we pinch */
2145                         sub_v3_v3v3(val1, test.location, vd.co);
2146                         if (brush->falloff_shape == PAINT_FALLOFF_SHAPE_TUBE) {
2147                                 project_plane_v3_v3v3(val1, val1, ss->cache->view_normal);
2148                         }
2149
2150                         mul_v3_fl(val1, fade * flippedbstrength);
2151
2152                         sculpt_project_v3(spvc, val1, val1);
2153
2154                         /* then we draw */
2155                         mul_v3_v3fl(val2, offset, fade);
2156
2157                         add_v3_v3v3(proxy[vd.i], val1, val2);
2158
2159                         if (vd.mvert)
2160                                 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
2161                 }
2162         }
2163         BKE_pbvh_vertex_iter_end;
2164 }
2165
2166 static void do_crease_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
2167 {
2168         SculptSession *ss = ob->sculpt;
2169         const Scene *scene = ss->cache->vc->scene;
2170         Brush *brush = BKE_paint_brush(&sd->paint);
2171         float offset[3];
2172         float bstrength = ss->cache->bstrength;
2173         float flippedbstrength, crease_correction;
2174         float brush_alpha;
2175
2176         SculptProjectVector spvc;
2177
2178         /* offset with as much as possible factored in already */
2179         mul_v3_v3fl(offset, ss->cache->sculpt_normal_symm, ss->cache->radius);
2180         mul_v3_v3(offset, ss->cache->scale);
2181         mul_v3_fl(offset, bstrength);
2182
2183         /* we divide out the squared alpha and multiply by the squared crease to give us the pinch strength */
2184         crease_correction = brush->crease_pinch_factor * brush->crease_pinch_factor;
2185         brush_alpha = BKE_brush_alpha_get(scene, brush);
2186         if (brush_alpha > 0.0f)
2187                 crease_correction /= brush_alpha * brush_alpha;
2188
2189         /* we always want crease to pinch or blob to relax even when draw is negative */
2190         flippedbstrength = (bstrength < 0) ? -crease_correction * bstrength : crease_correction * bstrength;
2191
2192         if (brush->sculpt_tool == SCULPT_TOOL_BLOB) flippedbstrength *= -1.0f;
2193
2194         /* Use surface normal for 'spvc', so the vertices are pinched towards a line instead of a single point.
2195          * Without this we get a 'flat' surface surrounding the pinch */
2196         sculpt_project_v3_cache_init(&spvc, ss->cache->sculpt_normal_symm);
2197
2198         /* threaded loop over nodes */
2199         SculptThreadedTaskData data = {
2200             .sd = sd, .ob = ob, .brush = brush, .nodes = nodes,
2201             .spvc = &spvc, .offset = offset, .flippedbstrength = flippedbstrength,
2202         };
2203
2204         ParallelRangeSettings settings;
2205         BLI_parallel_range_settings_defaults(&settings);
2206         settings.use_threading = ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT);
2207         BLI_task_parallel_range(
2208                     0, totnode,
2209                     &data,
2210                     do_crease_brush_task_cb_ex,
2211                     &settings);
2212 }
2213
2214 static void do_pinch_brush_task_cb_ex(
2215         void *__restrict userdata,
2216         const int n,
2217         const ParallelRangeTLS *__restrict tls)
2218 {
2219         SculptThreadedTaskData *data = userdata;
2220         SculptSession *ss = data->ob->sculpt;
2221         const Brush *brush = data->brush;
2222
2223         PBVHVertexIter vd;
2224         float (*proxy)[3];
2225         const float bstrength = ss->cache->bstrength;
2226
2227         proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co;
2228
2229         SculptBrushTest test;
2230         SculptBrushTestFn sculpt_brush_test_sq_fn =
2231                 sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape);
2232
2233         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
2234         {
2235                 if (sculpt_brush_test_sq_fn(&test, vd.co)) {
2236                         const float fade = bstrength * tex_strength(
2237                                 ss, brush, vd.co, sqrtf(test.dist),
2238                                 vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, tls->thread_id);
2239                         float val[3];
2240
2241                         sub_v3_v3v3(val, test.location, vd.co);
2242                         if (brush->falloff_shape == PAINT_FALLOFF_SHAPE_TUBE) {
2243                                 project_plane_v3_v3v3(val, val, ss->cache->view_normal);
2244                         }
2245                         mul_v3_v3fl(proxy[vd.i], val, fade);
2246
2247                         if (vd.mvert)
2248                                 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
2249                 }
2250         }
2251         BKE_pbvh_vertex_iter_end;
2252 }
2253
2254 static void do_pinch_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
2255 {
2256         Brush *brush = BKE_paint_brush(&sd->paint);
2257
2258         SculptThreadedTaskData data = {
2259             .sd = sd, .ob = ob, .brush = brush, .nodes = nodes,
2260         };
2261
2262         ParallelRangeSettings settings;
2263         BLI_parallel_range_settings_defaults(&settings);
2264         settings.use_threading = ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT);
2265         BLI_task_parallel_range(
2266                     0, totnode,
2267                     &data,
2268                     do_pinch_brush_task_cb_ex,
2269                     &settings);
2270 }
2271
2272 static void do_grab_brush_task_cb_ex(
2273         void *__restrict userdata,
2274         const int n,
2275         const ParallelRangeTLS *__restrict tls)
2276 {
2277         SculptThreadedTaskData *data = userdata;
2278         SculptSession *ss = data->ob->sculpt;
2279         const Brush *brush = data->brush;
2280         const float *grab_delta = data->grab_delta;
2281
2282         PBVHVertexIter vd;
2283         SculptOrigVertData orig_data;
2284         float (*proxy)[3];
2285         const float bstrength = ss->cache->bstrength;
2286
2287         sculpt_orig_vert_data_init(&orig_data, data->ob, data->nodes[n]);
2288
2289         proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co;
2290
2291         SculptBrushTest test;
2292         SculptBrushTestFn sculpt_brush_test_sq_fn =
2293                 sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape);
2294
2295         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
2296         {
2297                 sculpt_orig_vert_data_update(&orig_data, &vd);
2298
2299                 if (sculpt_brush_test_sq_fn(&test, orig_data.co)) {
2300                         const float fade = bstrength * tex_strength(
2301                                 ss, brush, orig_data.co, sqrtf(test.dist),
2302                                 orig_data.no, NULL, vd.mask ? *vd.mask : 0.0f, tls->thread_id);
2303
2304                         mul_v3_v3fl(proxy[vd.i], grab_delta, fade);
2305
2306                         if (vd.mvert)
2307                                 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
2308                 }
2309         }
2310         BKE_pbvh_vertex_iter_end;
2311 }
2312
2313 static void do_grab_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
2314 {
2315         SculptSession *ss = ob->sculpt;
2316         Brush *brush = BKE_paint_brush(&sd->paint);
2317         float grab_delta[3];
2318
2319         copy_v3_v3(grab_delta, ss->cache->grab_delta_symmetry);
2320
2321         if (ss->cache->normal_weight > 0.0f) {
2322                 sculpt_project_v3_normal_align(ss, ss->cache->normal_weight, grab_delta);
2323         }
2324
2325         SculptThreadedTaskData data = {
2326             .sd = sd, .ob = ob, .brush = brush, .nodes = nodes,
2327             .grab_delta = grab_delta,
2328         };
2329
2330         ParallelRangeSettings settings;
2331         BLI_parallel_range_settings_defaults(&settings);
2332         settings.use_threading = ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT);
2333         BLI_task_parallel_range(
2334                     0, totnode,
2335                     &data,
2336                     do_grab_brush_task_cb_ex,
2337                     &settings);
2338 }
2339
2340 static void do_nudge_brush_task_cb_ex(
2341         void *__restrict userdata,
2342         const int n,
2343         const ParallelRangeTLS *__restrict tls)
2344 {
2345         SculptThreadedTaskData *data = userdata;
2346         SculptSession *ss = data->ob->sculpt;
2347         const Brush *brush = data->brush;
2348         const float *cono = data->cono;
2349
2350         PBVHVertexIter vd;
2351         float (*proxy)[3];
2352         const float bstrength = ss->cache->bstrength;
2353
2354         proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co;
2355
2356         SculptBrushTest test;
2357         SculptBrushTestFn sculpt_brush_test_sq_fn =
2358                 sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape);
2359
2360         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
2361         {
2362                 if (sculpt_brush_test_sq_fn(&test, vd.co)) {
2363                         const float fade = bstrength * tex_strength(
2364                                 ss, brush, vd.co, sqrtf(test.dist),
2365                                 vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, tls->thread_id);
2366
2367                         mul_v3_v3fl(proxy[vd.i], cono, fade);
2368
2369                         if (vd.mvert)
2370                                 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
2371                 }
2372         }
2373         BKE_pbvh_vertex_iter_end;
2374 }
2375
2376 static void do_nudge_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
2377 {
2378         SculptSession *ss = ob->sculpt;
2379         Brush *brush = BKE_paint_brush(&sd->paint);
2380         float grab_delta[3];
2381         float tmp[3], cono[3];
2382
2383         copy_v3_v3(grab_delta, ss->cache->grab_delta_symmetry);
2384
2385         cross_v3_v3v3(tmp, ss->cache->sculpt_normal_symm, grab_delta);
2386         cross_v3_v3v3(cono, tmp, ss->cache->sculpt_normal_symm);
2387
2388         SculptThreadedTaskData data = {
2389             .sd = sd, .ob = ob, .brush = brush, .nodes = nodes,
2390             .cono = cono,
2391         };
2392
2393         ParallelRangeSettings settings;
2394         BLI_parallel_range_settings_defaults(&settings);
2395         settings.use_threading = ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT);
2396         BLI_task_parallel_range(
2397                     0, totnode,
2398                     &data,
2399                     do_nudge_brush_task_cb_ex,
2400                     &settings);
2401 }
2402
2403 static void do_snake_hook_brush_task_cb_ex(
2404         void *__restrict userdata,
2405         const int n,
2406         const ParallelRangeTLS *__restrict tls)
2407 {
2408         SculptThreadedTaskData *data = userdata;
2409         SculptSession *ss = data->ob->sculpt;
2410         const Brush *brush = data->brush;
2411         SculptProjectVector *spvc = data->spvc;
2412         const float *grab_delta = data->grab_delta;
2413
2414         PBVHVertexIter vd;
2415         float (*proxy)[3];
2416         const float bstrength = ss->cache->bstrength;
2417         const bool do_rake_rotation = ss->cache->is_rake_rotation_valid;
2418         const bool do_pinch = (brush->crease_pinch_factor != 0.5f);
2419         const float pinch = do_pinch ?
2420                 (2.0f * (0.5f - brush->crease_pinch_factor) * (len_v3(grab_delta) / ss->cache->radius)) : 0.0f;
2421
2422         proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co;
2423
2424         SculptBrushTest test;
2425         SculptBrushTestFn sculpt_brush_test_sq_fn =
2426                 sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape);
2427
2428         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
2429         {
2430                 if (sculpt_brush_test_sq_fn(&test, vd.co)) {
2431                         const float fade = bstrength * tex_strength(
2432                                 ss, brush, vd.co, sqrtf(test.dist),
2433                                 vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, tls->thread_id);
2434
2435                         mul_v3_v3fl(proxy[vd.i], grab_delta, fade);
2436
2437                         /* negative pinch will inflate, helps maintain volume */
2438                         if (do_pinch) {
2439                                 float delta_pinch_init[3], delta_pinch[3];
2440
2441                                 sub_v3_v3v3(delta_pinch, vd.co, test.location);
2442                                 if (brush->falloff_shape == PAINT_FALLOFF_SHAPE_TUBE) {
2443                                         project_plane_v3_v3v3(delta_pinch, delta_pinch, ss->cache->true_view_normal);
2444                                 }
2445
2446                                 /* important to calculate based on the grabbed location
2447                                  * (intentionally ignore fade here). */
2448                                 add_v3_v3(delta_pinch, grab_delta);
2449
2450                                 sculpt_project_v3(spvc, delta_pinch, delta_pinch);
2451
2452                                 copy_v3_v3(delta_pinch_init, delta_pinch);
2453
2454                                 float pinch_fade = pinch * fade;
2455                                 /* when reducing, scale reduction back by how close to the center we are,
2456                                  * so we don't pinch into nothingness */
2457                                 if (pinch > 0.0f) {
2458                                         /* square to have even less impact for close vertices */
2459                                         pinch_fade *= pow2f(min_ff(1.0f, len_v3(delta_pinch) / ss->cache->radius));
2460                                 }
2461                                 mul_v3_fl(delta_pinch, 1.0f + pinch_fade);
2462                                 sub_v3_v3v3(delta_pinch, delta_pinch_init, delta_pinch);
2463                                 add_v3_v3(proxy[vd.i], delta_pinch);
2464                         }
2465
2466                         if (do_rake_rotation) {
2467                                 float delta_rotate[3];
2468                                 sculpt_rake_rotate(ss, test.location, vd.co, fade, delta_rotate);
2469                                 add_v3_v3(proxy[vd.i], delta_rotate);
2470                         }
2471
2472                         if (vd.mvert)
2473                                 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
2474                 }
2475         }
2476         BKE_pbvh_vertex_iter_end;
2477 }
2478
2479 static void do_snake_hook_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
2480 {
2481         SculptSession *ss = ob->sculpt;
2482         Brush *brush = BKE_paint_brush(&sd->paint);
2483         const float bstrength = ss->cache->bstrength;
2484         float grab_delta[3];
2485
2486         SculptProjectVector spvc;
2487
2488         copy_v3_v3(grab_delta, ss->cache->grab_delta_symmetry);
2489
2490         if (bstrength < 0)
2491                 negate_v3(grab_delta);
2492
2493         if (ss->cache->normal_weight > 0.0f) {
2494                 sculpt_project_v3_normal_align(ss, ss->cache->normal_weight, grab_delta);
2495         }
2496
2497         /* optionally pinch while painting */
2498         if (brush->crease_pinch_factor != 0.5f) {
2499                 sculpt_project_v3_cache_init(&spvc, grab_delta);
2500         }
2501
2502         SculptThreadedTaskData data = {
2503             .sd = sd, .ob = ob, .brush = brush, .nodes = nodes,
2504             .spvc = &spvc, .grab_delta = grab_delta,
2505         };
2506
2507         ParallelRangeSettings settings;
2508         BLI_parallel_range_settings_defaults(&settings);
2509         settings.use_threading = ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT);
2510         BLI_task_parallel_range(
2511                     0, totnode,
2512                     &data,
2513                     do_snake_hook_brush_task_cb_ex,
2514                     &settings);
2515 }
2516
2517 static void do_thumb_brush_task_cb_ex(
2518         void *__restrict userdata,
2519         const int n,
2520         const ParallelRangeTLS *__restrict tls)
2521 {
2522         SculptThreadedTaskData *data = userdata;
2523         SculptSession *ss = data->ob->sculpt;
2524         const Brush *brush = data->brush;
2525         const float *cono = data->cono;
2526
2527         PBVHVertexIter vd;
2528         SculptOrigVertData orig_data;
2529         float (*proxy)[3];
2530         const float bstrength = ss->cache->bstrength;
2531
2532         sculpt_orig_vert_data_init(&orig_data, data->ob, data->nodes[n]);
2533
2534         proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co;
2535
2536         SculptBrushTest test;
2537         SculptBrushTestFn sculpt_brush_test_sq_fn =
2538                 sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape);
2539
2540         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
2541         {
2542                 sculpt_orig_vert_data_update(&orig_data, &vd);
2543
2544                 if (sculpt_brush_test_sq_fn(&test, orig_data.co)) {
2545                         const float fade = bstrength * tex_strength(
2546                                 ss, brush, orig_data.co, sqrtf(test.dist),
2547                                 orig_data.no, NULL, vd.mask ? *vd.mask : 0.0f, tls->thread_id);
2548
2549                         mul_v3_v3fl(proxy[vd.i], cono, fade);
2550
2551                         if (vd.mvert)
2552                                 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
2553                 }
2554         }
2555         BKE_pbvh_vertex_iter_end;
2556 }
2557
2558 static void do_thumb_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
2559 {
2560         SculptSession *ss = ob->sculpt;
2561         Brush *brush = BKE_paint_brush(&sd->paint);
2562         float grab_delta[3];
2563         float tmp[3], cono[3];
2564
2565         copy_v3_v3(grab_delta, ss->cache->grab_delta_symmetry);
2566
2567         cross_v3_v3v3(tmp, ss->cache->sculpt_normal_symm, grab_delta);
2568         cross_v3_v3v3(cono, tmp, ss->cache->sculpt_normal_symm);
2569
2570         SculptThreadedTaskData data = {
2571             .sd = sd, .ob = ob, .brush = brush, .nodes = nodes,
2572             .cono = cono,
2573         };
2574
2575         ParallelRangeSettings settings;
2576         BLI_parallel_range_settings_defaults(&settings);
2577         settings.use_threading = ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT);
2578         BLI_task_parallel_range(
2579                     0, totnode,
2580                     &data,
2581                     do_thumb_brush_task_cb_ex,
2582                     &settings);
2583 }
2584
2585 static void do_rotate_brush_task_cb_ex(
2586         void *__restrict userdata,
2587         const int n,
2588         const ParallelRangeTLS *__restrict tls)
2589 {
2590         SculptThreadedTaskData *data = userdata;
2591         SculptSession *ss = data->ob->sculpt;
2592         const Brush *brush = data->brush;
2593         const float angle = data->angle;
2594
2595         PBVHVertexIter vd;
2596         SculptOrigVertData orig_data;
2597         float (*proxy)[3];
2598         const float bstrength = ss->cache->bstrength;
2599
2600         sculpt_orig_vert_data_init(&orig_data, data->ob, data->nodes[n]);
2601
2602         proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co;
2603
2604         SculptBrushTest test;
2605         SculptBrushTestFn sculpt_brush_test_sq_fn =
2606                 sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape);
2607
2608         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
2609         {
2610                 sculpt_orig_vert_data_update(&orig_data, &vd);
2611
2612                 if (sculpt_brush_test_sq_fn(&test, orig_data.co)) {
2613                         float vec[3], rot[3][3];
2614                         const float fade = bstrength * tex_strength(
2615                                 ss, brush, orig_data.co, sqrtf(test.dist),
2616                                 orig_data.no, NULL, vd.mask ? *vd.mask : 0.0f, tls->thread_id);
2617
2618                         sub_v3_v3v3(vec, orig_data.co, ss->cache->location);
2619                         axis_angle_normalized_to_mat3(rot, ss->cache->sculpt_normal_symm, angle * fade);
2620                         mul_v3_m3v3(proxy[vd.i], rot, vec);
2621                         add_v3_v3(proxy[vd.i], ss->cache->location);
2622                         sub_v3_v3(proxy[vd.i], orig_data.co);
2623
2624                         if (vd.mvert)
2625                                 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
2626                 }
2627         }
2628         BKE_pbvh_vertex_iter_end;
2629 }
2630
2631 static void do_rotate_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
2632 {
2633         SculptSession *ss = ob->sculpt;
2634         Brush *brush = BKE_paint_brush(&sd->paint);
2635
2636         static const int flip[8] = { 1, -1, -1, 1, -1, 1, 1, -1 };
2637         const float angle = ss->cache->vertex_rotation * flip[ss->cache->mirror_symmetry_pass];
2638
2639         SculptThreadedTaskData data = {
2640             .sd = sd, .ob = ob, .brush = brush, .nodes = nodes,
2641             .angle = angle,
2642         };
2643
2644         ParallelRangeSettings settings;
2645         BLI_parallel_range_settings_defaults(&settings);
2646         settings.use_threading = ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT);
2647         BLI_task_parallel_range(
2648                     0, totnode,
2649                     &data,
2650                     do_rotate_brush_task_cb_ex,
2651                     &settings);
2652 }
2653
2654 static void do_layer_brush_task_cb_ex(
2655         void *__restrict userdata,
2656         const int n,
2657         const ParallelRangeTLS *__restrict tls)
2658 {
2659         SculptThreadedTaskData *data = userdata;
2660         SculptSession *ss = data->ob->sculpt;
2661         Sculpt *sd = data->sd;
2662         const Brush *brush = data->brush;
2663         const float *offset = data->offset;
2664
2665         PBVHVertexIter vd;
2666         SculptOrigVertData orig_data;
2667         float *layer_disp;
2668         const float bstrength = ss->cache->bstrength;
2669         const float lim = (bstrength < 0) ? -data->brush->height : data->brush->height;
2670         /* XXX: layer brush needs conversion to proxy but its more complicated */
2671         /* proxy = BKE_pbvh_node_add_proxy(ss->pbvh, nodes[n])->co; */
2672
2673         sculpt_orig_vert_data_init(&orig_data, data->ob, data->nodes[n]);
2674
2675         /* Why does this have to be thread-protected? */
2676         BLI_mutex_lock(&data->mutex);
2677         layer_disp = BKE_pbvh_node_layer_disp_get(ss->pbvh, data->nodes[n]);
2678         BLI_mutex_unlock(&data->mutex);
2679
2680         SculptBrushTest test;
2681         SculptBrushTestFn sculpt_brush_test_sq_fn =
2682                 sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape);
2683
2684         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
2685         {
2686                 sculpt_orig_vert_data_update(&orig_data, &vd);
2687
2688                 if (sculpt_brush_test_sq_fn(&test, orig_data.co)) {
2689                         const float fade = bstrength * tex_strength(
2690                                 ss, brush, vd.co, sqrtf(test.dist),
2691                                 vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, tls->thread_id);
2692                         float *disp = &layer_disp[vd.i];
2693                         float val[3];
2694
2695                         *disp += fade;
2696
2697                         /* Don't let the displacement go past the limit */
2698                         if ((lim < 0.0f && *disp < lim) || (lim >= 0.0f && *disp > lim))
2699                                 *disp = lim;
2700
2701                         mul_v3_v3fl(val, offset, *disp);
2702
2703                         if (!ss->multires && !ss->bm && ss->layer_co && (brush->flag & BRUSH_PERSISTENT)) {
2704                                 int index = vd.vert_indices[vd.i];
2705
2706                                 /* persistent base */
2707                                 add_v3_v3(val, ss->layer_co[index]);
2708                         }
2709                         else {
2710                                 add_v3_v3(val, orig_data.co);
2711                         }
2712
2713                         sculpt_clip(sd, ss, vd.co, val);
2714
2715                         if (vd.mvert)
2716                                 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
2717                 }
2718         }
2719         BKE_pbvh_vertex_iter_end;
2720 }
2721
2722 static void do_layer_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
2723 {
2724         SculptSession *ss = ob->sculpt;
2725         Brush *brush = BKE_paint_brush(&sd->paint);
2726         float offset[3];
2727
2728         mul_v3_v3v3(offset, ss->cache->scale, ss->cache->sculpt_normal_symm);
2729
2730         SculptThreadedTaskData data = {
2731             .sd = sd, .ob = ob, .brush = brush, .nodes = nodes,
2732             .offset = offset,
2733         };
2734         BLI_mutex_init(&data.mutex);
2735
2736         ParallelRangeSettings settings;
2737         BLI_parallel_range_settings_defaults(&settings);
2738         settings.use_threading = ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT);
2739         BLI_task_parallel_range(
2740                     0, totnode,
2741                     &data,
2742                     do_layer_brush_task_cb_ex,
2743                     &settings);
2744
2745         BLI_mutex_end(&data.mutex);
2746 }
2747
2748 static void do_inflate_brush_task_cb_ex(
2749         void *__restrict userdata,
2750         const int n,
2751         const ParallelRangeTLS *__restrict tls)
2752 {
2753         SculptThreadedTaskData *data = userdata;
2754         SculptSession *ss = data->ob->sculpt;
2755         const Brush *brush = data->brush;
2756
2757         PBVHVertexIter vd;
2758         float (*proxy)[3];
2759         const float bstrength = ss->cache->bstrength;
2760
2761         proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co;
2762
2763         SculptBrushTest test;
2764         SculptBrushTestFn sculpt_brush_test_sq_fn =
2765                 sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape);
2766
2767         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
2768         {
2769                 if (sculpt_brush_test_sq_fn(&test, vd.co)) {
2770                         const float fade = bstrength * tex_strength(
2771                                 ss, brush, vd.co, sqrtf(test.dist),
2772                                 vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, tls->thread_id);
2773                         float val[3];
2774
2775                         if (vd.fno)
2776                                 copy_v3_v3(val, vd.fno);
2777                         else
2778                                 normal_short_to_float_v3(val, vd.no);
2779
2780                         mul_v3_fl(val, fade * ss->cache->radius);
2781                         mul_v3_v3v3(proxy[vd.i], val, ss->cache->scale);
2782
2783                         if (vd.mvert)
2784                                 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
2785                 }
2786         }
2787         BKE_pbvh_vertex_iter_end;
2788 }
2789
2790 static void do_inflate_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
2791 {
2792         Brush *brush = BKE_paint_brush(&sd->paint);
2793
2794         SculptThreadedTaskData data = {
2795             .sd = sd, .ob = ob, .brush = brush, .nodes = nodes,
2796         };
2797
2798         ParallelRangeSettings settings;
2799         BLI_parallel_range_settings_defaults(&settings);
2800         settings.use_threading = ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT);
2801         BLI_task_parallel_range(
2802                     0, totnode,
2803                     &data,
2804                     do_inflate_brush_task_cb_ex,
2805                     &settings);
2806 }
2807
2808 static void calc_sculpt_plane(
2809         Sculpt *sd, Object *ob,
2810         PBVHNode **nodes, int totnode,
2811         float r_area_no[3], float r_area_co[3])
2812 {
2813         SculptSession *ss = ob->sculpt;
2814         Brush *brush = BKE_paint_brush(&sd->paint);
2815
2816         if (ss->cache->mirror_symmetry_pass == 0 &&
2817             ss->cache->radial_symmetry_pass == 0 &&
2818             ss->cache->tile_pass == 0 &&
2819             (ss->cache->first_time || !(brush->flag & BRUSH_ORIGINAL_NORMAL)))
2820         {
2821                 switch (brush->sculpt_plane) {
2822                         case SCULPT_DISP_DIR_VIEW:
2823                                 copy_v3_v3(r_area_no, ss->cache->true_view_normal);
2824                                 break;
2825
2826                         case SCULPT_DISP_DIR_X:
2827                                 ARRAY_SET_ITEMS(r_area_no, 1, 0, 0);
2828                                 break;
2829
2830                         case SCULPT_DISP_DIR_Y:
2831                                 ARRAY_SET_ITEMS(r_area_no, 0, 1, 0);
2832                                 break;
2833
2834                         case SCULPT_DISP_DIR_Z:
2835                                 ARRAY_SET_ITEMS(r_area_no, 0, 0, 1);
2836                                 break;
2837
2838                         case SCULPT_DISP_DIR_AREA:
2839                                 calc_area_normal_and_center(sd, ob, nodes, totnode, r_area_no, r_area_co);
2840                                 if (brush->falloff_shape == PAINT_FALLOFF_SHAPE_TUBE) {
2841                                         project_plane_v3_v3v3(r_area_no, r_area_no, ss->cache->view_normal);
2842                                         normalize_v3(r_area_no);
2843                                 }
2844                                 break;
2845
2846                         default:
2847                                 break;
2848                 }
2849
2850                 /* for flatten center */
2851                 /* flatten center has not been calculated yet if we are not using the area normal */
2852                 if (brush->sculpt_plane != SCULPT_DISP_DIR_AREA)
2853                         calc_area_center(sd, ob, nodes, totnode, r_area_co);
2854
2855                 /* for area normal */
2856                 copy_v3_v3(ss->cache->sculpt_normal, r_area_no);
2857
2858                 /* for flatten center */
2859                 copy_v3_v3(ss->cache->last_center, r_area_co);
2860         }
2861         else {
2862                 /* for area normal */
2863                 copy_v3_v3(r_area_no, ss->cache->sculpt_normal);
2864
2865                 /* for flatten center */
2866                 copy_v3_v3(r_area_co, ss->cache->last_center);
2867
2868                 /* for area normal */
2869                 flip_v3(r_area_no, ss->cache->mirror_symmetry_pass);
2870
2871                 /* for flatten center */
2872                 flip_v3(r_area_co, ss->cache->mirror_symmetry_pass);
2873
2874                 /* for area normal */
2875                 mul_m4_v3(ss->cache->symm_rot_mat, r_area_no);
2876
2877                 /* for flatten center */
2878                 mul_m4_v3(ss->cache->symm_rot_mat, r_area_co);
2879
2880                 /* shift the plane for the current tile */
2881                 add_v3_v3(r_area_co, ss->cache->plane_offset);
2882         }
2883 }
2884
2885 static int plane_trim(const StrokeCache *cache, const Brush *brush, const float val[3])
2886 {
2887         return (!(brush->flag & BRUSH_PLANE_TRIM) ||
2888                 ((dot_v3v3(val, val) <= cache->radius_squared * cache->plane_trim_squared)));
2889 }
2890
2891 static bool plane_point_side_flip(
2892         const float co[3], const float plane[4],
2893         const bool flip)
2894 {
2895         float d = plane_point_side_v3(plane, co);
2896         if (flip) d = -d;
2897         return d <= 0.0f;
2898 }
2899
2900 static int plane_point_side(const float co[3], const float plane[4])
2901 {
2902         float d = plane_point_side_v3(plane, co);
2903         return d <= 0.0f;
2904 }
2905
2906 static float get_offset(Sculpt *sd, SculptSession *ss)
2907 {
2908         Brush *brush = BKE_paint_brush(&sd->paint);
2909
2910         float rv = brush->plane_offset;
2911
2912         if (brush->flag & BRUSH_OFFSET_PRESSURE) {
2913                 rv *= ss->cache->pressure;
2914         }
2915
2916         return rv;
2917 }
2918
2919 static void do_flatten_brush_task_cb_ex(
2920         void *__restrict userdata,
2921         const int n,
2922         const ParallelRangeTLS *__restrict tls)
2923 {
2924         SculptThreadedTaskData *data = userdata;
2925         SculptSession *ss = data->ob->sculpt;
2926         const Brush *brush = data->brush;
2927         const float *area_no = data->area_no;
2928         const float *area_co = data->area_co;
2929
2930         PBVHVertexIter vd;
2931         float (*proxy)[3];
2932         const float bstrength = ss->cache->bstrength;
2933
2934         proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co;
2935
2936         SculptBrushTest test;
2937         SculptBrushTestFn sculpt_brush_test_sq_fn =
2938                 sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape);
2939
2940         plane_from_point_normal_v3(test.plane_tool, area_co, area_no);
2941
2942         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
2943         {
2944                 if (sculpt_brush_test_sq_fn(&test, vd.co)) {
2945                         float intr[3];
2946                         float val[3];
2947
2948                         closest_to_plane_normalized_v3(intr, test.plane_tool, vd.co);
2949
2950                         sub_v3_v3v3(val, intr, vd.co);
2951
2952                         if (plane_trim(ss->cache, brush, val)) {
2953                                 const float fade = bstrength * tex_strength(
2954                                         ss, brush, vd.co, sqrtf(test.dist),
2955                                         vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, tls->thread_id);
2956
2957                                 mul_v3_v3fl(proxy[vd.i], val, fade);
2958
2959                                 if (vd.mvert)
2960                                         vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
2961                         }
2962                 }
2963         }
2964         BKE_pbvh_vertex_iter_end;
2965 }
2966
2967 static void do_flatten_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
2968 {
2969         SculptSession *ss = ob->sculpt;
2970         Brush *brush = BKE_paint_brush(&sd->paint);
2971
2972         const float radius = ss->cache->radius;
2973
2974         float area_no[3];
2975         float area_co[3];
2976
2977         float offset = get_offset(sd, ss);
2978         float displace;
2979         float temp[3];
2980
2981         calc_sculpt_plane(sd, ob, nodes, totnode, area_no, area_co);
2982
2983         displace = radius * offset;
2984
2985         mul_v3_v3v3(temp, area_no, ss->cache->scale);
2986         mul_v3_fl(temp, displace);
2987         add_v3_v3(area_co, temp);
2988
2989         SculptThreadedTaskData data = {
2990             .sd = sd, .ob = ob, .brush = brush, .nodes = nodes,
2991             .area_no = area_no, .area_co = area_co,
2992         };
2993
2994         ParallelRangeSettings settings;
2995         BLI_parallel_range_settings_defaults(&settings);
2996         settings.use_threading = ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT);
2997         BLI_task_parallel_range(
2998                     0, totnode,
2999                     &data,
3000                     do_flatten_brush_task_cb_ex,
3001                     &settings);
3002 }
3003
3004 static void do_clay_brush_task_cb_ex(
3005         void *__restrict userdata,
3006         const int n,
3007         const ParallelRangeTLS *__restrict tls)
3008 {
3009         SculptThreadedTaskData *data = userdata;
3010         SculptSession *ss = data->ob->sculpt;
3011         const Brush *brush = data->brush;
3012         const float *area_no = data->area_no;
3013         const float *area_co = data->area_co;
3014
3015         PBVHVertexIter vd;
3016         float (*proxy)[3];
3017         const bool flip = (ss->cache->bstrength < 0);
3018         const float bstrength = flip ? -ss->cache->bstrength : ss->cache->bstrength;
3019
3020         proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co;
3021
3022         SculptBrushTest test;
3023         SculptBrushTestFn sculpt_brush_test_sq_fn =
3024                 sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape);
3025
3026         plane_from_point_normal_v3(test.plane_tool, area_co, area_no);
3027
3028         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
3029         {
3030                 if (sculpt_brush_test_sq_fn(&test, vd.co)) {
3031                         if (plane_point_side_flip(vd.co, test.plane_tool, flip)) {
3032                                 float intr[3];
3033                                 float val[3];
3034
3035                                 closest_to_plane_normalized_v3(intr, test.plane_tool, vd.co);
3036
3037                                 sub_v3_v3v3(val, intr, vd.co);
3038
3039                                 if (plane_trim(ss->cache, brush, val)) {
3040                                         /* note, the normal from the vertices is ignored,
3041                                          * causes glitch with planes, see: T44390 */
3042                                         const float fade = bstrength * tex_strength(
3043                                                 ss, brush, vd.co, sqrtf(test.dist),
3044                                                 vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, tls->thread_id);
3045
3046                                         mul_v3_v3fl(proxy[vd.i], val, fade);
3047
3048                                         if (vd.mvert)
3049                                                 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
3050                                 }
3051                         }
3052                 }
3053         }
3054         BKE_pbvh_vertex_iter_end;
3055 }
3056
3057 static void do_clay_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
3058 {
3059         SculptSession *ss = ob->sculpt;
3060         Brush *brush = BKE_paint_brush(&sd->paint);
3061
3062         const bool flip = (ss->cache->bstrength < 0);
3063         const float radius = flip ? -ss->cache->radius : ss->cache->radius;
3064
3065         float offset = get_offset(sd, ss);
3066         float displace;
3067
3068         float area_no[3];
3069         float area_co[3];
3070         float temp[3];
3071
3072         calc_sculpt_plane(sd, ob, nodes, totnode, area_no, area_co);
3073
3074         displace = radius * (0.25f + offset);
3075
3076         mul_v3_v3v3(temp, area_no, ss->cache->scale);
3077         mul_v3_fl(temp, displace);
3078         add_v3_v3(area_co, temp);
3079
3080         /* add_v3_v3v3(p, ss->cache->location, area_no); */
3081
3082         SculptThreadedTaskData data = {
3083             .sd = sd, .ob = ob, .brush = brush, .nodes = nodes,
3084             .area_no = area_no, .area_co = area_co,
3085         };
3086
3087         ParallelRangeSettings settings;
3088         BLI_parallel_range_settings_defaults(&settings);
3089         settings.use_threading = ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT);
3090         BLI_task_parallel_range(
3091                     0, totnode,
3092                     &data,
3093                     do_clay_brush_task_cb_ex,
3094                     &settings);
3095 }
3096
3097 static void do_clay_strips_brush_task_cb_ex(
3098         void *__restrict userdata,
3099         const int n,
3100         const ParallelRangeTLS *__restrict tls)
3101 {
3102         SculptThreadedTaskData *data = userdata;
3103         SculptSession *ss = data->ob->sculpt;
3104         const Brush *brush = data->brush;
3105         float (*mat)[4] = data->mat;
3106         const float *area_no_sp = data->area_no_sp;
3107         const float *area_co = data->area_co;
3108
3109         PBVHVertexIter vd;
3110         SculptBrushTest test;
3111         float (*proxy)[3];
3112         const bool flip = (ss->cache->bstrength < 0);
3113         const float bstrength = flip ? -ss->cache->bstrength : ss->cache->bstrength;
3114
3115         proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co;
3116
3117         sculpt_brush_test_init(ss, &test);
3118         plane_from_point_normal_v3(test.plane_tool, area_co, area_no_sp);
3119
3120         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
3121         {
3122                 if (sculpt_brush_test_cube(&test, vd.co, mat)) {
3123                         if (plane_point_side_flip(vd.co, test.plane_tool, flip)) {
3124                                 float intr[3];
3125                                 float val[3];
3126
3127                                 closest_to_plane_normalized_v3(intr, test.plane_tool, vd.co);
3128
3129                                 sub_v3_v3v3(val, intr, vd.co);
3130
3131                                 if (plane_trim(ss->cache, brush, val)) {
3132                                         /* note, the normal from the vertices is ignored,
3133                                          * causes glitch with planes, see: T44390 */
3134                                         const float fade = bstrength * tex_strength(
3135                                                 ss, brush, vd.co, ss->cache->radius * test.dist,
3136                                                 vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, tls->thread_id);
3137
3138                                         mul_v3_v3fl(proxy[vd.i], val, fade);
3139
3140                                         if (vd.mvert)
3141                                                 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
3142                                 }
3143                         }
3144                 }
3145         }
3146         BKE_pbvh_vertex_iter_end;
3147 }
3148
3149 static void do_clay_strips_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
3150 {
3151         SculptSession *ss = ob->sculpt;
3152         Brush *brush = BKE_paint_brush(&sd->paint);
3153
3154         const bool flip = (ss->cache->bstrength < 0);
3155         const float radius    = flip ? -ss->cache->radius : ss->cache->radius;
3156         const float offset    = get_offset(sd, ss);
3157         const float displace  = radius * (0.25f + offset);
3158
3159         float area_no_sp[3];  /* the sculpt-plane normal (whatever its set to) */
3160         float area_no[3];     /* geometry normal */
3161         float area_co[3];
3162
3163         float temp[3];
3164         float mat[4][4];
3165         float scale[4][4];
3166         float tmat[4][4];
3167
3168         calc_sculpt_plane(sd, ob, nodes, totnode, area_no_sp, area_co);
3169
3170         if (brush->sculpt_plane != SCULPT_DISP_DIR_AREA || (brush->flag & BRUSH_ORIGINAL_NORMAL))
3171                 calc_area_normal(sd, ob, nodes, totnode, area_no);
3172         else
3173                 copy_v3_v3(area_no, area_no_sp);
3174
3175         /* delay the first daub because grab delta is not setup */
3176         if (ss->cache->first_time)
3177                 return;
3178
3179         mul_v3_v3v3(temp, area_no_sp, ss->cache->scale);
3180         mul_v3_fl(temp, displace);
3181         add_v3_v3(area_co, temp);
3182
3183         /* init mat */
3184         cross_v3_v3v3(mat[0], area_no, ss->cache->grab_delta_symmetry);
3185         mat[0][3] = 0;
3186         cross_v3_v3v3(mat[1], area_no, mat[0]);
3187         mat[1][3] = 0;
3188         copy_v3_v3(mat[2], area_no);
3189         mat[2][3] = 0;
3190         copy_v3_v3(mat[3], ss->cache->location);
3191         mat[3][3] = 1;
3192         normalize_m4(mat);
3193
3194         /* scale mat */
3195         scale_m4_fl(scale, ss->cache->radius);
3196         mul_m4_m4m4(tmat, mat, scale);
3197         invert_m4_m4(mat, tmat);
3198
3199         SculptThreadedTaskData data = {
3200             .sd = sd, .ob = ob, .brush = brush, .nodes = nodes,
3201             .area_no_sp = area_no_sp, .area_co = area_co, .mat = mat,
3202         };
3203
3204         ParallelRangeSettings settings;
3205         BLI_parallel_range_settings_defaults(&settings);
3206         settings.use_threading = ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT);
3207         BLI_task_parallel_range(
3208                     0, totnode,
3209                     &data,
3210                     do_clay_strips_brush_task_cb_ex,
3211                     &settings);
3212 }
3213
3214 static void do_fill_brush_task_cb_ex(
3215         void *__restrict userdata,
3216         const int n,
3217         const ParallelRangeTLS *__restrict tls)
3218 {
3219         SculptThreadedTaskData *data = userdata;
3220         SculptSession *ss = data->ob->sculpt;
3221         const Brush *brush = data->brush;
3222         const float *area_no = data->area_no;
3223         const float *area_co = data->area_co;
3224
3225         PBVHVertexIter vd;
3226         float (*proxy)[3];
3227         const float bstrength = ss->cache->bstrength;
3228
3229         proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co;
3230
3231         SculptBrushTest test;
3232         SculptBrushTestFn sculpt_brush_test_sq_fn =
3233                 sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape);
3234
3235         plane_from_point_normal_v3(test.plane_tool, area_co, area_no);
3236
3237         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
3238         {
3239                 if (sculpt_brush_test_sq_fn(&test, vd.co)) {
3240                         if (plane_point_side(vd.co, test.plane_tool)) {
3241                                 float intr[3];
3242                                 float val[3];
3243
3244                                 closest_to_plane_normalized_v3(intr, test.plane_tool, vd.co);
3245
3246                                 sub_v3_v3v3(val, intr, vd.co);