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