Blender  V3.3
sculpt_mask_init.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2021 Blender Foundation. All rights reserved. */
3 
8 #include "MEM_guardedalloc.h"
9 
10 #include "BLI_blenlib.h"
11 #include "BLI_hash.h"
12 #include "BLI_math.h"
13 #include "BLI_task.h"
14 
15 #include "BLT_translation.h"
16 
17 #include "PIL_time.h"
18 
19 #include "DNA_brush_types.h"
20 #include "DNA_mesh_types.h"
21 #include "DNA_meshdata_types.h"
22 #include "DNA_object_types.h"
23 
24 #include "BKE_brush.h"
25 #include "BKE_ccg.h"
26 #include "BKE_context.h"
27 #include "BKE_mesh.h"
28 #include "BKE_multires.h"
29 #include "BKE_node.h"
30 #include "BKE_object.h"
31 #include "BKE_paint.h"
32 #include "BKE_pbvh.h"
33 
34 #include "DEG_depsgraph.h"
35 
36 #include "WM_api.h"
37 #include "WM_types.h"
38 
39 #include "RNA_access.h"
40 #include "RNA_define.h"
41 
42 #include "ED_sculpt.h"
43 #include "paint_intern.h"
44 #include "sculpt_intern.h"
45 
46 #include "bmesh.h"
47 
48 #include <math.h>
49 #include <stdlib.h>
50 
51 /* Mask Init operator. */
52 /* Initializes mask values for the entire mesh depending on the mode. */
53 
54 typedef enum eSculptMaskInitMode {
59 
61  {
63  "RANDOM_PER_VERTEX",
64  0,
65  "Random per Vertex",
66  "",
67  },
68  {
70  "RANDOM_PER_FACE_SET",
71  0,
72  "Random per Face Set",
73  "",
74  },
75  {
77  "RANDOM_PER_LOOSE_PART",
78  0,
79  "Random per Loose Part",
80  "",
81  },
82  {0, NULL, 0, NULL, NULL},
83 };
84 
85 static void mask_init_task_cb(void *__restrict userdata,
86  const int i,
87  const TaskParallelTLS *__restrict UNUSED(tls))
88 {
89  SculptThreadedTaskData *data = userdata;
90  SculptSession *ss = data->ob->sculpt;
91  PBVHNode *node = data->nodes[i];
92  PBVHVertexIter vd;
93  const int mode = data->mask_init_mode;
94  const int seed = data->mask_init_seed;
97  switch (mode) {
99  *vd.mask = BLI_hash_int_01(vd.index + seed);
100  break;
102  const int face_set = SCULPT_vertex_face_set_get(ss, vd.index);
103  *vd.mask = BLI_hash_int_01(face_set + seed);
104  break;
105  }
108  break;
109  }
110  }
113 }
114 
116 {
118  SculptSession *ss = ob->sculpt;
120 
121  const int mode = RNA_enum_get(op->ptr, "mode");
122 
123  BKE_sculpt_update_object_for_edit(depsgraph, ob, true, true, false);
124 
125  PBVH *pbvh = ob->sculpt->pbvh;
126  PBVHNode **nodes;
127  int totnode;
128  BKE_pbvh_search_gather(pbvh, NULL, NULL, &nodes, &totnode);
129 
130  if (totnode == 0) {
131  return OPERATOR_CANCELLED;
132  }
133 
134  SCULPT_undo_push_begin(ob, "init mask");
135 
138  }
139 
141  .ob = ob,
142  .nodes = nodes,
143  .mask_init_mode = mode,
144  .mask_init_seed = PIL_check_seconds_timer(),
145  };
146 
147  TaskParallelSettings settings;
148  BKE_pbvh_parallel_range_settings(&settings, true, totnode);
149  BLI_task_parallel_range(0, totnode, &data, mask_init_task_cb, &settings);
150 
152 
154 
156  MEM_SAFE_FREE(nodes);
158  return OPERATOR_FINISHED;
159 }
160 
162 {
163  /* identifiers */
164  ot->name = "Init Mask";
165  ot->description = "Creates a new mask for the entire mesh";
166  ot->idname = "SCULPT_OT_mask_init";
167 
168  /* api callbacks */
171 
174  "mode",
177  "Mode",
178  "");
179 }
struct Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C)
Definition: context.c:1528
struct Object * CTX_data_active_object(const bContext *C)
Definition: context.c:1353
void multires_stitch_grids(struct Object *)
Definition: multires.c:1178
General operations, lookup, etc. for blender objects.
void BKE_sculpt_update_object_for_edit(struct Depsgraph *depsgraph, struct Object *ob_orig, bool need_pmap, bool need_mask, bool is_paint_tool)
Definition: paint.c:1914
A BVH for high poly meshes.
#define BKE_pbvh_vertex_iter_begin(pbvh, node, vi, mode)
Definition: BKE_pbvh.h:439
#define BKE_pbvh_vertex_iter_end
Definition: BKE_pbvh.h:509
#define PBVH_ITER_UNIQUE
Definition: BKE_pbvh.h:391
void BKE_pbvh_parallel_range_settings(struct TaskParallelSettings *settings, bool use_threading, int totnode)
Definition: pbvh.c:3211
void BKE_pbvh_update_vertex_data(PBVH *pbvh, int flags)
Definition: pbvh.c:1567
void BKE_pbvh_node_mark_update_mask(PBVHNode *node)
Definition: pbvh.c:1875
void BKE_pbvh_search_gather(PBVH *pbvh, BKE_pbvh_SearchCallback scb, void *search_data, PBVHNode ***array, int *tot)
Definition: pbvh.c:838
@ PBVH_UpdateMask
Definition: BKE_pbvh.h:71
BLI_INLINE float BLI_hash_int_01(unsigned int k)
Definition: BLI_hash.h:94
void BLI_task_parallel_range(int start, int stop, void *userdata, TaskParallelRangeFunc func, const TaskParallelSettings *settings)
Definition: task_range.cc:94
#define UNUSED(x)
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:35
Object is a sort of wrapper for general info.
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
Read Guarded memory(de)allocation.
#define MEM_SAFE_FREE(v)
Platform independent time functions.
#define C
Definition: RandGen.cpp:25
@ OPTYPE_UNDO
Definition: WM_types.h:148
@ OPTYPE_REGISTER
Definition: WM_types.h:146
static unsigned long seed
Definition: btSoftBody.h:39
OperationNode * node
const Depsgraph * depsgraph
int RNA_enum_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:5004
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:3783
void SCULPT_connected_components_ensure(Object *ob)
Definition: sculpt.c:5815
bool SCULPT_mode_poll(bContext *C)
Definition: sculpt.c:3957
int SCULPT_vertex_face_set_get(SculptSession *ss, int index)
Definition: sculpt.c:508
void SCULPT_tag_update_overlays(bContext *C)
Definition: sculpt.c:1048
void SCULPT_undo_push_begin(struct Object *ob, const char *name)
Definition: sculpt_undo.c:1545
void SCULPT_undo_push_end(struct Object *ob)
Definition: sculpt_undo.c:1575
SculptUndoNode * SCULPT_undo_push_node(Object *ob, PBVHNode *node, SculptUndoType type)
Definition: sculpt_undo.c:1419
@ SCULPT_UNDO_MASK
void SCULPT_OT_mask_init(wmOperatorType *ot)
eSculptMaskInitMode
@ SCULPT_MASK_INIT_RANDOM_PER_LOOSE_PART
@ SCULPT_MASK_INIT_RANDOM_PER_FACE_SET
@ SCULPT_MASK_INIT_RANDOM_PER_VERTEX
static EnumPropertyItem prop_sculpt_mask_init_mode_types[]
static void mask_init_task_cb(void *__restrict userdata, const int i, const TaskParallelTLS *__restrict UNUSED(tls))
static int sculpt_mask_init_exec(bContext *C, wmOperator *op)
struct SculptSession * sculpt
float * mask
Definition: BKE_pbvh.h:433
SculptVertexInfo vertex_info
Definition: BKE_paint.h:608
struct PBVH * pbvh
Definition: BKE_paint.h:550
int * connected_component
Definition: BKE_paint.h:392
const char * name
Definition: WM_types.h:888
const char * idname
Definition: WM_types.h:890
bool(* poll)(struct bContext *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:943
struct StructRNA * srna
Definition: WM_types.h:969
const char * description
Definition: WM_types.h:893
int(* exec)(struct bContext *, struct wmOperator *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:903
struct PointerRNA * ptr
double PIL_check_seconds_timer(void)
Definition: time.c:64
wmOperatorType * ot
Definition: wm_files.c:3479