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