c# - 使用大量顶点从多边形创建图形需要很长时间
问题描述
我正在尝试用多面体构建一个图形。当我没有大量顶点但使用 1M 时,代码工作正常。有什么性能改进建议吗?
public Graph(List<Polygon> PolygonsSet)
{
edges = new List<Edge>();
graphID = Guid.NewGuid();
//Setting up Graph instance by adding vertices, edges and polygons
foreach (Polygon Polygon in PolygonsSet)
{
List<Vertex> vertices = Polygon.vertices;
// Clear pre-existing edges in the case this is an updating process.
Polygon.edges.Clear();
//If there is only one polygon, treat it as boundary
if (PolygonsSet.Count() == 1)
{
Polygon.isBoundary = true;
}
//If first and last point of vertices list are the same, remove last.
if (vertices.First().Equals(vertices.Last()) && vertices.Count() > 1)
{
vertices = vertices.Take(vertices.Count() - 1).ToList();
}
//For each point, creates vertex and associated edge and adds them
//to the polygons Dictionary
int vertexCount = vertices.Count();
// If valid polygon
if (vertexCount >= 3)
{
int newId = GetNextId();
for (var j = 0; j < vertexCount; j++)
{
int next_index = (j + 1) % vertexCount;
Vertex vertex = vertices[j];
Vertex next_vertex = vertices[next_index];
Edge edge = new Edge(vertex, next_vertex);
//If is a valid polygon, add id to vertex and
//edge to vertices dictionary
if (vertexCount > 2)
{
vertex.polygonId = newId;
next_vertex.polygonId = newId;
Polygon gPol = new Polygon();
if (polygons.TryGetValue(newId, out gPol))
{
gPol.edges.Add(edge);
}
else
{
Polygon.edges.Add(edge);
Polygon.id = newId;
polygons.Add(newId, Polygon);
}
}
AddEdge(edge);
}
}
}
}
AddEdge 方法是;
public void AddEdge(Edge edge)
{
List<Edge> startEdgesList = new List<Edge>();
List<Edge> endEdgesList = new List<Edge>();
if (graph.TryGetValue(edge.StartVertex, out startEdgesList))
{
if (!startEdgesList.Contains(edge)) { startEdgesList.Add(edge); }
}
else
{
graph.Add(edge.StartVertex, new List<Edge>() { edge });
}
if (graph.TryGetValue(edge.EndVertex, out endEdgesList))
{
if (!endEdgesList.Contains(edge)) { endEdgesList.Add(edge); }
}
else
{
graph.Add(edge.EndVertex, new List<Edge>() { edge });
}
if (!edges.Contains(edge)) { edges.Add(edge); }
}
代码工作正常,我唯一关心的是性能。
我试图简化多边形,甚至使用凸包来减少工作量,但在某些情况下,我需要按原样使用多边形。
所以任何帮助将不胜感激......
解决方案
在以 开头的代码行中if (vertexCount > 2)
,您测试顶点计数,但自上次测试以来计数没有改变if (vertexCount >= 3)
。放下这一秒if
。然后用 . 创建一个新的多边形Polygon gPol = new Polygon();
。该多边形随后立即被 in 中的out
参数替换polygons.TryGetValue(newId, out gPol)
。要么TryGetValue
产生true
,然后gPol
成为集合中的多边形,要么TryGetValue
产生false
并gPol
成为null
。不分配gPol
Polygon gPol;
if (polygons.TryGetValue(newId, out gPol)) ...
或者使用 C# 7.0 语法
if (polygons.TryGetValue(newId, out Polygon gPol)) ...
在这种else
情况下,您应该创建一个新多边形(因为gPol
is null
)。但是,您可以简化此代码,因为在两种情况下都添加了边缘:
if (!polygons.TryGetValue(newId, out Polygon gPol)) {
gPol = new Polygon { id = newId };
polygons.Add(newId, gPol);
}
gPol.edges.Add(edge);
您似乎也Polygon
与gPol
.
由于newId
在 for 循环之前创建,您可以将代码查找或创建多边形移出循环
int vertexCount = vertices.Count();
if (vertexCount >= 3)
{
int newId = GetNextId();
if (!polygons.TryGetValue(newId, out Polygon gPol)) {
gPol = new Polygon { id = newId };
polygons.Add(newId, gPol);
}
for (var j = 0; j < vertexCount; j++)
{
int next_index = (j + 1) % vertexCount;
Vertex vertex = vertices[j];
Vertex next_vertex = vertices[next_index];
Edge edge = new Edge(vertex, next_vertex);
vertex.polygonId = newId;
next_vertex.polygonId = newId;
gPol.edges.Add(edge);
AddEdge(edge);
}
}
在AddEdge
您重复覆盖刚刚创建的列表的相同错误TryGetValue
。
推荐阅读
- python - Python:尝试写入文件时出现“ValueError:无法打开控制台输入缓冲区进行写入”
- amazon-web-services - Apache Spark/AWS EMR 和处理文件的跟踪
- hadoop - hbase master 没有启动
- java - 如何监控从 pir 运动传感器到 Web 服务器的数据?
- angular - Create FaceListId FaceAPI Microsoft Azure
- jenkins - 詹金斯 - 多个实例
- node.js - Google 翻译 API 在 firebase 函数中不起作用
- netezza - 在 aginity 中使用 nzsql 命令
- django - Django 序列化器模型 LeftJoin
- google-maps - agm-marker iconUrl 在单击标记时更改