Blender  V3.3
bmesh_query_uv.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #include "MEM_guardedalloc.h"
8 
9 #include "BLI_alloca.h"
10 #include "BLI_linklist.h"
11 #include "BLI_math.h"
12 #include "BLI_utildefines_stack.h"
13 
14 #include "BKE_customdata.h"
15 
16 #include "DNA_meshdata_types.h"
17 
18 #include "bmesh.h"
19 #include "intern/bmesh_private.h"
20 
21 static void uv_aspect(const BMLoop *l,
22  const float aspect[2],
23  const int cd_loop_uv_offset,
24  float r_uv[2])
25 {
26  const float *uv = ((const MLoopUV *)BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset))->uv;
27  r_uv[0] = uv[0] * aspect[0];
28  r_uv[1] = uv[1] * aspect[1];
29 }
30 
35 #define UV_ASPECT(l, r_uv) uv_aspect(l, aspect, cd_loop_uv_offset, r_uv)
36 
38  const float aspect[2],
39  const int cd_loop_uv_offset,
40  float r_cent[2])
41 {
42  const BMLoop *l_iter;
43  const BMLoop *l_first;
44  float totw = 0.0f;
45  float w_prev;
46 
47  zero_v2(r_cent);
48 
49  l_iter = l_first = BM_FACE_FIRST_LOOP(f);
50 
51  float uv_prev[2], uv_curr[2];
52  UV_ASPECT(l_iter->prev, uv_prev);
53  UV_ASPECT(l_iter, uv_curr);
54  w_prev = len_v2v2(uv_prev, uv_curr);
55  do {
56  float uv_next[2];
57  UV_ASPECT(l_iter->next, uv_next);
58  const float w_curr = len_v2v2(uv_curr, uv_next);
59  const float w = (w_curr + w_prev);
60  madd_v2_v2fl(r_cent, uv_curr, w);
61  totw += w;
62  w_prev = w_curr;
63  copy_v2_v2(uv_curr, uv_next);
64  } while ((l_iter = l_iter->next) != l_first);
65 
66  if (totw != 0.0f) {
67  mul_v2_fl(r_cent, 1.0f / (float)totw);
68  }
69  /* Reverse aspect. */
70  r_cent[0] /= aspect[0];
71  r_cent[1] /= aspect[1];
72 }
73 
74 #undef UV_ASPECT
75 
76 void BM_face_uv_calc_center_median(const BMFace *f, const int cd_loop_uv_offset, float r_cent[2])
77 {
78  const BMLoop *l_iter;
79  const BMLoop *l_first;
80  zero_v2(r_cent);
81  l_iter = l_first = BM_FACE_FIRST_LOOP(f);
82  do {
83  const MLoopUV *luv = BM_ELEM_CD_GET_VOID_P(l_iter, cd_loop_uv_offset);
84  add_v2_v2(r_cent, luv->uv);
85  } while ((l_iter = l_iter->next) != l_first);
86 
87  mul_v2_fl(r_cent, 1.0f / (float)f->len);
88 }
89 
90 float BM_face_uv_calc_cross(const BMFace *f, const int cd_loop_uv_offset)
91 {
92  float(*uvs)[2] = BLI_array_alloca(uvs, f->len);
93  const BMLoop *l_iter;
94  const BMLoop *l_first;
95  int i = 0;
96  l_iter = l_first = BM_FACE_FIRST_LOOP(f);
97  do {
98  const MLoopUV *luv = BM_ELEM_CD_GET_VOID_P(l_iter, cd_loop_uv_offset);
99  copy_v2_v2(uvs[i++], luv->uv);
100  } while ((l_iter = l_iter->next) != l_first);
101  return cross_poly_v2(uvs, f->len);
102 }
103 
104 void BM_face_uv_minmax(const BMFace *f, float min[2], float max[2], const int cd_loop_uv_offset)
105 {
106  const BMLoop *l_iter;
107  const BMLoop *l_first;
108  l_iter = l_first = BM_FACE_FIRST_LOOP(f);
109  do {
110  const MLoopUV *luv = BM_ELEM_CD_GET_VOID_P(l_iter, cd_loop_uv_offset);
111  minmax_v2v2_v2(min, max, luv->uv);
112  } while ((l_iter = l_iter->next) != l_first);
113 }
114 
115 void BM_face_uv_transform(BMFace *f, const float matrix[2][2], const int cd_loop_uv_offset)
116 {
117  BMLoop *l_iter;
118  BMLoop *l_first;
119  l_iter = l_first = BM_FACE_FIRST_LOOP(f);
120  do {
121  MLoopUV *luv = BM_ELEM_CD_GET_VOID_P(l_iter, cd_loop_uv_offset);
122  mul_m2_v2(matrix, luv->uv);
123  } while ((l_iter = l_iter->next) != l_first);
124 }
125 
126 bool BM_loop_uv_share_edge_check(BMLoop *l_a, BMLoop *l_b, const int cd_loop_uv_offset)
127 {
128  BLI_assert(l_a->e == l_b->e);
129  MLoopUV *luv_a_curr = BM_ELEM_CD_GET_VOID_P(l_a, cd_loop_uv_offset);
130  MLoopUV *luv_a_next = BM_ELEM_CD_GET_VOID_P(l_a->next, cd_loop_uv_offset);
131  MLoopUV *luv_b_curr = BM_ELEM_CD_GET_VOID_P(l_b, cd_loop_uv_offset);
132  MLoopUV *luv_b_next = BM_ELEM_CD_GET_VOID_P(l_b->next, cd_loop_uv_offset);
133  if (l_a->v != l_b->v) {
134  SWAP(MLoopUV *, luv_b_curr, luv_b_next);
135  }
136  return (equals_v2v2(luv_a_curr->uv, luv_b_curr->uv) &&
137  equals_v2v2(luv_a_next->uv, luv_b_next->uv));
138 }
139 
140 bool BM_loop_uv_share_vert_check(BMLoop *l_a, BMLoop *l_b, const int cd_loop_uv_offset)
141 {
142  BLI_assert(l_a->v == l_b->v);
143  const MLoopUV *luv_a = BM_ELEM_CD_GET_VOID_P(l_a, cd_loop_uv_offset);
144  const MLoopUV *luv_b = BM_ELEM_CD_GET_VOID_P(l_b, cd_loop_uv_offset);
145  if (!equals_v2v2(luv_a->uv, luv_b->uv)) {
146  return false;
147  }
148  return true;
149 }
150 
151 bool BM_edge_uv_share_vert_check(BMEdge *e, BMLoop *l_a, BMLoop *l_b, const int cd_loop_uv_offset)
152 {
153  BLI_assert(l_a->v == l_b->v);
154  if (!BM_loop_uv_share_vert_check(l_a, l_b, cd_loop_uv_offset)) {
155  return false;
156  }
157 
158  /* No need for NULL checks, these will always succeed. */
159  const BMLoop *l_other_a = BM_loop_other_vert_loop_by_edge(l_a, e);
160  const BMLoop *l_other_b = BM_loop_other_vert_loop_by_edge(l_b, e);
161 
162  {
163  const MLoopUV *luv_other_a = BM_ELEM_CD_GET_VOID_P(l_other_a, cd_loop_uv_offset);
164  const MLoopUV *luv_other_b = BM_ELEM_CD_GET_VOID_P(l_other_b, cd_loop_uv_offset);
165  if (!equals_v2v2(luv_other_a->uv, luv_other_b->uv)) {
166  return false;
167  }
168  }
169 
170  return true;
171 }
172 
173 bool BM_face_uv_point_inside_test(const BMFace *f, const float co[2], const int cd_loop_uv_offset)
174 {
175  float(*projverts)[2] = BLI_array_alloca(projverts, f->len);
176 
177  BMLoop *l_iter;
178  int i;
179 
181 
182  for (i = 0, l_iter = BM_FACE_FIRST_LOOP(f); i < f->len; i++, l_iter = l_iter->next) {
183  copy_v2_v2(projverts[i], BM_ELEM_CD_GET_VOID_P(l_iter, cd_loop_uv_offset));
184  }
185 
186  return isect_point_poly_v2(co, projverts, f->len, false);
187 }
typedef float(TangentPoint)[2]
CustomData interface, see also DNA_customdata_types.h.
#define BLI_array_alloca(arr, realsize)
Definition: BLI_alloca.h:22
#define BLI_assert(a)
Definition: BLI_assert.h:46
float cross_poly_v2(const float verts[][2], unsigned int nr)
Definition: math_geom.c:141
bool isect_point_poly_v2(const float pt[2], const float verts[][2], unsigned int nr, bool use_holes)
void mul_m2_v2(const float M[2][2], float v[2])
Definition: math_matrix.c:785
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:890
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:622
#define BM_ELEM_CD_GET_VOID_P(ele, offset)
Definition: bmesh_class.h:541
bool BM_face_is_normal_valid(const BMFace *f)
Definition: bmesh_query.c:2033
BMLoop * BM_loop_other_vert_loop_by_edge(BMLoop *l, BMEdge *e)
Definition: bmesh_query.c:86
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:35
int len
Definition: bmesh_class.h:267
struct BMVert * v
Definition: bmesh_class.h:153
struct BMEdge * e
Definition: bmesh_class.h:164
struct BMLoop * prev
Definition: bmesh_class.h:233
struct BMLoop * next
Definition: bmesh_class.h:233
float max