Iterating Edge Loops

From K-3D

Jump to: navigation, search
Note: This article contains information specific to K-3D prior to version 0.7. This information is obsolete and is retained for historical reference only.

In K-3D, a polygonal face contains a pointer to an edge loop that defines the boundary of the face, plus zero-to-many edge loops that define optional "holes" in the face:

// Simplified for clarity
class face
{
public:
  /// Points to the first edge in the loop of edges that define the polygon
  split_edge* first_edge;
  /// Defines a collection of holes in the polygon face (each element points to the first edge in a loop that defines a hole)
  typedef std::vector<split_edge*> holes_t;
  /// Contains any holes in the polygon face
  holes_t holes;
};

When iterating over an edge loop, it is important to realize that the loop is optional, i.e. that face->first_edge, face->holes[0], etc. may be NULL. The correct way to iterate over an edge loop is thus:

// Do this!
for(split_edge* edge = face->first_edge; edge; edge = edge->face_clockwise)
{
  // Do something with the edge
  if(edge->face_clockwise == face->first_edge)
    break;
}

The *incorrect* way to iterate over an edge loop is:

// DON'T DO THIS
split_edge* edge = face->first_edge;
do
{
  // Do something with the edge
  edge = edge->face_clockwise;
}
while(edge != face->first_edge);

The problem with this loop is that it makes two incorrect assumptions:

  • It assumes that face->first_edge is never NULL. This is wrong.
  • It assumes that edge->face_clockwise is never NULL. This is also wrong.
Personal tools