// Copyright (C) 1997-2004 Alias Systems Corp.
// 
// The information in this file is provided for the exclusive use of the
// licensees of Alias.  Such users have the right to use, modify,
// and incorporate this code into other products for purposes authorized
// by the Alias license agreement, without fee.
// 
// ALIAS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
// INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
// EVENT SHALL ALIAS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
// DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

//
//  Alias Script File
//  MODIFY THIS AT YOUR OWN RISK
//
//  Creation Date:  30 January 2001
//
//  Description:
//      Script to compute the best tessellation settings.
//

proc float min(float $a, float $b)
{
    if ($a < $b)
        return $a;
    else
        return $b;
}
    
//
//  Procedure Name:
//      runupTessellation
//
//  Description:
//      Procedure to runup the tessellation for each frame specified by
//      render globals and compute the nurb surface chord height based
//      on the camera projection.
//
global proc runupTessellation(string $camera, float $start, float $end, float $by, string $surfaces[])
{
    float $ftols[];
    
    int $isFtolsInitialized = false;

	// setup progress bar
	global string $gMainProgressBar;
	int $numFrames = ($end - $start + 1) / $by;
	progressBar -edit
		-beginProgress
        -isInterruptable true
        -status "Evaluating Tessellation ..."
        -maxValue $numFrames
        $gMainProgressBar;

    for ($i = $start; $i <= $end; $i += $by) {
		if (`progressBar -query -isCancelled $gMainProgressBar`) break;
		progressBar -edit -step 1 $gMainProgressBar;

        currentTime -edit $i;

        if ($camera != "-allCameras")
            convertTessellation -camera $camera $surfaces;
        else
            convertTessellation -allCameras $surfaces;

        if ($isFtolsInitialized) {
            float $newFtol;

            for ($j = 0; $j < size($surfaces); $j = $j + 1) {
                $newFtol = `getAttr ($surfaces[$j]+".chordHeight")`;
                $ftols[$j] = min($newFtol, $ftols[$j]);
            }
        }
        else {
            for ($j = 0; $j < size($surfaces); $j = $j + 1) {
                $ftols[$j] = `getAttr ($surfaces[$j]+".chordHeight")`;
            }

            $isFtolsInitialized = true;
        }
    }

	progressBar -edit
		-endProgress
		$gMainProgressBar;

    if ($isFtolsInitialized) {
        for ($j = 0; $j < size($surfaces); $j = $j + 1) {
            setAttr ($surfaces[$j]+".chordHeight") $ftols[$j];
        }
    }
}
