Macaw.Draw.Contour (macaw_draw v0.1.0)

Copy Markdown View Source

Iso-lines through a scalar grid by marching squares (spec/09 §11), copied from visualize's Visualize.Contour and the contour step of its Visualize.Chart.Transform.Geo (visualize D-72, D-112).

iex> {:ok, c} = Macaw.Draw.Contour.new(size: {4, 4}, thresholds: [7.5])
iex> [%{value: 7.5, type: "MultiPolygon", coordinates: [[ring]]}] =
...>   Macaw.Draw.Contour.compute(c, [[0, 0, 0, 0], [0, 5, 5, 0], [0, 5, 10, 5], [0, 0, 5, 0]])
iex> ring
[[1.5, 2], [2, 1.5], [2.5, 2], [2, 2.5], [1.5, 2]]

One map per threshold, %{value, type: "MultiPolygon", coordinates}, each polygon a list holding a single ring of [x, y] in grid units (x the column, y the row, clamped to the grid). A corner is inside at or above the threshold; the grid is padded with a border of its minimum less 1000, so a region touching the edge closes along it; saddles emit two segments; a crossing never sits on a corner (D-112); a ring closes on its exact start point; and a threshold costs time linear in the cells, the grid indexed once (D-72). Holes are not told from outer rings: each ring is its own polygon.

Summary

Types

A refusal: faults with their paths.

t()

Functions

The grid layer's columns (spec/09 §11.2): one row per ring of every threshold, "value" and "path" (closed), each [x, y] stretched over a {w, h} plot from a grid of {columns, rows} as {x · w / max(columns − 1, 1), y · h / max(rows − 1, 1)}. No contours give no rows (visualize #1111, D-303).

The contours of grid at every threshold (spec/09 §11.1–§11.2). The grid is a list of rows (their count the height, the first row's length the width, overriding size) or a flat list indexed y · width + x of size's dimensions; a flat list of another length, or rows of unequal length, is {:error, [{[:grid], {:length, expected}}]}. A nil cell (or any that is no number) reads as the grid's minimum less 1; a grid with no number, or no threshold, gives no contours. A count n of thresholds is n values strictly inside the data range.

The work one compute/2 does, as counts (spec/09 §11.2, visualize #271): reads, the padded cells the marching visits, once per threshold; segments, the segments it builds; hops, the points the ring-following steps through.

A contour generator, its options checked at once (spec/09 §11.1): size ({columns, rows} of positive integers, {1, 1}), thresholds (a list of numbers, or a positive integer count, []) and smooth (true: crossings interpolated linearly; false: at edge midpoints).

One IR path per threshold, %{value, path}: M … L … per ring, and Z only when the ring's last point is its first (spec/09 §11.2).

Types

contour_result()

@type contour_result() :: %{
  value: number(),
  type: String.t(),
  coordinates: [[[[number()]]]]
}

error()

@type error() :: {:error, [Macaw.Base.Fault.t()]}

A refusal: faults with their paths.

t()

@type t() :: %Macaw.Draw.Contour{
  height: pos_integer(),
  smooth: boolean(),
  thresholds: [number()] | pos_integer(),
  width: pos_integer()
}

Functions

columns(contours, size, grid)

@spec columns(
  [contour_result()],
  {number(), number()},
  {pos_integer(), pos_integer()}
) ::
  %{required(String.t()) => [term()]} | error()

The grid layer's columns (spec/09 §11.2): one row per ring of every threshold, "value" and "path" (closed), each [x, y] stretched over a {w, h} plot from a grid of {columns, rows} as {x · w / max(columns − 1, 1), y · h / max(rows − 1, 1)}. No contours give no rows (visualize #1111, D-303).

compute(contour, grid)

@spec compute(t(), [[number() | nil]] | [number() | nil]) ::
  [contour_result()] | error()

The contours of grid at every threshold (spec/09 §11.1–§11.2). The grid is a list of rows (their count the height, the first row's length the width, overriding size) or a flat list indexed y · width + x of size's dimensions; a flat list of another length, or rows of unequal length, is {:error, [{[:grid], {:length, expected}}]}. A nil cell (or any that is no number) reads as the grid's minimum less 1; a grid with no number, or no threshold, gives no contours. A count n of thresholds is n values strictly inside the data range.

cost(contour, grid)

@spec cost(t(), [[number()]] | [number()]) ::
  %{
    reads: non_neg_integer(),
    segments: non_neg_integer(),
    hops: non_neg_integer()
  }
  | error()

The work one compute/2 does, as counts (spec/09 §11.2, visualize #271): reads, the padded cells the marching visits, once per threshold; segments, the segments it builds; hops, the points the ring-following steps through.

iex> {:ok, c} = Macaw.Draw.Contour.new(size: {4, 4}, thresholds: [3])
iex> cost = Macaw.Draw.Contour.cost(c, for(y <- 0..3, x <- 0..3, do: x + y))
iex> {cost.reads, cost.hops <= cost.segments}
{25, true}

new(opts \\ [])

@spec new(keyword() | map()) :: {:ok, t()} | error()

A contour generator, its options checked at once (spec/09 §11.1): size ({columns, rows} of positive integers, {1, 1}), thresholds (a list of numbers, or a positive integer count, []) and smooth (true: crossings interpolated linearly; false: at edge midpoints).

paths(contours)

@spec paths([contour_result()]) ::
  [%{value: number(), path: Macaw.Draw.IR.Path.t()}] | error()

One IR path per threshold, %{value, path}: M … L … per ring, and Z only when the ring's last point is its first (spec/09 §11.2).