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