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

mesh_mapping.c « intern « blenkernel « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 686f58a0cebd5ae65745f5c7f0b7916806bf1cf0 (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
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

/** \file
 * \ingroup bke
 *
 * Functions for accessing mesh connectivity data.
 * eg: polys connected to verts, UV's connected to verts.
 */

#include "MEM_guardedalloc.h"

#include "DNA_meshdata_types.h"
#include "DNA_vec_types.h"

#include "BLI_bitmap.h"
#include "BLI_buffer.h"
#include "BLI_math.h"
#include "BLI_utildefines.h"

#include "BKE_customdata.h"
#include "BKE_mesh_mapping.h"
#include "BLI_memarena.h"

#include "BLI_strict_flags.h"

/* -------------------------------------------------------------------- */
/** \name Mesh Connectivity Mapping
 * \{ */

/* ngon version wip, based on BM_uv_vert_map_create */
/* this replaces the non bmesh function (in trunk) which takes MTFace's,
 * if we ever need it back we could but for now this replaces it because its unused. */

UvVertMap *BKE_mesh_uv_vert_map_create(const MPoly *mpoly,
                                       const MLoop *mloop,
                                       const MLoopUV *mloopuv,
                                       unsigned int totpoly,
                                       unsigned int totvert,
                                       const float limit[2],
                                       const bool selected,
                                       const bool use_winding)
{
  UvVertMap *vmap;
  UvMapVert *buf;
  const MPoly *mp;
  unsigned int a;
  int i, totuv, nverts;

  bool *winding = NULL;
  BLI_buffer_declare_static(vec2f, tf_uv_buf, BLI_BUFFER_NOP, 32);

  totuv = 0;

  /* generate UvMapVert array */
  mp = mpoly;
  for (a = 0; a < totpoly; a++, mp++) {
    if (!selected || (!(mp->flag & ME_HIDE) && (mp->flag & ME_FACE_SEL))) {
      totuv += mp->totloop;
    }
  }

  if (totuv == 0) {
    return NULL;
  }

  vmap = (UvVertMap *)MEM_callocN(sizeof(*vmap), "UvVertMap");
  buf = vmap->buf = (UvMapVert *)MEM_callocN(sizeof(*vmap->buf) * (size_t)totuv, "UvMapVert");
  vmap->vert = (UvMapVert **)MEM_callocN(sizeof(*vmap->vert) * totvert, "UvMapVert*");
  if (use_winding) {
    winding = MEM_callocN(sizeof(*winding) * totpoly, "winding");
  }

  if (!vmap->vert || !vmap->buf) {
    BKE_mesh_uv_vert_map_free(vmap);
    return NULL;
  }

  mp = mpoly;
  for (a = 0; a < totpoly; a++, mp++) {
    if (!selected || (!(mp->flag & ME_HIDE) && (mp->flag & ME_FACE_SEL))) {
      float(*tf_uv)[2] = NULL;

      if (use_winding) {
        tf_uv = (float(*)[2])BLI_buffer_reinit_data(&tf_uv_buf, vec2f, (size_t)mp->totloop);
      }

      nverts = mp->totloop;

      for (i = 0; i < nverts; i++) {
        buf->loop_of_poly_index = (unsigned short)i;
        buf->poly_index = a;
        buf->separate = 0;
        buf->next = vmap->vert[mloop[mp->loopstart + i].v];
        vmap->vert[mloop[mp->loopstart + i].v] = buf;

        if (use_winding) {
          copy_v2_v2(tf_uv[i], mloopuv[mpoly[a].loopstart + i].uv);
        }

        buf++;
      }

      if (use_winding) {
        winding[a] = cross_poly_v2(tf_uv, (unsigned int)nverts) > 0;
      }
    }
  }

  /* sort individual uvs for each vert */
  for (a = 0; a < totvert; a++) {
    UvMapVert *newvlist = NULL, *vlist = vmap->vert[a];
    UvMapVert *iterv, *v, *lastv, *next;
    const float *uv, *uv2;
    float uvdiff[2];

    while (vlist) {
      v = vlist;
      vlist = vlist->next;
      v->next = newvlist;
      newvlist = v;

      uv = mloopuv[mpoly[v->poly_index].loopstart + v->loop_of_poly_index].uv;
      lastv = NULL;
      iterv = vlist;

      while (iterv) {
        next = iterv->next;

        uv2 = mloopuv[mpoly[iterv->poly_index].loopstart + iterv->loop_of_poly_index].uv;
        sub_v2_v2v2(uvdiff, uv2, uv);

        if (fabsf(uv[0] - uv2[0]) < limit[0] && fabsf(uv[1] - uv2[1]) < limit[1] &&
            (!use_winding || winding[iterv->poly_index] == winding[v->poly_index])) {
          if (lastv) {
            lastv->next = next;
          }
          else {
            vlist = next;
          }
          iterv->next = newvlist;
          newvlist = iterv;
        }
        else {
          lastv = iterv;
        }

        iterv = next;
      }

      newvlist->separate = 1;
    }

    vmap->vert[a] = newvlist;
  }

  if (use_winding) {
    MEM_freeN(winding);
  }

  BLI_buffer_free(&tf_uv_buf);

  return vmap;
}

UvMapVert *BKE_mesh_uv_vert_map_get_vert(UvVertMap *vmap, unsigned int v)
{
  return vmap->vert[v];
}

void BKE_mesh_uv_vert_map_free(UvVertMap *vmap)
{
  if (vmap) {
    if (vmap->vert) {
      MEM_freeN(vmap->vert);
    }
    if (vmap->buf) {
      MEM_freeN(vmap->buf);
    }
    MEM_freeN(vmap);
  }
}

/**
 * Generates a map where the key is the vertex and the value is a list
 * of polys or loops that use that vertex as a corner. The lists are allocated
 * from one memory pool.
 *
 * Wrapped by #BKE_mesh_vert_poly_map_create & BKE_mesh_vert_loop_map_create
 */
static void mesh_vert_poly_or_loop_map_create(MeshElemMap **r_map,
                                              int **r_mem,
                                              const MPoly *mpoly,
                                              const MLoop *mloop,
                                              int totvert,
                                              int totpoly,
                                              int totloop,
                                              const bool do_loops)
{
  MeshElemMap *map = MEM_callocN(sizeof(MeshElemMap) * (size_t)totvert, __func__);
  int *indices, *index_iter;
  int i, j;

  indices = index_iter = MEM_mallocN(sizeof(int) * (size_t)totloop, __func__);

  /* Count number of polys for each vertex */
  for (i = 0; i < totpoly; i++) {
    const MPoly *p = &mpoly[i];

    for (j = 0; j < p->totloop; j++) {
      map[mloop[p->loopstart + j].v].count++;
    }
  }

  /* Assign indices mem */
  for (i = 0; i < totvert; i++) {
    map[i].indices = index_iter;
    index_iter += map[i].count;

    /* Reset 'count' for use as index in last loop */
    map[i].count = 0;
  }

  /* Find the users */
  for (i = 0; i < totpoly; i++) {
    const MPoly *p = &mpoly[i];

    for (j = 0; j < p->totloop; j++) {
      unsigned int v = mloop[p->loopstart + j].v;

      map[v].indices[map[v].count] = do_loops ? p->loopstart + j : i;
      map[v].count++;
    }
  }

  *r_map = map;
  *r_mem = indices;
}

/**
 * Generates a map where the key is the vertex and the value
 * is a list of polys that use that vertex as a corner.
 * The lists are allocated from one memory pool.
 */
void BKE_mesh_vert_poly_map_create(MeshElemMap **r_map,
                                   int **r_mem,
                                   const MPoly *mpoly,
                                   const MLoop *mloop,
                                   int totvert,
                                   int totpoly,
                                   int totloop)
{
  mesh_vert_poly_or_loop_map_create(r_map, r_mem, mpoly, mloop, totvert, totpoly, totloop, false);
}

/**
 * Generates a map where the key is the vertex and the value
 * is a list of loops that use that vertex as a corner.
 * The lists are allocated from one memory pool.
 */
void BKE_mesh_vert_loop_map_create(MeshElemMap **r_map,
                                   int **r_mem,
                                   const MPoly *mpoly,
                                   const MLoop *mloop,
                                   int totvert,
                                   int totpoly,
                                   int totloop)
{
  mesh_vert_poly_or_loop_map_create(r_map, r_mem, mpoly, mloop, totvert, totpoly, totloop, true);
}

/**
 * Generates a map where the key is the edge and the value
 * is a list of looptris that use that edge.
 * The lists are allocated from one memory pool.
 */
void BKE_mesh_vert_looptri_map_create(MeshElemMap **r_map,
                                      int **r_mem,
                                      const MVert *UNUSED(mvert),
                                      const int totvert,
                                      const MLoopTri *mlooptri,
                                      const int totlooptri,
                                      const MLoop *mloop,
                                      const int UNUSED(totloop))
{
  MeshElemMap *map = MEM_callocN(sizeof(MeshElemMap) * (size_t)totvert, __func__);
  int *indices = MEM_mallocN(sizeof(int) * (size_t)totlooptri * 3, __func__);
  int *index_step;
  const MLoopTri *mlt;
  int i;

  /* count face users */
  for (i = 0, mlt = mlooptri; i < totlooptri; mlt++, i++) {
    for (int j = 3; j--;) {
      map[mloop[mlt->tri[j]].v].count++;
    }
  }

  /* create offsets */
  index_step = indices;
  for (i = 0; i < totvert; i++) {
    map[i].indices = index_step;
    index_step += map[i].count;

    /* re-count, using this as an index below */
    map[i].count = 0;
  }

  /* assign looptri-edge users */
  for (i = 0, mlt = mlooptri; i < totlooptri; mlt++, i++) {
    for (int j = 3; j--;) {
      MeshElemMap *map_ele = &map[mloop[mlt->tri[j]].v];
      map_ele->indices[map_ele->count++] = i;
    }
  }

  *r_map = map;
  *r_mem = indices;
}

/**
 * Generates a map where the key is the vertex and the value
 * is a list of edges that use that vertex as an endpoint.
 * The lists are allocated from one memory pool.
 */
void BKE_mesh_vert_edge_map_create(
    MeshElemMap **r_map, int **r_mem, const MEdge *medge, int totvert, int totedge)
{
  MeshElemMap *map = MEM_callocN(sizeof(MeshElemMap) * (size_t)totvert, "vert-edge map");
  int *indices = MEM_mallocN(sizeof(int[2]) * (size_t)totedge, "vert-edge map mem");
  int *i_pt = indices;

  int i;

  /* Count number of edges for each vertex */
  for (i = 0; i < totedge; i++) {
    map[medge[i].v1].count++;
    map[medge[i].v2].count++;
  }

  /* Assign indices mem */
  for (i = 0; i < totvert; i++) {
    map[i].indices = i_pt;
    i_pt += map[i].count;

    /* Reset 'count' for use as index in last loop */
    map[i].count = 0;
  }

  /* Find the users */
  for (i = 0; i < totedge; i++) {
    const unsigned int v[2] = {medge[i].v1, medge[i].v2};

    map[v[0]].indices[map[v[0]].count] = i;
    map[v[1]].indices[map[v[1]].count] = i;

    map[v[0]].count++;
    map[v[1]].count++;
  }

  *r_map = map;
  *r_mem = indices;
}

/**
 * A version of #BKE_mesh_vert_edge_map_create that references connected vertices directly
 * (not their edges).
 */
void BKE_mesh_vert_edge_vert_map_create(
    MeshElemMap **r_map, int **r_mem, const MEdge *medge, int totvert, int totedge)
{
  MeshElemMap *map = MEM_callocN(sizeof(MeshElemMap) * (size_t)totvert, "vert-edge map");
  int *indices = MEM_mallocN(sizeof(int[2]) * (size_t)totedge, "vert-edge map mem");
  int *i_pt = indices;

  int i;

  /* Count number of edges for each vertex */
  for (i = 0; i < totedge; i++) {
    map[medge[i].v1].count++;
    map[medge[i].v2].count++;
  }

  /* Assign indices mem */
  for (i = 0; i < totvert; i++) {
    map[i].indices = i_pt;
    i_pt += map[i].count;

    /* Reset 'count' for use as index in last loop */
    map[i].count = 0;
  }

  /* Find the users */
  for (i = 0; i < totedge; i++) {
    const unsigned int v[2] = {medge[i].v1, medge[i].v2};

    map[v[0]].indices[map[v[0]].count] = (int)v[1];
    map[v[1]].indices[map[v[1]].count] = (int)v[0];

    map[v[0]].count++;
    map[v[1]].count++;
  }

  *r_map = map;
  *r_mem = indices;
}

/**
 * Generates a map where the key is the edge and the value is a list of loops that use that edge.
 * Loops indices of a same poly are contiguous and in winding order.
 * The lists are allocated from one memory pool.
 */
void BKE_mesh_edge_loop_map_create(MeshElemMap **r_map,
                                   int **r_mem,
                                   const MEdge *UNUSED(medge),
                                   const int totedge,
                                   const MPoly *mpoly,
                                   const int totpoly,
                                   const MLoop *mloop,
                                   const int totloop)
{
  MeshElemMap *map = MEM_callocN(sizeof(MeshElemMap) * (size_t)totedge, "edge-poly map");
  int *indices = MEM_mallocN(sizeof(int) * (size_t)totloop * 2, "edge-poly map mem");
  int *index_step;
  const MPoly *mp;
  int i;

  /* count face users */
  for (i = 0, mp = mpoly; i < totpoly; mp++, i++) {
    const MLoop *ml;
    int j = mp->totloop;
    for (ml = &mloop[mp->loopstart]; j--; ml++) {
      map[ml->e].count += 2;
    }
  }

  /* create offsets */
  index_step = indices;
  for (i = 0; i < totedge; i++) {
    map[i].indices = index_step;
    index_step += map[i].count;

    /* re-count, using this as an index below */
    map[i].count = 0;
  }

  /* assign loop-edge users */
  for (i = 0, mp = mpoly; i < totpoly; mp++, i++) {
    const MLoop *ml;
    MeshElemMap *map_ele;
    const int max_loop = mp->loopstart + mp->totloop;
    int j = mp->loopstart;
    for (ml = &mloop[j]; j < max_loop; j++, ml++) {
      map_ele = &map[ml->e];
      map_ele->indices[map_ele->count++] = j;
      map_ele->indices[map_ele->count++] = j + 1;
    }
    /* last edge/loop of poly, must point back to first loop! */
    map_ele->indices[map_ele->count - 1] = mp->loopstart;
  }

  *r_map = map;
  *r_mem = indices;
}

/**
 * Generates a map where the key is the edge and the value
 * is a list of polygons that use that edge.
 * The lists are allocated from one memory pool.
 */
void BKE_mesh_edge_poly_map_create(MeshElemMap **r_map,
                                   int **r_mem,
                                   const MEdge *UNUSED(medge),
                                   const int totedge,
                                   const MPoly *mpoly,
                                   const int totpoly,
                                   const MLoop *mloop,
                                   const int totloop)
{
  MeshElemMap *map = MEM_callocN(sizeof(MeshElemMap) * (size_t)totedge, "edge-poly map");
  int *indices = MEM_mallocN(sizeof(int) * (size_t)totloop, "edge-poly map mem");
  int *index_step;
  const MPoly *mp;
  int i;

  /* count face users */
  for (i = 0, mp = mpoly; i < totpoly; mp++, i++) {
    const MLoop *ml;
    int j = mp->totloop;
    for (ml = &mloop[mp->loopstart]; j--; ml++) {
      map[ml->e].count++;
    }
  }

  /* create offsets */
  index_step = indices;
  for (i = 0; i < totedge; i++) {
    map[i].indices = index_step;
    index_step += map[i].count;

    /* re-count, using this as an index below */
    map[i].count = 0;
  }

  /* assign poly-edge users */
  for (i = 0, mp = mpoly; i < totpoly; mp++, i++) {
    const MLoop *ml;
    int j = mp->totloop;
    for (ml = &mloop[mp->loopstart]; j--; ml++) {
      MeshElemMap *map_ele = &map[ml->e];
      map_ele->indices[map_ele->count++] = i;
    }
  }

  *r_map = map;
  *r_mem = indices;
}

/**
 * This function creates a map so the source-data (vert/edge/loop/poly)
 * can loop over the destination data (using the destination arrays origindex).
 *
 * This has the advantage that it can operate on any data-types.
 *
 * \param totsource: The total number of elements the that \a final_origindex points to.
 * \param totfinal: The size of \a final_origindex
 * \param final_origindex: The size of the final array.
 *
 * \note ``totsource`` could be ``totpoly``,
 *       ``totfinal`` could be ``tottessface`` and ``final_origindex`` its ORIGINDEX customdata.
 *       This would allow an MPoly to loop over its tessfaces.
 */
void BKE_mesh_origindex_map_create(MeshElemMap **r_map,
                                   int **r_mem,
                                   const int totsource,
                                   const int *final_origindex,
                                   const int totfinal)
{
  MeshElemMap *map = MEM_callocN(sizeof(MeshElemMap) * (size_t)totsource, "poly-tessface map");
  int *indices = MEM_mallocN(sizeof(int) * (size_t)totfinal, "poly-tessface map mem");
  int *index_step;
  int i;

  /* count face users */
  for (i = 0; i < totfinal; i++) {
    if (final_origindex[i] != ORIGINDEX_NONE) {
      BLI_assert(final_origindex[i] < totsource);
      map[final_origindex[i]].count++;
    }
  }

  /* create offsets */
  index_step = indices;
  for (i = 0; i < totsource; i++) {
    map[i].indices = index_step;
    index_step += map[i].count;

    /* re-count, using this as an index below */
    map[i].count = 0;
  }

  /* assign poly-tessface users */
  for (i = 0; i < totfinal; i++) {
    if (final_origindex[i] != ORIGINDEX_NONE) {
      MeshElemMap *map_ele = &map[final_origindex[i]];
      map_ele->indices[map_ele->count++] = i;
    }
  }

  *r_map = map;
  *r_mem = indices;
}

/**
 * A version of #BKE_mesh_origindex_map_create that takes a looptri array.
 * Making a poly -> looptri map.
 */
void BKE_mesh_origindex_map_create_looptri(MeshElemMap **r_map,
                                           int **r_mem,
                                           const MPoly *mpoly,
                                           const int mpoly_num,
                                           const MLoopTri *looptri,
                                           const int looptri_num)
{
  MeshElemMap *map = MEM_callocN(sizeof(MeshElemMap) * (size_t)mpoly_num, "poly-tessface map");
  int *indices = MEM_mallocN(sizeof(int) * (size_t)looptri_num, "poly-tessface map mem");
  int *index_step;
  int i;

  /* create offsets */
  index_step = indices;
  for (i = 0; i < mpoly_num; i++) {
    map[i].indices = index_step;
    index_step += ME_POLY_TRI_TOT(&mpoly[i]);
  }

  /* assign poly-tessface users */
  for (i = 0; i < looptri_num; i++) {
    MeshElemMap *map_ele = &map[looptri[i].poly];
    map_ele->indices[map_ele->count++] = i;
  }

  *r_map = map;
  *r_mem = indices;
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Mesh loops/poly islands.
 * Used currently for UVs and 'smooth groups'.
 * \{ */

/**
 * Callback deciding whether the given poly/loop/edge define an island boundary or not.
 */
typedef bool (*MeshRemap_CheckIslandBoundary)(const struct MPoly *mpoly,
                                              const struct MLoop *mloop,
                                              const struct MEdge *medge,
                                              const int nbr_egde_users,
                                              const struct MPoly *mpoly_array,
                                              const struct MeshElemMap *edge_poly_map,
                                              void *user_data);

static void poly_edge_loop_islands_calc(const MEdge *medge,
                                        const int totedge,
                                        const MPoly *mpoly,
                                        const int totpoly,
                                        const MLoop *mloop,
                                        const int totloop,
                                        MeshElemMap *edge_poly_map,
                                        const bool use_bitflags,
                                        MeshRemap_CheckIslandBoundary edge_boundary_check,
                                        void *edge_boundary_check_data,
                                        int **r_poly_groups,
                                        int *r_totgroup,
                                        BLI_bitmap **r_edge_borders,
                                        int *r_totedgeborder)
{
  int *poly_groups;
  int *poly_stack;

  BLI_bitmap *edge_borders = NULL;
  int num_edgeborders = 0;

  int poly_prev = 0;
  const int temp_poly_group_id = 3; /* Placeholder value. */

  /* Group we could not find any available bit, will be reset to 0 at end. */
  const int poly_group_id_overflowed = 5;

  int tot_group = 0;
  bool group_id_overflow = false;

  /* map vars */
  int *edge_poly_mem = NULL;

  if (totpoly == 0) {
    *r_totgroup = 0;
    *r_poly_groups = NULL;
    if (r_edge_borders) {
      *r_edge_borders = NULL;
      *r_totedgeborder = 0;
    }
    return;
  }

  if (r_edge_borders) {
    edge_borders = BLI_BITMAP_NEW(totedge, __func__);
    *r_totedgeborder = 0;
  }

  if (!edge_poly_map) {
    BKE_mesh_edge_poly_map_create(
        &edge_poly_map, &edge_poly_mem, medge, totedge, mpoly, totpoly, mloop, totloop);
  }

  poly_groups = MEM_callocN(sizeof(int) * (size_t)totpoly, __func__);
  poly_stack = MEM_mallocN(sizeof(int) * (size_t)totpoly, __func__);

  while (true) {
    int poly;
    int bit_poly_group_mask = 0;
    int poly_group_id;
    int ps_curr_idx = 0, ps_end_idx = 0; /* stack indices */

    for (poly = poly_prev; poly < totpoly; poly++) {
      if (poly_groups[poly] == 0) {
        break;
      }
    }

    if (poly == totpoly) {
      /* all done */
      break;
    }

    poly_group_id = use_bitflags ? temp_poly_group_id : ++tot_group;

    /* start searching from here next time */
    poly_prev = poly + 1;

    poly_groups[poly] = poly_group_id;
    poly_stack[ps_end_idx++] = poly;

    while (ps_curr_idx != ps_end_idx) {
      const MPoly *mp;
      const MLoop *ml;
      int j;

      poly = poly_stack[ps_curr_idx++];
      BLI_assert(poly_groups[poly] == poly_group_id);

      mp = &mpoly[poly];
      for (ml = &mloop[mp->loopstart], j = mp->totloop; j--; ml++) {
        /* loop over poly users */
        const int me_idx = (int)ml->e;
        const MEdge *me = &medge[me_idx];
        const MeshElemMap *map_ele = &edge_poly_map[me_idx];
        const int *p = map_ele->indices;
        int i = map_ele->count;
        if (!edge_boundary_check(mp, ml, me, i, mpoly, map_ele, edge_boundary_check_data)) {
          for (; i--; p++) {
            /* if we meet other non initialized its a bug */
            BLI_assert(ELEM(poly_groups[*p], 0, poly_group_id));

            if (poly_groups[*p] == 0) {
              poly_groups[*p] = poly_group_id;
              poly_stack[ps_end_idx++] = *p;
            }
          }
        }
        else {
          if (edge_borders && !BLI_BITMAP_TEST(edge_borders, me_idx)) {
            BLI_BITMAP_ENABLE(edge_borders, me_idx);
            num_edgeborders++;
          }
          if (use_bitflags) {
            /* Find contiguous smooth groups already assigned,
             * these are the values we can't reuse! */
            for (; i--; p++) {
              int bit = poly_groups[*p];
              if (!ELEM(bit, 0, poly_group_id, poly_group_id_overflowed) &&
                  !(bit_poly_group_mask & bit)) {
                bit_poly_group_mask |= bit;
              }
            }
          }
        }
      }
    }
    /* And now, we have all our poly from current group in poly_stack
     * (from 0 to (ps_end_idx - 1)),
     * as well as all smoothgroups bits we can't use in bit_poly_group_mask.
     */
    if (use_bitflags) {
      int i, *p, gid_bit = 0;
      poly_group_id = 1;

      /* Find first bit available! */
      for (; (poly_group_id & bit_poly_group_mask) && (gid_bit < 32); gid_bit++) {
        poly_group_id <<= 1; /* will 'overflow' on last possible iteration. */
      }
      if (UNLIKELY(gid_bit > 31)) {
        /* All bits used in contiguous smooth groups, we can't do much!
         * Note: this is *very* unlikely - theoretically, four groups are enough,
         *       I don't think we can reach this goal with such a simple algo,
         *       but I don't think either we'll never need all 32 groups!
         */
        printf(
            "Warning, could not find an available id for current smooth group, faces will me "
            "marked "
            "as out of any smooth group...\n");

        /* Can't use 0, will have to set them to this value later. */
        poly_group_id = poly_group_id_overflowed;

        group_id_overflow = true;
      }
      if (gid_bit > tot_group) {
        tot_group = gid_bit;
      }
      /* And assign the final smooth group id to that poly group! */
      for (i = ps_end_idx, p = poly_stack; i--; p++) {
        poly_groups[*p] = poly_group_id;
      }
    }
  }

  if (use_bitflags) {
    /* used bits are zero-based. */
    tot_group++;
  }

  if (UNLIKELY(group_id_overflow)) {
    int i = totpoly, *gid = poly_groups;
    for (; i--; gid++) {
      if (*gid == poly_group_id_overflowed) {
        *gid = 0;
      }
    }
    /* Using 0 as group id adds one more group! */
    tot_group++;
  }

  if (edge_poly_mem) {
    MEM_freeN(edge_poly_map);
    MEM_freeN(edge_poly_mem);
  }
  MEM_freeN(poly_stack);

  *r_totgroup = tot_group;
  *r_poly_groups = poly_groups;
  if (r_edge_borders) {
    *r_edge_borders = edge_borders;
    *r_totedgeborder = num_edgeborders;
  }
}

static bool poly_is_island_boundary_smooth_cb(const MPoly *mp,
                                              const MLoop *UNUSED(ml),
                                              const MEdge *me,
                                              const int nbr_egde_users,
                                              const MPoly *mpoly_array,
                                              const MeshElemMap *edge_poly_map,
                                              void *UNUSED(user_data))
{
  /* Edge is sharp if one of its polys is flat, or edge itself is sharp,
   * or edge is not used by exactly two polygons. */
  if ((mp->flag & ME_SMOOTH) && !(me->flag & ME_SHARP) && (nbr_egde_users == 2)) {
    /* In that case, edge appears to be smooth, but we need to check its other poly too. */
    const MPoly *mp_other = (mp == &mpoly_array[edge_poly_map->indices[0]]) ?
                                &mpoly_array[edge_poly_map->indices[1]] :
                                &mpoly_array[edge_poly_map->indices[0]];
    return (mp_other->flag & ME_SMOOTH) == 0;
  }
  return true;
}

/**
 * Calculate smooth groups from sharp edges.
 *
 * \param r_totgroup: The total number of groups, 1 or more.
 * \return Polygon aligned array of group index values (bitflags if use_bitflags is true),
 * starting at 1 (0 being used as 'invalid' flag).
 * Note it's callers's responsibility to MEM_freeN returned array.
 */
int *BKE_mesh_calc_smoothgroups(const MEdge *medge,
                                const int totedge,
                                const MPoly *mpoly,
                                const int totpoly,
                                const MLoop *mloop,
                                const int totloop,
                                int *r_totgroup,
                                const bool use_bitflags)
{
  int *poly_groups = NULL;

  poly_edge_loop_islands_calc(medge,
                              totedge,
                              mpoly,
                              totpoly,
                              mloop,
                              totloop,
                              NULL,
                              use_bitflags,
                              poly_is_island_boundary_smooth_cb,
                              NULL,
                              &poly_groups,
                              r_totgroup,
                              NULL,
                              NULL);

  return poly_groups;
}

#define MISLAND_DEFAULT_BUFSIZE 64

void BKE_mesh_loop_islands_init(MeshIslandStore *island_store,
                                const short item_type,
                                const int items_num,
                                const short island_type,
                                const short innercut_type)
{
  MemArena *mem = island_store->mem;

  if (mem == NULL) {
    mem = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, __func__);
    island_store->mem = mem;
  }
  /* else memarena should be cleared */

  BLI_assert(
      ELEM(item_type, MISLAND_TYPE_VERT, MISLAND_TYPE_EDGE, MISLAND_TYPE_POLY, MISLAND_TYPE_LOOP));
  BLI_assert(ELEM(
      island_type, MISLAND_TYPE_VERT, MISLAND_TYPE_EDGE, MISLAND_TYPE_POLY, MISLAND_TYPE_LOOP));

  island_store->item_type = item_type;
  island_store->items_to_islands_num = items_num;
  island_store->items_to_islands = BLI_memarena_alloc(
      mem, sizeof(*island_store->items_to_islands) * (size_t)items_num);

  island_store->island_type = island_type;
  island_store->islands_num_alloc = MISLAND_DEFAULT_BUFSIZE;
  island_store->islands = BLI_memarena_alloc(
      mem, sizeof(*island_store->islands) * island_store->islands_num_alloc);

  island_store->innercut_type = innercut_type;
  island_store->innercuts = BLI_memarena_alloc(
      mem, sizeof(*island_store->innercuts) * island_store->islands_num_alloc);
}

void BKE_mesh_loop_islands_clear(MeshIslandStore *island_store)
{
  island_store->item_type = MISLAND_TYPE_NONE;
  island_store->items_to_islands_num = 0;
  island_store->items_to_islands = NULL;

  island_store->island_type = MISLAND_TYPE_NONE;
  island_store->islands_num = 0;
  island_store->islands = NULL;

  island_store->innercut_type = MISLAND_TYPE_NONE;
  island_store->innercuts = NULL;

  if (island_store->mem) {
    BLI_memarena_clear(island_store->mem);
  }

  island_store->islands_num_alloc = 0;
}

void BKE_mesh_loop_islands_free(MeshIslandStore *island_store)
{
  if (island_store->mem) {
    BLI_memarena_free(island_store->mem);
    island_store->mem = NULL;
  }
}

void BKE_mesh_loop_islands_add(MeshIslandStore *island_store,
                               const int item_num,
                               const int *items_indices,
                               const int num_island_items,
                               int *island_item_indices,
                               const int num_innercut_items,
                               int *innercut_item_indices)
{
  MemArena *mem = island_store->mem;

  MeshElemMap *isld, *innrcut;
  const int curr_island_idx = island_store->islands_num++;
  const size_t curr_num_islands = (size_t)island_store->islands_num;
  int i = item_num;

  while (i--) {
    island_store->items_to_islands[items_indices[i]] = curr_island_idx;
  }

  if (UNLIKELY(curr_num_islands > island_store->islands_num_alloc)) {
    MeshElemMap **islds, **innrcuts;

    island_store->islands_num_alloc *= 2;
    islds = BLI_memarena_alloc(mem, sizeof(*islds) * island_store->islands_num_alloc);
    memcpy(islds, island_store->islands, sizeof(*islds) * (curr_num_islands - 1));
    island_store->islands = islds;

    innrcuts = BLI_memarena_alloc(mem, sizeof(*innrcuts) * island_store->islands_num_alloc);
    memcpy(innrcuts, island_store->innercuts, sizeof(*innrcuts) * (curr_num_islands - 1));
    island_store->innercuts = innrcuts;
  }

  island_store->islands[curr_island_idx] = isld = BLI_memarena_alloc(mem, sizeof(*isld));
  isld->count = num_island_items;
  isld->indices = BLI_memarena_alloc(mem, sizeof(*isld->indices) * (size_t)num_island_items);
  memcpy(isld->indices, island_item_indices, sizeof(*isld->indices) * (size_t)num_island_items);

  island_store->innercuts[curr_island_idx] = innrcut = BLI_memarena_alloc(mem, sizeof(*innrcut));
  innrcut->count = num_innercut_items;
  innrcut->indices = BLI_memarena_alloc(mem,
                                        sizeof(*innrcut->indices) * (size_t)num_innercut_items);
  memcpy(innrcut->indices,
         innercut_item_indices,
         sizeof(*innrcut->indices) * (size_t)num_innercut_items);
}

/* TODO: I'm not sure edge seam flag is enough to define UV islands?
 *       Maybe we should also consider UVmaps values
 *       themselves (i.e. different UV-edges for a same mesh-edge => boundary edge too?).
 *       Would make things much more complex though,
 *       and each UVMap would then need its own mesh mapping, not sure we want that at all!
 */
typedef struct MeshCheckIslandBoundaryUv {
  const MLoop *loops;
  const MLoopUV *luvs;
  const MeshElemMap *edge_loop_map;
} MeshCheckIslandBoundaryUv;

static bool mesh_check_island_boundary_uv(const MPoly *UNUSED(mp),
                                          const MLoop *ml,
                                          const MEdge *me,
                                          const int UNUSED(nbr_egde_users),
                                          const MPoly *UNUSED(mpoly_array),
                                          const MeshElemMap *UNUSED(edge_poly_map),
                                          void *user_data)
{
  if (user_data) {
    const MeshCheckIslandBoundaryUv *data = user_data;
    const MLoop *loops = data->loops;
    const MLoopUV *luvs = data->luvs;
    const MeshElemMap *edge_to_loops = &data->edge_loop_map[ml->e];

    BLI_assert(edge_to_loops->count >= 2 && (edge_to_loops->count % 2) == 0);

    const unsigned int v1 = loops[edge_to_loops->indices[0]].v;
    const unsigned int v2 = loops[edge_to_loops->indices[1]].v;
    const float *uvco_v1 = luvs[edge_to_loops->indices[0]].uv;
    const float *uvco_v2 = luvs[edge_to_loops->indices[1]].uv;
    for (int i = 2; i < edge_to_loops->count; i += 2) {
      if (loops[edge_to_loops->indices[i]].v == v1) {
        if (!equals_v2v2(uvco_v1, luvs[edge_to_loops->indices[i]].uv) ||
            !equals_v2v2(uvco_v2, luvs[edge_to_loops->indices[i + 1]].uv)) {
          return true;
        }
      }
      else {
        BLI_assert(loops[edge_to_loops->indices[i]].v == v2);
        UNUSED_VARS_NDEBUG(v2);
        if (!equals_v2v2(uvco_v2, luvs[edge_to_loops->indices[i]].uv) ||
            !equals_v2v2(uvco_v1, luvs[edge_to_loops->indices[i + 1]].uv)) {
          return true;
        }
      }
    }
    return false;
  }
  else {
    /* Edge is UV boundary if tagged as seam. */
    return (me->flag & ME_SEAM) != 0;
  }
}

static bool mesh_calc_islands_loop_poly_uv(MVert *UNUSED(verts),
                                           const int UNUSED(totvert),
                                           MEdge *edges,
                                           const int totedge,
                                           MPoly *polys,
                                           const int totpoly,
                                           MLoop *loops,
                                           const int totloop,
                                           const MLoopUV *luvs,
                                           MeshIslandStore *r_island_store)
{
  int *poly_groups = NULL;
  int num_poly_groups;

  /* map vars */
  MeshElemMap *edge_poly_map;
  int *edge_poly_mem;

  MeshElemMap *edge_loop_map;
  int *edge_loop_mem;

  MeshCheckIslandBoundaryUv edge_boundary_check_data;

  int *poly_indices;
  int *loop_indices;
  int num_pidx, num_lidx;

  /* Those are used to detect 'inner cuts', i.e. edges that are borders,
   * and yet have two or more polys of a same group using them
   * (typical case: seam used to unwrap properly a cylinder). */
  BLI_bitmap *edge_borders = NULL;
  int num_edge_borders = 0;
  char *edge_border_count = NULL;
  int *edge_innercut_indices = NULL;
  int num_einnercuts = 0;

  int grp_idx, p_idx, pl_idx, l_idx;

  BKE_mesh_loop_islands_clear(r_island_store);
  BKE_mesh_loop_islands_init(
      r_island_store, MISLAND_TYPE_LOOP, totloop, MISLAND_TYPE_POLY, MISLAND_TYPE_EDGE);

  BKE_mesh_edge_poly_map_create(
      &edge_poly_map, &edge_poly_mem, edges, totedge, polys, totpoly, loops, totloop);

  if (luvs) {
    BKE_mesh_edge_loop_map_create(
        &edge_loop_map, &edge_loop_mem, edges, totedge, polys, totpoly, loops, totloop);
    edge_boundary_check_data.loops = loops;
    edge_boundary_check_data.luvs = luvs;
    edge_boundary_check_data.edge_loop_map = edge_loop_map;
  }

  poly_edge_loop_islands_calc(edges,
                              totedge,
                              polys,
                              totpoly,
                              loops,
                              totloop,
                              edge_poly_map,
                              false,
                              mesh_check_island_boundary_uv,
                              luvs ? &edge_boundary_check_data : NULL,
                              &poly_groups,
                              &num_poly_groups,
                              &edge_borders,
                              &num_edge_borders);

  if (!num_poly_groups) {
    /* Should never happen... */
    MEM_freeN(edge_poly_map);
    MEM_freeN(edge_poly_mem);

    if (edge_borders) {
      MEM_freeN(edge_borders);
    }
    return false;
  }

  if (num_edge_borders) {
    edge_border_count = MEM_mallocN(sizeof(*edge_border_count) * (size_t)totedge, __func__);
    edge_innercut_indices = MEM_mallocN(sizeof(*edge_innercut_indices) * (size_t)num_edge_borders,
                                        __func__);
  }

  poly_indices = MEM_mallocN(sizeof(*poly_indices) * (size_t)totpoly, __func__);
  loop_indices = MEM_mallocN(sizeof(*loop_indices) * (size_t)totloop, __func__);

  /* Note: here we ignore '0' invalid group - this should *never* happen in this case anyway? */
  for (grp_idx = 1; grp_idx <= num_poly_groups; grp_idx++) {
    num_pidx = num_lidx = 0;
    if (num_edge_borders) {
      num_einnercuts = 0;
      memset(edge_border_count, 0, sizeof(*edge_border_count) * (size_t)totedge);
    }

    for (p_idx = 0; p_idx < totpoly; p_idx++) {
      MPoly *mp;

      if (poly_groups[p_idx] != grp_idx) {
        continue;
      }

      mp = &polys[p_idx];
      poly_indices[num_pidx++] = p_idx;
      for (l_idx = mp->loopstart, pl_idx = 0; pl_idx < mp->totloop; l_idx++, pl_idx++) {
        MLoop *ml = &loops[l_idx];
        loop_indices[num_lidx++] = l_idx;
        if (num_edge_borders && BLI_BITMAP_TEST(edge_borders, ml->e) &&
            (edge_border_count[ml->e] < 2)) {
          edge_border_count[ml->e]++;
          if (edge_border_count[ml->e] == 2) {
            edge_innercut_indices[num_einnercuts++] = (int)ml->e;
          }
        }
      }
    }

    BKE_mesh_loop_islands_add(r_island_store,
                              num_lidx,
                              loop_indices,
                              num_pidx,
                              poly_indices,
                              num_einnercuts,
                              edge_innercut_indices);
  }

  MEM_freeN(edge_poly_map);
  MEM_freeN(edge_poly_mem);

  if (luvs) {
    MEM_freeN(edge_loop_map);
    MEM_freeN(edge_loop_mem);
  }

  MEM_freeN(poly_indices);
  MEM_freeN(loop_indices);
  MEM_freeN(poly_groups);

  if (edge_borders) {
    MEM_freeN(edge_borders);
  }

  if (num_edge_borders) {
    MEM_freeN(edge_border_count);
    MEM_freeN(edge_innercut_indices);
  }
  return true;
}

/**
 * Calculate 'generic' UV islands, i.e. based only on actual geometry data (edge seams),
 * not some UV layers coordinates.
 */
bool BKE_mesh_calc_islands_loop_poly_edgeseam(MVert *verts,
                                              const int totvert,
                                              MEdge *edges,
                                              const int totedge,
                                              MPoly *polys,
                                              const int totpoly,
                                              MLoop *loops,
                                              const int totloop,
                                              MeshIslandStore *r_island_store)
{
  return mesh_calc_islands_loop_poly_uv(
      verts, totvert, edges, totedge, polys, totpoly, loops, totloop, NULL, r_island_store);
}

/**
 * Calculate UV islands.
 *
 * \note If no MLoopUV layer is passed, we only consider edges tagged as seams as UV boundaries.
 * This has the advantages of simplicity, and being valid/common to all UV maps.
 * However, it means actual UV islands without matching UV seams will not be handled correctly...
 * If a valid UV layer is passed as \a luvs parameter,
 * UV coordinates are also used to detect islands boundaries.
 *
 * \note All this could be optimized...
 * Not sure it would be worth the more complex code, though,
 * those loops are supposed to be really quick to do...
 */
bool BKE_mesh_calc_islands_loop_poly_uvmap(MVert *verts,
                                           const int totvert,
                                           MEdge *edges,
                                           const int totedge,
                                           MPoly *polys,
                                           const int totpoly,
                                           MLoop *loops,
                                           const int totloop,
                                           const MLoopUV *luvs,
                                           MeshIslandStore *r_island_store)
{
  BLI_assert(luvs != NULL);
  return mesh_calc_islands_loop_poly_uv(
      verts, totvert, edges, totedge, polys, totpoly, loops, totloop, luvs, r_island_store);
}

/** \} */