Blender  V2.93
bmesh_query_uv.c
Go to the documentation of this file.
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 
21 #include "MEM_guardedalloc.h"
22 
23 #include "BLI_alloca.h"
24 #include "BLI_linklist.h"
25 #include "BLI_math.h"
26 #include "BLI_utildefines_stack.h"
27 
28 #include "BKE_customdata.h"
29 
30 #include "DNA_meshdata_types.h"
31 
32 #include "bmesh.h"
33 #include "intern/bmesh_private.h"
34 
35 static void uv_aspect(const BMLoop *l,
36  const float aspect[2],
37  const int cd_loop_uv_offset,
38  float r_uv[2])
39 {
40  const float *uv = ((const MLoopUV *)BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset))->uv;
41  r_uv[0] = uv[0] * aspect[0];
42  r_uv[1] = uv[1] * aspect[1];
43 }
44 
49 #define UV_ASPECT(l, r_uv) uv_aspect(l, aspect, cd_loop_uv_offset, r_uv)
50 
60  const float aspect[2],
61  const int cd_loop_uv_offset,
62  float r_cent[2])
63 {
64  const BMLoop *l_iter;
65  const BMLoop *l_first;
66  float totw = 0.0f;
67  float w_prev;
68 
69  zero_v2(r_cent);
70 
71  l_iter = l_first = BM_FACE_FIRST_LOOP(f);
72 
73  float uv_prev[2], uv_curr[2];
74  UV_ASPECT(l_iter->prev, uv_prev);
75  UV_ASPECT(l_iter, uv_curr);
76  w_prev = len_v2v2(uv_prev, uv_curr);
77  do {
78  float uv_next[2];
79  UV_ASPECT(l_iter->next, uv_next);
80  const float w_curr = len_v2v2(uv_curr, uv_next);
81  const float w = (w_curr + w_prev);
82  madd_v2_v2fl(r_cent, uv_curr, w);
83  totw += w;
84  w_prev = w_curr;
85  copy_v2_v2(uv_curr, uv_next);
86  } while ((l_iter = l_iter->next) != l_first);
87 
88  if (totw != 0.0f) {
89  mul_v2_fl(r_cent, 1.0f / (float)totw);
90  }
91  /* Reverse aspect. */
92  r_cent[0] /= aspect[0];
93  r_cent[1] /= aspect[1];
94 }
95 
96 #undef UV_ASPECT
97 
98 void BM_face_uv_calc_center_median(const BMFace *f, const int cd_loop_uv_offset, float r_cent[2])
99 {
100  const BMLoop *l_iter;
101  const BMLoop *l_first;
102  zero_v2(r_cent);
103  l_iter = l_first = BM_FACE_FIRST_LOOP(f);
104  do {
105  const MLoopUV *luv = BM_ELEM_CD_GET_VOID_P(l_iter, cd_loop_uv_offset);
106  add_v2_v2(r_cent, luv->uv);
107  } while ((l_iter = l_iter->next) != l_first);
108 
109  mul_v2_fl(r_cent, 1.0f / (float)f->len);
110 }
111 
115 float BM_face_uv_calc_cross(const BMFace *f, const int cd_loop_uv_offset)
116 {
117  float(*uvs)[2] = BLI_array_alloca(uvs, f->len);
118  const BMLoop *l_iter;
119  const BMLoop *l_first;
120  int i = 0;
121  l_iter = l_first = BM_FACE_FIRST_LOOP(f);
122  do {
123  const MLoopUV *luv = BM_ELEM_CD_GET_VOID_P(l_iter, cd_loop_uv_offset);
124  copy_v2_v2(uvs[i++], luv->uv);
125  } while ((l_iter = l_iter->next) != l_first);
126  return cross_poly_v2(uvs, f->len);
127 }
128 
129 void BM_face_uv_minmax(const BMFace *f, float min[2], float max[2], const int cd_loop_uv_offset)
130 {
131  const BMLoop *l_iter;
132  const BMLoop *l_first;
133  l_iter = l_first = BM_FACE_FIRST_LOOP(f);
134  do {
135  const MLoopUV *luv = BM_ELEM_CD_GET_VOID_P(l_iter, cd_loop_uv_offset);
136  minmax_v2v2_v2(min, max, luv->uv);
137  } while ((l_iter = l_iter->next) != l_first);
138 }
139 
140 void BM_face_uv_transform(BMFace *f, const float matrix[2][2], const int cd_loop_uv_offset)
141 {
142  BMLoop *l_iter;
143  BMLoop *l_first;
144  l_iter = l_first = BM_FACE_FIRST_LOOP(f);
145  do {
146  MLoopUV *luv = BM_ELEM_CD_GET_VOID_P(l_iter, cd_loop_uv_offset);
147  mul_m2_v2(matrix, luv->uv);
148  } while ((l_iter = l_iter->next) != l_first);
149 }
150 
154 bool BM_loop_uv_share_edge_check(BMLoop *l_a, BMLoop *l_b, const int cd_loop_uv_offset)
155 {
156  BLI_assert(l_a->e == l_b->e);
157  MLoopUV *luv_a_curr = BM_ELEM_CD_GET_VOID_P(l_a, cd_loop_uv_offset);
158  MLoopUV *luv_a_next = BM_ELEM_CD_GET_VOID_P(l_a->next, cd_loop_uv_offset);
159  MLoopUV *luv_b_curr = BM_ELEM_CD_GET_VOID_P(l_b, cd_loop_uv_offset);
160  MLoopUV *luv_b_next = BM_ELEM_CD_GET_VOID_P(l_b->next, cd_loop_uv_offset);
161  if (l_a->v != l_b->v) {
162  SWAP(MLoopUV *, luv_b_curr, luv_b_next);
163  }
164  return (equals_v2v2(luv_a_curr->uv, luv_b_curr->uv) &&
165  equals_v2v2(luv_a_next->uv, luv_b_next->uv));
166 }
167 
171 bool BM_loop_uv_share_vert_check(BMLoop *l_a, BMLoop *l_b, const int cd_loop_uv_offset)
172 {
173  BLI_assert(l_a->v == l_b->v);
174  const MLoopUV *luv_a = BM_ELEM_CD_GET_VOID_P(l_a, cd_loop_uv_offset);
175  const MLoopUV *luv_b = BM_ELEM_CD_GET_VOID_P(l_b, cd_loop_uv_offset);
176  if (!equals_v2v2(luv_a->uv, luv_b->uv)) {
177  return false;
178  }
179  return true;
180 }
181 
185 bool BM_edge_uv_share_vert_check(BMEdge *e, BMLoop *l_a, BMLoop *l_b, const int cd_loop_uv_offset)
186 {
187  BLI_assert(l_a->v == l_b->v);
188  if (!BM_loop_uv_share_vert_check(l_a, l_b, cd_loop_uv_offset)) {
189  return false;
190  }
191 
192  /* No need for NULL checks, these will always succeed. */
193  const BMLoop *l_other_a = BM_loop_other_vert_loop_by_edge(l_a, e);
194  const BMLoop *l_other_b = BM_loop_other_vert_loop_by_edge(l_b, e);
195 
196  {
197  const MLoopUV *luv_other_a = BM_ELEM_CD_GET_VOID_P(l_other_a, cd_loop_uv_offset);
198  const MLoopUV *luv_other_b = BM_ELEM_CD_GET_VOID_P(l_other_b, cd_loop_uv_offset);
199  if (!equals_v2v2(luv_other_a->uv, luv_other_b->uv)) {
200  return false;
201  }
202  }
203 
204  return true;
205 }
206 
210 bool BM_face_uv_point_inside_test(const BMFace *f, const float co[2], const int cd_loop_uv_offset)
211 {
212  float(*projverts)[2] = BLI_array_alloca(projverts, f->len);
213 
214  BMLoop *l_iter;
215  int i;
216 
218 
219  for (i = 0, l_iter = BM_FACE_FIRST_LOOP(f); i < f->len; i++, l_iter = l_iter->next) {
220  copy_v2_v2(projverts[i], BM_ELEM_CD_GET_VOID_P(l_iter, cd_loop_uv_offset));
221  }
222 
223  return isect_point_poly_v2(co, projverts, f->len, false);
224 }
typedef float(TangentPoint)[2]
CustomData interface, see also DNA_customdata_types.h.
#define BLI_array_alloca(arr, realsize)
Definition: BLI_alloca.h:36
#define BLI_assert(a)
Definition: BLI_assert.h:58
bool isect_point_poly_v2(const float pt[2], const float verts[][2], const unsigned int nr, const bool use_holes)
float cross_poly_v2(const float verts[][2], unsigned int nr)
Definition: math_geom.c:171
void mul_m2_v2(const float M[2][2], float v[2])
Definition: math_matrix.c:788
MINLINE void madd_v2_v2fl(float r[2], const float a[2], float f)
MINLINE void mul_v2_fl(float r[2], float f)
MINLINE void copy_v2_v2(float r[2], const float a[2])
void minmax_v2v2_v2(float min[2], float max[2], const float vec[2])
Definition: math_vector.c:1043
MINLINE void add_v2_v2(float r[2], const float a[2])
MINLINE bool equals_v2v2(const float v1[2], const float v2[2]) ATTR_WARN_UNUSED_RESULT
MINLINE void zero_v2(float r[2])
MINLINE float len_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
#define SWAP(type, a, b)
Read Guarded memory(de)allocation.
#define BM_FACE_FIRST_LOOP(p)
Definition: bmesh_class.h:553
#define BM_ELEM_CD_GET_VOID_P(ele, offset)
Definition: bmesh_class.h:530
bool BM_face_is_normal_valid(const BMFace *f)
Definition: bmesh_query.c:2533
BMLoop * BM_loop_other_vert_loop_by_edge(BMLoop *l, BMEdge *e)
Definition: bmesh_query.c:176
ATTR_WARN_UNUSED_RESULT const BMLoop * l
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
ATTR_WARN_UNUSED_RESULT const BMLoop * l_b
bool BM_edge_uv_share_vert_check(BMEdge *e, BMLoop *l_a, BMLoop *l_b, const int cd_loop_uv_offset)
static void uv_aspect(const BMLoop *l, const float aspect[2], const int cd_loop_uv_offset, float r_uv[2])
void BM_face_uv_transform(BMFace *f, const float matrix[2][2], const int cd_loop_uv_offset)
bool BM_loop_uv_share_vert_check(BMLoop *l_a, BMLoop *l_b, const int cd_loop_uv_offset)
bool BM_loop_uv_share_edge_check(BMLoop *l_a, BMLoop *l_b, const int cd_loop_uv_offset)
void BM_face_uv_minmax(const BMFace *f, float min[2], float max[2], const int cd_loop_uv_offset)
void BM_face_uv_calc_center_median(const BMFace *f, const int cd_loop_uv_offset, float r_cent[2])
bool BM_face_uv_point_inside_test(const BMFace *f, const float co[2], const int cd_loop_uv_offset)
float BM_face_uv_calc_cross(const BMFace *f, const int cd_loop_uv_offset)
void BM_face_uv_calc_center_median_weighted(const BMFace *f, const float aspect[2], const int cd_loop_uv_offset, float r_cent[2])
#define UV_ASPECT(l, r_uv)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition: btQuadWord.h:119
#define min(a, b)
Definition: sort.c:51
int len
Definition: bmesh_class.h:279
struct BMVert * v
Definition: bmesh_class.h:165
struct BMEdge * e
Definition: bmesh_class.h:176
struct BMLoop * prev
Definition: bmesh_class.h:245
struct BMLoop * next
Definition: bmesh_class.h:245
float max