Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jasperbarg · Oct 16, 2020 at 09:07 AM · meshesmesh verticesmesh manipulationmesh-deformation

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 alt text

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.

listnotworking.png (6.3 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
                  }
              }
          }
      }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

135 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Dynamically move vertices of a mesh 2 Answers

Line of Sight using Mesh ,Line of Sight Mesh 2 Answers

How can i generate my mesh to get a mesh for 2d light? 0 Answers

Get plain coordinates for another object 2 Answers

can somebody explain to me what is going on in this script ?? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges