Guest User

Untitled

a guest
Nov 26th, 2024
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.77 KB | None | 0 0
  1. std::vector<unsigned int> extractBoundaryEdges(const std::vector<unsigned int>& indices) {
  2.      /*
  3.         Count how many times each edge appears
  4.         In a triangular mesh, an edge is either shared between two triangles
  5.         or belongs to the boundary of the mesh  (appearing only once).
  6.        
  7.         By counting occurrences of each edge:
  8.         Edges with a count of 2 are shared edges between adjacent triangles (middle edge).
  9.         Edges with a count of 1 are boundary edges.
  10.     */
  11.     std::unordered_map<std::pair<unsigned int, unsigned int>, int> edgeCount;
  12.    
  13.     // Store boundary edges
  14.     std::vector<unsigned int> edges;
  15.  
  16.     auto addEdge = [&edgeCount](unsigned int v1, unsigned int v2) {
  17.         /*
  18.             Ensure consistent edge ordering. Why?
  19.             In a triangle mesh, an edge can appear in different orders depending on how triangles are defined.
  20.             For example: One triangle might define the edge as (v1, v2).
  21.             An adjacent triangle might define the same edge as (v2, v1).
  22.             If we treat (v1, v2) and (v2, v1) as distinct edges, we would incorrectly count them separately.
  23.             And remember that we needed the counting to detect the shared edge (between adjacent triangles)
  24.         */
  25.         auto edge = std::minmax(v1, v2);
  26.         edgeCount[edge]++;
  27.     };
  28.  
  29.     // Process triangles and count edges
  30.     for (size_t i = 0; i < indices.size(); i += 3) {
  31.         addEdge(indices[i], indices[i + 1]);
  32.         addEdge(indices[i + 1], indices[i + 2]);
  33.         addEdge(indices[i + 2], indices[i]);
  34.     }
  35.  
  36.     // Collect edges that appear only once
  37.     for (const auto& [edge, count] : edgeCount) {
  38.         if (count == 1) {
  39.             edges.push_back(edge.first);
  40.             edges.push_back(edge.second);
  41.         }
  42.     }
  43.  
  44.     return edges;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment