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