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