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