void disconnectInvalidVertices ( ) { // add elements with 1 or 2 edges openVertexes . clear ( ) ; for ( int idxVert = 0 ; idxVert < vertexes . size ; idxVert ++ ) { Vertex n = vertexes . get ( idxVert ) ; if ( n . connections . size ( ) == 1 || n . connections . size ( ) == 2 ) { openVertexes . add ( n ) ; } } // continue until there are no changes while ( ! openVertexes . isEmpty ( ) ) { dirtyVertexes . clear ( ) ; for ( int idxVert = 0 ; idxVert < openVertexes . size ( ) ; idxVert ++ ) { boolean remove = false ; Vertex n = openVertexes . get ( idxVert ) ; if ( n . connections . size ( ) == 1 ) { remove = true ; } else if ( n . connections . size ( ) == 2 ) { Edge ea = n . connections . get ( 0 ) ; Edge eb = n . connections . get ( 1 ) ; // Look for a common vertex that isn't 'n' remove = true ; for ( int i = 0 ; i < ea . dst . connections . size ( ) ; i ++ ) { Vertex va = ea . dst . connections . get ( i ) . dst ; if ( va == n ) continue ; for ( int j = 0 ; j < eb . dst . connections . size ( ) ; j ++ ) { Vertex vb = ea . dst . connections . get ( j ) . dst ; if ( va == vb ) { remove = false ; break ; } } } } if ( remove ) { // only go through the subset referenced the disconnected. Yes there could be duplicates // not worth the time to fix that for ( int i = 0 ; i < n . connections . size ( ) ; i ++ ) { dirtyVertexes . add ( n . connections . get ( i ) . dst ) ; } removeReferences ( n , EdgeType . CONNECTION ) ; } } openVertexes . clear ( ) ; openVertexes . addAll ( dirtyVertexes ) ; } }
Disconnect the edges from invalid vertices . Invalid ones have only one edge or two edges which do not connect to a common vertex i . e . they are point 180 degrees away from each other .