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