Thin-plate spline bug

Click to enlarge
These weird hairline artefacts in my thin-plate spline had me stumped for days. I post these images as there’s almost no online chatter of people troubleshooting their thin-plate spline code, so there were no easy answers to be found online.

Landscape with hairline bug (click to enlarge)

The same landscape with the bug fixed
- These images are renders of a heightmap, and the hairline artefacts represent very slight mismatches in height.
- They don’t seem to appear anywhere the land is flat, but they seemingly only appear randomly in the places where the land is not flat.
- They seem to follow edges in the TIN mesh and often pass straight through vertices - forming lines that are longer than a single triangle edge.
The three clues above guide toward the answer. This bug was caused by the mismatch between the sampling position of each pixel in the triangle’s rasterization, vs. the actual extents of the triangle. The pixels in the rasterization are sampling positions which are rounded to the center of the pixel, while my thin-plate spline code was sampling positions starting from the true edge of the triangle.
When the land is flat, that subpixel mismatch in sampling location makes no difference to the sampled height. When land is not flat the difference depends on how close each vertex happens to be to the centrer of the pixel that covers it, and whether that misalignment runs up/down the slope rather than orthogonal to it.
It’s probably going to be easier to solve this bug than to find this page through Google, but hopefully it helps someone one day.
Tags for search engines: Piecewise, patches