1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software Foundation,
14 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15 */
16
17 /** \file
18 * \ingroup bmesh
19 *
20 * BM Inline functions.
21 */
22
23 #pragma once
24
25 /* stuff for dealing with header flags */
26 #define BM_elem_flag_test(ele, hflag) _bm_elem_flag_test(&(ele)->head, hflag)
27 #define BM_elem_flag_test_bool(ele, hflag) _bm_elem_flag_test_bool(&(ele)->head, hflag)
28 #define BM_elem_flag_enable(ele, hflag) _bm_elem_flag_enable(&(ele)->head, hflag)
29 #define BM_elem_flag_disable(ele, hflag) _bm_elem_flag_disable(&(ele)->head, hflag)
30 #define BM_elem_flag_set(ele, hflag, val) _bm_elem_flag_set(&(ele)->head, hflag, val)
31 #define BM_elem_flag_toggle(ele, hflag) _bm_elem_flag_toggle(&(ele)->head, hflag)
32 #define BM_elem_flag_merge(ele_a, ele_b) _bm_elem_flag_merge(&(ele_a)->head, &(ele_b)->head)
33 #define BM_elem_flag_merge_ex(ele_a, ele_b, hflag_and) \
34 _bm_elem_flag_merge_ex(&(ele_a)->head, &(ele_b)->head, hflag_and)
35 #define BM_elem_flag_merge_into(ele, ele_a, ele_b) \
36 _bm_elem_flag_merge_into(&(ele)->head, &(ele_a)->head, &(ele_b)->head)
37
38 ATTR_WARN_UNUSED_RESULT
_bm_elem_flag_test(const BMHeader * head,const char hflag)39 BLI_INLINE char _bm_elem_flag_test(const BMHeader *head, const char hflag)
40 {
41 return head->hflag & hflag;
42 }
43
44 ATTR_WARN_UNUSED_RESULT
_bm_elem_flag_test_bool(const BMHeader * head,const char hflag)45 BLI_INLINE bool _bm_elem_flag_test_bool(const BMHeader *head, const char hflag)
46 {
47 return (head->hflag & hflag) != 0;
48 }
49
_bm_elem_flag_enable(BMHeader * head,const char hflag)50 BLI_INLINE void _bm_elem_flag_enable(BMHeader *head, const char hflag)
51 {
52 head->hflag |= hflag;
53 }
54
_bm_elem_flag_disable(BMHeader * head,const char hflag)55 BLI_INLINE void _bm_elem_flag_disable(BMHeader *head, const char hflag)
56 {
57 head->hflag &= (char)~hflag;
58 }
59
_bm_elem_flag_set(BMHeader * head,const char hflag,const int val)60 BLI_INLINE void _bm_elem_flag_set(BMHeader *head, const char hflag, const int val)
61 {
62 if (val) {
63 _bm_elem_flag_enable(head, hflag);
64 }
65 else {
66 _bm_elem_flag_disable(head, hflag);
67 }
68 }
69
_bm_elem_flag_toggle(BMHeader * head,const char hflag)70 BLI_INLINE void _bm_elem_flag_toggle(BMHeader *head, const char hflag)
71 {
72 head->hflag ^= hflag;
73 }
74
_bm_elem_flag_merge(BMHeader * head_a,BMHeader * head_b)75 BLI_INLINE void _bm_elem_flag_merge(BMHeader *head_a, BMHeader *head_b)
76 {
77 head_a->hflag = head_b->hflag = head_a->hflag | head_b->hflag;
78 }
79
_bm_elem_flag_merge_ex(BMHeader * head_a,BMHeader * head_b,const char hflag_and)80 BLI_INLINE void _bm_elem_flag_merge_ex(BMHeader *head_a, BMHeader *head_b, const char hflag_and)
81 {
82 if (((head_a->hflag & head_b->hflag) & hflag_and) == 0) {
83 head_a->hflag &= ~hflag_and;
84 head_b->hflag &= ~hflag_and;
85 }
86 _bm_elem_flag_merge(head_a, head_b);
87 }
88
_bm_elem_flag_merge_into(BMHeader * head,const BMHeader * head_a,const BMHeader * head_b)89 BLI_INLINE void _bm_elem_flag_merge_into(BMHeader *head,
90 const BMHeader *head_a,
91 const BMHeader *head_b)
92 {
93 head->hflag = head_a->hflag | head_b->hflag;
94 }
95
96 /**
97 * notes on #BM_elem_index_set(...) usage,
98 * Set index is sometimes abused as temp storage, other times we cant be
99 * sure if the index values are valid because certain operations have modified
100 * the mesh structure.
101 *
102 * To set the elements to valid indices 'BM_mesh_elem_index_ensure' should be used
103 * rather than adding inline loops, however there are cases where we still
104 * set the index directly
105 *
106 * In an attempt to manage this,
107 * here are 5 tags I'm adding to uses of #BM_elem_index_set
108 *
109 * - 'set_inline' -- since the data is already being looped over set to a
110 * valid value inline.
111 *
112 * - 'set_dirty!' -- intentionally sets the index to an invalid value,
113 * flagging 'bm->elem_index_dirty' so we don't use it.
114 *
115 * - 'set_ok' -- this is valid use since the part of the code is low level.
116 *
117 * - 'set_ok_invalid' -- set to -1 on purpose since this should not be
118 * used without a full array re-index, do this on
119 * adding new vert/edge/faces since they may be added at
120 * the end of the array.
121 *
122 * - campbell */
123
124 #define BM_elem_index_get(ele) _bm_elem_index_get(&(ele)->head)
125 #define BM_elem_index_set(ele, index) _bm_elem_index_set(&(ele)->head, index)
126
_bm_elem_index_set(BMHeader * head,const int index)127 BLI_INLINE void _bm_elem_index_set(BMHeader *head, const int index)
128 {
129 head->index = index;
130 }
131
132 ATTR_WARN_UNUSED_RESULT
_bm_elem_index_get(const BMHeader * head)133 BLI_INLINE int _bm_elem_index_get(const BMHeader *head)
134 {
135 return head->index;
136 }
137