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