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