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