Blender V4.5
bmesh_query_uv.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_array.hh"
10#include "BLI_math_geom.h"
11#include "BLI_math_vector.h"
13#include "BLI_string_ref.hh"
14
15#include "BKE_attribute.h"
16#include "BKE_customdata.hh"
17
18#include "bmesh.hh"
19
21{
22 using namespace blender;
23 using namespace blender::bke;
24 const int layer_index = CustomData_get_layer_index_n(&bm->ldata, CD_PROP_FLOAT2, layer);
25 if (layer_index == -1) {
26 return {-1, -1, -1, -1};
27 }
28
29 const StringRef name = bm->ldata.layers[layer_index].name;
30 char buffer[MAX_CUSTOMDATA_LAYER_NAME];
31
32 BMUVOffsets offsets;
33 offsets.uv = bm->ldata.layers[layer_index].offset;
35 &bm->ldata, CD_PROP_BOOL, BKE_uv_map_vert_select_name_get(name, buffer));
37 &bm->ldata, CD_PROP_BOOL, BKE_uv_map_edge_select_name_get(name, buffer));
39 &bm->ldata, CD_PROP_BOOL, BKE_uv_map_pin_name_get(name, buffer));
40
41 return offsets;
42}
43
45{
46 const int layer = CustomData_get_active_layer(&bm->ldata, CD_PROP_FLOAT2);
47 if (layer == -1) {
48 return {-1, -1, -1, -1};
49 }
50 return BM_uv_map_offsets_from_layer(bm, layer);
51}
52
53static void uv_aspect(const BMLoop *l,
54 const float aspect[2],
55 const int cd_loop_uv_offset,
56 float r_uv[2])
57{
58 const float *uv = BM_ELEM_CD_GET_FLOAT_P(l, cd_loop_uv_offset);
59 r_uv[0] = uv[0] * aspect[0];
60 r_uv[1] = uv[1] * aspect[1];
61}
62
67#define UV_ASPECT(l, r_uv) uv_aspect(l, aspect, cd_loop_uv_offset, r_uv)
68
70 const float aspect[2],
71 const int cd_loop_uv_offset,
72 float r_cent[2])
73{
74 const BMLoop *l_iter;
75 const BMLoop *l_first;
76 float totw = 0.0f;
77 float w_prev;
78
79 zero_v2(r_cent);
80
81 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
82
83 float uv_prev[2], uv_curr[2];
84 UV_ASPECT(l_iter->prev, uv_prev);
85 UV_ASPECT(l_iter, uv_curr);
86 w_prev = len_v2v2(uv_prev, uv_curr);
87 do {
88 float uv_next[2];
89 UV_ASPECT(l_iter->next, uv_next);
90 const float w_curr = len_v2v2(uv_curr, uv_next);
91 const float w = (w_curr + w_prev);
92 madd_v2_v2fl(r_cent, uv_curr, w);
93 totw += w;
94 w_prev = w_curr;
95 copy_v2_v2(uv_curr, uv_next);
96 } while ((l_iter = l_iter->next) != l_first);
97
98 if (totw != 0.0f) {
99 mul_v2_fl(r_cent, 1.0f / totw);
100 }
101 /* Reverse aspect. */
102 r_cent[0] /= aspect[0];
103 r_cent[1] /= aspect[1];
104}
105
106#undef UV_ASPECT
107
108void BM_face_uv_calc_center_median(const BMFace *f, const int cd_loop_uv_offset, float r_cent[2])
109{
110 const BMLoop *l_iter;
111 const BMLoop *l_first;
112 zero_v2(r_cent);
113 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
114 do {
115 const float *luv = BM_ELEM_CD_GET_FLOAT_P(l_iter, cd_loop_uv_offset);
116 add_v2_v2(r_cent, luv);
117 } while ((l_iter = l_iter->next) != l_first);
118
119 mul_v2_fl(r_cent, 1.0f / float(f->len));
120}
121
122float BM_face_uv_calc_cross(const BMFace *f, const int cd_loop_uv_offset)
123{
125 const BMLoop *l_iter;
126 const BMLoop *l_first;
127 int i = 0;
128 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
129 do {
130 uvs[i++] = BM_ELEM_CD_GET_FLOAT2_P(l_iter, cd_loop_uv_offset);
131 } while ((l_iter = l_iter->next) != l_first);
132 return cross_poly_v2(reinterpret_cast<const float(*)[2]>(uvs.data()), f->len);
133}
134
135void BM_face_uv_minmax(const BMFace *f, float min[2], float max[2], const int cd_loop_uv_offset)
136{
137 const BMLoop *l_iter;
138 const BMLoop *l_first;
139 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
140 do {
141 const float *luv = BM_ELEM_CD_GET_FLOAT_P(l_iter, cd_loop_uv_offset);
142 minmax_v2v2_v2(min, max, luv);
143 } while ((l_iter = l_iter->next) != l_first);
144}
145
146bool BM_loop_uv_share_edge_check(const BMLoop *l_a, const BMLoop *l_b, const int cd_loop_uv_offset)
147{
148 BLI_assert(l_a->e == l_b->e);
149 const float *luv_a_curr = BM_ELEM_CD_GET_FLOAT_P(l_a, cd_loop_uv_offset);
150 const float *luv_a_next = BM_ELEM_CD_GET_FLOAT_P(l_a->next, cd_loop_uv_offset);
151 const float *luv_b_curr = BM_ELEM_CD_GET_FLOAT_P(l_b, cd_loop_uv_offset);
152 const float *luv_b_next = BM_ELEM_CD_GET_FLOAT_P(l_b->next, cd_loop_uv_offset);
153 if (l_a->v != l_b->v) {
154 std::swap(luv_b_curr, luv_b_next);
155 }
156 return (equals_v2v2(luv_a_curr, luv_b_curr) && equals_v2v2(luv_a_next, luv_b_next));
157}
158
159bool BM_loop_uv_share_vert_check(const BMLoop *l_a, const BMLoop *l_b, const int cd_loop_uv_offset)
160{
161 BLI_assert(l_a->v == l_b->v);
162 const float *luv_a = BM_ELEM_CD_GET_FLOAT_P(l_a, cd_loop_uv_offset);
163 const float *luv_b = BM_ELEM_CD_GET_FLOAT_P(l_b, cd_loop_uv_offset);
164 if (!equals_v2v2(luv_a, luv_b)) {
165 return false;
166 }
167 return true;
168}
169
171 const BMLoop *l_a,
172 const BMLoop *l_b,
173 const int cd_loop_uv_offset)
174{
175 BLI_assert(l_a->v == l_b->v);
176 if (!BM_loop_uv_share_vert_check(l_a, l_b, cd_loop_uv_offset)) {
177 return false;
178 }
179
180 /* No need for null checks, these will always succeed. */
181 const BMLoop *l_other_a = BM_loop_other_vert_loop_by_edge(const_cast<BMLoop *>(l_a),
182 const_cast<BMEdge *>(e));
183 const BMLoop *l_other_b = BM_loop_other_vert_loop_by_edge(const_cast<BMLoop *>(l_b),
184 const_cast<BMEdge *>(e));
185
186 {
187 const float *luv_other_a = BM_ELEM_CD_GET_FLOAT_P(l_other_a, cd_loop_uv_offset);
188 const float *luv_other_b = BM_ELEM_CD_GET_FLOAT_P(l_other_b, cd_loop_uv_offset);
189 if (!equals_v2v2(luv_other_a, luv_other_b)) {
190 return false;
191 }
192 }
193
194 return true;
195}
196
197bool BM_face_uv_point_inside_test(const BMFace *f, const float co[2], const int cd_loop_uv_offset)
198{
200
201 BMLoop *l_iter;
202 int i;
203
205
206 for (i = 0, l_iter = BM_FACE_FIRST_LOOP(f); i < f->len; i++, l_iter = l_iter->next) {
207 projverts[i] = BM_ELEM_CD_GET_FLOAT2_P(l_iter, cd_loop_uv_offset);
208 }
209
210 return isect_point_poly_v2(co, reinterpret_cast<const float(*)[2]>(projverts.data()), f->len);
211}
Generic geometry attributes built on CustomData.
blender::StringRef BKE_uv_map_pin_name_get(blender::StringRef uv_map_name, char *buffer)
blender::StringRef BKE_uv_map_edge_select_name_get(blender::StringRef uv_map_name, char *buffer)
blender::StringRef BKE_uv_map_vert_select_name_get(blender::StringRef uv_map_name, char *buffer)
CustomData interface, see also DNA_customdata_types.h.
int CustomData_get_layer_index_n(const CustomData *data, eCustomDataType type, int n)
int CustomData_get_offset_named(const CustomData *data, eCustomDataType type, blender::StringRef name)
int CustomData_get_active_layer(const CustomData *data, eCustomDataType type)
#define BLI_assert(a)
Definition BLI_assert.h:46
bool isect_point_poly_v2(const float pt[2], const float verts[][2], unsigned int nr)
float cross_poly_v2(const float verts[][2], unsigned int nr)
Definition math_geom.cc:149
MINLINE void madd_v2_v2fl(float r[2], const float a[2], float f)
MINLINE float len_v2v2(const float v1[2], const float v2[2]) ATTR_WARN_UNUSED_RESULT
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])
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])
@ CD_PROP_FLOAT2
#define BM_ELEM_CD_GET_FLOAT2_P(ele, offset)
#define BM_FACE_FIRST_LOOP(p)
#define BM_ELEM_CD_GET_FLOAT_P(ele, offset)
BMesh * bm
bool BM_face_is_normal_valid(const BMFace *f)
BMLoop * BM_loop_other_vert_loop_by_edge(BMLoop *l, BMEdge *e)
ATTR_WARN_UNUSED_RESULT const BMLoop * l
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
ATTR_WARN_UNUSED_RESULT const BMLoop * l_b
BMUVOffsets BM_uv_map_offsets_get(const BMesh *bm)
static void uv_aspect(const BMLoop *l, const float aspect[2], const int cd_loop_uv_offset, float r_uv[2])
bool BM_loop_uv_share_edge_check(const BMLoop *l_a, const BMLoop *l_b, const int cd_loop_uv_offset)
bool BM_edge_uv_share_vert_check(const BMEdge *e, const BMLoop *l_a, const BMLoop *l_b, const int cd_loop_uv_offset)
bool BM_loop_uv_share_vert_check(const BMLoop *l_a, const 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])
BMUVOffsets BM_uv_map_offsets_from_layer(const BMesh *bm, const int layer)
#define UV_ASPECT(l, r_uv)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
const T * data() const
Definition BLI_array.hh:301
#define MAX_CUSTOMDATA_LAYER_NAME
#define min(a, b)
Definition sort.cc:36
struct BMVert * v
struct BMEdge * e
struct BMLoop * prev
struct BMLoop * next
i
Definition text_draw.cc:230
max
Definition text_draw.cc:251