Blender  V2.93
sculpt_transform.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2020 Blender Foundation.
17  * All rights reserved.
18  */
19 
24 #include "MEM_guardedalloc.h"
25 
26 #include "BLI_blenlib.h"
27 #include "BLI_math.h"
28 #include "BLI_task.h"
29 
30 #include "DNA_mesh_types.h"
31 #include "DNA_meshdata_types.h"
32 
33 #include "BKE_brush.h"
34 #include "BKE_context.h"
35 #include "BKE_mesh.h"
36 #include "BKE_mesh_mapping.h"
37 #include "BKE_object.h"
38 #include "BKE_paint.h"
39 #include "BKE_pbvh.h"
40 #include "BKE_scene.h"
41 
42 #include "DEG_depsgraph.h"
43 
44 #include "WM_api.h"
45 #include "WM_message.h"
46 #include "WM_toolsystem.h"
47 #include "WM_types.h"
48 
49 #include "ED_object.h"
50 #include "ED_screen.h"
51 #include "ED_sculpt.h"
52 #include "paint_intern.h"
53 #include "sculpt_intern.h"
54 
55 #include "RNA_access.h"
56 #include "RNA_define.h"
57 
58 #include "bmesh.h"
59 
60 #include <math.h>
61 #include <stdlib.h>
62 
64 {
66  SculptSession *ss = ob->sculpt;
68 
72 
76 
77  SCULPT_undo_push_begin(ob, "Transform");
78  BKE_sculpt_update_object_for_edit(depsgraph, ob, false, false, false);
79 
80  ss->pivot_rot[3] = 1.0f;
81 
84 
86 }
87 
89  const char symm,
91  float r_transform_mats[8][4][4])
92 {
93 
94  float final_pivot_pos[3], d_t[3], d_r[4], d_s[3];
95  float t_mat[4][4], r_mat[4][4], s_mat[4][4], pivot_mat[4][4], pivot_imat[4][4],
96  transform_mat[4][4];
97 
98  float start_pivot_pos[3], start_pivot_rot[4], start_pivot_scale[3];
99  switch (t_mode) {
101  copy_v3_v3(start_pivot_pos, ss->init_pivot_pos);
102  copy_v4_v4(start_pivot_rot, ss->init_pivot_rot);
103  copy_v3_v3(start_pivot_scale, ss->init_pivot_scale);
104  break;
106  copy_v3_v3(start_pivot_pos, ss->prev_pivot_pos);
107  copy_v4_v4(start_pivot_rot, ss->prev_pivot_rot);
108  copy_v3_v3(start_pivot_scale, ss->prev_pivot_scale);
109  break;
110  }
111 
112  for (int i = 0; i < PAINT_SYMM_AREAS; i++) {
113  ePaintSymmetryAreas v_symm = i;
114 
115  copy_v3_v3(final_pivot_pos, ss->pivot_pos);
116 
117  unit_m4(pivot_mat);
118 
119  unit_m4(t_mat);
120  unit_m4(r_mat);
121  unit_m4(s_mat);
122 
123  /* Translation matrix. */
124  sub_v3_v3v3(d_t, ss->pivot_pos, start_pivot_pos);
125  SCULPT_flip_v3_by_symm_area(d_t, symm, v_symm, ss->init_pivot_pos);
126  translate_m4(t_mat, d_t[0], d_t[1], d_t[2]);
127 
128  /* Rotation matrix. */
129  sub_qt_qtqt(d_r, ss->pivot_rot, start_pivot_rot);
130  normalize_qt(d_r);
131  SCULPT_flip_quat_by_symm_area(d_r, symm, v_symm, ss->init_pivot_pos);
132  quat_to_mat4(r_mat, d_r);
133 
134  /* Scale matrix. */
135  sub_v3_v3v3(d_s, ss->pivot_scale, start_pivot_scale);
136  add_v3_fl(d_s, 1.0f);
137  size_to_mat4(s_mat, d_s);
138 
139  /* Pivot matrix. */
140  SCULPT_flip_v3_by_symm_area(final_pivot_pos, symm, v_symm, start_pivot_pos);
141  translate_m4(pivot_mat, final_pivot_pos[0], final_pivot_pos[1], final_pivot_pos[2]);
142  invert_m4_m4(pivot_imat, pivot_mat);
143 
144  /* Final transform matrix. */
145  mul_m4_m4m4(transform_mat, r_mat, t_mat);
146  mul_m4_m4m4(transform_mat, transform_mat, s_mat);
147  mul_m4_m4m4(r_transform_mats[i], transform_mat, pivot_imat);
148  mul_m4_m4m4(r_transform_mats[i], pivot_mat, r_transform_mats[i]);
149  }
150 }
151 
152 static void sculpt_transform_task_cb(void *__restrict userdata,
153  const int i,
154  const TaskParallelTLS *__restrict UNUSED(tls))
155 {
156 
157  SculptThreadedTaskData *data = userdata;
158  SculptSession *ss = data->ob->sculpt;
159  PBVHNode *node = data->nodes[i];
160 
161  SculptOrigVertData orig_data;
162  SCULPT_orig_vert_data_init(&orig_data, data->ob, data->nodes[i]);
163 
164  PBVHVertexIter vd;
165 
168  SCULPT_orig_vert_data_update(&orig_data, &vd);
169  float transformed_co[3], orig_co[3], disp[3];
170  float *start_co;
171  float fade = vd.mask ? *vd.mask : 0.0f;
172  copy_v3_v3(orig_co, orig_data.co);
173  char symm_area = SCULPT_get_vertex_symm_area(orig_co);
174 
177  start_co = orig_co;
178  break;
180  start_co = vd.co;
181  break;
182  }
183 
184  copy_v3_v3(transformed_co, start_co);
185  mul_m4_v3(data->transform_mats[(int)symm_area], transformed_co);
186  sub_v3_v3v3(disp, transformed_co, start_co);
187  mul_v3_fl(disp, 1.0f - fade);
188  add_v3_v3v3(vd.co, start_co, disp);
189 
190  if (vd.mvert) {
192  }
193  }
195 
197 }
198 
200 {
201  SculptSession *ss = ob->sculpt;
202  const char symm = SCULPT_mesh_symmetry_xyz_get(ob);
203 
205  .sd = sd,
206  .ob = ob,
207  .nodes = ss->filter_cache->nodes,
208  };
209 
211  ss, symm, ss->filter_cache->transform_displacement_mode, data.transform_mats);
212 
213  /* Regular transform applies all symmetry passes at once as it is split by symmetry areas
214  * (each vertex can only be transformed once by the transform matrix of its area). */
215  TaskParallelSettings settings;
218  0, ss->filter_cache->totnode, &data, sculpt_transform_task_cb, &settings);
219 }
220 
222 {
224  SculptSession *ss = ob->sculpt;
226 
228  BKE_sculpt_update_object_for_edit(depsgraph, ob, false, false, false);
229 
231 
235 
236  if (ss->deform_modifiers_active || ss->shapekey_active) {
237  SCULPT_flush_stroke_deform(sd, ob, true);
238  }
239 
241 }
242 
244 {
245  SculptSession *ss = ob->sculpt;
246  if (ss->filter_cache) {
248  }
249  /* Force undo push to happen even inside transform operator, since the sculpt
250  * undo system works separate from regular undo and this is require to properly
251  * finish an undo step also when canceling. */
252  const bool use_nested_undo = true;
253  SCULPT_undo_push_end_ex(use_nested_undo);
255 }
256 
264 
267  "ORIGIN",
268  0,
269  "Origin",
270  "Sets the pivot to the origin of the sculpt"},
272  "UNMASKED",
273  0,
274  "Unmasked",
275  "Sets the pivot position to the average position of the unmasked vertices"},
277  "BORDER",
278  0,
279  "Mask Border",
280  "Sets the pivot position to the center of the border of the mask"},
282  "ACTIVE",
283  0,
284  "Active Vertex",
285  "Sets the pivot position to the active vertex position"},
287  "SURFACE",
288  0,
289  "Surface",
290  "Sets the pivot position to the surface under the cursor"},
291  {0, NULL, 0, NULL, NULL},
292 };
293 
295 {
297  SculptSession *ss = ob->sculpt;
298  ARegion *region = CTX_wm_region(C);
300  const char symm = SCULPT_mesh_symmetry_xyz_get(ob);
301 
302  int mode = RNA_enum_get(op->ptr, "mode");
303 
304  BKE_sculpt_update_object_for_edit(depsgraph, ob, false, true, false);
305 
306  /* Pivot to center. */
307  if (mode == SCULPT_PIVOT_POSITION_ORIGIN) {
308  zero_v3(ss->pivot_pos);
309  }
310  /* Pivot to active vertex. */
311  else if (mode == SCULPT_PIVOT_POSITION_ACTIVE_VERTEX) {
313  }
314  /* Pivot to raycast surface. */
315  else if (mode == SCULPT_PIVOT_POSITION_CURSOR_SURFACE) {
316  float stroke_location[3];
317  float mouse[2];
318  mouse[0] = RNA_float_get(op->ptr, "mouse_x");
319  mouse[1] = RNA_float_get(op->ptr, "mouse_y");
320  if (SCULPT_stroke_get_location(C, stroke_location, mouse)) {
321  copy_v3_v3(ss->pivot_pos, stroke_location);
322  }
323  }
324  else {
325  PBVHNode **nodes;
326  int totnode;
327  BKE_pbvh_search_gather(ss->pbvh, NULL, NULL, &nodes, &totnode);
328 
329  float avg[3];
330  int total = 0;
331  zero_v3(avg);
332 
333  /* Pivot to unmasked. */
334  if (mode == SCULPT_PIVOT_POSITION_UNMASKED) {
335  for (int n = 0; n < totnode; n++) {
336  PBVHVertexIter vd;
337  BKE_pbvh_vertex_iter_begin (ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE) {
338  const float mask = (vd.mask) ? *vd.mask : 0.0f;
339  if (mask < 1.0f) {
340  if (SCULPT_check_vertex_pivot_symmetry(vd.co, ss->pivot_pos, symm)) {
341  add_v3_v3(avg, vd.co);
342  total++;
343  }
344  }
345  }
347  }
348  }
349  /* Pivot to mask border. */
350  else if (mode == SCULPT_PIVOT_POSITION_MASK_BORDER) {
351  const float threshold = 0.2f;
352 
353  for (int n = 0; n < totnode; n++) {
354  PBVHVertexIter vd;
355  BKE_pbvh_vertex_iter_begin (ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE) {
356  const float mask = (vd.mask) ? *vd.mask : 0.0f;
357  if (mask < (0.5f + threshold) && mask > (0.5f - threshold)) {
358  if (SCULPT_check_vertex_pivot_symmetry(vd.co, ss->pivot_pos, symm)) {
359  add_v3_v3(avg, vd.co);
360  total++;
361  }
362  }
363  }
365  }
366  }
367 
368  if (total > 0) {
369  mul_v3_fl(avg, 1.0f / total);
370  copy_v3_v3(ss->pivot_pos, avg);
371  }
372 
373  MEM_SAFE_FREE(nodes);
374  }
375 
376  /* Update the viewport navigation rotation origin. */
379  ups->average_stroke_counter = 1;
380  ups->last_stroke_valid = true;
381 
382  ED_region_tag_redraw(region);
384 
385  return OPERATOR_FINISHED;
386 }
387 
389 {
390  RNA_float_set(op->ptr, "mouse_x", event->mval[0]);
391  RNA_float_set(op->ptr, "mouse_y", event->mval[1]);
392  return sculpt_set_pivot_position_exec(C, op);
393 }
394 
396 {
397  /* Identifiers. */
398  ot->name = "Set Pivot Position";
399  ot->idname = "SCULPT_OT_set_pivot_position";
400  ot->description = "Sets the sculpt transform pivot position";
401 
402  /* API callbacks. */
406 
409  "mode",
412  "Mode",
413  "");
414 
416  "mouse_x",
417  0.0f,
418  0.0f,
419  FLT_MAX,
420  "Mouse Position X",
421  "Position of the mouse used for \"Surface\" mode",
422  0.0f,
423  10000.0f);
425  "mouse_y",
426  0.0f,
427  0.0f,
428  FLT_MAX,
429  "Mouse Position Y",
430  "Position of the mouse used for \"Surface\" mode",
431  0.0f,
432  10000.0f);
433 }
struct Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C)
Definition: context.c:1424
struct Object * CTX_data_active_object(const bContext *C)
Definition: context.c:1279
struct ARegion * CTX_wm_region(const bContext *C)
Definition: context.c:725
struct Depsgraph * CTX_data_depsgraph_pointer(const bContext *C)
Definition: context.c:1401
struct ToolSettings * CTX_data_tool_settings(const bContext *C)
Definition: context.c:1208
General operations, lookup, etc. for blender objects.
#define PAINT_SYMM_AREAS
Definition: BKE_paint.h:126
ePaintSymmetryAreas
Definition: BKE_paint.h:120
void BKE_sculpt_update_object_for_edit(struct Depsgraph *depsgraph, struct Object *ob_orig, bool need_pmap, bool need_mask, bool need_colors)
Definition: paint.c:1817
A BVH for high poly meshes.
void BKE_pbvh_node_mark_update(PBVHNode *node)
Definition: pbvh.c:1732
#define BKE_pbvh_vertex_iter_begin(pbvh, node, vi, mode)
Definition: BKE_pbvh.h:384
#define BKE_pbvh_vertex_iter_end
Definition: BKE_pbvh.h:457
#define PBVH_ITER_UNIQUE
Definition: BKE_pbvh.h:335
void BKE_pbvh_parallel_range_settings(struct TaskParallelSettings *settings, bool use_threading, int totnode)
Definition: pbvh.c:3042
void BKE_pbvh_search_gather(PBVH *pbvh, BKE_pbvh_SearchCallback scb, void *search_data, PBVHNode ***array, int *tot)
Definition: pbvh.c:843
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
Definition: math_matrix.c:262
void unit_m4(float m[4][4])
Definition: rct.c:1140
void translate_m4(float mat[4][4], float tx, float ty, float tz)
Definition: math_matrix.c:2325
bool invert_m4_m4(float R[4][4], const float A[4][4])
Definition: math_matrix.c:1278
void size_to_mat4(float R[4][4], const float size[3])
Definition: math_matrix.c:2118
void mul_m4_v3(const float M[4][4], float r[3])
Definition: math_matrix.c:732
void sub_qt_qtqt(float q[4], const float a[4], const float b[4])
float normalize_qt(float q[4])
void quat_to_mat4(float mat[4][4], const float q[4])
MINLINE void copy_v4_v4(float r[4], const float a[4])
MINLINE void add_v3_fl(float r[3], float f)
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void mul_v3_fl(float r[3], float f)
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void zero_v3(float r[3])
MINLINE void add_v3_v3(float r[3], const float a[3])
void BLI_task_parallel_range(const int start, const int stop, void *userdata, TaskParallelRangeFunc func, const TaskParallelSettings *settings)
Definition: task_range.cc:110
#define UNUSED(x)
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:51
@ ME_VERT_PBVH_UPDATE
@ OPERATOR_FINISHED
void ED_region_tag_redraw(struct ARegion *region)
Definition: area.c:667
Read Guarded memory(de)allocation.
#define MEM_SAFE_FREE(v)
#define C
Definition: RandGen.cpp:39
#define NC_GEOM
Definition: WM_types.h:294
@ OPTYPE_UNDO
Definition: WM_types.h:155
@ OPTYPE_REGISTER
Definition: WM_types.h:153
#define ND_SELECT
Definition: WM_types.h:407
OperationNode * node
const Depsgraph * depsgraph
float RNA_float_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:6355
void RNA_float_set(PointerRNA *ptr, const char *name, float value)
Definition: rna_access.c:6366
int RNA_enum_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:6402
PropertyRNA * RNA_def_float(StructOrFunctionRNA *cont_, const char *identifier, float default_value, float hardmin, float hardmax, const char *ui_name, const char *ui_description, float softmin, float softmax)
Definition: rna_define.c:3825
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, int default_value, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3771
void SCULPT_orig_vert_data_update(SculptOrigVertData *orig_data, PBVHVertexIter *iter)
Definition: sculpt.c:1310
bool SCULPT_stroke_get_location(bContext *C, float out[3], const float mouse[2])
Definition: sculpt.c:7584
const float * SCULPT_active_vertex_co_get(SculptSession *ss)
Definition: sculpt.c:285
void SCULPT_vertex_random_access_ensure(SculptSession *ss)
Definition: sculpt.c:112
void SCULPT_flush_update_done(const bContext *C, Object *ob, SculptUpdateType update_flags)
Definition: sculpt.c:7819
void SCULPT_flush_update_step(bContext *C, SculptUpdateType update_flags)
Definition: sculpt.c:7759
void SCULPT_flip_v3_by_symm_area(float v[3], const ePaintSymmetryFlags symm, const ePaintSymmetryAreas symmarea, const float pivot[3])
Definition: sculpt.c:4190
bool SCULPT_mode_poll(bContext *C)
Definition: sculpt.c:6601
void SCULPT_flip_quat_by_symm_area(float quat[3], const ePaintSymmetryFlags symm, const ePaintSymmetryAreas symmarea, const float pivot[3])
Definition: sculpt.c:4209
char SCULPT_mesh_symmetry_xyz_get(Object *object)
Definition: sculpt.c:323
bool SCULPT_check_vertex_pivot_symmetry(const float vco[3], const float pco[3], const char symm)
Definition: sculpt.c:940
void SCULPT_flush_stroke_deform(Sculpt *sd, Object *ob, bool is_proxy_used)
Definition: sculpt.c:6340
ePaintSymmetryAreas SCULPT_get_vertex_symm_area(const float co[3])
Definition: sculpt.c:4175
void SCULPT_orig_vert_data_init(SculptOrigVertData *data, Object *ob, PBVHNode *node)
Definition: sculpt.c:1300
void SCULPT_filter_cache_init(bContext *C, Object *ob, Sculpt *sd, const int undo_type)
void SCULPT_filter_cache_free(SculptSession *ss)
void SCULPT_undo_push_begin(struct Object *ob, const char *name)
Definition: sculpt_undo.c:1383
SculptTransformDisplacementMode
@ SCULPT_TRANSFORM_DISPLACEMENT_ORIGINAL
@ SCULPT_TRANSFORM_DISPLACEMENT_INCREMENTAL
void SCULPT_undo_push_end_ex(const bool use_nested_undo)
Definition: sculpt_undo.c:1405
@ SCULPT_UPDATE_COORDS
Definition: sculpt_intern.h:58
SculptUndoNode * SCULPT_undo_push_node(Object *ob, PBVHNode *node, SculptUndoType type)
Definition: sculpt_undo.c:1292
@ SCULPT_UNDO_COORDS
static int sculpt_set_pivot_position_invoke(bContext *C, wmOperator *op, const wmEvent *event)
eSculptPivotPositionModes
@ SCULPT_PIVOT_POSITION_MASK_BORDER
@ SCULPT_PIVOT_POSITION_ACTIVE_VERTEX
@ SCULPT_PIVOT_POSITION_CURSOR_SURFACE
@ SCULPT_PIVOT_POSITION_UNMASKED
@ SCULPT_PIVOT_POSITION_ORIGIN
void ED_sculpt_end_transform(struct bContext *C, Object *ob)
void ED_sculpt_update_modal_transform(struct bContext *C, Object *ob)
static void sculpt_transform_task_cb(void *__restrict userdata, const int i, const TaskParallelTLS *__restrict UNUSED(tls))
void SCULPT_OT_set_pivot_position(wmOperatorType *ot)
static int sculpt_set_pivot_position_exec(bContext *C, wmOperator *op)
static EnumPropertyItem prop_sculpt_pivot_position_types[]
static void sculpt_transform_all_vertices(Sculpt *sd, Object *ob)
static void sculpt_transform_matrices_init(SculptSession *ss, const char symm, const SculptTransformDisplacementMode t_mode, float r_transform_mats[8][4][4])
void ED_sculpt_init_transform(struct bContext *C, Object *ob)
SculptTransformDisplacementMode transform_displacement_mode
PBVHNode ** nodes
struct SculptSession * sculpt
void * data
struct MVert * mvert
Definition: BKE_pbvh.h:372
float * co
Definition: BKE_pbvh.h:374
float * mask
Definition: BKE_pbvh.h:377
const float * co
float pivot_scale[3]
Definition: BKE_paint.h:569
float pivot_pos[3]
Definition: BKE_paint.h:567
struct KeyBlock * shapekey_active
Definition: BKE_paint.h:468
float init_pivot_scale[3]
Definition: BKE_paint.h:573
float pivot_rot[4]
Definition: BKE_paint.h:568
float init_pivot_rot[4]
Definition: BKE_paint.h:572
float prev_pivot_scale[3]
Definition: BKE_paint.h:577
float prev_pivot_pos[3]
Definition: BKE_paint.h:575
float init_pivot_pos[3]
Definition: BKE_paint.h:571
float prev_pivot_rot[4]
Definition: BKE_paint.h:576
struct FilterCache * filter_cache
Definition: BKE_paint.h:519
struct PBVH * pbvh
Definition: BKE_paint.h:504
bool deform_modifiers_active
Definition: BKE_paint.h:509
struct UnifiedPaintSettings unified_paint_settings
int mval[2]
Definition: WM_types.h:583
int(* invoke)(struct bContext *, struct wmOperator *, const struct wmEvent *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:752
const char * name
Definition: WM_types.h:721
const char * idname
Definition: WM_types.h:723
bool(* poll)(struct bContext *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:776
struct StructRNA * srna
Definition: WM_types.h:802
const char * description
Definition: WM_types.h:726
int(* exec)(struct bContext *, struct wmOperator *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:736
struct PointerRNA * ptr
CCL_NAMESPACE_BEGIN ccl_device float fade(float t)
Definition: svm_noise.h:37
ccl_device_inline float4 mask(const int4 &mask, const float4 &a)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
wmOperatorType * ot
Definition: wm_files.c:3156