Add triangulation of polygon

This commit is contained in:
Evgeny Redikultsev
2025-10-26 22:19:25 +05:00
parent 196dc636bb
commit 09dcf4e7e9
52 changed files with 686 additions and 180 deletions

View File

@@ -0,0 +1,43 @@
using StructureHelperCommon.Infrastructures.Exceptions;
using StructureHelperCommon.Infrastructures.Interfaces;
using StructureHelperCommon.Models.Shapes;
namespace DataAccess.DTOs
{
public class LinePolygonFromDTOConvertStrategy : ConvertStrategy<ILinePolygonShape, ILinePolygonShape>
{
private IUpdateStrategy<ILinePolygonShape> updateStrategy;
private IConvertStrategy<IVertex, IVertex> vertexConvertStrategy;
private LinePolygonShape newItem;
public LinePolygonFromDTOConvertStrategy(IUpdateStrategy<ILinePolygonShape> updateStrategy)
{
this.updateStrategy = updateStrategy;
}
public LinePolygonFromDTOConvertStrategy(IBaseConvertStrategy baseConvertStrategy) : base(baseConvertStrategy)
{
updateStrategy = new LinePolygonShapeUpdateStrategy() { UpdateChildren = false };
vertexConvertStrategy = new VertexFtomDTOConvertStrategy(this);
}
public override ILinePolygonShape GetNewItem(ILinePolygonShape source)
{
ChildClass = this;
if (source is not LinePolygonShapeDTO sourceDTO)
{
throw new StructureHelperException(ErrorStrings.ObjectTypeIsUnknownObj(source) + ": source line polygon is not DTO object");
}
newItem = new(sourceDTO.Id);
updateStrategy.Update(newItem, sourceDTO);
newItem.Clear();
foreach (var item in source.Vertices)
{
IVertex newVertex = vertexConvertStrategy.Convert(item);
newItem.AddVertex(newVertex);
}
NewItem = newItem;
return NewItem;
}
}
}

View File

@@ -12,24 +12,26 @@ namespace DataAccess.DTOs
{
private IUpdateStrategy<ILinePolygonShape> updateStrategy;
private IConvertStrategy<VertexDTO, IVertex> vertexConvertStrategy;
private LinePolygonShapeDTO newItem;
public LinePolygonToDTOConvertStrategy(IBaseConvertStrategy baseConvertStrategy) : base(baseConvertStrategy)
{
updateStrategy = new LinePolygonShapeUpdateStrategy() { UpdateChildren = false };
vertexConvertStrategy = new VertexToDTOConvertStrategy(this);
}
public override LinePolygonShapeDTO GetNewItem(ILinePolygonShape source)
{
ChildClass = this;
NewItem = new(source.Id);
updateStrategy = new LinePolygonShapeUpdateStrategy() { UpdateChildren = false };
vertexConvertStrategy = new VertexToDTOConvertStrategy(this);
updateStrategy.Update(NewItem, source);
NewItem.Clear();
newItem = new(source.Id);
updateStrategy.Update(newItem, source);
newItem.Clear();
foreach (var item in source.Vertices)
{
VertexDTO newVertex = vertexConvertStrategy.Convert(item);
NewItem.AddVertex(newVertex);
newItem.AddVertex(newVertex);
}
NewItem = newItem;
return NewItem;
}
}

View File

@@ -1,12 +1,5 @@
using StructureHelperCommon.Infrastructures.Interfaces;
using StructureHelperCommon.Models.Loggers;
using StructureHelperCommon.Models;
using StructureHelperCommon.Models.Shapes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataAccess.DTOs
{
@@ -24,27 +17,16 @@ namespace DataAccess.DTOs
}
public override Point2D GetNewItem(Point2DDTO source)
public Point2DFromDTOConvertStrategy(IBaseConvertStrategy baseConvertStrategy) : base(baseConvertStrategy)
{
TraceLogger?.AddMessage("Point 2D converting has been started");
try
{
Point2D newItem = GetNewItemBySource(source);
TraceLogger?.AddMessage("Point 2D converting has been finished");
return newItem;
}
catch (Exception ex)
{
TraceLogger?.AddMessage($"Logic: {LoggerStrings.LogicType(this)} made error: {ex.Message}", TraceLogStatuses.Error);
throw;
}
updateStrategy = new Point2DUpdateStrategy();
}
private Point2D GetNewItemBySource(Point2DDTO source)
public override Point2D GetNewItem(Point2DDTO source)
{
Point2D newItem = new(source.Id);
updateStrategy.Update(newItem, source);
return newItem;
NewItem = new(source.Id);
updateStrategy.Update(NewItem, source);
return NewItem;
}
}
}

View File

@@ -28,6 +28,12 @@ namespace DataAccess.DTOs
(this, new CircleShapeFromDTOConvertStrategy(this));
NewItem = circleConvertStrategy.Convert(circleShapeDTO);
}
else if (source is LinePolygonShapeDTO linePolygonDTO)
{
var polygonConvertStrategy = new DictionaryConvertStrategy<ILinePolygonShape, ILinePolygonShape>
(this, new LinePolygonFromDTOConvertStrategy(this));
NewItem = polygonConvertStrategy.Convert(linePolygonDTO);
}
else
{
throw new StructureHelperException(ErrorStrings.ObjectTypeIsUnknownObj(source) + ": shape is unknown");

View File

@@ -0,0 +1,33 @@
using StructureHelperCommon.Infrastructures.Exceptions;
using StructureHelperCommon.Infrastructures.Interfaces;
using StructureHelperCommon.Models.Shapes;
namespace DataAccess.DTOs
{
public class VertexFtomDTOConvertStrategy : ConvertStrategy<IVertex, IVertex>
{
private IUpdateStrategy<IVertex> updateStrategy;
private IConvertStrategy<Point2D, Point2DDTO> pointConvertStrategy;
private Vertex newItem;
public VertexFtomDTOConvertStrategy(IBaseConvertStrategy baseConvertStrategy) : base(baseConvertStrategy)
{
updateStrategy = new VertexUpdateStrategy() { UpdateChildren = false };
pointConvertStrategy = new Point2DFromDTOConvertStrategy(this);
}
public override IVertex GetNewItem(IVertex source)
{
ChildClass = this;
if (source is not VertexDTO sourceDTO)
{
throw new StructureHelperException(ErrorStrings.ObjectTypeIsUnknownObj(source) + ": source vertex is not DTO object");
}
newItem = new(sourceDTO.Id);
updateStrategy.Update(newItem, sourceDTO);
newItem.Point = pointConvertStrategy.Convert(sourceDTO.Point as Point2DDTO);
NewItem = newItem;
return NewItem;
}
}
}