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