Welcome to mirror list, hosted at ThFree Co, Russian Federation.

bmesh_strands.h « intern « bmesh « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1f060fa2daeb8643a4af44305cf9d9a69bbd80e6 (plain)
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
/*
 * ***** 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): Lukas Toenne.
 *
 * ***** END GPL LICENSE BLOCK *****
 */

#ifndef __BMESH_STRANDS_H__
#define __BMESH_STRANDS_H__

/** \file blender/bmesh/intern/bmesh_strands.h
 *  \ingroup bmesh
 */

#include "BLI_utildefines.h"

#include "bmesh.h"
#include "bmesh_queries.h"
#include "bmesh_structure.h"

/* True if v is the root of a strand */
BLI_INLINE bool BM_strands_vert_is_root(BMVert *v)
{
	BMEdge *e_first = v->e;
	BMEdge *e_next;
	
	if (!e_first)
		return true; /* single vertex is both root and tip */
	e_next = bmesh_disk_edge_next(e_first, v);
	
	/* with a single edge, the vertex is either first or last of the curve;
	 * first vertex is defined as the root
	 */
	if (e_next == e_first) {
		if (e_first->v1 == v)
			return true;
	}
	return false;
}

/* True if v is the tip of a strand */
BLI_INLINE bool BM_strands_vert_is_tip(BMVert *v)
{
	BMEdge *e_first = v->e;
	BMEdge *e_next;
	
	if (!e_first)
		return true; /* single vertex is both root and tip */
	e_next = bmesh_disk_edge_next(e_first, v);
	
	/* with a single edge, the vertex is either first or last of the curve;
	 * last vertex is defined as the tip
	 */
	if (e_next == e_first) {
		if (e_first->v2 == v)
			return true;
	}
	return false;
}

/* Next vertex on a strand */
BLI_INLINE BMVert *BM_strands_vert_next(BMVert *v)
{
	BMEdge *e_first = v->e;
	BMEdge *e_next;
	
	/* one of the edges leads to the previous vertex */
	if (e_first) {
		if (e_first->v1 == v)
			return e_first->v2;
		
		e_next = bmesh_disk_edge_next(e_first, v);
		if (e_next->v1 == v)
			return e_next->v2;
	}
	return NULL;
}

/* Previous vertex on a strand */
BLI_INLINE BMVert *BM_strands_vert_prev(BMVert *v)
{
	BMEdge *e_first = v->e;
	BMEdge *e_next;
	
	/* one of the edges leads to the previous vertex */
	if (e_first) {
		if (e_first->v2 == v)
			return e_first->v1;
		
		e_next = bmesh_disk_edge_next(e_first, v);
		if (e_next->v2 == v)
			return e_next->v1;
	}
	return NULL;
}

int BM_strands_count(BMesh *bm);
int BM_strands_keys_count(BMVert *root);
int BM_strands_keys_count_max(BMesh *bm);

/* Create a new strand */
struct BMVert *BM_strands_create(struct BMesh *bm, int len, bool set_defaults);

/* ==== Iterators ==== */

typedef enum BMStrandsIterType {
	BM_STRANDS_OF_MESH,
	BM_VERTS_OF_STRAND,
} BMStrandsIterType;

#define BM_ITER_STRANDS(ele, iter, bm, itype) \
	for (BM_CHECK_TYPE_ELEM_ASSIGN(ele) = BM_strand_iter_new(iter, bm, itype, NULL); \
	     ele; \
	     BM_CHECK_TYPE_ELEM_ASSIGN(ele) = BM_iter_step(iter))

#define BM_ITER_STRANDS_INDEX(ele, iter, bm, itype, indexvar) \
	for (BM_CHECK_TYPE_ELEM_ASSIGN(ele) = BM_strand_iter_new(iter, bm, itype, NULL), indexvar = 0; \
	     ele; \
	     BM_CHECK_TYPE_ELEM_ASSIGN(ele) = BM_iter_step(iter), (indexvar)++)

#define BM_ITER_STRANDS_ELEM(ele, iter, data, itype) \
	for (BM_CHECK_TYPE_ELEM_ASSIGN(ele) = BM_strand_iter_new(iter, NULL, itype, data); \
	     ele; \
	     BM_CHECK_TYPE_ELEM_ASSIGN(ele) = BM_iter_step(iter))

#define BM_ITER_STRANDS_ELEM_INDEX(ele, iter, data, itype, indexvar) \
	for (BM_CHECK_TYPE_ELEM_ASSIGN(ele) = BM_strand_iter_new(iter, NULL, itype, data), indexvar = 0; \
	     ele; \
	     BM_CHECK_TYPE_ELEM_ASSIGN(ele) = BM_iter_step(iter), (indexvar)++)

typedef struct BMIter__vert_of_strand {
	BMVert *v_next;
	BMEdge *e_next;
} BMIter__vert_of_strand;

void  bmstranditer__strands_of_mesh_begin(struct BMIter__elem_of_mesh *iter);
void *bmstranditer__strands_of_mesh_step(struct BMIter__elem_of_mesh *iter);

void  bmstranditer__verts_of_strand_begin(struct BMIter__vert_of_strand *iter);
void *bmstranditer__verts_of_strand_step(struct BMIter__vert_of_strand *iter);

BLI_INLINE bool BM_strand_iter_init(BMIter *iter, BMesh *bm, const char itype, void *data)
{
	/* int argtype; */
	iter->itype = itype;

	/* inlining optimizes out this switch when called with the defined type */
	switch ((BMStrandsIterType)itype) {
		case BM_STRANDS_OF_MESH:
			BLI_assert(bm != NULL);
			BLI_assert(data == NULL);
			iter->begin = (BMIter__begin_cb)bmstranditer__strands_of_mesh_begin;
			iter->step  = (BMIter__step_cb)bmstranditer__strands_of_mesh_step;
			iter->data.elem_of_mesh.pooliter.pool = bm->vpool;
			break;
		case BM_VERTS_OF_STRAND: {
			BMVert *root;
			
			BLI_assert(data != NULL);
			BLI_assert(((BMElem *)data)->head.htype == BM_VERT);
			root = (BMVert *)data;
			BLI_assert(BM_strands_vert_is_root(root));
			iter->begin = (BMIter__begin_cb)bmstranditer__verts_of_strand_begin;
			iter->step  = (BMIter__step_cb)bmstranditer__verts_of_strand_step;
			((BMIter__vert_of_strand *)(&iter->data))->v_next = root;
			break;
		}
		default:
			/* fallback to regular bmesh iterator */
			return BM_iter_init(iter, bm, itype, data);
			break;
	}
	
	iter->begin(iter);

	return true;
}

/**
 * \brief Iterator New
 *
 * Takes a bmesh iterator structure and fills
 * it with the appropriate function pointers based
 * upon its type and then calls BMeshIter_step()
 * to return the first element of the iterator.
 *
 */
BLI_INLINE void *BM_strand_iter_new(BMIter *iter, BMesh *bm, const char itype, void *data)
{
	if (LIKELY(BM_strand_iter_init(iter, bm, itype, data))) {
		return BM_iter_step(iter);
	}
	else {
		return NULL;
	}
}

#define BM_strand_iter_new(iter, bm, itype, data) \
	(BM_ITER_CHECK_TYPE_DATA(data), BM_strand_iter_new(iter, bm, itype, data))

#endif /* __BMESH_STRANDS_H__ */