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