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