- Home /
Sort a list based on edge connections
Context: I'm trying to make a concave mesh slicer. To do this i need to make a polygon of the new vertices on the slicing plane of my sliced mesh. After that I can use multiple algorithms to triangulate the polygon
Problem: For any of these triangulation algorithms to work the sequence/order of the vertices in this polygon must be sorted (clockwise or counter clockwise depending on direction of plane normal).
Right now I am able to build a list of edges (startPosition -> endPosition is always left to right depending on viewing direction). This list however is unordered because there is no way of knowing in which order the trianglesof the original mesh are being sliced.
I am trying to produce a List of polygons that contain a list of edges that is sorted like this: startP1 -> (endP1 = startP2) -> (endP2 = startP3) -> ....
Or basicly make this list: [1 2][3 4][5 6][2 3][4 5] Look like this: [1 2][2 3][3 4][4 5][5 6]
Here is my code to tackle this problem:
public List<Edge> rawEdges = new List<Edge>();
public List<List<Edge>> combinedEdges = new List<List<Edge>>();
// Take the raw edge list and connect the edges.
// If a full circle/polygon is formed but there are still edges left -> build a new polygon
public void CombineEdges()
{
int newComboIndex = 0;
List<Edge> listToEmpy = new List<Edge>(rawEdges);
while(listToEmpy.Count > 0)
{
if (combinedEdges.Count - 1 < newComboIndex)
combinedEdges.Add(new List<Edge>());
Edge firstEdgeToAdd = listToEmpy[0];
Edge nextEdge = firstEdgeToAdd;
Edge previousEdge = firstEdgeToAdd;
bool looped = false;
while (nextEdge != firstEdgeToAdd || !looped)
{
//first time nextedge is always first edge, so the second time they are alike we have looped
looped = true;
combinedEdges[newComboIndex].Add(nextEdge);
listToEmpy.Remove(nextEdge);
// The startingPosition of the next edge to add to the list should be the same as
// the endPosition of the previous edge added to the list
Edge newEdge = listToEmpy.Where(x => x.StartPositon.Equals(nextEdge.EndPosition)).FirstOrDefault();
// If there is no match null will be returned and the circle is closed.
if (newEdge != null) nextEdge = newEdge;
else nextEdge = firstEdgeToAdd;
}
newComboIndex++;
}
Debug.Log("Edges Combined!");
}
For some reason this doesn't work for me and returns an unordered list where the index is also off: I'm slicing a cube here horizontaly
As you can see it returns a list with multiple polygons instead of a list with a single polygon which contains all the edges ordered from startP -> endP -> endP - endP ....
I'm currently only looking for a way to build a list that contains a list of polygons whos edges are sorted like I described above.
Here is some more code to help you get started:
Edge:
public class Edge
{
private Vector3 startPosition, endPosition;
public Vector3 StartPositon { get => startPosition; }
public Vector3 EndPosition { get => endPosition; }
public Edge(Vector3 startPosition, Vector3 endPosition)
{
this.startPosition = startPosition;
this.endPosition = endPosition;
}
}
List of edges:
public List<Edge> rawEdges = new List<Edge>(){new Edge(new Vector3(0.5f, 0.4f, 0.5f), new Vector3(-0.4f, 0.4f, 0.5f)),
new Edge(new Vector3(-0.4f, 0.4f, 0.5f), new Vector3(-0.5f, 0.4f, 0.5f)),
new Edge(new Vector3(0.4f, 0.4f, -0.5f), new Vector3(0.5f, 0.4f, -0.5f)),
new Edge(new Vector3(-0.5f, 0.4f, -0.5f), new Vector3(0.4f, 0.4f, -0.5f)),
new Edge(new Vector3(-0.5f, 0.4f, 0.5f), new Vector3(-0.5f, 0.4f, -0.4f)),
new Edge(new Vector3(-0.5f, 0.4f, -0.4f), new Vector3(-0.5f, 0.4f, -0.5f)),
new Edge(new Vector3(0.5f, 0.4f, -0.5f), new Vector3(0.5f, 0.4f, 0.4f)),
new Edge(new Vector3(0.5f, 0.4f, 0.4f), new Vector3(0.5f, 0.4f, 0.5f)),
};
Answer: So after struggling with this problem for a week I managed to find the answer myself the same day I posted this question.
Here is the edited code of the CombineEdges method:
public void CombineEdges()
{
int newComboIndex = 0;
List<Edge> listToEmpy = new List<Edge>(rawEdges);
while(listToEmpy.Count > 0)
{
if (combinedEdges.Count - 1 < newComboIndex)
combinedEdges.Add(new List<Edge>());
if (combinedEdges[newComboIndex].Count == 0)
{
combinedEdges[newComboIndex].Add(listToEmpy[0]);
listToEmpy.RemoveAt(0);
}
Edge firstEdgeToAdd = combinedEdges[newComboIndex][combinedEdges[newComboIndex].Count - 1];
Edge nextEdge = firstEdgeToAdd;
while (true)
{
bool nextEgdeFound = false;
for (int i = 0; i < listToEmpy.Count; i++)
{
if(nextEdge.EndPosition == listToEmpy[i].StartPositon)
{
nextEgdeFound = true;
combinedEdges[newComboIndex].Add(listToEmpy[i]);
listToEmpy.RemoveAt(i);
nextEdge = combinedEdges[newComboIndex].Last();
}
}
if (!nextEgdeFound)
{
newComboIndex++;
break;
}
}
}
}
The change I made comes down to instead of taking the matching edge out of the listToEmpty and adding it to the new list and removing it from the listToEmpty, and using this edge to find the new matching edge in listToEmpty and so on. I now directly add the matching edge to the new list, remove it from listToEmpty and use the last edge of my new list to match with the corresponding edge in listToEmpty and so on.
Answer by jasperbarg · Oct 15, 2020 at 09:22 PM
Answer:
public void CombineEdges()
{
int newComboIndex = 0;
List<Edge> listToEmpy = new List<Edge>(rawEdges);
while(listToEmpy.Count > 0)
{
if (combinedEdges.Count - 1 < newComboIndex)
combinedEdges.Add(new List<Edge>());
if (combinedEdges[newComboIndex].Count == 0)
{
combinedEdges[newComboIndex].Add(listToEmpy[0]);
listToEmpy.RemoveAt(0);
}
Edge firstEdgeToAdd = combinedEdges[newComboIndex][combinedEdges[newComboIndex].Count - 1];
Edge nextEdge = firstEdgeToAdd;
while (true)
{
bool nextEgdeFound = false;
for (int i = 0; i < listToEmpy.Count; i++)
{
if(nextEdge.EndPosition == listToEmpy[i].StartPositon)
{
nextEgdeFound = true;
combinedEdges[newComboIndex].Add(listToEmpy[i]);
listToEmpy.RemoveAt(i);
nextEdge = combinedEdges[newComboIndex].Last();
}
}
if (!nextEgdeFound)
{
newComboIndex++;
break;
}
}
}
}
Your answer
Follow this Question
Related Questions
Polybrush on runtime 0 Answers
How to manipulate(flatten) the Mesh of an object 0 Answers
Problem with setting vertices positions and smoothing groups. 1 Answer
Mesh Deformation on Rigged character 0 Answers