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