public void process ( List < Point2D_I32 > contour , GrowQueue_I32 vertexes , Polygon2D_F64 output ) { int numDecreasing = 0 ; for ( int i = vertexes . size - 1 , j = 0 ; j < vertexes . size ; i = j , j ++ ) { if ( vertexes . get ( i ) > vertexes . get ( j ) ) numDecreasing ++ ; } boolean decreasing = numDecreasing > 1 ; output . vertexes . resize ( vertexes . size ) ; lines . resize ( vertexes . size ) ; // fit lines to each size for ( int i = vertexes . size - 1 , j = 0 ; j < vertexes . size ; i = j , j ++ ) { int idx0 = vertexes . get ( i ) ; int idx1 = vertexes . get ( j ) ; if ( decreasing ) { int tmp = idx0 ; idx0 = idx1 ; idx1 = tmp ; } if ( idx0 > idx1 ) { // handle special case where it wraps around work . clear ( ) ; for ( int k = idx0 ; k < contour . size ( ) ; k ++ ) { work . add ( contour . get ( k ) ) ; } for ( int k = 0 ; k < idx1 ; k ++ ) { work . add ( contour . get ( k ) ) ; } FitLine_I32 . polar ( work , 0 , work . size ( ) , polar ) ; } else { FitLine_I32 . polar ( contour , idx0 , idx1 - idx0 , polar ) ; } UtilLine2D_F64 . convert ( polar , lines . get ( i ) ) ; } // find the corners by intersecting the side for ( int i = vertexes . size - 1 , j = 0 ; j < vertexes . size ; i = j , j ++ ) { LineGeneral2D_F64 lineA = lines . get ( i ) ; LineGeneral2D_F64 lineB = lines . get ( j ) ; Intersection2D_F64 . intersection ( lineA , lineB , output . get ( j ) ) ; } }
Refines the estimate using all the points in the contour