Recreating my GSoC branch.
[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): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  *
29  * Implements the Sculpt Mode tools
30  *
31  */
32
33 #include "MEM_guardedalloc.h"
34
35 #include "BLI_math.h"
36 #include "BLI_blenlib.h"
37 #include "BLI_dynstr.h"
38 #include "BLI_ghash.h"
39 #include "BLI_pbvh.h"
40 #include "BLI_threads.h"
41
42 #include "DNA_key_types.h"
43 #include "DNA_mesh_types.h"
44 #include "DNA_meshdata_types.h"
45 #include "DNA_object_types.h"
46 #include "DNA_scene_types.h"
47
48 #include "BKE_brush.h"
49 #include "BKE_cdderivedmesh.h"
50 #include "BKE_context.h"
51 #include "BKE_customdata.h"
52 #include "BKE_DerivedMesh.h"
53 #include "BKE_depsgraph.h"
54 #include "BKE_global.h"
55 #include "BKE_image.h"
56 #include "BKE_key.h"
57 #include "BKE_library.h"
58 #include "BKE_main.h"
59 #include "BKE_mesh.h"
60 #include "BKE_modifier.h"
61 #include "BKE_multires.h"
62 #include "BKE_paint.h"
63 #include "BKE_report.h"
64 #include "BKE_texture.h"
65 #include "BKE_utildefines.h"
66 #include "BKE_colortools.h"
67
68 #include "BIF_gl.h"
69 #include "BIF_glutil.h"
70
71 #include "WM_api.h"
72 #include "WM_types.h"
73 #include "ED_screen.h"
74 #include "ED_sculpt.h"
75 #include "ED_view3d.h"
76 #include "paint_intern.h"
77 #include "sculpt_intern.h"
78
79 #include "RNA_access.h"
80 #include "RNA_define.h"
81
82
83 #include "RE_render_ext.h"
84
85 #include "gpu_buffers.h"
86
87 #include <math.h>
88 #include <stdlib.h>
89 #include <string.h>
90
91 /* Number of vertices to average in order to determine the flatten distance */
92 #define FLATTEN_SAMPLE_SIZE 10
93
94 /* ===== STRUCTS =====
95  *
96  */
97
98 typedef enum StrokeFlags {
99         CLIP_X = 1,
100         CLIP_Y = 2,
101         CLIP_Z = 4
102 } StrokeFlags;
103
104 /* Cache stroke properties. Used because
105    RNA property lookup isn't particularly fast.
106
107    For descriptions of these settings, check the operator properties.
108 */
109 typedef struct StrokeCache {
110         /* Invariants */
111         float initial_radius;
112         float scale[3];
113         int flag;
114         float clip_tolerance[3];
115         float initial_mouse[2];
116
117         /* Variants */
118         float radius;
119         float true_location[3];
120         float location[3];
121
122         float flip;
123         float pressure;
124         float mouse[2];
125         float bstrength;
126         float tex_mouse[2];
127
128         /* The rest is temporary storage that isn't saved as a property */
129
130         int first_time; /* Beginning of stroke may do some things special */
131
132         bglMats *mats;
133
134         /* Clean this up! */
135         ViewContext *vc;
136         Brush *brush;
137
138         float (*face_norms)[3]; /* Copy of the mesh faces' normals */
139         float rotation; /* Texture rotation (radians) for anchored and rake modes */
140         int pixel_radius, previous_pixel_radius;
141         float grab_active_location[8][3];
142         float grab_delta[3], grab_delta_symmetry[3];
143         float old_grab_location[3], orig_grab_location[3];
144         int symmetry; /* Symmetry index between 0 and 7 */
145         float view_normal[3], view_normal_symmetry[3];
146         int last_rake[2]; /* Last location of updating rake rotation */
147         int original;
148 } StrokeCache;
149
150 /* ===== OPENGL =====
151  *
152  * Simple functions to get data from the GL
153  */
154
155 /* Convert a point in model coordinates to 2D screen coordinates. */
156 static void projectf(bglMats *mats, const float v[3], float p[2])
157 {
158         double ux, uy, uz;
159
160         gluProject(v[0],v[1],v[2], mats->modelview, mats->projection,
161                    (GLint *)mats->viewport, &ux, &uy, &uz);
162         p[0]= ux;
163         p[1]= uy;
164 }
165
166 /*XXX: static void project(bglMats *mats, const float v[3], short p[2])
167 {
168         float f[2];
169         projectf(mats, v, f);
170
171         p[0]= f[0];
172         p[1]= f[1];
173 }
174 */
175
176 /*** BVH Tree ***/
177
178 /* Get a screen-space rectangle of the modified area */
179 int sculpt_get_redraw_rect(ARegion *ar, RegionView3D *rv3d,
180                                 Object *ob, rcti *rect)
181 {
182         PBVH *pbvh= ob->sculpt->pbvh;
183         float bb_min[3], bb_max[3], pmat[4][4];
184         int i, j, k;
185
186         view3d_get_object_project_mat(rv3d, ob, pmat);
187
188         if(!pbvh)
189                 return 0;
190
191         BLI_pbvh_redraw_BB(pbvh, bb_min, bb_max);
192
193         rect->xmin = rect->ymin = INT_MAX;
194         rect->xmax = rect->ymax = INT_MIN;
195
196         if(bb_min[0] > bb_max[0] || bb_min[1] > bb_max[1] || bb_min[2] > bb_max[2])
197                 return 0;
198
199         for(i = 0; i < 2; ++i) {
200                 for(j = 0; j < 2; ++j) {
201                         for(k = 0; k < 2; ++k) {
202                                 float vec[3], proj[2];
203                                 vec[0] = i ? bb_min[0] : bb_max[0];
204                                 vec[1] = j ? bb_min[1] : bb_max[1];
205                                 vec[2] = k ? bb_min[2] : bb_max[2];
206                                 view3d_project_float(ar, vec, proj, pmat);
207                                 rect->xmin = MIN2(rect->xmin, proj[0]);
208                                 rect->xmax = MAX2(rect->xmax, proj[0]);
209                                 rect->ymin = MIN2(rect->ymin, proj[1]);
210                                 rect->ymax = MAX2(rect->ymax, proj[1]);
211                         }
212                 }
213         }
214         
215         return rect->xmin < rect->xmax && rect->ymin < rect->ymax;
216 }
217
218 void sculpt_get_redraw_planes(float planes[4][4], ARegion *ar,
219                                   RegionView3D *rv3d, Object *ob)
220 {
221         PBVH *pbvh= ob->sculpt->pbvh;
222         BoundBox *bb = MEM_callocN(sizeof(BoundBox), "sculpt boundbox");
223         bglMats mats;
224         rcti rect;
225
226         view3d_get_transformation(ar, rv3d, ob, &mats);
227         sculpt_get_redraw_rect(ar, rv3d,ob, &rect);
228
229 #if 1
230         /* use some extra space just in case */
231         rect.xmin -= 2;
232         rect.xmax += 2;
233         rect.ymin -= 2;
234         rect.ymax += 2;
235 #else
236         /* it was doing this before, allows to redraw a smaller
237            part of the screen but also gives artifaces .. */
238         rect.xmin += 2;
239         rect.xmax -= 2;
240         rect.ymin += 2;
241         rect.ymax -= 2;
242 #endif
243
244         view3d_calculate_clipping(bb, planes, &mats, &rect);
245         mul_m4_fl(planes, -1.0f);
246
247         MEM_freeN(bb);
248
249         /* clear redraw flag from nodes */
250         if(pbvh)
251                 BLI_pbvh_update(pbvh, PBVH_UpdateRedraw, NULL);
252 }
253
254 /************************** Undo *************************/
255
256 typedef struct SculptUndoNode {
257         struct SculptUndoNode *next, *prev;
258
259         char idname[MAX_ID_NAME];       /* name instead of pointer*/
260         void *node;                                     /* only during push, not valid afterwards! */
261
262         float (*co)[3];
263         short (*no)[3];
264         int totvert;
265
266         /* non-multires */
267         int maxvert;                            /* to verify if totvert it still the same */
268         int *index;                                     /* to restore into right location */
269
270         /* multires */
271         int maxgrid;                            /* same for grid */
272         int gridsize;                           /* same for grid */
273         int totgrid;                            /* to restore into right location */
274         int *grids;                                     /* to restore into right location */
275
276         /* layer brush */
277         float *layer_disp;
278 } SculptUndoNode;
279
280 static void update_cb(PBVHNode *node, void *data)
281 {
282         BLI_pbvh_node_mark_update(node);
283 }
284
285 /* Checks whether full update mode (slower) needs to be used to work with modifiers */
286 static int sculpt_modifiers_active(Scene *scene, Object *ob)
287 {
288         ModifierData *md;
289         MultiresModifierData *mmd = sculpt_multires_active(scene, ob);
290
291         /* check if there are any modifiers after what we are sculpting,
292            for a multires modifier with a deform modifier in front, we
293            do no need to recalculate the modifier stack. note that this
294            needs to be in sync with ccgDM_use_grid_pbvh! */
295         if(mmd)
296                 md= mmd->modifier.next;
297         else
298                 md= modifiers_getVirtualModifierList(ob);
299         
300         /* exception for shape keys because we can edit those */
301         for(; md; md= md->next) {
302                 if(modifier_isEnabled(scene, md, eModifierMode_Realtime))
303                         if(md->type != eModifierType_ShapeKey)
304                                 return 1;
305         }
306         
307         return 0;
308 }
309
310 static void sculpt_undo_restore(bContext *C, ListBase *lb)
311 {
312         Scene *scene = CTX_data_scene(C);
313         Object *ob = CTX_data_active_object(C);
314         DerivedMesh *dm = mesh_get_derived_final(scene, ob, 0);
315         SculptSession *ss = ob->sculpt;
316         SculptUndoNode *unode;
317         MVert *mvert;
318         MultiresModifierData *mmd;
319         int *index;
320         int i, j, update= 0;
321
322         sculpt_update_mesh_elements(scene, ob, 0);
323
324         for(unode=lb->first; unode; unode=unode->next) {
325                 if(!(strcmp(unode->idname, ob->id.name)==0))
326                         continue;
327
328                 if(unode->maxvert) {
329                         /* regular mesh restore */
330                         if(ss->totvert != unode->maxvert)
331                                 continue;
332
333                         index= unode->index;
334                         mvert= ss->mvert;
335
336                         for(i=0; i<unode->totvert; i++) {
337                                 swap_v3_v3(mvert[index[i]].co, unode->co[i]);
338                                 mvert[index[i]].flag |= ME_VERT_PBVH_UPDATE;
339                         }
340                 }
341                 else if(unode->maxgrid && dm->getGridData) {
342                         /* multires restore */
343                         DMGridData **grids, *grid;
344                         float (*co)[3];
345                         int gridsize;
346
347                         if(dm->getNumGrids(dm) != unode->maxgrid)
348                                 continue;
349                         if(dm->getGridSize(dm) != unode->gridsize)
350                                 continue;
351
352                         grids= dm->getGridData(dm);
353                         gridsize= dm->getGridSize(dm);
354
355                         co = unode->co;
356                         for(j=0; j<unode->totgrid; j++) {
357                                 grid= grids[unode->grids[j]];
358
359                                 for(i=0; i<gridsize*gridsize; i++, co++)
360                                         swap_v3_v3(grid[i].co, co[0]);
361                         }
362                 }
363
364                 update= 1;
365         }
366
367         if(update) {
368                 if(ss->kb) sculpt_mesh_to_key(ss->ob, ss->kb);
369                 if(ss->refkb) sculpt_key_to_mesh(ss->refkb, ob);
370
371                 /* we update all nodes still, should be more clever, but also
372                    needs to work correct when exiting/entering sculpt mode and
373                    the nodes get recreated, though in that case it could do all */
374                 BLI_pbvh_search_callback(ss->pbvh, NULL, NULL, update_cb, NULL);
375                 BLI_pbvh_update(ss->pbvh, PBVH_UpdateBB|PBVH_UpdateOriginalBB|PBVH_UpdateRedraw, NULL);
376
377                 if((mmd=sculpt_multires_active(scene, ob)))
378                         multires_mark_as_modified(ob);
379
380                 if(sculpt_modifiers_active(scene, ob))
381                         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
382         }
383 }
384
385 static void sculpt_undo_free(ListBase *lb)
386 {
387         SculptUndoNode *unode;
388
389         for(unode=lb->first; unode; unode=unode->next) {
390                 if(unode->co)
391                         MEM_freeN(unode->co);
392                 if(unode->no)
393                         MEM_freeN(unode->no);
394                 if(unode->index)
395                         MEM_freeN(unode->index);
396                 if(unode->grids)
397                         MEM_freeN(unode->grids);
398                 if(unode->layer_disp)
399                         MEM_freeN(unode->layer_disp);
400         }
401 }
402
403 static SculptUndoNode *sculpt_undo_get_node(SculptSession *ss, PBVHNode *node)
404 {
405         ListBase *lb= undo_paint_push_get_list(UNDO_PAINT_MESH);
406         SculptUndoNode *unode;
407
408         if(!lb)
409                 return NULL;
410
411         for(unode=lb->first; unode; unode=unode->next)
412                 if(unode->node == node)
413                         return unode;
414
415         return NULL;
416 }
417
418 static SculptUndoNode *sculpt_undo_push_node(SculptSession *ss, PBVHNode *node)
419 {
420         ListBase *lb= undo_paint_push_get_list(UNDO_PAINT_MESH);
421         Object *ob= ss->ob;
422         SculptUndoNode *unode;
423         int totvert, allvert, totgrid, maxgrid, gridsize, *grids;
424
425         /* list is manipulated by multiple threads, so we lock */
426         BLI_lock_thread(LOCK_CUSTOM1);
427
428         if((unode= sculpt_undo_get_node(ss, node))) {
429                 BLI_unlock_thread(LOCK_CUSTOM1);
430                 return unode;
431         }
432
433         unode= MEM_callocN(sizeof(SculptUndoNode), "SculptUndoNode");
434         strcpy(unode->idname, ob->id.name);
435         unode->node= node;
436
437         BLI_pbvh_node_num_verts(ss->pbvh, node, &totvert, &allvert);
438         BLI_pbvh_node_get_grids(ss->pbvh, node, &grids, &totgrid,
439                 &maxgrid, &gridsize, NULL, NULL);
440
441         unode->totvert= totvert;
442         /* we will use this while sculpting, is mapalloc slow to access then? */
443         unode->co= MEM_mapallocN(sizeof(float)*3*allvert, "SculptUndoNode.co");
444         unode->no= MEM_mapallocN(sizeof(short)*3*allvert, "SculptUndoNode.no");
445         undo_paint_push_count_alloc(UNDO_PAINT_MESH, (sizeof(float)*3 + sizeof(short)*3 + sizeof(int))*allvert);
446         BLI_addtail(lb, unode);
447
448         if(maxgrid) {
449                 /* multires */
450                 unode->maxgrid= maxgrid;
451                 unode->totgrid= totgrid;
452                 unode->gridsize= gridsize;
453                 unode->grids= MEM_mapallocN(sizeof(int)*totgrid, "SculptUndoNode.grids");
454         }
455         else {
456                 /* regular mesh */
457                 unode->maxvert= ss->totvert;
458                 unode->index= MEM_mapallocN(sizeof(int)*allvert, "SculptUndoNode.index");
459         }
460
461         BLI_unlock_thread(LOCK_CUSTOM1);
462
463         /* copy threaded, hopefully this is the performance critical part */
464         {
465                 PBVHVertexIter vd;
466
467                 BLI_pbvh_vertex_iter_begin(ss->pbvh, node, vd, PBVH_ITER_ALL) {
468                         copy_v3_v3(unode->co[vd.i], vd.co);
469                         if(vd.no) VECCOPY(unode->no[vd.i], vd.no)
470                         else normal_float_to_short_v3(unode->no[vd.i], vd.fno);
471                         if(vd.vert_indices) unode->index[vd.i]= vd.vert_indices[vd.i];
472                 }
473                 BLI_pbvh_vertex_iter_end;
474         }
475
476         if(unode->grids)
477                 memcpy(unode->grids, grids, sizeof(int)*totgrid);
478         
479         return unode;
480 }
481
482 static void sculpt_undo_push_begin(SculptSession *ss, char *name)
483 {
484         undo_paint_push_begin(UNDO_PAINT_MESH, name,
485                 sculpt_undo_restore, sculpt_undo_free);
486 }
487
488 static void sculpt_undo_push_end(SculptSession *ss)
489 {
490         ListBase *lb= undo_paint_push_get_list(UNDO_PAINT_MESH);
491         SculptUndoNode *unode;
492
493         /* we don't need normals in the undo stack */
494         for(unode=lb->first; unode; unode=unode->next) {
495                 if(unode->no) {
496                         MEM_freeN(unode->no);
497                         unode->no= NULL;
498                 }
499
500                 if(unode->layer_disp) {
501                         MEM_freeN(unode->layer_disp);
502                         unode->layer_disp= NULL;
503                 }
504         }
505
506         undo_paint_push_end(UNDO_PAINT_MESH);
507 }
508
509 void ED_sculpt_force_update(bContext *C)
510 {
511         Object *ob= CTX_data_active_object(C);
512
513         if(ob && (ob->mode & OB_MODE_SCULPT))
514                 multires_force_update(ob);
515 }
516
517 /************************ Brush Testing *******************/
518
519 typedef struct SculptBrushTest {
520         float radius_squared;
521         float location[3];
522
523         float dist;
524 } SculptBrushTest;
525
526 static void sculpt_brush_test_init(SculptSession *ss, SculptBrushTest *test)
527 {
528         test->radius_squared= ss->cache->radius*ss->cache->radius;
529         copy_v3_v3(test->location, ss->cache->location);
530 }
531
532 static int sculpt_brush_test(SculptBrushTest *test, float co[3])
533 {
534         float distsq, delta[3];
535
536         sub_v3_v3v3(delta, co, test->location);
537         distsq = INPR(delta, delta);
538
539         if(distsq < test->radius_squared) {
540                 test->dist = sqrt(distsq);
541                 return 1;
542         }
543
544         return 0;
545 }
546
547 /* ===== Sculpting =====
548  *
549  */
550
551 /* Return modified brush strength. Includes the direction of the brush, positive
552    values pull vertices, negative values push. Uses tablet pressure and a
553    special multiplier found experimentally to scale the strength factor. */
554 static float brush_strength(Sculpt *sd, StrokeCache *cache)
555 {
556         Brush *brush = paint_brush(&sd->paint);
557         /* Primary strength input; square it to make lower values more sensitive */
558         float alpha = brush->alpha * brush->alpha;
559
560         float dir= brush->flag & BRUSH_DIR_IN ? -1 : 1;
561         float pressure= 1;
562         float flip= cache->flip ? -1:1;
563
564         if(brush->flag & BRUSH_ALPHA_PRESSURE)
565                 pressure *= cache->pressure;
566         
567         switch(brush->sculpt_tool){
568         case SCULPT_TOOL_DRAW:
569         case SCULPT_TOOL_INFLATE:
570         case SCULPT_TOOL_CLAY:
571         case SCULPT_TOOL_FLATTEN:
572         case SCULPT_TOOL_LAYER:
573                 return alpha * dir * pressure * flip; /*XXX: not sure why? was multiplied by G.vd->grid */;
574         case SCULPT_TOOL_SMOOTH:
575                 return alpha * 4 * pressure;
576         case SCULPT_TOOL_PINCH:
577                 return alpha / 2 * dir * pressure * flip;
578         case SCULPT_TOOL_GRAB:
579                 return 1;
580         default:
581                 return 0;
582         }
583 }
584
585 /* Uses symm to selectively flip any axis of a coordinate. */
586 static void flip_coord(float out[3], float in[3], const char symm)
587 {
588         if(symm & SCULPT_SYMM_X)
589                 out[0]= -in[0];
590         else
591                 out[0]= in[0];
592         if(symm & SCULPT_SYMM_Y)
593                 out[1]= -in[1];
594         else
595                 out[1]= in[1];
596         if(symm & SCULPT_SYMM_Z)
597                 out[2]= -in[2];
598         else
599                 out[2]= in[2];
600 }
601
602 /* Get a pixel from the texcache at (px, py) */
603 static unsigned char get_texcache_pixel(const SculptSession *ss, int px, int py)
604 {
605         unsigned *p;
606         p = ss->texcache + py * ss->texcache_side + px;
607         return ((unsigned char*)(p))[0];
608 }
609
610 static float get_texcache_pixel_bilinear(const SculptSession *ss, float u, float v)
611 {
612         int x, y, x2, y2;
613         const int tc_max = ss->texcache_side - 1;
614         float urat, vrat, uopp;
615
616         if(u < 0) u = 0;
617         else if(u >= ss->texcache_side) u = tc_max;
618         if(v < 0) v = 0;
619         else if(v >= ss->texcache_side) v = tc_max;
620
621         x = floor(u);
622         y = floor(v);
623         x2 = x + 1;
624         y2 = y + 1;
625
626         if(x2 > ss->texcache_side) x2 = tc_max;
627         if(y2 > ss->texcache_side) y2 = tc_max;
628         
629         urat = u - x;
630         vrat = v - y;
631         uopp = 1 - urat;
632                 
633         return ((get_texcache_pixel(ss, x, y) * uopp +
634                  get_texcache_pixel(ss, x2, y) * urat) * (1 - vrat) + 
635                 (get_texcache_pixel(ss, x, y2) * uopp +
636                  get_texcache_pixel(ss, x2, y2) * urat) * vrat) / 255.0;
637 }
638
639 /* Return a multiplier for brush strength on a particular vertex. */
640 static float tex_strength(SculptSession *ss, Brush *br, float *point, const float len)
641 {
642         MTex *tex = &br->mtex;
643         float avg= 1;
644
645         if(!tex) {
646                 avg= 1;
647         }
648         else if(tex->brush_map_mode == MTEX_MAP_MODE_3D) {
649                 float jnk;
650
651                 /* Get strength by feeding the vertex 
652                    location directly into a texture */
653                 externtex(tex, point, &avg,
654                           &jnk, &jnk, &jnk, &jnk);
655         }
656         else if(ss->texcache) {
657                 const float bsize= ss->cache->pixel_radius * 2;
658                 const float rot= tex->rot + ss->cache->rotation;
659                 int px, py;
660                 float flip[3], point_2d[2];
661
662                 /* If the active area is being applied for symmetry, flip it
663                    across the symmetry axis in order to project it. This insures
664                    that the brush texture will be oriented correctly. */
665                 copy_v3_v3(flip, point);
666                 flip_coord(flip, flip, ss->cache->symmetry);
667                 projectf(ss->cache->mats, flip, point_2d);
668
669                 /* For Tile and Drag modes, get the 2D screen coordinates of the
670                    and scale them up or down to the texture size. */
671                 if(tex->brush_map_mode == MTEX_MAP_MODE_TILED) {
672                         const int sx= (const int)tex->size[0];
673                         const int sy= (const int)tex->size[1];
674                         
675                         float fx= point_2d[0];
676                         float fy= point_2d[1];
677                         
678                         float angle= atan2(fy, fx) - rot;
679                         float flen= sqrtf(fx*fx + fy*fy);
680                         
681                         if(rot<0.001 && rot>-0.001) {
682                                 px= point_2d[0];
683                                 py= point_2d[1];
684                         } else {
685                                 px= flen * cos(angle) + 2000;
686                                 py= flen * sin(angle) + 2000;
687                         }
688                         if(sx != 1)
689                                 px %= sx-1;
690                         if(sy != 1)
691                                 py %= sy-1;
692                         avg= get_texcache_pixel_bilinear(ss, ss->texcache_side*px/sx, ss->texcache_side*py/sy);
693                 }
694                 else if(tex->brush_map_mode == MTEX_MAP_MODE_FIXED) {
695                         float fx= (point_2d[0] - ss->cache->tex_mouse[0]) / bsize;
696                         float fy= (point_2d[1] - ss->cache->tex_mouse[1]) / bsize;
697
698                         float angle= atan2(fy, fx) - rot;
699                         float flen= sqrtf(fx*fx + fy*fy);
700                         
701                         fx = flen * cos(angle) + 0.5;
702                         fy = flen * sin(angle) + 0.5;
703
704                         avg= get_texcache_pixel_bilinear(ss, fx * ss->texcache_side, fy * ss->texcache_side);
705                 }
706         }
707
708         avg*= brush_curve_strength(br, len, ss->cache->radius); /* Falloff curve */
709
710         return avg;
711 }
712
713 typedef struct {
714         Sculpt *sd;
715         SculptSession *ss;
716         float radius_squared;
717         int original;
718 } SculptSearchSphereData;
719
720 /* Test AABB against sphere */
721 static int sculpt_search_sphere_cb(PBVHNode *node, void *data_v)
722 {
723         SculptSearchSphereData *data = data_v;
724         float *center = data->ss->cache->location, nearest[3];
725         float t[3], bb_min[3], bb_max[3];
726         int i;
727
728         if(data->original)
729                 BLI_pbvh_node_get_original_BB(node, bb_min, bb_max);
730         else
731                 BLI_pbvh_node_get_BB(node, bb_min, bb_max);
732         
733         for(i = 0; i < 3; ++i) {
734                 if(bb_min[i] > center[i])
735                         nearest[i] = bb_min[i];
736                 else if(bb_max[i] < center[i])
737                         nearest[i] = bb_max[i];
738                 else
739                         nearest[i] = center[i]; 
740         }
741         
742         sub_v3_v3v3(t, center, nearest);
743
744         return t[0] * t[0] + t[1] * t[1] + t[2] * t[2] < data->radius_squared;
745 }
746
747 /* Handles clipping against a mirror modifier and SCULPT_LOCK axis flags */
748 static void sculpt_clip(Sculpt *sd, SculptSession *ss, float *co, const float val[3])
749 {
750         int i;
751
752         for(i=0; i<3; ++i) {
753                 if(sd->flags & (SCULPT_LOCK_X << i))
754                         continue;
755
756                 if((ss->cache->flag & (CLIP_X << i)) && (fabs(co[i]) <= ss->cache->clip_tolerance[i]))
757                         co[i]= 0.0f;
758                 else
759                         co[i]= val[i];
760         }               
761 }
762
763 static void add_norm_if(float view_vec[3], float out[3], float out_flip[3], float fno[3])
764 {
765         if((dot_v3v3(view_vec, fno)) > 0) {
766                 add_v3_v3(out, fno);
767         } else {
768                 add_v3_v3(out_flip, fno); /* out_flip is used when out is {0,0,0} */
769         }
770 }
771
772 /* For draw/layer/flatten; finds average normal for all active vertices */
773 static void calc_area_normal(Sculpt *sd, SculptSession *ss, float area_normal[3], PBVHNode **nodes, int totnode)
774 {
775         PBVH *bvh= ss->pbvh;
776         StrokeCache *cache = ss->cache;
777         const int view = 0; /* XXX: should probably be a flag, not number: brush_type==SCULPT_TOOL_DRAW ? sculptmode_brush()->view : 0; */
778         float out[3] = {0.0f, 0.0f, 0.0f};
779         float out_flip[3] = {0.0f, 0.0f, 0.0f};
780         float out_dir[3];
781         int n;
782
783         copy_v3_v3(out_dir, cache->view_normal_symmetry);
784
785         /* threaded loop over nodes */
786         //#pragma omp parallel for private(n) schedule(static)
787         for(n=0; n<totnode; n++) {
788                 PBVHVertexIter vd;
789                 SculptBrushTest test;
790                 SculptUndoNode *unode;
791                 float fno[3];
792                 float nout[3] = {0.0f, 0.0f, 0.0f};
793                 float nout_flip[3] = {0.0f, 0.0f, 0.0f};
794                 
795                 // XXX push instead of get for thread safety in draw
796                 // brush .. lame, but also not harmful really
797                 unode= sculpt_undo_push_node(ss, nodes[n]);
798                 sculpt_brush_test_init(ss, &test);
799
800                 if(ss->cache->original) {
801                         BLI_pbvh_vertex_iter_begin(bvh, nodes[n], vd, PBVH_ITER_UNIQUE) {
802                                 if(sculpt_brush_test(&test, unode->co[vd.i])) {
803                                         normal_short_to_float_v3(fno, unode->no[vd.i]);
804                                         add_norm_if(out_dir, nout, nout_flip, fno);
805                                 }
806                         }
807                         BLI_pbvh_vertex_iter_end;
808                 }
809                 else {
810                         BLI_pbvh_vertex_iter_begin(bvh, nodes[n], vd, PBVH_ITER_UNIQUE) {
811                                 if(sculpt_brush_test(&test, vd.co)) {
812                                         if(vd.no) {
813                                                 normal_short_to_float_v3(fno, vd.no);
814                                                 add_norm_if(out_dir, nout, nout_flip, fno);
815                                         }
816                                         else
817                                                 add_norm_if(out_dir, nout, nout_flip, vd.fno);
818                                 }
819                         }
820                         BLI_pbvh_vertex_iter_end;
821                 }
822
823                 //#pragma omp critical
824                 {
825                         /* we sum per node and add together later for threads */
826                         add_v3_v3(out, nout);
827                         add_v3_v3(out_flip, nout_flip);
828                 }
829         }
830
831         if (out[0]==0.0 && out[1]==0.0 && out[2]==0.0) {
832                 copy_v3_v3(out, out_flip);
833         }
834         
835         normalize_v3(out);
836
837         out[0] = out_dir[0] * view + out[0] * (10-view);
838         out[1] = out_dir[1] * view + out[1] * (10-view);
839         out[2] = out_dir[2] * view + out[2] * (10-view);
840         
841         normalize_v3(out);
842         copy_v3_v3(area_normal, out);
843 }
844
845 static void do_draw_brush(Sculpt *sd, SculptSession *ss, PBVHNode **nodes, int totnode)
846 {
847         Brush *brush = paint_brush(&sd->paint);
848         float offset[3], area_normal[3];
849         float bstrength= ss->cache->bstrength;
850         int n;
851
852         /* area normal */
853         calc_area_normal(sd, ss, area_normal, nodes, totnode);
854
855         /* offset with as much as possible factored in already */
856         offset[0]= area_normal[0]*ss->cache->radius*ss->cache->scale[0]*bstrength;
857         offset[1]= area_normal[1]*ss->cache->radius*ss->cache->scale[1]*bstrength;
858         offset[2]= area_normal[2]*ss->cache->radius*ss->cache->scale[2]*bstrength;
859
860         /* threaded loop over nodes */
861         //#pragma omp parallel for private(n) schedule(static)
862         for(n=0; n<totnode; n++) {
863                 PBVHVertexIter vd;
864                 SculptBrushTest test;
865                 
866                 sculpt_undo_push_node(ss, nodes[n]);
867                 sculpt_brush_test_init(ss, &test);
868
869                 BLI_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE) {
870                         if(sculpt_brush_test(&test, vd.co)) {
871                                 /* offset vertex */
872                                 float fade = tex_strength(ss, brush, vd.co, test.dist);
873                                 float val[3]= {vd.co[0] + offset[0]*fade,
874                                                            vd.co[1] + offset[1]*fade,
875                                                            vd.co[2] + offset[2]*fade};
876
877                                 sculpt_clip(sd, ss, vd.co, val);
878                                 if(vd.mvert) vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
879                         }
880                 }
881                 BLI_pbvh_vertex_iter_end;
882
883                 BLI_pbvh_node_mark_update(nodes[n]);
884         }
885 }
886
887 /* For the smooth brush, uses the neighboring vertices around vert to calculate
888    a smoothed location for vert. Skips corner vertices (used by only one
889    polygon.) */
890 static void neighbor_average(SculptSession *ss, float avg[3], const int vert)
891 {
892         int i, skip= -1, total=0;
893         IndexNode *node= ss->fmap[vert].first;
894         char ncount= BLI_countlist(&ss->fmap[vert]);
895         MFace *f;
896
897         avg[0] = avg[1] = avg[2] = 0;
898                 
899         /* Don't modify corner vertices */
900         if(ncount==1) {
901                 copy_v3_v3(avg, ss->mvert[vert].co);
902                 return;
903         }
904
905         while(node){
906                 f= &ss->mface[node->index];
907                 
908                 if(f->v4) {
909                         skip= (f->v1==vert?2:
910                                    f->v2==vert?3:
911                                    f->v3==vert?0:
912                                    f->v4==vert?1:-1);
913                 }
914
915                 for(i=0; i<(f->v4?4:3); ++i) {
916                         if(i != skip && (ncount!=2 || BLI_countlist(&ss->fmap[(&f->v1)[i]]) <= 2)) {
917                                 add_v3_v3(avg, ss->mvert[(&f->v1)[i]].co);
918                                 ++total;
919                         }
920                 }
921
922                 node= node->next;
923         }
924
925         if(total>0)
926                 mul_v3_fl(avg, 1.0f / total);
927         else
928                 copy_v3_v3(avg, ss->mvert[vert].co);
929 }
930
931 static void do_mesh_smooth_brush(Sculpt *sd, SculptSession *ss, PBVHNode *node)
932 {
933         Brush *brush = paint_brush(&sd->paint);
934         float bstrength= ss->cache->bstrength;
935         PBVHVertexIter vd;
936         SculptBrushTest test;
937         
938         sculpt_brush_test_init(ss, &test);
939
940         BLI_pbvh_vertex_iter_begin(ss->pbvh, node, vd, PBVH_ITER_UNIQUE) {
941                 if(sculpt_brush_test(&test, vd.co)) {
942                         float fade = tex_strength(ss, brush, vd.co, test.dist)*bstrength;
943                         float avg[3], val[3];
944
945                         CLAMP(fade, 0.0f, 1.0f);
946                         
947                         neighbor_average(ss, avg, vd.vert_indices[vd.i]);
948                         val[0] = vd.co[0]+(avg[0]-vd.co[0])*fade;
949                         val[1] = vd.co[1]+(avg[1]-vd.co[1])*fade;
950                         val[2] = vd.co[2]+(avg[2]-vd.co[2])*fade;
951                         
952                         sculpt_clip(sd, ss, vd.co, val);                        
953                         if(vd.mvert) vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
954                 }
955         }
956         BLI_pbvh_vertex_iter_end;
957 }
958
959 static void do_multires_smooth_brush(Sculpt *sd, SculptSession *ss, PBVHNode *node)
960 {
961         Brush *brush = paint_brush(&sd->paint);
962         SculptBrushTest test;
963         DMGridData **griddata, *data;
964         DMGridAdjacency *gridadj, *adj;
965         float bstrength= ss->cache->bstrength;
966         float co[3], (*tmpgrid)[3];
967         int v1, v2, v3, v4;
968         int *grid_indices, totgrid, gridsize, i, x, y;
969                         
970         sculpt_brush_test_init(ss, &test);
971
972         BLI_pbvh_node_get_grids(ss->pbvh, node, &grid_indices, &totgrid,
973                 NULL, &gridsize, &griddata, &gridadj);
974
975         //#pragma omp critical
976         tmpgrid= MEM_mallocN(sizeof(float)*3*gridsize*gridsize, "tmpgrid");
977
978         for(i = 0; i < totgrid; ++i) {
979                 data = griddata[grid_indices[i]];
980                 adj = &gridadj[grid_indices[i]];
981
982                 memset(tmpgrid, 0, sizeof(float)*3*gridsize*gridsize);
983
984                 /* average grid values */
985                 for(y = 0; y < gridsize-1; ++y)  {
986                         for(x = 0; x < gridsize-1; ++x)  {
987                                 v1 = x + y*gridsize;
988                                 v2 = (x + 1) + y*gridsize;
989                                 v3 = (x + 1) + (y + 1)*gridsize;
990                                 v4 = x + (y + 1)*gridsize;
991
992                                 cent_quad_v3(co, data[v1].co, data[v2].co, data[v3].co, data[v4].co);
993                                 mul_v3_fl(co, 0.25f);
994
995                                 add_v3_v3(tmpgrid[v1], co);
996                                 add_v3_v3(tmpgrid[v2], co);
997                                 add_v3_v3(tmpgrid[v3], co);
998                                 add_v3_v3(tmpgrid[v4], co);
999                         }
1000                 }
1001
1002                 /* blend with existing coordinates */
1003                 for(y = 0; y < gridsize; ++y)  {
1004                         for(x = 0; x < gridsize; ++x)  {
1005                                 if(x == 0 && adj->index[0] == -1) continue;
1006                                 if(x == gridsize - 1 && adj->index[2] == -1) continue;
1007                                 if(y == 0 && adj->index[3] == -1) continue;
1008                                 if(y == gridsize - 1 && adj->index[1] == -1) continue;
1009
1010                                 copy_v3_v3(co, data[x + y*gridsize].co);
1011
1012                                 if(sculpt_brush_test(&test, co)) {
1013                                         float fade = tex_strength(ss, brush, co, test.dist)*bstrength;
1014                                         float avg[3], val[3];
1015
1016                                         copy_v3_v3(avg, tmpgrid[x + y*gridsize]);
1017                                         if(x == 0 || x == gridsize - 1)
1018                                                 mul_v3_fl(avg, 2.0f);
1019                                         if(y == 0 || y == gridsize - 1)
1020                                                 mul_v3_fl(avg, 2.0f);
1021
1022                                         CLAMP(fade, 0.0f, 1.0f);
1023
1024                                         val[0] = co[0]+(avg[0]-co[0])*fade;
1025                                         val[1] = co[1]+(avg[1]-co[1])*fade;
1026                                         val[2] = co[2]+(avg[2]-co[2])*fade;
1027                                         
1028                                         sculpt_clip(sd, ss, data[x + y*gridsize].co, val);
1029                                 }
1030                         }
1031                 }
1032         }
1033
1034         //#pragma omp critical
1035         MEM_freeN(tmpgrid);
1036 }
1037
1038 static void do_smooth_brush(Sculpt *sd, SculptSession *ss, PBVHNode **nodes, int totnode)
1039 {
1040         int iteration, n;
1041
1042         for(iteration = 0; iteration < 2; ++iteration) {
1043                 //#pragma omp parallel for private(n) schedule(static)
1044                 for(n=0; n<totnode; n++) {
1045                         sculpt_undo_push_node(ss, nodes[n]);
1046
1047                         if(ss->multires)
1048                                 do_multires_smooth_brush(sd, ss, nodes[n]);
1049                         else if(ss->fmap)
1050                                 do_mesh_smooth_brush(sd, ss, nodes[n]);
1051
1052                         BLI_pbvh_node_mark_update(nodes[n]);
1053                 }
1054
1055                 if(ss->multires)
1056                         multires_stitch_grids(ss->ob);
1057         }
1058 }
1059
1060 static void do_pinch_brush(Sculpt *sd, SculptSession *ss, PBVHNode **nodes, int totnode)
1061 {
1062         Brush *brush = paint_brush(&sd->paint);
1063         float bstrength= ss->cache->bstrength;
1064         int n;
1065
1066         //#pragma omp parallel for private(n) schedule(static)
1067         for(n=0; n<totnode; n++) {
1068                 PBVHVertexIter vd;
1069                 SculptBrushTest test;
1070                 
1071                 sculpt_undo_push_node(ss, nodes[n]);
1072                 sculpt_brush_test_init(ss, &test);
1073
1074                 BLI_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE) {
1075                         if(sculpt_brush_test(&test, vd.co)) {
1076                                 float fade = tex_strength(ss, brush, vd.co, test.dist)*bstrength;
1077                                 float val[3]= {vd.co[0]+(test.location[0]-vd.co[0])*fade,
1078                                                            vd.co[1]+(test.location[1]-vd.co[1])*fade,
1079                                                            vd.co[2]+(test.location[2]-vd.co[2])*fade};
1080
1081                                 sculpt_clip(sd, ss, vd.co, val);                        
1082                                 if(vd.mvert) vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
1083                         }
1084                 }
1085                 BLI_pbvh_vertex_iter_end;
1086
1087                 BLI_pbvh_node_mark_update(nodes[n]);
1088         }
1089 }
1090
1091 static void do_grab_brush(Sculpt *sd, SculptSession *ss, PBVHNode **nodes, int totnode)
1092 {
1093         Brush *brush = paint_brush(&sd->paint);
1094         float bstrength= ss->cache->bstrength;
1095         float grab_delta[3];
1096         int n;
1097         
1098         copy_v3_v3(grab_delta, ss->cache->grab_delta_symmetry);
1099
1100         //#pragma omp parallel for private(n) schedule(static)
1101         for(n=0; n<totnode; n++) {
1102                 PBVHVertexIter vd;
1103                 SculptBrushTest test;
1104                 float (*origco)[3];
1105                 
1106                 origco= sculpt_undo_push_node(ss, nodes[n])->co;
1107                 sculpt_brush_test_init(ss, &test);
1108
1109                 BLI_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE) {
1110                         if(sculpt_brush_test(&test, origco[vd.i])) {
1111                                 float fade = tex_strength(ss, brush, origco[vd.i], test.dist)*bstrength;
1112                                 float add[3]= {vd.co[0]+fade*grab_delta[0],
1113                                                            vd.co[1]+fade*grab_delta[1],
1114                                                            vd.co[2]+fade*grab_delta[2]};
1115
1116                                 sculpt_clip(sd, ss, vd.co, add);                        
1117                                 if(vd.mvert) vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
1118                         }
1119                 }
1120                 BLI_pbvh_vertex_iter_end;
1121
1122                 BLI_pbvh_node_mark_update(nodes[n]);
1123         }
1124 }
1125
1126 static void do_layer_brush(Sculpt *sd, SculptSession *ss, PBVHNode **nodes, int totnode)
1127 {
1128         Brush *brush = paint_brush(&sd->paint);
1129         float bstrength= ss->cache->bstrength;
1130         float area_normal[3], offset[3];
1131         float lim= ss->cache->radius / 4;
1132         int n;
1133
1134         if(ss->cache->flip)
1135                 lim = -lim;
1136
1137         calc_area_normal(sd, ss, area_normal, nodes, totnode);
1138
1139         offset[0]= ss->cache->scale[0]*area_normal[0];
1140         offset[1]= ss->cache->scale[1]*area_normal[1];
1141         offset[2]= ss->cache->scale[2]*area_normal[2];
1142
1143         //#pragma omp parallel for private(n) schedule(static)
1144         for(n=0; n<totnode; n++) {
1145                 PBVHVertexIter vd;
1146                 SculptBrushTest test;
1147                 SculptUndoNode *unode;
1148                 float (*origco)[3], *layer_disp;
1149                 
1150                 unode= sculpt_undo_push_node(ss, nodes[n]);
1151                 origco=unode->co;
1152                 if(!unode->layer_disp)
1153                         unode->layer_disp= MEM_callocN(sizeof(float)*unode->totvert, "layer disp");
1154                 layer_disp= unode->layer_disp;
1155
1156                 sculpt_brush_test_init(ss, &test);
1157
1158                 BLI_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE) {
1159                         if(sculpt_brush_test(&test, vd.co)) {
1160                                 float fade = tex_strength(ss, brush, vd.co, test.dist)*bstrength;
1161                                 float *disp= &layer_disp[vd.i];
1162                                 float val[3];
1163                                 
1164                                 *disp+= fade;
1165                                 
1166                                 /* Don't let the displacement go past the limit */
1167                                 if((lim < 0 && *disp < lim) || (lim > 0 && *disp > lim))
1168                                         *disp = lim;
1169                                 
1170                                 if(ss->layer_co && (brush->flag & BRUSH_PERSISTENT)) {
1171                                         int index= vd.vert_indices[vd.i];
1172
1173                                         /* persistent base */
1174                                         val[0] = ss->layer_co[index][0] + (*disp)*offset[0];
1175                                         val[1] = ss->layer_co[index][1] + (*disp)*offset[1];
1176                                         val[2] = ss->layer_co[index][2] + (*disp)*offset[2];
1177                                 }
1178                                 else {
1179                                         val[0] = origco[vd.i][0] + (*disp)*offset[0];
1180                                         val[1] = origco[vd.i][1] + (*disp)*offset[1];
1181                                         val[2] = origco[vd.i][2] + (*disp)*offset[2];
1182                                 }
1183
1184                                 sculpt_clip(sd, ss, vd.co, val);
1185                                 if(vd.mvert) vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
1186                         }
1187                 }
1188                 BLI_pbvh_vertex_iter_end;
1189
1190                 BLI_pbvh_node_mark_update(nodes[n]);
1191         }
1192 }
1193
1194 static void do_inflate_brush(Sculpt *sd, SculptSession *ss, PBVHNode **nodes, int totnode)
1195 {
1196         Brush *brush = paint_brush(&sd->paint);
1197         float bstrength= ss->cache->bstrength;
1198         int n;
1199
1200         //#pragma omp parallel for private(n) schedule(static)
1201         for(n=0; n<totnode; n++) {
1202                 PBVHVertexIter vd;
1203                 SculptBrushTest test;
1204                 
1205                 sculpt_undo_push_node(ss, nodes[n]);
1206                 sculpt_brush_test_init(ss, &test);
1207
1208                 BLI_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE) {
1209                         if(sculpt_brush_test(&test, vd.co)) {
1210                                 float fade = tex_strength(ss, brush, vd.co, test.dist)*bstrength;
1211                                 float add[3];
1212
1213                                 if(vd.fno) copy_v3_v3(add, vd.fno);
1214                                 else normal_short_to_float_v3(add, vd.no);
1215                                 
1216                                 mul_v3_fl(add, fade * ss->cache->radius);
1217                                 add[0]*= ss->cache->scale[0];
1218                                 add[1]*= ss->cache->scale[1];
1219                                 add[2]*= ss->cache->scale[2];
1220                                 add_v3_v3(add, vd.co);
1221                                 
1222                                 sculpt_clip(sd, ss, vd.co, add);
1223                                 if(vd.mvert) vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
1224                         }
1225                 }
1226                 BLI_pbvh_vertex_iter_end;
1227
1228                 BLI_pbvh_node_mark_update(nodes[n]);
1229         }
1230 }
1231
1232 static void calc_flatten_center(Sculpt *sd, SculptSession *ss, PBVHNode **nodes, int totnode, float co[3])
1233 {
1234         float outer_dist[FLATTEN_SAMPLE_SIZE];
1235         float outer_co[FLATTEN_SAMPLE_SIZE][3];
1236         int i, n;
1237         
1238         for(i = 0; i < FLATTEN_SAMPLE_SIZE; ++i) {
1239                 zero_v3(outer_co[i]);
1240                 outer_dist[i]= -1.0f;
1241         }
1242                 
1243         //#pragma omp parallel for private(n) schedule(static)
1244         for(n=0; n<totnode; n++) {
1245                 PBVHVertexIter vd;
1246                 SculptBrushTest test;
1247                 int j;
1248                 
1249                 sculpt_undo_push_node(ss, nodes[n]);
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                                 for(j = 0; j < FLATTEN_SAMPLE_SIZE; ++j) {
1255                                         if(test.dist > outer_dist[j]) {
1256                                                 copy_v3_v3(outer_co[j], vd.co);
1257                                                 outer_dist[j] = test.dist;
1258                                                 break;
1259                                         }
1260                                 }
1261                         }
1262                 }
1263                 BLI_pbvh_vertex_iter_end;
1264
1265                 BLI_pbvh_node_mark_update(nodes[n]);
1266         }
1267         
1268         co[0] = co[1] = co[2] = 0.0f;
1269         for(i = 0; i < FLATTEN_SAMPLE_SIZE; ++i)
1270                 if(outer_dist[i] >= 0.0f)
1271                         add_v3_v3(co, outer_co[i]);
1272         mul_v3_fl(co, 1.0f / FLATTEN_SAMPLE_SIZE);
1273 }
1274
1275 /* Projects a point onto a plane along the plane's normal */
1276 static void point_plane_project(float intr[3], float co[3], float plane_normal[3], float plane_center[3])
1277 {
1278         float p1[3], sub1[3], sub2[3];
1279
1280         /* Find the intersection between squash-plane and vertex (along the area normal) */
1281         sub_v3_v3v3(p1, co, plane_normal);
1282         sub_v3_v3v3(sub1, plane_center, p1);
1283         sub_v3_v3v3(sub2, co, p1);
1284         sub_v3_v3v3(intr, co, p1);
1285         mul_v3_fl(intr, dot_v3v3(plane_normal, sub1) / dot_v3v3(plane_normal, sub2));
1286         add_v3_v3(intr, p1);
1287 }
1288
1289 static int plane_point_side(float co[3], float plane_normal[3], float plane_center[3], int flip)
1290 {
1291         float delta[3];
1292         float d;
1293
1294         sub_v3_v3v3(delta, co, plane_center);
1295         d = dot_v3v3(plane_normal, delta);
1296
1297         if(flip)
1298                 d = -d;
1299
1300         return d <= 0.0f;
1301 }
1302
1303 static void do_flatten_clay_brush(Sculpt *sd, SculptSession *ss, PBVHNode **nodes, int totnode, int clay)
1304 {
1305         /* area_normal and cntr define the plane towards which vertices are squashed */
1306         Brush *brush = paint_brush(&sd->paint);
1307         float bstrength= ss->cache->bstrength;
1308         float area_normal[3];
1309         float cntr[3], cntr2[3] = {0}, bstr = 0;
1310         int n, flip = 0;
1311
1312         calc_area_normal(sd, ss, area_normal, nodes, totnode);
1313         calc_flatten_center(sd, ss, nodes, totnode, cntr);
1314
1315         if(clay) {
1316                 bstr= brush_strength(sd, ss->cache);
1317                 /* Limit clay application to here */
1318                 cntr2[0]=cntr[0]+area_normal[0]*bstr*ss->cache->scale[0];
1319                 cntr2[1]=cntr[1]+area_normal[1]*bstr*ss->cache->scale[1];
1320                 cntr2[2]=cntr[2]+area_normal[2]*bstr*ss->cache->scale[2];
1321                 flip = bstr < 0;
1322         }
1323
1324         //#pragma omp parallel for private(n) schedule(static)
1325         for(n=0; n<totnode; n++) {
1326                 PBVHVertexIter vd;
1327                 SculptBrushTest test;
1328                 
1329                 sculpt_undo_push_node(ss, nodes[n]);
1330                 sculpt_brush_test_init(ss, &test);
1331
1332                 BLI_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE) {
1333                         if(sculpt_brush_test(&test, vd.co)) {
1334                                 float intr[3], val[3];
1335                                 
1336                                 if(!clay || plane_point_side(vd.co, area_normal, cntr2, flip)) {
1337                                         const float fade = tex_strength(ss, brush, vd.co, test.dist)*bstrength;
1338
1339                                         /* Find the intersection between squash-plane and vertex (along the area normal) */             
1340                                         point_plane_project(intr, vd.co, area_normal, cntr);
1341
1342                                         sub_v3_v3v3(val, intr, vd.co);
1343
1344                                         if(clay) {
1345                                                 if(bstr > FLT_EPSILON)
1346                                                         mul_v3_fl(val, fade / bstr);
1347                                                 else
1348                                                         mul_v3_fl(val, fade);
1349                                                 /* Clay displacement */
1350                                                 val[0]+=area_normal[0] * ss->cache->scale[0]*fade;
1351                                                 val[1]+=area_normal[1] * ss->cache->scale[1]*fade;
1352                                                 val[2]+=area_normal[2] * ss->cache->scale[2]*fade;
1353                                         }
1354                                         else
1355                                                 mul_v3_fl(val, fabs(fade));
1356
1357                                         add_v3_v3(val, vd.co);
1358
1359                                         sculpt_clip(sd, ss, vd.co, val);
1360                                         if(vd.mvert) vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
1361                                 }
1362                         }
1363                 }
1364                 BLI_pbvh_vertex_iter_end;
1365
1366                 BLI_pbvh_node_mark_update(nodes[n]);
1367         }
1368 }
1369
1370 static void do_brush_action(Sculpt *sd, SculptSession *ss, StrokeCache *cache)
1371 {
1372         SculptSearchSphereData data;
1373         Brush *brush = paint_brush(&sd->paint);
1374         PBVHNode **nodes= NULL;
1375         int totnode;
1376
1377         data.ss = ss;
1378         data.sd = sd;
1379         data.radius_squared = ss->cache->radius * ss->cache->radius;
1380
1381         /* Build a list of all nodes that are potentially within the brush's
1382            area of influence */
1383         if(brush->sculpt_tool == SCULPT_TOOL_GRAB) {
1384                 data.original= 1;
1385                 BLI_pbvh_search_gather(ss->pbvh, sculpt_search_sphere_cb, &data,
1386                                 &nodes, &totnode);
1387
1388                 if(cache->first_time)
1389                         copy_v3_v3(ss->cache->grab_active_location[ss->cache->symmetry], ss->cache->location);
1390                 else
1391                         copy_v3_v3(ss->cache->location, ss->cache->grab_active_location[ss->cache->symmetry]);
1392         }
1393         else {
1394                 BLI_pbvh_search_gather(ss->pbvh, sculpt_search_sphere_cb, &data,
1395                         &nodes, &totnode);
1396         }
1397
1398         /* Only act if some verts are inside the brush area */
1399         if(totnode) {
1400                 /* Apply one type of brush action */
1401                 switch(brush->sculpt_tool){
1402                 case SCULPT_TOOL_DRAW:
1403                         do_draw_brush(sd, ss, nodes, totnode);
1404                         break;
1405                 case SCULPT_TOOL_SMOOTH:
1406                         do_smooth_brush(sd, ss, nodes, totnode);
1407                         break;
1408                 case SCULPT_TOOL_PINCH:
1409                         do_pinch_brush(sd, ss, nodes, totnode);
1410                         break;
1411                 case SCULPT_TOOL_INFLATE:
1412                         do_inflate_brush(sd, ss, nodes, totnode);
1413                         break;
1414                 case SCULPT_TOOL_GRAB:
1415                         do_grab_brush(sd, ss, nodes, totnode);
1416                         break;
1417                 case SCULPT_TOOL_LAYER:
1418                         do_layer_brush(sd, ss, nodes, totnode);
1419                         break;
1420                 case SCULPT_TOOL_FLATTEN:
1421                         do_flatten_clay_brush(sd, ss, nodes, totnode, 0);
1422                         break;
1423                 case SCULPT_TOOL_CLAY:
1424                         do_flatten_clay_brush(sd, ss, nodes, totnode, 1);
1425                         break;
1426                 }
1427         
1428                 /* copy the modified vertices from mesh to the active key */
1429                 if(ss->kb) mesh_to_key(ss->ob->data, ss->kb);
1430                 
1431                 if(nodes)
1432                         MEM_freeN(nodes);
1433         }       
1434 }
1435
1436 /* Flip all the editdata across the axis/axes specified by symm. Used to
1437    calculate multiple modifications to the mesh when symmetry is enabled. */
1438 static void calc_brushdata_symm(StrokeCache *cache, const char symm)
1439 {
1440         flip_coord(cache->location, cache->true_location, symm);
1441         flip_coord(cache->view_normal_symmetry, cache->view_normal, symm);
1442         flip_coord(cache->grab_delta_symmetry, cache->grab_delta, symm);
1443         cache->symmetry= symm;
1444 }
1445
1446 static void do_symmetrical_brush_actions(Sculpt *sd, SculptSession *ss)
1447 {
1448         StrokeCache *cache = ss->cache;
1449         const char symm = sd->flags & 7;
1450         int i;
1451
1452         copy_v3_v3(cache->location, cache->true_location);
1453         copy_v3_v3(cache->grab_delta_symmetry, cache->grab_delta);
1454         cache->symmetry = 0;
1455         cache->bstrength = brush_strength(sd, cache);
1456         do_brush_action(sd, ss, cache);
1457
1458         for(i = 1; i <= symm; ++i) {
1459                 if(symm & i && (symm != 5 || i != 3) && (symm != 6 || (i != 3 && i != 5))) {
1460                         calc_brushdata_symm(cache, i);
1461                         do_brush_action(sd, ss, cache);
1462                 }
1463         }
1464
1465         cache->first_time = 0;
1466 }
1467
1468 static void sculpt_update_tex(Sculpt *sd, SculptSession *ss)
1469 {
1470         Brush *brush = paint_brush(&sd->paint);
1471
1472         if(ss->texcache) {
1473                 MEM_freeN(ss->texcache);
1474                 ss->texcache= NULL;
1475         }
1476
1477         /* Need to allocate a bigger buffer for bigger brush size */
1478         ss->texcache_side = brush->size * 2;
1479         if(!ss->texcache || ss->texcache_side > ss->texcache_actual) {
1480                 ss->texcache = brush_gen_texture_cache(brush, brush->size);
1481                 ss->texcache_actual = ss->texcache_side;
1482         }
1483 }
1484
1485 /* Sculpt mode handles multires differently from regular meshes, but only if
1486    it's the last modifier on the stack and it is not on the first level */
1487 struct MultiresModifierData *sculpt_multires_active(Scene *scene, Object *ob)
1488 {
1489         ModifierData *md, *nmd;
1490         
1491         for(md= modifiers_getVirtualModifierList(ob); md; md= md->next) {
1492                 if(md->type == eModifierType_Multires) {
1493                         MultiresModifierData *mmd= (MultiresModifierData*)md;
1494
1495                         /* Check if any of the modifiers after multires are active
1496                          * if not it can use the multires struct */
1497                         for(nmd= md->next; nmd; nmd= nmd->next)
1498                                 if(modifier_isEnabled(scene, nmd, eModifierMode_Realtime))
1499                                         break;
1500
1501                         if(!nmd && mmd->sculptlvl > 0)
1502                                 return mmd;
1503                 }
1504         }
1505
1506         return NULL;
1507 }
1508
1509 void sculpt_key_to_mesh(KeyBlock *kb, Object *ob)
1510 {
1511         Mesh *me= ob->data;
1512
1513         key_to_mesh(kb, me);
1514         mesh_calc_normals(me->mvert, me->totvert, me->mface, me->totface, NULL);
1515 }
1516
1517 void sculpt_mesh_to_key(Object *ob, KeyBlock *kb)
1518 {
1519         Mesh *me= ob->data;
1520
1521         mesh_to_key(me, kb);
1522 }
1523
1524 void sculpt_update_mesh_elements(Scene *scene, Object *ob, int need_fmap)
1525 {
1526         DerivedMesh *dm = mesh_get_derived_final(scene, ob, 0);
1527         SculptSession *ss = ob->sculpt;
1528         MultiresModifierData *mmd= sculpt_multires_active(scene, ob);
1529
1530         ss->ob= ob;
1531
1532         if((ob->shapeflag & OB_SHAPE_LOCK) && !mmd) {
1533                 ss->kb= ob_get_keyblock(ob);
1534                 ss->refkb= ob_get_reference_keyblock(ob);
1535         }
1536         else {
1537                 ss->kb= NULL;
1538                 ss->refkb= NULL;
1539         }
1540
1541         /* need to make PBVH with shape key coordinates */
1542         if(ss->kb) sculpt_key_to_mesh(ss->kb, ss->ob);
1543
1544         if(mmd) {
1545                 ss->multires = mmd;
1546                 ss->totvert = dm->getNumVerts(dm);
1547                 ss->totface = dm->getNumFaces(dm);
1548                 ss->mvert= NULL;
1549                 ss->mface= NULL;
1550                 ss->face_normals= NULL;
1551         }
1552         else {
1553                 Mesh *me = get_mesh(ob);
1554                 ss->totvert = me->totvert;
1555                 ss->totface = me->totface;
1556                 ss->mvert = me->mvert;
1557                 ss->mface = me->mface;
1558                 ss->face_normals = NULL;
1559                 ss->multires = NULL;
1560         }
1561
1562         ss->pbvh = dm->getPBVH(ob, dm);
1563         ss->fmap = (need_fmap && dm->getFaceMap)? dm->getFaceMap(ob, dm): NULL;
1564 }
1565
1566 static int sculpt_mode_poll(bContext *C)
1567 {
1568         Object *ob = CTX_data_active_object(C);
1569         return ob && ob->mode & OB_MODE_SCULPT;
1570 }
1571
1572 int sculpt_poll(bContext *C)
1573 {
1574         return sculpt_mode_poll(C) && paint_poll(C);
1575 }
1576
1577 static char *sculpt_tool_name(Sculpt *sd)
1578 {
1579         Brush *brush = paint_brush(&sd->paint);
1580
1581         switch(brush->sculpt_tool) {
1582         case SCULPT_TOOL_DRAW:
1583                 return "Draw Brush"; break;
1584         case SCULPT_TOOL_SMOOTH:
1585                 return "Smooth Brush"; break;
1586         case SCULPT_TOOL_PINCH:
1587                 return "Pinch Brush"; break;
1588         case SCULPT_TOOL_INFLATE:
1589                 return "Inflate Brush"; break;
1590         case SCULPT_TOOL_GRAB:
1591                 return "Grab Brush"; break;
1592         case SCULPT_TOOL_LAYER:
1593                 return "Layer Brush"; break;
1594         case SCULPT_TOOL_FLATTEN:
1595                  return "Flatten Brush"; break;
1596         default:
1597                 return "Sculpting"; break;
1598         }
1599 }
1600
1601 /**** Radial control ****/
1602 static int sculpt_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
1603 {
1604         Paint *p = paint_get_active(CTX_data_scene(C));
1605         Brush *brush = paint_brush(p);
1606
1607         WM_paint_cursor_end(CTX_wm_manager(C), p->paint_cursor);
1608         p->paint_cursor = NULL;
1609         brush_radial_control_invoke(op, brush, 1);
1610         return WM_radial_control_invoke(C, op, event);
1611 }
1612
1613 static int sculpt_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
1614 {
1615         int ret = WM_radial_control_modal(C, op, event);
1616         if(ret != OPERATOR_RUNNING_MODAL)
1617                 paint_cursor_start(C, sculpt_poll);
1618         return ret;
1619 }
1620
1621 static int sculpt_radial_control_exec(bContext *C, wmOperator *op)
1622 {
1623         Brush *brush = paint_brush(&CTX_data_tool_settings(C)->sculpt->paint);
1624         int ret = brush_radial_control_exec(op, brush, 1);
1625
1626         WM_event_add_notifier(C, NC_BRUSH|NA_EDITED, brush);
1627
1628         return ret;
1629 }
1630
1631 static void SCULPT_OT_radial_control(wmOperatorType *ot)
1632 {
1633         WM_OT_radial_control_partial(ot);
1634
1635         ot->name= "Sculpt Radial Control";
1636         ot->idname= "SCULPT_OT_radial_control";
1637
1638         ot->invoke= sculpt_radial_control_invoke;
1639         ot->modal= sculpt_radial_control_modal;
1640         ot->exec= sculpt_radial_control_exec;
1641         ot->poll= sculpt_poll;
1642
1643         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
1644 }
1645
1646 /**** Operator for applying a stroke (various attributes including mouse path)
1647           using the current brush. ****/
1648
1649 static float unproject_brush_radius(Object *ob, ViewContext *vc, float center[3], float offset)
1650 {
1651         float delta[3], scale, loc[3];
1652
1653         mul_v3_m4v3(loc, ob->obmat, center);
1654
1655         initgrabz(vc->rv3d, loc[0], loc[1], loc[2]);
1656         window_to_3d_delta(vc->ar, delta, offset, 0);
1657
1658         scale= fabsf(mat4_to_scale(ob->obmat));
1659         scale= (scale == 0.0f)? 1.0f: scale;
1660
1661         return len_v3(delta)/scale;
1662 }
1663
1664 static void sculpt_cache_free(StrokeCache *cache)
1665 {
1666         if(cache->face_norms)
1667                 MEM_freeN(cache->face_norms);
1668         if(cache->mats)
1669                 MEM_freeN(cache->mats);
1670         MEM_freeN(cache);
1671 }
1672
1673 /* Initialize the stroke cache invariants from operator properties */
1674 static void sculpt_update_cache_invariants(Sculpt *sd, SculptSession *ss, bContext *C, wmOperator *op)
1675 {
1676         StrokeCache *cache = MEM_callocN(sizeof(StrokeCache), "stroke cache");
1677         Brush *brush = paint_brush(&sd->paint);
1678         ViewContext *vc = paint_stroke_view_context(op->customdata);
1679         int i;
1680
1681         ss->cache = cache;
1682
1683         RNA_float_get_array(op->ptr, "scale", cache->scale);
1684         cache->flag = RNA_int_get(op->ptr, "flag");
1685         RNA_float_get_array(op->ptr, "clip_tolerance", cache->clip_tolerance);
1686         RNA_float_get_array(op->ptr, "initial_mouse", cache->initial_mouse);
1687
1688         copy_v2_v2(cache->mouse, cache->initial_mouse);
1689         copy_v2_v2(cache->tex_mouse, cache->initial_mouse);
1690
1691         /* Truly temporary data that isn't stored in properties */
1692
1693         cache->vc = vc;
1694         cache->brush = brush;
1695
1696         cache->mats = MEM_callocN(sizeof(bglMats), "sculpt bglMats");
1697         view3d_get_transformation(vc->ar, vc->rv3d, vc->obact, cache->mats);
1698
1699         /* Initialize layer brush displacements and persistent coords */
1700         if(brush->sculpt_tool == SCULPT_TOOL_LAYER) {
1701                 /* not supported yet for multires */
1702                 if(!ss->multires && !ss->layer_co && (brush->flag & BRUSH_PERSISTENT)) {
1703                         if(!ss->layer_co)
1704                                 ss->layer_co= MEM_mallocN(sizeof(float) * 3 * ss->totvert,
1705                                                                            "sculpt mesh vertices copy");
1706
1707                         for(i = 0; i < ss->totvert; ++i)
1708                                 copy_v3_v3(ss->layer_co[i], ss->mvert[i].co);
1709                 }
1710         }
1711
1712         /* Make copies of the mesh vertex locations and normals for some tools */
1713         if(brush->flag & BRUSH_ANCHORED) {
1714                 if(ss->face_normals) {
1715                         float *fn = ss->face_normals;
1716                         cache->face_norms= MEM_mallocN(sizeof(float) * 3 * ss->totface, "Sculpt face norms");
1717                         for(i = 0; i < ss->totface; ++i, fn += 3)
1718                                 copy_v3_v3(cache->face_norms[i], fn);
1719                 }
1720
1721                 cache->original = 1;
1722         }
1723
1724         if(ELEM3(brush->sculpt_tool, SCULPT_TOOL_DRAW, SCULPT_TOOL_LAYER, SCULPT_TOOL_INFLATE))
1725                 if(!(brush->flag & BRUSH_ACCUMULATE))
1726                         cache->original = 1;
1727
1728         cache->rotation = 0;
1729         cache->first_time = 1;
1730 }
1731
1732 /* Initialize the stroke cache variants from operator properties */
1733 static void sculpt_update_cache_variants(Sculpt *sd, SculptSession *ss, struct PaintStroke *stroke, PointerRNA *ptr)
1734 {
1735         StrokeCache *cache = ss->cache;
1736         Brush *brush = paint_brush(&sd->paint);
1737         
1738         int dx, dy;
1739
1740         if(!(brush->flag & BRUSH_ANCHORED) || cache->first_time)
1741                 RNA_float_get_array(ptr, "location", cache->true_location);
1742         cache->flip = RNA_boolean_get(ptr, "flip");
1743         RNA_float_get_array(ptr, "mouse", cache->mouse);
1744         cache->pressure = RNA_float_get(ptr, "pressure");
1745         
1746         /* Truly temporary data that isn't stored in properties */
1747
1748         cache->previous_pixel_radius = cache->pixel_radius;
1749         cache->pixel_radius = brush->size;
1750
1751         if(cache->first_time)
1752                 cache->initial_radius = unproject_brush_radius(ss->ob, cache->vc, cache->true_location, brush->size);
1753
1754         if(brush->flag & BRUSH_SIZE_PRESSURE && brush->sculpt_tool != SCULPT_TOOL_GRAB) {
1755                 cache->pixel_radius *= cache->pressure;
1756                 cache->radius = cache->initial_radius * cache->pressure;
1757         }
1758         else
1759                 cache->radius = cache->initial_radius;
1760
1761         if(!(brush->flag & BRUSH_ANCHORED))
1762                 copy_v2_v2(cache->tex_mouse, cache->mouse);
1763
1764         if(brush->flag & BRUSH_ANCHORED) {
1765                 dx = cache->mouse[0] - cache->initial_mouse[0];
1766                 dy = cache->mouse[1] - cache->initial_mouse[1];
1767                 cache->pixel_radius = sqrt(dx*dx + dy*dy);
1768                 cache->radius = unproject_brush_radius(ss->ob, paint_stroke_view_context(stroke),
1769                                                            cache->true_location, cache->pixel_radius);
1770                 cache->rotation = atan2(dy, dx);
1771         }
1772         else if(brush->flag & BRUSH_RAKE) {
1773                 int update;
1774
1775                 dx = cache->last_rake[0] - cache->mouse[0];
1776                 dy = cache->last_rake[1] - cache->mouse[1];
1777
1778                 update = dx*dx + dy*dy > 100;
1779
1780                 /* To prevent jitter, only update the angle if the mouse has moved over 10 pixels */
1781                 if(update && !cache->first_time)
1782                         cache->rotation = M_PI_2 + atan2(dy, dx);
1783
1784                 if(update || cache->first_time) {
1785                         cache->last_rake[0] = cache->mouse[0];
1786                         cache->last_rake[1] = cache->mouse[1];
1787                 }
1788         }
1789
1790         /* Find the grab delta */
1791         if(brush->sculpt_tool == SCULPT_TOOL_GRAB) {
1792                 float grab_location[3], imat[4][4];
1793
1794                 if(cache->first_time)
1795                         copy_v3_v3(cache->orig_grab_location, cache->true_location);
1796
1797                 /* compute 3d coordinate at same z from original location + mouse */
1798                 initgrabz(cache->vc->rv3d, cache->orig_grab_location[0],
1799                         cache->orig_grab_location[1], cache->orig_grab_location[2]);
1800                 window_to_3d_delta(cache->vc->ar, grab_location, cache->mouse[0], cache->mouse[1]);
1801
1802                 /* compute delta to move verts by */
1803                 if(!cache->first_time) {
1804                         sub_v3_v3v3(cache->grab_delta, grab_location, cache->old_grab_location);
1805                         invert_m4_m4(imat, ss->ob->obmat);
1806                         mul_mat3_m4_v3(imat, cache->grab_delta);
1807                 }
1808
1809                 copy_v3_v3(cache->old_grab_location, grab_location);
1810
1811                 /* location stays the same for finding vertices in brush radius */
1812                 copy_v3_v3(cache->true_location, cache->orig_grab_location);
1813         }
1814 }
1815
1816 static void sculpt_stroke_modifiers_check(bContext *C, SculptSession *ss)
1817 {
1818         Scene *scene= CTX_data_scene(C);
1819
1820         if(sculpt_modifiers_active(scene, ss->ob)) {
1821                 Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
1822                 Brush *brush = paint_brush(&sd->paint);
1823
1824                 sculpt_update_mesh_elements(CTX_data_scene(C), ss->ob, brush->sculpt_tool == SCULPT_TOOL_SMOOTH);
1825         }
1826 }
1827
1828 typedef struct {
1829         SculptSession *ss;
1830         float *ray_start, *ray_normal;
1831         int hit;
1832         float dist;
1833         int original;
1834 } SculptRaycastData;
1835
1836 void sculpt_raycast_cb(PBVHNode *node, void *data_v)
1837 {
1838         SculptRaycastData *srd = data_v;
1839         float (*origco)[3]= NULL;
1840
1841         if(srd->original && srd->ss->cache) {
1842                 /* intersect with coordinates from before we started stroke */
1843                 SculptUndoNode *unode= sculpt_undo_get_node(srd->ss, node);
1844                 origco= (unode)? unode->co: NULL;
1845         }
1846
1847         srd->hit |= BLI_pbvh_node_raycast(srd->ss->pbvh, node, origco,
1848                 srd->ray_start, srd->ray_normal, &srd->dist);
1849 }
1850
1851 /* Do a raycast in the tree to find the 3d brush location
1852    (This allows us to ignore the GL depth buffer)
1853    Returns 0 if the ray doesn't hit the mesh, non-zero otherwise
1854  */
1855 int sculpt_stroke_get_location(bContext *C, struct PaintStroke *stroke, float out[3], float mouse[2])
1856 {
1857         ViewContext *vc = paint_stroke_view_context(stroke);
1858         SculptSession *ss= vc->obact->sculpt;
1859         StrokeCache *cache= ss->cache;
1860         float ray_start[3], ray_end[3], ray_normal[3], dist;
1861         float obimat[4][4];
1862         float mval[2] = {mouse[0] - vc->ar->winrct.xmin,
1863                          mouse[1] - vc->ar->winrct.ymin};
1864         SculptRaycastData srd;
1865
1866         sculpt_stroke_modifiers_check(C, ss);
1867
1868         viewline(vc->ar, vc->v3d, mval, ray_start, ray_end);
1869
1870         invert_m4_m4(obimat, ss->ob->obmat);
1871         mul_m4_v3(obimat, ray_start);
1872         mul_m4_v3(obimat, ray_end);
1873
1874         sub_v3_v3v3(ray_normal, ray_end, ray_start);
1875         dist= normalize_v3(ray_normal);
1876
1877         srd.ss = vc->obact->sculpt;
1878         srd.ray_start = ray_start;
1879         srd.ray_normal = ray_normal;
1880         srd.dist = dist;
1881         srd.hit = 0;
1882         srd.original = (cache)? cache->original: 0;
1883         BLI_pbvh_raycast(ss->pbvh, sculpt_raycast_cb, &srd,
1884                          ray_start, ray_normal, srd.original);
1885         
1886         copy_v3_v3(out, ray_normal);
1887         mul_v3_fl(out, srd.dist);
1888         add_v3_v3(out, ray_start);
1889
1890         return srd.hit;
1891 }
1892
1893 /* Initialize stroke operator properties */
1894 static void sculpt_brush_stroke_init_properties(bContext *C, wmOperator *op, wmEvent *event, SculptSession *ss)
1895 {
1896         Object *ob= CTX_data_active_object(C);
1897         ModifierData *md;
1898         float scale[3], clip_tolerance[3] = {0,0,0};
1899         float mouse[2];
1900         int flag = 0;
1901
1902         /* Set scaling adjustment */
1903         scale[0] = 1.0f / ob->size[0];
1904         scale[1] = 1.0f / ob->size[1];
1905         scale[2] = 1.0f / ob->size[2];
1906         RNA_float_set_array(op->ptr, "scale", scale);
1907
1908         /* Initialize mirror modifier clipping */
1909         for(md= ob->modifiers.first; md; md= md->next) {
1910                 if(md->type==eModifierType_Mirror && (md->mode & eModifierMode_Realtime)) {
1911                         const MirrorModifierData *mmd = (MirrorModifierData*) md;
1912                         
1913                         /* Mark each axis that needs clipping along with its tolerance */
1914                         if(mmd->flag & MOD_MIR_CLIPPING) {
1915                                 flag |= CLIP_X << mmd->axis;
1916                                 if(mmd->tolerance > clip_tolerance[mmd->axis])
1917                                         clip_tolerance[mmd->axis] = mmd->tolerance;
1918                         }
1919                 }
1920         }
1921         RNA_int_set(op->ptr, "flag", flag);
1922         RNA_float_set_array(op->ptr, "clip_tolerance", clip_tolerance);
1923
1924         /* Initial mouse location */
1925         mouse[0] = event->x;
1926         mouse[1] = event->y;
1927         RNA_float_set_array(op->ptr, "initial_mouse", mouse);
1928 }
1929
1930 static int sculpt_brush_stroke_init(bContext *C, ReportList *reports)
1931 {
1932         Scene *scene= CTX_data_scene(C);
1933         Object *ob= CTX_data_active_object(C);
1934         Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
1935         SculptSession *ss = CTX_data_active_object(C)->sculpt;
1936         Brush *brush = paint_brush(&sd->paint);
1937
1938         if(ob_get_key(ob) && !(ob->shapeflag & OB_SHAPE_LOCK)) {
1939                 BKE_report(reports, RPT_ERROR, "Shape key sculpting requires a locked shape.");
1940                 return 0;
1941         }
1942
1943         view3d_operator_needs_opengl(C);
1944
1945         /* TODO: Shouldn't really have to do this at the start of every
1946            stroke, but sculpt would need some sort of notification when
1947            changes are made to the texture. */
1948         sculpt_update_tex(sd, ss);
1949
1950         sculpt_update_mesh_elements(scene, ob, brush->sculpt_tool == SCULPT_TOOL_SMOOTH);
1951
1952         return 1;
1953 }
1954
1955 static void sculpt_restore_mesh(Sculpt *sd, SculptSession *ss)
1956 {
1957         StrokeCache *cache = ss->cache;
1958         Brush *brush = paint_brush(&sd->paint);
1959         int i;
1960
1961         /* Restore the mesh before continuing with anchored stroke */
1962         if(brush->flag & BRUSH_ANCHORED) {
1963                 PBVHNode **nodes;
1964                 int n, totnode;
1965
1966                 BLI_pbvh_search_gather(ss->pbvh, NULL, NULL, &nodes, &totnode);
1967
1968                 //#pragma omp parallel for private(n) schedule(static)
1969                 for(n=0; n<totnode; n++) {
1970                         SculptUndoNode *unode;
1971                         
1972                         unode= sculpt_undo_get_node(ss, nodes[n]);
1973                         if(unode) {
1974                                 PBVHVertexIter vd;
1975
1976                                 BLI_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE) {
1977                                         copy_v3_v3(vd.co, unode->co[vd.i]);
1978                                         if(vd.no) VECCOPY(vd.no, unode->no[vd.i])
1979                                         else normal_short_to_float_v3(vd.fno, unode->no[vd.i]);
1980                                 }
1981                                 BLI_pbvh_vertex_iter_end;
1982                         }
1983                 }
1984
1985                 if(ss->face_normals) {
1986                         float *fn = ss->face_normals;
1987                         for(i = 0; i < ss->totface; ++i, fn += 3)
1988                                 copy_v3_v3(fn, cache->face_norms[i]);
1989                 }
1990
1991                 if(nodes)
1992                         MEM_freeN(nodes);
1993         }
1994 }
1995
1996 static void sculpt_flush_update(bContext *C)
1997 {
1998         Scene *scene = CTX_data_scene(C);
1999         Object *ob = CTX_data_active_object(C);
2000         SculptSession *ss = ob->sculpt;
2001         ARegion *ar = CTX_wm_region(C);
2002         MultiresModifierData *mmd = ss->multires;
2003         int redraw = 0;
2004
2005         if(mmd)
2006                 multires_mark_as_modified(ob);
2007
2008         if(sculpt_modifiers_active(scene, ob)) {
2009                 DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
2010                 ED_region_tag_redraw(ar);
2011         }
2012         else {
2013                 rcti r;
2014
2015                 BLI_pbvh_update(ss->pbvh, PBVH_UpdateBB, NULL);
2016                 redraw = sculpt_get_redraw_rect(ar, CTX_wm_region_view3d(C), ob, &r);
2017
2018                 if(redraw) {
2019                         r.xmin += ar->winrct.xmin + 1;
2020                         r.xmax += ar->winrct.xmin - 1;
2021                         r.ymin += ar->winrct.ymin + 1;
2022                         r.ymax += ar->winrct.ymin - 1;
2023                         
2024                         ss->partial_redraw = 1;
2025                         ED_region_tag_redraw_partial(ar, &r);
2026                 }
2027         }
2028 }
2029
2030 /* Returns whether the mouse/stylus is over the mesh (1)
2031    or over the background (0) */
2032 static int over_mesh(bContext *C, struct wmOperator *op, float x, float y)
2033 {
2034         float mouse[2] = {x, y}, co[3];
2035         
2036         return (int)sculpt_stroke_get_location(C, op->customdata, co, mouse);
2037 }
2038
2039 static int sculpt_stroke_test_start(bContext *C, struct wmOperator *op,
2040                                         wmEvent *event)
2041 {
2042         /* Don't start the stroke until mouse goes over the mesh */
2043         if(over_mesh(C, op, event->x, event->y)) {
2044                 Object *ob = CTX_data_active_object(C);
2045                 SculptSession *ss = ob->sculpt;
2046                 Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
2047
2048                 ED_view3d_init_mats_rv3d(ob, CTX_wm_region_view3d(C));
2049
2050                 sculpt_brush_stroke_init_properties(C, op, event, ss);
2051
2052                 sculpt_update_cache_invariants(sd, ss, C, op);
2053
2054                 sculpt_undo_push_begin(ss, sculpt_tool_name(sd));
2055
2056                 return 1;
2057         }
2058         else
2059                 return 0;
2060 }
2061
2062 static void sculpt_stroke_update_step(bContext *C, struct PaintStroke *stroke, PointerRNA *itemptr)
2063 {
2064         Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
2065         SculptSession *ss = CTX_data_active_object(C)->sculpt;
2066
2067         sculpt_stroke_modifiers_check(C, ss);
2068         sculpt_update_cache_variants(sd, ss, stroke, itemptr);
2069         sculpt_restore_mesh(sd, ss);
2070         do_symmetrical_brush_actions(sd, ss);
2071
2072         /* Cleanup */
2073         sculpt_flush_update(C);
2074 }
2075
2076 static void sculpt_stroke_done(bContext *C, struct PaintStroke *stroke)
2077 {
2078         Object *ob= CTX_data_active_object(C);
2079         SculptSession *ss = ob->sculpt;
2080
2081         /* Finished */
2082         if(ss->cache) {
2083                 sculpt_stroke_modifiers_check(C, ss);
2084
2085                 sculpt_cache_free(ss->cache);
2086                 ss->cache = NULL;
2087
2088                 sculpt_undo_push_end(ss);
2089
2090                 BLI_pbvh_update(ss->pbvh, PBVH_UpdateOriginalBB, NULL);
2091
2092                 if(ss->refkb) sculpt_key_to_mesh(ss->refkb, ob);
2093
2094                 ss->partial_redraw = 0;
2095                 
2096                 /* try to avoid calling this, only for e.g. linked duplicates now */
2097                 if(((Mesh*)ob->data)->id.us > 1)
2098                         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
2099
2100                 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
2101         }
2102 }
2103
2104 static int sculpt_brush_stroke_invoke(bContext *C, wmOperator *op, wmEvent *event)
2105 {
2106         struct PaintStroke *stroke;
2107         int ignore_background_click;
2108
2109         if(!sculpt_brush_stroke_init(C, op->reports))
2110                 return OPERATOR_CANCELLED;
2111
2112         stroke = paint_stroke_new(C, sculpt_stroke_get_location,
2113                                   sculpt_stroke_test_start,
2114                                   sculpt_stroke_update_step,
2115                                   sculpt_stroke_done);
2116
2117         op->customdata = stroke;
2118
2119         /* For tablet rotation */
2120         ignore_background_click = RNA_boolean_get(op->ptr,
2121                                                   "ignore_background_click"); 
2122         if(ignore_background_click && !over_mesh(C, op, event->x, event->y)) {
2123                 paint_stroke_free(stroke);
2124                 return OPERATOR_PASS_THROUGH;
2125         }
2126         
2127         /* add modal handler */
2128         WM_event_add_modal_handler(C, op);
2129
2130         op->type->modal(C, op, event);
2131         
2132         return OPERATOR_RUNNING_MODAL;
2133 }
2134
2135 static int sculpt_brush_stroke_exec(bContext *C, wmOperator *op)
2136 {
2137         Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
2138         SculptSession *ss = CTX_data_active_object(C)->sculpt;
2139
2140         if(!sculpt_brush_stroke_init(C, op->reports))
2141                 return OPERATOR_CANCELLED;
2142
2143         op->customdata = paint_stroke_new(C, sculpt_stroke_get_location, sculpt_stroke_test_start,
2144                                           sculpt_stroke_update_step, sculpt_stroke_done);
2145
2146         sculpt_update_cache_invariants(sd, ss, C, op);
2147
2148         paint_stroke_exec(C, op);
2149
2150         sculpt_flush_update(C);
2151         sculpt_cache_free(ss->cache);
2152
2153         return OPERATOR_FINISHED;
2154 }
2155
2156 static void SCULPT_OT_brush_stroke(wmOperatorType *ot)
2157 {
2158         ot->flag |= OPTYPE_REGISTER;
2159
2160         /* identifiers */
2161         ot->name= "Sculpt Mode";
2162         ot->idname= "SCULPT_OT_brush_stroke";
2163         
2164         /* api callbacks */
2165         ot->invoke= sculpt_brush_stroke_invoke;
2166         ot->modal= paint_stroke_modal;
2167         ot->exec= sculpt_brush_stroke_exec;
2168         ot->poll= sculpt_poll;
2169         
2170         /* flags (sculpt does own undo? (ton) */
2171         ot->flag= OPTYPE_REGISTER|OPTYPE_BLOCKING;
2172
2173         /* properties */
2174         RNA_def_collection_runtime(ot->srna, "stroke", &RNA_OperatorStrokeElement, "Stroke", "");
2175
2176         /* If the object has a scaling factor, brushes also need to be scaled
2177            to work as expected. */
2178         RNA_def_float_vector(ot->srna, "scale", 3, NULL, 0.0f, FLT_MAX, "Scale", "", 0.0f, 1000.0f);
2179
2180         RNA_def_int(ot->srna, "flag", 0, 0, INT_MAX, "flag", "", 0, INT_MAX);
2181
2182         /* For mirror modifiers */
2183         RNA_def_float_vector(ot->srna, "clip_tolerance", 3, NULL, 0.0f, FLT_MAX, "clip_tolerance", "", 0.0f, 1000.0f);
2184
2185         /* The initial 2D location of the mouse */
2186         RNA_def_float_vector(ot->srna, "initial_mouse", 2, NULL, INT_MIN, INT_MAX, "initial_mouse", "", INT_MIN, INT_MAX);
2187
2188         RNA_def_boolean(ot->srna, "ignore_background_click", 0,
2189                         "Ignore Background Click",
2190                         "Clicks on the background don't start the stroke");
2191 }
2192
2193 /**** Reset the copy of the mesh that is being sculpted on (currently just for the layer brush) ****/
2194
2195 static int sculpt_set_persistent_base(bContext *C, wmOperator *op)
2196 {
2197         SculptSession *ss = CTX_data_active_object(C)->sculpt;
2198
2199         if(ss) {
2200                 if(ss->layer_co)
2201                         MEM_freeN(ss->layer_co);
2202                 ss->layer_co = NULL;
2203         }
2204
2205         return OPERATOR_FINISHED;
2206 }
2207
2208 static void SCULPT_OT_set_persistent_base(wmOperatorType *ot)
2209 {
2210         /* identifiers */
2211         ot->name= "Set Persistent Base";
2212         ot->idname= "SCULPT_OT_set_persistent_base";
2213         
2214         /* api callbacks */
2215         ot->exec= sculpt_set_persistent_base;
2216         ot->poll= sculpt_mode_poll;
2217         
2218         ot->flag= OPTYPE_REGISTER;
2219 }
2220
2221 /**** Toggle operator for turning sculpt mode on or off ****/
2222
2223 static void sculpt_init_session(Scene *scene, Object *ob)
2224 {
2225         ob->sculpt = MEM_callocN(sizeof(SculptSession), "sculpt session");
2226         
2227         sculpt_update_mesh_elements(scene, ob, 0);
2228
2229         if(ob->sculpt->refkb)
2230                 sculpt_key_to_mesh(ob->sculpt->refkb, ob);
2231 }
2232
2233 static int sculpt_toggle_mode(bContext *C, wmOperator *op)
2234 {
2235         Scene *scene = CTX_data_scene(C);
2236         ToolSettings *ts = CTX_data_tool_settings(C);
2237         Object *ob = CTX_data_active_object(C);
2238         MultiresModifierData *mmd = sculpt_multires_active(scene, ob);
2239         int flush_recalc= 0;
2240
2241         /* multires in sculpt mode could have different from object mode subdivision level */
2242         flush_recalc |= mmd && mmd->sculptlvl != mmd->lvl;
2243         /* if object has got active modifiers, it's dm could be different in sculpt mode  */
2244         //flush_recalc |= sculpt_modifiers_active(scene, ob);
2245
2246         if(ob->mode & OB_MODE_SCULPT) {
2247                 if(mmd)
2248                         multires_force_update(ob);
2249
2250                 if(flush_recalc)
2251                         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
2252
2253                 /* Leave sculptmode */
2254                 ob->mode &= ~OB_MODE_SCULPT;
2255
2256                 free_sculptsession(ob);
2257         }
2258         else {
2259                 /* Enter sculptmode */
2260                 ob->mode |= OB_MODE_SCULPT;
2261
2262                 if(flush_recalc)
2263                         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
2264                 
2265                 /* Create persistent sculpt mode data */
2266                 if(!ts->sculpt)
2267                         ts->sculpt = MEM_callocN(sizeof(Sculpt), "sculpt mode data");
2268
2269                 /* Create sculpt mode session data */
2270                 if(ob->sculpt)
2271                         free_sculptsession(ob);
2272
2273                 sculpt_init_session(scene, ob);
2274
2275                 paint_init(&ts->sculpt->paint, PAINT_CURSOR_SCULPT);
2276                 
2277                 paint_cursor_start(C, sculpt_poll);
2278         }
2279
2280         WM_event_add_notifier(C, NC_SCENE|ND_MODE, CTX_data_scene(C));
2281
2282         return OPERATOR_FINISHED;
2283 }
2284
2285 static void SCULPT_OT_sculptmode_toggle(wmOperatorType *ot)
2286 {
2287         /* identifiers */
2288         ot->name= "Sculpt Mode";
2289         ot->idname= "SCULPT_OT_sculptmode_toggle";
2290         
2291         /* api callbacks */
2292         ot->exec= sculpt_toggle_mode;
2293         ot->poll= ED_operator_object_active;
2294         
2295         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2296 }
2297
2298 void ED_operatortypes_sculpt()
2299 {
2300         WM_operatortype_append(SCULPT_OT_radial_control);
2301         WM_operatortype_append(SCULPT_OT_brush_stroke);
2302         WM_operatortype_append(SCULPT_OT_sculptmode_toggle);
2303         WM_operatortype_append(SCULPT_OT_set_persistent_base);
2304 }
2305