Add polygon to DTO convert strategy
This commit is contained in:
@@ -15,7 +15,6 @@ namespace StructureHelperCommon.Models.Forces
|
||||
{
|
||||
CheckObject.IsNull(targetObject, sourceObject);
|
||||
if (ReferenceEquals(targetObject, sourceObject)) { return; }
|
||||
|
||||
targetObject.Mx = sourceObject.Mx;
|
||||
targetObject.My = sourceObject.My;
|
||||
targetObject.Nz = sourceObject.Nz;
|
||||
|
||||
19
StructureHelperCommon/Models/Shapes/ILinePolygonShape.cs
Normal file
19
StructureHelperCommon/Models/Shapes/ILinePolygonShape.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace StructureHelperCommon.Models.Shapes
|
||||
{
|
||||
public interface ILinePolygonShape : IShape
|
||||
{
|
||||
IReadOnlyList<IVertex> Vertices { get; }
|
||||
bool IsClosed { get; set; }
|
||||
|
||||
IVertex AddVertex(IVertex vertex);
|
||||
IVertex InsertVertex(int index, IVertex vertex);
|
||||
IVertex AddVertexBefore(IVertex existing, IVertex vertex);
|
||||
IVertex AddVertexAfter(IVertex existing, IVertex vertex);
|
||||
|
||||
void RemoveVertex(IVertex vertex);
|
||||
void Clear();
|
||||
}
|
||||
|
||||
}
|
||||
31
StructureHelperCommon/Models/Shapes/IPolygonArcSegment.cs
Normal file
31
StructureHelperCommon/Models/Shapes/IPolygonArcSegment.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StructureHelperCommon.Models.Shapes
|
||||
{
|
||||
/// <summary>
|
||||
/// Implements arc segment of polygon
|
||||
/// </summary>
|
||||
public interface IPolygonArcSegment : IPolygonSegment
|
||||
{
|
||||
/// <summary>
|
||||
/// Vertex of center of arc
|
||||
/// </summary>
|
||||
IVertex Center { get; }
|
||||
/// <summary>
|
||||
/// Radius of arc segment, meters
|
||||
/// </summary>
|
||||
double Radius { get; }
|
||||
/// <summary>
|
||||
/// Start angle, Radians
|
||||
/// </summary>
|
||||
double StartAngle { get; }
|
||||
/// <summary>
|
||||
/// Sweep angle, Radians
|
||||
/// </summary>
|
||||
double SweepAngle { get; }
|
||||
}
|
||||
}
|
||||
15
StructureHelperCommon/Models/Shapes/IPolygonLineSegment.cs
Normal file
15
StructureHelperCommon/Models/Shapes/IPolygonLineSegment.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StructureHelperCommon.Models.Shapes
|
||||
{
|
||||
/// <summary>
|
||||
/// Implements line segment of polygon
|
||||
/// </summary>
|
||||
internal interface IPolygonLineSegment : IPolygonSegment
|
||||
{
|
||||
}
|
||||
}
|
||||
11
StructureHelperCommon/Models/Shapes/IPolygonSegment.cs
Normal file
11
StructureHelperCommon/Models/Shapes/IPolygonSegment.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using StructureHelperCommon.Infrastructures.Interfaces;
|
||||
|
||||
namespace StructureHelperCommon.Models.Shapes
|
||||
{
|
||||
public interface IPolygonSegment : ISaveable
|
||||
{
|
||||
IVertex StartVertex { get; }
|
||||
IVertex EndVertex { get; }
|
||||
void UpdateEndFromParameters();
|
||||
}
|
||||
}
|
||||
@@ -1,23 +1,16 @@
|
||||
using System;
|
||||
using StructureHelperCommon.Infrastructures.Interfaces;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StructureHelperCommon.Models.Shapes
|
||||
{
|
||||
public interface IPolygonShape : IShape
|
||||
/// <summary>
|
||||
/// Implements properties of polygon with diferent types of segment
|
||||
/// </summary>
|
||||
public interface IPolygonShape : ISaveable
|
||||
{
|
||||
IReadOnlyList<IVertex> Vertices { get; }
|
||||
bool IsClosed { get; set; }
|
||||
|
||||
IVertex AddVertex(IVertex vertex);
|
||||
IVertex InsertVertex(int index, IVertex vertex);
|
||||
IVertex AddVertexBefore(IVertex existing, IVertex vertex);
|
||||
IVertex AddVertexAfter(IVertex existing, IVertex vertex);
|
||||
|
||||
void RemoveVertex(IVertex vertex);
|
||||
void Clear();
|
||||
/// <summary>
|
||||
/// Collection of sements of polygon
|
||||
/// </summary>
|
||||
List<IPolygonSegment> Segments { get; }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
70
StructureHelperCommon/Models/Shapes/LinePolygonShape.cs
Normal file
70
StructureHelperCommon/Models/Shapes/LinePolygonShape.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using StructureHelperCommon.Infrastructures.Exceptions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace StructureHelperCommon.Models.Shapes
|
||||
{
|
||||
public class LinePolygonShape : ILinePolygonShape
|
||||
{
|
||||
private readonly List<IVertex> _vertices = new();
|
||||
|
||||
public Guid Id { get; }
|
||||
public IReadOnlyList<IVertex> Vertices => _vertices;
|
||||
public bool IsClosed { get; set; } = true;
|
||||
|
||||
public LinePolygonShape(Guid id)
|
||||
{
|
||||
Id = id;
|
||||
}
|
||||
|
||||
public IVertex AddVertex(IVertex vertex)
|
||||
{
|
||||
_vertices.Add(vertex);
|
||||
return vertex;
|
||||
}
|
||||
|
||||
public IVertex InsertVertex(int index, IVertex vertex)
|
||||
{
|
||||
_vertices.Insert(index, vertex);
|
||||
return vertex;
|
||||
}
|
||||
|
||||
public IVertex AddVertexBefore(IVertex existing, IVertex vertex)
|
||||
{
|
||||
int index = CheckVertexExists(existing);
|
||||
_vertices.Insert(index, vertex);
|
||||
return vertex;
|
||||
}
|
||||
public IVertex AddVertexAfter(IVertex existing, IVertex vertex)
|
||||
{
|
||||
int index = CheckVertexExists(existing);
|
||||
_vertices.Insert(index + 1, vertex);
|
||||
return vertex;
|
||||
}
|
||||
|
||||
public void RemoveVertex(IVertex vertex)
|
||||
{
|
||||
if (!_vertices.Remove(vertex))
|
||||
throw new StructureHelperException("The specified vertex was not found in the polygon.");
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
_vertices.Clear();
|
||||
}
|
||||
|
||||
|
||||
|
||||
private int CheckVertexExists(IVertex existing)
|
||||
{
|
||||
int index = _vertices.IndexOf(existing);
|
||||
if (index == -1)
|
||||
{
|
||||
throw new StructureHelperException("The specified vertex was not found in the polygon.");
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,9 +9,9 @@ namespace StructureHelperCommon.Models.Shapes
|
||||
{
|
||||
public interface IPolygonCalculator
|
||||
{
|
||||
double GetPerimeter(IPolygonShape polygon);
|
||||
double GetArea(IPolygonShape polygon);
|
||||
bool ContainsPoint(IPolygonShape polygon, IPoint2D point);
|
||||
double GetPerimeter(ILinePolygonShape polygon);
|
||||
double GetArea(ILinePolygonShape polygon);
|
||||
bool ContainsPoint(ILinePolygonShape polygon, IPoint2D point);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
using StructureHelperCommon.Infrastructures.Interfaces;
|
||||
using StructureHelperCommon.Models.Shapes.Logics;
|
||||
using StructureHelperCommon.Services;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace StructureHelperCommon.Models.Shapes
|
||||
{
|
||||
public class LinePolygonShapeUpdateStrategy : IParentUpdateStrategy<ILinePolygonShape>
|
||||
{
|
||||
public bool UpdateChildren { get; set; } = true;
|
||||
|
||||
public void Update(ILinePolygonShape targetObject, ILinePolygonShape sourceObject)
|
||||
{
|
||||
CheckObject.IsNull(targetObject, sourceObject);
|
||||
if (ReferenceEquals(targetObject, sourceObject)) { return; }
|
||||
|
||||
// Update simple properties
|
||||
targetObject.IsClosed = sourceObject.IsClosed;
|
||||
if (UpdateChildren == true)
|
||||
{
|
||||
// Replace vertices
|
||||
targetObject.Clear();
|
||||
foreach (var vertex in sourceObject.Vertices)
|
||||
{
|
||||
// Copy the underlying point into a new vertex
|
||||
Vertex newVertex = GetVertexClone(vertex);
|
||||
targetObject.AddVertex(newVertex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static Vertex GetVertexClone(IVertex vertex)
|
||||
{
|
||||
var updateStrategy = new VertexUpdateStrategy();
|
||||
Vertex newVertex = new(Guid.NewGuid());
|
||||
updateStrategy.Update(newVertex, vertex);
|
||||
return newVertex;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,7 +4,7 @@ namespace StructureHelperCommon.Models.Shapes
|
||||
{
|
||||
public class PolygonCalculator : IPolygonCalculator
|
||||
{
|
||||
public double GetPerimeter(IPolygonShape polygon)
|
||||
public double GetPerimeter(ILinePolygonShape polygon)
|
||||
{
|
||||
if (polygon.Vertices.Count < 2)
|
||||
return 0;
|
||||
@@ -24,7 +24,7 @@ namespace StructureHelperCommon.Models.Shapes
|
||||
return perimeter;
|
||||
}
|
||||
|
||||
public double GetArea(IPolygonShape polygon)
|
||||
public double GetArea(ILinePolygonShape polygon)
|
||||
{
|
||||
if (!polygon.IsClosed || polygon.Vertices.Count < 3)
|
||||
return 0;
|
||||
@@ -41,7 +41,7 @@ namespace StructureHelperCommon.Models.Shapes
|
||||
return Math.Abs(sum) / 2.0;
|
||||
}
|
||||
|
||||
public bool ContainsPoint(IPolygonShape polygon, IPoint2D point)
|
||||
public bool ContainsPoint(ILinePolygonShape polygon, IPoint2D point)
|
||||
{
|
||||
if (!polygon.IsClosed || polygon.Vertices.Count < 3)
|
||||
return false;
|
||||
|
||||
@@ -9,10 +9,10 @@ namespace StructureHelperCommon.Models.Shapes
|
||||
{
|
||||
public static class PolygonGeometryUtils
|
||||
{
|
||||
public static IPolygonShape GetTratsfromedPolygon(IPolygonShape polygon, double dx, double dy)
|
||||
public static ILinePolygonShape GetTratsfromedPolygon(ILinePolygonShape polygon, double dx, double dy)
|
||||
{
|
||||
IPolygonShape newPolygon = new PolygonShape(Guid.Empty);
|
||||
var updateLogic = new PolygonShapeUpdateStrategy();
|
||||
ILinePolygonShape newPolygon = new LinePolygonShape(Guid.Empty);
|
||||
var updateLogic = new LinePolygonShapeUpdateStrategy();
|
||||
updateLogic.Update(newPolygon, polygon);
|
||||
foreach (var item in newPolygon.Vertices)
|
||||
{
|
||||
@@ -21,7 +21,7 @@ namespace StructureHelperCommon.Models.Shapes
|
||||
}
|
||||
return newPolygon;
|
||||
}
|
||||
public static bool DoPolygonsEdgesIntersect(IPolygonShape polygon)
|
||||
public static bool DoPolygonsEdgesIntersect(ILinePolygonShape polygon)
|
||||
{
|
||||
var vertices = polygon.Vertices;
|
||||
int n = vertices.Count;
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
using StructureHelperCommon.Infrastructures.Interfaces;
|
||||
using StructureHelperCommon.Services;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace StructureHelperCommon.Models.Shapes
|
||||
{
|
||||
public class PolygonShapeUpdateStrategy : IUpdateStrategy<IPolygonShape>
|
||||
{
|
||||
public void Update(IPolygonShape targetObject, IPolygonShape sourceObject)
|
||||
{
|
||||
CheckObject.IsNull(sourceObject);
|
||||
CheckObject.IsNull(targetObject);
|
||||
if (ReferenceEquals(targetObject, sourceObject)) { return; }
|
||||
|
||||
// Update simple properties
|
||||
targetObject.IsClosed = sourceObject.IsClosed;
|
||||
|
||||
// Replace vertices
|
||||
targetObject.Clear();
|
||||
foreach (var vertex in sourceObject.Vertices)
|
||||
{
|
||||
// Copy the underlying point into a new vertex
|
||||
Vertex newVertex = GetVertexClone(vertex);
|
||||
targetObject.AddVertex(newVertex);
|
||||
}
|
||||
}
|
||||
|
||||
private static Vertex GetVertexClone(IVertex vertex)
|
||||
{
|
||||
Point2D newVertexPoint = new(Guid.NewGuid())
|
||||
{
|
||||
X = vertex.Point.X,
|
||||
Y = vertex.Point.Y
|
||||
};
|
||||
Vertex newVertex = new(Guid.NewGuid())
|
||||
{
|
||||
Point = newVertexPoint
|
||||
};
|
||||
return newVertex;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -24,7 +24,7 @@ namespace StructureHelperCommon.Models.Shapes
|
||||
{
|
||||
ProcessCircles(targetObject, sourceCircle);
|
||||
}
|
||||
else if (sourceObject is IPolygonShape sourcePolygon)
|
||||
else if (sourceObject is ILinePolygonShape sourcePolygon)
|
||||
{
|
||||
ProcessPolygon(targetObject, sourcePolygon);
|
||||
}
|
||||
@@ -34,11 +34,11 @@ namespace StructureHelperCommon.Models.Shapes
|
||||
}
|
||||
}
|
||||
|
||||
private void ProcessPolygon(IShape targetObject, IPolygonShape sourcePolygon)
|
||||
private void ProcessPolygon(IShape targetObject, ILinePolygonShape sourcePolygon)
|
||||
{
|
||||
if (targetObject is IPolygonShape targetPolygon)
|
||||
if (targetObject is ILinePolygonShape targetPolygon)
|
||||
{
|
||||
var updateLogic = new PolygonShapeUpdateStrategy();
|
||||
var updateLogic = new LinePolygonShapeUpdateStrategy();
|
||||
updateLogic.Update(targetPolygon, sourcePolygon);
|
||||
}
|
||||
else
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
using StructureHelperCommon.Infrastructures.Interfaces;
|
||||
using StructureHelperCommon.Services;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StructureHelperCommon.Models.Shapes
|
||||
{
|
||||
public class VertexUpdateStrategy : IParentUpdateStrategy<IVertex>
|
||||
{
|
||||
public bool UpdateChildren { get; set; } = true;
|
||||
|
||||
public void Update(IVertex targetObject, IVertex sourceObject)
|
||||
{
|
||||
CheckObject.IsNull(targetObject, sourceObject);
|
||||
if (ReferenceEquals(targetObject, sourceObject)) { return; }
|
||||
if (UpdateChildren == true)
|
||||
{
|
||||
var newPoint = sourceObject.Point.Clone() as IPoint2D;
|
||||
targetObject.Point = newPoint;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
41
StructureHelperCommon/Models/Shapes/PolygonArcSegment.cs
Normal file
41
StructureHelperCommon/Models/Shapes/PolygonArcSegment.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StructureHelperCommon.Models.Shapes
|
||||
{
|
||||
public class PolygonArcSegment : IPolygonArcSegment
|
||||
{
|
||||
|
||||
public Guid Id { get; }
|
||||
public IVertex StartVertex { get; set; }
|
||||
public IVertex EndVertex { get; set; }
|
||||
public IVertex Center { get; }
|
||||
public double Radius { get; }
|
||||
public double StartAngle { get; }
|
||||
public double SweepAngle { get; }
|
||||
public PolygonArcSegment(Guid id, IVertex startVertex, IVertex center, double radius, double startAngleDeg, double sweepAngleDeg)
|
||||
{
|
||||
Id = id;
|
||||
StartVertex = startVertex;
|
||||
Center = center;
|
||||
Radius = radius;
|
||||
StartAngle = startAngleDeg;
|
||||
SweepAngle = sweepAngleDeg;
|
||||
EndVertex = new Vertex(Guid.NewGuid());
|
||||
UpdateEndFromParameters();
|
||||
}
|
||||
|
||||
public void UpdateEndFromParameters()
|
||||
{
|
||||
double endAngle = StartAngle + SweepAngle;
|
||||
double endVertexX = Center.Point.X + Radius * Math.Cos(endAngle);
|
||||
double endVertexY = Center.Point.Y + Radius * Math.Sin(endAngle);
|
||||
EndVertex.Point.X = endVertexX;
|
||||
EndVertex.Point.Y = endVertexY;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
22
StructureHelperCommon/Models/Shapes/PolygonLineSegment.cs
Normal file
22
StructureHelperCommon/Models/Shapes/PolygonLineSegment.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
|
||||
namespace StructureHelperCommon.Models.Shapes
|
||||
{
|
||||
public class PolygonLineSegment : IPolygonLineSegment
|
||||
{
|
||||
public Guid Id { get; }
|
||||
public IVertex StartVertex { get; }
|
||||
public IVertex EndVertex { get; }
|
||||
|
||||
|
||||
public PolygonLineSegment(Guid id)
|
||||
{
|
||||
Id = id;
|
||||
}
|
||||
|
||||
public void UpdateEndFromParameters()
|
||||
{
|
||||
//nothing to do
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,70 +1,16 @@
|
||||
using StructureHelperCommon.Infrastructures.Exceptions;
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace StructureHelperCommon.Models.Shapes
|
||||
{
|
||||
public class PolygonShape : IPolygonShape
|
||||
{
|
||||
private readonly List<IVertex> _vertices = new();
|
||||
|
||||
public Guid Id { get; }
|
||||
public IReadOnlyList<IVertex> Vertices => _vertices;
|
||||
public bool IsClosed { get; set; } = true;
|
||||
|
||||
public List<IPolygonSegment> Segments { get; } = [];
|
||||
public PolygonShape(Guid id)
|
||||
{
|
||||
Id = id;
|
||||
}
|
||||
|
||||
public IVertex AddVertex(IVertex vertex)
|
||||
{
|
||||
_vertices.Add(vertex);
|
||||
return vertex;
|
||||
}
|
||||
|
||||
public IVertex InsertVertex(int index, IVertex vertex)
|
||||
{
|
||||
_vertices.Insert(index, vertex);
|
||||
return vertex;
|
||||
}
|
||||
|
||||
public IVertex AddVertexBefore(IVertex existing, IVertex vertex)
|
||||
{
|
||||
int index = CheckVertexExists(existing);
|
||||
_vertices.Insert(index, vertex);
|
||||
return vertex;
|
||||
}
|
||||
public IVertex AddVertexAfter(IVertex existing, IVertex vertex)
|
||||
{
|
||||
int index = CheckVertexExists(existing);
|
||||
_vertices.Insert(index + 1, vertex);
|
||||
return vertex;
|
||||
}
|
||||
|
||||
public void RemoveVertex(IVertex vertex)
|
||||
{
|
||||
if (!_vertices.Remove(vertex))
|
||||
throw new InvalidOperationException("The specified vertex was not found in the polygon.");
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
_vertices.Clear();
|
||||
}
|
||||
|
||||
|
||||
|
||||
private int CheckVertexExists(IVertex existing)
|
||||
{
|
||||
int index = _vertices.IndexOf(existing);
|
||||
if (index == -1)
|
||||
{
|
||||
throw new StructureHelperException("The specified vertex was not found in the polygon.");
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user