Skip to content

Quickstart: C

The C API is one header (isosweep.h) and one library (libisosweep). The library is C11 with no third-party dependencies; the header can be used from C99 and C++. Link with:

Terminal window
cc -std=c11 app.c -lisosweep -lm -lpthread
#include <stdio.h>
#include "isosweep.h"
enum { NROWS = 200, NCOLS = 300 };
static float z[NROWS * NCOLS]; /* row-major: z[row * NCOLS + col] */
int main(void)
{
/* ... fill z ... */
isosweep_grid_t grid;
isosweep_grid_init(&grid);
grid.z = z;
grid.dtype = ISOSWEEP_FLOAT32; /* or ISOSWEEP_FLOAT64 */
grid.nrows = NROWS;
grid.ncols = NCOLS;
isosweep_options_t opts;
isosweep_options_init(&opts); /* LINES, all cores, float32 output */
const double levels[] = { -200.0, 0.0, 200.0 };
isosweep_result_t *res = NULL;
isosweep_status_t st = isosweep_contour(&grid, levels, 3, &opts, &res);
if (st != ISOSWEEP_OK) {
fprintf(stderr, "isosweep: %s\n", isosweep_status_str(st));
return 1;
}
int64_t n = isosweep_result_ncurves(res);
for (int64_t i = 0; i < n; i++) {
isosweep_curve_t c;
isosweep_result_curve(res, i, &c);
/* c.xf, c.yf : c.npoints ordered vertices (column, row units)
c.level : index into levels[]
c.closed : 1 = closed ring, 0 = ends at the grid edge or NoData
c.hill : closed and {z >= level} lies inside */
}
isosweep_result_free(res);
return 0;
}

Set opts.output = ISOSWEEP_BANDS for the regions between consecutive levels (including below the first and above the last) as polygons with holes, or ISOSWEEP_POLYGONS for the regions z ≥ level. The polygons come as flat arrays owned by the result:

opts.output = ISOSWEEP_BANDS;
isosweep_contour(&grid, levels, 3, &opts, &res);
isosweep_polygons_t p;
isosweep_result_polygons(res, &p);
for (int64_t k = 0; k < p.npolygons; k++) {
int band = p.group[k]; /* 0: below levels[0] ... 3: above levels[2] */
int64_t exterior = p.ring0[k]; /* rings ring0[k] .. ring0[k+1]-1; the first is the exterior */
int64_t v0 = p.vertex0[exterior], v1 = p.vertex0[exterior + 1];
/* p.xf[v0..v1), p.yf[v0..v1): the exterior ring (counter-clockwise);
the following rings are holes (clockwise) */
}

Every polygon is OGC-valid, including where a band meets the grid edge or a NoData void.

  • Outputs: ISOSWEEP_LINES (default; contour lines open at the grid edge), ISOSWEEP_RINGS (closed rings with the hill flag and the nesting tree parent / owner), ISOSWEEP_POLYGONS, ISOSWEEP_BANDS.
  • Coordinates: grid indices by default; grid.coords selects a rectilinear grid (x[ncols], y[nrows]), an affine transform (a GDAL geotransform, rotated rasters included) or a curvilinear grid. opts.coord_dtype = ISOSWEEP_FLOAT64 keeps world coordinates exact.
  • NoData: NaN, ±inf and |z| ≥ 1e19 are voids, plus grid.nodata_value and grid.mask when given. Lines end half-way to a void; rings and polygons run along it.
  • Threads: opts.threads (0 = all cores). The geometry is the same for any thread count. opts.runtime plugs in your own allocator and thread pool.
  • Grids larger than memory: isosweep_contour_stream() pulls rows from a callback and pushes each curve to another as soon as it is complete; isosweep_stream_new/feed/finish is the same engine driven by hand.
  • Errors are values: every call returns a status (isosweep_status_str() names it); isosweep_result_info() reports the engine used, threads, timings and memory.

Next: what the topology metadata means.