code cleanup: use const events for modal and invoke operators.
[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 "BLF_translation.h"
47
48 #include "DNA_mesh_types.h"
49 #include "DNA_meshdata_types.h"
50 #include "DNA_node_types.h"
51 #include "DNA_object_types.h"
52 #include "DNA_scene_types.h"
53 #include "DNA_brush_types.h"
54
55 #include "BKE_pbvh.h"
56 #include "BKE_brush.h"
57 #include "BKE_ccg.h"
58 #include "BKE_cdderivedmesh.h"
59 #include "BKE_context.h"
60 #include "BKE_depsgraph.h"
61 #include "BKE_image.h"
62 #include "BKE_key.h"
63 #include "BKE_library.h"
64 #include "BKE_mesh.h"
65 #include "BKE_modifier.h"
66 #include "BKE_multires.h"
67 #include "BKE_paint.h"
68 #include "BKE_report.h"
69 #include "BKE_lattice.h" /* for armature_deform_verts */
70 #include "BKE_node.h"
71 #include "BKE_object.h"
72 #include "BKE_subsurf.h"
73
74 #include "BIF_glutil.h"
75
76 #include "WM_api.h"
77 #include "WM_types.h"
78
79 #include "ED_sculpt.h"
80 #include "ED_screen.h"
81 #include "ED_view3d.h"
82 #include "ED_util.h" /* for crazyspace correction */
83 #include "paint_intern.h"
84 #include "sculpt_intern.h"
85
86 #include "RNA_access.h"
87 #include "RNA_define.h"
88
89 #include "RE_render_ext.h"
90
91 #include "GPU_buffers.h"
92
93 #include "bmesh.h"
94
95 #include <math.h>
96 #include <stdlib.h>
97 #include <string.h>
98
99 #ifdef _OPENMP
100 #include <omp.h>
101 #endif
102
103 void ED_sculpt_force_update(bContext *C)
104 {
105         Object *ob = CTX_data_active_object(C);
106
107         if (ob && (ob->mode & OB_MODE_SCULPT)) {
108                 multires_force_update(ob);
109
110                 /* Set reorder=false so that saving the file doesn't reorder
111                  * the BMesh's elements */
112                 sculptsession_bm_to_me(ob, FALSE);
113         }
114 }
115
116 float *ED_sculpt_get_last_stroke(struct Object *ob)
117 {
118         return (ob && ob->sculpt && ob->sculpt->last_stroke_valid) ? ob->sculpt->last_stroke : NULL;
119 }
120
121 int ED_sculpt_minmax(bContext *C, float min[3], float max[3])
122 {
123         Object *ob = CTX_data_active_object(C);
124
125         if (ob && ob->sculpt && ob->sculpt->last_stroke_valid) {
126                 copy_v3_v3(min, ob->sculpt->last_stroke);
127                 copy_v3_v3(max, ob->sculpt->last_stroke);
128
129                 return 1;
130         }
131         else {
132                 return 0;
133         }
134 }
135
136 /* Sculpt mode handles multires differently from regular meshes, but only if
137  * it's the last modifier on the stack and it is not on the first level */
138 MultiresModifierData *sculpt_multires_active(Scene *scene, Object *ob)
139 {
140         Mesh *me = (Mesh *)ob->data;
141         ModifierData *md;
142
143         if (ob->sculpt && ob->sculpt->bm) {
144                 /* can't combine multires and dynamic topology */
145                 return NULL;
146         }
147
148         if (!CustomData_get_layer(&me->ldata, CD_MDISPS)) {
149                 /* multires can't work without displacement layer */
150                 return NULL;
151         }
152
153         for (md = modifiers_getVirtualModifierList(ob); md; md = md->next) {
154                 if (md->type == eModifierType_Multires) {
155                         MultiresModifierData *mmd = (MultiresModifierData *)md;
156
157                         if (!modifier_isEnabled(scene, md, eModifierMode_Realtime))
158                                 continue;
159
160                         if (mmd->sculptlvl > 0) return mmd;
161                         else return NULL;
162                 }
163         }
164
165         return NULL;
166 }
167
168 /* Check if there are any active modifiers in stack (used for flushing updates at enter/exit sculpt mode) */
169 static int sculpt_has_active_modifiers(Scene *scene, Object *ob)
170 {
171         ModifierData *md;
172
173         md = modifiers_getVirtualModifierList(ob);
174
175         /* exception for shape keys because we can edit those */
176         for (; md; md = md->next) {
177                 if (modifier_isEnabled(scene, md, eModifierMode_Realtime))
178                         return 1;
179         }
180
181         return 0;
182 }
183
184 /* Checks if there are any supported deformation modifiers active */
185 static int sculpt_modifiers_active(Scene *scene, Sculpt *sd, Object *ob)
186 {
187         ModifierData *md;
188         Mesh *me = (Mesh *)ob->data;
189         MultiresModifierData *mmd = sculpt_multires_active(scene, ob);
190
191         if (mmd || ob->sculpt->bm)
192                 return 0;
193
194         /* non-locked shape keys could be handled in the same way as deformed mesh */
195         if ((ob->shapeflag & OB_SHAPE_LOCK) == 0 && me->key && ob->shapenr)
196                 return 1;
197
198         md = modifiers_getVirtualModifierList(ob);
199
200         /* exception for shape keys because we can edit those */
201         for (; md; md = md->next) {
202                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
203                 if (!modifier_isEnabled(scene, md, eModifierMode_Realtime)) continue;
204                 if (md->type == eModifierType_ShapeKey) continue;
205
206                 if (mti->type == eModifierTypeType_OnlyDeform) return 1;
207                 else if ((sd->flags & SCULPT_ONLY_DEFORM) == 0) return 1;
208         }
209
210         return 0;
211 }
212
213 typedef enum StrokeFlags {
214         CLIP_X = 1,
215         CLIP_Y = 2,
216         CLIP_Z = 4
217 } StrokeFlags;
218
219 /* Cache stroke properties. Used because
220  * RNA property lookup isn't particularly fast.
221  *
222  * For descriptions of these settings, check the operator properties.
223  */
224 typedef struct StrokeCache {
225         /* Invariants */
226         float initial_radius;
227         float scale[3];
228         int flag;
229         float clip_tolerance[3];
230         float initial_mouse[2];
231
232         /* Pre-allocated temporary storage used during smoothing */
233         int num_threads;
234         float (**tmpgrid_co)[3], (**tmprow_co)[3];
235         float **tmpgrid_mask, **tmprow_mask;
236
237         /* Variants */
238         float radius;
239         float radius_squared;
240         float true_location[3];
241         float location[3];
242
243         float pen_flip;
244         float invert;
245         float pressure;
246         float mouse[2];
247         float bstrength;
248
249         /* The rest is temporary storage that isn't saved as a property */
250
251         int first_time; /* Beginning of stroke may do some things special */
252
253         /* from ED_view3d_ob_project_mat_get() */
254         float projection_mat[4][4];
255
256         /* Clean this up! */
257         ViewContext *vc;
258         Brush *brush;
259
260         float (*face_norms)[3]; /* Copy of the mesh faces' normals */
261
262         float special_rotation;
263         float grab_delta[3], grab_delta_symmetry[3];
264         float old_grab_location[3], orig_grab_location[3];
265
266         int symmetry; /* Symmetry index between 0 and 7 bit combo 0 is Brush only;
267                        * 1 is X mirror; 2 is Y mirror; 3 is XY; 4 is Z; 5 is XZ; 6 is YZ; 7 is XYZ */
268         int mirror_symmetry_pass; /* the symmetry pass we are currently on between 0 and 7*/
269         float true_view_normal[3];
270         float view_normal[3];
271
272         /* sculpt_normal gets calculated by calc_sculpt_normal(), then the
273          * sculpt_normal_symm gets updated quickly with the usual symmetry
274          * transforms */
275         float sculpt_normal[3];
276         float sculpt_normal_symm[3];
277
278         /* Used for wrap texture mode, local_mat gets calculated by
279          * calc_brush_local_mat() and used in tex_strength(). */
280         float brush_local_mat[4][4];
281         
282         float last_center[3];
283         int radial_symmetry_pass;
284         float symm_rot_mat[4][4];
285         float symm_rot_mat_inv[4][4];
286         int original;
287         float anchored_location[3];
288
289         float vertex_rotation;
290
291         char saved_active_brush_name[MAX_ID_NAME];
292         char saved_mask_brush_tool;
293         int alt_smooth;
294
295         float plane_trim_squared;
296
297         rcti previous_r; /* previous redraw rectangle */
298 } StrokeCache;
299
300 /************** Access to original unmodified vertex data *************/
301
302 typedef struct {
303         BMLog *bm_log;
304
305         SculptUndoNode *unode;
306         float (*coords)[3];
307         short (*normals)[3];
308         float *vmasks;
309
310         /* Original coordinate, normal, and mask */
311         const float *co;
312         float mask;
313         short no[3];
314 } SculptOrigVertData;
315
316
317 /* Initialize a SculptOrigVertData for accessing original vertex data;
318  * handles BMesh, mesh, and multires */
319 static void sculpt_orig_vert_data_unode_init(SculptOrigVertData *data,
320                                              Object *ob,
321                                              SculptUndoNode *unode)
322 {
323         SculptSession *ss = ob->sculpt;
324         BMesh *bm = ss->bm;
325
326         memset(data, 0, sizeof(*data));
327         data->unode = unode;
328
329         if (bm) {
330                 data->bm_log = ss->bm_log;
331         }
332         else {
333                 data->coords = data->unode->co;
334                 data->normals = data->unode->no;
335                 data->vmasks = data->unode->mask;
336         }
337 }
338
339 /* Initialize a SculptOrigVertData for accessing original vertex data;
340  * handles BMesh, mesh, and multires */
341 static void sculpt_orig_vert_data_init(SculptOrigVertData *data,
342                                        Object *ob,
343                                        PBVHNode *node)
344 {
345         SculptUndoNode *unode;
346         unode = sculpt_undo_push_node(ob, node, SCULPT_UNDO_COORDS);
347         sculpt_orig_vert_data_unode_init(data, ob, unode);
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         const Scene *scene = ss->cache->vc->scene;
926         MTex *mtex = &br->mtex;
927         float avg = 1;
928         float rgba[4];
929
930         if (!mtex->tex) {
931                 avg = 1;
932         }
933         else if (mtex->brush_map_mode == MTEX_MAP_MODE_3D) {
934                 /* Get strength by feeding the vertex 
935                  * location directly into a texture */
936                 avg = BKE_brush_sample_tex_3D(scene, br, point, rgba, ss->tex_pool);
937         }
938         else if (ss->texcache) {
939                 float symm_point[3], point_2d[2];
940                 float x = 0.0f, y = 0.0f; /* Quite warnings */
941
942                 /* if the active area is being applied for symmetry, flip it
943                  * across the symmetry axis and rotate it back to the original
944                  * position in order to project it. This insures that the 
945                  * brush texture will be oriented correctly. */
946
947                 flip_v3_v3(symm_point, point, ss->cache->mirror_symmetry_pass);
948
949                 if (ss->cache->radial_symmetry_pass)
950                         mul_m4_v3(ss->cache->symm_rot_mat_inv, symm_point);
951
952                 ED_view3d_project_float_v2_m4(ss->cache->vc->ar, symm_point, point_2d, ss->cache->projection_mat);
953
954                 /* still no symmetry supported for other paint modes.
955                  * Sculpt does it DIY */
956                 if (mtex->brush_map_mode == MTEX_MAP_MODE_AREA) {
957                         /* Similar to fixed mode, but projects from brush angle
958                          * rather than view direction */
959
960                         mul_m4_v3(ss->cache->brush_local_mat, symm_point);
961
962                         x = symm_point[0];
963                         y = symm_point[1];
964
965                         x *= br->mtex.size[0];
966                         y *= br->mtex.size[1];
967
968                         x += br->mtex.ofs[0];
969                         y += br->mtex.ofs[1];
970
971                         avg = paint_get_tex_pixel(br, x, y, ss->tex_pool);
972
973                         avg += br->texture_sample_bias;
974                 }
975                 else {
976                         avg = BKE_brush_sample_tex_3D(scene, br, point_2d, rgba, ss->tex_pool);
977                 }
978         }
979
980         /* Falloff curve */
981         avg *= BKE_brush_curve_strength(br, len, ss->cache->radius);
982
983         avg *= frontface(br, sculpt_normal, vno, fno);
984
985         /* Paint mask */
986         avg *= 1.0f - mask;
987
988         return avg;
989 }
990
991 typedef struct {
992         Sculpt *sd;
993         SculptSession *ss;
994         float radius_squared;
995         int original;
996 } SculptSearchSphereData;
997
998 /* Test AABB against sphere */
999 static int sculpt_search_sphere_cb(PBVHNode *node, void *data_v)
1000 {
1001         SculptSearchSphereData *data = data_v;
1002         float *center = data->ss->cache->location, nearest[3];
1003         float t[3], bb_min[3], bb_max[3];
1004         int i;
1005
1006         if (data->original)
1007                 BKE_pbvh_node_get_original_BB(node, bb_min, bb_max);
1008         else
1009                 BKE_pbvh_node_get_BB(node, bb_min, bb_max);
1010         
1011         for (i = 0; i < 3; ++i) {
1012                 if (bb_min[i] > center[i])
1013                         nearest[i] = bb_min[i];
1014                 else if (bb_max[i] < center[i])
1015                         nearest[i] = bb_max[i];
1016                 else
1017                         nearest[i] = center[i]; 
1018         }
1019         
1020         sub_v3_v3v3(t, center, nearest);
1021
1022         return dot_v3v3(t, t) < data->radius_squared;
1023 }
1024
1025 /* Handles clipping against a mirror modifier and SCULPT_LOCK axis flags */
1026 static void sculpt_clip(Sculpt *sd, SculptSession *ss, float co[3], const float val[3])
1027 {
1028         int i;
1029
1030         for (i = 0; i < 3; ++i) {
1031                 if (sd->flags & (SCULPT_LOCK_X << i))
1032                         continue;
1033
1034                 if ((ss->cache->flag & (CLIP_X << i)) && (fabsf(co[i]) <= ss->cache->clip_tolerance[i]))
1035                         co[i] = 0.0f;
1036                 else
1037                         co[i] = val[i];
1038         }
1039 }
1040
1041 static void add_norm_if(float view_vec[3], float out[3], float out_flip[3], float fno[3])
1042 {
1043         if ((dot_v3v3(view_vec, fno)) > 0) {
1044                 add_v3_v3(out, fno);
1045         }
1046         else {
1047                 add_v3_v3(out_flip, fno); /* out_flip is used when out is {0,0,0} */
1048         }
1049 }
1050
1051 static void calc_area_normal(Sculpt *sd, Object *ob, float an[3], PBVHNode **nodes, int totnode)
1052 {
1053         SculptSession *ss = ob->sculpt;
1054         float out_flip[3] = {0.0f, 0.0f, 0.0f};
1055         int n, original;
1056
1057         /* Grab brush requires to test on original data (see r33888 and
1058          * bug #25371) */
1059         original = (paint_brush(&sd->paint)->sculpt_tool == SCULPT_TOOL_GRAB ?
1060                     TRUE : ss->cache->original);
1061
1062         /* In general the original coords are not available with dynamic
1063          * topology */
1064         if (ss->bm)
1065                 original = FALSE;
1066
1067         (void)sd; /* unused w/o openmp */
1068         
1069         zero_v3(an);
1070
1071         #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
1072         for (n = 0; n < totnode; n++) {
1073                 PBVHVertexIter vd;
1074                 SculptBrushTest test;
1075                 SculptUndoNode *unode;
1076                 float private_an[3] = {0.0f, 0.0f, 0.0f};
1077                 float private_out_flip[3] = {0.0f, 0.0f, 0.0f};
1078
1079                 unode = sculpt_undo_push_node(ob, nodes[n], SCULPT_UNDO_COORDS);
1080                 sculpt_brush_test_init(ss, &test);
1081
1082                 if (original) {
1083                         BKE_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE)
1084                         {
1085                                 if (sculpt_brush_test_fast(&test, unode->co[vd.i])) {
1086                                         float fno[3];
1087
1088                                         normal_short_to_float_v3(fno, unode->no[vd.i]);
1089                                         add_norm_if(ss->cache->view_normal, private_an, private_out_flip, fno);
1090                                 }
1091                         }
1092                         BKE_pbvh_vertex_iter_end;
1093                 }
1094                 else {
1095                         BKE_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE)
1096                         {
1097                                 if (sculpt_brush_test_fast(&test, vd.co)) {
1098                                         if (vd.no) {
1099                                                 float fno[3];
1100
1101                                                 normal_short_to_float_v3(fno, vd.no);
1102                                                 add_norm_if(ss->cache->view_normal, private_an, private_out_flip, fno);
1103                                         }
1104                                         else {
1105                                                 add_norm_if(ss->cache->view_normal, private_an, private_out_flip, vd.fno);
1106                                         }
1107                                 }
1108                         }
1109                         BKE_pbvh_vertex_iter_end;
1110                 }
1111
1112                 #pragma omp critical
1113                 {
1114                         add_v3_v3(an, private_an);
1115                         add_v3_v3(out_flip, private_out_flip);
1116                 }
1117         }
1118
1119         if (is_zero_v3(an))
1120                 copy_v3_v3(an, out_flip);
1121
1122         normalize_v3(an);
1123 }
1124
1125 /* Calculate primary direction of movement for many brushes */
1126 static void calc_sculpt_normal(Sculpt *sd, Object *ob,
1127                                PBVHNode **nodes, int totnode,
1128                                float an[3])
1129 {
1130         const Brush *brush = paint_brush(&sd->paint);
1131         const SculptSession *ss = ob->sculpt;
1132
1133         switch (brush->sculpt_plane) {
1134                 case SCULPT_DISP_DIR_VIEW:
1135                         ED_view3d_global_to_vector(ss->cache->vc->rv3d,
1136                                                    ss->cache->vc->rv3d->twmat[3],
1137                                                    an);
1138                         break;
1139
1140                 case SCULPT_DISP_DIR_X:
1141                         an[1] = 0.0;
1142                         an[2] = 0.0;
1143                         an[0] = 1.0;
1144                         break;
1145
1146                 case SCULPT_DISP_DIR_Y:
1147                         an[0] = 0.0;
1148                         an[2] = 0.0;
1149                         an[1] = 1.0;
1150                         break;
1151
1152                 case SCULPT_DISP_DIR_Z:
1153                         an[0] = 0.0;
1154                         an[1] = 0.0;
1155                         an[2] = 1.0;
1156                         break;
1157
1158                 case SCULPT_DISP_DIR_AREA:
1159                         calc_area_normal(sd, ob, an, nodes, totnode);
1160
1161                 default:
1162                         break;
1163         }
1164 }
1165
1166 static void update_sculpt_normal(Sculpt *sd, Object *ob,
1167                                  PBVHNode **nodes, int totnode)
1168 {
1169         const Brush *brush = paint_brush(&sd->paint);
1170         StrokeCache *cache = ob->sculpt->cache;
1171         
1172         if (cache->mirror_symmetry_pass == 0 &&
1173             cache->radial_symmetry_pass == 0 &&
1174             (cache->first_time || !(brush->flag & BRUSH_ORIGINAL_NORMAL)))
1175         {
1176                 calc_sculpt_normal(sd, ob, nodes, totnode, cache->sculpt_normal);
1177                 copy_v3_v3(cache->sculpt_normal_symm, cache->sculpt_normal);
1178         }
1179         else {
1180                 copy_v3_v3(cache->sculpt_normal_symm, cache->sculpt_normal);
1181                 flip_v3(cache->sculpt_normal_symm, cache->mirror_symmetry_pass);
1182                 mul_m4_v3(cache->symm_rot_mat, cache->sculpt_normal_symm);
1183         }
1184 }
1185
1186 static void calc_local_y(ViewContext *vc, const float center[3], float y[3])
1187 {
1188         Object *ob = vc->obact;
1189         float loc[3], mval_f[2] = {0.0f, 1.0f};
1190         float zfac;
1191
1192         mul_v3_m4v3(loc, ob->imat, center);
1193         zfac = ED_view3d_calc_zfac(vc->rv3d, loc, NULL);
1194
1195         ED_view3d_win_to_delta(vc->ar, mval_f, y, zfac);
1196         normalize_v3(y);
1197
1198         add_v3_v3(y, ob->loc);
1199         mul_m4_v3(ob->imat, y);
1200 }
1201
1202 static void calc_brush_local_mat(const Brush *brush, Object *ob,
1203                                  float local_mat[4][4])
1204 {
1205         const StrokeCache *cache = ob->sculpt->cache;
1206         float tmat[4][4];
1207         float mat[4][4];
1208         float scale[4][4];
1209         float angle, v[3];
1210         float up[3];
1211
1212         /* Ensure ob->imat is up to date */
1213         invert_m4_m4(ob->imat, ob->obmat);
1214
1215         /* Initialize last column of matrix */
1216         mat[0][3] = 0;
1217         mat[1][3] = 0;
1218         mat[2][3] = 0;
1219         mat[3][3] = 1;
1220
1221         /* Get view's up vector in object-space */
1222         calc_local_y(cache->vc, cache->location, up);
1223
1224         /* Calculate the X axis of the local matrix */
1225         cross_v3_v3v3(v, up, cache->sculpt_normal);
1226         /* Apply rotation (user angle, rake, etc.) to X axis */
1227         angle = brush->mtex.rot - cache->special_rotation;
1228         rotate_v3_v3v3fl(mat[0], v, cache->sculpt_normal, angle);
1229
1230         /* Get other axes */
1231         cross_v3_v3v3(mat[1], cache->sculpt_normal, mat[0]);
1232         copy_v3_v3(mat[2], cache->sculpt_normal);
1233
1234         /* Set location */
1235         copy_v3_v3(mat[3], cache->location);
1236
1237         /* Scale by brush radius */
1238         normalize_m4(mat);
1239         scale_m4_fl(scale, cache->radius);
1240         mult_m4_m4m4(tmat, mat, scale);
1241
1242         /* Return inverse (for converting from modelspace coords to local
1243          * area coords) */
1244         invert_m4_m4(local_mat, tmat);
1245 }
1246
1247 static void update_brush_local_mat(Sculpt *sd, Object *ob)
1248 {
1249         StrokeCache *cache = ob->sculpt->cache;
1250
1251         if (cache->mirror_symmetry_pass == 0 &&
1252             cache->radial_symmetry_pass == 0)
1253         {
1254                 calc_brush_local_mat(paint_brush(&sd->paint), ob,
1255                                      cache->brush_local_mat);
1256         }
1257 }
1258
1259 /* Test whether the StrokeCache.sculpt_normal needs update in
1260  * do_brush_action() */
1261 static int brush_needs_sculpt_normal(const Brush *brush)
1262 {
1263         return ((ELEM(brush->sculpt_tool,
1264                       SCULPT_TOOL_GRAB,
1265                       SCULPT_TOOL_SNAKE_HOOK) &&
1266                  ((brush->normal_weight > 0) ||
1267                   (brush->flag & BRUSH_FRONTFACE))) ||
1268
1269                 ELEM7(brush->sculpt_tool,
1270                       SCULPT_TOOL_BLOB,
1271                       SCULPT_TOOL_CREASE,
1272                       SCULPT_TOOL_DRAW,
1273                       SCULPT_TOOL_LAYER,
1274                       SCULPT_TOOL_NUDGE,
1275                       SCULPT_TOOL_ROTATE,
1276                       SCULPT_TOOL_THUMB) ||
1277
1278                 (brush->mtex.brush_map_mode == MTEX_MAP_MODE_AREA));
1279 }
1280
1281 /* For the smooth brush, uses the neighboring vertices around vert to calculate
1282  * a smoothed location for vert. Skips corner vertices (used by only one
1283  * polygon.) */
1284 static void neighbor_average(SculptSession *ss, float avg[3], unsigned vert)
1285 {
1286         const MeshElemMap *vert_map = &ss->pmap[vert];
1287         const MVert *mvert = ss->mvert;
1288         float (*deform_co)[3] = ss->deform_cos;
1289
1290         zero_v3(avg);
1291                 
1292         /* Don't modify corner vertices */
1293         if (vert_map->count > 1) {
1294                 int i, total = 0;
1295
1296                 for (i = 0; i < vert_map->count; i++) {
1297                         const MPoly *p = &ss->mpoly[vert_map->indices[i]];
1298                         unsigned f_adj_v[3];
1299
1300                         if (poly_get_adj_loops_from_vert(f_adj_v, p, ss->mloop, vert) != -1) {
1301                                 int j;
1302                         
1303                                 for (j = 0; j < 3; j++) {
1304                                         if (vert_map->count != 2 || ss->pmap[f_adj_v[j]].count <= 2) {
1305                                                 add_v3_v3(avg, deform_co ? deform_co[f_adj_v[j]] :
1306                                                           mvert[f_adj_v[j]].co);
1307
1308                                                 total++;
1309                                         }
1310                                 }
1311                         }
1312                 }
1313
1314                 if (total > 0) {
1315                         mul_v3_fl(avg, 1.0f / total);
1316                         return;
1317                 }
1318         }
1319
1320         copy_v3_v3(avg, deform_co ? deform_co[vert] : mvert[vert].co);
1321 }
1322
1323 /* Similar to neighbor_average(), but returns an averaged mask value
1324  * instead of coordinate. Also does not restrict based on border or
1325  * corner vertices. */
1326 static float neighbor_average_mask(SculptSession *ss, unsigned vert)
1327 {
1328         const float *vmask = ss->vmask;
1329         float avg = 0;
1330         int i, total = 0;
1331
1332         for (i = 0; i < ss->pmap[vert].count; i++) {
1333                 const MPoly *p = &ss->mpoly[ss->pmap[vert].indices[i]];
1334                 unsigned f_adj_v[3];
1335
1336                 if (poly_get_adj_loops_from_vert(f_adj_v, p, ss->mloop, vert) != -1) {
1337                         int j;
1338                         
1339                         for (j = 0; j < 3; j++) {
1340                                 avg += vmask[f_adj_v[j]];
1341                                 total++;
1342                         }
1343                 }
1344         }
1345
1346         if (total > 0)
1347                 return avg / (float)total;
1348         else
1349                 return vmask[vert];
1350 }
1351
1352 /* Same logic as neighbor_average(), but for bmesh rather than mesh */
1353 static void bmesh_neighbor_average(float avg[3], BMVert *v)
1354 {
1355         const int vfcount = BM_vert_face_count(v);
1356
1357         zero_v3(avg);
1358                 
1359         /* Don't modify corner vertices */
1360         if (vfcount > 1) {
1361                 BMIter liter;
1362                 BMLoop *l;
1363                 int i, total = 0;
1364
1365                 BM_ITER_ELEM (l, &liter, v, BM_LOOPS_OF_VERT) {
1366                         BMVert *adj_v[3] = {l->prev->v, v, l->next->v};
1367
1368                         for (i = 0; i < 3; i++) {
1369                                 if (vfcount != 2 || BM_vert_face_count(adj_v[i]) <= 2) {
1370                                         add_v3_v3(avg, adj_v[i]->co);
1371                                         total++;
1372                                 }
1373                         }
1374                 }
1375
1376                 if (total > 0) {
1377                         mul_v3_fl(avg, 1.0f / total);
1378                         return;
1379                 }
1380         }
1381
1382         copy_v3_v3(avg, v->co);
1383 }
1384
1385 /* Same logic as neighbor_average_mask(), but for bmesh rather than mesh */
1386 static float bmesh_neighbor_average_mask(BMesh *bm, BMVert *v)
1387 {
1388         BMIter liter;
1389         BMLoop *l;
1390         float avg = 0;
1391         int i, total = 0;
1392
1393         BM_ITER_ELEM (l, &liter, v, BM_LOOPS_OF_VERT) {
1394                 BMVert *adj_v[3] = {l->prev->v, v, l->next->v};
1395
1396                 for (i = 0; i < 3; i++) {
1397                         BMVert *v2 = adj_v[i];
1398                         float *vmask = CustomData_bmesh_get(&bm->vdata,
1399                                                             v2->head.data,
1400                                                             CD_PAINT_MASK);
1401                         avg += (*vmask);
1402                         total++;
1403                 }
1404         }
1405
1406         if (total > 0) {
1407                 return avg / (float)total;
1408         }
1409         else {
1410                 float *vmask = CustomData_bmesh_get(&bm->vdata,
1411                                                         v->head.data,
1412                                                         CD_PAINT_MASK);
1413                 return (*vmask);
1414         }
1415 }
1416
1417 static void do_mesh_smooth_brush(Sculpt *sd, SculptSession *ss, PBVHNode *node, float bstrength, int smooth_mask)
1418 {
1419         Brush *brush = paint_brush(&sd->paint);
1420         PBVHVertexIter vd;
1421         SculptBrushTest test;
1422         
1423         CLAMP(bstrength, 0.0f, 1.0f);
1424
1425         sculpt_brush_test_init(ss, &test);
1426
1427         BKE_pbvh_vertex_iter_begin(ss->pbvh, node, vd, PBVH_ITER_UNIQUE)
1428         {
1429                 if (sculpt_brush_test(&test, vd.co)) {
1430                         const float fade = bstrength * tex_strength(ss, brush, vd.co, test.dist,
1431                                                                     ss->cache->view_normal, vd.no, vd.fno,
1432                                                                     smooth_mask ? 0 : (vd.mask ? *vd.mask : 0.0f));
1433                         if (smooth_mask) {
1434                                 float val = neighbor_average_mask(ss, vd.vert_indices[vd.i]) - *vd.mask;
1435                                 val *= fade * bstrength;
1436                                 *vd.mask += val;
1437                                 CLAMP(*vd.mask, 0, 1);
1438                         }
1439                         else {
1440                                 float avg[3], val[3];
1441
1442                                 neighbor_average(ss, avg, vd.vert_indices[vd.i]);
1443                                 sub_v3_v3v3(val, avg, vd.co);
1444                                 mul_v3_fl(val, fade);
1445
1446                                 add_v3_v3(val, vd.co);
1447
1448                                 sculpt_clip(sd, ss, vd.co, val);
1449                         }
1450
1451                         if (vd.mvert)
1452                                 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
1453                 }
1454         }
1455         BKE_pbvh_vertex_iter_end;
1456 }
1457
1458 static void do_bmesh_smooth_brush(Sculpt *sd, SculptSession *ss, PBVHNode *node, float bstrength, int smooth_mask)
1459 {
1460         Brush *brush = paint_brush(&sd->paint);
1461         PBVHVertexIter vd;
1462         SculptBrushTest test;
1463         
1464         CLAMP(bstrength, 0.0f, 1.0f);
1465
1466         sculpt_brush_test_init(ss, &test);
1467
1468         BKE_pbvh_vertex_iter_begin(ss->pbvh, node, vd, PBVH_ITER_UNIQUE)
1469         {
1470                 if (sculpt_brush_test(&test, vd.co)) {
1471                         const float fade = bstrength * tex_strength(ss, brush, vd.co, test.dist,
1472                                                                     ss->cache->view_normal, vd.no, vd.fno,
1473                                                                     smooth_mask ? 0 : *vd.mask);
1474                         if (smooth_mask) {
1475                                 float val = bmesh_neighbor_average_mask(ss->bm, vd.bm_vert) - *vd.mask;
1476                                 val *= fade * bstrength;
1477                                 *vd.mask += val;
1478                                 CLAMP(*vd.mask, 0, 1);
1479                         }
1480                         else {
1481                                 float avg[3], val[3];
1482
1483                                 bmesh_neighbor_average(avg, vd.bm_vert);
1484                                 sub_v3_v3v3(val, avg, vd.co);
1485                                 mul_v3_fl(val, fade);
1486
1487                                 add_v3_v3(val, vd.co);
1488
1489                                 sculpt_clip(sd, ss, vd.co, val);
1490                         }
1491
1492                         if (vd.mvert)
1493                                 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
1494                 }
1495         }
1496         BKE_pbvh_vertex_iter_end;
1497 }
1498
1499 static void do_multires_smooth_brush(Sculpt *sd, SculptSession *ss, PBVHNode *node,
1500                                      float bstrength, int smooth_mask)
1501 {
1502         Brush *brush = paint_brush(&sd->paint);
1503         SculptBrushTest test;
1504         CCGElem **griddata, *data;
1505         CCGKey key;
1506         DMGridAdjacency *gridadj, *adj;
1507         float (*tmpgrid_co)[3], (*tmprow_co)[3];
1508         float *tmpgrid_mask, *tmprow_mask;
1509         int v1, v2, v3, v4;
1510         int thread_num;
1511         int *grid_indices, totgrid, gridsize, i, x, y;
1512
1513         sculpt_brush_test_init(ss, &test);
1514
1515         CLAMP(bstrength, 0.0f, 1.0f);
1516
1517         BKE_pbvh_node_get_grids(ss->pbvh, node, &grid_indices, &totgrid,
1518                                 NULL, &gridsize, &griddata, &gridadj);
1519         BKE_pbvh_get_grid_key(ss->pbvh, &key);
1520
1521         thread_num = 0;
1522 #ifdef _OPENMP
1523         if (sd->flags & SCULPT_USE_OPENMP)
1524                 thread_num = omp_get_thread_num();
1525 #endif
1526         tmpgrid_co = ss->cache->tmpgrid_co[thread_num];
1527         tmprow_co = ss->cache->tmprow_co[thread_num];
1528         tmpgrid_mask = ss->cache->tmpgrid_mask[thread_num];
1529         tmprow_mask = ss->cache->tmprow_mask[thread_num];
1530
1531         for (i = 0; i < totgrid; ++i) {
1532                 data = griddata[grid_indices[i]];
1533                 adj = &gridadj[grid_indices[i]];
1534
1535                 if (smooth_mask)
1536                         memset(tmpgrid_mask, 0, sizeof(float) * gridsize * gridsize);
1537                 else
1538                         memset(tmpgrid_co, 0, sizeof(float) * 3 * gridsize * gridsize);
1539
1540                 for (y = 0; y < gridsize - 1; y++) {
1541                         v1 = y * gridsize;
1542                         if (smooth_mask) {
1543                                 tmprow_mask[0] = (*CCG_elem_offset_mask(&key, data, v1) +
1544                                                   *CCG_elem_offset_mask(&key, data, v1 + gridsize));
1545                         }
1546                         else {
1547                                 add_v3_v3v3(tmprow_co[0],
1548                                             CCG_elem_offset_co(&key, data, v1),
1549                                             CCG_elem_offset_co(&key, data, v1 + gridsize));
1550                         }
1551
1552                         for (x = 0; x < gridsize - 1; x++) {
1553                                 v1 = x + y * gridsize;
1554                                 v2 = v1 + 1;
1555                                 v3 = v1 + gridsize;
1556                                 v4 = v3 + 1;
1557
1558                                 if (smooth_mask) {
1559                                         float tmp;
1560
1561                                         tmprow_mask[x + 1] = (*CCG_elem_offset_mask(&key, data, v2) +
1562                                                               *CCG_elem_offset_mask(&key, data, v4));
1563                                         tmp = tmprow_mask[x + 1] + tmprow_mask[x];
1564
1565                                         tmpgrid_mask[v1] += tmp;
1566                                         tmpgrid_mask[v2] += tmp;
1567                                         tmpgrid_mask[v3] += tmp;
1568                                         tmpgrid_mask[v4] += tmp;
1569                                 }
1570                                 else {
1571                                         float tmp[3];
1572
1573                                         add_v3_v3v3(tmprow_co[x + 1],
1574                                                     CCG_elem_offset_co(&key, data, v2),
1575                                                     CCG_elem_offset_co(&key, data, v4));
1576                                         add_v3_v3v3(tmp, tmprow_co[x + 1], tmprow_co[x]);
1577
1578                                         add_v3_v3(tmpgrid_co[v1], tmp);
1579                                         add_v3_v3(tmpgrid_co[v2], tmp);
1580                                         add_v3_v3(tmpgrid_co[v3], tmp);
1581                                         add_v3_v3(tmpgrid_co[v4], tmp);
1582                                 }
1583                         }
1584                 }
1585
1586                 /* blend with existing coordinates */
1587                 for (y = 0; y < gridsize; ++y) {
1588                         for (x = 0; x < gridsize; ++x) {
1589                                 float *co;
1590                                 float *fno;
1591                                 float *mask;
1592                                 int index;
1593
1594                                 if (x == 0 && adj->index[0] == -1)
1595                                         continue;
1596
1597                                 if (x == gridsize - 1 && adj->index[2] == -1)
1598                                         continue;
1599
1600                                 if (y == 0 && adj->index[3] == -1)
1601                                         continue;
1602
1603                                 if (y == gridsize - 1 && adj->index[1] == -1)
1604                                         continue;
1605
1606                                 index = x + y * gridsize;
1607                                 co = CCG_elem_offset_co(&key, data, index);
1608                                 fno = CCG_elem_offset_no(&key, data, index);
1609                                 mask = CCG_elem_offset_mask(&key, data, index);
1610
1611                                 if (sculpt_brush_test(&test, co)) {
1612                                         const float strength_mask = (smooth_mask ? 0 : *mask);
1613                                         const float fade = bstrength * tex_strength(ss, brush, co, test.dist,
1614                                                                                     ss->cache->view_normal,
1615                                                                                     NULL, fno, strength_mask);
1616                                         float n = 1.0f / 16.0f;
1617                                         
1618                                         if (x == 0 || x == gridsize - 1)
1619                                                 n *= 2;
1620                                         
1621                                         if (y == 0 || y == gridsize - 1)
1622                                                 n *= 2;
1623                                         
1624                                         if (smooth_mask) {
1625                                                 *mask += ((tmpgrid_mask[x + y * gridsize] * n) - *mask) * fade;
1626                                         }
1627                                         else {
1628                                                 float *avg, val[3];
1629
1630                                                 avg = tmpgrid_co[x + y * gridsize];
1631
1632                                                 mul_v3_fl(avg, n);
1633
1634                                                 sub_v3_v3v3(val, avg, co);
1635                                                 mul_v3_fl(val, fade);
1636
1637                                                 add_v3_v3(val, co);
1638
1639                                                 sculpt_clip(sd, ss, co, val);
1640                                         }
1641                                 }
1642                         }
1643                 }
1644         }
1645 }
1646
1647 static void smooth(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode,
1648                    float bstrength, int smooth_mask)
1649 {
1650         SculptSession *ss = ob->sculpt;
1651         const int max_iterations = 4;
1652         const float fract = 1.0f / max_iterations;
1653         PBVHType type = BKE_pbvh_type(ss->pbvh);
1654         int iteration, n, count;
1655         float last;
1656
1657         CLAMP(bstrength, 0, 1);
1658
1659         count = (int)(bstrength * max_iterations);
1660         last  = max_iterations * (bstrength - count * fract);
1661
1662         if (type == PBVH_FACES && !ss->pmap) {
1663                 BLI_assert(!"sculpt smooth: pmap missing");
1664                 return;
1665         }
1666
1667         for (iteration = 0; iteration <= count; ++iteration) {
1668                 float strength = (iteration != count) ? 1.0f : last;
1669
1670                 #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
1671                 for (n = 0; n < totnode; n++) {
1672                         switch (type) {
1673                                 case PBVH_GRIDS:
1674                                         do_multires_smooth_brush(sd, ss, nodes[n], strength,
1675                                                                  smooth_mask);
1676                                         break;
1677                                 case PBVH_FACES:
1678                                         do_mesh_smooth_brush(sd, ss, nodes[n], strength,
1679                                                              smooth_mask);
1680                                         break;
1681                                 case PBVH_BMESH:
1682                                         do_bmesh_smooth_brush(sd, ss, nodes[n], strength, smooth_mask);
1683                                         break;
1684                         }
1685                 }
1686
1687                 if (ss->multires)
1688                         multires_stitch_grids(ob);
1689         }
1690 }
1691
1692 static void do_smooth_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
1693 {
1694         SculptSession *ss = ob->sculpt;
1695         smooth(sd, ob, nodes, totnode, ss->cache->bstrength, FALSE);
1696 }
1697
1698 static void do_mask_brush_draw(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
1699 {
1700         SculptSession *ss = ob->sculpt;
1701         Brush *brush = paint_brush(&sd->paint);
1702         float bstrength = ss->cache->bstrength;
1703         int n;
1704
1705         /* threaded loop over nodes */
1706         #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
1707         for (n = 0; n < totnode; n++) {
1708                 PBVHVertexIter vd;
1709                 SculptBrushTest test;
1710
1711                 sculpt_brush_test_init(ss, &test);
1712
1713                 BKE_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE)
1714                 {
1715                         if (sculpt_brush_test(&test, vd.co)) {
1716                                 float fade = tex_strength(ss, brush, vd.co, test.dist,
1717                                                           ss->cache->view_normal, vd.no, vd.fno, 0);
1718
1719                                 (*vd.mask) += fade * bstrength;
1720                                 CLAMP(*vd.mask, 0, 1);
1721
1722                                 if (vd.mvert)
1723                                         vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
1724                         }
1725                         BKE_pbvh_vertex_iter_end;
1726                 }
1727         }
1728 }
1729
1730 static void do_mask_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
1731 {
1732         SculptSession *ss = ob->sculpt;
1733         Brush *brush = paint_brush(&sd->paint);
1734         
1735         switch ((BrushMaskTool)brush->mask_tool) {
1736                 case BRUSH_MASK_DRAW:
1737                         do_mask_brush_draw(sd, ob, nodes, totnode);
1738                         break;
1739                 case BRUSH_MASK_SMOOTH:
1740                         smooth(sd, ob, nodes, totnode, ss->cache->bstrength, TRUE);
1741                         break;
1742         }
1743 }
1744
1745 static void do_draw_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
1746 {
1747         SculptSession *ss = ob->sculpt;
1748         Brush *brush = paint_brush(&sd->paint);
1749         float offset[3];
1750         float bstrength = ss->cache->bstrength;
1751         int n;
1752
1753         /* offset with as much as possible factored in already */
1754         mul_v3_v3fl(offset, ss->cache->sculpt_normal_symm, ss->cache->radius);
1755         mul_v3_v3(offset, ss->cache->scale);
1756         mul_v3_fl(offset, bstrength);
1757
1758         /* threaded loop over nodes */
1759         #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
1760         for (n = 0; n < totnode; n++) {
1761                 PBVHVertexIter vd;
1762                 SculptBrushTest test;
1763                 float (*proxy)[3];
1764
1765                 proxy = BKE_pbvh_node_add_proxy(ss->pbvh, nodes[n])->co;
1766
1767                 sculpt_brush_test_init(ss, &test);
1768
1769                 BKE_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE)
1770                 {
1771                         if (sculpt_brush_test(&test, vd.co)) {
1772                                 /* offset vertex */
1773                                 float fade = tex_strength(ss, brush, vd.co, test.dist,
1774                                                           ss->cache->sculpt_normal_symm, vd.no,
1775                                                           vd.fno, vd.mask ? *vd.mask : 0.0f);
1776
1777                                 mul_v3_v3fl(proxy[vd.i], offset, fade);
1778
1779                                 if (vd.mvert)
1780                                         vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
1781                         }
1782                 }
1783                 BKE_pbvh_vertex_iter_end;
1784         }
1785 }
1786
1787 static void do_crease_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
1788 {
1789         SculptSession *ss = ob->sculpt;
1790         const Scene *scene = ss->cache->vc->scene;
1791         Brush *brush = paint_brush(&sd->paint);
1792         float offset[3];
1793         float bstrength = ss->cache->bstrength;
1794         float flippedbstrength, crease_correction;
1795         float brush_alpha;
1796         int n;
1797
1798         /* offset with as much as possible factored in already */
1799         mul_v3_v3fl(offset, ss->cache->sculpt_normal_symm, ss->cache->radius);
1800         mul_v3_v3(offset, ss->cache->scale);
1801         mul_v3_fl(offset, bstrength);
1802         
1803         /* we divide out the squared alpha and multiply by the squared crease to give us the pinch strength */
1804         crease_correction = brush->crease_pinch_factor * brush->crease_pinch_factor;
1805         brush_alpha = BKE_brush_alpha_get(scene, brush);
1806         if (brush_alpha > 0.0f)
1807                 crease_correction /= brush_alpha * brush_alpha;
1808
1809         /* we always want crease to pinch or blob to relax even when draw is negative */
1810         flippedbstrength = (bstrength < 0) ? -crease_correction * bstrength : crease_correction * bstrength;
1811
1812         if (brush->sculpt_tool == SCULPT_TOOL_BLOB) flippedbstrength *= -1.0f;
1813
1814         /* threaded loop over nodes */
1815         #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
1816         for (n = 0; n < totnode; n++) {
1817                 PBVHVertexIter vd;
1818                 SculptBrushTest test;
1819                 float (*proxy)[3];
1820
1821                 proxy = BKE_pbvh_node_add_proxy(ss->pbvh, nodes[n])->co;
1822
1823                 sculpt_brush_test_init(ss, &test);
1824
1825                 BKE_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE)
1826                 {
1827                         if (sculpt_brush_test(&test, vd.co)) {
1828                                 /* offset vertex */
1829                                 const float fade = tex_strength(ss, brush, vd.co, test.dist,
1830                                                                 ss->cache->sculpt_normal_symm,
1831                                                                 vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f);
1832                                 float val1[3];
1833                                 float val2[3];
1834
1835                                 /* first we pinch */
1836                                 sub_v3_v3v3(val1, test.location, vd.co);
1837                                 mul_v3_fl(val1, fade * flippedbstrength);
1838
1839                                 /* then we draw */
1840                                 mul_v3_v3fl(val2, offset, fade);
1841
1842                                 add_v3_v3v3(proxy[vd.i], val1, val2);
1843
1844                                 if (vd.mvert)
1845                                         vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
1846                         }
1847                 }
1848                 BKE_pbvh_vertex_iter_end;
1849         }
1850 }
1851
1852 static void do_pinch_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
1853 {
1854         SculptSession *ss = ob->sculpt;
1855         Brush *brush = paint_brush(&sd->paint);
1856         float bstrength = ss->cache->bstrength;
1857         int n;
1858
1859         #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
1860         for (n = 0; n < totnode; n++) {
1861                 PBVHVertexIter vd;
1862                 SculptBrushTest test;
1863                 float (*proxy)[3];
1864
1865                 proxy = BKE_pbvh_node_add_proxy(ss->pbvh, nodes[n])->co;
1866
1867                 sculpt_brush_test_init(ss, &test);
1868
1869                 BKE_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE)
1870                 {
1871                         if (sculpt_brush_test(&test, vd.co)) {
1872                                 float fade = bstrength * tex_strength(ss, brush, vd.co, test.dist,
1873                                                                       ss->cache->view_normal, vd.no,
1874                                                                       vd.fno, vd.mask ? *vd.mask : 0.0f);
1875                                 float val[3];
1876
1877                                 sub_v3_v3v3(val, test.location, vd.co);
1878                                 mul_v3_v3fl(proxy[vd.i], val, fade);
1879
1880                                 if (vd.mvert)
1881                                         vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
1882                         }
1883                 }
1884                 BKE_pbvh_vertex_iter_end;
1885         }
1886 }
1887
1888 static void do_grab_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
1889 {
1890         SculptSession *ss = ob->sculpt;
1891         Brush *brush = paint_brush(&sd->paint);
1892         float bstrength = ss->cache->bstrength;
1893         float grab_delta[3];
1894         int n;
1895         float len;
1896
1897         copy_v3_v3(grab_delta, ss->cache->grab_delta_symmetry);
1898
1899         len = len_v3(grab_delta);
1900
1901         if (brush->normal_weight > 0) {
1902                 mul_v3_fl(ss->cache->sculpt_normal_symm, len * brush->normal_weight);
1903                 mul_v3_fl(grab_delta, 1.0f - brush->normal_weight);
1904                 add_v3_v3(grab_delta, ss->cache->sculpt_normal_symm);
1905         }
1906
1907         #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
1908         for (n = 0; n < totnode; n++) {
1909                 PBVHVertexIter vd;
1910                 SculptBrushTest test;
1911                 SculptOrigVertData orig_data;
1912                 float (*proxy)[3];
1913
1914                 sculpt_orig_vert_data_init(&orig_data, ob, nodes[n]);
1915
1916                 proxy = BKE_pbvh_node_add_proxy(ss->pbvh, nodes[n])->co;
1917
1918                 sculpt_brush_test_init(ss, &test);
1919
1920                 BKE_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE)
1921                 {
1922                         sculpt_orig_vert_data_update(&orig_data, &vd);
1923
1924                         if (sculpt_brush_test(&test, orig_data.co)) {
1925                                 const float fade = bstrength * tex_strength(ss, brush,
1926                                                                             orig_data.co,
1927                                                                             test.dist,
1928                                                                             ss->cache->sculpt_normal_symm,
1929                                                                             orig_data.no,
1930                                                                             NULL, vd.mask ? *vd.mask : 0.0f);
1931
1932                                 mul_v3_v3fl(proxy[vd.i], grab_delta, fade);
1933
1934                                 if (vd.mvert)
1935                                         vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
1936                         }
1937                 }
1938                 BKE_pbvh_vertex_iter_end;
1939         }
1940 }
1941
1942 static void do_nudge_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
1943 {
1944         SculptSession *ss = ob->sculpt;
1945         Brush *brush = paint_brush(&sd->paint);
1946         float bstrength = ss->cache->bstrength;
1947         float grab_delta[3];
1948         float tmp[3], cono[3];
1949         int n;
1950
1951         copy_v3_v3(grab_delta, ss->cache->grab_delta_symmetry);
1952
1953         cross_v3_v3v3(tmp, ss->cache->sculpt_normal_symm, grab_delta);
1954         cross_v3_v3v3(cono, tmp, ss->cache->sculpt_normal_symm);
1955
1956         #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
1957         for (n = 0; n < totnode; n++) {
1958                 PBVHVertexIter vd;
1959                 SculptBrushTest test;
1960                 float (*proxy)[3];
1961
1962                 proxy = BKE_pbvh_node_add_proxy(ss->pbvh, nodes[n])->co;
1963
1964                 sculpt_brush_test_init(ss, &test);
1965
1966                 BKE_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE)
1967                 {
1968                         if (sculpt_brush_test(&test, vd.co)) {
1969                                 const float fade = bstrength * tex_strength(ss, brush, vd.co, test.dist,
1970                                                                             ss->cache->sculpt_normal_symm,
1971                                                                             vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f);
1972
1973                                 mul_v3_v3fl(proxy[vd.i], cono, fade);
1974
1975                                 if (vd.mvert)
1976                                         vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
1977                         }
1978                 }
1979                 BKE_pbvh_vertex_iter_end;
1980         }
1981 }
1982
1983 static void do_snake_hook_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
1984 {
1985         SculptSession *ss = ob->sculpt;
1986         Brush *brush = paint_brush(&sd->paint);
1987         float bstrength = ss->cache->bstrength;
1988         float grab_delta[3];
1989         int n;
1990         float len;
1991
1992         copy_v3_v3(grab_delta, ss->cache->grab_delta_symmetry);
1993
1994         len = len_v3(grab_delta);
1995
1996         if (bstrength < 0)
1997                 negate_v3(grab_delta);
1998
1999         if (brush->normal_weight > 0) {
2000                 mul_v3_fl(ss->cache->sculpt_normal_symm, len * brush->normal_weight);
2001                 mul_v3_fl(grab_delta, 1.0f - brush->normal_weight);
2002                 add_v3_v3(grab_delta, ss->cache->sculpt_normal_symm);
2003         }
2004
2005         #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
2006         for (n = 0; n < totnode; n++) {
2007                 PBVHVertexIter vd;
2008                 SculptBrushTest test;
2009                 float (*proxy)[3];
2010
2011                 proxy = BKE_pbvh_node_add_proxy(ss->pbvh, nodes[n])->co;
2012
2013                 sculpt_brush_test_init(ss, &test);
2014
2015                 BKE_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE)
2016                 {
2017                         if (sculpt_brush_test(&test, vd.co)) {
2018                                 const float fade = bstrength * tex_strength(ss, brush, vd.co, test.dist,
2019                                                                             ss->cache->sculpt_normal_symm,
2020                                                                             vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f);
2021
2022                                 mul_v3_v3fl(proxy[vd.i], grab_delta, fade);
2023
2024                                 if (vd.mvert)
2025                                         vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
2026                         }
2027                 }
2028                 BKE_pbvh_vertex_iter_end;
2029         }
2030 }
2031
2032 static void do_thumb_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
2033 {
2034         SculptSession *ss = ob->sculpt;
2035         Brush *brush = paint_brush(&sd->paint);
2036         float bstrength = ss->cache->bstrength;
2037         float grab_delta[3];
2038         float tmp[3], cono[3];
2039         int n;
2040
2041         copy_v3_v3(grab_delta, ss->cache->grab_delta_symmetry);
2042
2043         cross_v3_v3v3(tmp, ss->cache->sculpt_normal_symm, grab_delta);
2044         cross_v3_v3v3(cono, tmp, ss->cache->sculpt_normal_symm);
2045
2046         #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
2047         for (n = 0; n < totnode; n++) {
2048                 PBVHVertexIter vd;
2049                 SculptBrushTest test;
2050                 SculptOrigVertData orig_data;
2051                 float (*proxy)[3];
2052
2053                 sculpt_orig_vert_data_init(&orig_data, ob, nodes[n]);
2054
2055                 proxy = BKE_pbvh_node_add_proxy(ss->pbvh, nodes[n])->co;
2056
2057                 sculpt_brush_test_init(ss, &test);
2058
2059                 BKE_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE)
2060                 {
2061                         sculpt_orig_vert_data_update(&orig_data, &vd);
2062
2063                         if (sculpt_brush_test(&test, orig_data.co)) {
2064                                 const float fade = bstrength * tex_strength(ss, brush,
2065                                                                             orig_data.co,
2066                                                                             test.dist,
2067                                                                             ss->cache->sculpt_normal_symm,
2068                                                                             orig_data.no,
2069                                                                             NULL, vd.mask ? *vd.mask : 0.0f);
2070
2071                                 mul_v3_v3fl(proxy[vd.i], cono, fade);
2072
2073                                 if (vd.mvert)
2074                                         vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
2075                         }
2076                 }
2077                 BKE_pbvh_vertex_iter_end;
2078         }
2079 }
2080
2081 static void do_rotate_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
2082 {
2083         SculptSession *ss = ob->sculpt;
2084         Brush *brush = paint_brush(&sd->paint);
2085         float bstrength = ss->cache->bstrength;
2086         int n;
2087         float m[4][4], rot[4][4], lmat[4][4], ilmat[4][4];
2088         static const int flip[8] = { 1, -1, -1, 1, -1, 1, 1, -1 };
2089         float angle = ss->cache->vertex_rotation * flip[ss->cache->mirror_symmetry_pass];
2090
2091         unit_m4(m);
2092         unit_m4(lmat);
2093
2094         copy_v3_v3(lmat[3], ss->cache->location);
2095         invert_m4_m4(ilmat, lmat);
2096         axis_angle_to_mat4(rot, ss->cache->sculpt_normal_symm, angle);
2097
2098         mul_serie_m4(m, lmat, rot, ilmat, NULL, NULL, NULL, NULL, NULL);
2099
2100         #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
2101         for (n = 0; n < totnode; n++) {
2102                 PBVHVertexIter vd;
2103                 SculptBrushTest test;
2104                 SculptOrigVertData orig_data;
2105                 float (*proxy)[3];
2106
2107                 sculpt_orig_vert_data_init(&orig_data, ob, nodes[n]);
2108
2109                 proxy = BKE_pbvh_node_add_proxy(ss->pbvh, nodes[n])->co;
2110
2111                 sculpt_brush_test_init(ss, &test);
2112
2113                 BKE_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE)
2114                 {
2115                         sculpt_orig_vert_data_update(&orig_data, &vd);
2116
2117                         if (sculpt_brush_test(&test, orig_data.co)) {
2118                                 const float fade = bstrength * tex_strength(ss, brush,
2119                                                                             orig_data.co,
2120                                                                             test.dist,
2121                                                                             ss->cache->sculpt_normal_symm,
2122                                                                             orig_data.no,
2123                                                                             NULL, vd.mask ? *vd.mask : 0.0f);
2124
2125                                 mul_v3_m4v3(proxy[vd.i], m, orig_data.co);
2126                                 sub_v3_v3(proxy[vd.i], orig_data.co);
2127                                 mul_v3_fl(proxy[vd.i], fade);
2128
2129                                 if (vd.mvert)
2130                                         vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
2131                         }
2132                 }
2133                 BKE_pbvh_vertex_iter_end;
2134         }
2135 }
2136
2137 static void do_layer_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
2138 {
2139         SculptSession *ss = ob->sculpt;
2140         Brush *brush = paint_brush(&sd->paint);
2141         float bstrength = ss->cache->bstrength;
2142         float offset[3];
2143         float lim = brush->height;
2144         int n;
2145
2146         if (bstrength < 0)
2147                 lim = -lim;
2148
2149         mul_v3_v3v3(offset, ss->cache->scale, ss->cache->sculpt_normal_symm);
2150
2151         #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
2152         for (n = 0; n < totnode; n++) {
2153                 PBVHVertexIter vd;
2154                 SculptBrushTest test;
2155                 SculptOrigVertData orig_data;
2156                 float *layer_disp;
2157                 /* XXX: layer brush needs conversion to proxy but its more complicated */
2158                 /* proxy = BKE_pbvh_node_add_proxy(ss->pbvh, nodes[n])->co; */
2159                 
2160                 sculpt_orig_vert_data_init(&orig_data, ob, nodes[n]);
2161
2162                 #pragma omp critical
2163                 {
2164                         layer_disp = BKE_pbvh_node_layer_disp_get(ss->pbvh, nodes[n]);
2165                 }
2166                 
2167                 sculpt_brush_test_init(ss, &test);
2168
2169                 BKE_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE)
2170                 {
2171                         sculpt_orig_vert_data_update(&orig_data, &vd);
2172
2173                         if (sculpt_brush_test(&test, orig_data.co)) {
2174                                 const float fade = bstrength * tex_strength(ss, brush, vd.co, test.dist,
2175                                                                             ss->cache->sculpt_normal_symm,
2176                                                                             vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f);
2177                                 float *disp = &layer_disp[vd.i];
2178                                 float val[3];
2179
2180                                 *disp += fade;
2181
2182                                 /* Don't let the displacement go past the limit */
2183                                 if ((lim < 0 && *disp < lim) || (lim >= 0 && *disp > lim))
2184                                         *disp = lim;
2185
2186                                 mul_v3_v3fl(val, offset, *disp);
2187
2188                                 if (ss->layer_co && (brush->flag & BRUSH_PERSISTENT)) {
2189                                         int index = vd.vert_indices[vd.i];
2190
2191                                         /* persistent base */
2192                                         add_v3_v3(val, ss->layer_co[index]);
2193                                 }
2194                                 else {
2195                                         add_v3_v3(val, orig_data.co);
2196                                 }
2197
2198                                 sculpt_clip(sd, ss, vd.co, val);
2199
2200                                 if (vd.mvert)
2201                                         vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
2202                         }
2203                 }
2204                 BKE_pbvh_vertex_iter_end;
2205         }
2206 }
2207
2208 static void do_inflate_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
2209 {
2210         SculptSession *ss = ob->sculpt;
2211         Brush *brush = paint_brush(&sd->paint);
2212         float bstrength = ss->cache->bstrength;
2213         int n;
2214
2215         #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
2216         for (n = 0; n < totnode; n++) {
2217                 PBVHVertexIter vd;
2218                 SculptBrushTest test;
2219                 float (*proxy)[3];
2220
2221                 proxy = BKE_pbvh_node_add_proxy(ss->pbvh, nodes[n])->co;
2222
2223                 sculpt_brush_test_init(ss, &test);
2224
2225                 BKE_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE)
2226                 {
2227                         if (sculpt_brush_test(&test, vd.co)) {
2228                                 const float fade = bstrength * tex_strength(ss, brush, vd.co, test.dist,
2229                                                                             ss->cache->view_normal,
2230                                                                             vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f);
2231                                 float val[3];
2232
2233                                 if (vd.fno) copy_v3_v3(val, vd.fno);
2234                                 else normal_short_to_float_v3(val, vd.no);
2235                                 
2236                                 mul_v3_fl(val, fade * ss->cache->radius);
2237                                 mul_v3_v3v3(proxy[vd.i], val, ss->cache->scale);
2238
2239                                 if (vd.mvert)
2240                                         vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
2241                         }
2242                 }
2243                 BKE_pbvh_vertex_iter_end;
2244         }
2245 }
2246
2247 static void calc_flatten_center(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode, float fc[3])
2248 {
2249         SculptSession *ss = ob->sculpt;
2250         int n;
2251
2252         float count = 0;
2253
2254         (void)sd; /* unused w/o openmp */
2255
2256         zero_v3(fc);
2257
2258         #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
2259         for (n = 0; n < totnode; n++) {
2260                 PBVHVertexIter vd;
2261                 SculptBrushTest test;
2262                 SculptUndoNode *unode;
2263                 float private_fc[3] = {0.0f, 0.0f, 0.0f};
2264                 int private_count = 0;
2265
2266                 unode = sculpt_undo_push_node(ob, nodes[n], SCULPT_UNDO_COORDS);
2267                 sculpt_brush_test_init(ss, &test);
2268
2269                 if (ss->cache->original && unode->co) {
2270                         BKE_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE)
2271                         {
2272                                 if (sculpt_brush_test_fast(&test, unode->co[vd.i])) {
2273                                         add_v3_v3(private_fc, unode->co[vd.i]);
2274                                         private_count++;
2275                                 }
2276                         }
2277                         BKE_pbvh_vertex_iter_end;
2278                 }
2279                 else {
2280                         BKE_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE)
2281                         {
2282                                 if (sculpt_brush_test_fast(&test, vd.co)) {
2283                                         add_v3_v3(private_fc, vd.co);
2284                                         private_count++;
2285                                 }
2286                         }
2287                         BKE_pbvh_vertex_iter_end;
2288                 }
2289
2290                 #pragma omp critical
2291                 {
2292                         add_v3_v3(fc, private_fc);
2293                         count += private_count;
2294                 }
2295         }
2296
2297         mul_v3_fl(fc, 1.0f / count);
2298 }
2299
2300 /* this calculates flatten center and area normal together, 
2301  * amortizing the memory bandwidth and loop overhead to calculate both at the same time */
2302 static void calc_area_normal_and_flatten_center(Sculpt *sd, Object *ob,
2303                                                 PBVHNode **nodes, int totnode,
2304                                                 float an[3], float fc[3])
2305 {
2306         SculptSession *ss = ob->sculpt;
2307         int n;
2308
2309         /* for area normal */
2310         float out_flip[3] = {0.0f, 0.0f, 0.0f};
2311
2312         /* for flatten center */
2313         float count = 0;
2314
2315         (void)sd; /* unused w/o openmp */
2316         
2317         /* for area normal */
2318         zero_v3(an);
2319
2320         /* for flatten center */
2321         zero_v3(fc);
2322
2323         #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
2324         for (n = 0; n < totnode; n++) {
2325                 PBVHVertexIter vd;
2326                 SculptBrushTest test;
2327                 SculptUndoNode *unode;
2328                 float private_an[3] = {0.0f, 0.0f, 0.0f};
2329                 float private_out_flip[3] = {0.0f, 0.0f, 0.0f};
2330                 float private_fc[3] = {0.0f, 0.0f, 0.0f};
2331                 int private_count = 0;
2332
2333                 unode = sculpt_undo_push_node(ob, nodes[n], SCULPT_UNDO_COORDS);
2334                 sculpt_brush_test_init(ss, &test);
2335
2336                 if (ss->cache->original && unode->co) {
2337                         BKE_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE)
2338                         {
2339                                 if (sculpt_brush_test_fast(&test, unode->co[vd.i])) {
2340                                         /* for area normal */
2341                                         float fno[3];
2342
2343                                         normal_short_to_float_v3(fno, unode->no[vd.i]);
2344                                         add_norm_if(ss->cache->view_normal, private_an, private_out_flip, fno);
2345
2346                                         /* for flatten center */
2347                                         add_v3_v3(private_fc, unode->co[vd.i]);
2348                                         private_count++;
2349                                 }
2350                         }
2351                         BKE_pbvh_vertex_iter_end;
2352                 }
2353                 else {
2354                         BKE_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE)
2355                         {
2356                                 if (sculpt_brush_test_fast(&test, vd.co)) {
2357                                         /* for area normal */
2358                                         if (vd.no) {
2359                                                 float fno[3];
2360
2361                                                 normal_short_to_float_v3(fno, vd.no);
2362                                                 add_norm_if(ss->cache->view_normal, private_an, private_out_flip, fno);
2363                                         }
2364                                         else {
2365                                                 add_norm_if(ss->cache->view_normal, private_an, private_out_flip, vd.fno);
2366                                         }
2367
2368                                         /* for flatten center */
2369                                         add_v3_v3(private_fc, vd.co);
2370                                         private_count++;
2371                                 }
2372                         }
2373                         BKE_pbvh_vertex_iter_end;
2374                 }
2375
2376                 #pragma omp critical
2377                 {
2378                         /* for area normal */
2379                         add_v3_v3(an, private_an);
2380                         add_v3_v3(out_flip, private_out_flip);
2381
2382                         /* for flatten center */
2383                         add_v3_v3(fc, private_fc);
2384                         count += private_count;
2385                 }
2386         }
2387
2388         /* for area normal */
2389         if (is_zero_v3(an))
2390                 copy_v3_v3(an, out_flip);
2391
2392         normalize_v3(an);
2393
2394         /* for flatten center */
2395         if (count != 0) {
2396                 mul_v3_fl(fc, 1.0f / count);
2397         }
2398         else {
2399                 zero_v3(fc);
2400         }
2401 }
2402
2403 static void calc_sculpt_plane(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode, float an[3], float fc[3])
2404 {
2405         SculptSession *ss = ob->sculpt;
2406         Brush *brush = paint_brush(&sd->paint);
2407
2408         if (ss->cache->mirror_symmetry_pass == 0 &&
2409             ss->cache->radial_symmetry_pass == 0 &&
2410             (ss->cache->first_time || !(brush->flag & BRUSH_ORIGINAL_NORMAL)))
2411         {
2412                 switch (brush->sculpt_plane) {
2413                         case SCULPT_DISP_DIR_VIEW:
2414                                 ED_view3d_global_to_vector(ss->cache->vc->rv3d, ss->cache->vc->rv3d->twmat[3], an);
2415                                 break;
2416
2417                         case SCULPT_DISP_DIR_X:
2418                                 an[1] = 0.0;
2419                                 an[2] = 0.0;
2420                                 an[0] = 1.0;
2421                                 break;
2422
2423                         case SCULPT_DISP_DIR_Y:
2424                                 an[0] = 0.0;
2425                                 an[2] = 0.0;
2426                                 an[1] = 1.0;
2427                                 break;
2428
2429                         case SCULPT_DISP_DIR_Z:
2430                                 an[0] = 0.0;
2431                                 an[1] = 0.0;
2432                                 an[2] = 1.0;
2433                                 break;
2434
2435                         case SCULPT_DISP_DIR_AREA:
2436                                 calc_area_normal_and_flatten_center(sd, ob, nodes, totnode, an, fc);
2437
2438                         default:
2439                                 break;
2440                 }
2441
2442                 /* for flatten center */
2443                 /* flatten center has not been calculated yet if we are not using the area normal */
2444                 if (brush->sculpt_plane != SCULPT_DISP_DIR_AREA)
2445                         calc_flatten_center(sd, ob, nodes, totnode, fc);
2446
2447                 /* for area normal */
2448                 copy_v3_v3(ss->cache->sculpt_normal, an);
2449
2450                 /* for flatten center */
2451                 copy_v3_v3(ss->cache->last_center, fc);
2452         }
2453         else {
2454                 /* for area normal */
2455                 copy_v3_v3(an, ss->cache->sculpt_normal);
2456
2457                 /* for flatten center */
2458                 copy_v3_v3(fc, ss->cache->last_center);
2459
2460                 /* for area normal */
2461                 flip_v3(an, ss->cache->mirror_symmetry_pass);
2462
2463                 /* for flatten center */
2464                 flip_v3(fc, ss->cache->mirror_symmetry_pass);
2465
2466                 /* for area normal */
2467                 mul_m4_v3(ss->cache->symm_rot_mat, an);
2468
2469                 /* for flatten center */
2470                 mul_m4_v3(ss->cache->symm_rot_mat, fc);
2471         }
2472 }
2473
2474 /* Projects a point onto a plane along the plane's normal */
2475 static void point_plane_project(float intr[3], float co[3], float plane_normal[3], float plane_center[3])
2476 {
2477         sub_v3_v3v3(intr, co, plane_center);
2478         mul_v3_v3fl(intr, plane_normal, dot_v3v3(plane_normal, intr));
2479         sub_v3_v3v3(intr, co, intr);
2480 }
2481
2482 static int plane_trim(StrokeCache *cache, Brush *brush, float val[3])
2483 {
2484         return (!(brush->flag & BRUSH_PLANE_TRIM) ||
2485                 ((dot_v3v3(val, val) <= cache->radius_squared * cache->plane_trim_squared)));
2486 }
2487
2488 static int plane_point_side_flip(float co[3], float plane_normal[3], float plane_center[3], int flip)
2489 {
2490         float delta[3];
2491         float d;
2492
2493         sub_v3_v3v3(delta, co, plane_center);
2494         d = dot_v3v3(plane_normal, delta);
2495
2496         if (flip) d = -d;
2497
2498         return d <= 0.0f;
2499 }
2500
2501 static int plane_point_side(float co[3], float plane_normal[3], float plane_center[3])
2502 {
2503         float delta[3];
2504
2505         sub_v3_v3v3(delta, co, plane_center);
2506         return dot_v3v3(plane_normal, delta) <= 0.0f;
2507 }
2508
2509 static float get_offset(Sculpt *sd, SculptSession *ss)
2510 {
2511         Brush *brush = paint_brush(&sd->paint);
2512
2513         float rv = brush->plane_offset;
2514
2515         if (brush->flag & BRUSH_OFFSET_PRESSURE) {
2516                 rv *= ss->cache->pressure;
2517         }
2518
2519         return rv;
2520 }
2521
2522 static void do_flatten_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
2523 {
2524         SculptSession *ss = ob->sculpt;
2525         Brush *brush = paint_brush(&sd->paint);
2526
2527         float bstrength = ss->cache->bstrength;
2528         const float radius = ss->cache->radius;
2529
2530         float an[3];
2531         float fc[3];
2532
2533         float offset = get_offset(sd, ss);
2534
2535         float displace;
2536
2537         int n;
2538
2539         float temp[3];
2540
2541         calc_sculpt_plane(sd, ob, nodes, totnode, an, fc);
2542
2543         displace = radius * offset;
2544
2545         mul_v3_v3v3(temp, an, ss->cache->scale);
2546         mul_v3_fl(temp, displace);
2547         add_v3_v3(fc, temp);
2548
2549         #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
2550         for (n = 0; n < totnode; n++) {
2551                 PBVHVertexIter vd;
2552                 SculptBrushTest test;
2553                 float (*proxy)[3];
2554
2555                 proxy = BKE_pbvh_node_add_proxy(ss->pbvh, nodes[n])->co;
2556
2557                 sculpt_brush_test_init(ss, &test);
2558
2559                 BKE_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE)
2560                 {
2561                         if (sculpt_brush_test_sq(&test, vd.co)) {
2562                                 float intr[3];
2563                                 float val[3];
2564
2565                                 point_plane_project(intr, vd.co, an, fc);
2566
2567                                 sub_v3_v3v3(val, intr, vd.co);
2568
2569                                 if (plane_trim(ss->cache, brush, val)) {
2570                                         const float fade = bstrength * tex_strength(ss, brush, vd.co, sqrt(test.dist),
2571                                                                                     an, vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f);
2572
2573                                         mul_v3_v3fl(proxy[vd.i], val, fade);
2574
2575                                         if (vd.mvert)
2576                                                 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
2577                                 }
2578                         }
2579                 }
2580                 BKE_pbvh_vertex_iter_end;
2581         }
2582 }
2583
2584 static void do_clay_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
2585 {
2586         SculptSession *ss = ob->sculpt;
2587         Brush *brush = paint_brush(&sd->paint);
2588
2589         float bstrength = ss->cache->bstrength;
2590         float radius    = ss->cache->radius;
2591         float offset    = get_offset(sd, ss);
2592         
2593         float displace;
2594
2595         float an[3];
2596         float fc[3];
2597
2598         int n;
2599
2600         float temp[3];
2601
2602         int flip;
2603
2604         calc_sculpt_plane(sd, ob, nodes, totnode, an, fc);
2605
2606         flip = bstrength < 0;
2607
2608         if (flip) {
2609                 bstrength = -bstrength;
2610                 radius    = -radius;
2611         }
2612
2613         displace = radius * (0.25f + offset);
2614
2615         mul_v3_v3v3(temp, an, ss->cache->scale);
2616         mul_v3_fl(temp, displace);
2617         add_v3_v3(fc, temp);
2618
2619         /* add_v3_v3v3(p, ss->cache->location, an); */
2620
2621         #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
2622         for (n = 0; n < totnode; n++) {
2623                 PBVHVertexIter vd;
2624                 SculptBrushTest test;
2625                 float (*proxy)[3];
2626
2627                 proxy = BKE_pbvh_node_add_proxy(ss->pbvh, nodes[n])->co;
2628
2629                 sculpt_brush_test_init(ss, &test);
2630
2631                 BKE_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE)
2632                 {
2633                         if (sculpt_brush_test_sq(&test, vd.co)) {
2634                                 if (plane_point_side_flip(vd.co, an, fc, flip)) {
2635                                         float intr[3];
2636                                         float val[3];
2637
2638                                         point_plane_project(intr, vd.co, an, fc);
2639
2640                                         sub_v3_v3v3(val, intr, vd.co);
2641
2642                                         if (plane_trim(ss->cache, brush, val)) {
2643                                                 const float fade = bstrength * tex_strength(ss, brush, vd.co,
2644                                                                                             sqrt(test.dist),
2645                                                                                             an, vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f);
2646
2647                                                 mul_v3_v3fl(proxy[vd.i], val, fade);
2648
2649                                                 if (vd.mvert)
2650                                                         vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
2651                                         }
2652                                 }
2653                         }
2654                 }
2655                 BKE_pbvh_vertex_iter_end;
2656         }
2657 }
2658
2659 static void do_clay_strips_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
2660 {
2661         SculptSession *ss = ob->sculpt;
2662         Brush *brush = paint_brush(&sd->paint);
2663
2664         float bstrength = ss->cache->bstrength;
2665         float radius    = ss->cache->radius;
2666         float offset    = get_offset(sd, ss);
2667         
2668         float displace;
2669
2670         float sn[3];
2671         float an[3];
2672         float fc[3];
2673
2674         int n;
2675
2676         float temp[3];
2677         float mat[4][4];
2678         float scale[4][4];
2679         float tmat[4][4];
2680
2681         int flip;
2682
2683         calc_sculpt_plane(sd, ob, nodes, totnode, sn, fc);
2684
2685         if (brush->sculpt_plane != SCULPT_DISP_DIR_AREA || (brush->flag & BRUSH_ORIGINAL_NORMAL))
2686                 calc_area_normal(sd, ob, an, nodes, totnode);
2687         else
2688                 copy_v3_v3(an, sn);
2689
2690         /* delay the first daub because grab delta is not setup */
2691         if (ss->cache->first_time)
2692                 return;
2693
2694         flip = bstrength < 0;
2695
2696         if (flip) {
2697                 bstrength = -bstrength;
2698                 radius    = -radius;
2699         }
2700
2701         displace = radius * (0.25f + offset);
2702
2703         mul_v3_v3v3(temp, sn, ss->cache->scale);
2704         mul_v3_fl(temp, displace);
2705         add_v3_v3(fc, temp);
2706
2707         /* init mat */
2708         cross_v3_v3v3(mat[0], an, ss->cache->grab_delta_symmetry);
2709         mat[0][3] = 0;
2710         cross_v3_v3v3(mat[1], an, mat[0]);
2711         mat[1][3] = 0;
2712         copy_v3_v3(mat[2], an);
2713         mat[2][3] = 0;
2714         copy_v3_v3(mat[3], ss->cache->location);
2715         mat[3][3] = 1;
2716         normalize_m4(mat);
2717
2718         /* scale mat */
2719         scale_m4_fl(scale, ss->cache->radius);
2720         mult_m4_m4m4(tmat, mat, scale);
2721         invert_m4_m4(mat, tmat);
2722
2723         #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
2724         for (n = 0; n < totnode; n++) {
2725                 PBVHVertexIter vd;
2726                 SculptBrushTest test;
2727                 float (*proxy)[3];
2728
2729                 proxy = BKE_pbvh_node_add_proxy(ss->pbvh, nodes[n])->co;
2730
2731                 sculpt_brush_test_init(ss, &test);
2732
2733                 BKE_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE)
2734                 {
2735                         if (sculpt_brush_test_cube(&test, vd.co, mat)) {
2736                                 if (plane_point_side_flip(vd.co, sn, fc, flip)) {
2737                                         float intr[3];
2738                                         float val[3];
2739
2740                                         point_plane_project(intr, vd.co, sn, fc);
2741
2742                                         sub_v3_v3v3(val, intr, vd.co);
2743
2744                                         if (plane_trim(ss->cache, brush, val)) {
2745                                                 const float fade = bstrength * tex_strength(ss, brush, vd.co,
2746                                                                                             ss->cache->radius * test.dist,
2747                                                                                             an, vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f);
2748
2749                                                 mul_v3_v3fl(proxy[vd.i], val, fade);
2750
2751                                                 if (vd.mvert)
2752                                                         vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
2753                                         }
2754                                 }
2755                         }
2756                 }
2757                 BKE_pbvh_vertex_iter_end;
2758         }
2759 }
2760
2761 static void do_fill_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
2762 {
2763         SculptSession *ss = ob->sculpt;
2764         Brush *brush = paint_brush(&sd->paint);
2765
2766         float bstrength = ss->cache->bstrength;
2767         const float radius = ss->cache->radius;
2768
2769         float an[3];
2770         float fc[3];
2771         float offset = get_offset(sd, ss);
2772
2773         float displace;
2774
2775         int n;
2776
2777         float temp[3];
2778
2779         calc_sculpt_plane(sd, ob, nodes, totnode, an, fc);
2780
2781         displace = radius * offset;
2782
2783         mul_v3_v3v3(temp, an, ss->cache->scale);
2784         mul_v3_fl(temp, displace);
2785         add_v3_v3(fc, temp);
2786
2787         #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
2788         for (n = 0; n < totnode; n++) {
2789                 PBVHVertexIter vd;
2790                 SculptBrushTest test;
2791                 float (*proxy)[3];
2792
2793                 proxy = BKE_pbvh_node_add_proxy(ss->pbvh, nodes[n])->co;
2794
2795                 sculpt_brush_test_init(ss, &test);
2796
2797                 BKE_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE)
2798                 {
2799                         if (sculpt_brush_test_sq(&test, vd.co)) {
2800                                 if (plane_point_side(vd.co, an, fc)) {
2801                                         float intr[3];
2802                                         float val[3];
2803
2804                                         point_plane_project(intr, vd.co, an, fc);
2805
2806                                         sub_v3_v3v3(val, intr, vd.co);
2807
2808                                         if (plane_trim(ss->cache, brush, val)) {
2809                                                 const float fade = bstrength * tex_strength(ss, brush, vd.co,
2810                                                                                             sqrt(test.dist),
2811                                                                                             an, vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f);
2812
2813                                                 mul_v3_v3fl(proxy[vd.i], val, fade);
2814
2815                                                 if (vd.mvert)
2816                                                         vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
2817                                         }
2818                                 }
2819                         }
2820                 }
2821                 BKE_pbvh_vertex_iter_end;
2822         }
2823 }
2824
2825 static void do_scrape_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
2826 {
2827         SculptSession *ss = ob->sculpt;
2828         Brush *brush = paint_brush(&sd->paint);
2829
2830         float bstrength = ss->cache->bstrength;
2831         const float radius = ss->cache->radius;
2832
2833         float an[3];
2834         float fc[3];
2835         float offset = get_offset(sd, ss);
2836
2837         float displace;
2838
2839         int n;
2840
2841         float temp[3];
2842
2843         calc_sculpt_plane(sd, ob, nodes, totnode, an, fc);
2844
2845         displace = -radius * offset;
2846
2847         mul_v3_v3v3(temp, an, ss->cache->scale);
2848         mul_v3_fl(temp, displace);
2849         add_v3_v3(fc, temp);
2850
2851         #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
2852         for (n = 0; n < totnode; n++) {
2853                 PBVHVertexIter vd;
2854                 SculptBrushTest test;
2855                 float (*proxy)[3];
2856
2857                 proxy = BKE_pbvh_node_add_proxy(ss->pbvh, nodes[n])->co;
2858
2859                 sculpt_brush_test_init(ss, &test);
2860
2861                 BKE_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE)
2862                 {
2863                         if (sculpt_brush_test_sq(&test, vd.co)) {
2864                                 if (!plane_point_side(vd.co, an, fc)) {
2865                                         float intr[3];
2866                                         float val[3];
2867
2868                                         point_plane_project(intr, vd.co, an, fc);
2869
2870                                         sub_v3_v3v3(val, intr, vd.co);
2871
2872                                         if (plane_trim(ss->cache, brush, val)) {
2873                                                 const float fade = bstrength * tex_strength(ss, brush, vd.co,
2874                                                                                             sqrt(test.dist),
2875                                                                                             an, vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f);
2876
2877                                                 mul_v3_v3fl(proxy[vd.i], val, fade);
2878
2879                                                 if (vd.mvert)
2880                                                         vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
2881                                         }
2882                                 }
2883                         }
2884                 }
2885                 BKE_pbvh_vertex_iter_end;
2886         }
2887 }
2888
2889 void sculpt_vertcos_to_key(Object *ob, KeyBlock *kb, float (*vertCos)[3])
2890 {
2891         Mesh *me = (Mesh *)ob->data;
2892         float (*ofs)[3] = NULL;
2893         int a, is_basis = 0;
2894         KeyBlock *currkey;
2895
2896         /* for relative keys editing of base should update other keys */
2897         if (me->key->type == KEY_RELATIVE)
2898                 for (currkey = me->key->block.first; currkey; currkey = currkey->next)
2899                         if (ob->shapenr - 1 == currkey->relative) {
2900                                 is_basis = 1;
2901                                 break;
2902                         }
2903
2904         if (is_basis) {
2905                 ofs = BKE_key_convert_to_vertcos(ob, kb);
2906
2907                 /* calculate key coord offsets (from previous location) */
2908                 for (a = 0; a < me->totvert; a++) {
2909                         sub_v3_v3v3(ofs[a], vertCos[a], ofs[a]);
2910                 }
2911
2912                 /* apply offsets on other keys */
2913                 currkey = me->key->block.first;
2914                 while (currkey) {
2915                         int apply_offset = ((currkey != kb) && (ob->shapenr - 1 == currkey->relative));
2916
2917                         if (apply_offset)
2918                                 BKE_key_convert_from_offset(ob, currkey, ofs);
2919
2920                         currkey = currkey->next;
2921                 }
2922
2923                 MEM_freeN(ofs);
2924         }
2925
2926         /* modifying of basis key should update mesh */
2927         if (kb == me->key->refkey) {
2928                 MVert *mvert = me->mvert;
2929
2930                 for (a = 0; a < me->totvert; a++, mvert++)
2931                         copy_v3_v3(mvert->co, vertCos[a]);
2932
2933                 BKE_mesh_calc_normals_mapping(me->mvert, me->totvert, me->mloop,
2934                                               me->mpoly, me->totloop, me->totpoly,
2935                                               NULL, NULL, 0, NULL, NULL);
2936         }
2937
2938         /* apply new coords on active key block */
2939         BKE_key_convert_from_vertcos(ob, kb, vertCos);
2940 }
2941
2942 /* Note: we do the topology update before any brush actions to avoid
2943  * issues with the proxies. The size of the proxy can't change, so
2944  * topology must be updated first. */
2945 static void sculpt_topology_update(Sculpt *sd, Object *ob, Brush *brush)
2946 {
2947         SculptSession *ss = ob->sculpt;
2948         SculptSearchSphereData data;
2949         PBVHNode **nodes = NULL;
2950         float radius;
2951         int n, totnode;
2952
2953         /* Build a list of all nodes that are potentially within the
2954          * brush's area of influence */
2955         data.ss = ss;
2956         data.sd = sd;
2957
2958         radius = ss->cache->radius * 1.25f;
2959
2960         data.radius_squared = radius * radius;
2961         data.original = ELEM4(brush->sculpt_tool,
2962                               SCULPT_TOOL_GRAB,
2963                               SCULPT_TOOL_ROTATE,
2964                               SCULPT_TOOL_THUMB,
2965                               SCULPT_TOOL_LAYER);
2966
2967         BKE_pbvh_search_gather(ss->pbvh, sculpt_search_sphere_cb, &data, &nodes, &totnode);
2968
2969         /* Only act if some verts are inside the brush area */
2970         if (totnode) {
2971                 PBVHTopologyUpdateMode mode = PBVH_Subdivide;
2972
2973                 if ((sd->flags & SCULPT_DYNTOPO_COLLAPSE) ||
2974                         (brush->sculpt_tool == SCULPT_TOOL_SIMPLIFY))
2975                 {
2976                         mode |= PBVH_Collapse;
2977                 }
2978
2979                 for (n = 0; n < totnode; n++) {
2980                         sculpt_undo_push_node(ob, nodes[n],
2981                                               brush->sculpt_tool == SCULPT_TOOL_MASK ?
2982                                               SCULPT_UNDO_MASK : SCULPT_UNDO_COORDS);
2983                         BKE_pbvh_node_mark_update(nodes[n]);
2984
2985                         if (BKE_pbvh_type(ss->pbvh) == PBVH_BMESH) {
2986                                 BKE_pbvh_node_mark_topology_update(nodes[n]);
2987                                 BKE_pbvh_bmesh_node_save_orig(nodes[n]);
2988                         }
2989                 }
2990
2991                 if (BKE_pbvh_type(ss->pbvh) == PBVH_BMESH) {
2992                         BKE_pbvh_bmesh_update_topology(ss->pbvh, mode,
2993                                                        ss->cache->location,
2994                                                        ss->cache->radius);
2995                 }
2996
2997                 MEM_freeN(nodes);
2998         }
2999 }
3000
3001 static void do_brush_action(Sculpt *sd, Object *ob, Brush *brush)
3002 {
3003         SculptSession *ss = ob->sculpt;
3004         SculptSearchSphereData data;
3005         PBVHNode **nodes = NULL;
3006         int n, totnode;
3007
3008         /* Build a list of all nodes that are potentially within the brush's area of influence */
3009         data.ss = ss;
3010         data.sd = sd;
3011         data.radius_squared = ss->cache->radius_squared;
3012         data.original = ELEM4(brush->sculpt_tool,
3013                               SCULPT_TOOL_GRAB,
3014                               SCULPT_TOOL_ROTATE,
3015                               SCULPT_TOOL_THUMB,
3016                               SCULPT_TOOL_LAYER);
3017         BKE_pbvh_search_gather(ss->pbvh, sculpt_search_sphere_cb, &data, &nodes, &totnode);
3018
3019         /* Only act if some verts are inside the brush area */
3020         if (totnode) {
3021                 #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
3022                 for (n = 0; n < totnode; n++) {
3023                         sculpt_undo_push_node(ob, nodes[n],
3024                                               brush->sculpt_tool == SCULPT_TOOL_MASK ?
3025                                               SCULPT_UNDO_MASK : SCULPT_UNDO_COORDS);
3026                         BKE_pbvh_node_mark_update(nodes[n]);
3027                 }
3028
3029                 if (brush_needs_sculpt_normal(brush))
3030                         update_sculpt_normal(sd, ob, nodes, totnode);
3031
3032                 if (brush->mtex.brush_map_mode == MTEX_MAP_MODE_AREA)
3033                         update_brush_local_mat(sd, ob);
3034
3035                 /* Apply one type of brush action */
3036                 switch (brush->sculpt_tool) {
3037                         case SCULPT_TOOL_DRAW:
3038                                 do_draw_brush(sd, ob, nodes, totnode);
3039                                 break;
3040                         case SCULPT_TOOL_SMOOTH:
3041                                 do_smooth_brush(sd, ob, nodes, totnode);
3042                                 break;
3043                         case SCULPT_TOOL_CREASE:
3044                                 do_crease_brush(sd, ob, nodes, totnode);
3045                                 break;
3046                         case SCULPT_TOOL_BLOB:
3047                                 do_crease_brush(sd, ob, nodes, totnode);
3048                                 break;
3049                         case SCULPT_TOOL_PINCH:
3050                                 do_pinch_brush(sd, ob, nodes, totnode);
3051                                 break;
3052                         case SCULPT_TOOL_INFLATE:
3053                                 do_inflate_brush(sd, ob, nodes, totnode);
3054                                 break;
3055                         case SCULPT_TOOL_GRAB:
3056                                 do_grab_brush(sd, ob, nodes, totnode);
3057                                 break;
3058                         case SCULPT_TOOL_ROTATE:
3059                                 do_rotate_brush(sd, ob, nodes, totnode);
3060                                 break;
3061                         case SCULPT_TOOL_SNAKE_HOOK:
3062                                 do_snake_hook_brush(sd, ob, nodes, totnode);
3063                                 break;
3064                         case SCULPT_TOOL_NUDGE:
3065                                 do_nudge_brush(sd, ob, nodes, totnode);
3066                                 break;
3067                         case SCULPT_TOOL_THUMB:
3068                                 do_thumb_brush(sd, ob, nodes, totnode);
3069                                 break;
3070                         case SCULPT_TOOL_LAYER:
3071                                 do_layer_brush(sd, ob, nodes, totnode);
3072                                 break;
3073                         case SCULPT_TOOL_FLATTEN:
3074                                 do_flatten_brush(sd, ob, nodes, totnode);
3075                                 break;
3076                         case SCULPT_TOOL_CLAY:
3077                                 do_clay_brush(sd, ob, nodes, totnode);
3078                                 break;
3079                         case SCULPT_TOOL_CLAY_STRIPS:
3080                                 do_clay_strips_brush(sd, ob, nodes, totnode);
3081                                 break;
3082                         case SCULPT_TOOL_FILL:
3083                                 do_fill_brush(sd, ob, nodes, totnode);
3084                                 break;
3085                         case SCULPT_TOOL_SCRAPE:
3086                                 do_scrape_brush(sd, ob, nodes, totnode);
3087                                 break;
3088                         case SCULPT_TOOL_MASK:
3089                                 do_mask_brush(sd, ob, nodes, totnode);
3090                                 break;
3091                 }
3092
3093                 if (!ELEM(brush->sculpt_tool, SCULPT_TOOL_SMOOTH, SCULPT_TOOL_MASK) &&
3094                     brush->autosmooth_factor > 0)
3095                 {
3096                         if (brush->flag & BRUSH_INVERSE_SMOOTH_PRESSURE) {
3097                                 smooth(sd, ob, nodes, totnode, brush->autosmooth_factor * (1 - ss->cache->pressure), FALSE);
3098                         }
3099                         else {
3100                                 smooth(sd, ob, nodes, totnode, brush->autosmooth_factor, FALSE);
3101                         }
3102                 }
3103
3104                 MEM_freeN(nodes);
3105         }
3106 }
3107
3108 /* flush displacement from deformed PBVH vertex to original mesh */
3109 static void sculpt_flush_pbvhvert_deform(Object *ob, PBVHVertexIter *vd)
3110 {
3111         SculptSession *ss = ob->sculpt;
3112         Mesh *me = ob->data;
3113         float disp[3], newco[3];
3114         int index = vd->vert_indices[vd->i];
3115
3116         sub_v3_v3v3(disp, vd->co, ss->deform_cos[index]);
3117         mul_m3_v3(ss->deform_imats[index], disp);
3118         add_v3_v3v3(newco, disp, ss->orig_cos[index]);
3119