Blender V5.3
mesh_iterators.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include "BKE_customdata.hh"
12#include "BKE_editmesh.hh"
13#include "BKE_editmesh_cache.hh"
14#include "BKE_mesh.hh"
15#include "BKE_mesh_iterators.hh"
16
17#include "BLI_bitmap.hh"
18#include "BLI_math_vector_c.hh"
19
20#include "MEM_guardedalloc.h"
21
22namespace blender {
23
24/* General note on iterating verts/loops/edges/faces and end mode.
25 *
26 * The edit mesh pointer is set for both final and cage meshes in both cases when there are
27 * modifiers applied and not. This helps consistency of checks in the draw manager, where the
28 * existence of the edit mesh pointer does not depend on object configuration.
29 *
30 * For the iterating, however, we need to follow the `CD_ORIGINDEX` code paths when there are
31 * modifiers applied on the cage. In the code terms it means that the check for the edit mode code
32 * path needs to consist of both edit mesh and edit data checks. */
33
35 const Mesh *mesh,
36 void (*func)(void *user_data, int index, const float co[3], const float no[3]),
37 void *user_data,
38 MeshForeachFlag flag)
39{
40 if (mesh->runtime->edit_mesh != nullptr && mesh->runtime->edit_data != nullptr) {
42 BMIter iter;
43 BMVert *eve;
44 int i;
45 if (!mesh->runtime->edit_data->vert_positions.is_empty()) {
46 const Span<float3> positions = mesh->runtime->edit_data->vert_positions;
47 Span<float3> vert_normals;
48 if (flag & MESH_FOREACH_USE_NORMAL) {
49 vert_normals = BKE_editmesh_cache_ensure_vert_normals(*const_cast<BMesh *>(bm),
50 *mesh->runtime->edit_data);
51 }
52 BM_ITER_MESH_INDEX (eve, &iter, const_cast<BMesh *>(bm), BM_VERTS_OF_MESH, i) {
53 const float *no = (flag & MESH_FOREACH_USE_NORMAL) ? &vert_normals[i].x : nullptr;
54 func(user_data, i, positions[i], no);
55 }
56 }
57 else {
58 BM_ITER_MESH_INDEX (eve, &iter, const_cast<BMesh *>(bm), BM_VERTS_OF_MESH, i) {
59 const float *no = (flag & MESH_FOREACH_USE_NORMAL) ? eve->no : nullptr;
60 func(user_data, i, eve->co, no);
61 }
62 }
63 }
64 else {
65 const Span<float3> positions = mesh->vert_positions();
66 const int *index = static_cast<const int *>(
68 Span<float3> vert_normals;
69 if (flag & MESH_FOREACH_USE_NORMAL) {
70 vert_normals = mesh->vert_normals();
71 }
72
73 if (index) {
74 for (int i = 0; i < mesh->verts_num; i++) {
75 const float *no = (flag & MESH_FOREACH_USE_NORMAL) ? &vert_normals[i].x : nullptr;
76 const int orig = *index++;
77 if (orig == ORIGINDEX_NONE) {
78 continue;
79 }
80 func(user_data, orig, positions[i], no);
81 }
82 }
83 else {
84 for (int i = 0; i < mesh->verts_num; i++) {
85 const float *no = (flag & MESH_FOREACH_USE_NORMAL) ? &vert_normals[i].x : nullptr;
86 func(user_data, i, positions[i], no);
87 }
88 }
89 }
90}
91
93 const Mesh *mesh,
94 const int tot_edges,
95 void (*func)(void *user_data, int index, const float v0co[3], const float v1co[3]),
96 void *user_data)
97{
98 if (mesh->runtime->edit_mesh != nullptr && mesh->runtime->edit_data) {
99 BMesh *bm = const_cast<BMesh *>(BKE_editmesh_bmesh_get(mesh));
100 BMIter iter;
101 BMEdge *eed;
102 int i;
103 if (!mesh->runtime->edit_data->vert_positions.is_empty()) {
104 const Span<float3> positions = mesh->runtime->edit_data->vert_positions;
106 BM_ITER_MESH_INDEX (eed, &iter, bm, BM_EDGES_OF_MESH, i) {
107 func(user_data,
108 i,
109 positions[BM_elem_index_get(eed->v1)],
110 positions[BM_elem_index_get(eed->v2)]);
111 }
112 }
113 else {
114 BM_ITER_MESH_INDEX (eed, &iter, bm, BM_EDGES_OF_MESH, i) {
115 func(user_data, i, eed->v1->co, eed->v2->co);
116 }
117 }
118 }
119 else {
120 const Span<float3> positions = mesh->vert_positions();
121 const Span<int2> edges = mesh->edges();
122 const int *index = static_cast<const int *>(
124
125 if (index) {
126 for (const int i : edges.index_range()) {
127
128 const int orig = *index++;
129 if (orig == ORIGINDEX_NONE) {
130 continue;
131 }
132 func(user_data, orig, positions[edges[i][0]], positions[edges[i][1]]);
133 }
134 }
135 else if (mesh->edges_num == tot_edges) {
136 for (const int i : edges.index_range()) {
137 func(user_data, i, positions[edges[i][0]], positions[edges[i][1]]);
138 }
139 }
140 }
141}
142
144 void (*func)(void *user_data,
145 int vertex_index,
146 int face_index,
147 const float co[3],
148 const float no[3]),
149 void *user_data,
150 MeshForeachFlag flag)
151{
152
153 /* We can't use `dm->getLoopDataLayout(dm)` here,
154 * we want to always access `dm->loopData`, `EditDerivedBMesh` would
155 * return loop data from BMesh itself. */
156 if (mesh->runtime->edit_mesh != nullptr && mesh->runtime->edit_data) {
157 BMesh *bm = const_cast<BMesh *>(BKE_editmesh_bmesh_get(mesh));
158 BMIter iter;
159 BMFace *efa;
160
161 const Span<float3> positions = mesh->runtime->edit_data->vert_positions;
162
163 /* XXX: investigate using EditMesh data. */
164 Span<float3> corner_normals;
165 if (flag & MESH_FOREACH_USE_NORMAL) {
166 corner_normals = mesh->corner_normals();
167 }
168
169 int f_idx;
170
172
173 BM_ITER_MESH_INDEX (efa, &iter, bm, BM_FACES_OF_MESH, f_idx) {
174 BMLoop *l_iter, *l_first;
175
176 l_iter = l_first = BM_FACE_FIRST_LOOP(efa);
177 do {
178 const BMVert *eve = l_iter->v;
179 const int v_idx = BM_elem_index_get(eve);
180 func(user_data,
181 v_idx,
182 f_idx,
183 positions.is_empty() ? positions[v_idx] : float3(eve->co),
184 corner_normals.is_empty() ? nullptr : &corner_normals[BM_elem_index_get(l_iter)].x);
185 } while ((l_iter = l_iter->next) != l_first);
186 }
187 }
188 else {
189 Span<float3> corner_normals;
190 if (flag & MESH_FOREACH_USE_NORMAL) {
191 corner_normals = mesh->corner_normals();
192 }
193
194 const Span<float3> positions = mesh->vert_positions();
195 const OffsetIndices faces = mesh->faces();
196 const Span<int> corner_verts = mesh->corner_verts();
197 const int *v_index = static_cast<const int *>(
199 const int *f_index = static_cast<const int *>(
201
202 if (v_index || f_index) {
203 for (const int face_i : faces.index_range()) {
204 for (const int corner : faces[face_i]) {
205 const int vert = corner_verts[corner];
206 const int v_idx = v_index ? v_index[vert] : vert;
207 const int f_idx = f_index ? f_index[face_i] : face_i;
208 const float *no = corner_normals.is_empty() ? nullptr : &corner_normals[corner].x;
209 if (ELEM(ORIGINDEX_NONE, v_idx, f_idx)) {
210 continue;
211 }
212 func(user_data, v_idx, f_idx, positions[vert], no);
213 }
214 }
215 }
216 else {
217 for (const int face_i : faces.index_range()) {
218 for (const int corner : faces[face_i]) {
219 const int vert = corner_verts[corner];
220 const float *no = corner_normals.is_empty() ? nullptr : &corner_normals[corner].x;
221 func(user_data, vert, face_i, positions[vert], no);
222 }
223 }
224 }
225 }
226}
227
229 const Mesh *mesh,
230 void (*func)(void *user_data, int index, const float cent[3], const float no[3]),
231 void *user_data,
232 MeshForeachFlag flag)
233{
234 if (mesh->runtime->edit_mesh != nullptr && mesh->runtime->edit_data != nullptr) {
235 BMesh *bm = const_cast<BMesh *>(BKE_editmesh_bmesh_get(mesh));
236 BMFace *efa;
237 BMIter iter;
238 int i;
239
241 *bm, *mesh->runtime->edit_data);
242
243 Span<float3> face_normals;
244 if (flag & MESH_FOREACH_USE_NORMAL) {
245 face_normals = BKE_editmesh_cache_ensure_face_normals(*bm, *mesh->runtime->edit_data);
246 }
247
248 if (!face_normals.is_empty()) {
249 BM_ITER_MESH_INDEX (efa, &iter, bm, BM_FACES_OF_MESH, i) {
250 const float *no = face_normals[i];
251 func(user_data, i, face_centers[i], no);
252 }
253 }
254 else {
255 BM_ITER_MESH_INDEX (efa, &iter, bm, BM_FACES_OF_MESH, i) {
256 const float *no = (flag & MESH_FOREACH_USE_NORMAL) ? efa->no : nullptr;
257 func(user_data, i, face_centers[i], no);
258 }
259 }
260 }
261 else {
262 const Span<float3> positions = mesh->vert_positions();
263 const OffsetIndices faces = mesh->faces();
264 const Span<int> corner_verts = mesh->corner_verts();
265 const int *index = static_cast<const int *>(
267
268 if (index) {
269 for (const int i : faces.index_range()) {
270 const int orig = *index++;
271 if (orig == ORIGINDEX_NONE) {
272 continue;
273 }
274 const Span<int> face_verts = corner_verts.slice(faces[i]);
275 const float3 center = bke::mesh::face_center_calc(positions, face_verts);
276 if (flag & MESH_FOREACH_USE_NORMAL) {
277 const float3 normal = bke::mesh::face_normal_calc(positions, face_verts);
278 func(user_data, orig, center, normal);
279 }
280 else {
281 func(user_data, orig, center, nullptr);
282 }
283 }
284 }
285 else {
286 for (const int i : faces.index_range()) {
287 const Span<int> face_verts = corner_verts.slice(faces[i]);
288 const float3 center = bke::mesh::face_center_calc(positions, face_verts);
289 if (flag & MESH_FOREACH_USE_NORMAL) {
290 const float3 normal = bke::mesh::face_normal_calc(positions, face_verts);
291 func(user_data, i, center, normal);
292 }
293 else {
294 func(user_data, i, center, nullptr);
295 }
296 }
297 }
298 }
299}
300
302 const Mesh *mesh,
303 void (*func)(void *user_data, int index, const float cent[3], const float no[3]),
304 void *user_data,
305 MeshForeachFlag flag)
306{
307 const Span<float3> positions = mesh->vert_positions();
308 const OffsetIndices faces = mesh->faces();
309 const Span<int> corner_verts = mesh->corner_verts();
310 Span<float3> vert_normals;
311 if (flag & MESH_FOREACH_USE_NORMAL) {
312 vert_normals = mesh->vert_normals();
313 }
314 const int *index = static_cast<const int *>(
316 const BitSpan facedot_tags = mesh->runtime->subsurf_face_dot_tags;
317
318 if (index) {
319 for (const int i : faces.index_range()) {
320 const int orig = *index++;
321 if (orig == ORIGINDEX_NONE) {
322 continue;
323 }
324 for (const int vert : corner_verts.slice(faces[i])) {
325 if (facedot_tags[vert]) {
326 func(user_data,
327 orig,
328 positions[vert],
329 (flag & MESH_FOREACH_USE_NORMAL) ? &vert_normals[vert].x : nullptr);
330 }
331 }
332 }
333 }
334 else {
335 for (const int i : faces.index_range()) {
336 for (const int vert : corner_verts.slice(faces[i])) {
337 if (facedot_tags[vert]) {
338 func(user_data,
339 i,
340 positions[vert],
341 (flag & MESH_FOREACH_USE_NORMAL) ? &vert_normals[vert].x : nullptr);
342 }
343 }
344 }
345 }
346}
347
348/* Helpers based on above foreach loopers> */
349
354
355static void get_vertexcos__mapFunc(void *user_data,
356 int index,
357 const float co[3],
358 const float /*no*/[3])
359{
360 MappedVCosData *mapped_vcos_data = static_cast<MappedVCosData *>(user_data);
361
362 if (BLI_BITMAP_TEST(mapped_vcos_data->vertex_visit, index) == 0) {
363 /* We need coord from prototype vertex, not from copies,
364 * we assume they stored in the beginning of vertex array stored in evaluated mesh
365 * (mirror modifier for eg does this). */
366 copy_v3_v3(mapped_vcos_data->vertexcos[index], co);
367 BLI_BITMAP_ENABLE(mapped_vcos_data->vertex_visit, index);
368 }
369}
370
372 float (*r_cos)[3],
373 const int totcos)
374{
375 MappedVCosData user_data;
376 memset(r_cos, 0, sizeof(*r_cos) * totcos);
377 user_data.vertexcos = r_cos;
378 user_data.vertex_visit = BLI_BITMAP_NEW(totcos, __func__);
380 MEM_delete(user_data.vertex_visit);
381}
382
383} // namespace blender
CustomData interface, see also DNA_customdata_types.h.
#define ORIGINDEX_NONE
#define BLI_BITMAP_NEW(_num, _alloc_string)
Definition BLI_bitmap.hh:39
#define BLI_BITMAP_TEST(_bitmap, _index)
Definition BLI_bitmap.hh:63
#define BLI_BITMAP_ENABLE(_bitmap, _index)
Definition BLI_bitmap.hh:80
Read Blender memory allocation and freeing.
#define ELEM(...)
#define BM_FACE_FIRST_LOOP(p)
#define BM_elem_index_get(ele)
#define BM_ITER_MESH_INDEX(ele, iter, bm, itype, indexvar)
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:141
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
constexpr bool is_empty() const
Definition BLI_span.hh:264
float3 face_normal_calc(Span< float3 > vert_positions, Span< int > face_verts)
float3 face_center_calc(Span< float3 > vert_positions, Span< int > face_verts)
BMesh Iterators.
Span< float3 > BKE_editmesh_cache_ensure_vert_normals(BMesh &bm, bke::EditMeshData &emd)
void BM_mesh_elem_index_ensure(BMesh *bm, const char htype)
MINLINE void copy_v3_v3(float r[3], const float a[3])
static void get_vertexcos__mapFunc(void *user_data, int index, const float co[3], const float[3])
Span< float3 > BKE_editmesh_cache_ensure_face_centers(BMesh &bm, bke::EditMeshData &emd)
void BKE_mesh_foreach_mapped_vert_coords_get(const Mesh *mesh_eval, float(*r_cos)[3], int totcos)
void BKE_mesh_foreach_mapped_subdiv_face_center(const Mesh *mesh, void(*func)(void *user_data, int index, const float cent[3], const float no[3]), void *user_data, MeshForeachFlag flag)
void BKE_mesh_foreach_mapped_face_center(const Mesh *mesh, void(*func)(void *user_data, int index, const float cent[3], const float no[3]), void *user_data, MeshForeachFlag flag)
const void * CustomData_get_layer(const CustomData *data, eCustomDataType type)
unsigned int BLI_bitmap
Definition BLI_bitmap.hh:15
void BKE_mesh_foreach_mapped_edge(const Mesh *mesh, int tot_edges, void(*func)(void *user_data, int index, const float v0co[3], const float v1co[3]), void *user_data)
void BKE_mesh_foreach_mapped_vert(const Mesh *mesh, void(*func)(void *user_data, int index, const float co[3], const float no[3]), void *user_data, MeshForeachFlag flag)
const BMesh * BKE_editmesh_bmesh_get(const Object *ob)
Definition editmesh.cc:65
Span< float3 > BKE_editmesh_cache_ensure_face_normals(BMesh &bm, bke::EditMeshData &emd)
VecBase< float, 3 > float3
void BKE_mesh_foreach_mapped_loop(const Mesh *mesh, void(*func)(void *user_data, int vert, int face_index, const float co[3], const float no[3]), void *user_data, MeshForeachFlag flag)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
struct BMLoop * next
struct BMVert * v