public int smoothPath ( SmoothableGraphPath < N , V > path ) { int inputPathLength = path . getCount ( ) ; // If the path is two nodes long or less, then we can't smooth it if ( inputPathLength <= 2 ) return 0 ; // Make sure the ray is instantiated if ( this . ray == null ) { V vec = path . getNodePosition ( 0 ) ; this . ray = new Ray < V > ( vec . cpy ( ) , vec . cpy ( ) ) ; } // Keep track of where we are in the smoothed path. // We start at 1, because we must always include the start node in the smoothed path. int outputIndex = 1 ; // Keep track of where we are in the input path // We start at 2, because we assume two adjacent // nodes will pass the ray cast int inputIndex = 2 ; // Loop until we find the last item in the input while ( inputIndex < inputPathLength ) { // Set the ray ray . start . set ( path . getNodePosition ( outputIndex - 1 ) ) ; ray . end . set ( path . getNodePosition ( inputIndex ) ) ; // Do the ray cast boolean collides = raycastCollisionDetector . collides ( ray ) ; if ( collides ) { // The ray test failed, swap nodes and consider the next output node path . swapNodes ( outputIndex , inputIndex - 1 ) ; outputIndex ++ ; } // Consider the next input node inputIndex ++ ; } // Reached the last input node, always add it to the smoothed path. path . swapNodes ( outputIndex , inputIndex - 1 ) ; path . truncatePath ( outputIndex + 1 ) ; // Return the number of removed nodes return inputIndex - outputIndex - 1 ; }
Smoothes the given path in place .