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