Vertex/Weight Paint: Use PBVH for painting
[blender.git] / source / blender / editors / sculpt_paint / sculpt.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software  Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2006 by Nicholas Bishop
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Jason Wilkins, Tom Musgrove.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  *
27  * Implements the Sculpt Mode tools
28  *
29  */
30
31 /** \file blender/editors/sculpt_paint/sculpt.c
32  *  \ingroup edsculpt
33  */
34
35
36 #include "MEM_guardedalloc.h"
37
38 #include "BLI_math.h"
39 #include "BLI_blenlib.h"
40 #include "BLI_dial.h"
41 #include "BLI_task.h"
42 #include "BLI_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         test->mirror_symmetry_pass = ss->cache->mirror_symmetry_pass;
527
528         if (rv3d->rflag & RV3D_CLIPPING) {
529                 test->clip_rv3d = rv3d;
530         }
531         else {
532                 test->clip_rv3d = NULL;
533         }
534 }
535
536 BLI_INLINE bool sculpt_brush_test_clipping(const SculptBrushTest *test, const float co[3])
537 {
538         RegionView3D *rv3d = test->clip_rv3d;
539         if (!rv3d) {
540                 return false;
541         }
542         float symm_co[3];
543         flip_v3_v3(symm_co, co, test->mirror_symmetry_pass);
544         return ED_view3d_clipping_test(rv3d, symm_co, true);
545 }
546
547 bool sculpt_brush_test(SculptBrushTest *test, const float co[3])
548 {
549         float distsq = len_squared_v3v3(co, test->location);
550
551         if (distsq <= test->radius_squared) {
552                 if (sculpt_brush_test_clipping(test, co)) {
553                         return 0;
554                 }
555                 test->dist = sqrtf(distsq);
556                 return 1;
557         }
558         else {
559                 return 0;
560         }
561 }
562
563 bool sculpt_brush_test_sq(SculptBrushTest *test, const float co[3])
564 {
565         float distsq = len_squared_v3v3(co, test->location);
566
567         if (distsq <= test->radius_squared) {
568                 if (sculpt_brush_test_clipping(test, co)) {
569                         return 0;
570                 }
571                 test->dist = distsq;
572                 return 1;
573         }
574         else {
575                 return 0;
576         }
577 }
578
579 bool sculpt_brush_test_fast(const SculptBrushTest *test, const float co[3])
580 {
581         if (sculpt_brush_test_clipping(test, co)) {
582                 return 0;
583         }
584         return len_squared_v3v3(co, test->location) <= test->radius_squared;
585 }
586
587 bool sculpt_brush_test_cube(SculptBrushTest *test, const float co[3], float local[4][4])
588 {
589         float side = M_SQRT1_2;
590         float local_co[3];
591
592         if (sculpt_brush_test_clipping(test, co)) {
593                 return 0;
594         }
595
596         mul_v3_m4v3(local_co, local, co);
597
598         local_co[0] = fabsf(local_co[0]);
599         local_co[1] = fabsf(local_co[1]);
600         local_co[2] = fabsf(local_co[2]);
601
602         if (local_co[0] <= side && local_co[1] <= side && local_co[2] <= side) {
603                 float p = 4.0f;
604                 
605                 test->dist = ((powf(local_co[0], p) +
606                                powf(local_co[1], p) +
607                                powf(local_co[2], p)) / powf(side, p));
608
609                 return 1;
610         }
611         else {
612                 return 0;
613         }
614 }
615
616 static float frontface(Brush *br, const float sculpt_normal[3],
617                        const short no[3], const float fno[3])
618 {
619         if (br->flag & BRUSH_FRONTFACE) {
620                 float dot;
621
622                 if (no) {
623                         float tmp[3];
624
625                         normal_short_to_float_v3(tmp, no);
626                         dot = dot_v3v3(tmp, sculpt_normal);
627                 }
628                 else {
629                         dot = dot_v3v3(fno, sculpt_normal);
630                 }
631                 return dot > 0 ? dot : 0;
632         }
633         else {
634                 return 1;
635         }
636 }
637
638 #if 0
639
640 static bool sculpt_brush_test_cyl(SculptBrushTest *test, float co[3], float location[3], const float area_no[3])
641 {
642         if (sculpt_brush_test_fast(test, co)) {
643                 float t1[3], t2[3], t3[3], dist;
644
645                 sub_v3_v3v3(t1, location, co);
646                 sub_v3_v3v3(t2, x2, location);
647
648                 cross_v3_v3v3(t3, area_no, t1);
649
650                 dist = len_v3(t3) / len_v3(t2);
651
652                 test->dist = dist;
653
654                 return 1;
655         }
656
657         return 0;
658 }
659
660 #endif
661
662 /* ===== Sculpting =====
663  *
664  */
665 static void flip_v3(float v[3], const char symm)
666 {
667         flip_v3_v3(v, v, symm);
668 }
669
670 static float calc_overlap(StrokeCache *cache, const char symm, const char axis, const float angle)
671 {
672         float mirror[3];
673         float distsq;
674         
675         /* flip_v3_v3(mirror, cache->traced_location, symm); */
676         flip_v3_v3(mirror, cache->true_location, symm);
677
678         if (axis != 0) {
679                 float mat[3][3];
680                 axis_angle_to_mat3_single(mat, axis, angle);
681                 mul_m3_v3(mat, mirror);
682         }
683
684         /* distsq = len_squared_v3v3(mirror, cache->traced_location); */
685         distsq = len_squared_v3v3(mirror, cache->true_location);
686
687         if (distsq <= 4.0f * (cache->radius_squared))
688                 return (2.0f * (cache->radius) - sqrtf(distsq)) / (2.0f * (cache->radius));
689         else
690                 return 0;
691 }
692
693 static float calc_radial_symmetry_feather(Sculpt *sd, StrokeCache *cache, const char symm, const char axis)
694 {
695         int i;
696         float overlap;
697
698         overlap = 0;
699         for (i = 1; i < sd->radial_symm[axis - 'X']; ++i) {
700                 const float angle = 2 * M_PI * i / sd->radial_symm[axis - 'X'];
701                 overlap += calc_overlap(cache, symm, axis, angle);
702         }
703
704         return overlap;
705 }
706
707 static float calc_symmetry_feather(Sculpt *sd, StrokeCache *cache)
708 {
709         if (sd->paint.symmetry_flags & PAINT_SYMMETRY_FEATHER) {
710                 float overlap;
711                 int symm = cache->symmetry;
712                 int i;
713
714                 overlap = 0;
715                 for (i = 0; i <= symm; i++) {
716                         if (i == 0 || (symm & i && (symm != 5 || i != 3) && (symm != 6 || (i != 3 && i != 5)))) {
717
718                                 overlap += calc_overlap(cache, i, 0, 0);
719
720                                 overlap += calc_radial_symmetry_feather(sd, cache, i, 'X');
721                                 overlap += calc_radial_symmetry_feather(sd, cache, i, 'Y');
722                                 overlap += calc_radial_symmetry_feather(sd, cache, i, 'Z');
723                         }
724                 }
725
726                 return 1 / overlap;
727         }
728         else {
729                 return 1;
730         }
731 }
732
733 /** \name Calculate Normal and Center
734  *
735  * Calculate geometry surrounding the brush center.
736  * (optionally using original coordinates).
737  *
738  * Functions are:
739  * - #calc_area_center
740  * - #calc_area_normal
741  * - #calc_area_normal_and_center
742  *
743  * \note These are all _very_ similar, when changing one, check others.
744  * \{ */
745
746 static void calc_area_normal_and_center_task_cb(void *userdata, const int n)
747 {
748         SculptThreadedTaskData *data = userdata;
749         SculptSession *ss = data->ob->sculpt;
750         float (*area_nos)[3] = data->area_nos;
751         float (*area_cos)[3] = data->area_cos;
752
753         PBVHVertexIter vd;
754         SculptBrushTest test;
755         SculptUndoNode *unode;
756
757         float private_co[2][3] = {{0.0f}};
758         float private_no[2][3] = {{0.0f}};
759         int   private_count[2] = {0};
760         bool use_original;
761
762         unode = sculpt_undo_push_node(data->ob, data->nodes[n], SCULPT_UNDO_COORDS);
763         sculpt_brush_test_init(ss, &test);
764
765         use_original = (ss->cache->original && (unode->co || unode->bm_entry));
766
767         /* when the mesh is edited we can't rely on original coords
768          * (original mesh may not even have verts in brush radius) */
769         if (use_original && data->has_bm_orco) {
770                 float (*orco_coords)[3];
771                 int   (*orco_tris)[3];
772                 int     orco_tris_num;
773                 int i;
774
775                 BKE_pbvh_node_get_bm_orco_data(data->nodes[n], &orco_tris, &orco_tris_num, &orco_coords);
776
777                 for (i = 0; i < orco_tris_num; i++) {
778                         const float *co_tri[3] = {
779                             orco_coords[orco_tris[i][0]],
780                             orco_coords[orco_tris[i][1]],
781                             orco_coords[orco_tris[i][2]],
782                         };
783                         float co[3];
784
785                         closest_on_tri_to_point_v3(co, test.location, UNPACK3(co_tri));
786
787                         if (sculpt_brush_test_fast(&test, co)) {
788                                 float no[3];
789                                 int flip_index;
790
791                                 normal_tri_v3(no, UNPACK3(co_tri));
792
793                                 flip_index = (dot_v3v3(ss->cache->view_normal, no) <= 0.0f);
794                                 if (area_cos)
795                                         add_v3_v3(private_co[flip_index], co);
796                                 if (area_nos)
797                                         add_v3_v3(private_no[flip_index], no);
798                                 private_count[flip_index] += 1;
799                         }
800                 }
801         }
802         else {
803                 BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
804                 {
805                         const float *co;
806                         const short *no_s;  /* bm_vert only */
807
808                         if (use_original) {
809                                 if (unode->bm_entry) {
810                                         BM_log_original_vert_data(ss->bm_log, vd.bm_vert, &co, &no_s);
811                                 }
812                                 else {
813                                         co = unode->co[vd.i];
814                                         no_s = unode->no[vd.i];
815                                 }
816                         }
817                         else {
818                                 co = vd.co;
819                         }
820
821                         if (sculpt_brush_test_fast(&test, co)) {
822                                 float no_buf[3];
823                                 const float *no;
824                                 int flip_index;
825
826                                 if (use_original) {
827                                         normal_short_to_float_v3(no_buf, no_s);
828                                         no = no_buf;
829                                 }
830                                 else {
831                                         if (vd.no) {
832                                                 normal_short_to_float_v3(no_buf, vd.no);
833                                                 no = no_buf;
834                                         }
835                                         else {
836                                                 no = vd.fno;
837                                         }
838                                 }
839
840                                 flip_index = (dot_v3v3(ss->cache->view_normal, no) <= 0.0f);
841                                 if (area_cos)
842                                         add_v3_v3(private_co[flip_index], co);
843                                 if (area_nos)
844                                         add_v3_v3(private_no[flip_index], no);
845                                 private_count[flip_index] += 1;
846                         }
847                 }
848                 BKE_pbvh_vertex_iter_end;
849         }
850
851         BLI_mutex_lock(&data->mutex);
852
853         /* for flatten center */
854         if (area_cos) {
855                 add_v3_v3(area_cos[0], private_co[0]);
856                 add_v3_v3(area_cos[1], private_co[1]);
857         }
858
859         /* for area normal */
860         if (area_nos) {
861                 add_v3_v3(area_nos[0], private_no[0]);
862                 add_v3_v3(area_nos[1], private_no[1]);
863         }
864
865         /* weights */
866         data->count[0] += private_count[0];
867         data->count[1] += private_count[1];
868
869         BLI_mutex_unlock(&data->mutex);
870 }
871
872 static void calc_area_center(
873         Sculpt *sd, Object *ob,
874         PBVHNode **nodes, int totnode,
875         float r_area_co[3])
876 {
877         const Brush *brush = BKE_paint_brush(&sd->paint);
878         SculptSession *ss = ob->sculpt;
879         const bool has_bm_orco = ss->bm && sculpt_stroke_is_dynamic_topology(ss, brush);
880         int n;
881
882         /* 0=towards view, 1=flipped */
883         float area_cos[2][3] = {{0.0f}};
884
885         int count[2] = {0};
886
887         SculptThreadedTaskData data = {
888                 .sd = sd, .ob = ob, .nodes = nodes, .totnode = totnode,
889                 .has_bm_orco = has_bm_orco, .area_cos = area_cos, .area_nos = NULL, .count = count,
890         };
891         BLI_mutex_init(&data.mutex);
892
893         BLI_task_parallel_range(
894                     0, totnode, &data, calc_area_normal_and_center_task_cb,
895                     ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT));
896
897         BLI_mutex_end(&data.mutex);
898
899         /* for flatten center */
900         for (n = 0; n < ARRAY_SIZE(area_cos); n++) {
901                 if (count[n] != 0) {
902                         mul_v3_v3fl(r_area_co, area_cos[n], 1.0f / count[n]);
903                         break;
904                 }
905         }
906         if (n == 2) {
907                 zero_v3(r_area_co);
908         }
909 }
910
911
912 static void calc_area_normal(
913         Sculpt *sd, Object *ob,
914         PBVHNode **nodes, int totnode,
915         float r_area_no[3])
916 {
917         const Brush *brush = BKE_paint_brush(&sd->paint);
918         SculptSession *ss = ob->sculpt;
919         const bool has_bm_orco = ss->bm && sculpt_stroke_is_dynamic_topology(ss, brush);
920         int n;
921
922         /* 0=towards view, 1=flipped */
923         float area_nos[2][3] = {{0.0f}};
924
925         int count[2] = {0};
926
927         SculptThreadedTaskData data = {
928                 .sd = sd, .ob = ob, .nodes = nodes, .totnode = totnode,
929                 .has_bm_orco = has_bm_orco, .area_cos = NULL, .area_nos = area_nos, .count = count,
930         };
931         BLI_mutex_init(&data.mutex);
932
933         BLI_task_parallel_range(
934                     0, totnode, &data, calc_area_normal_and_center_task_cb,
935                     ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT));
936
937         BLI_mutex_end(&data.mutex);
938
939         /* for area normal */
940         for (n = 0; n < ARRAY_SIZE(area_nos); n++) {
941                 if (normalize_v3_v3(r_area_no, area_nos[n]) != 0.0f) {
942                         break;
943                 }
944         }
945 }
946
947 /* this calculates flatten center and area normal together,
948  * amortizing the memory bandwidth and loop overhead to calculate both at the same time */
949 static void calc_area_normal_and_center(
950         Sculpt *sd, Object *ob,
951         PBVHNode **nodes, int totnode,
952         float r_area_no[3], float r_area_co[3])
953 {
954         const Brush *brush = BKE_paint_brush(&sd->paint);
955         SculptSession *ss = ob->sculpt;
956         const bool has_bm_orco = ss->bm && sculpt_stroke_is_dynamic_topology(ss, brush);
957         int n;
958
959         /* 0=towards view, 1=flipped */
960         float area_cos[2][3] = {{0.0f}};
961         float area_nos[2][3] = {{0.0f}};
962
963         int count[2] = {0};
964
965         SculptThreadedTaskData data = {
966                 .sd = sd, .ob = ob, .nodes = nodes, .totnode = totnode,
967                 .has_bm_orco = has_bm_orco, .area_cos = area_cos, .area_nos = area_nos, .count = count,
968         };
969         BLI_mutex_init(&data.mutex);
970
971         BLI_task_parallel_range(
972                     0, totnode, &data, calc_area_normal_and_center_task_cb,
973                     ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT));
974
975         BLI_mutex_end(&data.mutex);
976
977         /* for flatten center */
978         for (n = 0; n < ARRAY_SIZE(area_cos); n++) {
979                 if (count[n] != 0) {
980                         mul_v3_v3fl(r_area_co, area_cos[n], 1.0f / count[n]);
981                         break;
982                 }
983         }
984         if (n == 2) {
985                 zero_v3(r_area_co);
986         }
987
988         /* for area normal */
989         for (n = 0; n < ARRAY_SIZE(area_nos); n++) {
990                 if (normalize_v3_v3(r_area_no, area_nos[n]) != 0.0f) {
991                         break;
992                 }
993         }
994 }
995
996 /** \} */
997
998
999 /* Return modified brush strength. Includes the direction of the brush, positive
1000  * values pull vertices, negative values push. Uses tablet pressure and a
1001  * special multiplier found experimentally to scale the strength factor. */
1002 static float brush_strength(
1003         const Sculpt *sd, const StrokeCache *cache,
1004         const float feather, const UnifiedPaintSettings *ups)
1005 {
1006         const Scene *scene = cache->vc->scene;
1007         const Brush *brush = BKE_paint_brush((Paint *)&sd->paint);
1008
1009         /* Primary strength input; square it to make lower values more sensitive */
1010         const float root_alpha = BKE_brush_alpha_get(scene, brush);
1011         float alpha        = root_alpha * root_alpha;
1012         float dir          = (brush->flag & BRUSH_DIR_IN) ? -1 : 1;
1013         float pressure     = BKE_brush_use_alpha_pressure(scene, brush) ? cache->pressure : 1;
1014         float pen_flip     = cache->pen_flip ? -1 : 1;
1015         float invert       = cache->invert ? -1 : 1;
1016         float overlap       = ups->overlap_factor;
1017         /* spacing is integer percentage of radius, divide by 50 to get
1018          * normalized diameter */
1019
1020         float flip         = dir * invert * pen_flip;
1021
1022         switch (brush->sculpt_tool) {
1023                 case SCULPT_TOOL_CLAY:
1024                 case SCULPT_TOOL_CLAY_STRIPS:
1025                 case SCULPT_TOOL_DRAW:
1026                 case SCULPT_TOOL_LAYER:
1027                         return alpha * flip * pressure * overlap * feather;
1028                         
1029                 case SCULPT_TOOL_MASK:
1030                         overlap = (1 + overlap) / 2;
1031                         switch ((BrushMaskTool)brush->mask_tool) {
1032                                 case BRUSH_MASK_DRAW:
1033                                         return alpha * flip * pressure * overlap * feather;
1034                                 case BRUSH_MASK_SMOOTH:
1035                                         return alpha * pressure * feather;
1036                         }
1037
1038                 case SCULPT_TOOL_CREASE:
1039                 case SCULPT_TOOL_BLOB:
1040                         return alpha * flip * pressure * overlap * feather;
1041
1042                 case SCULPT_TOOL_INFLATE:
1043                         if (flip > 0) {
1044                                 return 0.250f * alpha * flip * pressure * overlap * feather;
1045                         }
1046                         else {
1047                                 return 0.125f * alpha * flip * pressure * overlap * feather;
1048                         }
1049
1050                 case SCULPT_TOOL_FILL:
1051                 case SCULPT_TOOL_SCRAPE:
1052                 case SCULPT_TOOL_FLATTEN:
1053                         if (flip > 0) {
1054                                 overlap = (1 + overlap) / 2;
1055                                 return alpha * flip * pressure * overlap * feather;
1056                         }
1057                         else {
1058                                 /* reduce strength for DEEPEN, PEAKS, and CONTRAST */
1059                                 return 0.5f * alpha * flip * pressure * overlap * feather; 
1060                         }
1061
1062                 case SCULPT_TOOL_SMOOTH:
1063                         return alpha * pressure * feather;
1064
1065                 case SCULPT_TOOL_PINCH:
1066                         if (flip > 0) {
1067                                 return alpha * flip * pressure * overlap * feather;
1068                         }
1069                         else {
1070                                 return 0.25f * alpha * flip * pressure * overlap * feather;
1071                         }
1072
1073                 case SCULPT_TOOL_NUDGE:
1074                         overlap = (1 + overlap) / 2;
1075                         return alpha * pressure * overlap * feather;
1076
1077                 case SCULPT_TOOL_THUMB:
1078                         return alpha * pressure * feather;
1079
1080                 case SCULPT_TOOL_SNAKE_HOOK:
1081                         return root_alpha * feather;
1082
1083                 case SCULPT_TOOL_GRAB:
1084                         return root_alpha * feather;
1085
1086                 case SCULPT_TOOL_ROTATE:
1087                         return alpha * pressure * feather;
1088
1089                 default:
1090                         return 0;
1091         }
1092 }
1093
1094 /* Return a multiplier for brush strength on a particular vertex. */
1095 float tex_strength(SculptSession *ss, Brush *br,
1096                    const float brush_point[3],
1097                    const float len,
1098                    const short vno[3],
1099                    const float fno[3],
1100                    const float mask,
1101                    const int thread_id)
1102 {
1103         StrokeCache *cache = ss->cache;
1104         const Scene *scene = cache->vc->scene;
1105         MTex *mtex = &br->mtex;
1106         float avg = 1;
1107         float rgba[4];
1108         float point[3];
1109
1110         sub_v3_v3v3(point, brush_point, cache->plane_offset);
1111
1112         if (!mtex->tex) {
1113                 avg = 1;
1114         }
1115         else if (mtex->brush_map_mode == MTEX_MAP_MODE_3D) {
1116                 /* Get strength by feeding the vertex 
1117                  * location directly into a texture */
1118                 avg = BKE_brush_sample_tex_3D(scene, br, point, rgba, 0, ss->tex_pool);
1119         }
1120         else if (ss->texcache) {
1121                 float symm_point[3], point_2d[2];
1122                 float x = 0.0f, y = 0.0f; /* Quite warnings */
1123
1124                 /* if the active area is being applied for symmetry, flip it
1125                  * across the symmetry axis and rotate it back to the original
1126                  * position in order to project it. This insures that the 
1127                  * brush texture will be oriented correctly. */
1128
1129                 flip_v3_v3(symm_point, point, cache->mirror_symmetry_pass);
1130
1131                 if (cache->radial_symmetry_pass)
1132                         mul_m4_v3(cache->symm_rot_mat_inv, symm_point);
1133
1134                 ED_view3d_project_float_v2_m4(cache->vc->ar, symm_point, point_2d, cache->projection_mat);
1135
1136                 /* still no symmetry supported for other paint modes.
1137                  * Sculpt does it DIY */
1138                 if (mtex->brush_map_mode == MTEX_MAP_MODE_AREA) {
1139                         /* Similar to fixed mode, but projects from brush angle
1140                          * rather than view direction */
1141
1142                         mul_m4_v3(cache->brush_local_mat, symm_point);
1143
1144                         x = symm_point[0];
1145                         y = symm_point[1];
1146
1147                         x *= br->mtex.size[0];
1148                         y *= br->mtex.size[1];
1149
1150                         x += br->mtex.ofs[0];
1151                         y += br->mtex.ofs[1];
1152
1153                         avg = paint_get_tex_pixel(&br->mtex, x, y, ss->tex_pool, thread_id);
1154
1155                         avg += br->texture_sample_bias;
1156                 }
1157                 else {
1158                         const float point_3d[3] = {point_2d[0], point_2d[1], 0.0f};
1159                         avg = BKE_brush_sample_tex_3D(scene, br, point_3d, rgba, 0, ss->tex_pool);
1160                 }
1161         }
1162
1163         /* Falloff curve */
1164         avg *= BKE_brush_curve_strength(br, len, cache->radius);
1165
1166         avg *= frontface(br, cache->view_normal, vno, fno);
1167
1168         /* Paint mask */
1169         avg *= 1.0f - mask;
1170
1171         return avg;
1172 }
1173
1174 /* Test AABB against sphere */
1175 bool sculpt_search_sphere_cb(PBVHNode *node, void *data_v)
1176 {
1177         SculptSearchSphereData *data = data_v;
1178         float *center = data->ss->cache->location, nearest[3];
1179         float t[3], bb_min[3], bb_max[3];
1180         int i;
1181
1182         if (data->original)
1183                 BKE_pbvh_node_get_original_BB(node, bb_min, bb_max);
1184         else
1185                 BKE_pbvh_node_get_BB(node, bb_min, bb_max);
1186         
1187         for (i = 0; i < 3; ++i) {
1188                 if (bb_min[i] > center[i])
1189                         nearest[i] = bb_min[i];
1190                 else if (bb_max[i] < center[i])
1191                         nearest[i] = bb_max[i];
1192                 else
1193                         nearest[i] = center[i]; 
1194         }
1195         
1196         sub_v3_v3v3(t, center, nearest);
1197
1198         return len_squared_v3(t) < data->radius_squared;
1199 }
1200
1201 /* Handles clipping against a mirror modifier and SCULPT_LOCK axis flags */
1202 static void sculpt_clip(Sculpt *sd, SculptSession *ss, float co[3], const float val[3])
1203 {
1204         int i;
1205
1206         for (i = 0; i < 3; ++i) {
1207                 if (sd->flags & (SCULPT_LOCK_X << i))
1208                         continue;
1209
1210                 if ((ss->cache->flag & (CLIP_X << i)) && (fabsf(co[i]) <= ss->cache->clip_tolerance[i]))
1211                         co[i] = 0.0f;
1212                 else
1213                         co[i] = val[i];
1214         }
1215 }
1216
1217 /* Calculate primary direction of movement for many brushes */
1218 static void calc_sculpt_normal(
1219         Sculpt *sd, Object *ob,
1220         PBVHNode **nodes, int totnode,
1221         float r_area_no[3])
1222 {
1223         const Brush *brush = BKE_paint_brush(&sd->paint);
1224         const SculptSession *ss = ob->sculpt;
1225
1226         switch (brush->sculpt_plane) {
1227                 case SCULPT_DISP_DIR_VIEW:
1228                         copy_v3_v3(r_area_no, ss->cache->true_view_normal);
1229                         break;
1230
1231                 case SCULPT_DISP_DIR_X:
1232                         ARRAY_SET_ITEMS(r_area_no, 1, 0, 0);
1233                         break;
1234
1235                 case SCULPT_DISP_DIR_Y:
1236                         ARRAY_SET_ITEMS(r_area_no, 0, 1, 0);
1237                         break;
1238
1239                 case SCULPT_DISP_DIR_Z:
1240                         ARRAY_SET_ITEMS(r_area_no, 0, 0, 1);
1241                         break;
1242
1243                 case SCULPT_DISP_DIR_AREA:
1244                         calc_area_normal(sd, ob, nodes, totnode, r_area_no);
1245                         break;
1246
1247                 default:
1248                         break;
1249         }
1250 }
1251
1252 static void update_sculpt_normal(Sculpt *sd, Object *ob,
1253                                  PBVHNode **nodes, int totnode)
1254 {
1255         const Brush *brush = BKE_paint_brush(&sd->paint);
1256         StrokeCache *cache = ob->sculpt->cache;
1257         
1258         if (cache->mirror_symmetry_pass == 0 &&
1259             cache->radial_symmetry_pass == 0 &&
1260             (cache->first_time || !(brush->flag & BRUSH_ORIGINAL_NORMAL)))
1261         {
1262                 calc_sculpt_normal(sd, ob, nodes, totnode, cache->sculpt_normal);
1263                 copy_v3_v3(cache->sculpt_normal_symm, cache->sculpt_normal);
1264         }
1265         else {
1266                 copy_v3_v3(cache->sculpt_normal_symm, cache->sculpt_normal);
1267                 flip_v3(cache->sculpt_normal_symm, cache->mirror_symmetry_pass);
1268                 mul_m4_v3(cache->symm_rot_mat, cache->sculpt_normal_symm);
1269         }
1270 }
1271
1272 static void calc_local_y(ViewContext *vc, const float center[3], float y[3])
1273 {
1274         Object *ob = vc->obact;
1275         float loc[3], mval_f[2] = {0.0f, 1.0f};
1276         float zfac;
1277
1278         mul_v3_m4v3(loc, ob->imat, center);
1279         zfac = ED_view3d_calc_zfac(vc->rv3d, loc, NULL);
1280
1281         ED_view3d_win_to_delta(vc->ar, mval_f, y, zfac);
1282         normalize_v3(y);
1283
1284         add_v3_v3(y, ob->loc);
1285         mul_m4_v3(ob->imat, y);
1286 }
1287
1288 static void calc_brush_local_mat(const Brush *brush, Object *ob,
1289                                  float local_mat[4][4])
1290 {
1291         const StrokeCache *cache = ob->sculpt->cache;
1292         float tmat[4][4];
1293         float mat[4][4];
1294         float scale[4][4];
1295         float angle, v[3];
1296         float up[3];
1297
1298         /* Ensure ob->imat is up to date */
1299         invert_m4_m4(ob->imat, ob->obmat);
1300
1301         /* Initialize last column of matrix */
1302         mat[0][3] = 0;
1303         mat[1][3] = 0;
1304         mat[2][3] = 0;
1305         mat[3][3] = 1;
1306
1307         /* Get view's up vector in object-space */
1308         calc_local_y(cache->vc, cache->location, up);
1309
1310         /* Calculate the X axis of the local matrix */
1311         cross_v3_v3v3(v, up, cache->sculpt_normal);
1312         /* Apply rotation (user angle, rake, etc.) to X axis */
1313         angle = brush->mtex.rot - cache->special_rotation;
1314         rotate_v3_v3v3fl(mat[0], v, cache->sculpt_normal, angle);
1315
1316         /* Get other axes */
1317         cross_v3_v3v3(mat[1], cache->sculpt_normal, mat[0]);
1318         copy_v3_v3(mat[2], cache->sculpt_normal);
1319
1320         /* Set location */
1321         copy_v3_v3(mat[3], cache->location);
1322
1323         /* Scale by brush radius */
1324         normalize_m4(mat);
1325         scale_m4_fl(scale, cache->radius);
1326         mul_m4_m4m4(tmat, mat, scale);
1327
1328         /* Return inverse (for converting from modelspace coords to local
1329          * area coords) */
1330         invert_m4_m4(local_mat, tmat);
1331 }
1332
1333 static void update_brush_local_mat(Sculpt *sd, Object *ob)
1334 {
1335         StrokeCache *cache = ob->sculpt->cache;
1336
1337         if (cache->mirror_symmetry_pass == 0 &&
1338             cache->radial_symmetry_pass == 0)
1339         {
1340                 calc_brush_local_mat(BKE_paint_brush(&sd->paint), ob,
1341                                      cache->brush_local_mat);
1342         }
1343 }
1344
1345 /* For the smooth brush, uses the neighboring vertices around vert to calculate
1346  * a smoothed location for vert. Skips corner vertices (used by only one
1347  * polygon.) */
1348 static void neighbor_average(SculptSession *ss, float avg[3], unsigned vert)
1349 {
1350         const MeshElemMap *vert_map = &ss->pmap[vert];
1351         const MVert *mvert = ss->mvert;
1352         float (*deform_co)[3] = ss->deform_cos;
1353
1354         /* Don't modify corner vertices */
1355         if (vert_map->count > 1) {
1356                 int i, total = 0;
1357
1358                 zero_v3(avg);
1359
1360                 for (i = 0; i < vert_map->count; i++) {
1361                         const MPoly *p = &ss->mpoly[vert_map->indices[i]];
1362                         unsigned f_adj_v[2];
1363
1364                         if (poly_get_adj_loops_from_vert(p, ss->mloop, vert, f_adj_v) != -1) {
1365                                 int j;
1366                                 for (j = 0; j < ARRAY_SIZE(f_adj_v); j += 1) {
1367                                         if (vert_map->count != 2 || ss->pmap[f_adj_v[j]].count <= 2) {
1368                                                 add_v3_v3(avg, deform_co ? deform_co[f_adj_v[j]] :
1369                                                           mvert[f_adj_v[j]].co);
1370
1371                                                 total++;
1372                                         }
1373                                 }
1374                         }
1375                 }
1376
1377                 if (total > 0) {
1378                         mul_v3_fl(avg, 1.0f / total);
1379                         return;
1380                 }
1381         }
1382
1383         copy_v3_v3(avg, deform_co ? deform_co[vert] : mvert[vert].co);
1384 }
1385
1386 /* Similar to neighbor_average(), but returns an averaged mask value
1387  * instead of coordinate. Also does not restrict based on border or
1388  * corner vertices. */
1389 static float neighbor_average_mask(SculptSession *ss, unsigned vert)
1390 {
1391         const float *vmask = ss->vmask;
1392         float avg = 0;
1393         int i, total = 0;
1394
1395         for (i = 0; i < ss->pmap[vert].count; i++) {
1396                 const MPoly *p = &ss->mpoly[ss->pmap[vert].indices[i]];
1397                 unsigned f_adj_v[2];
1398
1399                 if (poly_get_adj_loops_from_vert(p, ss->mloop, vert, f_adj_v) != -1) {
1400                         int j;
1401                         for (j = 0; j < ARRAY_SIZE(f_adj_v); j += 1) {
1402                                 avg += vmask[f_adj_v[j]];
1403                                 total++;
1404                         }
1405                 }
1406         }
1407
1408         if (total > 0)
1409                 return avg / (float)total;
1410         else
1411                 return vmask[vert];
1412 }
1413
1414 /* Same logic as neighbor_average(), but for bmesh rather than mesh */
1415 static void bmesh_neighbor_average(float avg[3], BMVert *v)
1416 {
1417         /* logic for 3 or more is identical */
1418         const int vfcount = BM_vert_face_count_ex(v, 3);
1419
1420         /* Don't modify corner vertices */
1421         if (vfcount > 1) {
1422                 BMIter liter;
1423                 BMLoop *l;
1424                 int i, total = 0;
1425
1426                 zero_v3(avg);
1427
1428                 BM_ITER_ELEM (l, &liter, v, BM_LOOPS_OF_VERT) {
1429                         const BMVert *adj_v[2] = {l->prev->v, l->next->v};
1430
1431                         for (i = 0; i < ARRAY_SIZE(adj_v); i++) {
1432                                 const BMVert *v_other = adj_v[i];
1433                                 if (vfcount != 2 || BM_vert_face_count_ex(v_other, 2) <= 2) {
1434                                         add_v3_v3(avg, v_other->co);
1435                                         total++;
1436                                 }
1437                         }
1438                 }
1439
1440                 if (total > 0) {
1441                         mul_v3_fl(avg, 1.0f / total);
1442                         return;
1443                 }
1444         }
1445
1446         copy_v3_v3(avg, v->co);
1447 }
1448
1449 /* Same logic as neighbor_average_mask(), but for bmesh rather than mesh */
1450 static float bmesh_neighbor_average_mask(BMVert *v, const int cd_vert_mask_offset)
1451 {
1452         BMIter liter;
1453         BMLoop *l;
1454         float avg = 0;
1455         int i, total = 0;
1456
1457         BM_ITER_ELEM (l, &liter, v, BM_LOOPS_OF_VERT) {
1458                 /* skip this vertex */
1459                 const BMVert *adj_v[2] = {l->prev->v, l->next->v};
1460
1461                 for (i = 0; i < ARRAY_SIZE(adj_v); i++) {
1462                         const BMVert *v_other = adj_v[i];
1463                         const float *vmask = BM_ELEM_CD_GET_VOID_P(v_other, cd_vert_mask_offset);
1464                         avg += (*vmask);
1465                         total++;
1466                 }
1467         }
1468
1469         if (total > 0) {
1470                 return avg / (float)total;
1471         }
1472         else {
1473                 const float *vmask = BM_ELEM_CD_GET_VOID_P(v, cd_vert_mask_offset);
1474                 return (*vmask);
1475         }
1476 }
1477
1478 /* Note: uses after-struct allocated mem to store actual cache... */
1479 typedef struct SculptDoBrushSmoothGridDataChunk {
1480         size_t tmpgrid_size;
1481 } SculptDoBrushSmoothGridDataChunk;
1482
1483 typedef struct {
1484         SculptSession *ss;
1485         const float *ray_start, *ray_normal;
1486         bool hit;
1487         float dist;
1488         bool original;
1489         PBVHNode* node;
1490 } SculptRaycastData;
1491
1492 typedef struct {
1493         const float *ray_start, *ray_normal;
1494         bool hit;
1495         float dist;
1496         float detail;
1497 } SculptDetailRaycastData;
1498
1499 static void do_smooth_brush_mesh_task_cb_ex(
1500         void *userdata, void *UNUSED(userdata_chunk), const int n, const int thread_id)
1501 {
1502         SculptThreadedTaskData *data = userdata;
1503         SculptSession *ss = data->ob->sculpt;
1504         Sculpt *sd = data->sd;
1505         Brush *brush = data->brush;
1506         const bool smooth_mask = data->smooth_mask;
1507         float bstrength = data->strength;
1508
1509         PBVHVertexIter vd;
1510         SculptBrushTest test;
1511
1512         CLAMP(bstrength, 0.0f, 1.0f);
1513
1514         sculpt_brush_test_init(ss, &test);
1515
1516         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
1517         {
1518                 if (sculpt_brush_test(&test, vd.co)) {
1519                         const float fade = bstrength * tex_strength(
1520                                                ss, brush, vd.co, test.dist, vd.no, vd.fno,
1521                                                smooth_mask ? 0.0f : (vd.mask ? *vd.mask : 0.0f),
1522                                                thread_id);
1523                         if (smooth_mask) {
1524                                 float val = neighbor_average_mask(ss, vd.vert_indices[vd.i]) - *vd.mask;
1525                                 val *= fade * bstrength;
1526                                 *vd.mask += val;
1527                                 CLAMP(*vd.mask, 0.0f, 1.0f);
1528                         }
1529                         else {
1530                                 float avg[3], val[3];
1531
1532                                 neighbor_average(ss, avg, vd.vert_indices[vd.i]);
1533                                 sub_v3_v3v3(val, avg, vd.co);
1534
1535                                 madd_v3_v3v3fl(val, vd.co, val, fade);
1536
1537                                 sculpt_clip(sd, ss, vd.co, val);
1538                         }
1539
1540                         if (vd.mvert)
1541                                 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
1542                 }
1543         }
1544         BKE_pbvh_vertex_iter_end;
1545 }
1546
1547 static void do_smooth_brush_bmesh_task_cb_ex(
1548         void *userdata, void *UNUSED(userdata_chunk), const int n, const int thread_id)
1549 {
1550         SculptThreadedTaskData *data = userdata;
1551         SculptSession *ss = data->ob->sculpt;
1552         Sculpt *sd = data->sd;
1553         Brush *brush = data->brush;
1554         const bool smooth_mask = data->smooth_mask;
1555         float bstrength = data->strength;
1556
1557         PBVHVertexIter vd;
1558         SculptBrushTest test;
1559
1560         CLAMP(bstrength, 0.0f, 1.0f);
1561
1562         sculpt_brush_test_init(ss, &test);
1563
1564         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
1565         {
1566                 if (sculpt_brush_test(&test, vd.co)) {
1567                         const float fade = bstrength * tex_strength(
1568                                                ss, brush, vd.co, test.dist, vd.no, vd.fno, smooth_mask ? 0.0f : *vd.mask,
1569                                                thread_id);
1570                         if (smooth_mask) {
1571                                 float val = bmesh_neighbor_average_mask(vd.bm_vert, vd.cd_vert_mask_offset) - *vd.mask;
1572                                 val *= fade * bstrength;
1573                                 *vd.mask += val;
1574                                 CLAMP(*vd.mask, 0.0f, 1.0f);
1575                         }
1576                         else {
1577                                 float avg[3], val[3];
1578
1579                                 bmesh_neighbor_average(avg, vd.bm_vert);
1580                                 sub_v3_v3v3(val, avg, vd.co);
1581
1582                                 madd_v3_v3v3fl(val, vd.co, val, fade);
1583
1584                                 sculpt_clip(sd, ss, vd.co, val);
1585                         }
1586
1587                         if (vd.mvert)
1588                                 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
1589                 }
1590         }
1591         BKE_pbvh_vertex_iter_end;
1592 }
1593
1594 static void do_smooth_brush_multires_task_cb_ex(
1595         void *userdata, void *userdata_chunk, const int n, const int thread_id)
1596 {
1597         SculptThreadedTaskData *data = userdata;
1598         SculptDoBrushSmoothGridDataChunk *data_chunk = userdata_chunk;
1599         SculptSession *ss = data->ob->sculpt;
1600         Sculpt *sd = data->sd;
1601         Brush *brush = data->brush;
1602         const bool smooth_mask = data->smooth_mask;
1603         float bstrength = data->strength;
1604
1605         SculptBrushTest test;
1606         CCGElem **griddata, *gddata;
1607         CCGKey key;
1608
1609         float (*tmpgrid_co)[3] = NULL;
1610         float tmprow_co[2][3];
1611         float *tmpgrid_mask = NULL;
1612         float tmprow_mask[2];
1613
1614         BLI_bitmap * const *grid_hidden;
1615         int *grid_indices, totgrid, gridsize;
1616         int i, x, y;
1617
1618         sculpt_brush_test_init(ss, &test);
1619
1620         CLAMP(bstrength, 0.0f, 1.0f);
1621
1622         BKE_pbvh_node_get_grids(ss->pbvh, data->nodes[n], &grid_indices, &totgrid, NULL, &gridsize, &griddata);
1623         BKE_pbvh_get_grid_key(ss->pbvh, &key);
1624
1625         grid_hidden = BKE_pbvh_grid_hidden(ss->pbvh);
1626
1627         if (smooth_mask)
1628                 tmpgrid_mask = (void *)(data_chunk + 1);
1629         else
1630                 tmpgrid_co = (void *)(data_chunk + 1);
1631
1632         for (i = 0; i < totgrid; i++) {
1633                 int gi = grid_indices[i];
1634                 const BLI_bitmap *gh = grid_hidden[gi];
1635                 gddata = griddata[gi];
1636
1637                 if (smooth_mask)
1638                         memset(tmpgrid_mask, 0, data_chunk->tmpgrid_size);
1639                 else
1640                         memset(tmpgrid_co, 0, data_chunk->tmpgrid_size);
1641
1642                 for (y = 0; y < gridsize - 1; y++) {
1643                         const int v = y * gridsize;
1644                         if (smooth_mask) {
1645                                 tmprow_mask[0] = (*CCG_elem_offset_mask(&key, gddata, v) +
1646                                                   *CCG_elem_offset_mask(&key, gddata, v + gridsize));
1647                         }
1648                         else {
1649                                 add_v3_v3v3(tmprow_co[0],
1650                                             CCG_elem_offset_co(&key, gddata, v),
1651                                             CCG_elem_offset_co(&key, gddata, v + gridsize));
1652                         }
1653
1654                         for (x = 0; x < gridsize - 1; x++) {
1655                                 const int v1 = x + y * gridsize;
1656                                 const int v2 = v1 + 1;
1657                                 const int v3 = v1 + gridsize;
1658                                 const int v4 = v3 + 1;
1659
1660                                 if (smooth_mask) {
1661                                         float tmp;
1662
1663                                         tmprow_mask[(x + 1) % 2] = (*CCG_elem_offset_mask(&key, gddata, v2) +
1664                                                                     *CCG_elem_offset_mask(&key, gddata, v4));
1665                                         tmp = tmprow_mask[(x + 1) % 2] + tmprow_mask[x % 2];
1666
1667                                         tmpgrid_mask[v1] += tmp;
1668                                         tmpgrid_mask[v2] += tmp;
1669                                         tmpgrid_mask[v3] += tmp;
1670                                         tmpgrid_mask[v4] += tmp;
1671                                 }
1672                                 else {
1673                                         float tmp[3];
1674
1675                                         add_v3_v3v3(tmprow_co[(x + 1) % 2],
1676                                                     CCG_elem_offset_co(&key, gddata, v2),
1677                                                     CCG_elem_offset_co(&key, gddata, v4));
1678                                         add_v3_v3v3(tmp, tmprow_co[(x + 1) % 2], tmprow_co[x % 2]);
1679
1680                                         add_v3_v3(tmpgrid_co[v1], tmp);
1681                                         add_v3_v3(tmpgrid_co[v2], tmp);
1682                                         add_v3_v3(tmpgrid_co[v3], tmp);
1683                                         add_v3_v3(tmpgrid_co[v4], tmp);
1684                                 }
1685                         }
1686                 }
1687
1688                 /* blend with existing coordinates */
1689                 for (y = 0; y < gridsize; y++) {
1690                         for (x = 0; x < gridsize; x++) {
1691                                 float *co;
1692                                 const float *fno;
1693                                 float *mask;
1694                                 const int index = y * gridsize + x;
1695
1696                                 if (gh) {
1697                                         if (BLI_BITMAP_TEST(gh, index))
1698                                                 continue;
1699                                 }
1700
1701                                 co = CCG_elem_offset_co(&key, gddata, index);
1702                                 fno = CCG_elem_offset_no(&key, gddata, index);
1703                                 mask = CCG_elem_offset_mask(&key, gddata, index);
1704
1705                                 if (sculpt_brush_test(&test, co)) {
1706                                         const float strength_mask = (smooth_mask ? 0.0f : *mask);
1707                                         const float fade = bstrength * tex_strength(
1708                                                                ss, brush, co, test.dist, NULL, fno, strength_mask, thread_id);
1709                                         float f = 1.0f / 16.0f;
1710
1711                                         if (x == 0 || x == gridsize - 1)
1712                                                 f *= 2.0f;
1713
1714                                         if (y == 0 || y == gridsize - 1)
1715                                                 f *= 2.0f;
1716
1717                                         if (smooth_mask) {
1718                                                 *mask += ((tmpgrid_mask[index] * f) - *mask) * fade;
1719                                         }
1720                                         else {
1721                                                 float *avg = tmpgrid_co[index];
1722                                                 float val[3];
1723
1724                                                 mul_v3_fl(avg, f);
1725                                                 sub_v3_v3v3(val, avg, co);
1726                                                 madd_v3_v3v3fl(val, co, val, fade);
1727
1728                                                 sculpt_clip(sd, ss, co, val);
1729                                         }
1730                                 }
1731                         }
1732                 }
1733         }
1734 }
1735
1736 static void smooth(
1737         Sculpt *sd, Object *ob, PBVHNode **nodes, const int totnode, float bstrength, const bool smooth_mask)
1738 {
1739         SculptSession *ss = ob->sculpt;
1740         Brush *brush = BKE_paint_brush(&sd->paint);
1741
1742         const int max_iterations = 4;
1743         const float fract = 1.0f / max_iterations;
1744         PBVHType type = BKE_pbvh_type(ss->pbvh);
1745         int iteration, count;
1746         float last;
1747
1748         CLAMP(bstrength, 0.0f, 1.0f);
1749
1750         count = (int)(bstrength * max_iterations);
1751         last  = max_iterations * (bstrength - count * fract);
1752
1753         if (type == PBVH_FACES && !ss->pmap) {
1754                 BLI_assert(!"sculpt smooth: pmap missing");
1755                 return;
1756         }
1757
1758         for (iteration = 0; iteration <= count; ++iteration) {
1759                 const float strength = (iteration != count) ? 1.0f : last;
1760
1761                 SculptThreadedTaskData data = {
1762                     .sd = sd, .ob = ob, .brush = brush, .nodes = nodes,
1763                     .smooth_mask = smooth_mask, .strength = strength,
1764                 };
1765
1766                 switch (type) {
1767                         case PBVH_GRIDS:
1768                         {
1769                                 int gridsize;
1770                                 size_t size;
1771                                 SculptDoBrushSmoothGridDataChunk *data_chunk;
1772
1773                                 BKE_pbvh_node_get_grids(ss->pbvh, NULL, NULL, NULL, NULL, &gridsize, NULL);
1774                                 size = (size_t)gridsize;
1775                                 size = sizeof(float) * size * size * (smooth_mask ? 1 : 3);
1776                                 data_chunk = MEM_mallocN(sizeof(*data_chunk) + size, __func__);
1777                                 data_chunk->tmpgrid_size = size;
1778                                 size += sizeof(*data_chunk);
1779
1780                                 BLI_task_parallel_range_ex(
1781                                             0, totnode, &data, data_chunk, size, do_smooth_brush_multires_task_cb_ex,
1782                                             ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT), false);
1783
1784                                 MEM_freeN(data_chunk);
1785                                 break;
1786                         }
1787                         case PBVH_FACES:
1788                                 BLI_task_parallel_range_ex(
1789                                             0, totnode, &data, NULL, 0, do_smooth_brush_mesh_task_cb_ex,
1790                                             ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT), false);
1791                                 break;
1792                         case PBVH_BMESH:
1793                                 BLI_task_parallel_range_ex(
1794                                             0, totnode, &data, NULL, 0, do_smooth_brush_bmesh_task_cb_ex,
1795                                             ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT), false);
1796                                 break;
1797                 }
1798
1799                 if (ss->multires)
1800                         multires_stitch_grids(ob);
1801         }
1802 }
1803
1804 static void do_smooth_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
1805 {
1806         SculptSession *ss = ob->sculpt;
1807         smooth(sd, ob, nodes, totnode, ss->cache->bstrength, false);
1808 }
1809
1810 static void do_mask_brush_draw_task_cb_ex(
1811         void *userdata, void *UNUSED(userdata_chunk), const int n, const int thread_id)
1812 {
1813         SculptThreadedTaskData *data = userdata;
1814         SculptSession *ss = data->ob->sculpt;
1815         Brush *brush = data->brush;
1816         const float bstrength = ss->cache->bstrength;
1817
1818         PBVHVertexIter vd;
1819         SculptBrushTest test;
1820
1821         sculpt_brush_test_init(ss, &test);
1822
1823         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
1824         {
1825                 if (sculpt_brush_test(&test, vd.co)) {
1826                         const float fade = tex_strength(ss, brush, vd.co, test.dist, vd.no, vd.fno, 0.0f, thread_id);
1827
1828                         (*vd.mask) += fade * bstrength;
1829                         CLAMP(*vd.mask, 0, 1);
1830
1831                         if (vd.mvert)
1832                                 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
1833                 }
1834                 BKE_pbvh_vertex_iter_end;
1835         }
1836 }
1837
1838 static void do_mask_brush_draw(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
1839 {
1840         Brush *brush = BKE_paint_brush(&sd->paint);
1841
1842         /* threaded loop over nodes */
1843         SculptThreadedTaskData data = {
1844             .sd = sd, .ob = ob, .brush = brush, .nodes = nodes,
1845         };
1846
1847         BLI_task_parallel_range_ex(
1848                     0, totnode, &data, NULL, 0, do_mask_brush_draw_task_cb_ex,
1849                     ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT), false);
1850 }
1851
1852 static void do_mask_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
1853 {
1854         SculptSession *ss = ob->sculpt;
1855         Brush *brush = BKE_paint_brush(&sd->paint);
1856         
1857         switch ((BrushMaskTool)brush->mask_tool) {
1858                 case BRUSH_MASK_DRAW:
1859                         do_mask_brush_draw(sd, ob, nodes, totnode);
1860                         break;
1861                 case BRUSH_MASK_SMOOTH:
1862                         smooth(sd, ob, nodes, totnode, ss->cache->bstrength, true);
1863                         break;
1864         }
1865 }
1866
1867 static void do_draw_brush_task_cb_ex(
1868         void *userdata, void *UNUSED(userdata_chunk), const int n, const int thread_id)
1869 {
1870         SculptThreadedTaskData *data = userdata;
1871         SculptSession *ss = data->ob->sculpt;
1872         Brush *brush = data->brush;
1873         const float *offset = data->offset;
1874
1875         PBVHVertexIter vd;
1876         SculptBrushTest test;
1877         float (*proxy)[3];
1878
1879         proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co;
1880
1881         sculpt_brush_test_init(ss, &test);
1882
1883         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
1884         {
1885                 if (sculpt_brush_test(&test, vd.co)) {
1886                         /* offset vertex */
1887                         const float fade = tex_strength(
1888                                                ss, brush, vd.co, test.dist, vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, thread_id);
1889
1890                         mul_v3_v3fl(proxy[vd.i], offset, fade);
1891
1892                         if (vd.mvert)
1893                                 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
1894                 }
1895         }
1896         BKE_pbvh_vertex_iter_end;
1897 }
1898
1899 static void do_draw_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
1900 {
1901         SculptSession *ss = ob->sculpt;
1902         Brush *brush = BKE_paint_brush(&sd->paint);
1903         float offset[3];
1904         const float bstrength = ss->cache->bstrength;
1905
1906         /* offset with as much as possible factored in already */
1907         mul_v3_v3fl(offset, ss->cache->sculpt_normal_symm, ss->cache->radius);
1908         mul_v3_v3(offset, ss->cache->scale);
1909         mul_v3_fl(offset, bstrength);
1910
1911         /* threaded loop over nodes */
1912         SculptThreadedTaskData data = {
1913             .sd = sd, .ob = ob, .brush = brush, .nodes = nodes,
1914             .offset = offset,
1915         };
1916
1917         BLI_task_parallel_range_ex(
1918                     0, totnode, &data, NULL, 0, do_draw_brush_task_cb_ex,
1919                     ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT), false);
1920 }
1921
1922 static void do_crease_brush_task_cb_ex(
1923         void *userdata, void *UNUSED(userdata_chunk), const int n, const int thread_id)
1924 {
1925         SculptThreadedTaskData *data = userdata;
1926         SculptSession *ss = data->ob->sculpt;
1927         Brush *brush = data->brush;
1928         SculptProjectVector *spvc = data->spvc;
1929         const float flippedbstrength = data->flippedbstrength;
1930         const float *offset = data->offset;
1931
1932         PBVHVertexIter vd;
1933         SculptBrushTest test;
1934         float (*proxy)[3];
1935
1936         proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co;
1937
1938         sculpt_brush_test_init(ss, &test);
1939
1940         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
1941         {
1942                 if (sculpt_brush_test(&test, vd.co)) {
1943                         /* offset vertex */
1944                         const float fade = tex_strength(
1945                                                ss, brush, vd.co, test.dist, vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, thread_id);
1946                         float val1[3];
1947                         float val2[3];
1948
1949                         /* first we pinch */
1950                         sub_v3_v3v3(val1, test.location, vd.co);
1951                         mul_v3_fl(val1, fade * flippedbstrength);
1952
1953                         sculpt_project_v3(spvc, val1, val1);
1954
1955                         /* then we draw */
1956                         mul_v3_v3fl(val2, offset, fade);
1957
1958                         add_v3_v3v3(proxy[vd.i], val1, val2);
1959
1960                         if (vd.mvert)
1961                                 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
1962                 }
1963         }
1964         BKE_pbvh_vertex_iter_end;
1965 }
1966
1967 static void do_crease_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
1968 {
1969         SculptSession *ss = ob->sculpt;
1970         const Scene *scene = ss->cache->vc->scene;
1971         Brush *brush = BKE_paint_brush(&sd->paint);
1972         float offset[3];
1973         float bstrength = ss->cache->bstrength;
1974         float flippedbstrength, crease_correction;
1975         float brush_alpha;
1976
1977         SculptProjectVector spvc;
1978
1979         /* offset with as much as possible factored in already */
1980         mul_v3_v3fl(offset, ss->cache->sculpt_normal_symm, ss->cache->radius);
1981         mul_v3_v3(offset, ss->cache->scale);
1982         mul_v3_fl(offset, bstrength);
1983         
1984         /* we divide out the squared alpha and multiply by the squared crease to give us the pinch strength */
1985         crease_correction = brush->crease_pinch_factor * brush->crease_pinch_factor;
1986         brush_alpha = BKE_brush_alpha_get(scene, brush);
1987         if (brush_alpha > 0.0f)
1988                 crease_correction /= brush_alpha * brush_alpha;
1989
1990         /* we always want crease to pinch or blob to relax even when draw is negative */
1991         flippedbstrength = (bstrength < 0) ? -crease_correction * bstrength : crease_correction * bstrength;
1992
1993         if (brush->sculpt_tool == SCULPT_TOOL_BLOB) flippedbstrength *= -1.0f;
1994
1995         /* Use surface normal for 'spvc', so the vertices are pinched towards a line instead of a single point.
1996          * Without this we get a 'flat' surface surrounding the pinch */
1997         sculpt_project_v3_cache_init(&spvc, ss->cache->sculpt_normal_symm);
1998
1999         /* threaded loop over nodes */
2000         SculptThreadedTaskData data = {
2001             .sd = sd, .ob = ob, .brush = brush, .nodes = nodes,
2002             .spvc = &spvc, .offset = offset, .flippedbstrength = flippedbstrength,
2003         };
2004
2005         BLI_task_parallel_range_ex(
2006                     0, totnode, &data, NULL, 0, do_crease_brush_task_cb_ex,
2007                     ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT), false);
2008 }
2009
2010 static void do_pinch_brush_task_cb_ex(
2011         void *userdata, void *UNUSED(userdata_chunk), const int n, const int thread_id)
2012 {
2013         SculptThreadedTaskData *data = userdata;
2014         SculptSession *ss = data->ob->sculpt;
2015         Brush *brush = data->brush;
2016
2017         PBVHVertexIter vd;
2018         SculptBrushTest test;
2019         float (*proxy)[3];
2020         const float bstrength = ss->cache->bstrength;
2021
2022         proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co;
2023
2024         sculpt_brush_test_init(ss, &test);
2025
2026         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
2027         {
2028                 if (sculpt_brush_test(&test, vd.co)) {
2029                         const float fade = bstrength * tex_strength(
2030                                                ss, brush, vd.co, test.dist, vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, thread_id);
2031                         float val[3];
2032
2033                         sub_v3_v3v3(val, test.location, vd.co);
2034                         mul_v3_v3fl(proxy[vd.i], val, fade);
2035
2036                         if (vd.mvert)
2037                                 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
2038                 }
2039         }
2040         BKE_pbvh_vertex_iter_end;
2041 }
2042
2043 static void do_pinch_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
2044 {
2045         Brush *brush = BKE_paint_brush(&sd->paint);
2046
2047         SculptThreadedTaskData data = {
2048             .sd = sd, .ob = ob, .brush = brush, .nodes = nodes,
2049         };
2050
2051         BLI_task_parallel_range_ex(
2052                     0, totnode, &data, NULL, 0, do_pinch_brush_task_cb_ex,
2053                     ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT), false);
2054 }
2055
2056 static void do_grab_brush_task_cb_ex(
2057         void *userdata, void *UNUSED(userdata_chunk), const int n, const int thread_id)
2058 {
2059         SculptThreadedTaskData *data = userdata;
2060         SculptSession *ss = data->ob->sculpt;
2061         Brush *brush = data->brush;
2062         const float *grab_delta = data->grab_delta;
2063
2064         PBVHVertexIter vd;
2065         SculptBrushTest test;
2066         SculptOrigVertData orig_data;
2067         float (*proxy)[3];
2068         const float bstrength = ss->cache->bstrength;
2069
2070         sculpt_orig_vert_data_init(&orig_data, data->ob, data->nodes[n]);
2071
2072         proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co;
2073
2074         sculpt_brush_test_init(ss, &test);
2075
2076         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
2077         {
2078                 sculpt_orig_vert_data_update(&orig_data, &vd);
2079
2080                 if (sculpt_brush_test(&test, orig_data.co)) {
2081                         const float fade = bstrength * tex_strength(
2082                                                ss, brush, orig_data.co, test.dist, orig_data.no, NULL, vd.mask ? *vd.mask : 0.0f,
2083                                                thread_id);
2084
2085                         mul_v3_v3fl(proxy[vd.i], grab_delta, fade);
2086
2087                         if (vd.mvert)
2088                                 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
2089                 }
2090         }
2091         BKE_pbvh_vertex_iter_end;
2092 }
2093
2094 static void do_grab_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
2095 {
2096         SculptSession *ss = ob->sculpt;
2097         Brush *brush = BKE_paint_brush(&sd->paint);
2098         float grab_delta[3];
2099
2100         copy_v3_v3(grab_delta, ss->cache->grab_delta_symmetry);
2101
2102         if (ss->cache->normal_weight > 0.0f) {
2103                 sculpt_project_v3_normal_align(ss, ss->cache->normal_weight, grab_delta);
2104         }
2105
2106         SculptThreadedTaskData data = {
2107             .sd = sd, .ob = ob, .brush = brush, .nodes = nodes,
2108             .grab_delta = grab_delta,
2109         };
2110
2111         BLI_task_parallel_range_ex(
2112                     0, totnode, &data, NULL, 0, do_grab_brush_task_cb_ex,
2113                     ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT), false);
2114 }
2115
2116 static void do_nudge_brush_task_cb_ex(
2117         void *userdata, void *UNUSED(userdata_chunk), const int n, const int thread_id)
2118 {
2119         SculptThreadedTaskData *data = userdata;
2120         SculptSession *ss = data->ob->sculpt;
2121         Brush *brush = data->brush;
2122         const float *cono = data->cono;
2123
2124         PBVHVertexIter vd;
2125         SculptBrushTest test;
2126         float (*proxy)[3];
2127         const float bstrength = ss->cache->bstrength;
2128
2129         proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co;
2130
2131         sculpt_brush_test_init(ss, &test);
2132
2133         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
2134         {
2135                 if (sculpt_brush_test(&test, vd.co)) {
2136                         const float fade = bstrength * tex_strength(
2137                                                ss, brush, vd.co, test.dist, vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, thread_id);
2138
2139                         mul_v3_v3fl(proxy[vd.i], cono, 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_nudge_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         float tmp[3], cono[3];
2154
2155         copy_v3_v3(grab_delta, ss->cache->grab_delta_symmetry);
2156
2157         cross_v3_v3v3(tmp, ss->cache->sculpt_normal_symm, grab_delta);
2158         cross_v3_v3v3(cono, tmp, ss->cache->sculpt_normal_symm);
2159
2160         SculptThreadedTaskData data = {
2161             .sd = sd, .ob = ob, .brush = brush, .nodes = nodes,
2162             .cono = cono,
2163         };
2164
2165         BLI_task_parallel_range_ex(
2166                     0, totnode, &data, NULL, 0, do_nudge_brush_task_cb_ex,
2167                     ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT), false);
2168 }
2169
2170 static void do_snake_hook_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         SculptProjectVector *spvc = data->spvc;
2177         const float *grab_delta = data->grab_delta;
2178
2179         PBVHVertexIter vd;
2180         SculptBrushTest test;
2181         float (*proxy)[3];
2182         const float bstrength = ss->cache->bstrength;
2183         const bool do_rake_rotation = ss->cache->is_rake_rotation_valid;
2184         const bool do_pinch = (brush->crease_pinch_factor != 0.5f);
2185         const float pinch = do_pinch ?
2186                 (2.0f * (0.5f - brush->crease_pinch_factor) * (len_v3(grab_delta) / ss->cache->radius)) : 0.0f;
2187
2188         proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co;
2189
2190         sculpt_brush_test_init(ss, &test);
2191
2192         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
2193         {
2194                 if (sculpt_brush_test(&test, vd.co)) {
2195                         const float fade = bstrength * tex_strength(
2196                                                ss, brush, vd.co, test.dist, vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, thread_id);
2197
2198                         mul_v3_v3fl(proxy[vd.i], grab_delta, fade);
2199
2200                         /* negative pinch will inflate, helps maintain volume */
2201                         if (do_pinch) {
2202                                 float delta_pinch_init[3], delta_pinch[3];
2203
2204                                 sub_v3_v3v3(delta_pinch, vd.co, test.location);
2205
2206                                 /* important to calculate based on the grabbed location (intentionally ignore fade here). */
2207                                 add_v3_v3(delta_pinch, grab_delta);
2208
2209                                 sculpt_project_v3(spvc, delta_pinch, delta_pinch);
2210
2211                                 copy_v3_v3(delta_pinch_init, delta_pinch);
2212
2213                                 float pinch_fade = pinch * fade;
2214                                 /* when reducing, scale reduction back by how close to the center we are,
2215                                  * so we don't pinch into nothingness */
2216                                 if (pinch > 0.0f) {
2217                                         /* square to have even less impact for close vertices */
2218                                         pinch_fade *= pow2f(min_ff(1.0f, len_v3(delta_pinch) / ss->cache->radius));
2219                                 }
2220                                 mul_v3_fl(delta_pinch, 1.0f + pinch_fade);
2221                                 sub_v3_v3v3(delta_pinch, delta_pinch_init, delta_pinch);
2222                                 add_v3_v3(proxy[vd.i], delta_pinch);
2223                         }
2224
2225                         if (do_rake_rotation) {
2226                                 float delta_rotate[3];
2227                                 sculpt_rake_rotate(ss, test.location, vd.co, fade, delta_rotate);
2228                                 add_v3_v3(proxy[vd.i], delta_rotate);
2229                         }
2230
2231                         if (vd.mvert)
2232                                 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
2233                 }
2234         }
2235         BKE_pbvh_vertex_iter_end;
2236 }
2237
2238 static void do_snake_hook_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
2239 {
2240         SculptSession *ss = ob->sculpt;
2241         Brush *brush = BKE_paint_brush(&sd->paint);
2242         const float bstrength = ss->cache->bstrength;
2243         float grab_delta[3];
2244
2245         SculptProjectVector spvc;
2246
2247         copy_v3_v3(grab_delta, ss->cache->grab_delta_symmetry);
2248
2249         if (bstrength < 0)
2250                 negate_v3(grab_delta);
2251
2252         if (ss->cache->normal_weight > 0.0f) {
2253                 sculpt_project_v3_normal_align(ss, ss->cache->normal_weight, grab_delta);
2254         }
2255
2256         /* optionally pinch while painting */
2257         if (brush->crease_pinch_factor != 0.5f) {
2258                 sculpt_project_v3_cache_init(&spvc, grab_delta);
2259         }
2260
2261         SculptThreadedTaskData data = {
2262             .sd = sd, .ob = ob, .brush = brush, .nodes = nodes,
2263             .spvc = &spvc, .grab_delta = grab_delta,
2264         };
2265
2266         BLI_task_parallel_range_ex(
2267                     0, totnode, &data, NULL, 0, do_snake_hook_brush_task_cb_ex,
2268                     ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT), false);
2269 }
2270
2271 static void do_thumb_brush_task_cb_ex(
2272         void *userdata, void *UNUSED(userdata_chunk), const int n, const int thread_id)
2273 {
2274         SculptThreadedTaskData *data = userdata;
2275         SculptSession *ss = data->ob->sculpt;
2276         Brush *brush = data->brush;
2277         const float *cono = data->cono;
2278
2279         PBVHVertexIter vd;
2280         SculptBrushTest test;
2281         SculptOrigVertData orig_data;
2282         float (*proxy)[3];
2283         const float bstrength = ss->cache->bstrength;
2284
2285         sculpt_orig_vert_data_init(&orig_data, data->ob, data->nodes[n]);
2286
2287         proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co;
2288
2289         sculpt_brush_test_init(ss, &test);
2290
2291         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
2292         {
2293                 sculpt_orig_vert_data_update(&orig_data, &vd);
2294
2295                 if (sculpt_brush_test(&test, orig_data.co)) {
2296                         const float fade = bstrength * tex_strength(
2297                                                ss, brush, orig_data.co, test.dist, orig_data.no, NULL, vd.mask ? *vd.mask : 0.0f,
2298                                                thread_id);
2299
2300                         mul_v3_v3fl(proxy[vd.i], cono, fade);
2301
2302                         if (vd.mvert)
2303                                 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
2304                 }
2305         }
2306         BKE_pbvh_vertex_iter_end;
2307 }
2308
2309 static void do_thumb_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
2310 {
2311         SculptSession *ss = ob->sculpt;
2312         Brush *brush = BKE_paint_brush(&sd->paint);
2313         float grab_delta[3];
2314         float tmp[3], cono[3];
2315
2316         copy_v3_v3(grab_delta, ss->cache->grab_delta_symmetry);
2317
2318         cross_v3_v3v3(tmp, ss->cache->sculpt_normal_symm, grab_delta);
2319         cross_v3_v3v3(cono, tmp, ss->cache->sculpt_normal_symm);
2320
2321         SculptThreadedTaskData data = {
2322             .sd = sd, .ob = ob, .brush = brush, .nodes = nodes,
2323             .cono = cono,
2324         };
2325
2326         BLI_task_parallel_range_ex(
2327                     0, totnode, &data, NULL, 0, do_thumb_brush_task_cb_ex,
2328                     ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT), false);
2329 }
2330
2331 static void do_rotate_brush_task_cb_ex(
2332         void *userdata, void *UNUSED(userdata_chunk), const int n, const int thread_id)
2333 {
2334         SculptThreadedTaskData *data = userdata;
2335         SculptSession *ss = data->ob->sculpt;
2336         Brush *brush = data->brush;
2337         const float angle = data->angle;
2338
2339         PBVHVertexIter vd;
2340         SculptBrushTest test;
2341         SculptOrigVertData orig_data;
2342         float (*proxy)[3];
2343         const float bstrength = ss->cache->bstrength;
2344
2345         sculpt_orig_vert_data_init(&orig_data, data->ob, data->nodes[n]);
2346
2347         proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co;
2348
2349         sculpt_brush_test_init(ss, &test);
2350
2351         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
2352         {
2353                 sculpt_orig_vert_data_update(&orig_data, &vd);
2354
2355                 if (sculpt_brush_test(&test, orig_data.co)) {
2356                         float vec[3], rot[3][3];
2357                         const float fade = bstrength * tex_strength(
2358                                                ss, brush, orig_data.co, test.dist, orig_data.no, NULL, vd.mask ? *vd.mask : 0.0f,
2359                                                thread_id);
2360
2361                         sub_v3_v3v3(vec, orig_data.co, ss->cache->location);
2362                         axis_angle_normalized_to_mat3(rot, ss->cache->sculpt_normal_symm, angle * fade);
2363                         mul_v3_m3v3(proxy[vd.i], rot, vec);
2364                         add_v3_v3(proxy[vd.i], ss->cache->location);
2365                         sub_v3_v3(proxy[vd.i], orig_data.co);
2366
2367                         if (vd.mvert)
2368                                 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
2369                 }
2370         }
2371         BKE_pbvh_vertex_iter_end;
2372 }
2373
2374 static void do_rotate_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
2375 {
2376         SculptSession *ss = ob->sculpt;
2377         Brush *brush = BKE_paint_brush(&sd->paint);
2378
2379         static const int flip[8] = { 1, -1, -1, 1, -1, 1, 1, -1 };
2380         const float angle = ss->cache->vertex_rotation * flip[ss->cache->mirror_symmetry_pass];
2381
2382         SculptThreadedTaskData data = {
2383             .sd = sd, .ob = ob, .brush = brush, .nodes = nodes,
2384             .angle = angle,
2385         };
2386
2387         BLI_task_parallel_range_ex(
2388                     0, totnode, &data, NULL, 0, do_rotate_brush_task_cb_ex,
2389                     ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT), false);
2390 }
2391
2392 static void do_layer_brush_task_cb_ex(
2393         void *userdata, void *UNUSED(userdata_chunk), const int n, const int thread_id)
2394 {
2395         SculptThreadedTaskData *data = userdata;
2396         SculptSession *ss = data->ob->sculpt;
2397         Sculpt *sd = data->sd;
2398         Brush *brush = data->brush;
2399         const float *offset = data->offset;
2400
2401         PBVHVertexIter vd;
2402         SculptBrushTest test;
2403         SculptOrigVertData orig_data;
2404         float *layer_disp;
2405         const float bstrength = ss->cache->bstrength;
2406         const float lim = (bstrength < 0) ? -data->brush->height : data->brush->height;
2407         /* XXX: layer brush needs conversion to proxy but its more complicated */
2408         /* proxy = BKE_pbvh_node_add_proxy(ss->pbvh, nodes[n])->co; */
2409
2410         sculpt_orig_vert_data_init(&orig_data, data->ob, data->nodes[n]);
2411
2412         /* Why does this have to be thread-protected? */
2413         BLI_mutex_lock(&data->mutex);
2414         layer_disp = BKE_pbvh_node_layer_disp_get(ss->pbvh, data->nodes[n]);
2415         BLI_mutex_unlock(&data->mutex);
2416
2417         sculpt_brush_test_init(ss, &test);
2418
2419         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
2420         {
2421                 sculpt_orig_vert_data_update(&orig_data, &vd);
2422
2423                 if (sculpt_brush_test(&test, orig_data.co)) {
2424                         const float fade = bstrength * tex_strength(
2425                                                ss, brush, vd.co, test.dist, vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, thread_id);
2426                         float *disp = &layer_disp[vd.i];
2427                         float val[3];
2428
2429                         *disp += fade;
2430
2431                         /* Don't let the displacement go past the limit */
2432                         if ((lim < 0.0f && *disp < lim) || (lim >= 0.0f && *disp > lim))
2433                                 *disp = lim;
2434
2435                         mul_v3_v3fl(val, offset, *disp);
2436
2437                         if (!ss->multires && !ss->bm && ss->layer_co && (brush->flag & BRUSH_PERSISTENT)) {
2438                                 int index = vd.vert_indices[vd.i];
2439
2440                                 /* persistent base */
2441                                 add_v3_v3(val, ss->layer_co[index]);
2442                         }
2443                         else {
2444                                 add_v3_v3(val, orig_data.co);
2445                         }
2446
2447                         sculpt_clip(sd, ss, vd.co, val);
2448
2449                         if (vd.mvert)
2450                                 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
2451                 }
2452         }
2453         BKE_pbvh_vertex_iter_end;
2454 }
2455
2456 static void do_layer_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
2457 {
2458         SculptSession *ss = ob->sculpt;
2459         Brush *brush = BKE_paint_brush(&sd->paint);
2460         float offset[3];
2461
2462         mul_v3_v3v3(offset, ss->cache->scale, ss->cache->sculpt_normal_symm);
2463
2464         SculptThreadedTaskData data = {
2465             .sd = sd, .ob = ob, .brush = brush, .nodes = nodes,
2466             .offset = offset,
2467         };
2468         BLI_mutex_init(&data.mutex);
2469
2470         BLI_task_parallel_range_ex(
2471                     0, totnode, &data, NULL, 0, do_layer_brush_task_cb_ex,
2472                     ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT), false);
2473
2474         BLI_mutex_end(&data.mutex);
2475 }
2476
2477 static void do_inflate_brush_task_cb_ex(
2478         void *userdata, void *UNUSED(userdata_chunk), const int n, const int thread_id)
2479 {
2480         SculptThreadedTaskData *data = userdata;
2481         SculptSession *ss = data->ob->sculpt;
2482         Brush *brush = data->brush;
2483
2484         PBVHVertexIter vd;
2485         SculptBrushTest test;
2486         float (*proxy)[3];
2487         const float bstrength = ss->cache->bstrength;
2488
2489         proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co;
2490
2491         sculpt_brush_test_init(ss, &test);
2492
2493         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
2494         {
2495                 if (sculpt_brush_test(&test, vd.co)) {
2496                         const float fade = bstrength * tex_strength(
2497                                                ss, brush, vd.co, test.dist, vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, thread_id);
2498                         float val[3];
2499
2500                         if (vd.fno)
2501                                 copy_v3_v3(val, vd.fno);
2502                         else
2503                                 normal_short_to_float_v3(val, vd.no);
2504
2505                         mul_v3_fl(val, fade * ss->cache->radius);
2506                         mul_v3_v3v3(proxy[vd.i], val, ss->cache->scale);
2507
2508                         if (vd.mvert)
2509                                 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
2510                 }
2511         }
2512         BKE_pbvh_vertex_iter_end;
2513 }
2514
2515 static void do_inflate_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
2516 {
2517         Brush *brush = BKE_paint_brush(&sd->paint);
2518
2519         SculptThreadedTaskData data = {
2520             .sd = sd, .ob = ob, .brush = brush, .nodes = nodes,
2521         };
2522
2523         BLI_task_parallel_range_ex(
2524                     0, totnode, &data, NULL, 0, do_inflate_brush_task_cb_ex,
2525                     ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT), false);
2526 }
2527
2528 static void calc_sculpt_plane(
2529         Sculpt *sd, Object *ob,
2530         PBVHNode **nodes, int totnode,
2531         float r_area_no[3], float r_area_co[3])
2532 {
2533         SculptSession *ss = ob->sculpt;
2534         Brush *brush = BKE_paint_brush(&sd->paint);
2535
2536         if (ss->cache->mirror_symmetry_pass == 0 &&
2537             ss->cache->radial_symmetry_pass == 0 &&
2538             ss->cache->tile_pass == 0 &&
2539             (ss->cache->first_time || !(brush->flag & BRUSH_ORIGINAL_NORMAL)))
2540         {
2541                 switch (brush->sculpt_plane) {
2542                         case SCULPT_DISP_DIR_VIEW:
2543                                 copy_v3_v3(r_area_no, ss->cache->true_view_normal);
2544                                 break;
2545
2546                         case SCULPT_DISP_DIR_X:
2547                                 ARRAY_SET_ITEMS(r_area_no, 1, 0, 0);
2548                                 break;
2549
2550                         case SCULPT_DISP_DIR_Y:
2551                                 ARRAY_SET_ITEMS(r_area_no, 0, 1, 0);
2552                                 break;
2553
2554                         case SCULPT_DISP_DIR_Z:
2555                                 ARRAY_SET_ITEMS(r_area_no, 0, 0, 1);
2556                                 break;
2557
2558                         case SCULPT_DISP_DIR_AREA:
2559                                 calc_area_normal_and_center(sd, ob, nodes, totnode, r_area_no, r_area_co);
2560                                 break;
2561
2562                         default:
2563                                 break;
2564                 }
2565
2566                 /* for flatten center */
2567                 /* flatten center has not been calculated yet if we are not using the area normal */
2568                 if (brush->sculpt_plane != SCULPT_DISP_DIR_AREA)
2569                         calc_area_center(sd, ob, nodes, totnode, r_area_co);
2570
2571                 /* for area normal */
2572                 copy_v3_v3(ss->cache->sculpt_normal, r_area_no);
2573
2574                 /* for flatten center */
2575                 copy_v3_v3(ss->cache->last_center, r_area_co);
2576         }
2577         else {
2578                 /* for area normal */
2579                 copy_v3_v3(r_area_no, ss->cache->sculpt_normal);
2580
2581                 /* for flatten center */
2582                 copy_v3_v3(r_area_co, ss->cache->last_center);
2583
2584                 /* for area normal */
2585                 flip_v3(r_area_no, ss->cache->mirror_symmetry_pass);
2586
2587                 /* for flatten center */
2588                 flip_v3(r_area_co, ss->cache->mirror_symmetry_pass);
2589
2590                 /* for area normal */
2591                 mul_m4_v3(ss->cache->symm_rot_mat, r_area_no);
2592
2593                 /* for flatten center */
2594                 mul_m4_v3(ss->cache->symm_rot_mat, r_area_co);
2595
2596                 /* shift the plane for the current tile */
2597                 add_v3_v3(r_area_co, ss->cache->plane_offset);
2598         }
2599 }
2600
2601 /* Projects a point onto a plane along the plane's normal */
2602 static void point_plane_project(
2603         float intr[3],
2604         const float co[3], const float plane_normal[3], const float plane_center[3])
2605 {
2606         sub_v3_v3v3(intr, co, plane_center);
2607         mul_v3_v3fl(intr, plane_normal, dot_v3v3(plane_normal, intr));
2608         sub_v3_v3v3(intr, co, intr);
2609 }
2610
2611 static int plane_trim(const StrokeCache *cache, const Brush *brush, const float val[3])
2612 {
2613         return (!(brush->flag & BRUSH_PLANE_TRIM) ||
2614                 ((dot_v3v3(val, val) <= cache->radius_squared * cache->plane_trim_squared)));
2615 }
2616
2617 static bool plane_point_side_flip(
2618         const float co[3], const float plane_normal[3], const float plane_center[3],
2619         const bool flip)
2620 {
2621         float delta[3];
2622         float d;
2623
2624         sub_v3_v3v3(delta, co, plane_center);
2625         d = dot_v3v3(plane_normal, delta);
2626
2627         if (flip) d = -d;
2628
2629         return d <= 0.0f;
2630 }
2631
2632 static int plane_point_side(const float co[3], const float plane_normal[3], const float plane_center[3])
2633 {
2634         float delta[3];
2635
2636         sub_v3_v3v3(delta, co, plane_center);
2637         return dot_v3v3(plane_normal, delta) <= 0.0f;
2638 }
2639
2640 static float get_offset(Sculpt *sd, SculptSession *ss)
2641 {
2642         Brush *brush = BKE_paint_brush(&sd->paint);
2643
2644         float rv = brush->plane_offset;
2645
2646         if (brush->flag & BRUSH_OFFSET_PRESSURE) {
2647                 rv *= ss->cache->pressure;
2648         }
2649
2650         return rv;
2651 }
2652
2653 static void do_flatten_brush_task_cb_ex(
2654         void *userdata, void *UNUSED(userdata_chunk), const int n, const int thread_id)
2655 {
2656         SculptThreadedTaskData *data = userdata;
2657         SculptSession *ss = data->ob->sculpt;
2658         Brush *brush = data->brush;
2659         const float *area_no = data->area_no;
2660         const float *area_co = data->area_co;
2661
2662         PBVHVertexIter vd;
2663         SculptBrushTest test;
2664         float (*proxy)[3];
2665         const float bstrength = ss->cache->bstrength;
2666
2667         proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co;
2668
2669         sculpt_brush_test_init(ss, &test);
2670
2671         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
2672         {
2673                 if (sculpt_brush_test_sq(&test, vd.co)) {
2674                         float intr[3];
2675                         float val[3];
2676
2677                         point_plane_project(intr, vd.co, area_no, area_co);
2678
2679                         sub_v3_v3v3(val, intr, vd.co);
2680
2681                         if (plane_trim(ss->cache, brush, val)) {
2682                                 const float fade = bstrength * tex_strength(
2683                                                        ss, brush, vd.co, sqrtf(test.dist), vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f,
2684                                                        thread_id);
2685
2686                                 mul_v3_v3fl(proxy[vd.i], val, fade);
2687
2688                                 if (vd.mvert)
2689                                         vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
2690                         }
2691                 }
2692         }
2693         BKE_pbvh_vertex_iter_end;
2694 }
2695
2696 static void do_flatten_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
2697 {
2698         SculptSession *ss = ob->sculpt;
2699         Brush *brush = BKE_paint_brush(&sd->paint);
2700
2701         const float radius = ss->cache->radius;
2702
2703         float area_no[3];
2704         float area_co[3];
2705
2706         float offset = get_offset(sd, ss);
2707         float displace;
2708         float temp[3];
2709
2710         calc_sculpt_plane(sd, ob, nodes, totnode, area_no, area_co);
2711
2712         displace = radius * offset;
2713
2714         mul_v3_v3v3(temp, area_no, ss->cache->scale);
2715         mul_v3_fl(temp, displace);
2716         add_v3_v3(area_co, temp);
2717
2718         SculptThreadedTaskData data = {
2719             .sd = sd, .ob = ob, .brush = brush, .nodes = nodes,
2720             .area_no = area_no, .area_co = area_co,
2721         };
2722
2723         BLI_task_parallel_range_ex(
2724                     0, totnode, &data, NULL, 0, do_flatten_brush_task_cb_ex,
2725                     ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT), false);
2726 }
2727
2728 static void do_clay_brush_task_cb_ex(
2729         void *userdata, void *UNUSED(userdata_chunk), const int n, const int thread_id)
2730 {
2731         SculptThreadedTaskData *data = userdata;
2732         SculptSession *ss = data->ob->sculpt;
2733         Brush *brush = data->brush;
2734         const float *area_no = data->area_no;
2735         const float *area_co = data->area_co;
2736
2737         PBVHVertexIter vd;
2738         SculptBrushTest test;
2739         float (*proxy)[3];
2740         const bool flip = (ss->cache->bstrength < 0);
2741         const float bstrength = flip ? -ss->cache->bstrength : ss->cache->bstrength;
2742
2743         proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co;
2744
2745         sculpt_brush_test_init(ss, &test);
2746
2747         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
2748         {
2749                 if (sculpt_brush_test_sq(&test, vd.co)) {
2750                         if (plane_point_side_flip(vd.co, area_no, area_co, flip)) {
2751                                 float intr[3];
2752                                 float val[3];
2753
2754                                 point_plane_project(intr, vd.co, area_no, area_co);
2755
2756                                 sub_v3_v3v3(val, intr, vd.co);
2757
2758                                 if (plane_trim(ss->cache, brush, val)) {
2759                                         /* note, the normal from the vertices is ignored,
2760                                          * causes glitch with planes, see: T44390 */
2761                                         const float fade = bstrength * tex_strength(
2762                                                                ss, brush, vd.co, sqrtf(test.dist), vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f,
2763                                                                thread_id);
2764
2765                                         mul_v3_v3fl(proxy[vd.i], val, fade);
2766
2767                                         if (vd.mvert)
2768                                                 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
2769                                 }
2770                         }
2771                 }
2772         }
2773         BKE_pbvh_vertex_iter_end;
2774 }
2775
2776 static void do_clay_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
2777 {
2778         SculptSession *ss = ob->sculpt;
2779         Brush *brush = BKE_paint_brush(&sd->paint);
2780
2781         const bool flip = (ss->cache->bstrength < 0);
2782         const float radius = flip ? -ss->cache->radius : ss->cache->radius;
2783
2784         float offset = get_offset(sd, ss);
2785         float displace;
2786
2787         float area_no[3];
2788         float area_co[3];
2789         float temp[3];
2790
2791         calc_sculpt_plane(sd, ob, nodes, totnode, area_no, area_co);
2792
2793         displace = radius * (0.25f + offset);
2794
2795         mul_v3_v3v3(temp, area_no, ss->cache->scale);
2796         mul_v3_fl(temp, displace);
2797         add_v3_v3(area_co, temp);
2798
2799         /* add_v3_v3v3(p, ss->cache->location, area_no); */
2800
2801         SculptThreadedTaskData data = {
2802             .sd = sd, .ob = ob, .brush = brush, .nodes = nodes,
2803             .area_no = area_no, .area_co = area_co,
2804         };
2805
2806         BLI_task_parallel_range_ex(
2807                     0, totnode, &data, NULL, 0, do_clay_brush_task_cb_ex,
2808                     ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT), false);
2809 }
2810
2811 static void do_clay_strips_brush_task_cb_ex(
2812         void *userdata, void *UNUSED(userdata_chunk), const int n, const int thread_id)
2813 {
2814         SculptThreadedTaskData *data = userdata;
2815         SculptSession *ss = data->ob->sculpt;
2816         Brush *brush = data->brush;
2817         float (*mat)[4] = data->mat;
2818         const float *area_no_sp = data->area_no_sp;
2819         const float *area_co = data->area_co;
2820
2821         PBVHVertexIter vd;
2822         SculptBrushTest test;
2823         float (*proxy)[3];
2824         const bool flip = (ss->cache->bstrength < 0);
2825         const float bstrength = flip ? -ss->cache->bstrength : ss->cache->bstrength;
2826
2827         proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co;
2828
2829         sculpt_brush_test_init(ss, &test);
2830
2831         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
2832         {
2833                 if (sculpt_brush_test_cube(&test, vd.co, mat)) {
2834                         if (plane_point_side_flip(vd.co, area_no_sp, area_co, flip)) {
2835                                 float intr[3];
2836                                 float val[3];
2837
2838                                 point_plane_project(intr, vd.co, area_no_sp, area_co);
2839
2840                                 sub_v3_v3v3(val, intr, vd.co);
2841
2842                                 if (plane_trim(ss->cache, brush, val)) {
2843                                         /* note, the normal from the vertices is ignored,
2844                                          * causes glitch with planes, see: T44390 */
2845                                         const float fade = bstrength * tex_strength(
2846                                                                ss, brush, vd.co, ss->cache->radius * test.dist,
2847                                                                vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, thread_id);
2848
2849                                         mul_v3_v3fl(proxy[vd.i], val, fade);
2850
2851                                         if (vd.mvert)
2852                                                 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
2853                                 }
2854                         }
2855                 }
2856         }
2857         BKE_pbvh_vertex_iter_end;
2858 }
2859
2860 static void do_clay_strips_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
2861 {
2862         SculptSession *ss = ob->sculpt;
2863         Brush *brush = BKE_paint_brush(&sd->paint);
2864
2865         const bool flip = (ss->cache->bstrength < 0);
2866         const float radius    = flip ? -ss->cache->radius : ss->cache->radius;
2867         const float offset    = get_offset(sd, ss);
2868         const float displace  = radius * (0.25f + offset);
2869
2870         float area_no_sp[3];  /* the sculpt-plane normal (whatever its set to) */
2871         float area_no[3];     /* geometry normal */
2872         float area_co[3];
2873
2874         float temp[3];
2875         float mat[4][4];
2876         float scale[4][4];
2877         float tmat[4][4];
2878
2879         calc_sculpt_plane(sd, ob, nodes, totnode, area_no_sp, area_co);
2880
2881         if (brush->sculpt_plane != SCULPT_DISP_DIR_AREA || (brush->flag & BRUSH_ORIGINAL_NORMAL))
2882                 calc_area_normal(sd, ob, nodes, totnode, area_no);
2883         else
2884                 copy_v3_v3(area_no, area_no_sp);
2885
2886         /* delay the first daub because grab delta is not setup */
2887         if (ss->cache->first_time)
2888                 return;
2889
2890         mul_v3_v3v3(temp, area_no_sp, ss->cache->scale);
2891         mul_v3_fl(temp, displace);
2892         add_v3_v3(area_co, temp);
2893
2894         /* init mat */
2895         cross_v3_v3v3(mat[0], area_no, ss->cache->grab_delta_symmetry);
2896         mat[0][3] = 0;
2897         cross_v3_v3v3(mat[1], area_no, mat[0]);
2898         mat[1][3] = 0;
2899         copy_v3_v3(mat[2], area_no);
2900         mat[2][3] = 0;
2901         copy_v3_v3(mat[3], ss->cache->location);
2902         mat[3][3] = 1;
2903         normalize_m4(mat);
2904
2905         /* scale mat */
2906         scale_m4_fl(scale, ss->cache->radius);
2907         mul_m4_m4m4(tmat, mat, scale);
2908         invert_m4_m4(mat, tmat);
2909
2910         SculptThreadedTaskData data = {
2911             .sd = sd, .ob = ob, .brush = brush, .nodes = nodes,
2912             .area_no_sp = area_no_sp, .area_co = area_co, .mat = mat,
2913         };
2914
2915         BLI_task_parallel_range_ex(
2916                     0, totnode, &data, NULL, 0, do_clay_strips_brush_task_cb_ex,
2917                     ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT), false);
2918 }
2919
2920 static void do_fill_brush_task_cb_ex(
2921         void *userdata, void *UNUSED(userdata_chunk), const int n, const int thread_id)
2922 {
2923         SculptThreadedTaskData *data = userdata;
2924         SculptSession *ss = data->ob->sculpt;
2925         Brush *brush = data->brush;
2926         const float *area_no = data->area_no;
2927         const float *area_co = data->area_co;
2928
2929         PBVHVertexIter vd;
2930         SculptBrushTest test;
2931         float (*proxy)[3];
2932         const float bstrength = ss->cache->bstrength;
2933
2934         proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co;
2935
2936         sculpt_brush_test_init(ss, &test);
2937
2938         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
2939         {
2940                 if (sculpt_brush_test_sq(&test, vd.co)) {
2941                         if (plane_point_side(vd.co, area_no, area_co)) {
2942                                 float intr[3];
2943                                 float val[3];
2944
2945                                 point_plane_project(intr, vd.co, area_no, area_co);
2946
2947                                 sub_v3_v3v3(val, intr, vd.co);
2948
2949                                 if (plane_trim(ss->cache, brush, val)) {
2950                                         const float fade = bstrength * tex_strength(
2951                                                                ss, brush, vd.co, sqrtf(test.dist),
2952                                                                vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, thread_id);
2953
2954                                         mul_v3_v3fl(proxy[vd.i], val, fade);
2955
2956                                         if (vd.mvert)
2957                                                 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
2958                                 }
2959                         }
2960                 }
2961         }
2962         BKE_pbvh_vertex_iter_end;
2963 }
2964
2965 static void do_fill_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
2966 {
2967         SculptSession *ss = ob->sculpt;
2968         Brush *brush = BKE_paint_brush(&sd->paint);
2969
2970         const float radius = ss->cache->radius;
2971
2972         float area_no[3];
2973         float area_co[3];
2974         float offset = get_offset(sd, ss);
2975
2976         float displace;
2977
2978         float temp[3];
2979
2980         calc_sculpt_plane(sd, ob, nodes, totnode, area_no, area_co);
2981
2982         displace = radius * offset;
2983
2984         mul_v3_v3v3(temp, area_no, ss->cache->scale);
2985         mul_v3_fl(temp, displace);
2986         add_v3_v3(area_co, temp);
2987
2988         SculptThreadedTaskData data = {
2989             .sd = sd, .ob = ob, .brush = brush, .nodes = nodes,
2990             .area_no = area_no, .area_co = area_co,
2991         };
2992
2993         BLI_task_parallel_range_ex(
2994                     0, totnode, &data, NULL, 0, do_fill_brush_task_cb_ex,
2995                     ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT), false);
2996 }
2997
2998 static void do_scrape_brush_task_cb_ex(
2999         void *userdata, void *UNUSED(userdata_chunk), const int n, const int thread_id)
3000 {
3001         SculptThreadedTaskData *data = userdata;
3002         SculptSession *ss = data->ob->sculpt;
3003         Brush *brush = data->brush;
3004         const float *area_no = data->area_no;
3005         const float *area_co = data->area_co;
3006
3007         PBVHVertexIter vd;
3008         SculptBrushTest test;
3009         float (*proxy)[3];
3010         const float bstrength = ss->cache->bstrength;
3011
3012         proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co;
3013
3014         sculpt_brush_test_init(ss, &test);
3015
3016         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
3017         {
3018                 if (sculpt_brush_test_sq(&test, vd.co)) {
3019                         if (!plane_point_side(vd.co, area_no, area_co)) {
3020                                 float intr[3];
3021                                 float val[3];
3022
3023                                 point_plane_project(intr, vd.co, area_no, area_co);
3024
3025                                 sub_v3_v3v3(val, intr, vd.co);
3026
3027                                 if (plane_trim(ss->cache, brush, val)) {
3028                                         const float fade = bstrength * tex_strength(
3029                                                                ss, brush, vd.co, sqrtf(test.dist), vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f,
3030                                                                thread_id);
3031
3032                                         mul_v3_v3fl(proxy[vd.i], val, fade);
3033
3034                                         if (vd.mvert)
3035                                                 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
3036                                 }
3037                         }
3038                 }
3039         }
3040         BKE_pbvh_vertex_iter_end;
3041 }
3042
3043 static void do_scrape_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
3044 {
3045         SculptSession *ss = ob->sculpt;
3046         Brush *brush = BKE_paint_brush(&sd->paint);
3047
3048         const float radius = ss->cache->radius;
3049
3050         float area_no[3];
3051         float area_co[3];
3052         float offset = get_offset(sd, ss);
3053
3054         float displace;
3055
3056         float temp[3];
3057
3058         calc_sculpt_plane(sd, ob, nodes, totnode, area_no, area_co);
3059
3060         displace = -radius * offset;
3061
3062         mul_v3_v3v3(temp, area_no, ss->cache->scale);
3063         mul_v3_fl(temp, displace);
3064         add_v3_v3(area_co, temp);
3065
3066         SculptThreadedTaskData data = {
3067             .sd = sd, .ob = ob, .brush = brush, .nodes = nodes,
3068             .area_no = area_no, .area_co = area_co,
3069         };
3070
3071         BLI_task_parallel_range_ex(
3072                     0, totnode, &data, NULL, 0, do_scrape_brush_task_cb_ex,
3073                     ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT), false);
3074 }
3075
3076 static void do_gravity_task_cb_ex(
3077         void *userdata, void *UNUSED(userdata_chunk), const int n, const int thread_id)
3078 {
3079         SculptThreadedTaskData *data = userdata;
3080         SculptSession *ss = data->ob->sculpt;
3081         Brush *brush = data->brush;
3082         float *offset = data->offset;
3083
3084         PBVHVertexIter vd;
3085         SculptBrushTest test;
3086         float (*proxy)[3];
3087
3088         proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co;
3089
3090         sculpt_brush_test_init(ss, &test);
3091
3092         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE) {
3093                 if (sculpt_brush_test_sq(&test, vd.co)) {
3094                         const float fade = tex_strength(
3095                                                ss, brush, vd.co, sqrtf(test.dist), vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f,
3096                                                thread_id);
3097
3098                         mul_v3_v3fl(proxy[vd.i], offset, fade);
3099
3100                         if (vd.mvert)
3101                                 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
3102                 }
3103         }
3104         BKE_pbvh_vertex_iter_end;
3105 }
3106
3107 static void do_gravity(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode, float bstrength)
3108 {
3109         SculptSession *ss = ob->sculpt;
3110         Brush *brush = BKE_paint_brush(&sd->paint);
3111
3112         float offset[3]/*, area_no[3]*/;
3113         float gravity_vector[3];
3114
3115         mul_v3_v3fl(gravity_vector, ss->cache->gravity_direction, -ss->cache->radius_squared);
3116
3117         /* offset with as much as possible factored in already */
3118         mul_v3_v3v3(offset, gravity_vector, ss->cache->scale);
3119         mul_v3_fl(offset, bstrength);
3120
3121         /* threaded loop over nodes */
3122         SculptThreadedTaskData data = {
3123             .sd = sd, .ob = ob, .brush = brush, .nodes = nodes,
3124             .offset = offset,
3125         };
3126
3127         BLI_task_parallel_range_ex(
3128                     0, totnode, &data, NULL, 0, do_gravity_task_cb_ex,
3129                     ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT), false);
3130 }
3131
3132
3133 void sculpt_vertcos_to_key(Object *ob, KeyBlock *kb, float (*vertCos)[3])
3134 {
3135         Mesh *me = (Mesh *)ob->data;
3136         float (*ofs)[3] = NULL;
3137         int a;
3138         const int kb_act_idx = ob->shapenr - 1;
3139         KeyBlock *currkey;
3140
3141         /* for relative keys editing of base should update other keys */
3142         if (BKE_keyblock_is_basis(me->key, kb_act_idx)) {
3143                 ofs = BKE_keyblock_convert_to_vertcos(ob, kb);
3144
3145                 /* calculate key coord offsets (from previous location) */
3146                 for (a = 0; a < me->totvert; a++) {
3147                         sub_v3_v3v3(ofs[a], vertCos[a], ofs[a]);
3148                 }
3149
3150                 /* apply offsets on other keys */
3151                 for (currkey = me->key->block.first; currkey; currkey = currkey->next) {
3152                         if ((currkey != kb) && (currkey->relative == kb_act_idx)) {
3153                                 BKE_keyblock_update_from_offset(ob, currkey, ofs);
3154                         }
3155                 }
3156
3157                 MEM_freeN(ofs);
3158         }
3159
3160         /* modifying of basis key should update mesh */
3161         if (kb == me->key->refkey) {
3162                 MVert *mvert = me->mvert;
3163
3164                 for (a = 0; a < me->totvert; a++, mvert++)
3165                         copy_v3_v3(mvert->co, vertCos[a]);
3166
3167                 BKE_mesh_calc_normals(me);
3168         }
3169
3170         /* apply new coords on active key block, no need to re-allocate kb->data here! */
3171         BKE_keyblock_update_from_vertcos(ob, kb, vertCos);
3172 }
3173
3174 /* Note: we do the topology update before any brush actions to avoid
3175  * issues with the proxies. The size of the proxy can't change, so
3176  * topology must be updated first. */
3177 static void sculpt_topology_update(Sculpt *sd, Object *ob, Brush *brush, UnifiedPaintSettings *UNUSED(ups))
3178 {
3179         SculptSession *ss = ob->sculpt;
3180         SculptSearchSphereData data;
3181         PBVHNode **nodes = NULL;
3182         float radius;
3183         int n, totnode;
3184
3185         /* Build a list of all nodes that are potentially within the
3186          * brush's area of influence */
3187         data.ss = ss;
3188         data.sd = sd;
3189
3190         radius = ss->cache->radius * 1.25f;
3191
3192         data.radius_squared = radius * radius;
3193         data.original = sculpt_tool_needs_original(brush->sculpt_tool) ? true : ss->cache->original;
3194
3195         BKE_pbvh_search_gather(ss->pbvh, sculpt_search_sphere_cb, &data, &nodes, &totnode);
3196
3197         /* Only act if some verts are inside the brush area */
3198         if (totnode) {
3199                 PBVHTopologyUpdateMode mode = 0;
3200                 float location[3];
3201
3202                 if (sd->flags & SCULPT_DYNTOPO_SUBDIVIDE)
3203                         mode |= PBVH_Subdivide;
3204
3205                 if ((sd->flags & SCULPT_DYNTOPO_COLLAPSE) ||
3206                     (brush->sculpt_tool == SCULPT_TOOL_SIMPLIFY))
3207                 {
3208                         mode |= PBVH_Collapse;
3209                 }
3210
3211                 for (n = 0; n < totnode; n++) {
3212                         sculpt_undo_push_node(ob, nodes[n],
3213                                               brush->sculpt_tool == SCULPT_TOOL_MASK ?
3214                                               SCULPT_UNDO_MASK : SCULPT_UNDO_COORDS);
3215                         BKE_pbvh_node_mark_update(nodes[n]);
3216
3217                         if (BKE_pbvh_type(ss->pbvh) == PBVH_BMESH) {
3218                                 BKE_pbvh_node_mark_topology_update(nodes[n]);
3219                                 BKE_pbvh_bmesh_node_save_orig(nodes[n]);
3220                         }
3221                 }
3222
3223                 if (BKE_pbvh_type(ss->pbvh) == PBVH_BMESH) {
3224                         BKE_pbvh_bmesh_update_topology(
3225                                 ss->pbvh, mode,
3226                                 ss->cache->location,
3227                                 (brush->flag & BRUSH_FRONTFACE) ? ss->cache->view_normal : NULL,
3228                                 ss->cache->radius);
3229                 }
3230
3231                 MEM_freeN(nodes);
3232
3233                 /* update average stroke position */
3234                 copy_v3_v3(location, ss->cache->true_location);
3235                 mul_m4_v3(ob->obmat, location);
3236         }
3237 }
3238
3239 static void do_brush_action_task_cb(void *userdata, const int n)
3240 {
3241         SculptThreadedTaskData *data = userdata;
3242
3243         sculpt_undo_push_node(data->ob, data->nodes[n],
3244                               data->brush->sculpt_tool == SCULPT_TOOL_MASK ? SCULPT_UNDO_MASK : SCULPT_UNDO_COORDS);
3245         BKE_pbvh_node_mark_update(data->nodes[n]);
3246 }
3247