Finally something we can use for real project! Here we analyse the grid cells, and if a grid is not homogenious enough, we split it into four small cells. And so on, until we either reach the smallest allowed size of have a grid or more or less same darkness.
The reason we use this approach rather than breaking the map into small cells is efficiency. 1000x1000 meters map produces 1,000,000 1m cells, which results in very slow calculations. While in most cases map has large areas of more or less same color (road in a forest) and we can have most of a map represented by large cells.
Note that this is not the most compact representation, but it by all means is a simple one.
Here is a route produced by a sample "main" function of the class. Note that the route is going around a mountain and not across it, just the way it should be.