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