【发布时间】:2021-02-03 13:13:46
【问题描述】:
我有一个 3D 网格,我想做的是绘制我的顶部 2D 面的轮廓,所以在这里我可以访问顶部网格顶点和三角形索引,我想要的是排序顶点以便我可以画出我的轮廓。
List<Vector3> GetSortedVerticesTest(Dictionary<CreateMesh.Faces, MeshData> mesh, Mesh mainMesh)
{
List<Vector3> borderVertices = mesh[CreateMesh.Faces.Top].vertices.Where(p => mesh[CreateMesh.Faces.other].vertices.Any(e => e == p)).ToList();
borderVertices = borderVertices.Distinct().ToList();
List<Vector3> sortedVertices = new List<Vector3>();
List<PointAngle> candidateAngles = new List<PointAngle>();
Vector3 startingPoint = borderVertices[0];
sortedVertices.Add(startingPoint);
borderVertices.Remove(startingPoint);
while (borderVertices.Count > 0)
{
List<Vector3> candidates = new List<Vector3>();
for (int i = 0; i < mesh[CreateMesh.Faces.Top].triangles.Count(); i += 3)
{
bool commonTriangle = false;
// verifie que le vertice courant est compris dans le triangle
for (int k = 0; k < 3; k++)
{
int triIndex = mesh[CreateMesh.Faces.Top].triangles[i + k];
Vector3 posToCompare = mainMesh.vertices[triIndex];
if (startingPoint == posToCompare)
{
commonTriangle = true;
}
}
if (commonTriangle)
{
for (int k = 0; k < 3; k++)
{
int triIndex = mesh[CreateMesh.Faces.Top].triangles[i + k];
Vector3 posToCompare = mainMesh.vertices[triIndex];
if (borderVertices.Contains(posToCompare))//epsilon
candidates.Add(posToCompare);
}
candidates.Remove(startingPoint);
if (candidates.Count != 0)
{
candidates.ForEach(c => candidateAngles.Add(new PointAngle { angle = Vector3.SignedAngle(c - startingPoint, transform.forward, Vector3.up), point = c }));
double min = candidateAngles.Min(kvp => kvp.angle);
var a = candidateAngles[0].point;
PointAngle pointsToKeep = new PointAngle();
pointsToKeep = candidateAngles.Where(s => s.angle == min).First();
sortedVertices.Add(pointsToKeep.point);
borderVertices.Remove(pointsToKeep.point);
print("vert coun" + borderVertices.Count);
startingPoint = pointsToKeep.point;
candidateAngles.Clear();
}
}
}
}
return sortedVertices;
}
我在这里尝试让所有点候选人获得下一个顶点
【问题讨论】:
-
我会假设输出将代表顶部三角形的边界边缘?边缘是否排序是否重要?问题是如何将边列表排序为线段,还是如何生成边列表?
-
输出将是有序的顶点或顶部网格边的列表。
-
按什么顺序?您认为正确排序的顶点数组的标准是什么?还要考虑一个在 X 上旋转约 45° 的网格......现在哪个是“顶部顶点”?
-
我的最终目标是将网格数据导出到 dxf 文件,这样我就需要网格的所有顶点(顶部、底部)按照绘制每个面轮廓的顺序。