Add polygon shape
This commit is contained in:
@@ -61,7 +61,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Autofac" Version="8.2.0" />
|
||||
<PackageReference Include="Autofac" Version="8.4.0" />
|
||||
<PackageReference Include="LiveChartsCore" Version="2.0.0-rc4.5" />
|
||||
<PackageReference Include="LiveCharts" Version="0.9.7" />
|
||||
<PackageReference Include="LiveCharts.Wpf" Version="0.9.7" />
|
||||
|
||||
23
StructureHelperCommon/Models/Shapes/IPolygonShape.cs
Normal file
23
StructureHelperCommon/Models/Shapes/IPolygonShape.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StructureHelperCommon.Models.Shapes
|
||||
{
|
||||
public interface IPolygonShape : 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();
|
||||
}
|
||||
|
||||
}
|
||||
15
StructureHelperCommon/Models/Shapes/IVertex.cs
Normal file
15
StructureHelperCommon/Models/Shapes/IVertex.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using StructureHelperCommon.Infrastructures.Interfaces;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StructureHelperCommon.Models.Shapes
|
||||
{
|
||||
public interface IVertex : ISaveable
|
||||
{
|
||||
IPoint2D Point { get; set; }
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
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 interface IPolygonCalculator
|
||||
{
|
||||
double GetPerimeter(IPolygonShape polygon);
|
||||
double GetArea(IPolygonShape polygon);
|
||||
bool ContainsPoint(IPolygonShape polygon, IPoint2D point);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
using System;
|
||||
|
||||
namespace StructureHelperCommon.Models.Shapes
|
||||
{
|
||||
public class PolygonCalculator : IPolygonCalculator
|
||||
{
|
||||
public double GetPerimeter(IPolygonShape polygon)
|
||||
{
|
||||
if (polygon.Vertices.Count < 2)
|
||||
return 0;
|
||||
|
||||
double perimeter = 0;
|
||||
|
||||
for (int i = 0; i < polygon.Vertices.Count - 1; i++)
|
||||
{
|
||||
perimeter += Distance(polygon.Vertices[i].Point, polygon.Vertices[i + 1].Point);
|
||||
}
|
||||
|
||||
if (polygon.IsClosed && polygon.Vertices.Count > 2)
|
||||
{
|
||||
perimeter += Distance(polygon.Vertices[^1].Point, polygon.Vertices[0].Point);
|
||||
}
|
||||
|
||||
return perimeter;
|
||||
}
|
||||
|
||||
public double GetArea(IPolygonShape polygon)
|
||||
{
|
||||
if (!polygon.IsClosed || polygon.Vertices.Count < 3)
|
||||
return 0;
|
||||
|
||||
// Shoelace formula
|
||||
double sum = 0;
|
||||
for (int i = 0; i < polygon.Vertices.Count; i++)
|
||||
{
|
||||
var p1 = polygon.Vertices[i].Point;
|
||||
var p2 = polygon.Vertices[(i + 1) % polygon.Vertices.Count].Point;
|
||||
sum += (p1.X * p2.Y - p2.X * p1.Y);
|
||||
}
|
||||
|
||||
return Math.Abs(sum) / 2.0;
|
||||
}
|
||||
|
||||
public bool ContainsPoint(IPolygonShape polygon, IPoint2D point)
|
||||
{
|
||||
if (!polygon.IsClosed || polygon.Vertices.Count < 3)
|
||||
return false;
|
||||
|
||||
// Ray casting algorithm
|
||||
bool inside = false;
|
||||
int j = polygon.Vertices.Count - 1;
|
||||
|
||||
for (int i = 0; i < polygon.Vertices.Count; i++)
|
||||
{
|
||||
var pi = polygon.Vertices[i].Point;
|
||||
var pj = polygon.Vertices[j].Point;
|
||||
|
||||
if (((pi.Y > point.Y) != (pj.Y > point.Y)) &&
|
||||
(point.X < (pj.X - pi.X) * (point.Y - pi.Y) / (pj.Y - pi.Y) + pi.X))
|
||||
{
|
||||
inside = !inside;
|
||||
}
|
||||
j = i;
|
||||
}
|
||||
|
||||
return inside;
|
||||
}
|
||||
|
||||
private double Distance(IPoint2D p1, IPoint2D p2)
|
||||
{
|
||||
double dx = p2.X - p1.X;
|
||||
double dy = p2.Y - p1.Y;
|
||||
return Math.Sqrt(dx * dx + dy * dy);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
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 static class PolygonGeometryUtils
|
||||
{
|
||||
public static bool DoPolygonsEdgesIntersect(IPolygonShape polygon)
|
||||
{
|
||||
var vertices = polygon.Vertices;
|
||||
int n = vertices.Count;
|
||||
|
||||
if (n < 4)
|
||||
return false; // Triangles cannot self-intersect
|
||||
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
var a1 = vertices[i].Point;
|
||||
var a2 = vertices[(i + 1) % n].Point;
|
||||
|
||||
for (int j = i + 1; j < n; j++)
|
||||
{
|
||||
var b1 = vertices[j].Point;
|
||||
var b2 = vertices[(j + 1) % n].Point;
|
||||
|
||||
// Skip adjacent edges (they share a vertex)
|
||||
if (AreAdjacent(i, j, n, polygon.IsClosed))
|
||||
continue;
|
||||
|
||||
if (SegmentsIntersect(a1, a2, b1, b2))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool AreAdjacent(int i, int j, int n, bool isClosed)
|
||||
{
|
||||
// Edges (i,i+1) and (j,j+1) are adjacent if:
|
||||
if (j == i) return true;
|
||||
if (j == i + 1) return true;
|
||||
if (i == j + 1) return true;
|
||||
|
||||
// Special case: first and last edges in closed polygon
|
||||
if (isClosed &&
|
||||
((i == 0 && j == n - 1) || (j == 0 && i == n - 1)))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool SegmentsIntersect(IPoint2D p1, IPoint2D p2, IPoint2D q1, IPoint2D q2)
|
||||
{
|
||||
return (Orientation(p1, p2, q1) * Orientation(p1, p2, q2) < 0) &&
|
||||
(Orientation(q1, q2, p1) * Orientation(q1, q2, p2) < 0);
|
||||
}
|
||||
|
||||
private static int Orientation(IPoint2D a, IPoint2D b, IPoint2D c)
|
||||
{
|
||||
double val = (b.Y - a.Y) * (c.X - b.X) -
|
||||
(b.X - a.X) * (c.Y - b.Y);
|
||||
|
||||
if (Math.Abs(val) < 1e-12) return 0; // Collinear
|
||||
return (val > 0) ? 1 : -1; // Clockwise / Counter-clockwise
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
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 PolygonUpdateStrategy : 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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
71
StructureHelperCommon/Models/Shapes/PolygonShape.cs
Normal file
71
StructureHelperCommon/Models/Shapes/PolygonShape.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using StructureHelperCommon.Infrastructures.Exceptions;
|
||||
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; }
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
19
StructureHelperCommon/Models/Shapes/Vertex.cs
Normal file
19
StructureHelperCommon/Models/Shapes/Vertex.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using StructureHelperCommon.Infrastructures.Exceptions;
|
||||
using System;
|
||||
|
||||
namespace StructureHelperCommon.Models.Shapes
|
||||
{
|
||||
public class Vertex : IVertex
|
||||
{
|
||||
public Guid Id { get; }
|
||||
|
||||
public Vertex(Guid id)
|
||||
{
|
||||
Id = id;
|
||||
}
|
||||
|
||||
public IPoint2D Point { get; set; }
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -11,7 +11,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="ExcelDataReader" Version="3.7.0" />
|
||||
<PackageReference Include="NLog" Version="5.3.4" />
|
||||
<PackageReference Include="NLog" Version="6.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -21,10 +21,5 @@ namespace StructureHelperLogics.NdmCalculations.Primitives
|
||||
{
|
||||
Id = id;
|
||||
}
|
||||
|
||||
public DivisionSize() : this (Guid.NewGuid())
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ namespace StructureHelperLogics.NdmCalculations.Primitives
|
||||
/// <inheritdoc/>
|
||||
public INdmElement NdmElement { get; } = new NdmElement();
|
||||
/// <inheritdoc/>
|
||||
public IDivisionSize DivisionSize { get; } = new DivisionSize();
|
||||
public IDivisionSize DivisionSize { get; } = new DivisionSize(Guid.NewGuid());
|
||||
/// <inheritdoc/>
|
||||
public IShape Shape => rectangleShape;
|
||||
/// <inheritdoc/>
|
||||
|
||||
@@ -6,6 +6,6 @@ namespace StructureHelperLogics.NdmCalculations.Primitives
|
||||
{
|
||||
double Area { get; set; }
|
||||
string Name { get; set; }
|
||||
Point2D Point { get; set; }
|
||||
IPoint2D Point { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using StructureHelperCommon.Models.Shapes;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StructureHelperLogics.NdmCalculations.Primitives
|
||||
{
|
||||
public interface IShapeNDMPrimitive : INdmPrimitive, IHasDivisionSize
|
||||
{
|
||||
void SetShape(IShape shape);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using LoaderCalculator.Data.Ndms;
|
||||
using StructureHelperLogics.NdmCalculations.Triangulations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StructureHelperLogics.NdmCalculations.Primitives
|
||||
{
|
||||
public interface MeshNDMLogic
|
||||
{
|
||||
IEnumerable<INdm> GetNdms(ITriangulationOptions triangulation);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using StructureHelperCommon.Infrastructures.Interfaces;
|
||||
using StructureHelperCommon.Models.Shapes;
|
||||
using StructureHelperCommon.Models.Shapes.Logics;
|
||||
using StructureHelperCommon.Services;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StructureHelperLogics.NdmCalculations.Primitives
|
||||
{
|
||||
public class ShapeNDMPrimitiveUpdateStrategy : IUpdateStrategy<IShapeNDMPrimitive>
|
||||
{
|
||||
private IUpdateStrategy<INdmPrimitive> basePrimitiveUpdateStrategy;
|
||||
private IUpdateStrategy<IDivisionSize> divisionPropsUpdateStrategy;
|
||||
private IUpdateStrategy<IShape> shapeUpdateStrategy;
|
||||
|
||||
public ShapeNDMPrimitiveUpdateStrategy(
|
||||
IUpdateStrategy<INdmPrimitive> basePrimitiveUpdateStrategy,
|
||||
IUpdateStrategy<IDivisionSize> divisionPropsUpdateStrategy,
|
||||
IUpdateStrategy<IShape> shapeUpdateStrategy)
|
||||
{
|
||||
this.basePrimitiveUpdateStrategy = basePrimitiveUpdateStrategy;
|
||||
this.divisionPropsUpdateStrategy = divisionPropsUpdateStrategy;
|
||||
this.shapeUpdateStrategy = shapeUpdateStrategy;
|
||||
}
|
||||
|
||||
public void Update(IShapeNDMPrimitive targetObject, IShapeNDMPrimitive sourceObject)
|
||||
{
|
||||
CheckObject.IsNull(sourceObject, "source object");
|
||||
CheckObject.IsNull(targetObject, "target object");
|
||||
if (ReferenceEquals(targetObject, sourceObject)) { return; }
|
||||
InitializeStrategies();
|
||||
basePrimitiveUpdateStrategy.Update(targetObject, sourceObject);
|
||||
divisionPropsUpdateStrategy.Update(targetObject.DivisionSize, sourceObject.DivisionSize);
|
||||
shapeUpdateStrategy.Update(targetObject.Shape, sourceObject.Shape);
|
||||
}
|
||||
|
||||
private void InitializeStrategies()
|
||||
{
|
||||
basePrimitiveUpdateStrategy ??= new BaseUpdateStrategy();
|
||||
divisionPropsUpdateStrategy ??= new DivisionSizeUpdateStrategy();
|
||||
shapeUpdateStrategy ??= new ShapeUpdateStrategy();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,7 @@ namespace StructureHelperLogics.NdmCalculations.Primitives
|
||||
public class NamedAreaPoint : INamedAreaPoint
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public Point2D Point { get; set; }
|
||||
public IPoint2D Point { get; set; }
|
||||
public double Area { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ namespace StructureHelperLogics.NdmCalculations.Primitives
|
||||
|
||||
public INdmElement NdmElement { get; } = new NdmElement();
|
||||
|
||||
public IDivisionSize DivisionSize { get; } = new DivisionSize();
|
||||
public IDivisionSize DivisionSize { get; } = new DivisionSize(Guid.NewGuid());
|
||||
|
||||
public IShape Shape => rectangleShape;
|
||||
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
using LoaderCalculator.Data.Ndms;
|
||||
using StructureHelperCommon.Models.Shapes;
|
||||
using StructureHelperLogics.Models.CrossSections;
|
||||
using StructureHelperLogics.NdmCalculations.Triangulations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StructureHelperLogics.NdmCalculations.Primitives
|
||||
{
|
||||
public class ShapeNdmPrimitive : IShapeNDMPrimitive
|
||||
{
|
||||
private IShape shape;
|
||||
|
||||
public Guid Id { get; }
|
||||
public string? Name { get; set; } = string.Empty;
|
||||
public IPoint2D Center { get; set; } = new Point2D();
|
||||
public IShape Shape => shape;
|
||||
public INdmElement NdmElement { get; } = new NdmElement(Guid.NewGuid());
|
||||
public ICrossSection? CrossSection { get; set; }
|
||||
public IVisualProperty VisualProperty { get; } = new VisualProperty { Opacity = 0.8d };
|
||||
public double RotationAngle { get; set; }
|
||||
public IDivisionSize DivisionSize { get; } = new DivisionSize(Guid.NewGuid());
|
||||
public ShapeNdmPrimitive(Guid id)
|
||||
{
|
||||
Id = id;
|
||||
}
|
||||
|
||||
public object Clone()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public IEnumerable<INdm> GetNdms(ITriangulationOptions triangulationOptions)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public List<INamedAreaPoint> GetValuePoints()
|
||||
{
|
||||
var points = new List<INamedAreaPoint>();
|
||||
INamedAreaPoint newPoint;
|
||||
newPoint = new NamedAreaPoint
|
||||
{
|
||||
Name = "Center",
|
||||
Point = Center.Clone() as Point2D,
|
||||
Area = 0d
|
||||
};
|
||||
points.Add(newPoint);
|
||||
if (shape is IPolygonShape polygon)
|
||||
{
|
||||
int i = 0;
|
||||
foreach (var item in polygon.Vertices)
|
||||
{
|
||||
newPoint = new NamedAreaPoint
|
||||
{
|
||||
Name = $"Vertex {i}",
|
||||
Point = item.Point.Clone() as IPoint2D,
|
||||
Area = 0d
|
||||
};
|
||||
points.Add(newPoint);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return points;
|
||||
}
|
||||
|
||||
public bool IsPointInside(IPoint2D point)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void SetShape(IShape shape)
|
||||
{
|
||||
this.shape = shape;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,11 +9,11 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Castle.Core" Version="5.1.1" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
|
||||
<PackageReference Include="Castle.Core" Version="5.2.1" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
|
||||
<PackageReference Include="Moq" Version="4.20.72" />
|
||||
<PackageReference Include="NUnit" Version="4.3.0" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
|
||||
<PackageReference Include="NUnit" Version="4.4.0" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="5.1.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user