571 const Span<std::unique_ptr<UVAABBIsland>> islands,
572 const float target_aspect_y,
577 float next_u1 = exclude.
xmax;
578 float next_v1 = exclude.
ymax;
579 bool zigzag = next_u1 / target_aspect_y < next_v1;
583 float2 hole_diagonal(0.0f);
584 bool hole_rotate =
false;
586 float u0 = zigzag ? next_u1 : 0.0f;
587 float v0 = zigzag ? 0.0f : next_v1;
590 for (
int64_t index = exclude_index; index < islands.size(); index++) {
594 float min_dsm = std::min(uvdiag_x, island.
uv_diagonal.y);
595 float max_dsm = std::max(uvdiag_x, island.
uv_diagonal.y);
597 if (min_dsm < hole_diagonal.x && max_dsm < hole_diagonal.y) {
599 if (hole_rotate == (min_dsm == island.
uv_diagonal.x)) {
606 phi.translation.x = hole[0] + island.
uv_diagonal.x * 0.5f;
607 phi.translation.y = hole[1] + island.
uv_diagonal.y * 0.5f;
614 p[2] = hole[0] + (hole_rotate ? max_dsm : min_dsm) / island.
aspect_y;
615 p[3] = hole[1] + (hole_rotate ? min_dsm : max_dsm);
616 p[4] = hole[0] + (hole_rotate ? hole_diagonal.y : hole_diagonal.x);
617 p[5] = hole[1] + (hole_rotate ? hole_diagonal.x : hole_diagonal.y);
626 bool restart =
false;
628 restart = (next_v1 < v0 + min_dsm);
631 restart = (next_u1 < u0 + min_dsm / island.
aspect_y);
636 zigzag = next_u1 / target_aspect_y < next_v1;
637 u0 = zigzag ? next_u1 : 0.0f;
638 v0 = zigzag ? 0.0f : next_v1;
642 if (zigzag == (min_dsm == uvdiag_x)) {
649 phi.translation.x = u0 + island.
uv_diagonal.x * 0.5f;
650 phi.translation.y = v0 + island.
uv_diagonal.y * 0.5f;
658 next_v1 =
max_ff(next_v1, v0);
663 next_v1 =
max_ff(next_v1, v0 + max_dsm);
664 next_u1 =
max_ff(next_u1, u0);
669 *r_extent = {0.0f, next_u1, 0.0f, next_v1};
766 if (
params.target_aspect_y != 1.0f) {
773 float large_uv = 0.0f;
774 for (
const int64_t i : aabbs.index_range()) {
775 large_uv =
max_ff(large_uv, aabbs[i]->uv_diagonal.x);
776 large_uv =
max_ff(large_uv, aabbs[i]->uv_diagonal.y);
779 int64_t island_count_patch = aabbs.size();
781 const float opt_11[] = {
783 2.6238700165660708840676f,
784 2.4365065643739085565755f,
785 0.70130710554829878145f,
786 1.9596047386700836678841f,
787 1.6885655318806973568257f,
788 0.70130710554829878145f,
789 1.9364970731945949644626f,
790 3.1724566890997589752033f,
791 0.70130710554829878145f,
792 1.2722458068219282267819f,
793 2.4245322476118422727609f,
794 0.70130710554829878145f,
795 3.1724918301381124230431f,
796 1.536261617698265524723f,
797 0.70130710554829878145f,
798 3.3770999999999999907629f,
799 3.3770999999999999907629f,
804 2.5325444557069398676674f,
808 3.3770999999999999907629f,
818 3.8770999999999999907629f,
820 "Walter Trump, 1979",
828 const float opt_18[] = {
830 2.4700161985907582717914f,
831 2.4335783708246112588824f,
832 0.42403103949074028022892f,
833 1.3528594569415370862941f,
834 2.3892972847076845432923f,
835 0.42403103949074028022892f,
836 2.0585783708246108147932f,
837 1.5221405430584633577951f,
838 0.42403103949074028022892f,
839 1.7642972847076845432923f,
840 3.3007351124738324443797f,
841 0.42403103949074028022892f,
842 3.3228756555322949139963f,
845 3.3228756555322949139963f,
846 3.3228756555322949139963f,
851 2.3228756555322949139963f,
852 4.3228756555322949139963f,
855 3.3228756555322949139963f,
860 3.3228756555322949139963f,
863 3.3228756555322949139963f,
864 4.3228756555322949139963f,
866 4.3228756555322949139963f,
869 4.3228756555322949139963f,
870 3.3228756555322949139963f,
876 4.3228756555322949139963f,
878 4.3228756555322949139963f,
881 4.3228756555322949139963f,
882 4.3228756555322949139963f,
886 4.8228756555322949139963f,
888 "Pertti Hamalainen, 1979",
896 const float opt_19[] = {
898 2.1785113019775792508881f,
899 1.9428090415820631342569f,
900 0.78539816339744827899949f,
901 1.4714045207910317891731f,
902 2.6499158227686105959719f,
903 0.78539816339744827899949f,
904 2.9428090415820640224354f,
905 2.7071067811865479058042f,
906 0.78539816339744827899949f,
907 2.2357022603955165607204f,
908 3.4142135623730953675192f,
909 0.78539816339744827899949f,
910 1.4428090415820635783462f,
911 1.2642977396044836613243f,
912 0.78539816339744827899949f,
913 3.3856180831641271566923f,
916 0.73570226039551600560884f,
917 1.9714045207910311230393f,
918 0.78539816339744827899949f,
919 3.6213203435596432733234f,
920 3.4428090415820635783462f,
921 0.78539816339744827899949f,
922 2.9142135623730958116084f,
923 4.1499158227686105959719f,
924 0.78539816339744827899949f,
925 2.3856180831641271566923f,
929 3.3856180831641271566923f,
932 4.3856180831641271566923f,
934 4.3856180831641271566923f,
937 3.3856180831641271566923f,
940 4.3856180831641271566923f,
947 4.3856180831641271566923f,
949 4.3856180831641271566923f,
952 4.3856180831641271566923f,
953 4.3856180831641271566923f,
957 4.8856180831641271566923f,
959 "Robert Wainwright, 1979",
967 const float opt_26[] = {
969 2.3106601717798209705279f,
970 2.8106601717798214146171f,
971 0.78539816339744827899949f,
972 1.6035533905932735088129f,
973 2.1035533905932739529021f,
974 0.78539816339744827899949f,
975 3.0177669529663684322429f,
976 2.1035533905932739529021f,
977 0.78539816339744827899949f,
978 2.3106601717798209705279f,
979 1.3964466094067264911871f,
980 0.78539816339744827899949f,
981 1.6035533905932735088129f,
982 3.5177669529663688763321f,
983 0.78539816339744827899949f,
984 0.89644660940672593607559f,
985 2.8106601717798214146171f,
986 0.78539816339744827899949f,
987 3.0177669529663684322429f,
988 3.5177669529663688763321f,
989 0.78539816339744827899949f,
990 3.7248737341529158939579f,
991 2.8106601717798214146171f,
992 0.78539816339744827899949f,
993 2.3106601717798209705279f,
994 4.2248737341529167821363f,
995 0.78539816339744827899949f,
1002 3.1213203435596419410558f,
1005 4.1213203435596419410558f,
1009 4.1213203435596419410558f,
1014 4.1213203435596419410558f,
1015 4.1213203435596419410558f,
1017 4.1213203435596419410558f,
1021 5.1213203435596419410558f,
1023 3.1213203435596419410558f,
1024 5.1213203435596419410558f,
1026 5.1213203435596419410558f,
1029 5.1213203435596419410558f,
1033 5.1213203435596419410558f,
1035 4.1213203435596419410558f,
1036 5.1213203435596419410558f,
1038 5.1213203435596419410558f,
1039 4.1213203435596419410558f,
1041 5.1213203435596419410558f,
1044 5.1213203435596419410558f,
1045 5.1213203435596419410558f,
1049 5.6213203435596419410558f,
1051 "Erich Friedman, 1997",
1059 if (island_count_patch == 37) {
1060 island_count_patch = 38;
1062 if (island_count_patch == 50) {
1063 island_count_patch = 52;
1065 if (island_count_patch == 51) {
1066 island_count_patch = 52;
1068 if (island_count_patch == 65) {
1069 island_count_patch = 67;
1071 if (island_count_patch == 66) {
1072 island_count_patch = 67;
1076 for (
int a = 1; a < 20; a++) {
1077 int n = a * a + a + 3 +
floorf((a - 1) *
sqrtf(2.0f));
1078 if (island_count_patch == n) {
1079 float max_uv_gobel = large_uv * (a + 1 +
sqrtf(0.5f));
1080 rctf extent = {0.0f, max_uv_gobel, 0.0f, max_uv_gobel};
1835 for (
const int64_t i : islands.index_range()) {
1837 float island_scale = scale;
1839 island_scale = 1.0f;
1841 std::unique_ptr<UVAABBIsland> aabb = std::make_unique<UVAABBIsland>();
1843 aabb->uv_diagonal.x = pack_island->
half_diagonal_.x * 2 * island_scale + 2 * margin;
1844 aabb->uv_diagonal.y = pack_island->
half_diagonal_.y * 2 * island_scale + 2 * margin;
1845 aabb->aspect_y = pack_island->
aspect_y;
1846 aabbs[i] = std::move(aabb);
1851 if (all_can_rotate) {
1855 [&](
const std::unique_ptr<UVAABBIsland> &a,
const std::unique_ptr<UVAABBIsland> &
b) {
1856 const bool can_translate_a = islands[a->index]->can_translate_(params);
1857 const bool can_translate_b = islands[b->index]->can_translate_(params);
1858 if (can_translate_a != can_translate_b) {
1859 return can_translate_b;
1864 float a_u = a->uv_diagonal.x * a->aspect_y;
1865 float a_v = a->uv_diagonal.y;
1866 float b_u =
b->uv_diagonal.x *
b->aspect_y;
1867 float b_v =
b->uv_diagonal.y;
1869 std::swap(a_u, a_v);
1872 std::swap(b_u, b_v);
1874 float diff_u = a_u - b_u;
1875 float diff_v = a_v - b_v;
1876 diff_v += diff_u * 0.05f;
1877 if (diff_v == 0.0f) {
1878 return diff_u > 0.0f;
1880 return diff_v > 0.0f;
1887 [&](
const std::unique_ptr<UVAABBIsland> &a,
const std::unique_ptr<UVAABBIsland> &
b) {
1888 const bool can_translate_a = islands[a->index]->can_translate_(params);
1889 const bool can_translate_b = islands[b->index]->can_translate_(params);
1890 if (can_translate_a != can_translate_b) {
1891 return can_translate_b;
1895 return b->uv_diagonal.x *
b->uv_diagonal.y < a->uv_diagonal.x * a->uv_diagonal.y;
1900 rctf locked_bounds = {0.0f};
1901 int64_t locked_island_count = 0;
1902 for (
int64_t i = 0; i < islands.size(); i++) {
1903 PackIsland *pack_island = islands[aabbs[i]->index];
1904 if (pack_island->can_translate_(
params)) {
1907 float2 bottom_left = pack_island->pivot_ - pack_island->half_diagonal_;
1908 float2 top_right = pack_island->pivot_ + pack_island->half_diagonal_;
1910 locked_bounds.
xmin = bottom_left.
x;
1911 locked_bounds.
xmax = top_right.
x;
1912 locked_bounds.
ymin = bottom_left.
y;
1913 locked_bounds.
ymax = top_right.
y;
1920 UVPhi &
phi = r_phis[aabbs[i]->index];
1921 phi.translation = pack_island->pivot_;
1923 phi.rotation = 0.0f;
1925 locked_island_count = i + 1;
1931 int64_t alpaca_cutoff_fast = 81;
1933 if (margin > 0.0f) {
1934 alpaca_cutoff = alpaca_cutoff_fast;
1938 alpaca_cutoff = std::max(alpaca_cutoff, locked_island_count);
1940 Span<std::unique_ptr<UVAABBIsland>> slow_aabbs = aabbs.as_span().take_front(
1941 std::min(alpaca_cutoff, islands.size()));
1942 rctf extent = {0.0f, 1e30f, 0.0f, 1e30f};
1952 rctf fast_extent = extent;
1955 if (locked_island_count == 0) {
1960 if (locked_island_count == 0) {
1966 slow_aabbs, islands, scale, margin,
params, r_phis, &extent);
1968 slow_aabbs = aabbs.as_span().take_front(max_xatlas);
1976 if (all_can_rotate && aabbs.size() > slow_aabbs.size()) {
1982 slow_aabbs = slow_aabbs.take_front(locked_island_count);
1983 extent = locked_bounds;
1987 rctf final_extent = {0.0f, 1e30f, 0.0f, 1e30f};