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