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