Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check for CCW of outer ring and CW for inner rings #2290

Merged
merged 3 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 18 additions & 5 deletions Mapsui.Rendering.Skia/Extensions/PolygonExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Mapsui.Rendering.Skia.Functions;
using NetTopologySuite.Geometries;
using SkiaSharp;
using System.Linq;

namespace Mapsui.Rendering.Skia.Extensions;

Expand All @@ -16,13 +17,19 @@ internal static class PolygonExtensions
/// <returns></returns>
public static SKPath ToSkiaPath(this Polygon polygon, Viewport viewport, SKRect clipRect, float strokeWidth)
{
// Reduce exterior ring to parts, that are visible in clipping rectangle
// Inflate clipRect, so that we could be sure, nothing of stroke is visible on screen
var exterior = ClippingFunctions.ReducePointsToClipRect(polygon.ExteriorRing?.Coordinates, viewport, SKRect.Inflate(clipRect, strokeWidth * 2, strokeWidth * 2));

// Create path for exterior and interior parts
var path = new SKPath();

if (polygon.ExteriorRing is null)
return path;

// Bring outer ring in CCW direction
var outerRing = (polygon.ExteriorRing.IsRing && ((LinearRing)polygon.ExteriorRing).IsCCW) ? polygon.ExteriorRing : (LineString)((Geometry)polygon.ExteriorRing).Reverse();

// Reduce exterior ring to parts, that are visible in clipping rectangle
// Inflate clipRect, so that we could be sure, nothing of stroke is visible on screen
var exterior = ClippingFunctions.ReducePointsToClipRect(outerRing?.Coordinates, viewport, SKRect.Inflate(clipRect, strokeWidth * 2, strokeWidth * 2));

if (exterior.Count == 0)
return path;

Expand All @@ -37,13 +44,19 @@ public static SKPath ToSkiaPath(this Polygon polygon, Viewport viewport, SKRect

foreach (var interiorRing in polygon.InteriorRings)
{
if (interiorRing is null)
continue;

// note: For Skia inner rings need to be clockwise and outer rings
// need to be counter clockwise (if this is the other way around it also
// seems to work)
// this is not a requirement of the OGC polygon.

// Bring inner ring in CW direction
var innerRing = (interiorRing.IsRing && ((LinearRing)interiorRing).IsCCW) ? (LineString)((Geometry)interiorRing).Reverse() : interiorRing;

// Reduce interior ring to parts, that are visible in clipping rectangle
var interior = ClippingFunctions.ReducePointsToClipRect(interiorRing.Coordinates, viewport, SKRect.Inflate(clipRect, strokeWidth, strokeWidth));
var interior = ClippingFunctions.ReducePointsToClipRect(innerRing?.Coordinates, viewport, SKRect.Inflate(clipRect, strokeWidth, strokeWidth));

if (interior.Count == 0)
continue;
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.