static H3Error createGlobeMultiPolygon()

in src/h3lib/lib/cellsToMultiPoly.c [465:521]


static H3Error createGlobeMultiPolygon(GeoMultiPolygon *mpoly) {
    const int numPolygons = 8;
    const int numVerts = 3;
    const LatLng verts[8][3] = {
        {{M_PI_2, 0.0}, {0.0, 0.0}, {0.0, M_PI_2}},
        {{M_PI_2, 0.0}, {0.0, M_PI_2}, {0.0, M_PI}},
        {{M_PI_2, 0.0}, {0.0, M_PI}, {0.0, -M_PI_2}},
        {{M_PI_2, 0.0}, {0.0, -M_PI_2}, {0.0, 0.0}},
        {{-M_PI_2, 0.0}, {0.0, 0.0}, {0.0, -M_PI_2}},
        {{-M_PI_2, 0.0}, {0.0, -M_PI_2}, {0.0, -M_PI}},
        {{-M_PI_2, 0.0}, {0.0, -M_PI}, {0.0, M_PI_2}},
        {{-M_PI_2, 0.0}, {0.0, M_PI_2}, {0.0, 0.0}},
    };

    SortablePoly *spolys =
        H3_MEMORY(malloc)(sizeof(SortablePoly) * numPolygons);
    if (!spolys) {
        return E_MEMORY_ALLOC;
    }

    for (int i = 0; i < numPolygons; i++) {
        GeoPolygon *poly = &spolys[i].poly;
        poly->numHoles = 0;
        poly->holes = NULL;
        poly->geoloop.numVerts = numVerts;
        poly->geoloop.verts = H3_MEMORY(malloc)(sizeof(LatLng) * numVerts);
        if (!poly->geoloop.verts) {
            // Free any verts already allocated in previous iterations
            destroySortablePolyVerts(spolys, i);
            return E_MEMORY_ALLOC;
        }

        for (int j = 0; j < numVerts; j++) {
            poly->geoloop.verts[j] = verts[i][j];
        }

        // Calculate outer area for sorting
        geoLoopAreaRads2(poly->geoloop, &spolys[i].outerArea);
    }

    qsort(spolys, numPolygons, sizeof(SortablePoly), cmp_SortablePoly);

    mpoly->polygons = H3_MEMORY(malloc)(sizeof(GeoPolygon) * numPolygons);
    if (!mpoly->polygons) {
        destroySortablePolyVerts(spolys, numPolygons);
        return E_MEMORY_ALLOC;
    }

    mpoly->numPolygons = numPolygons;
    for (int i = 0; i < numPolygons; i++) {
        mpoly->polygons[i] = spolys[i].poly;
    }

    H3_MEMORY(free)(spolys);

    return E_SUCCESS;
}