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