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