Opened 16 years ago

Last modified 16 years ago

#2734 assigned enhancement

[PATCH] Improved polygon scan conversion for imageFilledPolygon()

Reported by: brage Owned by: sdlime
Priority: normal Milestone: 5.6 release
Component: MapServer C Library Version: svn-trunk (development)
Severity: normal Keywords:
Cc: dmorissette

Description

The attached patch is a new implemetation of imageFilledPolygon() for mapgd.c.

With the current mapgd.c it is not really possible to make a polygon without an outline. If no outline is specified, the polygon will have a outline the same color as the fill. This results in loss of detail, and makes small polygons (for instance lakes and islands) look bigger than they should.

The problem is that imageFilledPolygon() uses a midpoint line algorithm, and includes points which are not strictly interior to the polygon. The patch replaces this with an interior extrema algorithm, based on Paul Heckbert's algorithm from from "Graphics Gems".

The attached examples should demonstrate how big difference this makes.

The patch is also eliminates the loop iterating over every point for every scanline, and is generally much faster than the old imageFilledPolygon. It should actually give mapsever appliactions using the gd backend a noticable performance improvement.

For example, the average runtime for shp2img when creating the example maps was

Unpatched Patched
Itasca lakes99ms84ms
North Europe coastline610ms399ms

And, at last, Mapserver should be able to render the coastline of Norway correctly!

-- Brage Førland

Attachments (9)

mapgd.patch (9.9 KB ) - added by brage 16 years ago.
Patch of mapgd.c with new implementation of imageFilledPolygon()
itasca-lakes-pathed.png (16.2 KB ) - added by brage 16 years ago.
Itasca lakes theme, patched mapg.c
itasca-lakes-orig.png (18.5 KB ) - added by brage 16 years ago.
Itasca lakes theme, unpatched mapg.c
northeurope-orig.png (10.3 KB ) - added by brage 16 years ago.
North Europe coastline, unpatched mapgd.c
mapgd.c.diff (10.3 KB ) - added by brage 16 years ago.
Updated patch
gd_poly_fill.map (609 bytes ) - added by brage 16 years ago.
Test demonstrating overlapping polygons
poly_original.png (2.9 KB ) - added by brage 16 years ago.
Output from test with unpatched mapgd
poly_patched.png (2.3 KB ) - added by brage 16 years ago.
Output from test with patched mapgd.c
northeurope-new.png (11.4 KB ) - added by brage 16 years ago.
North Europe coastline, patched mapgd.c

Download all attachments as: .zip

Change History (17)

by brage, 16 years ago

Attachment: mapgd.patch added

Patch of mapgd.c with new implementation of imageFilledPolygon()

by brage, 16 years ago

Attachment: itasca-lakes-pathed.png added

Itasca lakes theme, patched mapg.c

by brage, 16 years ago

Attachment: itasca-lakes-orig.png added

Itasca lakes theme, unpatched mapg.c

by brage, 16 years ago

Attachment: northeurope-orig.png added

North Europe coastline, unpatched mapgd.c

comment:1 by brage, 16 years ago

Summary: Improved polygon scan conversion for imageFilledPolygon()[PATCH] Improved polygon scan conversion for imageFilledPolygon()

comment:2 by sdlime, 16 years ago

Milestone: 5.4 release
Status: newassigned
Version: unspecifiedsvn-trunk (development)

Wow, excellent contribution. Does AGG suffer from the same problem or is this an GD only issue? Also, regarding the patch, since this is more than a one-liner, I need to make sure the code contributed in accordance with the MapServer committer guidelines:

http://mapserver.gis.umn.edu/development/rfc/ms-rfc-7.1/

Steve

comment:3 by dmorissette, 16 years ago

Cc: dmorissette added

comment:4 by brage, 16 years ago

I have done som further refinements to the patch to use bucket sort instead of qsort, which should give a slightly better performance than the original patch, especially for for complex polygons.

in reply to:  2 comment:5 by brage, 16 years ago

Replying to sdlime:

Wow, excellent contribution. Does AGG suffer from the same problem or is this an GD only issue?

I haven't been using the AGG backend to any extent, so I do not know if this issue affects AGG to.

Also, regarding the patch, since this is more than a one-liner, I need to make sure the code contributed in accordance with the MapServer committer guidelines:

http://mapserver.gis.umn.edu/development/rfc/ms-rfc-7.1/

As far as I can see the patch should not violate any of the guidelines. I have been looking at Heckbert's implementation at http://tog.acm.org/GraphicsGems/gems/ConcaveScan.c, but the code is quite different from his implementation. The license of the Graphic Gems examples is rather liberal anyway, see http://tog.acm.org/GraphicsGems/.

--Brage

by brage, 16 years ago

Attachment: mapgd.c.diff added

Updated patch

by brage, 16 years ago

Attachment: gd_poly_fill.map added

Test demonstrating overlapping polygons

by brage, 16 years ago

Attachment: poly_original.png added

Output from test with unpatched mapgd

comment:6 by brage, 16 years ago

gd_poly_fill.map is a simple testcase based on the dataset in msautotest/misc. Overlap between rendered polygons will create lines/artifacts in a darker greeen.

by brage, 16 years ago

Attachment: poly_patched.png added

Output from test with patched mapgd.c

comment:7 by brage, 16 years ago

The AGG renderer suffers from the same problem, but for a different reason. I have created a new issue (#2749) for AGG.

by brage, 16 years ago

Attachment: northeurope-new.png added

North Europe coastline, patched mapgd.c

comment:8 by sdlime, 16 years ago

I've applied the patch to the development branch. Let's see how it works! Brage, can you make one of the complex coastline shapefiles available for test development?

Steve

Note: See TracTickets for help on using tickets.