// 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:  Jan 12, 1998
//
//  Description:
//      The attachCurveLine() procedure takes the selected two curves and 
//		creates a one span straight curve between them, using the end of curve1
//      and the start of curve2. The result will always be at least a degree
//      3 curve. If you don't want to attach the curves then call this proc
//      with $doAttach = 0.
//
//  Input Arguments:
//      $doAttach - if 1 attach results otherwise just create new curve
//
//  Return Value:
//      String.
//

global proc string attachCurveLine( int $doAttach )
{
	global int $gSelectNurbsCurvesBit;
	string $curvesList[] = `filterExpand -ex true -sm $gSelectNurbsCurvesBit`;

	int $numCurves = size($curvesList);
	if ( $numCurves < 2 )
	{
		error("Need to select two curves.");
		return "";
	}

    // get the last cv on the first curve and the first cv on the second curve
    //
	int $degree = eval("getAttr " + $curvesList[0] + ".degree");
	int $numSpans = eval("getAttr " + $curvesList[0] + ".spans");
    int $numCVs = $degree + $numSpans;

    int $lastCV = $numCVs - 1;
    float $cvCurve1[] = `getAttr ($curvesList[0] + ".cp[" + $lastCV + "]")`;
    float $cvCurve2[] = `getAttr ($curvesList[1] + ".cp[0]")`;

    // this is the vector between the two cvs
    //
    float $vector[3];
    $vector[0] = $cvCurve2[0] - $cvCurve1[0];
    $vector[1] = $cvCurve2[1] - $cvCurve1[1];
    $vector[2] = $cvCurve2[2] - $cvCurve1[2];

    // calculate two other cvs on the line between the end cvs
    //
    float $cvNew1[3];
    $cvNew1[0] = $cvCurve1[0] + ($vector[0] * 0.333);
    $cvNew1[1] = $cvCurve1[1] + ($vector[1] * 0.333);
    $cvNew1[2] = $cvCurve1[2] + ($vector[2] * 0.333);
    float $cvNew2[3];
    $cvNew2[0] = $cvCurve1[0] + ($vector[0] * 0.666);
    $cvNew2[1] = $cvCurve1[1] + ($vector[1] * 0.666);
    $cvNew2[2] = $cvCurve1[2] + ($vector[2] * 0.666);

    // create the degree 3 curve
    //
    string $resultCurve = eval("curve -p " + $cvCurve1[0] + " " + $cvCurve1[1] + " " + $cvCurve1[2] + " -p " + $cvNew1[0] + " " + $cvNew1[1] + " " + $cvNew1[2] + " -p " + $cvNew2[0] + " " + $cvNew2[1] + " " + $cvNew2[2] + " -p " + $cvCurve2[0] + " " + $cvCurve2[1] + " " + $cvCurve2[2] + " -k 0 -k 0 -k 0 -k 1 -k 1 -k 1");

    // attach all 3 curves if required
    //
    if ( $doAttach == 1 )
    {
        // attach curve1 to the new curve
        //
        string $attachedCurve[] = eval("attachCurve -ch 0 -rpo 0 -kmk 1 " + $curvesList[0] + " " + $resultCurve);

        // the straight curve is no longer needed
        //
        delete $resultCurve;

        // attach curve2 to the previously attached curves
        //
        $attachedCurve = eval("attachCurve -ch 0 -rpo 1 -kmk 1 " + $attachedCurve[0] + " " + $curvesList[1]);
        $resultCurve = $attachedCurve[0];
    }

    select -r $resultCurve;
    return $resultCurve;
}
