public boolean fitAnchored ( int anchor0 , int anchor1 , GrowQueue_I32 corners , GrowQueue_I32 output ) { this . anchor0 = anchor0 ; this . anchor1 = anchor1 ; int numLines = anchor0 == anchor1 ? corners . size ( ) : CircularIndex . distanceP ( anchor0 , anchor1 , corners . size ) ; if ( numLines < 2 ) { throw new RuntimeException ( "The one line is anchored and can't be optimized" ) ; } lines . resize ( numLines ) ; if ( verbose ) System . out . println ( "ENTER FitLinesToContour" ) ; // Check pre-condition // checkDuplicateCorner(corners); workCorners . setTo ( corners ) ; for ( int iteration = 0 ; iteration < maxIterations ; iteration ++ ) { // fit the lines to the contour using only lines between each corner for each line if ( ! fitLinesUsingCorners ( numLines , workCorners ) ) { return false ; } // intersect each line and find the closest point on the contour as the new corner if ( ! linesIntoCorners ( numLines , workCorners ) ) { return false ; } // sanity check to see if corner order is still met if ( ! sanityCheckCornerOrder ( numLines , workCorners ) ) { return false ; // TODO detect and handle this condition better } // TODO check for convergence } if ( verbose ) System . out . println ( "EXIT FitLinesToContour. " + corners . size ( ) + " " + workCorners . size ( ) ) ; output . setTo ( workCorners ) ; return true ; }
Fits line segments along the contour with the first and last corner fixed at the original corners . The output will be a new set of corner indexes . Since the corner list is circular it is assumed that anchor1 comes after anchor0 . The same index can be specified for an anchor it will just go around the entire circle