Blender V5.3
MOD_build.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2005 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_array_utils.hh"
10#include "MEM_guardedalloc.h"
11
12#include "BLI_utildefines.hh"
13
14#include "BLI_math_vector_c.hh"
15#include "BLI_rand_c.hh"
16
17#include "BLT_translation.hh"
18
19#include "DNA_mesh_types.h"
20#include "DNA_screen_types.h"
21
23
25#include "BKE_customdata.hh"
26#include "BKE_mesh.hh"
27#include "BKE_modifier.hh"
28#include "BKE_scene.hh"
29
31#include "UI_resources.hh"
32
33#include "RNA_access.hh"
34#include "RNA_prototypes.hh"
35
36#include "MOD_ui_common.hh"
37
38namespace blender {
39
40static void init_data(ModifierData *md)
41{
42 BuildModifierData *bmd = reinterpret_cast<BuildModifierData *>(md);
44}
45
46static bool depends_on_time(Scene * /*scene*/, ModifierData * /*md*/)
47{
48 return true;
49}
50
52{
53 Mesh *result;
54 BuildModifierData *bmd = reinterpret_cast<BuildModifierData *>(md);
55 int i, j, k;
56 int faces_dst_num, edges_dst_num, loops_dst_num = 0;
57 float frac;
58 /* maps vert indices in old mesh to indices in new mesh */
59 Map<int, int> vertHash;
60 /* maps edge indices in new mesh to indices in old mesh */
61 Map<int, int> edgeHash;
62 /* maps edge indices in old mesh to indices in new mesh */
63 Map<int, int> edgeHash2;
64
65 const int vert_src_num = mesh->verts_num;
66 const Span<int2> edges_src = mesh->edges();
67 const OffsetIndices faces_src = mesh->faces();
68 const Span<int> corner_verts_src = mesh->corner_verts();
69 const Span<int> corner_edges_src = mesh->corner_edges();
70
71 int *vertMap = MEM_new_array_uninitialized<int>(size_t(vert_src_num), __func__);
72 int *edgeMap = MEM_new_array_uninitialized<int>(size_t(edges_src.size()), __func__);
73 int *faceMap = MEM_new_array_uninitialized<int>(size_t(faces_src.size()), __func__);
74
75 array_utils::fill_index_range<int>({vertMap, vert_src_num});
76 array_utils::fill_index_range<int>({edgeMap, edges_src.size()});
78
79 Scene *scene = DEG_get_input_scene(ctx->depsgraph);
80 frac = (BKE_scene_ctime_get(scene) - bmd->start) / bmd->length;
81 CLAMP(frac, 0.0f, 1.0f);
82 if (bmd->flag & MOD_BUILD_FLAG_REVERSE) {
83 frac = 1.0f - frac;
84 }
85
86 faces_dst_num = faces_src.size() * frac;
87 edges_dst_num = edges_src.size() * frac;
88
89 /* if there's at least one face, build based on faces */
90 if (faces_dst_num) {
91 uintptr_t hash_num, hash_num_alt;
92
93 if (bmd->flag & MOD_BUILD_FLAG_RANDOMIZE) {
94 BLI_array_randomize(faceMap, sizeof(*faceMap), faces_src.size(), bmd->seed);
95 }
96
97 /* get the set of all vert indices that will be in the final mesh,
98 * mapped to the new indices
99 */
100 hash_num = 0;
101 for (i = 0; i < faces_dst_num; i++) {
102 const IndexRange face = faces_src[faceMap[i]];
103 for (j = 0; j < face.size(); j++) {
104 const int vert_i = corner_verts_src[face[j]];
105 if (vertHash.add(vert_i, hash_num)) {
106 hash_num++;
107 }
108 }
109
110 loops_dst_num += face.size();
111 }
112 BLI_assert(hash_num == vertHash.size());
113
114 /* get the set of edges that will be in the new mesh (i.e. all edges
115 * that have both verts in the new mesh)
116 */
117 hash_num = 0;
118 hash_num_alt = 0;
119 for (i = 0; i < edges_src.size(); i++, hash_num_alt++) {
120 const int2 &edge = edges_src[i];
121
122 if (vertHash.contains(edge[0]) && vertHash.contains(edge[1])) {
123 edgeHash.add(hash_num, hash_num_alt);
124 edgeHash2.add(hash_num_alt, hash_num);
125 hash_num++;
126 }
127 }
128 BLI_assert(hash_num == edgeHash.size());
129 }
130 else if (edges_dst_num) {
131 uintptr_t hash_num;
132
133 if (bmd->flag & MOD_BUILD_FLAG_RANDOMIZE) {
134 BLI_array_randomize(edgeMap, sizeof(*edgeMap), edges_src.size(), bmd->seed);
135 }
136
137 /* get the set of all vert indices that will be in the final mesh,
138 * mapped to the new indices
139 */
140 const int2 *edges = edges_src.data();
141 hash_num = 0;
142 BLI_assert(hash_num == vertHash.size());
143 for (i = 0; i < edges_dst_num; i++) {
144 const int2 &edge = edges[edgeMap[i]];
145 if (vertHash.add(edge[0], hash_num)) {
146 hash_num++;
147 }
148 if (vertHash.add(edge[1], hash_num)) {
149 hash_num++;
150 }
151 }
152 BLI_assert(hash_num == vertHash.size());
153
154 /* get the set of edges that will be in the new mesh */
155 for (i = 0; i < edges_dst_num; i++) {
156 j = edgeHash.size();
157
158 edgeHash.add(j, edgeMap[i]);
159 edgeHash2.add(edgeMap[i], j);
160 }
161 }
162 else {
163 int verts_num = vert_src_num * frac;
164
165 if (bmd->flag & MOD_BUILD_FLAG_RANDOMIZE) {
166 BLI_array_randomize(vertMap, sizeof(*vertMap), vert_src_num, bmd->seed);
167 }
168
169 /* get the set of all vert indices that will be in the final mesh,
170 * mapped to the new indices
171 */
172 for (i = 0; i < verts_num; i++) {
173 vertHash.add(vertMap[i], i);
174 }
175 }
176
177 /* now we know the number of verts, edges and faces, we can create the mesh. */
179 mesh, vertHash.size(), edgeHash.size(), faces_dst_num, loops_dst_num);
180 MutableSpan<int2> result_edges = result->edges_for_write();
181 MutableSpan<int> result_face_offsets = result->face_offsets_for_write();
182 MutableSpan<int> result_corner_verts = result->corner_verts_for_write();
183 MutableSpan<int> result_corner_edges = result->corner_edges_for_write();
184
189
190 /* copy the vertices across */
191 for (const auto &item : vertHash.items()) {
192 const int oldIndex = item.key;
193 const int newIndex = item.value;
194 vert_interp.copy(oldIndex, newIndex, 1);
195 }
196
197 /* copy the edges across, remapping indices */
198 for (i = 0; i < edgeHash.size(); i++) {
199 int2 source;
200 int2 *dest;
201 int oldIndex = edgeHash.lookup(i);
202
203 source = edges_src[oldIndex];
204 dest = &result_edges[i];
205
206 source[0] = vertHash.lookup(source[0]);
207 source[1] = vertHash.lookup(source[1]);
208
209 edge_interp.copy(oldIndex, i, 1);
210 *dest = source;
211 }
212
213 /* copy the faces across, remapping indices */
214 k = 0;
215 for (i = 0; i < faces_dst_num; i++) {
216 const IndexRange src_face = faces_src[faceMap[i]];
217 result_face_offsets[i] = k;
218
219 face_interp.copy(faceMap[i], i, 1);
220
221 corner_interp.copy(src_face.start(), k, src_face.size());
222
223 for (j = 0; j < src_face.size(); j++, k++) {
224 const int vert_src = corner_verts_src[src_face[j]];
225 const int edge_src = corner_edges_src[src_face[j]];
226 result_corner_verts[k] = vertHash.lookup(vert_src);
227 result_corner_edges[k] = edgeHash2.lookup(edge_src);
228 }
229 }
230
231 MEM_delete(vertMap);
232 MEM_delete(edgeMap);
233 MEM_delete(faceMap);
234
235 /* TODO(sybren): also copy flags & tags? */
236 return result;
237}
238
239static void panel_draw(const bContext * /*C*/, Panel *panel)
240{
241 ui::Layout &layout = *panel->layout;
242
244
245 layout.use_property_split_set(true);
246
247 layout.prop(ptr, "frame_start", UI_ITEM_NONE, std::nullopt, ICON_NONE);
248 layout.prop(ptr, "frame_duration", UI_ITEM_NONE, std::nullopt, ICON_NONE);
249 layout.prop(ptr, "use_reverse", UI_ITEM_NONE, std::nullopt, ICON_NONE);
250
251 modifier_error_message_draw(layout, ptr);
252}
253
254static void random_panel_header_draw(const bContext * /*C*/, Panel *panel)
255{
256 ui::Layout &layout = *panel->layout;
257
259
260 layout.prop(ptr, "use_random_order", UI_ITEM_NONE, std::nullopt, ICON_NONE);
261}
262
263static void random_panel_draw(const bContext * /*C*/, Panel *panel)
264{
265 ui::Layout &layout = *panel->layout;
266
268
269 layout.use_property_split_set(true);
270
271 layout.active_set(RNA_boolean_get(ptr, "use_random_order"));
272 layout.prop(ptr, "seed", UI_ITEM_NONE, std::nullopt, ICON_NONE);
273}
274
275static void panel_register(ARegionType *region_type)
276{
279 region_type, "randomize", "", random_panel_header_draw, random_panel_draw, panel_type);
280}
281
283 /*idname*/ "Build",
284 /*name*/ N_("Build"),
285 /*struct_name*/ "BuildModifierData",
286 /*struct_size*/ sizeof(BuildModifierData),
287 /*srna*/ &RNA_BuildModifier,
290 /*icon*/ ICON_MOD_BUILD,
291
292 /*copy_data*/ BKE_modifier_copydata_generic,
293
294 /*deform_verts*/ nullptr,
295 /*deform_matrices*/ nullptr,
296 /*deform_verts_EM*/ nullptr,
297 /*deform_matrices_EM*/ nullptr,
298 /*modify_mesh*/ modify_mesh,
299 /*modify_geometry_set*/ nullptr,
300
301 /*init_data*/ init_data,
302 /*required_data_mask*/ nullptr,
303 /*free_data*/ nullptr,
304 /*is_disabled*/ nullptr,
305 /*update_depsgraph*/ nullptr,
306 /*depends_on_time*/ depends_on_time,
307 /*depends_on_normals*/ nullptr,
308 /*foreach_ID_link*/ nullptr,
309 /*foreach_tex_link*/ nullptr,
310 /*free_runtime_data*/ nullptr,
311 /*panel_register*/ panel_register,
312 /*blend_write*/ nullptr,
313 /*blend_read*/ nullptr,
314 /*foreach_cache*/ nullptr,
315 /*foreach_working_space_color*/ nullptr,
316};
317
318} // namespace blender
CustomData interface, see also DNA_customdata_types.h.
Random number functions.
Read Blender memory allocation and freeing.
#define UI_ITEM_NONE
#define CLAMP(a, b, c)
#define INIT_DEFAULT_STRUCT_AFTER(struct_dst, member)
constexpr int64_t size() const
constexpr int64_t start() const
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:295
const Value & lookup(const Key &key) const
Definition BLI_map.hh:545
int64_t size() const
Definition BLI_map.hh:976
bool contains(const Key &key) const
Definition BLI_map.hh:353
ItemIterator items() const &
Definition BLI_map.hh:902
constexpr const T * data() const
Definition BLI_span.hh:219
constexpr int64_t size() const
Definition BLI_span.hh:256
void copy(int src_index, int dst_index, int count) const
CCL_NAMESPACE_BEGIN ccl_device_inline float frac(const float x, ccl_private int *ix)
const int faceMap[6][4]
void fill_index_range(MutableSpan< T > span, const T start=0)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
PointerRNA * modifier_panel_get_property_pointers(Panel *panel, PointerRNA *r_ob_ptr)
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
static bool depends_on_time(Scene *, ModifierData *)
Definition MOD_build.cc:46
static void init_data(ModifierData *md)
static void random_panel_header_draw(const bContext *, Panel *panel)
Definition MOD_build.cc:254
void BLI_array_randomize(void *data, unsigned int elem_size, unsigned int elem_num, unsigned int seed)
Definition rand.cc:158
void modifier_error_message_draw(ui::Layout &layout, PointerRNA *ptr)
VecBase< int32_t, 2 > int2
static void panel_register(ARegionType *region_type)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
static Mesh * modify_mesh(ModifierData *md, const ModifierEvalContext *ctx, Mesh *mesh)
Definition MOD_array.cc:873
@ eModifierTypeFlag_AcceptsCVs
@ eModifierTypeFlag_AcceptsMesh
_W64 unsigned int uintptr_t
Definition BKE_text.h:120
float BKE_scene_ctime_get(const Scene *scene)
Definition scene.cc:2581
PanelType * modifier_panel_register(ARegionType *region_type, ModifierType type, PanelDrawFn draw)
Scene * DEG_get_input_scene(const Depsgraph *graph)
Mesh * BKE_mesh_new_nomain_from_template(const Mesh *me_src, int verts_num, int edges_num, int faces_num, int corners_num)
static void random_panel_draw(const bContext *, Panel *panel)
Definition MOD_build.cc:263
ModifierTypeInfo modifierType_Build
Definition MOD_build.cc:282
static void panel_draw(const bContext *, Panel *panel)
PanelType * modifier_subpanel_register(ARegionType *region_type, const char *name, const char *label, PanelDrawFn draw_header, PanelDrawFn draw, PanelType *parent)
void BKE_modifier_copydata_generic(const ModifierData *md, ModifierData *md_dst, int flag)
ui::Layout * layout
void use_property_split_set(bool is_sep)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)
void active_set(bool active)
#define N_(msgid)