4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2006 by Nicholas Bishop
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): Jason Wilkins, Tom Musgrove.
27 * ***** END GPL LICENSE BLOCK *****
29 * Implements the Sculpt Mode tools
33 /** \file blender/editors/sculpt_paint/sculpt.c
38 #include "MEM_guardedalloc.h"
41 #include "BLI_blenlib.h"
42 #include "BLI_utildefines.h"
43 #include "BLI_dynstr.h"
44 #include "BLI_ghash.h"
46 #include "BLI_threads.h"
47 #include "BLI_editVert.h"
50 #include "DNA_meshdata_types.h"
51 #include "DNA_object_types.h"
52 #include "DNA_scene_types.h"
53 #include "DNA_brush_types.h"
55 #include "BKE_brush.h"
56 #include "BKE_cdderivedmesh.h"
57 #include "BKE_context.h"
58 #include "BKE_depsgraph.h"
60 #include "BKE_library.h"
62 #include "BKE_modifier.h"
63 #include "BKE_multires.h"
64 #include "BKE_paint.h"
65 #include "BKE_report.h"
66 #include "BKE_lattice.h" /* for armature_deform_verts */
69 #include "BIF_glutil.h"
74 #include "ED_sculpt.h"
75 #include "ED_screen.h"
76 #include "ED_view3d.h"
77 #include "ED_util.h" /* for crazyspace correction */
78 #include "paint_intern.h"
79 #include "sculpt_intern.h"
81 #include "RNA_access.h"
82 #include "RNA_define.h"
84 #include "RE_render_ext.h"
86 #include "GPU_buffers.h"
96 void ED_sculpt_force_update(bContext *C)
98 Object *ob= CTX_data_active_object(C);
100 if(ob && (ob->mode & OB_MODE_SCULPT))
101 multires_force_update(ob);
104 void ED_sculpt_modifiers_changed(Object *ob)
106 SculptSession *ss= ob->sculpt;
109 /* we free pbvh on changes, except during sculpt since it can't deal with
110 changing PVBH node organization, we hope topology does not change in
111 the meantime .. weak */
113 BLI_pbvh_free(ss->pbvh);
117 sculpt_free_deformMats(ob->sculpt);
122 BLI_pbvh_search_gather(ss->pbvh, NULL, NULL, &nodes, &totnode);
124 for(n = 0; n < totnode; n++)
125 BLI_pbvh_node_mark_update(nodes[n]);
131 /* Sculpt mode handles multires differently from regular meshes, but only if
132 it's the last modifier on the stack and it is not on the first level */
133 struct MultiresModifierData *sculpt_multires_active(Scene *scene, Object *ob)
135 Mesh *me= (Mesh*)ob->data;
138 if(!CustomData_get_layer(&me->fdata, CD_MDISPS)) {
139 /* multires can't work without displacement layer */
143 for(md= modifiers_getVirtualModifierList(ob); md; md= md->next) {
144 if(md->type == eModifierType_Multires) {
145 MultiresModifierData *mmd= (MultiresModifierData*)md;
147 if(!modifier_isEnabled(scene, md, eModifierMode_Realtime))
150 if(mmd->sculptlvl > 0) return mmd;
158 /* Check if there are any active modifiers in stack (used for flushing updates at enter/exit sculpt mode) */
159 static int sculpt_has_active_modifiers(Scene *scene, Object *ob)
163 md= modifiers_getVirtualModifierList(ob);
165 /* exception for shape keys because we can edit those */
166 for(; md; md= md->next) {
167 if(modifier_isEnabled(scene, md, eModifierMode_Realtime))
174 /* Checks if there are any supported deformation modifiers active */
175 int sculpt_modifiers_active(Scene *scene, Object *ob)
178 Mesh *me= (Mesh*)ob->data;
179 MultiresModifierData *mmd= sculpt_multires_active(scene, ob);
183 /* non-locked shaoe keys could be handled in the same way as deformed mesh */
184 if((ob->shapeflag&OB_SHAPE_LOCK)==0 && me->key && ob->shapenr)
187 md= modifiers_getVirtualModifierList(ob);
189 /* exception for shape keys because we can edit those */
190 for(; md; md= md->next) {
191 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
193 if(!modifier_isEnabled(scene, md, eModifierMode_Realtime)) continue;
194 if(md->type==eModifierType_ShapeKey) continue;
196 if(mti->type==eModifierTypeType_OnlyDeform)
203 typedef enum StrokeFlags {
209 /* Cache stroke properties. Used because
210 RNA property lookup isn't particularly fast.
212 For descriptions of these settings, check the operator properties.
214 typedef struct StrokeCache {
216 float initial_radius;
219 float clip_tolerance[3];
220 float initial_mouse[2];
224 float radius_squared;
225 //float traced_location[3];
226 float true_location[3];
236 /* The rest is temporary storage that isn't saved as a property */
238 int first_time; /* Beginning of stroke may do some things special */
246 float (*face_norms)[3]; /* Copy of the mesh faces' normals */
247 float special_rotation; /* Texture rotation (radians) for anchored and rake modes */
248 int pixel_radius, previous_pixel_radius;
249 float grab_delta[3], grab_delta_symmetry[3];
250 float old_grab_location[3], orig_grab_location[3];
252 int symmetry; /* Symmetry index between 0 and 7 bit combo 0 is Brush only;
253 1 is X mirror; 2 is Y mirror; 3 is XY; 4 is Z; 5 is XZ; 6 is YZ; 7 is XYZ */
254 int mirror_symmetry_pass; /* the symmetry pass we are currently on between 0 and 7*/
255 float true_view_normal[3];
256 float view_normal[3];
257 float last_area_normal[3];
258 float last_center[3];
259 int radial_symmetry_pass;
260 float symm_rot_mat[4][4];
261 float symm_rot_mat_inv[4][4];
262 float last_rake[2]; /* Last location of updating rake rotation */
265 float vertex_rotation;
267 char saved_active_brush_name[24];
270 float plane_trim_squared;
272 rcti previous_r; /* previous redraw rectangle */
277 /* Get a screen-space rectangle of the modified area */
278 static int sculpt_get_redraw_rect(ARegion *ar, RegionView3D *rv3d,
279 Object *ob, rcti *rect)
281 PBVH *pbvh= ob->sculpt->pbvh;
282 float bb_min[3], bb_max[3], pmat[4][4];
285 view3d_get_object_project_mat(rv3d, ob, pmat);
290 BLI_pbvh_redraw_BB(pbvh, bb_min, bb_max);
292 rect->xmin = rect->ymin = INT_MAX;
293 rect->xmax = rect->ymax = INT_MIN;
295 if(bb_min[0] > bb_max[0] || bb_min[1] > bb_max[1] || bb_min[2] > bb_max[2])
298 for(i = 0; i < 2; ++i) {
299 for(j = 0; j < 2; ++j) {
300 for(k = 0; k < 2; ++k) {
301 float vec[3], proj[2];
302 vec[0] = i ? bb_min[0] : bb_max[0];
303 vec[1] = j ? bb_min[1] : bb_max[1];
304 vec[2] = k ? bb_min[2] : bb_max[2];
305 view3d_project_float(ar, vec, proj, pmat);
306 rect->xmin = MIN2(rect->xmin, proj[0]);
307 rect->xmax = MAX2(rect->xmax, proj[0]);
308 rect->ymin = MIN2(rect->ymin, proj[1]);
309 rect->ymax = MAX2(rect->ymax, proj[1]);
314 if (rect->xmin < rect->xmax && rect->ymin < rect->ymax) {
315 /* expand redraw rect with redraw rect from previous step to prevent
316 partial-redraw issues caused by fast strokes. This is needed here (not in sculpt_flush_update)
317 as it was before because redraw rectangle should be the same in both of
318 optimized PBVH draw function and 3d view redraw (if not -- some mesh parts could
319 disapper from screen (sergey) */
320 SculptSession *ss = ob->sculpt;
323 if (!BLI_rcti_is_empty(&ss->cache->previous_r))
324 BLI_union_rcti(rect, &ss->cache->previous_r);
333 void sculpt_get_redraw_planes(float planes[4][4], ARegion *ar,
334 RegionView3D *rv3d, Object *ob)
336 PBVH *pbvh= ob->sculpt->pbvh;
341 memset(&bb, 0, sizeof(BoundBox));
343 view3d_get_transformation(ar, rv3d, ob, &mats);
344 sculpt_get_redraw_rect(ar, rv3d,ob, &rect);
347 /* use some extra space just in case */
353 /* it was doing this before, allows to redraw a smaller
354 part of the screen but also gives artifaces .. */
361 view3d_calculate_clipping(&bb, planes, &mats, &rect);
362 mul_m4_fl(planes, -1.0f);
364 /* clear redraw flag from nodes */
366 BLI_pbvh_update(pbvh, PBVH_UpdateRedraw, NULL);
369 /************************ Brush Testing *******************/
371 typedef struct SculptBrushTest {
372 float radius_squared;
377 static void sculpt_brush_test_init(SculptSession *ss, SculptBrushTest *test)
379 test->radius_squared= ss->cache->radius_squared;
380 copy_v3_v3(test->location, ss->cache->location);
381 test->dist= 0.0f; /* just for initialize */
384 static int sculpt_brush_test(SculptBrushTest *test, float co[3])
386 float distsq = len_squared_v3v3(co, test->location);
388 if(distsq <= test->radius_squared) {
389 test->dist = sqrt(distsq);
397 static int sculpt_brush_test_sq(SculptBrushTest *test, float co[3])
399 float distsq = len_squared_v3v3(co, test->location);
401 if(distsq <= test->radius_squared) {
410 static int sculpt_brush_test_fast(SculptBrushTest *test, float co[3])
412 return len_squared_v3v3(co, test->location) <= test->radius_squared;
415 static int sculpt_brush_test_cube(SculptBrushTest *test, float co[3], float local[4][4])
417 static const float side = 0.70710678118654752440084436210485; // sqrt(.5);
421 mul_v3_m4v3(local_co, local, co);
423 local_co[0] = fabs(local_co[0]);
424 local_co[1] = fabs(local_co[1]);
425 local_co[2] = fabs(local_co[2]);
427 if (local_co[0] <= side && local_co[1] <= side && local_co[2] <= side) {
428 test->dist = MAX3(local_co[0], local_co[1], local_co[2]) / side;
437 static float frontface(Brush *brush, float sculpt_normal[3], short no[3], float fno[3])
439 if (brush->flag & BRUSH_FRONTFACE) {
445 normal_short_to_float_v3(tmp, no);
446 dot= dot_v3v3(tmp, sculpt_normal);
449 dot= dot_v3v3(fno, sculpt_normal);
451 return dot > 0 ? dot : 0;
460 static int sculpt_brush_test_cyl(SculptBrushTest *test, float co[3], float location[3], float an[3])
462 if (sculpt_brush_test_fast(test, co)) {
463 float t1[3], t2[3], t3[3], dist;
465 sub_v3_v3v3(t1, location, co);
466 sub_v3_v3v3(t2, x2, location);
468 cross_v3_v3v3(t3, an, t1);
470 dist = len_v3(t3)/len_v3(t2);
482 /* ===== Sculpting =====
487 static float overlapped_curve(Brush* br, float x)
490 const int n = 100 / br->spacing;
491 const float h = br->spacing / 50.0f;
492 const float x0 = x-1;
497 for (i= 0; i < n; i++) {
503 sum += brush_curve_strength(br, xx, 1);
509 static float integrate_overlap(Brush* br)
519 for(i= 0; i < m; i++) {
520 overlap = overlapped_curve(br, i*g);
529 /* Uses symm to selectively flip any axis of a coordinate. */
530 static void flip_coord(float out[3], float in[3], const char symm)
532 if(symm & SCULPT_SYMM_X)
536 if(symm & SCULPT_SYMM_Y)
540 if(symm & SCULPT_SYMM_Z)
546 static float calc_overlap(StrokeCache *cache, const char symm, const char axis, const float angle)
551 //flip_coord(mirror, cache->traced_location, symm);
552 flip_coord(mirror, cache->true_location, symm);
555 float mat[4][4]= MAT4_UNITY;
556 rotate_m4(mat, axis, angle);
557 mul_m4_v3(mat, mirror);
560 //distsq = len_squared_v3v3(mirror, cache->traced_location);
561 distsq = len_squared_v3v3(mirror, cache->true_location);
563 if (distsq <= 4.0f*(cache->radius_squared))
564 return (2.0f*(cache->radius) - sqrtf(distsq)) / (2.0f*(cache->radius));
569 static float calc_radial_symmetry_feather(Sculpt *sd, StrokeCache *cache, const char symm, const char axis)
575 for(i = 1; i < sd->radial_symm[axis-'X']; ++i) {
576 const float angle = 2*M_PI*i/sd->radial_symm[axis-'X'];
577 overlap += calc_overlap(cache, symm, axis, angle);
583 static float calc_symmetry_feather(Sculpt *sd, StrokeCache* cache)
585 if (sd->flags & SCULPT_SYMMETRY_FEATHER) {
587 int symm = cache->symmetry;
591 for (i = 0; i <= symm; i++) {
592 if(i == 0 || (symm & i && (symm != 5 || i != 3) && (symm != 6 || (i != 3 && i != 5)))) {
594 overlap += calc_overlap(cache, i, 0, 0);
596 overlap += calc_radial_symmetry_feather(sd, cache, i, 'X');
597 overlap += calc_radial_symmetry_feather(sd, cache, i, 'Y');
598 overlap += calc_radial_symmetry_feather(sd, cache, i, 'Z');
609 /* Return modified brush strength. Includes the direction of the brush, positive
610 values pull vertices, negative values push. Uses tablet pressure and a
611 special multiplier found experimentally to scale the strength factor. */
612 static float brush_strength(Sculpt *sd, StrokeCache *cache, float feather)
614 Brush *brush = paint_brush(&sd->paint);
616 /* Primary strength input; square it to make lower values more sensitive */
617 const float root_alpha = brush_alpha(brush);
618 float alpha = root_alpha*root_alpha;
619 float dir = brush->flag & BRUSH_DIR_IN ? -1 : 1;
620 float pressure = brush_use_alpha_pressure(brush) ? cache->pressure : 1;
621 float pen_flip = cache->pen_flip ? -1 : 1;
622 float invert = cache->invert ? -1 : 1;
623 float accum = integrate_overlap(brush);
624 float overlap = (brush->flag & BRUSH_SPACE_ATTEN && brush->flag & BRUSH_SPACE && !(brush->flag & BRUSH_ANCHORED)) && (brush->spacing < 100) ? 1.0f/accum : 1; // spacing is integer percentage of radius, divide by 50 to get normalized diameter
625 float flip = dir * invert * pen_flip;
627 switch(brush->sculpt_tool){
628 case SCULPT_TOOL_CLAY:
629 case SCULPT_TOOL_CLAY_TUBES:
630 case SCULPT_TOOL_DRAW:
631 case SCULPT_TOOL_LAYER:
632 return alpha * flip * pressure * overlap * feather;
634 case SCULPT_TOOL_CREASE:
635 case SCULPT_TOOL_BLOB:
636 return alpha * flip * pressure * overlap * feather;
638 case SCULPT_TOOL_INFLATE:
640 return 0.250f * alpha * flip * pressure * overlap * feather;
643 return 0.125f * alpha * flip * pressure * overlap * feather;
646 case SCULPT_TOOL_FILL:
647 case SCULPT_TOOL_SCRAPE:
648 case SCULPT_TOOL_FLATTEN:
650 overlap = (1+overlap) / 2;
651 return alpha * flip * pressure * overlap * feather;
654 /* reduce strength for DEEPEN, PEAKS, and CONTRAST */
655 return 0.5f * alpha * flip * pressure * overlap * feather;
658 case SCULPT_TOOL_SMOOTH:
659 return alpha * pressure * feather;
661 case SCULPT_TOOL_PINCH:
663 return alpha * flip * pressure * overlap * feather;
666 return 0.25f * alpha * flip * pressure * overlap * feather;
669 case SCULPT_TOOL_NUDGE:
670 overlap = (1+overlap) / 2;
671 return alpha * pressure * overlap * feather;
673 case SCULPT_TOOL_THUMB:
674 return alpha*pressure*feather;
676 case SCULPT_TOOL_SNAKE_HOOK:
679 case SCULPT_TOOL_GRAB:
680 case SCULPT_TOOL_ROTATE:
688 /* Return a multiplier for brush strength on a particular vertex. */
689 static float tex_strength(SculptSession *ss, Brush *br, float *point, const float len)
691 MTex *mtex = &br->mtex;
697 else if(mtex->brush_map_mode == MTEX_MAP_MODE_3D) {
700 /* Get strength by feeding the vertex
701 location directly into a texture */
702 externtex(mtex, point, &avg,
703 &jnk, &jnk, &jnk, &jnk, 0);
705 else if(ss->texcache) {
706 float rotation = -mtex->rot;
707 float x, y, point_2d[3];
710 /* if the active area is being applied for symmetry, flip it
711 across the symmetry axis and rotate it back to the orignal
712 position in order to project it. This insures that the
713 brush texture will be oriented correctly. */
715 flip_coord(point_2d, point, ss->cache->mirror_symmetry_pass);
717 if (ss->cache->radial_symmetry_pass)
718 mul_m4_v3(ss->cache->symm_rot_mat_inv, point_2d);
720 projectf(ss->cache->mats, point_2d, point_2d);
722 /* if fixed mode, keep coordinates relative to mouse */
723 if(mtex->brush_map_mode == MTEX_MAP_MODE_FIXED) {
724 rotation += ss->cache->special_rotation;
726 point_2d[0] -= ss->cache->tex_mouse[0];
727 point_2d[1] -= ss->cache->tex_mouse[1];
729 radius = ss->cache->pixel_radius; // use pressure adjusted size for fixed mode
734 else /* else (mtex->brush_map_mode == MTEX_MAP_MODE_TILED),
735 leave the coordinates relative to the screen */
737 radius = brush_size(br); // use unadjusted size for tiled mode
739 x = point_2d[0] - ss->cache->vc->ar->winrct.xmin;
740 y = point_2d[1] - ss->cache->vc->ar->winrct.ymin;
743 x /= ss->cache->vc->ar->winx;
744 y /= ss->cache->vc->ar->winy;
746 if (mtex->brush_map_mode == MTEX_MAP_MODE_TILED) {
751 x *= ss->cache->vc->ar->winx / radius;
752 y *= ss->cache->vc->ar->winy / radius;
754 /* it is probably worth optimizing for those cases where
755 the texture is not rotated by skipping the calls to
756 atan2, sqrtf, sin, and cos. */
757 if (rotation > 0.001f || rotation < -0.001f) {
758 const float angle = atan2f(y, x) + rotation;
759 const float flen = sqrtf(x*x + y*y);
761 x = flen * cosf(angle);
762 y = flen * sinf(angle);
765 x *= br->mtex.size[0];
766 y *= br->mtex.size[1];
768 x += br->mtex.ofs[0];
769 y += br->mtex.ofs[1];
771 avg = paint_get_tex_pixel(br, x, y);
774 avg += br->texture_sample_bias;
776 avg *= brush_curve_strength(br, len, ss->cache->radius); /* Falloff curve */
784 float radius_squared;
786 } SculptSearchSphereData;
788 /* Test AABB against sphere */
789 static int sculpt_search_sphere_cb(PBVHNode *node, void *data_v)
791 SculptSearchSphereData *data = data_v;
792 float *center = data->ss->cache->location, nearest[3];
793 float t[3], bb_min[3], bb_max[3];
797 BLI_pbvh_node_get_original_BB(node, bb_min, bb_max);
799 BLI_pbvh_node_get_BB(node, bb_min, bb_max);
801 for(i = 0; i < 3; ++i) {
802 if(bb_min[i] > center[i])
803 nearest[i] = bb_min[i];
804 else if(bb_max[i] < center[i])
805 nearest[i] = bb_max[i];
807 nearest[i] = center[i];
810 sub_v3_v3v3(t, center, nearest);
812 return dot_v3v3(t, t) < data->radius_squared;
815 /* Handles clipping against a mirror modifier and SCULPT_LOCK axis flags */
816 static void sculpt_clip(Sculpt *sd, SculptSession *ss, float *co, const float val[3])
821 if(sd->flags & (SCULPT_LOCK_X << i))
824 if((ss->cache->flag & (CLIP_X << i)) && (fabsf(co[i]) <= ss->cache->clip_tolerance[i]))
831 static void add_norm_if(float view_vec[3], float out[3], float out_flip[3], float fno[3])
833 if((dot_v3v3(view_vec, fno)) > 0) {
836 add_v3_v3(out_flip, fno); /* out_flip is used when out is {0,0,0} */
840 static void calc_area_normal(Sculpt *sd, Object *ob, float an[3], PBVHNode **nodes, int totnode)
842 SculptSession *ss = ob->sculpt;
845 float out_flip[3] = {0.0f, 0.0f, 0.0f};
847 (void)sd; /* unused w/o openmp */
851 #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
852 for(n=0; n<totnode; n++) {
854 SculptBrushTest test;
855 SculptUndoNode *unode;
856 float private_an[3] = {0.0f, 0.0f, 0.0f};
857 float private_out_flip[3] = {0.0f, 0.0f, 0.0f};
859 unode = sculpt_undo_push_node(ob, nodes[n]);
860 sculpt_brush_test_init(ss, &test);
862 if(ss->cache->original) {
863 BLI_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE) {
864 if(sculpt_brush_test_fast(&test, unode->co[vd.i])) {
867 normal_short_to_float_v3(fno, unode->no[vd.i]);
868 add_norm_if(ss->cache->view_normal, private_an, private_out_flip, fno);
871 BLI_pbvh_vertex_iter_end;
874 BLI_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE) {
875 if(sculpt_brush_test_fast(&test, vd.co)) {
879 normal_short_to_float_v3(fno, vd.no);
880 add_norm_if(ss->cache->view_normal, private_an, private_out_flip, fno);
883 add_norm_if(ss->cache->view_normal, private_an, private_out_flip, vd.fno);
887 BLI_pbvh_vertex_iter_end;
892 add_v3_v3(an, private_an);
893 add_v3_v3(out_flip, private_out_flip);
898 copy_v3_v3(an, out_flip);
903 /* This initializes the faces to be moved for this sculpt for draw/layer/flatten; then it
904 finds average normal for all active vertices - note that this is called once for each mirroring direction */
905 static void calc_sculpt_normal(Sculpt *sd, Object *ob, float an[3], PBVHNode **nodes, int totnode)
907 SculptSession *ss = ob->sculpt;
908 Brush *brush = paint_brush(&sd->paint);
910 if (ss->cache->mirror_symmetry_pass == 0 &&
911 ss->cache->radial_symmetry_pass == 0 &&
912 (ss->cache->first_time || !(brush->flag & BRUSH_ORIGINAL_NORMAL)))
914 switch (brush->sculpt_plane) {
915 case SCULPT_DISP_DIR_VIEW:
916 viewvector(ss->cache->vc->rv3d, ss->cache->vc->rv3d->twmat[3], an);
919 case SCULPT_DISP_DIR_X:
925 case SCULPT_DISP_DIR_Y:
931 case SCULPT_DISP_DIR_Z:
937 case SCULPT_DISP_DIR_AREA:
938 calc_area_normal(sd, ob, an, nodes, totnode);
944 copy_v3_v3(ss->cache->last_area_normal, an);
947 copy_v3_v3(an, ss->cache->last_area_normal);
948 flip_coord(an, an, ss->cache->mirror_symmetry_pass);
949 mul_m4_v3(ss->cache->symm_rot_mat, an);
953 /* For the smooth brush, uses the neighboring vertices around vert to calculate
954 a smoothed location for vert. Skips corner vertices (used by only one
956 static void neighbor_average(SculptSession *ss, float avg[3], const unsigned vert)
958 int i, skip= -1, total=0;
959 IndexNode *node= ss->fmap[vert].first;
960 char ncount= BLI_countlist(&ss->fmap[vert]);
963 avg[0] = avg[1] = avg[2] = 0;
965 /* Don't modify corner vertices */
967 if(ss->deform_cos) copy_v3_v3(avg, ss->deform_cos[vert]);
968 else copy_v3_v3(avg, ss->mvert[vert].co);
974 f= &ss->mface[node->index];
977 skip= (f->v1==vert?2:
983 for(i=0; i<(f->v4?4:3); ++i) {
984 if(i != skip && (ncount!=2 || BLI_countlist(&ss->fmap[(&f->v1)[i]]) <= 2)) {
985 if(ss->deform_cos) add_v3_v3(avg, ss->deform_cos[(&f->v1)[i]]);
986 else add_v3_v3(avg, ss->mvert[(&f->v1)[i]].co);
995 mul_v3_fl(avg, 1.0f / total);
997 if(ss->deform_cos) copy_v3_v3(avg, ss->deform_cos[vert]);
998 else copy_v3_v3(avg, ss->mvert[vert].co);
1002 static void do_mesh_smooth_brush(Sculpt *sd, SculptSession *ss, PBVHNode *node, float bstrength)
1004 Brush *brush = paint_brush(&sd->paint);
1006 SculptBrushTest test;
1008 CLAMP(bstrength, 0.0f, 1.0f);
1010 sculpt_brush_test_init(ss, &test);
1012 BLI_pbvh_vertex_iter_begin(ss->pbvh, node, vd, PBVH_ITER_UNIQUE) {
1013 if(sculpt_brush_test(&test, vd.co)) {
1014 const float fade = bstrength*tex_strength(ss, brush, vd.co, test.dist)*frontface(brush, ss->cache->view_normal, vd.no, vd.fno);
1015 float avg[3], val[3];
1017 neighbor_average(ss, avg, vd.vert_indices[vd.i]);
1018 sub_v3_v3v3(val, avg, vd.co);
1019 mul_v3_fl(val, fade);
1021 add_v3_v3(val, vd.co);
1023 sculpt_clip(sd, ss, vd.co, val);
1026 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
1029 BLI_pbvh_vertex_iter_end;
1032 static void do_multires_smooth_brush(Sculpt *sd, SculptSession *ss, PBVHNode *node, float bstrength)
1034 Brush *brush = paint_brush(&sd->paint);
1035 SculptBrushTest test;
1036 DMGridData **griddata, *data;
1037 DMGridAdjacency *gridadj, *adj;
1038 float (*tmpgrid)[3], (*tmprow)[3];
1040 int *grid_indices, totgrid, gridsize, i, x, y;
1042 sculpt_brush_test_init(ss, &test);
1044 CLAMP(bstrength, 0.0f, 1.0f);
1046 BLI_pbvh_node_get_grids(ss->pbvh, node, &grid_indices, &totgrid,
1047 NULL, &gridsize, &griddata, &gridadj);
1049 #pragma omp critical
1051 tmpgrid= MEM_mallocN(sizeof(float)*3*gridsize*gridsize, "tmpgrid");
1052 tmprow= MEM_mallocN(sizeof(float)*3*gridsize, "tmprow");
1055 for(i = 0; i < totgrid; ++i) {
1056 data = griddata[grid_indices[i]];
1057 adj = &gridadj[grid_indices[i]];
1059 memset(tmpgrid, 0, sizeof(float)*3*gridsize*gridsize);
1061 for (y= 0; y < gridsize-1; y++) {
1065 add_v3_v3v3(tmprow[0], data[v1].co, data[v1+gridsize].co);
1067 for (x= 0; x < gridsize-1; x++) {
1068 v1 = x + y*gridsize;
1073 add_v3_v3v3(tmprow[x+1], data[v2].co, data[v4].co);
1074 add_v3_v3v3(tmp, tmprow[x+1], tmprow[x]);
1076 add_v3_v3(tmpgrid[v1], tmp);
1077 add_v3_v3(tmpgrid[v2], tmp);
1078 add_v3_v3(tmpgrid[v3], tmp);
1079 add_v3_v3(tmpgrid[v4], tmp);
1083 /* blend with existing coordinates */
1084 for(y = 0; y < gridsize; ++y) {
1085 for(x = 0; x < gridsize; ++x) {
1090 if(x == 0 && adj->index[0] == -1)
1093 if(x == gridsize - 1 && adj->index[2] == -1)
1096 if(y == 0 && adj->index[3] == -1)
1099 if(y == gridsize - 1 && adj->index[1] == -1)
1102 index = x + y*gridsize;
1104 fno= data[index].no;
1106 if(sculpt_brush_test(&test, co)) {
1107 const float fade = bstrength*tex_strength(ss, brush, co, test.dist)*frontface(brush, ss->cache->view_normal, NULL, fno);
1111 avg = tmpgrid[x + y*gridsize];
1115 if(x == 0 || x == gridsize - 1)
1118 if(y == 0 || y == gridsize - 1)
1123 sub_v3_v3v3(val, avg, co);
1124 mul_v3_fl(val, fade);
1128 sculpt_clip(sd, ss, co, val);
1134 #pragma omp critical
1141 static void smooth(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode, float bstrength)
1143 SculptSession *ss = ob->sculpt;
1144 const int max_iterations = 4;
1145 const float fract = 1.0f/max_iterations;
1146 int iteration, n, count;
1149 CLAMP(bstrength, 0, 1);
1151 count = (int)(bstrength*max_iterations);
1152 last = max_iterations*(bstrength - count*fract);
1154 for(iteration = 0; iteration <= count; ++iteration) {
1155 #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
1156 for(n=0; n<totnode; n++) {
1158 do_multires_smooth_brush(sd, ss, nodes[n], iteration != count ? 1.0f : last);
1161 do_mesh_smooth_brush(sd, ss, nodes[n], iteration != count ? 1.0f : last);
1165 multires_stitch_grids(ob);
1169 static void do_smooth_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
1171 SculptSession *ss = ob->sculpt;
1172 smooth(sd, ob, nodes, totnode, ss->cache->bstrength);
1175 static void do_draw_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
1177 SculptSession *ss = ob->sculpt;
1178 Brush *brush = paint_brush(&sd->paint);
1179 float offset[3], area_normal[3];
1180 float bstrength= ss->cache->bstrength;
1183 calc_sculpt_normal(sd, ob, area_normal, nodes, totnode);
1185 /* offset with as much as possible factored in already */
1186 mul_v3_v3fl(offset, area_normal, ss->cache->radius);
1187 mul_v3_v3(offset, ss->cache->scale);
1188 mul_v3_fl(offset, bstrength);
1190 /* threaded loop over nodes */
1191 #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
1192 for(n=0; n<totnode; n++) {
1194 SculptBrushTest test;
1197 proxy= BLI_pbvh_node_add_proxy(ss->pbvh, nodes[n])->co;
1199 sculpt_brush_test_init(ss, &test);
1201 BLI_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE) {
1202 if (sculpt_brush_test(&test, vd.co)) {
1203 //if(sculpt_brush_test_cyl(&test, vd.co, ss->cache->location, area_normal)) {
1205 float fade = tex_strength(ss, brush, vd.co, test.dist)*frontface(brush, area_normal, vd.no, vd.fno);
1207 mul_v3_v3fl(proxy[vd.i], offset, fade);
1210 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
1213 BLI_pbvh_vertex_iter_end;
1217 static void do_crease_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
1219 SculptSession *ss = ob->sculpt;
1220 Brush *brush = paint_brush(&sd->paint);
1221 float offset[3], area_normal[3];
1222 float bstrength= ss->cache->bstrength;
1223 float flippedbstrength, crease_correction;
1226 calc_sculpt_normal(sd, ob, area_normal, nodes, totnode);
1228 /* offset with as much as possible factored in already */
1229 mul_v3_v3fl(offset, area_normal, ss->cache->radius);
1230 mul_v3_v3(offset, ss->cache->scale);
1231 mul_v3_fl(offset, bstrength);
1233 /* we divide out the squared alpha and multiply by the squared crease to give us the pinch strength */
1235 if(brush_alpha(brush) > 0.0f)
1236 crease_correction = brush->crease_pinch_factor*brush->crease_pinch_factor/(brush_alpha(brush)*brush_alpha(brush));
1238 crease_correction = brush->crease_pinch_factor*brush->crease_pinch_factor;
1240 /* we always want crease to pinch or blob to relax even when draw is negative */
1241 flippedbstrength = (bstrength < 0) ? -crease_correction*bstrength : crease_correction*bstrength;
1243 if(brush->sculpt_tool == SCULPT_TOOL_BLOB) flippedbstrength *= -1.0f;
1245 /* threaded loop over nodes */
1246 #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
1247 for(n=0; n<totnode; n++) {
1249 SculptBrushTest test;
1252 proxy= BLI_pbvh_node_add_proxy(ss->pbvh, nodes[n])->co;
1254 sculpt_brush_test_init(ss, &test);
1256 BLI_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE) {
1257 if(sculpt_brush_test(&test, vd.co)) {
1259 const float fade = tex_strength(ss, brush, vd.co, test.dist)*frontface(brush, area_normal, vd.no, vd.fno);
1263 /* first we pinch */
1264 sub_v3_v3v3(val1, test.location, vd.co);
1265 //mul_v3_v3(val1, ss->cache->scale);
1266 mul_v3_fl(val1, fade*flippedbstrength);
1269 mul_v3_v3fl(val2, offset, fade);
1271 add_v3_v3v3(proxy[vd.i], val1, val2);
1274 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
1277 BLI_pbvh_vertex_iter_end;
1281 static void do_pinch_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
1283 SculptSession *ss = ob->sculpt;
1284 Brush *brush = paint_brush(&sd->paint);
1285 float bstrength= ss->cache->bstrength;
1288 #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
1289 for(n=0; n<totnode; n++) {
1291 SculptBrushTest test;
1294 proxy= BLI_pbvh_node_add_proxy(ss->pbvh, nodes[n])->co;
1296 sculpt_brush_test_init(ss, &test);
1298 BLI_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE) {
1299 if(sculpt_brush_test(&test, vd.co)) {
1300 float fade = bstrength*tex_strength(ss, brush, vd.co, test.dist)*frontface(brush, ss->cache->view_normal, vd.no, vd.fno);
1303 sub_v3_v3v3(val, test.location, vd.co);
1304 mul_v3_v3fl(proxy[vd.i], val, fade);
1307 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
1310 BLI_pbvh_vertex_iter_end;
1314 static void do_grab_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
1316 SculptSession *ss = ob->sculpt;
1317 Brush *brush= paint_brush(&sd->paint);
1318 float bstrength= ss->cache->bstrength;
1319 float grab_delta[3], an[3];
1323 if (brush->normal_weight > 0 || brush->flag & BRUSH_FRONTFACE) {
1325 /* grab brush requires to test on original data */
1326 SWAP(int, ss->cache->original, cache);
1327 calc_sculpt_normal(sd, ob, an, nodes, totnode);
1328 SWAP(int, ss->cache->original, cache);
1331 copy_v3_v3(grab_delta, ss->cache->grab_delta_symmetry);
1333 len = len_v3(grab_delta);
1335 if (brush->normal_weight > 0) {
1336 mul_v3_fl(an, len*brush->normal_weight);
1337 mul_v3_fl(grab_delta, 1.0f - brush->normal_weight);
1338 add_v3_v3(grab_delta, an);
1341 #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
1342 for(n=0; n<totnode; n++) {
1344 SculptUndoNode* unode;
1345 SculptBrushTest test;
1350 unode= sculpt_undo_push_node(ob, nodes[n]);
1354 proxy= BLI_pbvh_node_add_proxy(ss->pbvh, nodes[n])->co;
1356 sculpt_brush_test_init(ss, &test);
1358 BLI_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE) {
1359 if(sculpt_brush_test(&test, origco[vd.i])) {
1360 const float fade = bstrength*tex_strength(ss, brush, origco[vd.i], test.dist)*frontface(brush, an, origno[vd.i], NULL);
1362 mul_v3_v3fl(proxy[vd.i], grab_delta, fade);
1365 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
1368 BLI_pbvh_vertex_iter_end;
1372 static void do_nudge_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
1374 SculptSession *ss = ob->sculpt;
1375 Brush *brush = paint_brush(&sd->paint);
1376 float bstrength = ss->cache->bstrength;
1377 float grab_delta[3];
1380 float tmp[3], cono[3];
1382 copy_v3_v3(grab_delta, ss->cache->grab_delta_symmetry);
1384 calc_sculpt_normal(sd, ob, an, nodes, totnode);
1386 cross_v3_v3v3(tmp, an, grab_delta);
1387 cross_v3_v3v3(cono, tmp, an);
1389 #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
1390 for(n = 0; n < totnode; n++) {
1392 SculptBrushTest test;
1395 proxy= BLI_pbvh_node_add_proxy(ss->pbvh, nodes[n])->co;
1397 sculpt_brush_test_init(ss, &test);
1399 BLI_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE) {
1400 if(sculpt_brush_test(&test, vd.co)) {
1401 const float fade = bstrength*tex_strength(ss, brush, vd.co, test.dist)*frontface(brush, an, vd.no, vd.fno);
1403 mul_v3_v3fl(proxy[vd.i], cono, fade);
1406 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
1409 BLI_pbvh_vertex_iter_end;
1413 static void do_snake_hook_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
1415 SculptSession *ss = ob->sculpt;
1416 Brush *brush = paint_brush(&sd->paint);
1417 float bstrength = ss->cache->bstrength;
1418 float grab_delta[3], an[3];
1422 if (brush->normal_weight > 0 || brush->flag & BRUSH_FRONTFACE)
1423 calc_sculpt_normal(sd, ob, an, nodes, totnode);
1425 copy_v3_v3(grab_delta, ss->cache->grab_delta_symmetry);
1427 len = len_v3(grab_delta);
1430 negate_v3(grab_delta);
1432 if (brush->normal_weight > 0) {
1433 mul_v3_fl(an, len*brush->normal_weight);
1434 mul_v3_fl(grab_delta, 1.0f - brush->normal_weight);
1435 add_v3_v3(grab_delta, an);
1438 #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
1439 for(n = 0; n < totnode; n++) {
1441 SculptBrushTest test;
1444 proxy= BLI_pbvh_node_add_proxy(ss->pbvh, nodes[n])->co;
1446 sculpt_brush_test_init(ss, &test);
1448 BLI_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE) {
1449 if(sculpt_brush_test(&test, vd.co)) {
1450 const float fade = bstrength*tex_strength(ss, brush, vd.co, test.dist)*frontface(brush, an, vd.no, vd.fno);
1452 mul_v3_v3fl(proxy[vd.i], grab_delta, fade);
1455 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
1458 BLI_pbvh_vertex_iter_end;
1462 static void do_thumb_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
1464 SculptSession *ss = ob->sculpt;
1465 Brush *brush = paint_brush(&sd->paint);
1466 float bstrength = ss->cache->bstrength;
1467 float grab_delta[3];
1470 float tmp[3], cono[3];
1472 copy_v3_v3(grab_delta, ss->cache->grab_delta_symmetry);
1474 calc_sculpt_normal(sd, ob, an, nodes, totnode);
1476 cross_v3_v3v3(tmp, an, grab_delta);
1477 cross_v3_v3v3(cono, tmp, an);
1479 #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
1480 for(n = 0; n < totnode; n++) {
1482 SculptUndoNode* unode;
1483 SculptBrushTest test;
1488 unode= sculpt_undo_push_node(ob, nodes[n]);
1492 proxy= BLI_pbvh_node_add_proxy(ss->pbvh, nodes[n])->co;
1494 sculpt_brush_test_init(ss, &test);
1496 BLI_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE) {
1497 if(sculpt_brush_test(&test, origco[vd.i])) {
1498 const float fade = bstrength*tex_strength(ss, brush, origco[vd.i], test.dist)*frontface(brush, an, origno[vd.i], NULL);
1500 mul_v3_v3fl(proxy[vd.i], cono, fade);
1503 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
1506 BLI_pbvh_vertex_iter_end;
1510 static void do_rotate_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
1512 SculptSession *ss = ob->sculpt;
1513 Brush *brush= paint_brush(&sd->paint);
1514 float bstrength= ss->cache->bstrength;
1518 static const int flip[8] = { 1, -1, -1, 1, -1, 1, 1, -1 };
1519 float angle = ss->cache->vertex_rotation * flip[ss->cache->mirror_symmetry_pass];
1521 calc_sculpt_normal(sd, ob, an, nodes, totnode);
1523 axis_angle_to_mat3(m, an, angle);
1525 #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
1526 for(n=0; n<totnode; n++) {
1528 SculptUndoNode* unode;
1529 SculptBrushTest test;
1534 unode= sculpt_undo_push_node(ob, nodes[n]);
1538 proxy= BLI_pbvh_node_add_proxy(ss->pbvh, nodes[n])->co;
1540 sculpt_brush_test_init(ss, &test);
1542 BLI_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE) {
1543 if(sculpt_brush_test(&test, origco[vd.i])) {
1544 const float fade = bstrength*tex_strength(ss, brush, origco[vd.i], test.dist)*frontface(brush, an, origno[vd.i], NULL);
1546 mul_v3_m3v3(proxy[vd.i], m, origco[vd.i]);
1547 sub_v3_v3(proxy[vd.i], origco[vd.i]);
1548 mul_v3_fl(proxy[vd.i], fade);
1551 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
1554 BLI_pbvh_vertex_iter_end;
1558 static void do_layer_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
1560 SculptSession *ss = ob->sculpt;
1561 Brush *brush = paint_brush(&sd->paint);
1562 float bstrength= ss->cache->bstrength;
1563 float area_normal[3], offset[3];
1564 float lim= brush->height;
1570 calc_sculpt_normal(sd, ob, area_normal, nodes, totnode);
1572 mul_v3_v3v3(offset, ss->cache->scale, area_normal);
1574 #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
1575 for(n=0; n<totnode; n++) {
1577 SculptBrushTest test;
1578 SculptUndoNode *unode;
1579 float (*origco)[3], *layer_disp;
1580 //float (*proxy)[3]; // XXX layer brush needs conversion to proxy but its more complicated
1582 //proxy= BLI_pbvh_node_add_proxy(ss->pbvh, nodes[n])->co;
1584 unode= sculpt_undo_push_node(ob, nodes[n]);
1586 if(!unode->layer_disp)
1588 #pragma omp critical
1589 unode->layer_disp= MEM_callocN(sizeof(float)*unode->totvert, "layer disp");
1592 layer_disp= unode->layer_disp;
1594 sculpt_brush_test_init(ss, &test);
1596 BLI_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE) {
1597 if(sculpt_brush_test(&test, origco[vd.i])) {
1598 const float fade = bstrength*tex_strength(ss, brush, vd.co, test.dist)*frontface(brush, area_normal, vd.no, vd.fno);
1599 float *disp= &layer_disp[vd.i];
1604 /* Don't let the displacement go past the limit */
1605 if((lim < 0 && *disp < lim) || (lim >= 0 && *disp > lim))
1608 mul_v3_v3fl(val, offset, *disp);
1610 if(ss->layer_co && (brush->flag & BRUSH_PERSISTENT)) {
1611 int index= vd.vert_indices[vd.i];
1613 /* persistent base */
1614 add_v3_v3(val, ss->layer_co[index]);
1617 add_v3_v3(val, origco[vd.i]);
1620 sculpt_clip(sd, ss, vd.co, val);
1623 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
1626 BLI_pbvh_vertex_iter_end;
1630 static void do_inflate_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
1632 SculptSession *ss = ob->sculpt;
1633 Brush *brush = paint_brush(&sd->paint);
1634 float bstrength= ss->cache->bstrength;
1637 #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
1638 for(n=0; n<totnode; n++) {
1640 SculptBrushTest test;
1643 proxy= BLI_pbvh_node_add_proxy(ss->pbvh, nodes[n])->co;
1645 sculpt_brush_test_init(ss, &test);
1647 BLI_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE) {
1648 if(sculpt_brush_test(&test, vd.co)) {
1649 const float fade = bstrength*tex_strength(ss, brush, vd.co, test.dist)*frontface(brush, ss->cache->view_normal, vd.no, vd.fno);
1652 if(vd.fno) copy_v3_v3(val, vd.fno);
1653 else normal_short_to_float_v3(val, vd.no);
1655 mul_v3_fl(val, fade * ss->cache->radius);
1656 mul_v3_v3v3(proxy[vd.i], val, ss->cache->scale);
1659 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
1662 BLI_pbvh_vertex_iter_end;
1666 static void calc_flatten_center(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode, float fc[3])
1668 SculptSession *ss = ob->sculpt;
1673 (void)sd; /* unused w/o openmp */
1677 #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
1678 for(n=0; n<totnode; n++) {
1680 SculptBrushTest test;
1681 SculptUndoNode *unode;
1682 float private_fc[3] = {0.0f, 0.0f, 0.0f};
1683 int private_count = 0;
1685 unode = sculpt_undo_push_node(ob, nodes[n]);
1686 sculpt_brush_test_init(ss, &test);
1688 if(ss->cache->original) {
1689 BLI_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE) {
1690 if(sculpt_brush_test_fast(&test, unode->co[vd.i])) {
1691 add_v3_v3(private_fc, vd.co);
1695 BLI_pbvh_vertex_iter_end;
1698 BLI_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE) {
1699 if(sculpt_brush_test_fast(&test, vd.co)) {
1700 add_v3_v3(private_fc, vd.co);
1704 BLI_pbvh_vertex_iter_end;
1707 #pragma omp critical
1709 add_v3_v3(fc, private_fc);
1710 count += private_count;
1714 mul_v3_fl(fc, 1.0f / count);
1717 /* this calculates flatten center and area normal together,
1718 amortizing the memory bandwidth and loop overhead to calculate both at the same time */
1719 static void calc_area_normal_and_flatten_center(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode, float an[3], float fc[3])
1721 SculptSession *ss = ob->sculpt;
1725 float out_flip[3] = {0.0f, 0.0f, 0.0f};
1730 (void)sd; /* unused w/o openmp */
1738 #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
1739 for(n=0; n<totnode; n++) {
1741 SculptBrushTest test;
1742 SculptUndoNode *unode;
1743 float private_an[3] = {0.0f, 0.0f, 0.0f};
1744 float private_out_flip[3] = {0.0f, 0.0f, 0.0f};
1745 float private_fc[3] = {0.0f, 0.0f, 0.0f};
1746 int private_count = 0;
1748 unode = sculpt_undo_push_node(ob, nodes[n]);
1749 sculpt_brush_test_init(ss, &test);
1751 if(ss->cache->original) {
1752 BLI_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE) {
1753 if(sculpt_brush_test_fast(&test, unode->co[vd.i])) {
1757 normal_short_to_float_v3(fno, unode->no[vd.i]);
1758 add_norm_if(ss->cache->view_normal, private_an, private_out_flip, fno);
1761 add_v3_v3(private_fc, vd.co);
1765 BLI_pbvh_vertex_iter_end;
1768 BLI_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE) {
1769 if(sculpt_brush_test_fast(&test, vd.co)) {
1774 normal_short_to_float_v3(fno, vd.no);
1775 add_norm_if(ss->cache->view_normal, private_an, private_out_flip, fno);
1778 add_norm_if(ss->cache->view_normal, private_an, private_out_flip, vd.fno);
1782 add_v3_v3(private_fc, vd.co);
1786 BLI_pbvh_vertex_iter_end;
1789 #pragma omp critical
1792 add_v3_v3(an, private_an);
1793 add_v3_v3(out_flip, private_out_flip);
1796 add_v3_v3(fc, private_fc);
1797 count += private_count;
1803 copy_v3_v3(an, out_flip);
1809 mul_v3_fl(fc, 1.0f / count);
1816 static void calc_sculpt_plane(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode, float an[3], float fc[3])
1818 SculptSession *ss = ob->sculpt;
1819 Brush *brush = paint_brush(&sd->paint);
1821 if (ss->cache->mirror_symmetry_pass == 0 &&
1822 ss->cache->radial_symmetry_pass == 0 &&
1823 (ss->cache->first_time || !(brush->flag & BRUSH_ORIGINAL_NORMAL)))
1825 switch (brush->sculpt_plane) {
1826 case SCULPT_DISP_DIR_VIEW:
1827 viewvector(ss->cache->vc->rv3d, ss->cache->vc->rv3d->twmat[3], an);
1830 case SCULPT_DISP_DIR_X:
1836 case SCULPT_DISP_DIR_Y:
1842 case SCULPT_DISP_DIR_Z:
1848 case SCULPT_DISP_DIR_AREA:
1849 calc_area_normal_and_flatten_center(sd, ob, nodes, totnode, an, fc);
1856 /* flatten center has not been calculated yet if we are not using the area normal */
1857 if (brush->sculpt_plane != SCULPT_DISP_DIR_AREA)
1858 calc_flatten_center(sd, ob, nodes, totnode, fc);
1861 copy_v3_v3(ss->cache->last_area_normal, an);
1864 copy_v3_v3(ss->cache->last_center, fc);
1868 copy_v3_v3(an, ss->cache->last_area_normal);
1871 copy_v3_v3(fc, ss->cache->last_center);
1874 flip_coord(an, an, ss->cache->mirror_symmetry_pass);
1877 flip_coord(fc, fc, ss->cache->mirror_symmetry_pass);
1880 mul_m4_v3(ss->cache->symm_rot_mat, an);
1883 mul_m4_v3(ss->cache->symm_rot_mat, fc);
1887 /* Projects a point onto a plane along the plane's normal */
1888 static void point_plane_project(float intr[3], float co[3], float plane_normal[3], float plane_center[3])
1890 sub_v3_v3v3(intr, co, plane_center);
1891 mul_v3_v3fl(intr, plane_normal, dot_v3v3(plane_normal, intr));
1892 sub_v3_v3v3(intr, co, intr);
1895 static int plane_trim(StrokeCache *cache, Brush *brush, float val[3])
1897 return !(brush->flag & BRUSH_PLANE_TRIM) || (dot_v3v3(val, val) <= cache->radius_squared*cache->plane_trim_squared);
1900 static int plane_point_side_flip(float co[3], float plane_normal[3], float plane_center[3], int flip)
1905 sub_v3_v3v3(delta, co, plane_center);
1906 d = dot_v3v3(plane_normal, delta);
1913 static int plane_point_side(float co[3], float plane_normal[3], float plane_center[3])
1917 sub_v3_v3v3(delta, co, plane_center);
1918 return dot_v3v3(plane_normal, delta) <= 0.0f;
1921 static float get_offset(Sculpt *sd, SculptSession *ss)
1923 Brush* brush = paint_brush(&sd->paint);
1925 float rv = brush->plane_offset;
1927 if (brush->flag & BRUSH_OFFSET_PRESSURE) {
1928 rv *= ss->cache->pressure;
1934 static void do_flatten_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
1936 SculptSession *ss = ob->sculpt;
1937 Brush *brush = paint_brush(&sd->paint);
1939 float bstrength = ss->cache->bstrength;
1940 const float radius = ss->cache->radius;
1945 float offset = get_offset(sd, ss);
1953 calc_sculpt_plane(sd, ob, nodes, totnode, an, fc);
1955 displace = radius*offset;
1957 mul_v3_v3v3(temp, an, ss->cache->scale);
1958 mul_v3_fl(temp, displace);
1959 add_v3_v3(fc, temp);
1961 #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
1962 for(n = 0; n < totnode; n++) {
1964 SculptBrushTest test;
1967 proxy= BLI_pbvh_node_add_proxy(ss->pbvh, nodes[n])->co;
1969 sculpt_brush_test_init(ss, &test);
1971 BLI_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE) {
1972 if (sculpt_brush_test_sq(&test, vd.co)) {
1976 point_plane_project(intr, vd.co, an, fc);
1978 sub_v3_v3v3(val, intr, vd.co);
1980 if (plane_trim(ss->cache, brush, val)) {
1981 const float fade = bstrength*tex_strength(ss, brush, vd.co, sqrt(test.dist))*frontface(brush, an, vd.no, vd.fno);
1983 mul_v3_v3fl(proxy[vd.i], val, fade);
1986 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
1990 BLI_pbvh_vertex_iter_end;
1994 static void do_clay_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
1996 SculptSession *ss = ob->sculpt;
1997 Brush *brush = paint_brush(&sd->paint);
1999 float bstrength = ss->cache->bstrength;
2000 float radius = ss->cache->radius;
2001 float offset = get_offset(sd, ss);
2005 float an[3]; // area normal
2006 float fc[3]; // flatten center
2015 calc_sculpt_plane(sd, ob, nodes, totnode, an, fc);
2017 flip = bstrength < 0;
2020 bstrength = -bstrength;
2024 displace = radius * (0.25f+offset);
2026 mul_v3_v3v3(temp, an, ss->cache->scale);
2027 mul_v3_fl(temp, displace);
2028 add_v3_v3(fc, temp);
2030 //add_v3_v3v3(p, ss->cache->location, an);
2032 #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
2033 for (n = 0; n < totnode; n++) {
2035 SculptBrushTest test;
2038 proxy= BLI_pbvh_node_add_proxy(ss->pbvh, nodes[n])->co;
2040 sculpt_brush_test_init(ss, &test);
2042 BLI_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE) {
2043 if (sculpt_brush_test_sq(&test, vd.co)) {
2044 if (plane_point_side_flip(vd.co, an, fc, flip)) {
2045 //if (sculpt_brush_test_cyl(&test, vd.co, ss->cache->location, p)) {
2049 point_plane_project(intr, vd.co, an, fc);
2051 sub_v3_v3v3(val, intr, vd.co);
2053 if (plane_trim(ss->cache, brush, val)) {
2054 const float fade = bstrength*tex_strength(ss, brush, vd.co, sqrt(test.dist))*frontface(brush, an, vd.no, vd.fno);
2056 mul_v3_v3fl(proxy[vd.i], val, fade);
2059 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
2064 BLI_pbvh_vertex_iter_end;
2068 static void do_clay_tubes_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
2070 SculptSession *ss = ob->sculpt;
2071 Brush *brush = paint_brush(&sd->paint);
2073 float bstrength = ss->cache->bstrength;
2074 float radius = ss->cache->radius;
2075 float offset = get_offset(sd, ss);
2079 float sn[3]; // sculpt normal
2080 float an[3]; // area normal
2081 float fc[3]; // flatten center
2092 calc_sculpt_plane(sd, ob, nodes, totnode, sn, fc);
2094 if (brush->sculpt_plane != SCULPT_DISP_DIR_AREA || (brush->flag & BRUSH_ORIGINAL_NORMAL))
2095 calc_area_normal(sd, ob, an, nodes, totnode);
2099 if (ss->cache->first_time)
2100 return; // delay the first daub because grab delta is not setup
2102 flip = bstrength < 0;
2105 bstrength = -bstrength;
2109 displace = radius * (0.25f+offset);
2111 mul_v3_v3v3(temp, sn, ss->cache->scale);
2112 mul_v3_fl(temp, displace);
2113 add_v3_v3(fc, temp);
2115 cross_v3_v3v3(mat[0], an, ss->cache->grab_delta_symmetry); mat[0][3] = 0;
2116 cross_v3_v3v3(mat[1], an, mat[0]); mat[1][3] = 0;
2117 copy_v3_v3(mat[2], an); mat[2][3] = 0;
2118 copy_v3_v3(mat[3], ss->cache->location); mat[3][3] = 1;
2120 scale_m4_fl(scale, ss->cache->radius);
2121 mul_m4_m4m4(tmat, scale, mat);
2122 invert_m4_m4(mat, tmat);
2124 #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
2125 for (n = 0; n < totnode; n++) {
2127 SculptBrushTest test;
2130 proxy= BLI_pbvh_node_add_proxy(ss->pbvh, nodes[n])->co;
2132 sculpt_brush_test_init(ss, &test);
2134 BLI_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE) {
2135 if (sculpt_brush_test_cube(&test, vd.co, mat)) {
2136 if (plane_point_side_flip(vd.co, sn, fc, flip)) {
2140 point_plane_project(intr, vd.co, sn, fc);
2142 sub_v3_v3v3(val, intr, vd.co);
2144 if (plane_trim(ss->cache, brush, val)) {
2145 const float fade = bstrength*tex_strength(ss, brush, vd.co, ss->cache->radius*test.dist)*frontface(brush, an, vd.no, vd.fno);
2147 mul_v3_v3fl(proxy[vd.i], val, fade);
2150 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
2155 BLI_pbvh_vertex_iter_end;
2159 static void do_fill_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
2161 SculptSession *ss = ob->sculpt;
2162 Brush *brush = paint_brush(&sd->paint);
2164 float bstrength = ss->cache->bstrength;
2165 const float radius = ss->cache->radius;
2169 float offset = get_offset(sd, ss);
2177 calc_sculpt_plane(sd, ob, nodes, totnode, an, fc);
2179 displace = radius*offset;
2181 mul_v3_v3v3(temp, an, ss->cache->scale);
2182 mul_v3_fl(temp, displace);
2183 add_v3_v3(fc, temp);
2185 #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
2186 for (n = 0; n < totnode; n++) {
2188 SculptBrushTest test;
2191 proxy= BLI_pbvh_node_add_proxy(ss->pbvh, nodes[n])->co;
2193 sculpt_brush_test_init(ss, &test);
2195 BLI_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE) {
2196 if (sculpt_brush_test_sq(&test, vd.co)) {
2197 if (plane_point_side(vd.co, an, fc)) {
2201 point_plane_project(intr, vd.co, an, fc);
2203 sub_v3_v3v3(val, intr, vd.co);
2205 if (plane_trim(ss->cache, brush, val)) {
2206 const float fade = bstrength*tex_strength(ss, brush, vd.co, sqrt(test.dist))*frontface(brush, an, vd.no, vd.fno);
2208 mul_v3_v3fl(proxy[vd.i], val, fade);
2211 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
2216 BLI_pbvh_vertex_iter_end;
2220 static void do_scrape_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
2222 SculptSession *ss = ob->sculpt;
2223 Brush *brush = paint_brush(&sd->paint);
2225 float bstrength = ss->cache->bstrength;
2226 const float radius = ss->cache->radius;
2230 float offset = get_offset(sd, ss);
2238 calc_sculpt_plane(sd, ob, nodes, totnode, an, fc);
2240 displace = -radius*offset;
2242 mul_v3_v3v3(temp, an, ss->cache->scale);
2243 mul_v3_fl(temp, displace);
2244 add_v3_v3(fc, temp);
2246 #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
2247 for (n = 0; n < totnode; n++) {
2249 SculptBrushTest test;
2252 proxy= BLI_pbvh_node_add_proxy(ss->pbvh, nodes[n])->co;
2254 sculpt_brush_test_init(ss, &test);
2256 BLI_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE) {
2257 if (sculpt_brush_test_sq(&test, vd.co)) {
2258 if (!plane_point_side(vd.co, an, fc)) {
2262 point_plane_project(intr, vd.co, an, fc);
2264 sub_v3_v3v3(val, intr, vd.co);
2266 if (plane_trim(ss->cache, brush, val)) {
2267 const float fade = bstrength*tex_strength(ss, brush, vd.co, sqrt(test.dist))*frontface(brush, an, vd.no, vd.fno);
2269 mul_v3_v3fl(proxy[vd.i], val, fade);
2272 vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
2277 BLI_pbvh_vertex_iter_end;
2281 void sculpt_vertcos_to_key(Object *ob, KeyBlock *kb, float (*vertCos)[3])
2283 Mesh *me= (Mesh*)ob->data;
2284 float (*ofs)[3]= NULL;
2288 /* for relative keys editing of base should update other keys */
2289 if (me->key->type == KEY_RELATIVE)
2290 for (currkey = me->key->block.first; currkey; currkey= currkey->next)
2291 if(ob->shapenr-1 == currkey->relative) {
2297 ofs= key_to_vertcos(ob, kb);
2299 /* calculate key coord offsets (from previous location) */
2300 for (a= 0; a < me->totvert; a++)
2301 VECSUB(ofs[a], vertCos[a], ofs[a]);
2303 /* apply offsets on other keys */
2304 currkey = me->key->block.first;
2306 int apply_offset = ((currkey != kb) && (ob->shapenr-1 == currkey->relative));
2309 offset_to_key(ob, currkey, ofs);
2311 currkey= currkey->next;
2317 /* modifying of basis key should update mesh */
2318 if (kb == me->key->refkey) {
2319 MVert *mvert= me->mvert;
2321 for (a= 0; a < me->totvert; a++, mvert++)
2322 VECCOPY(mvert->co, vertCos[a]);
2324 mesh_calc_normals(me->mvert, me->totvert, me->mface, me->totface, NULL);
2327 /* apply new coords on active key block */
2328 vertcos_to_key(ob, kb, vertCos);
2331 static void do_brush_action(Sculpt *sd, Object *ob, Brush *brush)
2333 SculptSession *ss = ob->sculpt;
2334 SculptSearchSphereData data;
2335 PBVHNode **nodes = NULL;
2338 /* Build a list of all nodes that are potentially within the brush's area of influence */
2341 data.radius_squared = ss->cache->radius_squared;
2342 data.original = ELEM4(brush->sculpt_tool, SCULPT_TOOL_GRAB, SCULPT_TOOL_ROTATE, SCULPT_TOOL_THUMB, SCULPT_TOOL_LAYER);
2343 BLI_pbvh_search_gather(ss->pbvh, sculpt_search_sphere_cb, &data, &nodes, &totnode);
2345 /* Only act if some verts are inside the brush area */
2347 #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
2348 for (n= 0; n < totnode; n++) {
2349 sculpt_undo_push_node(ob, nodes[n]);
2350 BLI_pbvh_node_mark_update(nodes[n]);
2353 /* Apply one type of brush action */
2354 switch(brush->sculpt_tool){
2355 case SCULPT_TOOL_DRAW:
2356 do_draw_brush(sd, ob, nodes, totnode);
2358 case SCULPT_TOOL_SMOOTH:
2359 do_smooth_brush(sd, ob, nodes, totnode);
2361 case SCULPT_TOOL_CREASE:
2362 do_crease_brush(sd, ob, nodes, totnode);
2364 case SCULPT_TOOL_BLOB:
2365 do_crease_brush(sd, ob, nodes, totnode);
2367 case SCULPT_TOOL_PINCH:
2368 do_pinch_brush(sd, ob, nodes, totnode);
2370 case SCULPT_TOOL_INFLATE:
2371 do_inflate_brush(sd, ob, nodes, totnode);
2373 case SCULPT_TOOL_GRAB:
2374 do_grab_brush(sd, ob, nodes, totnode);
2376 case SCULPT_TOOL_ROTATE:
2377 do_rotate_brush(sd, ob, nodes, totnode);
2379 case SCULPT_TOOL_SNAKE_HOOK:
2380 do_snake_hook_brush(sd, ob, nodes, totnode);
2382 case SCULPT_TOOL_NUDGE:
2383 do_nudge_brush(sd, ob, nodes, totnode);
2385 case SCULPT_TOOL_THUMB:
2386 do_thumb_brush(sd, ob, nodes, totnode);
2388 case SCULPT_TOOL_LAYER:
2389 do_layer_brush(sd, ob, nodes, totnode);
2391 case SCULPT_TOOL_FLATTEN:
2392 do_flatten_brush(sd, ob, nodes, totnode);
2394 case SCULPT_TOOL_CLAY:
2395 do_clay_brush(sd, ob, nodes, totnode);
2397 case SCULPT_TOOL_CLAY_TUBES:
2398 do_clay_tubes_brush(sd, ob, nodes, totnode);
2400 case SCULPT_TOOL_FILL:
2401 do_fill_brush(sd, ob, nodes, totnode);
2403 case SCULPT_TOOL_SCRAPE:
2404 do_scrape_brush(sd, ob, nodes, totnode);
2408 if (brush->sculpt_tool != SCULPT_TOOL_SMOOTH && brush->autosmooth_factor > 0) {
2409 if (brush->flag & BRUSH_INVERSE_SMOOTH_PRESSURE) {
2410 smooth(sd, ob, nodes, totnode, brush->autosmooth_factor*(1-ss->cache->pressure));
2413 smooth(sd, ob, nodes, totnode, brush->autosmooth_factor);
2421 /* flush displacement from deformed PBVH vertex to original mesh */
2422 static void sculpt_flush_pbvhvert_deform(Object *ob, PBVHVertexIter *vd)
2424 SculptSession *ss = ob->sculpt;
2426 float disp[3], newco[3];
2427 int index= vd->vert_indices[vd->i];
2429 sub_v3_v3v3(disp, vd->co, ss->deform_cos[index]);
2430 mul_m3_v3(ss->deform_imats[index], disp);
2431 add_v3_v3v3(newco, disp, ss->orig_cos[index]);
2433 copy_v3_v3(ss->deform_cos[index], vd->co);
2434 copy_v3_v3(ss->orig_cos[index], newco);
2437 copy_v3_v3(me->mvert[index].co, newco);
2440 static void sculpt_combine_proxies(Sculpt *sd, Object *ob)
2442 SculptSession *ss = ob->sculpt;
2443 Brush *brush= paint_brush(&sd->paint);
2447 BLI_pbvh_gather_proxies(ss->pbvh, &nodes, &totnode);
2449 if(!ELEM(brush->sculpt_tool, SCULPT_TOOL_SMOOTH, SCULPT_TOOL_LAYER)) {
2450 /* these brushes start from original coordinates */
2451 const int use_orco = (ELEM3(brush->sculpt_tool, SCULPT_TOOL_GRAB,
2452 SCULPT_TOOL_ROTATE, SCULPT_TOOL_THUMB));
2454 #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
2455 for (n= 0; n < totnode; n++) {
2457 PBVHProxyNode* proxies;
2462 orco= sculpt_undo_push_node(ob, nodes[n])->co;
2464 BLI_pbvh_node_get_proxies(nodes[n], &proxies, &proxy_count);
2466 BLI_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE) {
2471 copy_v3_v3(val, orco[vd.i]);
2473 copy_v3_v3(val, vd.co);
2475 for (p= 0; p < proxy_count; p++)
2476 add_v3_v3(val, proxies[p].co[vd.i]);
2478 sculpt_clip(sd, ss, vd.co, val);
2480 if(ss->modifiers_active)
2481 sculpt_flush_pbvhvert_deform(ob, &vd);
2483 BLI_pbvh_vertex_iter_end;
2485 BLI_pbvh_node_free_proxies(nodes[n]);
2493 /* copy the modified vertices from bvh to the active key */
2494 static void sculpt_update_keyblock(Object *ob)
2496 SculptSession *ss = ob->sculpt;
2497 float (*vertCos)[3];
2499 /* Keyblock update happens after hadning deformation caused by modifiers,
2500 so ss->orig_cos would be updated with new stroke */
2501 if(ss->orig_cos) vertCos = ss->orig_cos;
2502 else vertCos = BLI_pbvh_get_vertCos(ss->pbvh);
2505 sculpt_vertcos_to_key(ob, ss->kb, vertCos);
2507 if(vertCos != ss->orig_cos)
2512 /* flush displacement from deformed PBVH to original layer */
2513 static void sculpt_flush_stroke_deform(Sculpt *sd, Object *ob)
2515 SculptSession *ss = ob->sculpt;
2516 Brush *brush= paint_brush(&sd->paint);
2518 if(ELEM(brush->sculpt_tool, SCULPT_TOOL_SMOOTH, SCULPT_TOOL_LAYER)) {
2519 /* this brushes aren't using proxies, so sculpt_combine_proxies() wouldn't
2520 propagate needed deformation to original base */
2523 Mesh *me= (Mesh*)ob->data;
2525 float (*vertCos)[3]= NULL;
2528 vertCos= MEM_callocN(sizeof(*vertCos)*me->totvert, "flushStrokeDeofrm keyVerts");
2530 BLI_pbvh_search_gather(ss->pbvh, NULL, NULL, &nodes, &totnode);
2532 #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
2533 for (n= 0; n < totnode; n++) {
2536 BLI_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE) {
2537 sculpt_flush_pbvhvert_deform(ob, &vd);
2540 int index= vd.vert_indices[vd.i];
2541 copy_v3_v3(vertCos[index], ss->orig_cos[index]);
2544 BLI_pbvh_vertex_iter_end;
2548 sculpt_vertcos_to_key(ob, ss->kb, vertCos);
2554 /* Modifiers could depend on mesh normals, so we should update them/
2555 Note, then if sculpting happens on locked key, normals should be re-calculated
2556 after applying coords from keyblock on base mesh */
2557 mesh_calc_normals(me->mvert, me->totvert, me->mface, me->totface, NULL);
2559 sculpt_update_keyblock(ob);
2562 //static int max_overlap_count(Sculpt *sd)
2567 // for (i= 0; i < 3; i++) {
2568 // count[i] = sd->radial_symm[i];
2570 // for (j= 0; j < 3; j++) {
2571 // if (i != j && sd->flags & (SCULPT_SYMM_X<<i))
2576 // return MAX3(count[0], count[1], count[2]);
2579 /* Flip all the editdata across the axis/axes specified by symm. Used to
2580 calculate multiple modifications to the mesh when symmetry is enabled. */
2581 static void calc_brushdata_symm(Sculpt *sd, StrokeCache *cache, const char symm, const char axis, const float angle, const float UNUSED(feather))
2583 (void)sd; /* unused */
2585 flip_coord(cache->location, cache->true_location, symm);
2586 flip_coord(cache->grab_delta_symmetry, cache->grab_delta, symm);
2587 flip_coord(cache->view_normal, cache->true_view_normal, symm);
2589 // XXX This reduces the length of the grab delta if it approaches the line of symmetry
2590 // XXX However, a different approach appears to be needed
2591 //if (sd->flags & SCULPT_SYMMETRY_FEATHER) {
2592 // float frac = 1.0f/max_overlap_count(sd);
2593 // float reduce = (feather-frac)/(1-frac);
2595 // printf("feather: %f frac: %f reduce: %f\n", feather, frac, reduce);
2598 // mul_v3_fl(cache->grab_delta_symmetry, reduce);
2601 unit_m4(cache->symm_rot_mat);
2602 unit_m4(cache->symm_rot_mat_inv);
2604 if(axis) { /* expects XYZ */
2605 rotate_m4(cache->symm_rot_mat, axis, angle);
2606 rotate_m4(cache->symm_rot_mat_inv, axis, -angle);
2609 mul_m4_v3(cache->symm_rot_mat, cache->location);
2610 mul_m4_v3(cache->symm_rot_mat, cache->grab_delta_symmetry);
2613 static void do_radial_symmetry(Sculpt *sd, Object *ob, Brush *brush, const char symm, const int axis, const float feather)
2615 SculptSession *ss = ob->sculpt;
2618 for(i = 1; i < sd->radial_symm[axis-'X']; ++i) {
2619 const float angle = 2*M_PI*i/sd->radial_symm[axis-'X'];
2620 ss->cache->radial_symmetry_pass= i;
2621 calc_brushdata_symm(sd, ss->cache, symm, axis, angle, feather);
2622 do_brush_action(sd, ob, brush);
2626 /* noise texture gives different values for the same input coord; this
2627 can tear a multires mesh during sculpting so do a stitch in this
2629 static void sculpt_fix_noise_tear(Sculpt *sd, Object *ob)
2631 SculptSession *ss = ob->sculpt;
2632 Brush *brush = paint_brush(&sd->paint);
2633 MTex *mtex = &brush->mtex;
2635 if(ss->multires && mtex->tex && mtex->tex->type == TEX_NOISE)
2636 multires_stitch_grids(ob);
2639 static void do_symmetrical_brush_actions(Sculpt *sd, Object *ob)
2641 Brush *brush = paint_brush(&sd->paint);
2642 SculptSession *ss = ob->sculpt;
2643 StrokeCache *cache = ss->cache;
2644 const char symm = sd->flags & 7;
2647 float feather = calc_symmetry_feather(sd, ss->cache);
2649 cache->bstrength= brush_strength(sd, cache, feather);
2651 cache->symmetry= symm;
2653 /* symm is a bit combination of XYZ - 1 is mirror X; 2 is Y; 3 is XY; 4 is Z; 5 is XZ; 6 is YZ; 7 is XYZ */
2654 for(i = 0; i <= symm; ++i) {
2655 if(i == 0 || (symm & i && (symm != 5 || i != 3) && (symm != 6 || (i != 3 && i != 5)))) {
2656 cache->mirror_symmetry_pass= i;
2657 cache->radial_symmetry_pass= 0;
2659 calc_brushdata_symm(sd, cache, i, 0, 0, feather);
2660 do_brush_action(sd, ob, brush);
2662 do_radial_symmetry(sd, ob, brush, i, 'X', feather);
2663 do_radial_symmetry(sd, ob, brush, i, 'Y', feather);
2664 do_radial_symmetry(sd, ob, brush, i, 'Z', feather);
2668 sculpt_combine_proxies(sd, ob);
2670 /* hack to fix noise texture tearing mesh */
2671 sculpt_fix_noise_tear(sd, ob);
2673 if (ss->modifiers_active)
2674 sculpt_flush_stroke_deform(sd, ob);
2676 cache->first_time= 0;
2679 static void sculpt_update_tex(Sculpt *sd, SculptSession *ss)
2681 Brush *brush = paint_brush(&sd->paint);
2682 const int radius= brush_size(brush);
2685 MEM_freeN(ss->texcache);
2689 /* Need to allocate a bigger buffer for bigger brush size */
2690 ss->texcache_side = 2*radius;
2691 if(!ss->texcache || ss->texcache_side > ss->texcache_actual) {
2692 ss->texcache = brush_gen_texture_cache(brush, radius);
2693 ss->texcache_actual = ss->texcache_side;
2697 void sculpt_free_deformMats(SculptSession *ss)
2699 if(ss->orig_cos) MEM_freeN(ss->orig_cos);
2700 if(ss->deform_cos) MEM_freeN(ss->deform_cos);
2701 if(ss->deform_imats) MEM_freeN(ss->deform_imats);
2703 ss->orig_cos = NULL;
2704 ss->deform_cos = NULL;
2705 ss->deform_imats = NULL;
2708 void sculpt_update_mesh_elements(Scene *scene, Object *ob, int need_fmap)
2710 DerivedMesh *dm = mesh_get_derived_final(scene, ob, CD_MASK_BAREMESH);
2711 SculptSession *ss = ob->sculpt;
2712 MultiresModifierData *mmd= sculpt_multires_active(scene, ob);
2714 ss->modifiers_active= sculpt_modifiers_active(scene, ob);
2716 if(!mmd) ss->kb= ob_get_keyblock(ob);
2721 ss->totvert = dm->getNumVerts(dm);
2722 ss->totface = dm->getNumFaces(dm);
2725 ss->face_normals= NULL;
2728 Mesh *me = get_mesh(ob);
2729 ss->totvert = me->totvert;
2730 ss->totface = me->totface;
2731 ss->mvert = me->mvert;
2732 ss->mface = me->mface;
2733 ss->face_normals = NULL;
2734 ss->multires = NULL;
2737 ss->pbvh = dm->getPBVH(ob, dm);
2738 ss->fmap = (need_fmap && dm->getFaceMap)? dm->getFaceMap(ob, dm): NULL;
2740 if(ss->modifiers_active) {
2744 sculpt_free_deformMats(ss);
2746 if(ss->kb) ss->orig_cos = key_to_vertcos(ob, ss->kb);
2747 else ss->orig_cos = mesh_getVertexCos(ob->data, NULL);
2749 crazyspace_build_sculpt(scene, ob, &ss->deform_imats, &ss->deform_cos);
2750 BLI_pbvh_apply_vertCos(ss->pbvh, ss->deform_cos);
2752 for(a = 0; a < ((Mesh*)ob->data)->totvert; ++a)
2753 invert_m3(ss->deform_imats[a]);
2755 } else sculpt_free_deformMats(ss);
2757 /* if pbvh is deformed, key block is already applied to it */
2758 if (ss->kb && !BLI_pbvh_isDeformed(ss->pbvh)) {
2759 float (*vertCos)[3]= key_to_vertcos(ob, ss->kb);
2762 /* apply shape keys coordinates to PBVH */
2763 BLI_pbvh_apply_vertCos(ss->pbvh, vertCos);
2769 static int sculpt_mode_poll(bContext *C)
2771 Object *ob = CTX_data_active_object(C);
2772 return ob && ob->mode & OB_MODE_SCULPT;
2775 int sculpt_poll(bContext *C)
2777 return sculpt_mode_poll(C) && paint_poll(C);
2780 static const char *sculpt_tool_name(Sculpt *sd)
2782 Brush *brush = paint_brush(&sd->paint);
2784 switch(brush->sculpt_tool) {
2785 case SCULPT_TOOL_DRAW:
2786 return "Draw Brush"; break;
2787 case SCULPT_TOOL_SMOOTH:
2788 return "Smooth Brush"; break;
2789 case SCULPT_TOOL_CREASE:
2790 return "Crease Brush"; break;
2791 case SCULPT_TOOL_BLOB:
2792 return "Blob Brush"; break;
2793 case SCULPT_TOOL_PINCH:
2794 return "Pinch Brush"; break;
2795 case SCULPT_TOOL_INFLATE:
2796 return "Inflate Brush"; break;
2797 case SCULPT_TOOL_GRAB:
2798 return "Grab Brush"; break;
2799 case SCULPT_TOOL_NUDGE:
2800 return "Nudge Brush"; break;
2801 case SCULPT_TOOL_THUMB:
2802 return "Thumb Brush"; break;
2803 case SCULPT_TOOL_LAYER:
2804 return "Layer Brush"; break;
2805 case SCULPT_TOOL_FLATTEN:
2806 return "Flatten Brush"; break;
2807 case SCULPT_TOOL_CLAY:
2808 return "Clay Brush"; break;
2809 case SCULPT_TOOL_CLAY_TUBES:
2810 return "Clay Tubes Brush"; break;
2811 case SCULPT_TOOL_FILL:
2812 return "Fill Brush"; break;
2813 case SCULPT_TOOL_SCRAPE:
2814 return "Scrape Brush"; break;
2816 return "Sculpting"; break;
2820 /**** Radial control ****/
2821 static int sculpt_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
2823 Paint *p = paint_get_active(CTX_data_scene(C));
2824 Brush *brush = paint_brush(p);
2825 float col[4], tex_col[4];
2827 WM_paint_cursor_end(CTX_wm_manager(C), p->paint_cursor);
2828 p->paint_cursor = NULL;
2829 brush_radial_control_invoke(op, brush, 1);
2831 if((brush->flag & BRUSH_DIR_IN) && ELEM4(brush->sculpt_tool, SCULPT_TOOL_DRAW, SCULPT_TOOL_INFLATE, SCULPT_TOOL_CLAY, SCULPT_TOOL_PINCH))
2832 copy_v3_v3(col, brush->sub_col);
2834 copy_v3_v3(col, brush->add_col);
2837 copy_v3_v3(tex_col, U.sculpt_paint_overlay_col);
2838 tex_col[3]= (brush->texture_overlay_alpha / 100.0f);
2840 RNA_float_set_array(op->ptr, "color", col);
2841 RNA_float_set_array(op->ptr, "texture_color", tex_col);
2843 return WM_radial_control_invoke(C, op, event);
2846 static int sculpt_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
2848 int ret = WM_radial_control_modal(C, op, event);
2849 if(ret != OPERATOR_RUNNING_MODAL)
2850 paint_cursor_start(C, sculpt_poll);
2854 static int sculpt_radial_control_exec(bContext *C, wmOperator *op)
2856 Brush *brush = paint_brush(&CTX_data_tool_settings(C)->sculpt->paint);
2857 int ret = brush_radial_control_exec(op, brush, 1);
2859 WM_event_add_notifier(C, NC_BRUSH|NA_EDITED, brush);
2864 static void SCULPT_OT_radial_control(wmOperatorType *ot)
2866 WM_OT_radial_control_partial(ot);
2868 ot->name= "Sculpt Radial Control";
2869 ot->idname= "SCULPT_OT_radial_control";
2871 ot->invoke= sculpt_radial_control_invoke;
2872 ot->modal= sculpt_radial_control_modal;
2873 ot->exec= sculpt_radial_control_exec;
2874 ot->poll= sculpt_poll;
2876 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
2879 /**** Operator for applying a stroke (various attributes including mouse path)
2880 using the current brush. ****/
2882 static void sculpt_cache_free(StrokeCache *cache)
2884 if(cache->face_norms)
2885 MEM_freeN(cache->face_norms);
2887 MEM_freeN(cache->mats);
2891 /* Initialize mirror modifier clipping */
2892 static void sculpt_init_mirror_clipping(Object *ob, SculptSession *ss)
2897 for(md= ob->modifiers.first; md; md= md->next) {
2898 if(md->type==eModifierType_Mirror &&
2899 (md->mode & eModifierMode_Realtime)) {
2900 MirrorModifierData *mmd = (MirrorModifierData*)md;
2902 if(mmd->flag & MOD_MIR_CLIPPING) {
2903 /* check each axis for mirroring */
2904 for(i = 0; i < 3; ++i) {
2905 if(mmd->flag & (MOD_MIR_AXIS_X << i)) {
2906 /* enable sculpt clipping */
2907 ss->cache->flag |= CLIP_X << i;
2909 /* update the clip tolerance */
2911 ss->cache->clip_tolerance[i])
2912 ss->cache->clip_tolerance[i] =
2921 /* Initialize the stroke cache invariants from operator properties */
2922 static void sculpt_update_cache_invariants(bContext* C, Sculpt *sd, SculptSession *ss, wmOperator *op, wmEvent *event)
2924 StrokeCache *cache = MEM_callocN(sizeof(StrokeCache), "stroke cache");
2925 Brush *brush = paint_brush(&sd->paint);
2926 ViewContext *vc = paint_stroke_view_context(op->customdata);
2927 Object *ob= CTX_data_active_object(C);
2933 /* Set scaling adjustment */
2934 ss->cache->scale[0] = 1.0f / ob->size[0];
2935 ss->cache->scale[1] = 1.0f / ob->size[1];
2936 ss->cache->scale[2] = 1.0f / ob->size[2];
2938 ss->cache->plane_trim_squared = brush->plane_trim * brush->plane_trim;
2940 ss->cache->flag = 0;
2942 sculpt_init_mirror_clipping(ob, ss);
2944 /* Initial mouse location */
2946 ss->cache->initial_mouse[0] = event->x;
2947 ss->cache->initial_mouse[1] = event->y;
2950 ss->cache->initial_mouse[0] = 0;
2951 ss->cache->initial_mouse[1] = 0;
2954 mode = RNA_enum_get(op->ptr, "mode");
2955 cache->invert = mode == BRUSH_STROKE_INVERT;
2956 cache->alt_smooth = mode == BRUSH_STROKE_SMOOTH;
2958 /* not very nice, but with current events system implementation
2959 we can't handle brush appearance inversion hotkey separately (sergey) */
2960 if(cache->invert) brush->flag |= BRUSH_INVERTED;
2961 else brush->flag &= ~BRUSH_INVERTED;
2964 if (ss->cache->alt_smooth) {
2965 Paint *p= &sd->paint;
2968 BLI_strncpy(cache->saved_active_brush_name, brush->id.name+2, sizeof(cache->saved_active_brush_name));
2970 br= (Brush *)find_id("BR", "Smooth");
2972 paint_brush_set(p, br);
2977 copy_v2_v2(cache->mouse, cache->initial_mouse);
2978 copy_v2_v2(cache->tex_mouse, cache->initial_mouse);
2980 /* Truly temporary data that isn't stored in properties */
2984 cache->brush = brush;
2986 cache->mats = MEM_callocN(sizeof(bglMats), "sculpt bglMats");
2987 view3d_get_transformation(vc->ar, vc->rv3d, vc->obact, cache->mats);
2989 viewvector(cache->vc->rv3d, cache->vc->rv3d->twmat[3], cache->true_view_normal);
2990 /* Initialize layer brush displacements and persistent coords */
2991 if(brush->sculpt_tool == SCULPT_TOOL_LAYER) {
2992 /* not supported yet for multires */
2993 if(!ss->multires && !ss->layer_co && (brush->flag & BRUSH_PERSISTENT)) {
2995 ss->layer_co= MEM_mallocN(sizeof(float) * 3 * ss->totvert,
2996 "sculpt mesh vertices copy");
2998 if(ss->deform_cos) memcpy(ss->layer_co, ss->deform_cos, ss->totvert);
3000 for(i = 0; i < ss->totvert; ++i) {
3001 copy_v3_v3(ss->layer_co[i], ss->mvert[i].co);
3007 /* Make copies of the mesh vertex locations and normals for some tools */
3008 if(brush->flag & BRUSH_ANCHORED) {
3009 if(ss->face_normals) {
3010 float *fn = ss->face_normals;
3011 cache->face_norms= MEM_mallocN(sizeof(float) * 3 * ss->totface, "Sculpt face norms");
3012 for(i = 0; i < ss->totface; ++i, fn += 3)
3013 copy_v3_v3(cache->face_norms[i], fn);
3016 cache->original = 1;
3019 if(ELEM8(brush->sculpt_tool, SCULPT_TOOL_DRAW, SCULPT_TOOL_CREASE, SCULPT_TOOL_BLOB, SCULPT_TOOL_LAYER, SCULPT_TOOL_INFLATE, SCULPT_TOOL_CLAY, SCULPT_TOOL_CLAY_TUBES, SCULPT_TOOL_ROTATE))
3020 if(!(brush->flag & BRUSH_ACCUMULATE))
3021 cache->original = 1;
3023 cache->special_rotation = (brush->flag & BRUSH_RAKE) ? sd->last_angle : 0;
3024 //cache->last_rake[0] = sd->last_x;
3025 //cache->last_rake[1] = sd->last_y;
3027 cache->first_time= 1;
3029 cache->vertex_rotation= 0;
3032 static void sculpt_update_brush_delta(Sculpt *sd, Object *ob, Brush *brush)
3034 SculptSession *ss = ob->sculpt;
3035 StrokeCache *cache = ss->cache;
3036 int tool = brush->sculpt_tool;
3039 SCULPT_TOOL_GRAB, SCULPT_TOOL_NUDGE,
3040 SCULPT_TOOL_CLAY_TUBES, SCULPT_TOOL_SNAKE_HOOK,
3041 SCULPT_TOOL_THUMB)) {
3042 float grab_location[3], imat[4][4], delta[3];
3044 if(cache->first_time) {
3045 copy_v3_v3(cache->orig_grab_location,
3046 cache->true_location);
3048 else if(tool == SCULPT_TOOL_SNAKE_HOOK)
3049 add_v3_v3(cache->true_location, cache->grab_delta);
3051 /* compute 3d coordinate at same z from original location + mouse */
3052 initgrabz(cache->vc->rv3d,
3053 cache->orig_grab_location[0],
3054 cache->orig_grab_location[1],
3055 cache->orig_grab_location[2]);
3057 window_to_3d_delta(cache->vc->ar, grab_location,
3058 cache->mouse[0], cache->mouse[1]);
3060 /* compute delta to move verts by */
3061 if(!cache->first_time) {
3063 case SCULPT_TOOL_GRAB:
3064 case SCULPT_TOOL_THUMB:
3065 sub_v3_v3v3(delta, grab_location, cache->old_grab_location);
3066 invert_m4_m4(imat, ob->obmat);
3067 mul_mat3_m4_v3(imat, delta);
3068 add_v3_v3(cache->grab_delta, delta);
3070 case SCULPT_TOOL_CLAY_TUBES:
3071 case SCULPT_TOOL_NUDGE:
3072 sub_v3_v3v3(cache->grab_delta, grab_location, cache->old_grab_location);
3073 invert_m4_m4(imat, ob->obmat);
3074 mul_mat3_m4_v3(imat, cache->grab_delta);
3076 case SCULPT_TOOL_SNAKE_HOOK:
3077 sub_v3_v3v3(cache->grab_delta, grab_location, cache->old_grab_location);
3078 invert_m4_m4(imat, ob->obmat);
3079 mul_mat3_m4_v3(imat, cache->grab_delta);
3084 zero_v3(cache->grab_delta);
3087 copy_v3_v3(cache->old_grab_location, grab_location);
3089 if(tool == SCULPT_TOOL_GRAB)
3090 copy_v3_v3(sd->anchored_location, cache->true_location);
3091 else if(tool == SCULPT_TOOL_THUMB)
3092 copy_v3_v3(sd->anchored_location, cache->orig_grab_location);
3094 if(ELEM(tool, SCULPT_TOOL_GRAB, SCULPT_TOOL_THUMB)) {
3095 /* location stays the same for finding vertices in brush radius */
3096 copy_v3_v3(cache->true_location, cache->orig_grab_location);
3098 sd->draw_anchored = 1;
3099 copy_v3_v3(sd->anchored_initial_mouse, cache->initial_mouse);
3100 sd->anchored_size = cache->pixel_radius;
3105 /* Initialize the stroke cache variants from operator properties */
3106 static void sculpt_update_cache_variants(bContext *C, Sculpt *sd, Object *ob, struct PaintStroke *stroke, PointerRNA *ptr)
3108 SculptSession *ss = ob->sculpt;
3109 StrokeCache *cache = ss->cache;
3110 Brush *brush = paint_brush(&sd->paint);
3114 //RNA_float_get_array(ptr, "location", cache->traced_location);
3116 if (cache->first_time ||
3117 !((brush->flag & BRUSH_ANCHORED)||
3118 (brush->sculpt_tool == SCULPT_TOOL_SNAKE_HOOK)||
3119 (brush->sculpt_tool == SCULPT_TOOL_ROTATE))
3122 RNA_float_get_array(ptr, "location", cache->true_location);
3125 cache->pen_flip = RNA_boolean_get(ptr, "pen_flip");
3126 RNA_float_get_array(ptr, "mouse", cache->mouse);
3128 /* XXX: Use preassure value from first brush step for brushes which don't
3129 support strokes (grab, thumb). They depends on initial state and
3130 brush coord/pressure/etc.
3131 It's more an events design issue, which doesn't split coordinate/pressure/angle
3132 changing events. We should avoid this after events system re-design */
3133 if(paint_space_stroke_enabled(brush) || cache->first_time)
3134 cache->pressure = RNA_float_get(ptr, "pressure");
3136 /* Truly temporary data that isn't stored in properties */
3138 sd->draw_pressure= 1;
3139 sd->pressure_value= cache->pressure;
3141 cache->previous_pixel_radius = cache->pixel_radius;
3142 cache->pixel_radius = brush_size(brush);
3144 if(cache->first_time) {
3145 if (!brush_use_locked_size(brush)) {
3146 cache->initial_radius= paint_calc_object_space_radius(cache->vc, cache->true_location, brush_size(brush));
3147 brush_set_unprojected_radius(brush, cache->initial_radius);
3150 cache->initial_radius= brush_unprojected_radius(brush);
3153 if (ELEM(brush->sculpt_tool, SCULPT_TOOL_GRAB, SCULPT_TOOL_SNAKE_HOOK))
3154 cache->initial_radius *= 2.0f;
3157 if(brush_use_size_pressure(brush)) {
3158 cache->pixel_radius *= cache->pressure;
3159 cache->radius= cache->initial_radius * cache->pressure;
3162 cache->radius= cache->initial_radius;
3164 cache->radius_squared = cache->radius*cache->radius;
3166 if(!(brush->flag & BRUSH_ANCHORED || ELEM4(brush->sculpt_tool, SCULPT_TOOL_GRAB, SCULPT_TOOL_SNAKE_HOOK, SCULPT_TOOL_THUMB, SCULPT_TOOL_ROTATE))) {
3167 copy_v2_v2(cache->tex_mouse, cache->mouse);
3169 if ( (brush->mtex.brush_map_mode == MTEX_MAP_MODE_FIXED) &&
3170 (brush->flag & BRUSH_RANDOM_ROTATION) &&
3171 !(brush->flag & BRUSH_RAKE))
3173 cache->special_rotation = 2.0f*(float)M_PI*BLI_frand();
3177 if(brush->flag & BRUSH_ANCHORED) {
3180 dx = cache->mouse[0] - cache->initial_mouse[0];
3181 dy = cache->mouse[1] - cache->initial_mouse[1];
3183 sd->anchored_size = cache->pixel_radius = sqrt(dx*dx + dy*dy);
3185 cache->special_rotation = atan2(dx, dy) + M_PI;
3187 if (brush->flag & BRUSH_EDGE_TO_EDGE) {
3191 halfway[0] = (float)dx * 0.5f + cache->initial_mouse[0];
3192 halfway[1] = (float)dy * 0.5f + cache->initial_mouse[1];
3194 if (sculpt_stroke_get_location(C, stroke, out, halfway)) {
3195 copy_v3_v3(sd->anchored_location, out);
3196 copy_v2_v2(sd->anchored_initial_mouse, halfway);
3197 copy_v2_v2(cache->tex_mouse, halfway);
3198 copy_v3_v3(cache->true_location, sd->anchored_location);
3199 sd->anchored_size /= 2.0f;
3200 cache->pixel_radius /= 2.0f;
3206 copy_v2_v2(sd->anchored_initial_mouse, cache->initial_mouse);
3208 cache->radius= paint_calc_object_space_radius(paint_stroke_view_context(stroke), cache->true_location, cache->pixel_radius);
3209 cache->radius_squared = cache->radius*cache->radius;