Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BKE_uv_islands.hh « blenkernel « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8a8d14aeab52b4e51c4c0a6f0fb1a05cf5be2ea1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751

/* SPDX-License-Identifier: GPL-2.0-or-later */

#pragma once

#include <fstream>
#include <optional>

#include "BLI_array.hh"
#include "BLI_edgehash.h"
#include "BLI_float3x3.hh"
#include "BLI_map.hh"
#include "BLI_math.h"
#include "BLI_math_vec_types.hh"
#include "BLI_rect.h"
#include "BLI_vector.hh"
#include "BLI_vector_list.hh"

#include "DNA_meshdata_types.h"

#include "PIL_time_utildefines.h"

namespace blender::bke::uv_islands {

struct MeshEdge;
struct MeshPrimitive;
struct UVBorder;
struct UVEdge;
struct UVIslands;
struct UVIslandsMask;
struct UVPrimitive;
struct UVPrimitiveEdge;
struct UVVertex;

struct MeshVertex {
  int64_t v;
  Vector<MeshEdge *> edges;
};

struct MeshUVVert {
  MeshVertex *vertex;
  float2 uv;
  int64_t loop;
};

struct MeshEdge {
  MeshVertex *vert1;
  MeshVertex *vert2;
  Vector<MeshPrimitive *> primitives;
};

/** Represents a triangle in 3d space (MLoopTri) */
struct MeshPrimitive {
  int64_t index;
  int64_t poly;
  Vector<MeshEdge *, 3> edges;
  Vector<MeshUVVert, 3> vertices;

  /**
   * UV island this primitive belongs to. This is used to speed up the initial uv island
   * extraction, but should not be used when extending uv islands.
   */
  int64_t uv_island_id;

  MeshUVVert *get_other_uv_vertex(const MeshVertex *v1, const MeshVertex *v2)
  {
    BLI_assert(vertices[0].vertex == v1 || vertices[1].vertex == v1 || vertices[2].vertex == v1);
    BLI_assert(vertices[0].vertex == v2 || vertices[1].vertex == v2 || vertices[2].vertex == v2);
    for (MeshUVVert &uv_vertex : vertices) {
      if (uv_vertex.vertex != v1 && uv_vertex.vertex != v2) {
        return &uv_vertex;
      }
    }
    return nullptr;
  }

  rctf uv_bounds() const;

  bool has_shared_uv_edge(const MeshPrimitive *other) const
  {
    int shared_uv_verts = 0;
    for (const MeshUVVert &vert : vertices) {
      for (const MeshUVVert &other_vert : other->vertices) {
        if (vert.uv == other_vert.uv) {
          shared_uv_verts += 1;
        }
      }
    }
    return shared_uv_verts >= 2;
  }
};

/**
 * MeshData contains input geometry data converted in a list of primitives, edges and vertices for
 * quick access for both local space and uv space.
 */
struct MeshData {
 public:
  const MLoopTri *looptri;
  const int64_t looptri_len;
  const int64_t vert_len;
  const MLoop *mloop;
  const MLoopUV *mloopuv;

 public:
  Vector<MeshPrimitive> primitives;
  Vector<MeshEdge> edges;
  Vector<MeshVertex> vertices;
  /** Total number of uv islands detected. */
  int64_t uv_island_len;

  explicit MeshData(const MLoopTri *looptri,
                    const int64_t looptri_len,
                    const int64_t vert_len,
                    const MLoop *mloop,
                    const MLoopUV *mloopuv)
      : looptri(looptri),
        looptri_len(looptri_len),
        vert_len(vert_len),
        mloop(mloop),
        mloopuv(mloopuv)
  {
    TIMEIT_START(init_mesh_data);
    init_vertices();
    init_primitives();
    init_edges();
    init_primitive_uv_island_ids();
    TIMEIT_END(init_mesh_data);
  }

  void init_vertices()
  {
    vertices.reserve(vert_len);
    for (int64_t i = 0; i < vert_len; i++) {
      MeshVertex vert;
      vert.v = i;
      vertices.append(vert);
    }
  }

  void init_primitives()
  {
    primitives.reserve(looptri_len);
    for (int64_t i = 0; i < looptri_len; i++) {
      const MLoopTri &tri = looptri[i];
      MeshPrimitive primitive;
      primitive.index = i;
      primitive.poly = tri.poly;

      for (int j = 0; j < 3; j++) {
        MeshUVVert uv_vert;
        uv_vert.loop = tri.tri[j];
        uv_vert.vertex = &vertices[mloop[uv_vert.loop].v];
        uv_vert.uv = mloopuv[uv_vert.loop].uv;
        primitive.vertices.append(uv_vert);
      }
      primitives.append(primitive);
    }
  }

  void init_edges()
  {
    edges.reserve(looptri_len * 2);
    EdgeHash *eh = BLI_edgehash_new_ex(__func__, looptri_len * 3);
    for (int64_t i = 0; i < looptri_len; i++) {
      const MLoopTri &tri = looptri[i];
      MeshPrimitive &primitive = primitives[i];
      for (int j = 0; j < 3; j++) {
        int v1 = mloop[tri.tri[j]].v;
        int v2 = mloop[tri.tri[(j + 1) % 3]].v;
        /* TODO: Use lookup_ptr to be able to store edge 0. */
        void *v = BLI_edgehash_lookup(eh, v1, v2);
        int64_t edge_index;
        if (v == nullptr) {
          edge_index = edges.size();
          BLI_edgehash_insert(eh, v1, v2, POINTER_FROM_INT(edge_index + 1));
          MeshEdge edge;
          edge.vert1 = &vertices[v1];
          edge.vert2 = &vertices[v2];
          edges.append(edge);
          MeshEdge *edge_ptr = &edges.last();
          vertices[v1].edges.append(edge_ptr);
          vertices[v2].edges.append(edge_ptr);
        }
        else {
          edge_index = POINTER_AS_INT(v) - 1;
        }

        MeshEdge *edge = &edges[edge_index];
        edge->primitives.append(&primitive);
        primitive.edges.append(edge);
      }
    }
    BLI_edgehash_free(eh, nullptr);
  }

  static const int64_t INVALID_UV_ISLAND_ID = -1;
  /**
   * NOTE: doesn't support weird topology where unconnected mesh primitives share the same uv
   * island. For a accurate implementation we should use implement an uv_prim_lookup.
   */
  static void extract_uv_neighbors(Vector<MeshPrimitive *> &prims_to_add, MeshPrimitive *primitive)
  {
    for (MeshEdge *edge : primitive->edges) {
      for (MeshPrimitive *other_primitive : edge->primitives) {
        if (primitive == other_primitive) {
          continue;
        }
        if (other_primitive->uv_island_id != MeshData::INVALID_UV_ISLAND_ID) {
          continue;
        }

        if (primitive->has_shared_uv_edge(other_primitive)) {
          prims_to_add.append(other_primitive);
        }
      }
    }
  }

  void init_primitive_uv_island_ids()
  {
    for (MeshPrimitive &primitive : primitives) {
      primitive.uv_island_id = INVALID_UV_ISLAND_ID;
    }

    int64_t uv_island_id = 0;
    Vector<MeshPrimitive *> prims_to_add;
    for (MeshPrimitive &primitive : primitives) {
      /* Early exit when uv island id is already extracted during uv neighbor extractions. */
      if (primitive.uv_island_id != INVALID_UV_ISLAND_ID) {
        continue;
      }

      prims_to_add.append(&primitive);
      while (!prims_to_add.is_empty()) {
        MeshPrimitive *primitive = prims_to_add.pop_last();
        primitive->uv_island_id = uv_island_id;
        extract_uv_neighbors(prims_to_add, primitive);
      }
      uv_island_id++;
    }
    uv_island_len = uv_island_id;
  }
};

struct UVVertex {
  MeshVertex *vertex;
  /* Position in uv space. */
  float2 uv;

  /* uv edges that share this UVVertex. */
  Vector<UVEdge *> uv_edges;

  struct {
    bool is_border : 1;
    bool is_extended : 1;
  } flags;

  explicit UVVertex()
  {
    flags.is_border = false;
    flags.is_extended = false;
  }

  explicit UVVertex(const MeshUVVert &vert) : vertex(vert.vertex), uv(vert.uv)
  {
    flags.is_border = false;
    flags.is_extended = false;
  }
};

struct UVEdge {
  std::array<UVVertex *, 2> vertices;
  Vector<UVPrimitive *, 2> uv_primitives;

  bool has_shared_edge(const MeshUVVert &v1, const MeshUVVert &v2) const
  {
    return (vertices[0]->uv == v1.uv && vertices[1]->uv == v2.uv) ||
           (vertices[0]->uv == v2.uv && vertices[1]->uv == v1.uv);
  }

  bool has_shared_edge(const UVVertex &v1, const UVVertex &v2) const
  {
    return (vertices[0]->uv == v1.uv && vertices[1]->uv == v2.uv) ||
           (vertices[0]->uv == v2.uv && vertices[1]->uv == v1.uv);
  }

  bool has_shared_edge(const UVEdge &other) const
  {
    return has_shared_edge(*other.vertices[0], *other.vertices[1]);
  }

  bool has_same_vertices(const MeshVertex &vert1, const MeshVertex &vert2) const
  {
    return (vertices[0]->vertex == &vert1 && vertices[1]->vertex == &vert2) ||
           (vertices[0]->vertex == &vert2 && vertices[1]->vertex == &vert1);
  }

  bool has_same_uv_vertices(const UVEdge &other) const
  {
    return has_shared_edge(other) &&
           has_same_vertices(*other.vertices[0]->vertex, *other.vertices[1]->vertex);
    ;
  }

  bool has_same_vertices(const MeshEdge &edge) const
  {
    return has_same_vertices(*edge.vert1, *edge.vert2);
  }

  bool is_border_edge() const
  {
    return uv_primitives.size() == 1;
  }

  void append_to_uv_vertices()
  {
    for (UVVertex *vertex : vertices) {
      vertex->uv_edges.append_non_duplicates(this);
    }
  }

  UVVertex *get_other_uv_vertex(const MeshVertex *vertex)
  {
    if (vertices[0]->vertex == vertex) {
      return vertices[1];
    }
    return vertices[0];
  }
};

struct UVPrimitive {
  /**
   * Index of the primitive in the original mesh.
   */
  MeshPrimitive *primitive;
  Vector<UVEdge *, 3> edges;

  explicit UVPrimitive(MeshPrimitive *primitive) : primitive(primitive)
  {
  }

  void append_to_uv_edges()
  {
    for (UVEdge *uv_edge : edges) {
      uv_edge->uv_primitives.append_non_duplicates(this);
    }
  }
  void append_to_uv_vertices()
  {
    for (UVEdge *uv_edge : edges) {
      uv_edge->append_to_uv_vertices();
    }
  }

  Vector<std::pair<UVEdge *, UVEdge *>> shared_edges(UVPrimitive &other)
  {
    Vector<std::pair<UVEdge *, UVEdge *>> result;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        if (edges[i]->has_shared_edge(*other.edges[j])) {
          result.append(std::pair<UVEdge *, UVEdge *>(edges[i], other.edges[j]));
        }
      }
    }
    return result;
  }

  bool has_shared_edge(const UVPrimitive &other) const
  {
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        if (edges[i]->has_shared_edge(*other.edges[j])) {
          return true;
        }
      }
    }
    return false;
  }

  bool has_shared_edge(const MeshPrimitive &primitive) const
  {
    for (const UVEdge *uv_edge : edges) {
      const MeshUVVert *v1 = &primitive.vertices.last();
      for (int i = 0; i < primitive.vertices.size(); i++) {
        const MeshUVVert *v2 = &primitive.vertices[i];
        if (uv_edge->has_shared_edge(*v1, *v2)) {
          return true;
        }
        v1 = v2;
      }
    }
    return false;
  }

  /**
   * Get the UVVertex in the order that the verts are ordered in the MeshPrimitive.
   */
  const UVVertex *get_uv_vertex(const uint8_t mesh_vert_index) const
  {
    const MeshVertex *mesh_vertex = primitive->vertices[mesh_vert_index].vertex;
    for (const UVEdge *uv_edge : edges) {
      for (const UVVertex *uv_vert : uv_edge->vertices) {
        if (uv_vert->vertex == mesh_vertex) {
          return uv_vert;
        }
      }
    }
    BLI_assert_unreachable();
    return nullptr;
  }

  /**
   * Get the UVEdge that share the given uv coordinates.
   * Will assert when no UVEdge found.
   */
  UVEdge *get_uv_edge(const float2 uv1, const float2 uv2) const
  {
    for (UVEdge *uv_edge : edges) {
      const float2 &e1 = uv_edge->vertices[0]->uv;
      const float2 &e2 = uv_edge->vertices[1]->uv;
      if ((e1 == uv1 && e2 == uv2) || (e1 == uv2 && e2 == uv1)) {
        return uv_edge;
      }
    }
    BLI_assert_unreachable();
    return nullptr;
  }

  UVEdge *get_uv_edge(const MeshVertex *v1, const MeshVertex *v2) const
  {
    for (UVEdge *uv_edge : edges) {
      const MeshVertex *e1 = uv_edge->vertices[0]->vertex;
      const MeshVertex *e2 = uv_edge->vertices[1]->vertex;
      if ((e1 == v1 && e2 == v2) || (e1 == v2 && e2 == v1)) {
        return uv_edge;
      }
    }
    BLI_assert_unreachable();
    return nullptr;
  }

  const bool contains_uv_vertex(const UVVertex *uv_vertex) const
  {
    for (UVEdge *edge : edges) {
      if (std::find(edge->vertices.begin(), edge->vertices.end(), uv_vertex) !=
          edge->vertices.end()) {
        return true;
      }
    }
    return false;
  }

  const UVVertex *get_other_uv_vertex(const UVVertex *v1, const UVVertex *v2) const
  {
    BLI_assert(contains_uv_vertex(v1));
    BLI_assert(contains_uv_vertex(v2));

    for (const UVEdge *edge : edges) {
      for (const UVVertex *uv_vertex : edge->vertices) {
        if (uv_vertex != v1 && uv_vertex != v2) {
          return uv_vertex;
        }
      }
    }
    BLI_assert_unreachable();
    return nullptr;
  }

  UVBorder extract_border() const;
};

struct UVBorderEdge {
  UVEdge *edge;
  bool tag = false;
  UVPrimitive *uv_primitive;
  /* Should the vertices of the edge be evaluated in reverse order. */
  bool reverse_order = false;

  int64_t index = -1;
  int64_t prev_index = -1;
  int64_t next_index = -1;
  int64_t border_index = -1;

  explicit UVBorderEdge(UVEdge *edge, UVPrimitive *uv_primitive)
      : edge(edge), uv_primitive(uv_primitive)
  {
  }

  UVVertex *get_uv_vertex(int index)
  {
    int actual_index = reverse_order ? 1 - index : index;
    return edge->vertices[actual_index];
  }

  const UVVertex *get_uv_vertex(int index) const
  {
    int actual_index = reverse_order ? 1 - index : index;
    return edge->vertices[actual_index];
  }

  /**
   * Get the uv vertex from the primitive that is not part of the edge.
   */
  const UVVertex *get_other_uv_vertex() const
  {
    return uv_primitive->get_other_uv_vertex(edge->vertices[0], edge->vertices[1]);
  }

  float length() const
  {
    return len_v2v2(edge->vertices[0]->uv, edge->vertices[1]->uv);
  }
};

struct UVBorderCorner {
  UVBorderEdge *first;
  UVBorderEdge *second;
  float angle;

  UVBorderCorner(UVBorderEdge *first, UVBorderEdge *second, float angle)
      : first(first), second(second), angle(angle)
  {
  }

  /**
   * Calculate a uv coordinate between the edges of the corner.
   *
   * 'min_uv_distance' is the minimum distance between the corner and the
   * resulting uv coordinate. The distance is in uv space.
   */
  float2 uv(float factor, float min_uv_distance);
};

struct UVBorder {
  /** Ordered list of UV Verts of the border of this island. */
  // TODO: support multiple rings + order (CW, CCW)
  Vector<UVBorderEdge> edges;

  /**
   * Check if the border is counter clock wise from its island.
   */
  bool is_ccw() const;

  /**
   * Flip the order of the verts, changing the order between CW and CCW.
   */
  void flip();

  /**
   * Calculate the outside angle of the given vert.
   */
  float outside_angle(const UVBorderEdge &vert) const;

  void update_indexes(uint64_t border_index);

  static std::optional<UVBorder> extract_from_edges(Vector<UVBorderEdge> &edges);

  /** Remove edge from the border. updates the indexes. */
  void remove(int64_t index)
  {
    /* Could read the border_index from any border edge as they are consistent. */
    uint64_t border_index = edges[0].border_index;
    edges.remove(index);
    update_indexes(border_index);
  }
};

struct UVIsland {
  VectorList<UVVertex> uv_vertices;
  VectorList<UVEdge> uv_edges;
  VectorList<UVPrimitive> uv_primitives;
  /**
   * List of borders of this island. There can be multiple borders per island as a border could
   * be completely encapsulated by another one.
   */
  Vector<UVBorder> borders;

  /**
   * Key is mesh vert index, Value is list of UVVertices that refer to the mesh vertex with that
   * index. Map is used internally to quickly lookup similar UVVertices.
   */
  Map<int64_t, Vector<UVVertex *>> uv_vertex_lookup;

  UVVertex *lookup(const UVVertex &vertex)
  {
    int64_t vert_index = vertex.vertex->v;
    Vector<UVVertex *> &vertices = uv_vertex_lookup.lookup_or_add_default(vert_index);
    for (UVVertex *v : vertices) {
      if (v->uv == vertex.uv) {
        return v;
      }
    }
    return nullptr;
  }

  UVVertex *lookup_or_create(const UVVertex &vertex)
  {
    UVVertex *found_vertex = lookup(vertex);
    if (found_vertex != nullptr) {
      return found_vertex;
    }

    uv_vertices.append(vertex);
    UVVertex *result = &uv_vertices.last();
    result->uv_edges.clear();
    /* v is already a key. Ensured by UVIsland::lookup in this method. */
    uv_vertex_lookup.lookup(vertex.vertex->v).append(result);
    return result;
  }

  UVEdge *lookup(const UVEdge &edge)
  {
    UVVertex *found_vertex = lookup(*edge.vertices[0]);
    if (found_vertex == nullptr) {
      return nullptr;
    }
    for (UVEdge *e : found_vertex->uv_edges) {
      UVVertex *other_vertex = e->get_other_uv_vertex(found_vertex->vertex);
      if (other_vertex->vertex == edge.vertices[1]->vertex &&
          other_vertex->uv == edge.vertices[1]->uv) {
        return e;
      }
    }
    return nullptr;
  }

  UVEdge *lookup_or_create(const UVEdge &edge)
  {
    UVEdge *found_edge = lookup(edge);
    if (found_edge != nullptr) {
      return found_edge;
    }

    uv_edges.append(edge);
    UVEdge *result = &uv_edges.last();
    result->uv_primitives.clear();
    return result;
  }

  /** Initialize the border attribute. */
  void extract_borders();
  /** Iterative extend border to fit the mask. */
  void extend_border(const UVIslandsMask &mask, const short island_index);

 private:
  void append(const UVPrimitive &primitive)
  {
    uv_primitives.append(primitive);
    UVPrimitive *new_prim_ptr = &uv_primitives.last();
    for (int i = 0; i < 3; i++) {
      UVEdge *other_edge = primitive.edges[i];
      UVEdge uv_edge_template;
      uv_edge_template.vertices[0] = lookup_or_create(*other_edge->vertices[0]);
      uv_edge_template.vertices[1] = lookup_or_create(*other_edge->vertices[1]);
      new_prim_ptr->edges[i] = lookup_or_create(uv_edge_template);
      new_prim_ptr->edges[i]->append_to_uv_vertices();
      new_prim_ptr->edges[i]->uv_primitives.append(new_prim_ptr);
    }
  }

 public:
  bool has_shared_edge(const UVPrimitive &primitive) const
  {
    for (const VectorList<UVPrimitive>::UsedVector &prims : uv_primitives) {
      for (const UVPrimitive &prim : prims) {
        if (prim.has_shared_edge(primitive)) {
          return true;
        }
      }
    }
    return false;
  }

  bool has_shared_edge(const MeshPrimitive &primitive) const
  {
    for (const VectorList<UVPrimitive>::UsedVector &primitives : uv_primitives) {
      for (const UVPrimitive &prim : primitives) {
        if (prim.has_shared_edge(primitive)) {
          return true;
        }
      }
    }
    return false;
  }

  const void extend_border(const UVPrimitive &primitive)
  {
    for (const VectorList<UVPrimitive>::UsedVector &primitives : uv_primitives) {
      for (const UVPrimitive &prim : primitives) {
        if (prim.has_shared_edge(primitive)) {
          append(primitive);
        }
      }
    }
  }
};

struct UVIslands {
  Vector<UVIsland> islands;

  explicit UVIslands(MeshData &mesh_data);

  void extract_borders();
  void extend_borders(const UVIslandsMask &islands_mask);
};

/** Mask to find the index of the UVIsland for a given UV coordinate. */
struct UVIslandsMask {

  /** Mask for each udim tile. */
  struct Tile {
    float2 udim_offset;
    ushort2 tile_resolution;
    ushort2 mask_resolution;
    Array<uint16_t> mask;

    Tile(float2 udim_offset, ushort2 tile_resolution);

    bool is_masked(const uint16_t island_index, const float2 uv) const;
    bool contains(const float2 uv) const;
    float get_pixel_size_in_uv_space() const;
  };

  Vector<Tile> tiles;

  void add_tile(float2 udim_offset, ushort2 resolution);

  /**
   * Find a tile containing the given uv coordinate.
   */
  const Tile *find_tile(const float2 uv) const;

  /**
   * Is the given uv coordinate part of the given island_index mask.
   *
   * true - part of the island mask.
   * false - not part of the island mask.
   */
  bool is_masked(const uint16_t island_index, const float2 uv) const;

  /**
   * Add the given UVIslands to the mask. Tiles should be added beforehand using the 'add_tile'
   * method.
   */
  void add(const UVIslands &islands);

  void dilate(int max_iterations);
};

}  // namespace blender::bke::uv_islands