//
// Copyright (C) 1997-1998 Alias|Wavefront,
// a division of Silicon Graphics Limited.
//
// The information in this file is provided for the exclusive use of the
// licensees of Alias|Wavefront.  Such users have the right to use, modify,
// and incorporate this code into other products for purposes authorized
// by the Alias|Wavefront license agreement, without fee.
//
// ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
// INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
// EVENT SHALL ALIAS|WAVEFRONT 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|Wavefront Script File
// MODIFY THIS AT YOUR OWN RISK
//
////
//	Description:
//		Given a surface, this proc produces cross sections with the
//		given spacing along the specified axis.  
//		The resulting cross sections are 3D NURBS curves.
//		Offset is how far from the edge of the bounding box to start intersecting.
//		The axis should be given as 0, 1 or 2, for x, y, z respectively.
//
//	Method: 
//		A series of 1x1 degree NURBS planes are created, and intersected
//		with the given surface.
//
//	Returns:
//		A string array, where the first string is the name ofthe group of
//		resulting intersect curves.
//
//	Example:
//		To create cross sections on a surface in the Y direction,
//		where the sections are 2.2 units apart and offset by 1.5 units:
//			crossSection( "surface1", 1, 2.2, 1.5 );
//

proc int getWorldSpaceBoundingBox( 
	string $surfaceShape, float $min[], float $max[], 
	float $center[], float $dimensions[] )
{
	string $surfaceSh = `createNode nurbsSurface`;
	string $surfaceCopy = `createNode transform`;
	string $xformNode = `createNode transformGeometry`;
	// Need to use the -s, to parent a shape to a transform
	// (instead of a transform to a transform) - Saty
	parent -s $surfaceSh $surfaceCopy;
	connectAttr ($surfaceShape + ".local") ($xformNode + ".inputGeometry");
	connectAttr ($surfaceShape + ".worldMatrix") ($xformNode + ".transform");
	connectAttr ($xformNode + ".outputGeometry") ($surfaceSh + ".create");

	$min = `getAttr ($surfaceCopy + ".boundingBoxMin")`;
	$max = `getAttr ($surfaceCopy + ".boundingBoxMax")`;
	$dimensions = `getAttr ($surfaceCopy + ".boundingBoxSize")`;
	$center = `getAttr ($surfaceCopy + ".center")`;
	delete $surfaceCopy;

	return 0;
}

global proc string[] crossSection( string $surface, int $axis, 
	float $spacing, float $offset )

{
	string $results[];

	// Check that the axis is valid.	
	//
	if( $axis != 0 && $axis != 1 && $axis != 2 )  {
		error("You must specify axis 0, 1 or 2 for x, y, z\n");
		return $results;
	}
	if( size(`ls $surface`) != 1 ) {
		error(" You must specify the name of a surface\n");
		return $results;
	}
	string $surfaceShape[] = `listRelatives -shapes $surface`;
	if( size($surfaceShape) != 1 ) {
		error(" You must specify a transform above a single NURBS surface\n");
		return $results;
	}

	string $planes[];
	string $intersectCrvs[];
	string $intersectDNs[];

	// Get the bounding box from the surface, incorporate the spacing + offset
	//
	float $bboxMin[3];
	float $bboxMax[3];
	float $bboxSize[3];
	float $bboxCen[3];
	int $tmp = getWorldSpaceBoundingBox( $surfaceShape[0], $bboxMin, 
				$bboxMax, $bboxCen, $bboxSize );
	float $min = $bboxMin[$axis];
	float $max = $bboxMax[$axis];
	int $ax[3];
	$ax[0] = ($axis == 0) ? 1 : 0;
	$ax[1] = ($axis == 1) ? 1 : 0;
	$ax[2] = ($axis == 2) ? 1 : 0;
	float $pos[3];
	$pos[0] = ($axis == 0) ? $bboxMin[0] + $offset: $bboxCen[0];
	$pos[1] = ($axis == 1) ? $bboxMin[1] + $offset: $bboxCen[1];
	$pos[2] = ($axis == 2) ? $bboxMin[2] + $offset: $bboxCen[2];

	float $width = ($axis==0) ? $bboxSize[2] : 
					(($axis==1) ? $bboxSize[0] : $bboxSize[0]) ;
	float $lr = ($axis==0) ? $bboxSize[1]/$bboxSize[2] : 
				(($axis==1) ? $bboxSize[2]/$bboxSize[0] : 
				$bboxSize[1]/$bboxSize[0]) ;

	// Create nurbs planes in the specified direction, between min and max,
	// intersect with the surface.  
	//
	for( ; $pos[$axis] < $max; ) {

		string $plane[] = `nurbsPlane -d 1 -ch off -ax $ax[0] $ax[1] $ax[2] 
			-p $pos[0] $pos[1] $pos[2] -w $width -lr $lr`;
		$planes[size($planes)] = $plane[0];

		string $intersect[] = `intersect -ch on -cos off $plane[0] $surface`;
		$intersectCrvs[size($intersectCrvs)] = $intersect[0];

		$pos[0] += ($axis == 0) ? $spacing : 0.0;
		$pos[1] += ($axis == 1) ? $spacing : 0.0;
		$pos[2] += ($axis == 2) ? $spacing : 0.0;
	}

	// delete the intersecting planes and group the result curves.
	//
	// Actually, don't do this.. - Saty
	// delete $planes;
	string $intersectCrvsGrp = `group $intersectCrvs`;
	$results[0] = $intersectCrvsGrp;

	return $results;
}
