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