private boolean canonize ( int index ) { final int count = getPointCount ( ) ; int ix = index ; if ( ix < 0 ) { ix = count + ix ; } if ( ix < 0 ) { throw new IndexOutOfBoundsException ( ix + "<0" ) ; //$NON-NLS-1$ } if ( ix >= count ) { throw new IndexOutOfBoundsException ( ix + ">=" + count ) ; //$NON-NLS-1$ } final int partIndex = groupIndexForPoint ( ix ) ; final int firstPts = firstInGroup ( partIndex ) ; final int endPts = lastInGroup ( partIndex ) ; final int myIndex = ix * 2 ; int firstToRemove = myIndex ; int lastToRemove = myIndex ; boolean removeOne = false ; final double xbase = this . pointCoordinates [ ix * 2 ] ; final double ybase = this . pointCoordinates [ ix * 2 + 1 ] ; final PointFusionValidator validator = getPointFusionValidator ( ) ; // Search for the first point to remove for ( int idx = myIndex - 2 ; idx >= firstPts ; idx -= 2 ) { final double x = this . pointCoordinates [ idx ] ; final double y = this . pointCoordinates [ idx + 1 ] ; if ( validator . isSame ( xbase , ybase , x , y ) ) { firstToRemove = idx ; removeOne = true ; } else { // Stop search break ; } } // Search for the last point to remove for ( int idx = myIndex + 2 ; idx <= endPts ; idx += 2 ) { final double x = this . pointCoordinates [ idx ] ; final double y = this . pointCoordinates [ idx + 1 ] ; if ( validator . isSame ( xbase , ybase , x , y ) ) { lastToRemove = idx ; removeOne = true ; } else { // Stop search break ; } } if ( removeOne ) { // A set of points are detected as too near. // They should be removed and replaced by the reference point final int removalCount = ( lastToRemove / 2 - firstToRemove / 2 ) * 2 ; // Deleting the point final double [ ] newPtsArray = new double [ this . pointCoordinates . length - removalCount ] ; assert newPtsArray . length >= 2 ; System . arraycopy ( this . pointCoordinates , 0 , newPtsArray , 0 , firstToRemove ) ; System . arraycopy ( this . pointCoordinates , lastToRemove + 2 , newPtsArray , firstToRemove + 2 , this . pointCoordinates . length - lastToRemove - 2 ) ; newPtsArray [ firstToRemove ] = xbase ; newPtsArray [ firstToRemove + 1 ] = ybase ; this . pointCoordinates = newPtsArray ; if ( this . partIndexes != null ) { // Shift the group's indexes for ( int i = partIndex ; i < this . partIndexes . length ; ++ i ) { this . partIndexes [ i ] -= removalCount ; } } return true ; } return false ; }
Remove unnecessary points from the specified index .