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