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