Skip to content

Hill shading improvements + new algorithm #2 - #1521

Merged
devemux86 merged 1 commit into
mapsforge:masterfrom
Sublimis:Sublimis-hillshading-1
Aug 21, 2024
Merged

devemux86 merged 1 commit into
mapsforge:masterfrom
Sublimis:Sublimis-hillshading-1

Conversation

@Sublimis

Copy link
Copy Markdown
Contributor
  • Enable sub-classing of hill shading algorithms, so users of the library can create theirs

  • Get rid of the memory mapping of DEM files, use a simple and more efficient BufferedInputStream instead (InputStream in general, instead of the ByteBuffer)

  • Create a new hill shading algorithm with better appearance and more customization

- Enable sub-classing of hill shading algorithms, so users of the library can create theirs

- Get rid of the memory mapping of DEM files, use a simple and more efficient `BufferedInputStream` instead (`InputStream `in general, instead of the `ByteBuffer`)

- Create a new hill shading algorithm with better appearance and more customization
@devemux86

devemux86 commented Aug 20, 2024 •

Copy link
Copy Markdown
Collaborator

@Sublimis thanks!

  • No shade seems to work only on Android. Desktop still shows shades (see desktop image).

  • Is there an easy way for the other algorithms to not show a shade when there is no slope?

Desktop

@Sublimis

Copy link
Copy Markdown
Contributor Author

This doesn't seem like an effect of the new algorithm (ClearAsymmetryShadingAlgorithm), but something else. Not sure, though, will check and get back to you. Can you compare slightly more zoomed in screenshots with those of other algorithms? If the effect is visible only over the sea, this should be indicative.
Can it be caching effect, i.e. outside areas (those darker) are cached tiles made with other algorithm which shaded them?

Unfortunately there's no easy way for other algorithms (Simple and Diffuse), as they split the shading range in the middle, and use lower half (less shading) for one direction, and upper half (more shading) for other direction. Flat areas being in the middle always get 50% shading. When this is fixed, i.e. we put flat areas at zero shading, and distribute the shading range asymmetrically over two slope directions, we basically arrive at the new algorithm presented (ClearAsymmetryShadingAlgorithm).

@devemux86

Copy link
Copy Markdown
Collaborator

@Sublimis thanks for the extra info!

I make the tests without permanent cache, so it is always new rendered tiles.

The sea has different colors with all algorithms on desktop.
The other 2 algorithms have a dark shade, the new algorithm has a light shade.
Maybe there is something on desktop Java color rendering.

@devemux86

Copy link
Copy Markdown
Collaborator

No shade seems to work only on Android. Desktop still shows shades (see desktop image).

This seems to be related to AwtCanvas.selectHillShadingComposite,
using AlphaComposite (current implementation) or AwtLuminanceShadingComposite (no sea shades).

@Sublimis

Copy link
Copy Markdown
Contributor Author

The solution seems to be very simple. Take a look at line 49 of the AwtCanvas.java:

private static final java.awt.Color NEUTRAL_HILLS = new java.awt.Color(127, 127, 127);

This is 50% gray shade.

Instead, it should be:

private static final java.awt.Color NEUTRAL_HILLS = new java.awt.Color(0, 0, 0, 0);

(last extra zero is alpha component).

Even better than "drawing" a 100% transparent bitmap would probably be to draw nothing at all in this case, lines 483 to 496:

        if (bitmap == null) {
            // apply flat shading
            if (tileRect != null) {
                this.graphics2D.setClip(
                        (int) Math.round(tileRect.left), (int) Math.round(tileRect.top),
                        (int) Math.round(tileRect.right - (int) tileRect.left), (int) Math.round(tileRect.bottom) - (int) Math.round(tileRect.top) // subtract in after rounding to get same error as on neighbor tile
                );
            }
            this.graphics2D.setColor(NEUTRAL_HILLS);
            this.graphics2D.fillRect(0, 0, getWidth(), getHeight());
            this.graphics2D.setComposite(oldComposite);
            this.graphics2D.setClip(null);
            return;
        }

@devemux86

Copy link
Copy Markdown
Collaborator

@Sublimis thanks for the ideas.

Have you tried them? Unfortunately none of them work.
Only AwtLuminanceShadingComposite seems to work.

@devemux86 devemux86 added this to the 0.22.0 milestone Aug 21, 2024
@devemux86
devemux86 merged commit bac1fcd into mapsforge:master Aug 21, 2024
@devemux86

Copy link
Copy Markdown
Collaborator

@Sublimis thanks for your nice work! 👍

@Sublimis

Copy link
Copy Markdown
Contributor Author

Have you tried them? Unfortunately none of them work. Only AwtLuminanceShadingComposite seems to work.

Sorry, no, just looking at the code, should have mentioned that.

@Sublimis

Copy link
Copy Markdown
Contributor Author

@devemux86
50% gray for "neutral hills" seemed a logical, if not the only possible culprit, and since this constant hasn't changed, are you sure that this problem is fixed for all use cases? What if someone puts Parameters.LUMINANCE_COMPOSITE = false, what motivation and consequences would be in doing that?

@devemux86

Copy link
Copy Markdown
Collaborator

AlphaComposite is faster, so better with the other algorithms.
With this algorithm it does not work on sea.

AwtLuminanceShadingComposite seems to work with this algorithm.
Except the Motorider dark version, we will set a different blue color.

Anyway, for better results on the desktop, more research may be needed.

@Sublimis

Sublimis commented Aug 22, 2024 •

Copy link
Copy Markdown
Contributor Author

@devemux86
If a v0.22.0 release is planned soon, maybe you can give us a few more days for further hill shading improvements? The plan is to remove the gray shade of flat areas from the Simple and Diffuse algorithms without major impact on the algorithms themselves. Maybe then there will be no need for Parameters.LUMINANCE_COMPOSITE?

If we do this, is it better to mark them as @Deprecated and create new classes with similar names, or to just modify the existing algorithms as such?

@devemux86

devemux86 commented Aug 22, 2024 •

Copy link
Copy Markdown
Collaborator

@Sublimis

There is still no plan for the next version, so we have plenty of time for improvements.

The plan is to remove the gray shade of flat areas from the Simple and Diffuse algorithms without major impact on the algorithms themselves.

That would be great! 👍

Maybe then there will be no need for Parameters.LUMINANCE_COMPOSITE?

If the old algorithms can be improved to work without gray shade in flat areas
and we manage to fix the desktop AlphaComposite then we can revert this flag.

is it better to mark them as @deprecated and create new classes with similar names, or to just modify the existing algorithms as such?

Removing the gray shade in flat areas is something we want for all algorithms.
So we can directly modify the existing algorithms.

@devemux86

Copy link
Copy Markdown
Collaborator

@Sublimis

Is there any news on the work to remove the gray shade of flat areas from the Simple and Diffuse algorithms?

@Sublimis

Sublimis commented Sep 5, 2024

Copy link
Copy Markdown
Contributor Author

Yes -- sorry for the wait. This was the easy part, but we also created two new algorithms, both of which can look similar to the diffuse, but with more possible adjustments and optimizations, and clearer math. One of them is the "ultimate" high resolution algorithm (explanation comes with the code), and for the last week we've been trying to parallelize it to improve performance. This was a bit of a nightmare, but we succeeded yesterday. Multi-threading as an option will therefore be available in our other algorithms as well.

Since more work is needed on the new algorithms, we can create a pull request just for the fixes to the existing algorithms to reduce the delay, and then another later for the new algorithms, is that okay?

Here is an example of the original versus the corrected diffuse algorithm as it currently stands:

20240905_diffuse_original20240905_diffuse_corrected

@Sublimis

Sublimis commented Sep 5, 2024

Copy link
Copy Markdown
Contributor Author

And the high resolution one as it currently stands:

20240905_high_resolution

@devemux86

Copy link
Copy Markdown
Collaborator

@Sublimis Impressive results! 👍

Which algorithms will have a multi-threading option?
The new ones or all algorithms?

Since more work is needed on the new algorithms, we can create a pull request just for the fixes to the existing algorithms to reduce the delay, and then another later for the new algorithms, is that okay?

Let's do this, so we can test the changes in the old algorithms.

high resolution

Do you use 1" for this? Aren't they slow to process?

@Sublimis

Copy link
Copy Markdown
Contributor Author

Which algorithms will have a multi-threading option? The new ones or all algorithms?

Only the new ones for now. But since the solution will be generic, in an abstract class, covering all aspects of file reading, buffer management, and everything else except converting a square of altitude points to shade pixel(s), basically any algorithm can be easily adapted to use multi threading as desired.

However, multi threading is implemented in such a way that only one thread (per producer/calling thread) does the reading, with a custom number (can be zero) of other threads doing the processing. For simple algorithms that only do a few adds/muls per pixel (like e.g. the Simple one) there's probably no justification for increasing the complexity with multi threading.

Do you use 1" for this? Aren't they slow to process?

All the images above use 1" data, but we use the term "high resolution algorithm" not for the data it uses (it can as well be 3"), but for the fact that it produces shading that has four times as many pixels than that of other ("normal") algorithms. Whether it is justified is for a user to decide, but it's useful to have in the arsenal. 1" data is not too much to process on modern hardware even with a high resolution algorithm, and especially on desktops, or at least not when performance is not of the utmost importance.

BTW, the new algorithms are in the "code beautification" phase, basically almost finished!
The performance of the "normal resolution" one is comparable to the Diffuse one, doing one sqrt (like Diffuse) and a few more adds/muls than Diffuse per pixel (in the general/asymmetrical case), so maybe a tad slower, but on the plus side it looks better.

@Sublimis

Copy link
Copy Markdown
Contributor Author

We ended up fighting on two fronts: While working on our shading algorithms, we found some inefficiencies in HgtCache, the class that prepares hill shading data for rendering, due to a lack of proper synchronization. As if someone forgot that e.g. HgtCache.getHillshadingBitmap() can be called simultaneously by multiple threads. This could (and did, as debugging clearly showed) lead to race conditions i.e. extra redundant work being done by more than one thread (for example indexing HGT files, but also bitmap rendering), a work that ends up being discarded for all but the thread that finishes first.

So we took care of this, and while there, we also implemented reading DEM data from ZIP archives containing multiple HGT files, so files downloaded from e.g. viewfinderpanoramas.org can be used as is, without repackaging.

Should we make a separate pull request for these improvements (separate from hill shading algorithms)?

@devemux86

Copy link
Copy Markdown
Collaborator

@Sublimis

Should we make a separate pull request for these improvements (separate from hill shading algorithms)?

Let's do it this way. 👍

@devemux86

Copy link
Copy Markdown
Collaborator

@Sublimis

One thing I have seen with the recent improvements, is that the hill shading is not as "strong" anymore.

For example, in the past the "simple" algorithm had more "strong" hill shading, it looked better to the eyes.
(using the same 3" data)

@Sublimis

Sublimis commented Sep 17, 2024 •

Copy link
Copy Markdown
Contributor Author

Unfortunately, there is no single "perfect" method of shading hills, it's all a matter of wishes and compromises. Let us explain.

Previous versions of the algorithms had maximum contrast between NW and SE facing slopes: if you imagine a very steep hill with a triangle-like cross-section, spanning from SW to NE, then the NW-facing slope would have a shading value of 0, and the SE-facing slope would have 255 (in the range 0-255). At the top of such a hill there would be a "cut", an apparent line where two such opposite slopes meet, one with 0 and the other with 255 shading, so we can call it a "maximum contrast cut". Walking over the top of such a hill, the shading quickly changes from 0 to 127 (horizontal) to 255. However, for this to happen, the horizontal surface must be between the two extremes (unwanted horizontal shading), and the shading of NW slopes decreases as the slope is steeper.

The current versions do not have such a maximum cut at the top of the hill, but a milder one. Horizontal surfaces are at 0 shading. NW slopes get shading proportional to the steepness, up to some maximum that can be less than 255 (and by default it is; 128 in the case of simple shading, which would be called 50% asymmetry in light of upcoming algorithms). The shading of SE slopes is similar, but always goes to 255. Walking over the top of such a hill, the shading quickly changes from 128 to 255 (going down to 0 and back up in an instant), which is less contrast.

This can be alleviated somewhat (in upcoming algorithms, but perhaps also in the current ClearAsymmetry implementation) by adjusting the algorithm parameters, for example the maximum slope and the asymmetry factor.

Also please see this: #1539

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants