TopoToolbox / libtopotoolbox

A C library for the analysis of digital elevation models.
https://topotoolbox.github.io/libtopotoolbox/
GNU General Public License v3.0
0 stars 3 forks source link

Implement gray-weighted distance transform #47

Closed wkearn closed 3 weeks ago

wkearn commented 3 weeks ago

Closes #10

This provides an implementation of a gray-weighted distance transform nearly identical in functionality to MATLAB's graydist(I,mask,'quasi-euclidean'). The difference is that the mask is given by an array with the same encoding as the output of our identifyflats function: the algorithm finds paths over components where (flats[pixel] & 1) !=0 and starting from locations where (flats[pixel] & 4) != 0. Every other pixel is skipped, and you don't need to modify the costs raster to prevent them from being visited. The priority queue from #46 is reused here.

As discussed in https://github.com/TopoToolbox/libtopotoolbox/pull/37#issuecomment-2107751658, we can return backlinks to the lower neighbor along the geodesic path. If you don't need the backlinks (e.g because you don't want to allocate the memory for them upfront), it is safe to pass a null pointer as prev. The pointer is always checked before it is accessed.

The tests are fairly basic property-based tests that check that the right costs are assigned to non-flat pixels and presills and that use the backlinks to check that the relative distance between sequential pixels along a path is as expected. I have also manually compared the output of this against that computed in MATLAB, and they are identical.

The different name (gwdt versus graydist) is meant to remind us that they are not equivalent in functionality, and I was already using the gwdt_ namespacing scheme for the gwdt_computecosts function.