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