Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Windows;
- using System.Windows.Media;
- using Unwrapper.CalculationClasses;
- namespace Unwrapper.Elements
- {
- [Serializable]
- public class PolygonUW
- {
- public List<Vertex> Vertices { get; private set; }
- public List<Edge> Edges { get; private set; }
- public bool Selected = false;
- private static readonly Brush DefaultColor = Brushes.Gray.Clone();
- private static readonly Brush SelectedColor = Brushes.Blue.Clone();
- //Create polygon
- public PolygonUW(List<Vertex> vertices)
- {
- Vertices = vertices;
- DefaultColor.Opacity = 0.2;
- SelectedColor.Opacity = 0.4;
- Edges=new List<Edge>();
- for (int i = 0; i < vertices.Count; i++)
- {
- var edge = new Edge(vertices[i], Vertices[(i + 1)%Vertices.Count]);
- Edges.Add(edge);
- vertices[i].Polygon = this;
- }
- }
- //Draw polygon
- public void DrawPolygon(DrawingContext drawingContext)
- {
- //Cull invalid polygons
- if (Vertices.Count < 3) return;
- //Fill polygon if selected
- if (Selected)
- {
- var segments = new LineSegment[Vertices.Count - 1];
- for (int i = 1; i < Vertices.Count; i++)
- {
- segments[i - 1] = new LineSegment(ScreenTransform.UvToScreen(Vertices[i].Group.Position), false);
- }
- var figure = new PathFigure(ScreenTransform.UvToScreen(Vertices[0].Group.Position), segments, true);
- var geo = new PathGeometry(new[] {figure});
- Brush brush;
- if (Selected) brush = SelectedColor;
- else brush = DefaultColor;
- drawingContext.DrawGeometry(brush, null, geo);
- }
- //Draw edges
- foreach (var edge in Edges)
- {
- if(!edge.Selected)
- edge.DrawEdge(drawingContext);
- }
- }
- //check if clicked on the polygon
- public bool PointInsidePolygon(Point point)
- {
- var screenVertex0=ScreenTransform.UvToScreen(Vertices[0].Group.Position);
- float xMin;
- float xMax = xMin = (float)(screenVertex0.X);
- float yMin;
- float yMax = yMin = (float)(screenVertex0.Y);
- foreach (var vertex in Vertices)
- {
- var screenVertex = ScreenTransform.UvToScreen(vertex.Group.Position);
- xMin = (float)Math.Min(xMin, screenVertex.X);
- xMax = (float)Math.Max(xMax, screenVertex.X);
- yMin = (float)Math.Min(yMin, screenVertex.Y);
- yMax = (float)Math.Max(yMax, screenVertex.Y);
- }
- //Check if point is in the bounding box around polygon
- if (point.X < xMin || point.X > xMax || point.Y < yMin || point.Y > yMax) return false;
- return PointInPolygon(point);
- }
- //Check if given point is in the polygon
- private bool PointInPolygon(Point point)
- {
- List<Point> transformedVertices=new List<Point>();
- foreach (var vertex in Vertices)
- {
- transformedVertices.Add(ScreenTransform.UvToScreen(vertex.Group.Position));
- }
- int i, j, nvert = Vertices.Count();
- bool c = false;
- for (i = 0, j = nvert - 1; i < nvert; j = i++)
- {
- if ((((transformedVertices[i].Y) >= point.Y) != (transformedVertices[j].Y >= point.Y)) &&
- (point.X <=
- (transformedVertices[j].X - transformedVertices[i].X) *
- (point.Y - transformedVertices[i].Y) /
- (transformedVertices[j].Y - transformedVertices[i].Y) + transformedVertices[i].X)
- )
- c = !c;
- }
- return c;
- }
- //Check which polygons are selected by clicking
- static public PolygonUW SelectPolygon(List<PolygonUW> Polygons, Point position)
- {
- PolygonUW result = null;
- foreach (var polygon in Polygons)
- {
- if (polygon.PointInsidePolygon(position)) result = polygon;
- }
- return result;
- }
- //Check which polygons are selected by selection box
- static public List<PolygonUW> SelectPolygons(List<PolygonUW> polygons, Point position1, Point position2,bool completely)
- {
- List<PolygonUW> result = new List<PolygonUW>();
- float xMin = (float)Math.Min(position1.X, position2.X);
- float xMax = (float)Math.Max(position1.X, position2.X);
- float yMin = (float)Math.Min(position1.Y, position2.Y);
- float yMax = (float)Math.Max(position1.Y, position2.Y);
- foreach (var polygon in polygons)
- {
- bool allPointsIn = true;
- foreach (var point in polygon.Vertices)
- {
- var position=ScreenTransform.UvToScreen(point.Group.Position);
- if (!VertexGroup.PointInBox(xMin, xMax, yMin, yMax, position)) allPointsIn = false;
- else if (!completely)
- {
- allPointsIn = true;
- break;
- }
- }
- //If the polygons need to completely in the selection box, then all points need to be in the selection box
- //else one of the edges of the polygon need to interesect with a line of the selectionbox
- if(allPointsIn)
- {
- result.Add(polygon);
- }
- else if(!completely)
- {
- foreach (var edge in polygon.Edges)
- {
- var edge1 = ScreenTransform.UvToScreen(edge.VertexOne.Group.Position);
- var edge2 = ScreenTransform.UvToScreen(edge.VertexTwo.Group.Position);
- var position3 = new Point(position2.X, position1.Y);
- var position4 = new Point(position1.X, position2.Y);
- if (
- Edge.LineIntersectsWithEdge(edge1, edge2, position1, position3) ||
- Edge.LineIntersectsWithEdge(edge1, edge2, position3, position2) ||
- Edge.LineIntersectsWithEdge(edge1, edge2, position2, position4) ||
- Edge.LineIntersectsWithEdge(edge1, edge2, position4, position1))
- {
- result.Add(polygon);
- break;
- }
- }
- }
- }
- return result;
- }
- //Selected all connected polygons, based on click
- static public List<PolygonUW> SelectPolygonIsland(List<PolygonUW> polygons, Point position)
- {
- var result = new List<PolygonUW>();
- foreach (var polygon in polygons.Where(polygon => polygon.PointInsidePolygon(position)).Where(polygon => !result.Contains(polygon)))
- {
- result.Add(polygon);
- AddAllConectedPolygons(polygon,result);
- }
- return result;
- }
- //Selected all connected polygons, based on selectionbox
- static public List<PolygonUW> SelectPolygonIslands(List<PolygonUW> polygons, Point position1, Point position2, bool completely)
- {
- List<PolygonUW> result = new List<PolygonUW>();
- float xMin = (float)Math.Min(position1.X, position2.X);
- float xMax = (float)Math.Max(position1.X, position2.X);
- float yMin = (float)Math.Min(position1.Y, position2.Y);
- float yMax = (float)Math.Max(position1.Y, position2.Y);
- var polygonIslands = new List<List<PolygonUW>>();
- var alreadyAddedPolygons = new List<PolygonUW>();
- foreach (var polygon in polygons)
- {
- if (alreadyAddedPolygons.Contains(polygon)) continue;
- var polygonIsland = new List<PolygonUW> {polygon};
- AddAllConectedPolygons(polygon, polygonIsland);
- polygonIslands.Add(polygonIsland);
- alreadyAddedPolygons.AddRange(polygonIsland);
- }
- foreach (var polygonIsland in polygonIslands)
- {
- var allPointsIn = true;
- foreach (var polygon in polygonIsland)
- {
- var doneChecking = false;
- foreach (var point in polygon.Vertices)
- {
- var position = ScreenTransform.UvToScreen(point.Group.Position);
- if (!VertexGroup.PointInBox(xMin, xMax, yMin, yMax, position)) allPointsIn = false;
- else if (!completely)
- {
- allPointsIn = true;
- doneChecking = true;
- break;
- }
- }
- if (doneChecking) break;
- }
- //If the polygons need to completely in the selection box, then all points need to be in the selection box
- //else one of the edges of one of the polygons need to interesect with a line of the selectionbox
- if (allPointsIn)
- {
- result.AddRange(polygonIsland);
- }
- else if (!completely)
- {
- var gotHit = false;
- foreach (var polygon in polygonIsland)
- {
- foreach (var edge in polygon.Edges)
- {
- var edge1 = ScreenTransform.UvToScreen(edge.VertexOne.Group.Position);
- var edge2 = ScreenTransform.UvToScreen(edge.VertexTwo.Group.Position);
- var position3 = new Point(position2.X, position1.Y);
- var position4 = new Point(position1.X, position2.Y);
- if (!Edge.LineIntersectsWithEdge(edge1, edge2, position1, position3) &&
- !Edge.LineIntersectsWithEdge(edge1, edge2, position3, position2) &&
- !Edge.LineIntersectsWithEdge(edge1, edge2, position2, position4) &&
- !Edge.LineIntersectsWithEdge(edge1, edge2, position4, position1)) continue;
- gotHit = true;
- break;
- }
- if (!gotHit) continue;
- result.AddRange(polygonIsland);
- break;
- }
- }
- }
- return result;
- }
- //Add all polygons connected to the given polygon
- static private void AddAllConectedPolygons(PolygonUW polygon, List<PolygonUW> polygonList)
- {
- foreach (var vertexPolygon in polygon.Vertices)
- {
- foreach (var vertex in vertexPolygon.Group.Vertices)
- {
- if(!polygonList.Contains(vertex.Polygon))
- {
- polygonList.Add(vertex.Polygon);
- AddAllConectedPolygons(vertex.Polygon, polygonList);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment