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