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