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