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