2
* ***** BEGIN GPL LICENSE BLOCK *****
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public License
6
* as published by the Free Software Foundation; either version 2
7
* of the License, or (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software Foundation,
16
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
* The Original Code is Copyright (C) 2007 Blender Foundation.
19
* All rights reserved.
21
* The Original Code is: all of this file.
23
* Contributor(s): Geoffrey Bantle.
25
* ***** END GPL LICENSE BLOCK *****
28
/** \file blender/bmesh/intern/bmesh_structure.c
31
* Low level routines for manipulating the BM structure.
34
#include "BLI_utildefines.h"
37
#include "intern/bmesh_private.h"
40
* MISC utility functions.
43
int bmesh_vert_in_edge(BMEdge *e, BMVert *v)
45
if (e->v1 == v || e->v2 == v) return TRUE;
48
int bmesh_verts_in_edge(BMVert *v1, BMVert *v2, BMEdge *e)
50
if (e->v1 == v1 && e->v2 == v2) return TRUE;
51
else if (e->v1 == v2 && e->v2 == v1) return TRUE;
55
BMVert *bmesh_edge_other_vert_get(BMEdge *e, BMVert *v)
60
else if (e->v2 == v) {
66
int bmesh_edge_swapverts(BMEdge *e, BMVert *orig, BMVert *newv)
70
e->v1_disk_link.next = e->v1_disk_link.prev = NULL;
73
else if (e->v2 == orig) {
75
e->v2_disk_link.next = e->v2_disk_link.prev = NULL;
82
* \section bm_cycles BMesh Cycles
83
* (this is somewhat outdate, though bits of its API are still used) - joeedh
85
* Cycles are circular doubly linked lists that form the basis of adjacency
86
* information in the BME modeler. Full adjacency relations can be derived
87
* from examining these cycles very quickly. Although each cycle is a double
88
* circular linked list, each one is considered to have a 'base' or 'head',
89
* and care must be taken by Euler code when modifying the contents of a cycle.
91
* The contents of this file are split into two parts. First there are the
92
* bmesh_cycle family of functions which are generic circular double linked list
93
* procedures. The second part contains higher level procedures for supporting
94
* modification of specific cycle types.
96
* The three cycles explicitly stored in the BM data structure are as follows:
99
* 1: The Disk Cycle - A circle of edges around a vertex
100
* Base: vertex->edge pointer.
102
* This cycle is the most complicated in terms of its structure. Each bmesh_Edge contains
103
* two bmesh_CycleNode structures to keep track of that edges membership in the disk cycle
104
* of each of its vertices. However for any given vertex it may be the first in some edges
105
* in its disk cycle and the second for others. The bmesh_disk_XXX family of functions contain
106
* some nice utilities for navigating disk cycles in a way that hides this detail from the
109
* Note that the disk cycle is completely independent from face data. One advantage of this
110
* is that wire edges are fully integrated into the topology database. Another is that the
111
* the disk cycle has no problems dealing with non-manifold conditions involving faces.
113
* Functions relating to this cycle:
114
* - #bmesh_disk_edge_append
115
* - #bmesh_disk_edge_remove
116
* - #bmesh_disk_edge_next
117
* - #bmesh_disk_edge_prev
118
* - #bmesh_disk_facevert_count
119
* - #bmesh_disk_faceedge_find_first
120
* - #bmesh_disk_faceedge_find_next
123
* 2: The Radial Cycle - A circle of face edges (bmesh_Loop) around an edge
124
* Base: edge->l->radial structure.
126
* The radial cycle is similar to the radial cycle in the radial edge data structure.*
127
* Unlike the radial edge however, the radial cycle does not require a large amount of memory
128
* to store non-manifold conditions since BM does not keep track of region/shell information.
130
* Functions relating to this cycle:
131
* - #bmesh_radial_append
132
* - #bmesh_radial_loop_remove
133
* - #bmesh_radial_face_find
134
* - #bmesh_radial_facevert_count
135
* - #bmesh_radial_faceloop_find_first
136
* - #bmesh_radial_faceloop_find_next
137
* - #bmesh_radial_validate
140
* 3: The Loop Cycle - A circle of face edges around a polygon.
141
* Base: polygon->lbase.
143
* The loop cycle keeps track of a faces vertices and edges. It should be noted that the
144
* direction of a loop cycle is either CW or CCW depending on the face normal, and is
145
* not oriented to the faces editedges.
147
* Functions relating to this cycle:
148
* - bmesh_cycle_XXX family of functions.
151
* \note the order of elements in all cycles except the loop cycle is undefined. This
152
* leads to slightly increased seek time for deriving some adjacency relations, however the
153
* advantage is that no intrinsic properties of the data structures are dependent upon the
154
* cycle order and all non-manifold conditions are represented trivially.
156
int bmesh_disk_edge_append(BMEdge *e, BMVert *v)
159
BMDiskLink *dl1 = BM_DISK_EDGE_LINK_GET(e, v);
162
dl1->next = dl1->prev = e;
165
BMDiskLink *dl1, *dl2, *dl3;
167
dl1 = BM_DISK_EDGE_LINK_GET(e, v);
168
dl2 = BM_DISK_EDGE_LINK_GET(v->e, v);
169
dl3 = dl2->prev ? BM_DISK_EDGE_LINK_GET(dl2->prev, v) : NULL;
172
dl1->prev = dl2->prev;
182
void bmesh_disk_edge_remove(BMEdge *e, BMVert *v)
184
BMDiskLink *dl1, *dl2;
186
dl1 = BM_DISK_EDGE_LINK_GET(e, v);
188
dl2 = BM_DISK_EDGE_LINK_GET(dl1->prev, v);
189
dl2->next = dl1->next;
193
dl2 = BM_DISK_EDGE_LINK_GET(dl1->next, v);
194
dl2->prev = dl1->prev;
198
v->e = (e != dl1->next) ? dl1->next : NULL;
200
dl1->next = dl1->prev = NULL;
204
* \brief Next Disk Edge
206
* Find the next edge in a disk cycle
208
* \return Pointer to the next edge in the disk cycle for the vertex v.
210
BMEdge *bmesh_disk_edge_next(BMEdge *e, BMVert *v)
213
return e->v1_disk_link.next;
215
return e->v2_disk_link.next;
219
BMEdge *bmesh_disk_edge_prev(BMEdge *e, BMVert *v)
222
return e->v1_disk_link.prev;
224
return e->v2_disk_link.prev;
228
BMEdge *bmesh_disk_edge_exists(BMVert *v1, BMVert *v2)
230
BMEdge *e_iter, *e_first;
233
e_first = e_iter = v1->e;
236
if (bmesh_verts_in_edge(v1, v2, e_iter)) {
239
} while ((e_iter = bmesh_disk_edge_next(e_iter, v1)) != e_first);
245
int bmesh_disk_count(BMVert *v)
248
BMEdge *e_first, *e_iter;
251
e_iter = e_first = v->e;
258
if (count >= (1 << 20)) {
259
printf("bmesh error: infinite loop in disk cycle!\n");
263
} while ((e_iter = bmesh_disk_edge_next(e_iter, v)) != e_first);
271
int bmesh_disk_validate(int len, BMEdge *e, BMVert *v)
275
if (!BM_vert_in_edge(e, v))
277
if (bmesh_disk_count(v) != len || len == 0)
282
if (len != 1 && bmesh_disk_edge_prev(e_iter, v) == e_iter) {
285
} while ((e_iter = bmesh_disk_edge_next(e_iter, v)) != e);
291
* \brief DISK COUNT FACE VERT
293
* Counts the number of loop users
294
* for this vertex. Note that this is
295
* equivalent to counting the number of
296
* faces incident upon this vertex
298
int bmesh_disk_facevert_count(BMVert *v)
300
/* is there an edge on this vert at all */
302
BMEdge *e_first, *e_iter;
305
/* first, loop around edge */
306
e_first = e_iter = v->e;
309
count += bmesh_radial_facevert_count(e_iter->l, v);
311
} while ((e_iter = bmesh_disk_edge_next(e_iter, v)) != e_first);
320
* \brief FIND FIRST FACE EDGE
322
* Finds the first edge in a vertices
323
* Disk cycle that has one of this
324
* vert's loops attached
327
BMEdge *bmesh_disk_faceedge_find_first(BMEdge *e, BMVert *v)
329
BMEdge *searchedge = NULL;
332
if (searchedge->l && bmesh_radial_facevert_count(searchedge->l, v)) {
335
} while ((searchedge = bmesh_disk_edge_next(searchedge, v)) != e);
340
BMEdge *bmesh_disk_faceedge_find_next(BMEdge *e, BMVert *v)
342
BMEdge *searchedge = NULL;
343
searchedge = bmesh_disk_edge_next(e, v);
345
if (searchedge->l && bmesh_radial_facevert_count(searchedge->l, v)) {
348
} while ((searchedge = bmesh_disk_edge_next(searchedge, v)) != e);
352
/*****radial cycle functions, e.g. loops surrounding edges**** */
353
int bmesh_radial_validate(int radlen, BMLoop *l)
358
if (bmesh_radial_length(l) != radlen)
362
if (UNLIKELY(!l_iter)) {
367
if (l_iter->e != l->e)
369
if (l_iter->v != l->e->v1 && l_iter->v != l->e->v2)
372
if (UNLIKELY(i > BM_LOOP_RADIAL_MAX)) {
378
} while ((l_iter = l_iter->radial_next) != l);
384
* \brief BMESH RADIAL REMOVE LOOP
386
* Removes a loop from an radial cycle. If edge e is non-NULL
387
* it should contain the radial cycle, and it will also get
388
* updated (in the case that the edge's link into the radial
389
* cycle was the loop which is being removed from the cycle).
391
void bmesh_radial_loop_remove(BMLoop *l, BMEdge *e)
393
/* if e is non-NULL, l must be in the radial cycle of e */
394
if (UNLIKELY(e && e != l->e)) {
398
if (l->radial_next != l) {
400
e->l = l->radial_next;
402
l->radial_next->radial_prev = l->radial_prev;
403
l->radial_prev->radial_next = l->radial_next;
416
/* l is no longer in a radial cycle; empty the links
417
* to the cycle and the link back to an edge */
418
l->radial_next = l->radial_prev = NULL;
424
* \brief BME RADIAL FIND FIRST FACE VERT
426
* Finds the first loop of v around radial
429
BMLoop *bmesh_radial_faceloop_find_first(BMLoop *l, BMVert *v)
434
if (l_iter->v == v) {
437
} while ((l_iter = l_iter->radial_next) != l);
441
BMLoop *bmesh_radial_faceloop_find_next(BMLoop *l, BMVert *v)
444
l_iter = l->radial_next;
446
if (l_iter->v == v) {
449
} while ((l_iter = l_iter->radial_next) != l);
453
int bmesh_radial_length(BMLoop *l)
462
if (UNLIKELY(!l_iter)) {
463
/* radial cycle is broken (not a circulat loop) */
469
if (UNLIKELY(i >= BM_LOOP_RADIAL_MAX)) {
473
} while ((l_iter = l_iter->radial_next) != l);
478
void bmesh_radial_append(BMEdge *e, BMLoop *l)
482
l->radial_next = l->radial_prev = l;
485
l->radial_prev = e->l;
486
l->radial_next = e->l->radial_next;
488
e->l->radial_next->radial_prev = l;
489
e->l->radial_next = l;
494
if (UNLIKELY(l->e && l->e != e)) {
495
/* l is already in a radial cycle for a different edge */
502
int bmesh_radial_face_find(BMEdge *e, BMFace *f)
507
len = bmesh_radial_length(e->l);
508
for (i = 0, l_iter = e->l; i < len; i++, l_iter = l_iter->radial_next) {
516
* \brief RADIAL COUNT FACE VERT
518
* Returns the number of times a vertex appears
521
int bmesh_radial_facevert_count(BMLoop *l, BMVert *v)
527
if (l_iter->v == v) {
530
} while ((l_iter = l_iter->radial_next) != l);
535
/*****loop cycle functions, e.g. loops surrounding a face**** */
536
int bmesh_loop_validate(BMFace *f)
540
BMLoop *l_iter, *l_first;
542
l_first = BM_FACE_FIRST_LOOP(f);
544
if (l_first == NULL) {
548
/* Validate that the face loop cycle is the length specified by f->len */
549
for (i = 1, l_iter = l_first->next; i < len; i++, l_iter = l_iter->next) {
550
if ((l_iter->f != f) ||
556
if (l_iter != l_first) {
560
/* Validate the loop->prev links also form a cycle of length f->len */
561
for (i = 1, l_iter = l_first->prev; i < len; i++, l_iter = l_iter->prev) {
562
if (l_iter == l_first) {
566
if (l_iter != l_first) {