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