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