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