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