This commit is contained in:
Scott Williams 2017-01-18 22:13:52 +00:00
Родитель ce54281500
Коммит bbae488d8b
4 изменённых файлов: 71 добавлений и 64 удалений

Просмотреть файл

@ -51,7 +51,6 @@ namespace Shaper2D
: this(new[] { start, controlPoint1, controlPoint2, end }.Merge(additionalPoints))
{
}
/// <summary>
/// Returns the current <see cref="ILineSegment" /> a simple linear path.

Просмотреть файл

@ -1,24 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
// <copyright file="ArrayExtensions.cs" company="Scott Williams">
// Copyright (c) Scott Williams and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace Shaper2D
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
/// <summary>
/// Extensions on arrays.
/// </summary>
internal static class ArrayExtensions
{
/// <summary>
/// Merges the specified source2.
/// </summary>
/// <typeparam name="T">the type of the array</typeparam>
/// <param name="source1">The source1.</param>
/// <param name="source2">The source2.</param>
/// <returns>the Merged arrays</returns>
public static T[] Merge<T>(this T[] source1, T[] source2)
{
if (source2 == null)
{
return source1;
}
var target = new T[source1.Length + source2.Length];
for (var i = 0; i < source1.Length; i++)
{
target[i] = source1[i];
}
for (var i = 0; i < source2.Length; i++)
{
target[i + source1.Length] = source2[i];

Просмотреть файл

@ -41,12 +41,11 @@ namespace Shaper2D.PolygonClipper
/// </summary>
/// <param name="path">The path.</param>
/// <param name="polyType">Type of the poly.</param>
/// <returns></returns>
public void AddPaths(IEnumerable<IShape> path, PolyType polyType)
{
foreach (var p in path)
{
AddPath(p, polyType);
this.AddPath(p, polyType);
}
}
@ -55,12 +54,11 @@ namespace Shaper2D.PolygonClipper
/// </summary>
/// <param name="path">The path.</param>
/// <param name="polyType">Type of the poly.</param>
/// <returns></returns>
public void AddPaths(IEnumerable<IPath> path, PolyType polyType)
{
foreach (var p in path)
{
AddPath(p, polyType);
this.AddPath(p, polyType);
}
}
@ -69,7 +67,6 @@ namespace Shaper2D.PolygonClipper
/// </summary>
/// <param name="path">The path.</param>
/// <param name="polyType">Type of the poly.</param>
/// <returns></returns>
public void AddPath(IShape path, PolyType polyType)
{
if (path is IPath)
@ -297,48 +294,15 @@ namespace Shaper2D.PolygonClipper
return true;
}
private static List<IShape> ExtractOutlines(PolyNode tree)
{
var result = new List<IShape>();
ExtractOutlines(tree, result);
return result;
}
private static void ExtractOutlines(PolyNode tree, List<IShape> shapes)
{
if (tree.Contour.Any())
{
// if the source path is set then we clipper retained the full path intact thus we can freely
// use it and get any shape optimisations that are availible.
if (tree.SourcePath != null)
{
shapes.Add((IShape)tree.SourcePath);
}
else
{
Polygon polygon = new Polygon(new LinearLineSegment(tree.Contour.Select(x => new Point(x)).ToArray()));
shapes.Add(polygon);
}
}
foreach (PolyNode c in tree.Children)
{
ExtractOutlines(c, shapes);
}
}
/// <summary>
/// Executes the specified clip type.
/// </summary>
/// <returns>
/// Returns the <see cref="IShape[]" /> containing the converted polygons.
/// Returns the <see cref="IShape" /> array containing the converted polygons.
/// </returns>
public IShape[] Execute()
{
PolyTree polytree = new PolyTree();
bool succeeded = this.ExecuteInternal();
// build the return polygons ...
@ -365,6 +329,37 @@ namespace Shaper2D.PolygonClipper
return edge.Bot.X + Round(edge.Dx * (currentY - edge.Bot.Y));
}
private static List<IShape> ExtractOutlines(PolyNode tree)
{
var result = new List<IShape>();
ExtractOutlines(tree, result);
return result;
}
private static void ExtractOutlines(PolyNode tree, List<IShape> shapes)
{
if (tree.Contour.Any())
{
// if the source path is set then we clipper retained the full path intact thus we can freely
// use it and get any shape optimisations that are availible.
if (tree.SourcePath != null)
{
shapes.Add((IShape)tree.SourcePath);
}
else
{
Polygon polygon = new Polygon(new LinearLineSegment(tree.Contour.Select(x => new Point(x)).ToArray()));
shapes.Add(polygon);
}
}
foreach (PolyNode c in tree.Children)
{
ExtractOutlines(c, shapes);
}
}
private static double DistanceFromLineSqrd(Vector2 pt, Vector2 ln1, Vector2 ln2)
{
// The equation of a line in general form (Ax + By + C = 0)
@ -3084,9 +3079,7 @@ namespace Shaper2D.PolygonClipper
// outRec1 contains outRec2 ...
outRec2.IsHole = !outRec1.IsHole;
outRec2.FirstLeft = outRec1;
this.FixupFirstLefts2(outRec2, outRec1);
}
else if (Poly2ContainsPoly1(outRec1.Pts, outRec2.Pts))
{
@ -3095,19 +3088,14 @@ namespace Shaper2D.PolygonClipper
outRec1.IsHole = !outRec2.IsHole;
outRec2.FirstLeft = outRec1.FirstLeft;
outRec1.FirstLeft = outRec2;
this.FixupFirstLefts2(outRec1, outRec2);
}
else
{
// the 2 polygons are completely separate ...
outRec2.IsHole = outRec1.IsHole;
outRec2.FirstLeft = outRec1.FirstLeft;
this.FixupFirstLefts1(outRec1, outRec2);
}
}
else
@ -3126,7 +3114,6 @@ namespace Shaper2D.PolygonClipper
outRec2.FirstLeft = outRec1;
this.FixupFirstLefts3(outRec2, outRec1);
}
}
}
@ -3188,9 +3175,7 @@ namespace Shaper2D.PolygonClipper
outrec.IsHole = !outrec2.IsHole;
outrec2.FirstLeft = outrec.FirstLeft;
outrec.FirstLeft = outrec2;
this.FixupFirstLefts2(outrec, outrec2);
}
else
{
@ -3198,7 +3183,6 @@ namespace Shaper2D.PolygonClipper
outrec2.IsHole = outrec.IsHole;
outrec2.FirstLeft = outrec.FirstLeft;
this.FixupFirstLefts1(outrec, outrec2);
}
op2 = op; // ie get ready for the next iteration

Просмотреть файл

@ -1,4 +1,4 @@
// <copyright file="TEdge.cs" company="Scott Williams">
// <copyright file="Edge.cs" company="Scott Williams">
// Copyright (c) Scott Williams and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
@ -49,7 +49,7 @@ namespace Shaper2D.PolygonClipper
/// <value>
/// The top.
/// </value>
internal System.Numerics.Vector2 Top { get; set; }
public System.Numerics.Vector2 Top { get; set; }
/// <summary>
/// Gets or sets the delta.
@ -84,7 +84,6 @@ namespace Shaper2D.PolygonClipper
/// <remarks>Side only refers to current side of solution poly</remarks>
public EdgeSide Side { get; set; }
/// <summary>
/// Gets or sets the wind delta.
/// </summary>
@ -97,10 +96,16 @@ namespace Shaper2D.PolygonClipper
public int WindindDelta { get; set; }
/// <summary>
/// The winding count
/// Gets or sets the winding count
/// </summary>
public int WindingCount { get; set; }
/// <summary>
/// Gets or sets the type of the winding count in opposite poly.
/// </summary>
/// <value>
/// The type of the winding count in opposite poly.
/// </value>
public int WindingCountInOppositePolyType { get; set; }
/// <summary>
@ -120,11 +125,10 @@ namespace Shaper2D.PolygonClipper
public Edge NextEdge { get; set; }
/// <summary>
/// The previous
/// Gets or sets the previous
/// </summary>
public Edge PreviousEdge { get; set; }
/// <summary>
/// Gets or sets the next in LML.
/// </summary>
@ -142,18 +146,21 @@ namespace Shaper2D.PolygonClipper
public Edge NextInAEL { get; set; }
/// <summary>
/// The previous in ael
/// Gets or sets the previous in ael
/// </summary>
public Edge PreviousInAEL { get; set; }
/// <summary>
/// The next in sel
/// Gets or sets the next in sel
/// </summary>
public Edge NextInSEL { get; set; }
/// <summary>
/// The previous in sel
/// Gets or sets the previous in sel.
/// </summary>
/// <value>
/// The previous in sel.
/// </value>
public Edge PreviousInSEL { get; set; }
}
}