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