Blender  V2.93
sculpt_mask_init.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) 2021 Blender Foundation.
17  * All rights reserved.
18  */
19 
24 #include "MEM_guardedalloc.h"
25 
26 #include "BLI_blenlib.h"
27 #include "BLI_hash.h"
28 #include "BLI_math.h"
29 #include "BLI_task.h"
30 
31 #include "BLT_translation.h"
32 
33 #include "PIL_time.h"
34 
35 #include "DNA_brush_types.h"
36 #include "DNA_mesh_types.h"
37 #include "DNA_meshdata_types.h"
38 #include "DNA_object_types.h"
39 
40 #include "BKE_brush.h"
41 #include "BKE_ccg.h"
42 #include "BKE_context.h"
43 #include "BKE_mesh.h"
44 #include "BKE_multires.h"
45 #include "BKE_node.h"
46 #include "BKE_object.h"
47 #include "BKE_paint.h"
48 #include "BKE_pbvh.h"
49 
50 #include "DEG_depsgraph.h"
51 
52 #include "WM_api.h"
53 #include "WM_types.h"
54 
55 #include "RNA_access.h"
56 #include "RNA_define.h"
57 
58 #include "ED_sculpt.h"
59 #include "paint_intern.h"
60 #include "sculpt_intern.h"
61 
62 #include "bmesh.h"
63 
64 #include <math.h>
65 #include <stdlib.h>
66 
67 /* Mask Init operator. */
68 /* Initializes mask values for the entire mesh depending on the mode. */
69 
70 typedef enum eSculptMaskInitMode {
75 
77  {
79  "RANDOM_PER_VERTEX",
80  0,
81  "Random per Vertex",
82  "",
83  },
84  {
86  "RANDOM_PER_FACE_SET",
87  0,
88  "Random per Face Set",
89  "",
90  },
91  {
93  "RANDOM_PER_LOOSE_PART",
94  0,
95  "Random per Loose Part",
96  "",
97  },
98  {0, NULL, 0, NULL, NULL},
99 };
100 
101 static void mask_init_task_cb(void *__restrict userdata,
102  const int i,
103  const TaskParallelTLS *__restrict UNUSED(tls))
104 {
105  SculptThreadedTaskData *data = userdata;
106  SculptSession *ss = data->ob->sculpt;
107  PBVHNode *node = data->nodes[i];
108  PBVHVertexIter vd;
109  const int mode = data->mask_init_mode;
110  const int seed = data->mask_init_seed;
113  switch (mode) {
115  *vd.mask = BLI_hash_int_01(vd.index + seed);
116  break;
118  const int face_set = SCULPT_vertex_face_set_get(ss, vd.index);
119  *vd.mask = BLI_hash_int_01(face_set + seed);
120  break;
121  }
124  break;
125  }
126  }
129 }
130 
132 {
134  SculptSession *ss = ob->sculpt;
136 
137  const int mode = RNA_enum_get(op->ptr, "mode");
138 
139  BKE_sculpt_update_object_for_edit(depsgraph, ob, true, true, false);
140 
141  PBVH *pbvh = ob->sculpt->pbvh;
142  PBVHNode **nodes;
143  int totnode;
144  BKE_pbvh_search_gather(pbvh, NULL, NULL, &nodes, &totnode);
145 
146  if (totnode == 0) {
147  return OPERATOR_CANCELLED;
148  }
149 
150  SCULPT_undo_push_begin(ob, "init mask");
151 
154  }
155 
157  .ob = ob,
158  .nodes = nodes,
159  .mask_init_mode = mode,
160  .mask_init_seed = PIL_check_seconds_timer(),
161  };
162 
163  TaskParallelSettings settings;
164  BKE_pbvh_parallel_range_settings(&settings, true, totnode);
165  BLI_task_parallel_range(0, totnode, &data, mask_init_task_cb, &settings);
166 
168 
170 
172  MEM_SAFE_FREE(nodes);
174  return OPERATOR_FINISHED;
175 }
176 
178 {
179  /* identifiers */
180  ot->name = "Init Mask";
181  ot->description = "Creates a new mask for the entire mesh";
182  ot->idname = "SCULPT_OT_mask_init";
183 
184  /* api callbacks */
187 
190  "mode",
193  "Mode",
194  "");
195 }
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
void multires_stitch_grids(struct Object *)
Definition: multires.c:1205
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 need_colors)
Definition: paint.c:1817
A BVH for high poly meshes.
#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_update_vertex_data(PBVH *pbvh, int flags)
Definition: pbvh.c:1432
void BKE_pbvh_node_mark_update_mask(PBVHNode *node)
Definition: pbvh.c:1738
void BKE_pbvh_search_gather(PBVH *pbvh, BKE_pbvh_SearchCallback scb, void *search_data, PBVHNode ***array, int *tot)
Definition: pbvh.c:843
@ PBVH_UpdateMask
Definition: BKE_pbvh.h:71
BLI_INLINE float BLI_hash_int_01(unsigned int k)
Definition: BLI_hash.h:108
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
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:39
@ OPTYPE_UNDO
Definition: WM_types.h:155
@ OPTYPE_REGISTER
Definition: WM_types.h:153
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:6402
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_connected_components_ensure(Object *ob)
Definition: sculpt.c:9007
bool SCULPT_mode_poll(bContext *C)
Definition: sculpt.c:6601
int SCULPT_vertex_face_set_get(SculptSession *ss, int index)
Definition: sculpt.c:514
void SCULPT_tag_update_overlays(bContext *C)
Definition: sculpt.c:1066
void SCULPT_undo_push_begin(struct Object *ob, const char *name)
Definition: sculpt_undo.c:1383
void SCULPT_undo_push_end(void)
Definition: sculpt_undo.c:1400
SculptUndoNode * SCULPT_undo_push_node(Object *ob, PBVHNode *node, SculptUndoType type)
Definition: sculpt_undo.c:1292
@ 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:377
SculptVertexInfo vertex_info
Definition: BKE_paint.h:563
struct PBVH * pbvh
Definition: BKE_paint.h:504
int * connected_component
Definition: BKE_paint.h:355
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
double PIL_check_seconds_timer(void)
Definition: time.c:80
wmOperatorType * ot
Definition: wm_files.c:3156