static H3Error createSortableLoop()

in src/h3lib/lib/cellsToMultiPoly.c [324:380]


static H3Error createSortableLoop(Arc *arc, SortableLoop *sloop) {
    CellBoundary gb;
    H3Index start = arc->id;

    int64_t numVerts;
    LatLng *verts;

    numVerts = 0;
    do {
        // This is an overestimate for numVerts.
        // Most cell edges will just need one vert (we don't use the last vertex
        // in the edge).
        // For even resolutions, all cell edges need just one vert.
        // Over-allocate for now; realloc to actual number later.
        numVerts += 2;
        arc = arc->next;
    } while (arc->id != start);

    verts = H3_MEMORY(malloc)(sizeof(LatLng) * numVerts);
    if (!verts) {
        return E_MEMORY_ALLOC;
    }

    numVerts = 0;
    int64_t j = 0;
    do {
        H3Error err = H3_EXPORT(directedEdgeToBoundary)(arc->id, &gb);
        if (NEVER(err)) {
            free(verts);
            return err;
        }

        for (int64_t i = 0; i < gb.numVerts - 1; i++) {
            verts[j] = gb.verts[i];
            j++;
        }
        numVerts += gb.numVerts - 1;
        arc->isVisited = true;
        arc = arc->next;
    } while (arc->id != start);

    // This memory ends up in GeoMultiPolygon, to be freed by caller of
    // cellsToMultiPolygon()
    LatLng *reallocVerts = H3_MEMORY(realloc)(verts, sizeof(LatLng) * numVerts);
    if (!reallocVerts) {
        H3_MEMORY(free)(verts);
        return E_MEMORY_ALLOC;
    }
    verts = reallocVerts;

    sloop->root = getRoot(arc)->id;
    sloop->loop.numVerts = numVerts;
    sloop->loop.verts = verts;
    geoLoopAreaRads2(sloop->loop, &sloop->area);

    return E_SUCCESS;
}