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