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