41 static const int directions[8][2];
42 static const int distances[8];
65 const float uv_offset[2],
73 corner_edges_(corner_edges),
79 pixel_data_.resize(w_ * h_, 0xFFFFFFFF);
95 pixel_data_[
y * w_ +
x] = value;
104 return pixel_data_[
y * w_ +
x];
108 float *v1,
float *
v2,
float *v3,
uint32_t value,
char *
mask,
bool writemask,
bool fill)
112 value_to_store_ = value;
114 write_mask_ = writemask;
127 void *map,
int x,
int y, [[maybe_unused]]
float u, [[maybe_unused]]
float v)
132 if (m->write_mask_) {
134 m->mask_[
y * m->w_ +
x] = 1;
141 if (m->mask_[
y * m->w_ +
x] != 0) {
156#define PackDijkstraPixel(dist, dir) (0x80000000 + ((dist) << 4) + (dir))
157#define DijkstraPixelGetDistance(dp) (((dp) ^ 0x80000000) >> 4)
158#define DijkstraPixelGetDirection(dp) ((dp) & 0xF)
159#define IsDijkstraPixel(dp) ((dp) & 0x80000000)
160#define DijkstraPixelIsUnset(dp) ((dp) == 0xFFFFFFFF)
168 class DijkstraActivePixel {
170 DijkstraActivePixel(
int dist,
int _x,
int _y) :
distance(dist),
x(_x),
y(_y) {}
174 auto cmp_dijkstrapixel_fun = [](DijkstraActivePixel
const &a1, DijkstraActivePixel
const &a2) {
175 return a1.distance > a2.distance;
179 for (
int y = 0;
y < h_;
y++) {
180 for (
int x = 0;
x < w_;
x++) {
182 for (
int i = 0; i < 8; i++) {
183 int xx =
x - directions[i][0];
184 int yy =
y - directions[i][1];
188 active_pixels.
append(DijkstraActivePixel(distances[i],
x,
y));
198 std::make_heap(active_pixels.
begin(), active_pixels.
end(), cmp_dijkstrapixel_fun);
201 while (active_pixels.
size()) {
202 std::pop_heap(active_pixels.
begin(), active_pixels.
end(), cmp_dijkstrapixel_fun);
203 DijkstraActivePixel p = active_pixels.
pop_last();
205 int dist = p.distance;
207 if (dist < 2 * (margin + 1)) {
208 for (
int i = 0; i < 8; i++) {
209 int x = p.x + directions[i][0];
210 int y = p.y + directions[i][1];
216 active_pixels.
append(DijkstraActivePixel(dist + distances[i],
x,
y));
217 std::push_heap(active_pixels.
begin(), active_pixels.
end(), cmp_dijkstrapixel_fun);
234 size_t pixel_index = 0;
235 for (
int y = 0;
y < h_;
y++) {
236 for (
int x = 0;
x < w_;
x++) {
237 uint32_t dp = pixel_data_[pixel_index];
247 xx -= directions[direction][0];
248 yy -= directions[direction][1];
250 dist -= distances[direction];
262 bool found_pixel_in_polygon =
false;
263 if (lookup_pixel_polygon_neighborhood(
x,
y, &face, &destX, &destY, &other_poly)) {
265 for (
int i = 0; i < maxPolygonSteps; i++) {
267 int nx = int(
round(destX));
268 int ny = int(
round(destY));
270 if (other_poly == polygon_from_map) {
271 found_pixel_in_polygon =
true;
277 if (!lookup_pixel(nx, ny, other_poly, &destX, &destY, &other_poly, &dist_to_edge)) {
278 found_pixel_in_polygon =
false;
283 if (found_pixel_in_polygon) {
293 mask[pixel_index] = 1;
301 mask[pixel_index] = 1;
312 ret.x = (((uv_map[0] - uv_offset_[0]) * w_) - (0.5f + 0.001f));
313 ret.y = (((uv_map[1] - uv_offset_[1]) * h_) - (0.5f + 0.001f));
321 loop_adjacency_map_.
resize(corner_edges_.
size(), -1);
324 tmpmap.
resize(totedge_, -1);
327 int edge = corner_edges_[i];
328 if (tmpmap[edge] == -1) {
329 loop_adjacency_map_[i] = -1;
334 loop_adjacency_map_[i] = tmpmap[edge];
335 loop_adjacency_map_[tmpmap[edge]] = i;
346 bool lookup_pixel_polygon_neighborhood(
347 float x,
float y,
uint32_t *r_start_poly,
float *r_destx,
float *r_desty,
int *r_other_poly)
350 if (lookup_pixel(
x,
y, *r_start_poly, r_destx, r_desty, r_other_poly, &found_dist)) {
354 int loopstart = faces_[*r_start_poly].start();
355 int totloop = faces_[*r_start_poly].size();
360 float mindist = -1.0f;
365 for (
int i = 0; i < totloop; i++) {
366 int otherloop = loop_adjacency_map_[i + loopstart];
372 uint32_t face = loop_to_face_map_[otherloop];
374 if (lookup_pixel(
x,
y, face, &destx, &desty, &foundpoly, &found_dist)) {
375 if (mindist < 0.0f || found_dist < mindist) {
376 mindist = found_dist;
377 *r_other_poly = foundpoly;
380 *r_start_poly = face;
385 return mindist >= 0.0f;
394 bool lookup_pixel(
float x,
400 float *r_dist_to_edge)
404 *r_destx = *r_desty = 0;
407 float found_dist = -1;
412 for (
size_t i = 0; i < faces_[src_poly].size(); i++) {
413 int l1 = faces_[src_poly].start() + i;
415 if (l2 >= faces_[src_poly].start() + faces_[src_poly].
size()) {
416 l2 = faces_[src_poly].start();
419 float2 edgepoint1 = uv_to_xy(uv_map_[l1]);
420 float2 edgepoint2 = uv_to_xy(uv_map_[l2]);
423 float2 ab = edgepoint2 - edgepoint1;
424 float2 ap = point - edgepoint1;
431 float t = dotv / ablensq;
433 if (t >= 0.0 && t <= 1.0) {
436 float2 reflect_point = edgepoint1 + (t * ab);
438 float2 reflect_vec = reflect_point - point;
440 float reflectLen =
sqrt(reflect_vec[0] * reflect_vec[0] + reflect_vec[1] * reflect_vec[1]);
441 float cross = ab[0] * reflect_vec[1] - ab[1] * reflect_vec[0];
445 bool valid = (
cross > 0.0);
447 if (valid && (found_dist < 0 || reflectLen < found_dist)) {
449 found_dist = reflectLen;
451 found_edge = i + faces_[src_poly].start();
456 if (found_edge < 0) {
460 *r_dist_to_edge = found_dist;
463 int other_edge = loop_adjacency_map_[found_edge];
465 if (other_edge < 0) {
469 int dst_poly = loop_to_face_map_[other_edge];
472 *r_other_poly = dst_poly;
475 int other_edge2 = other_edge + 1;
476 if (other_edge2 >= faces_[dst_poly].start() + faces_[dst_poly].
size()) {
477 other_edge2 = faces_[dst_poly].start();
480 float2 other_edgepoint1 = uv_to_xy(uv_map_[other_edge]);
481 float2 other_edgepoint2 = uv_to_xy(uv_map_[other_edge2]);
484 float2 other_ab = other_edgepoint1 - other_edgepoint2;
485 float2 other_reflect_point = other_edgepoint2 + (found_t * other_ab);
486 float2 perpendicular_other_ab;
487 perpendicular_other_ab.x = other_ab.
y;
488 perpendicular_other_ab.y = -other_ab.
x;
492 float2 new_point = other_reflect_point + (found_dist /
math::length(perpendicular_other_ab)) *
493 perpendicular_other_ab;
495 *r_destx = new_point.
x;
496 *r_desty = new_point.
y;
502const int TextureMarginMap::directions[8][2] = {
503 {-1, 0}, {-1, -1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}};
504const int TextureMarginMap::distances[8] = {2, 3, 2, 3, 2, 3, 2, 3};
515 const float uv_offset[2],
516 const bool conservative)
524 TextureMarginMap map(ibuf->
x, ibuf->
y, uv_offset, edges_num, faces, corner_edges, uv_map);
526 bool draw_new_mask =
false;
534 draw_new_mask =
true;
537 for (
int fill = conservative ? 0 : 1; fill < 2; fill++) {
539 const int3 tri = corner_tris[i];
542 for (
int a = 0; a < 3; a++) {
543 const float *uv = uv_map[tri[a]];
545 vec[a][0] = (uv[0] - uv_offset[0]) *
float(ibuf->
x);
546 vec[a][1] = (uv[1] - uv_offset[1]) *
float(ibuf->
y);
552 map.
rasterize_tri(vec[0], vec[1], vec[2], tri_faces[i],
mask, draw_new_mask, fill);
556 char *tmpmask = MEM_dupalloc(
mask);
584 const float uv_offset[2],
585 const bool conservative)
594 mesh->vert_positions(),
597 mesh->corner_edges(),
598 mesh->corner_verts(),
CustomData interface, see also DNA_customdata_types.h.
Read Blender memory allocation and freeing.
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
IndexRange index_range() const
constexpr int64_t size() const
constexpr IndexRange index_range() const
void append(const T &value)
void resize(const int64_t new_size)
GAttributeReader lookup(const StringRef name) const
constexpr bool is_empty() const
void set_pixel(int x, int y, uint32_t value)
TextureMarginMap(size_t w, size_t h, const float uv_offset[2], const int totedge, const OffsetIndices< int > faces, const Span< int > corner_edges, const Span< float2 > uv_map)
void rasterize_tri(float *v1, float *v2, float *v3, uint32_t value, char *mask, bool writemask, bool fill)
void grow_dijkstra(int margin)
void lookup_pixels(ImBuf *ibuf, char *mask, int maxPolygonSteps)
static void zscan_store_pixel(void *map, int x, int y, float u, float v)
uint32_t get_pixel(int x, int y) const
float distance(float, float) RET
VecBase< float, 3 > cross(VecOp< float, 3 >, VecOp< float, 3 >) RET
void corner_tris_calc(Span< float3 > vert_positions, OffsetIndices< int > faces, Span< int > corner_verts, MutableSpan< int3 > corner_tris)
Array< int > build_corner_to_face_map(OffsetIndices< int > faces)
void corner_tris_calc_face_indices(OffsetIndices< int > faces, MutableSpan< int > tri_faces)
int size(const AssetLibraryReference *library_reference)
uchar4 interpolate_bilinear_border_byte(const ImBuf *in, float u, float v)
float4 interpolate_bilinear_border_fl(const ImBuf *in, float u, float v)
T length_squared(const VecBase< T, Size > &a)
T length(const VecBase< T, Size > &a)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
static void generate_margin(ImBuf *ibuf, char *mask, const int margin, const Span< float3 > vert_positions, const int edges_num, const OffsetIndices< int > faces, const Span< int > corner_edges, const Span< int > corner_verts, const Span< float2 > uv_map, const float uv_offset[2], const bool conservative)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
VecBase< uint8_t, 4 > uchar4
void zspan_rasterize_conservative_wireframe(ZSpan *zspan, void *handle, const float *v1, const float *v2, const float *v3, void(*func)(void *, int, int, float, float))
void zbuf_alloc_span(ZSpan *zspan, int rectx, int recty)
MINLINE void copy_v2_v2(float r[2], const float a[2])
void RE_generate_texturemargin_adjacentfaces(ImBuf *ibuf, char *mask, const int margin, const Mesh *mesh, StringRef uv_layer, const float uv_offset[2], const bool conservative)
ATTR_WARN_UNUSED_RESULT const BMVert * v
void zbuf_free_span(ZSpan *zspan)
VecBase< float, 4 > float4
VecBase< float, 2 > float2
VecBase< int32_t, 3 > int3
MINLINE int poly_to_tri_count(int poly_count, int corner_count)
ATTR_WARN_UNUSED_RESULT const BMVert * v2
void IMB_filter_extend(ImBuf *ibuf, char *mask, int filter)
void zspan_rasterize_triangle(ZSpan *zspan, void *handle, const float *v1_in, const float *v2_in, const float *v3_in, void(*func)(void *, int, int, float, float))
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
uint8_t * byte_data_for_write()
float * float_data_for_write()
#define PackDijkstraPixel(dist, dir)
#define DijkstraPixelIsUnset(dp)
#define DijkstraPixelGetDistance(dp)
#define DijkstraPixelGetDirection(dp)
#define IsDijkstraPixel(dp)