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