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