void selectConnections ( Vertex target ) { // There needs to be at least two corners if ( target . perpendicular . size ( ) <= 1 ) return ; // if( target.index == 16 ) { // System.out.println("ASDSAD"); // } // System.out.println("======= Connecting "+target.index); double bestError = Double . MAX_VALUE ; List < Edge > bestSolution = target . connections . edges ; // Greedily select one vertex at a time to connect to. findNext() looks at all possible // vertexes it can connect too and minimizes an error function based on projectively invariant features for ( int i = 0 ; i < target . perpendicular . size ( ) ; i ++ ) { Edge e = target . perpendicular . get ( i ) ; solution . clear ( ) ; solution . add ( e ) ; double sumDistance = solution . get ( 0 ) . distance ; double minDistance = sumDistance ; if ( ! findNext ( i , target . parallel , target . perpendicular , Double . NaN , results ) ) { continue ; } solution . add ( target . perpendicular . get ( results . index ) ) ; sumDistance += solution . get ( 1 ) . distance ; minDistance = Math . min ( minDistance , solution . get ( 1 ) . distance ) ; // Use knowledge that solution[0] and solution[2] form a line. // Lines are straight under projective distortion if ( findNext ( results . index , target . parallel , target . perpendicular , solution . get ( 0 ) . direction , results ) ) { solution . add ( target . perpendicular . get ( results . index ) ) ; sumDistance += solution . get ( 2 ) . distance ; minDistance = Math . min ( minDistance , solution . get ( 2 ) . distance ) ; // Use knowledge that solution[1] and solution[3] form a line. if ( findNext ( results . index , target . parallel , target . perpendicular , solution . get ( 1 ) . direction , results ) ) { solution . add ( target . perpendicular . get ( results . index ) ) ; sumDistance += solution . get ( 3 ) . distance ; minDistance = Math . min ( minDistance , solution . get ( 3 ) . distance ) ; } } // Prefer closer valid sets of edges and larger sets. // the extra + minDistance was needed to bias it against smaller sets which would be more likely // to have a smaller average error. Division by the square of set/solution size biases it towards larger sets double error = ( sumDistance + minDistance ) / ( solution . size ( ) * solution . size ( ) ) ; // System.out.println(" first="+solution.get(0).dst.index+" size="+solution.size()+" error="+error); if ( error < bestError ) { bestError = error ; bestSolution . clear ( ) ; bestSolution . addAll ( solution ) ; } } }
Select the best 2 3 or 4 perpendicular vertexes to connect to . These are the output grid connections .