1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
|
/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Contributor(s): Joseph Eagar, Geoffrey Bantle, Campbell Barton
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file blender/bmesh/intern/bmesh_iterators.c
* \ingroup bmesh
*
* Functions to abstract looping over bmesh data structures.
*
* See: bmesh_iterators_inlin.c too, some functions are here for speed reasons.
*/
#include "MEM_guardedalloc.h"
#include "BLI_utildefines.h"
#include "bmesh.h"
#include "intern/bmesh_private.h"
const char bm_iter_itype_htype_map[BM_ITYPE_MAX] = {
'\0',
BM_VERT, /* BM_VERTS_OF_MESH */
BM_EDGE, /* BM_EDGES_OF_MESH */
BM_FACE, /* BM_FACES_OF_MESH */
BM_EDGE, /* BM_EDGES_OF_VERT */
BM_FACE, /* BM_FACES_OF_VERT */
BM_LOOP, /* BM_LOOPS_OF_VERT */
BM_VERT, /* BM_VERTS_OF_EDGE */
BM_FACE, /* BM_FACES_OF_EDGE */
BM_VERT, /* BM_VERTS_OF_FACE */
BM_EDGE, /* BM_EDGES_OF_FACE */
BM_LOOP, /* BM_LOOPS_OF_FACE */
BM_LOOP, /* BM_ALL_LOOPS_OF_FACE */
BM_LOOP, /* BM_LOOPS_OF_LOOP */
BM_LOOP /* BM_LOOPS_OF_EDGE */
};
/**
* \note Use #BM_vert_at_index / #BM_edge_at_index / #BM_face_at_index for mesh arrays.
*/
void *BM_iter_at_index(BMesh *bm, const char itype, void *data, int index)
{
BMIter iter;
void *val;
int i;
/* sanity check */
if (index < 0) {
return NULL;
}
val = BM_iter_new(&iter, bm, itype, data);
i = 0;
while (i < index) {
val = BM_iter_step(&iter);
i++;
}
return val;
}
/**
* \brief Iterator as Array
*
* Sometimes its convenient to get the iterator as an array
* to avoid multiple calls to #BM_iter_at_index.
*/
int BM_iter_as_array(BMesh *bm, const char itype, void *data, void **array, const int len)
{
int i = 0;
/* sanity check */
if (len > 0) {
BMIter iter;
void *ele;
for (ele = BM_iter_new(&iter, bm, itype, data); ele; ele = BM_iter_step(&iter)) {
array[i] = ele;
i++;
if (i == len) {
return len;
}
}
}
return i;
}
/**
* \brief Iterator as Array
*
* Allocates a new array, has the advantage that you dont need to know the size ahead of time.
*
* Takes advantage of less common iterator usage to avoid counting twice,
* which you might end up doing when #BM_iter_as_array is used.
*
* Caller needs to free the array.
*/
void *BM_iter_as_arrayN(BMesh *bm, const char itype, void *data, int *r_len)
{
BMIter iter;
if (BM_iter_init(&iter, bm, itype, data) && iter.count > 0) {
BMElem *ele;
BMElem **array = MEM_mallocN(sizeof(ele) * iter.count, __func__);
int i = 0;
*r_len = iter.count; /* set before iterating */
while ((ele = BM_iter_step(&iter))) {
array[i++] = ele;
}
return array;
}
else {
*r_len = 0;
return NULL;
}
}
/**
* \brief Elem Iter Flag Count
*
* Counts how many flagged / unflagged items are found in this element.
*/
int BM_iter_elem_count_flag(const char itype, void *data, const char hflag, const short value)
{
BMIter iter;
BMElem *ele;
int count = 0;
BLI_assert(ELEM(value, TRUE, FALSE));
for (ele = BM_iter_new(&iter, NULL, itype, data); ele; ele = BM_iter_step(&iter)) {
if (BM_elem_flag_test_bool(ele, hflag) == value) {
count++;
}
}
return count;
}
/**
* \brief Mesh Iter Flag Count
*
* Counts how many flagged / unflagged items are found in this mesh.
*/
int BM_iter_mesh_count_flag(const char itype, BMesh *bm, const char hflag, const short value)
{
BMIter iter;
BMElem *ele;
int count = 0;
BLI_assert(ELEM(value, TRUE, FALSE));
for (ele = BM_iter_new(&iter, bm, itype, NULL); ele; ele = BM_iter_step(&iter)) {
if (BM_elem_flag_test_bool(ele, hflag) == value) {
count++;
}
}
return count;
}
/**
* \brief Init Iterator
*
* Clears the internal state of an iterator for begin() callbacks.
*/
static void init_iterator(BMIter *iter)
{
iter->firstvert = iter->nextvert = NULL;
iter->firstedge = iter->nextedge = NULL;
iter->firstloop = iter->nextloop = NULL;
iter->firstpoly = iter->nextpoly = NULL;
iter->ldata = NULL;
}
/**
* Notes on iterator implementation:
*
* Iterators keep track of the next element in a sequence.
* When a step() callback is invoked the current value of 'next'
* is stored to be returned later and the next variable is incremented.
*
* When the end of a sequence is reached, next should always equal NULL
*
* The 'bmiter__' prefix is used because these are used in
* bmesh_iterators_inine.c but should otherwise be seen as
* private.
*/
/*
* VERT OF MESH CALLBACKS
*/
void bmiter__vert_of_mesh_begin(BMIter *iter)
{
BLI_mempool_iternew(iter->bm->vpool, &iter->pooliter);
}
void *bmiter__vert_of_mesh_step(BMIter *iter)
{
return BLI_mempool_iterstep(&iter->pooliter);
}
void bmiter__edge_of_mesh_begin(BMIter *iter)
{
BLI_mempool_iternew(iter->bm->epool, &iter->pooliter);
}
void *bmiter__edge_of_mesh_step(BMIter *iter)
{
return BLI_mempool_iterstep(&iter->pooliter);
}
void bmiter__face_of_mesh_begin(BMIter *iter)
{
BLI_mempool_iternew(iter->bm->fpool, &iter->pooliter);
}
void *bmiter__face_of_mesh_step(BMIter *iter)
{
return BLI_mempool_iterstep(&iter->pooliter);
}
/*
* EDGE OF VERT CALLBACKS
*/
void bmiter__edge_of_vert_begin(BMIter *iter)
{
init_iterator(iter);
if (iter->vdata->e) {
iter->firstedge = iter->vdata->e;
iter->nextedge = iter->vdata->e;
}
}
void *bmiter__edge_of_vert_step(BMIter *iter)
{
BMEdge *current = iter->nextedge;
if (iter->nextedge)
iter->nextedge = bmesh_disk_edge_next(iter->nextedge, iter->vdata);
if (iter->nextedge == iter->firstedge) iter->nextedge = NULL;
return current;
}
/*
* FACE OF VERT CALLBACKS
*/
void bmiter__face_of_vert_begin(BMIter *iter)
{
init_iterator(iter);
iter->count = 0;
if (iter->vdata->e)
iter->count = bmesh_disk_facevert_count(iter->vdata);
if (iter->count) {
iter->firstedge = bmesh_disk_faceedge_find_first(iter->vdata->e, iter->vdata);
iter->nextedge = iter->firstedge;
iter->firstloop = bmesh_radial_faceloop_find_first(iter->firstedge->l, iter->vdata);
iter->nextloop = iter->firstloop;
}
}
void *bmiter__face_of_vert_step(BMIter *iter)
{
BMLoop *current = iter->nextloop;
if (iter->count && iter->nextloop) {
iter->count--;
iter->nextloop = bmesh_radial_faceloop_find_next(iter->nextloop, iter->vdata);
if (iter->nextloop == iter->firstloop) {
iter->nextedge = bmesh_disk_faceedge_find_next(iter->nextedge, iter->vdata);
iter->firstloop = bmesh_radial_faceloop_find_first(iter->nextedge->l, iter->vdata);
iter->nextloop = iter->firstloop;
}
}
if (!iter->count) iter->nextloop = NULL;
return current ? current->f : NULL;
}
/*
* LOOP OF VERT CALLBACKS
*
*/
void bmiter__loop_of_vert_begin(BMIter *iter)
{
init_iterator(iter);
iter->count = 0;
if (iter->vdata->e)
iter->count = bmesh_disk_facevert_count(iter->vdata);
if (iter->count) {
iter->firstedge = bmesh_disk_faceedge_find_first(iter->vdata->e, iter->vdata);
iter->nextedge = iter->firstedge;
iter->firstloop = bmesh_radial_faceloop_find_first(iter->firstedge->l, iter->vdata);
iter->nextloop = iter->firstloop;
}
}
void *bmiter__loop_of_vert_step(BMIter *iter)
{
BMLoop *current = iter->nextloop;
if (iter->count) {
iter->count--;
iter->nextloop = bmesh_radial_faceloop_find_next(iter->nextloop, iter->vdata);
if (iter->nextloop == iter->firstloop) {
iter->nextedge = bmesh_disk_faceedge_find_next(iter->nextedge, iter->vdata);
iter->firstloop = bmesh_radial_faceloop_find_first(iter->nextedge->l, iter->vdata);
iter->nextloop = iter->firstloop;
}
}
if (!iter->count) iter->nextloop = NULL;
if (current) {
return current;
}
return NULL;
}
void bmiter__loops_of_edge_begin(BMIter *iter)
{
BMLoop *l;
l = iter->edata->l;
/* note sure why this sets ldata ... */
init_iterator(iter);
iter->firstloop = iter->nextloop = l;
}
void *bmiter__loops_of_edge_step(BMIter *iter)
{
BMLoop *current = iter->nextloop;
if (iter->nextloop) {
iter->nextloop = iter->nextloop->radial_next;
}
if (iter->nextloop == iter->firstloop) {
iter->nextloop = NULL;
}
if (current) {
return current;
}
return NULL;
}
void bmiter__loops_of_loop_begin(BMIter *iter)
{
BMLoop *l;
l = iter->ldata;
/* note sure why this sets ldata ... */
init_iterator(iter);
iter->firstloop = l;
iter->nextloop = iter->firstloop->radial_next;
if (iter->nextloop == iter->firstloop)
iter->nextloop = NULL;
}
void *bmiter__loops_of_loop_step(BMIter *iter)
{
BMLoop *current = iter->nextloop;
if (iter->nextloop) {
iter->nextloop = iter->nextloop->radial_next;
}
if (iter->nextloop == iter->firstloop) {
iter->nextloop = NULL;
}
if (current) {
return current;
}
return NULL;
}
/*
* FACE OF EDGE CALLBACKS
*/
void bmiter__face_of_edge_begin(BMIter *iter)
{
init_iterator(iter);
if (iter->edata->l) {
iter->firstloop = iter->edata->l;
iter->nextloop = iter->edata->l;
}
}
void *bmiter__face_of_edge_step(BMIter *iter)
{
BMLoop *current = iter->nextloop;
if (iter->nextloop) {
iter->nextloop = iter->nextloop->radial_next;
}
if (iter->nextloop == iter->firstloop) iter->nextloop = NULL;
return current ? current->f : NULL;
}
/*
* VERTS OF EDGE CALLBACKS
*/
void bmiter__vert_of_edge_begin(BMIter *iter)
{
init_iterator(iter);
iter->count = 0;
}
void *bmiter__vert_of_edge_step(BMIter *iter)
{
iter->count++;
switch (iter->count) {
case 1:
return iter->edata->v1;
case 2:
return iter->edata->v2;
default:
return NULL;
}
}
/*
* VERT OF FACE CALLBACKS
*/
void bmiter__vert_of_face_begin(BMIter *iter)
{
init_iterator(iter);
iter->firstloop = iter->nextloop = BM_FACE_FIRST_LOOP(iter->pdata);
}
void *bmiter__vert_of_face_step(BMIter *iter)
{
BMLoop *current = iter->nextloop;
if (iter->nextloop) iter->nextloop = iter->nextloop->next;
if (iter->nextloop == iter->firstloop) iter->nextloop = NULL;
return current ? current->v : NULL;
}
/*
* EDGE OF FACE CALLBACKS
*/
void bmiter__edge_of_face_begin(BMIter *iter)
{
init_iterator(iter);
iter->firstloop = iter->nextloop = BM_FACE_FIRST_LOOP(iter->pdata);
}
void *bmiter__edge_of_face_step(BMIter *iter)
{
BMLoop *current = iter->nextloop;
if (iter->nextloop) iter->nextloop = iter->nextloop->next;
if (iter->nextloop == iter->firstloop) iter->nextloop = NULL;
return current ? current->e : NULL;
}
/*
* LOOP OF FACE CALLBACKS
*/
void bmiter__loop_of_face_begin(BMIter *iter)
{
init_iterator(iter);
iter->firstloop = iter->nextloop = BM_FACE_FIRST_LOOP(iter->pdata);
}
void *bmiter__loop_of_face_step(BMIter *iter)
{
BMLoop *current = iter->nextloop;
if (iter->nextloop) iter->nextloop = iter->nextloop->next;
if (iter->nextloop == iter->firstloop) iter->nextloop = NULL;
return current;
}
|