Layout: golden-angle sphere seed + the shared optimizer, edges replicated * by log2(weight) so heavy couplings pull harder. Deterministic. */
| 848 | /* Layout: golden-angle sphere seed + the shared optimizer, edges replicated |
| 849 | * by log2(weight) so heavy couplings pull harder. Deterministic. */ |
| 850 | static void rg_layout(rg_build_t *b, float *out_xyz, float *out_size) { |
| 851 | int rc = b->region_count; |
| 852 | cbm_layout_body_t *bodies = |
| 853 | cbm_atlas_calloc_array(rc > 0 ? (size_t)rc : 1, sizeof(cbm_layout_body_t)); |
| 854 | if (!bodies) |
| 855 | return; |
| 856 | long long max_members = 1; |
| 857 | for (int r = 0; r < rc; r++) |
| 858 | if (b->regions[r].members > max_members) |
| 859 | max_members = b->regions[r].members; |
| 860 | const float golden = 2.39996323f; /* golden angle in radians */ |
| 861 | for (int r = 0; r < rc; r++) { |
| 862 | float t = rc > 1 ? (float)r / (float)(rc - 1) : 0.5f; |
| 863 | float inclination = acosf(1.0f - 2.0f * t); |
| 864 | float azimuth = golden * (float)r; |
| 865 | float radius = (float)REGION_SEED_RADIUS; |
| 866 | bodies[r].x = radius * sinf(inclination) * cosf(azimuth); |
| 867 | bodies[r].y = radius * sinf(inclination) * sinf(azimuth); |
| 868 | bodies[r].z = radius * cosf(inclination); |
| 869 | bodies[r].ax = bodies[r].x; |
| 870 | bodies[r].ay = bodies[r].y; |
| 871 | bodies[r].az = bodies[r].z; |
| 872 | bodies[r].mass = (float)(1.0 + sqrt((double)b->regions[r].members)); |
| 873 | } |
| 874 | /* Replicated edge list. */ |
| 875 | int ecap = 0, ne = 0; |
| 876 | int *es = NULL, *ed = NULL; |
| 877 | for (int s = 0; s < rc; s++) { |
| 878 | for (int t2 = 0; t2 < rc; t2++) { |
| 879 | long long w = b->edge_w[s * rc + t2]; |
| 880 | if (w <= 0) |
| 881 | continue; |
| 882 | int rep = 1; |
| 883 | while ((1LL << rep) < w && rep < REGION_EDGE_PULL_CAP) |
| 884 | rep++; |
| 885 | for (int k = 0; k < rep; k++) { |
| 886 | if (ne >= ecap) { |
| 887 | int nc = ecap ? ecap * 2 : 256; |
| 888 | int *ns = cbm_realloc(CBM_MEM_CLASS_OTHER, es, (size_t)nc * sizeof(int)); |
| 889 | int *nd = cbm_realloc(CBM_MEM_CLASS_OTHER, ed, (size_t)nc * sizeof(int)); |
| 890 | if (!ns || !nd) { |
| 891 | cbm_free(CBM_MEM_CLASS_OTHER, ns ? ns : es); |
| 892 | cbm_free(CBM_MEM_CLASS_OTHER, nd ? nd : ed); |
| 893 | es = NULL; |
| 894 | ed = NULL; |
| 895 | ne = 0; |
| 896 | goto layout; |
| 897 | } |
| 898 | es = ns; |
| 899 | ed = nd; |
| 900 | ecap = nc; |
| 901 | } |
| 902 | es[ne] = s; |
| 903 | ed[ne] = t2; |
| 904 | ne++; |
| 905 | } |
| 906 | } |
| 907 | } |
no test coverage detected