558 const Span<std::unique_ptr<UVAABBIsland>> islands,
559 const float target_aspect_y,
564 float next_u1 = exclude.
xmax;
565 float next_v1 = exclude.
ymax;
566 bool zigzag = next_u1 / target_aspect_y < next_v1;
570 float2 hole_diagonal(0.0f);
571 bool hole_rotate =
false;
573 float u0 = zigzag ? next_u1 : 0.0f;
574 float v0 = zigzag ? 0.0f : next_v1;
577 for (
int64_t index = exclude_index; index < islands.size(); index++) {
581 float min_dsm = std::min(uvdiag_x, island.
uv_diagonal.y);
582 float max_dsm = std::max(uvdiag_x, island.
uv_diagonal.y);
584 if (min_dsm < hole_diagonal.x && max_dsm < hole_diagonal.y) {
586 if (hole_rotate == (min_dsm == island.
uv_diagonal.x)) {
593 phi.translation.x = hole[0] + island.
uv_diagonal.x * 0.5f;
594 phi.translation.y = hole[1] + island.
uv_diagonal.y * 0.5f;
601 p[2] = hole[0] + (hole_rotate ? max_dsm : min_dsm) / island.
aspect_y;
602 p[3] = hole[1] + (hole_rotate ? min_dsm : max_dsm);
603 p[4] = hole[0] + (hole_rotate ? hole_diagonal.y : hole_diagonal.x);
604 p[5] = hole[1] + (hole_rotate ? hole_diagonal.x : hole_diagonal.y);
613 bool restart =
false;
615 restart = (next_v1 < v0 + min_dsm);
618 restart = (next_u1 < u0 + min_dsm / island.
aspect_y);
623 zigzag = next_u1 / target_aspect_y < next_v1;
624 u0 = zigzag ? next_u1 : 0.0f;
625 v0 = zigzag ? 0.0f : next_v1;
629 if (zigzag == (min_dsm == uvdiag_x)) {
636 phi.translation.x = u0 + island.
uv_diagonal.x * 0.5f;
637 phi.translation.y = v0 + island.
uv_diagonal.y * 0.5f;
645 next_v1 =
max_ff(next_v1, v0);
650 next_v1 =
max_ff(next_v1, v0 + max_dsm);
651 next_u1 =
max_ff(next_u1, u0);
656 *r_extent = {0.0f, next_u1, 0.0f, next_v1};
753 if (
params.target_aspect_y != 1.0f) {
760 float large_uv = 0.0f;
761 for (
const int64_t i : aabbs.index_range()) {
762 large_uv =
max_ff(large_uv, aabbs[
i]->uv_diagonal.x);
763 large_uv =
max_ff(large_uv, aabbs[
i]->uv_diagonal.y);
766 int64_t island_count_patch = aabbs.size();
768 const float opt_11[] = {
770 2.6238700165660708840676f,
771 2.4365065643739085565755f,
772 0.70130710554829878145f,
773 1.9596047386700836678841f,
774 1.6885655318806973568257f,
775 0.70130710554829878145f,
776 1.9364970731945949644626f,
777 3.1724566890997589752033f,
778 0.70130710554829878145f,
779 1.2722458068219282267819f,
780 2.4245322476118422727609f,
781 0.70130710554829878145f,
782 3.1724918301381124230431f,
783 1.536261617698265524723f,
784 0.70130710554829878145f,
785 3.3770999999999999907629f,
786 3.3770999999999999907629f,
791 2.5325444557069398676674f,
795 3.3770999999999999907629f,
805 3.8770999999999999907629f,
807 "Walter Trump, 1979",
815 const float opt_18[] = {
817 2.4700161985907582717914f,
818 2.4335783708246112588824f,
819 0.42403103949074028022892f,
820 1.3528594569415370862941f,
821 2.3892972847076845432923f,
822 0.42403103949074028022892f,
823 2.0585783708246108147932f,
824 1.5221405430584633577951f,
825 0.42403103949074028022892f,
826 1.7642972847076845432923f,
827 3.3007351124738324443797f,
828 0.42403103949074028022892f,
829 3.3228756555322949139963f,
832 3.3228756555322949139963f,
833 3.3228756555322949139963f,
838 2.3228756555322949139963f,
839 4.3228756555322949139963f,
842 3.3228756555322949139963f,
847 3.3228756555322949139963f,
850 3.3228756555322949139963f,
851 4.3228756555322949139963f,
853 4.3228756555322949139963f,
856 4.3228756555322949139963f,
857 3.3228756555322949139963f,
863 4.3228756555322949139963f,
865 4.3228756555322949139963f,
868 4.3228756555322949139963f,
869 4.3228756555322949139963f,
873 4.8228756555322949139963f,
875 "Pertti Hamalainen, 1979",
883 const float opt_19[] = {
885 2.1785113019775792508881f,
886 1.9428090415820631342569f,
887 0.78539816339744827899949f,
888 1.4714045207910317891731f,
889 2.6499158227686105959719f,
890 0.78539816339744827899949f,
891 2.9428090415820640224354f,
892 2.7071067811865479058042f,
893 0.78539816339744827899949f,
894 2.2357022603955165607204f,
895 3.4142135623730953675192f,
896 0.78539816339744827899949f,
897 1.4428090415820635783462f,
898 1.2642977396044836613243f,
899 0.78539816339744827899949f,
900 3.3856180831641271566923f,
903 0.73570226039551600560884f,
904 1.9714045207910311230393f,
905 0.78539816339744827899949f,
906 3.6213203435596432733234f,
907 3.4428090415820635783462f,
908 0.78539816339744827899949f,
909 2.9142135623730958116084f,
910 4.1499158227686105959719f,
911 0.78539816339744827899949f,
912 2.3856180831641271566923f,
916 3.3856180831641271566923f,
919 4.3856180831641271566923f,
921 4.3856180831641271566923f,
924 3.3856180831641271566923f,
927 4.3856180831641271566923f,
934 4.3856180831641271566923f,
936 4.3856180831641271566923f,
939 4.3856180831641271566923f,
940 4.3856180831641271566923f,
944 4.8856180831641271566923f,
946 "Robert Wainwright, 1979",
954 const float opt_26[] = {
956 2.3106601717798209705279f,
957 2.8106601717798214146171f,
958 0.78539816339744827899949f,
959 1.6035533905932735088129f,
960 2.1035533905932739529021f,
961 0.78539816339744827899949f,
962 3.0177669529663684322429f,
963 2.1035533905932739529021f,
964 0.78539816339744827899949f,
965 2.3106601717798209705279f,
966 1.3964466094067264911871f,
967 0.78539816339744827899949f,
968 1.6035533905932735088129f,
969 3.5177669529663688763321f,
970 0.78539816339744827899949f,
971 0.89644660940672593607559f,
972 2.8106601717798214146171f,
973 0.78539816339744827899949f,
974 3.0177669529663684322429f,
975 3.5177669529663688763321f,
976 0.78539816339744827899949f,
977 3.7248737341529158939579f,
978 2.8106601717798214146171f,
979 0.78539816339744827899949f,
980 2.3106601717798209705279f,
981 4.2248737341529167821363f,
982 0.78539816339744827899949f,
989 3.1213203435596419410558f,
992 4.1213203435596419410558f,
996 4.1213203435596419410558f,
1001 4.1213203435596419410558f,
1002 4.1213203435596419410558f,
1004 4.1213203435596419410558f,
1008 5.1213203435596419410558f,
1010 3.1213203435596419410558f,
1011 5.1213203435596419410558f,
1013 5.1213203435596419410558f,
1016 5.1213203435596419410558f,
1020 5.1213203435596419410558f,
1022 4.1213203435596419410558f,
1023 5.1213203435596419410558f,
1025 5.1213203435596419410558f,
1026 4.1213203435596419410558f,
1028 5.1213203435596419410558f,
1031 5.1213203435596419410558f,
1032 5.1213203435596419410558f,
1036 5.6213203435596419410558f,
1038 "Erich Friedman, 1997",
1046 if (island_count_patch == 37) {
1047 island_count_patch = 38;
1049 if (island_count_patch == 50) {
1050 island_count_patch = 52;
1052 if (island_count_patch == 51) {
1053 island_count_patch = 52;
1055 if (island_count_patch == 65) {
1056 island_count_patch = 67;
1058 if (island_count_patch == 66) {
1059 island_count_patch = 67;
1063 for (
int a = 1; a < 20; a++) {
1064 int n = a * a + a + 3 +
floorf((a - 1) *
sqrtf(2.0f));
1065 if (island_count_patch == n) {
1066 float max_uv_gobel = large_uv * (a + 1 +
sqrtf(0.5f));
1067 rctf extent = {0.0f, max_uv_gobel, 0.0f, max_uv_gobel};
1818 float island_scale = scale;
1820 island_scale = 1.0f;
1822 std::unique_ptr<UVAABBIsland> aabb = std::make_unique<UVAABBIsland>();
1824 aabb->uv_diagonal.x = pack_island->
half_diagonal_.x * 2 * island_scale + 2 * margin;
1825 aabb->uv_diagonal.y = pack_island->
half_diagonal_.y * 2 * island_scale + 2 * margin;
1826 aabb->aspect_y = pack_island->
aspect_y;
1827 aabbs[
i] = std::move(aabb);
1832 if (all_can_rotate) {
1836 [&](
const std::unique_ptr<UVAABBIsland> &a,
const std::unique_ptr<UVAABBIsland> &
b) {
1837 const bool can_translate_a = islands[a->index]->can_translate_(params);
1838 const bool can_translate_b = islands[b->index]->can_translate_(params);
1839 if (can_translate_a != can_translate_b) {
1840 return can_translate_b;
1845 float a_u = a->uv_diagonal.x * a->aspect_y;
1846 float a_v = a->uv_diagonal.y;
1847 float b_u =
b->uv_diagonal.x *
b->aspect_y;
1848 float b_v =
b->uv_diagonal.y;
1850 std::swap(a_u, a_v);
1853 std::swap(b_u, b_v);
1855 float diff_u = a_u - b_u;
1856 float diff_v = a_v - b_v;
1857 diff_v += diff_u * 0.05f;
1858 if (diff_v == 0.0f) {
1859 return diff_u > 0.0f;
1861 return diff_v > 0.0f;
1868 [&](
const std::unique_ptr<UVAABBIsland> &a,
const std::unique_ptr<UVAABBIsland> &
b) {
1869 const bool can_translate_a = islands[a->index]->can_translate_(params);
1870 const bool can_translate_b = islands[b->index]->can_translate_(params);
1871 if (can_translate_a != can_translate_b) {
1872 return can_translate_b;
1876 return b->uv_diagonal.x *
b->uv_diagonal.y < a->uv_diagonal.x * a->uv_diagonal.y;
1881 rctf locked_bounds = {0.0f};
1882 int64_t locked_island_count = 0;
1883 for (
int64_t i = 0;
i < islands.size();
i++) {
1884 PackIsland *pack_island = islands[aabbs[
i]->index];
1885 if (pack_island->can_translate_(
params)) {
1888 float2 bottom_left = pack_island->pivot_ - pack_island->half_diagonal_;
1889 float2 top_right = pack_island->pivot_ + pack_island->half_diagonal_;
1891 locked_bounds.
xmin = bottom_left.
x;
1892 locked_bounds.
xmax = top_right.
x;
1893 locked_bounds.
ymin = bottom_left.
y;
1894 locked_bounds.
ymax = top_right.
y;
1901 UVPhi &
phi = r_phis[aabbs[
i]->index];
1902 phi.translation = pack_island->pivot_;
1904 phi.rotation = 0.0f;
1906 locked_island_count =
i + 1;
1912 int64_t alpaca_cutoff_fast = 81;
1914 if (margin > 0.0f) {
1915 alpaca_cutoff = alpaca_cutoff_fast;
1919 alpaca_cutoff = std::max(alpaca_cutoff, locked_island_count);
1922 std::min(alpaca_cutoff, islands.size()));
1923 rctf extent = {0.0f, 1e30f, 0.0f, 1e30f};
1933 rctf fast_extent = extent;
1936 if (locked_island_count == 0) {
1941 if (locked_island_count == 0) {
1947 slow_aabbs, islands, scale, margin,
params, r_phis, &extent);
1949 slow_aabbs = aabbs.as_span().
take_front(max_xatlas);
1957 if (all_can_rotate && aabbs.size() > slow_aabbs.
size()) {
1963 slow_aabbs = slow_aabbs.
take_front(locked_island_count);
1964 extent = locked_bounds;
1968 rctf final_extent = {0.0f, 1e30f, 0.0f, 1e30f};