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