Index the polygon edges when classifying big grids against it
Most of a 52k-node height-field build went to point-in-polygon and contour-distance queries that scanned every outline edge for each query: 400ms classify, 230ms choosing points, 110ms filtering centroids. On CI the same code ran about 1.7x slower than locally, so a unit test with a 1000ms budget failed there (1338ms) while passing locally (797ms). Two exact indexes fixed it. Bucket edges by y-band for the even-odd crossing test, keeping the same half-open y rule and strict x comparison. Bucket segments by bounding box into cells and query the 3x3 neighbourhood when the distance is at most the cell size. Build time dropped about 10x. Keep the full-scan functions as a reference and test that the index gives byte-identical answers across the whole grid, on exact vertices, and on horizontal crossings.
When every query point lies on a regular grid, a scanline pass can be cheaper still. Compute the sorted x crossings of the outline once per row and classify the whole row by walking them, which avoids a per-point edge lookup. Prefer it only when the queries are grid-aligned.