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