Compare commits

..

2 commits

Author SHA1 Message Date
borovec zdenek
75d12d6a5a Implement Region.GetContourCopies 2024-09-04 14:36:34 +02:00
borovec zdenek
6744f5e967 Optimize the contour creation. 2024-09-04 14:29:30 +02:00
3 changed files with 32 additions and 37 deletions

View file

@ -111,7 +111,12 @@ namespace Boprs
BooleanOperation bopr = new BooleanOperation(subject, clip, type);
bopr.Result.Draw();
List <Contour> contours = bopr.Result.GetContourCopies();
foreach (Contour contour in contours)
{
contour.Draw();
}
}
}
}

View file

@ -20,17 +20,17 @@ namespace Boprs
/// <summary>
/// Simple closed polygon.
/// </summary>
public class Contour
public class Contour : ICloneable
{
/// <summary>
/// Depth of the contour within the region.
/// </summary>
internal int ContourDepth { get; set; }
public object Clone()
{
return new Contour() { Vertices = new List<Point2d>(Vertices) };
}
/// <summary>
/// Vertices of the contour.
/// </summary>
public List<Point2d> Vertices { get; }
public List<Point2d> Vertices { get; private set; }
/// <summary>
/// Are the vertices of the contour arranged in clockwise manner?
@ -70,7 +70,6 @@ namespace Boprs
/// </summary>
public Contour()
{
ContourDepth = 0;
Vertices = new List<Point2d>();
}

View file

@ -151,40 +151,33 @@ namespace Boprs
// Lexico-graphically sort the vertices.
vertices.Sort();
// Sweepline to keep track of edges.
SweepLine sweepLine = new SweepLine();
bool[] processed = new bool[vertices.Count];
List<Contour> contours = new List<Contour>();
for(int i = 0; i < vertices.Count; i++)
{
SweepVertex acVx = vertices[i];
if (acVx.IsLeft())
{
sweepLine.Add(acVx.Edge, false);
}
else
{
sweepLine.Remove(acVx.Edge);
}
// If we already traversed this vertex, skip it.
if (processed[i])
{
continue;
}
// Otherwise this is an edge on an indiscovered contour, create it.
Contour contour = new Contour();
contours.Add(contour);
SweepEdge prev = sweepLine.PrevEdge(acVx.Edge);
bool shouldBeCW = prev == null ? false : prev.TransitionInside;
Point2d target = acVx.Point;
// acVx should be guaranteed to be the lowest and leftest vertex of the new polygon.
// Thus it cannot be vertical.
// if it is transition inside, it is a positive contour and should wind counter-clockwise,
// if it is transition outside, it is a negative contour and should wind clockwise.
SweepVertex acVx = vertices[i];
bool shouldBeCW = !acVx.Edge.TransitionInside;
// Traverse connected edges, and mark taversed vertices as processed,
// until we loop back to the first discovered vertex on the contour.
int pos = i;
processed[i] = true;
Point2d target = acVx.Point;
contour.Vertices.Add(vertices[pos].Point);
acVx.Edge.ParentContour = contour;
while (!Utils.DoubleEquals(vertices[pos].OtherVertex().Point.GetDistanceTo(target), 0))
@ -200,6 +193,7 @@ namespace Boprs
}
processed[vertices.IndexOf(vertices[pos].OtherVertex())] = true;
// If the contour is a hole and winds ccw, or positive and winds cw, flip it.
if (shouldBeCW != contour.IsClockwise())
{
contour.Reverse();
@ -230,7 +224,14 @@ namespace Boprs
/// <returns>List of copies of the regions contours.</returns>
public List<Contour> GetContourCopies()
{
throw new NotImplementedException();
List<Contour> copies = new List<Contour>();
foreach (Contour contour in Contours)
{
copies.Add((Contour)contour.Clone());
}
return copies;
}
/// <summary>
@ -257,15 +258,5 @@ namespace Boprs
return region;
}
#if DEBUG
internal void Draw()
{
foreach (Contour contour in Contours)
{
contour.Draw();
}
}
#endif
}
}