// 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 attachCurveTangent() procedure takes the selected two curves and 
//		creates a one span curve between them that is tangent to 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 attachCurveTangent( 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 2 cvs on the first curve and the first 2 cvs on the 
	// second curve
    //
	int $degree = eval("getAttr " + $curvesList[0] + ".degree");
	int $numSpans = eval("getAttr " + $curvesList[0] + ".spans");
    int $numCVs1 = $degree + $numSpans;
	$degree = eval("getAttr " + $curvesList[1] + ".degree");
	$numSpans = eval("getAttr " + $curvesList[1] + ".spans");
    int $numCVs2 = $degree + $numSpans;
    if ( $numCVs1 < 2 || $numCVs2 < 2 )
	{
		error("Each selected curve must have at least two cvs.");
		return "";
	}

    int $lastCV = $numCVs1 - 1;
    float $cvLastCurve1[] = `getAttr ($curvesList[0] + ".cp[" + $lastCV + "]")`;
	$lastCV--;
    float $cv2ndLastCurve1[] = `getAttr ($curvesList[0] + ".cp[" + $lastCV + "]")`;
    float $cvOneCurve2[] = `getAttr ($curvesList[1] + ".cp[0]")`;
    float $cvTwoCurve2[] = `getAttr ($curvesList[1] + ".cp[1]")`;

    // this is the vector between the two cvs on the first curve
    //
    float $vector[3];
    $vector[0] = $cvLastCurve1[0] - $cv2ndLastCurve1[0];
    $vector[1] = $cvLastCurve1[1] - $cv2ndLastCurve1[1];
    $vector[2] = $cvLastCurve1[2] - $cv2ndLastCurve1[2];

    // calculate a cv on the line between the two end cvs so that it will 
	// be tangent
    //
    float $cvNew1[3];
    $cvNew1[0] = $cvLastCurve1[0] + $vector[0];
    $cvNew1[1] = $cvLastCurve1[1] + $vector[1];
    $cvNew1[2] = $cvLastCurve1[2] + $vector[2];
	
    // this is the vector between the two cvs on the second curve
    //
    float $vector[3];
    $vector[0] = $cvOneCurve2[0] - $cvTwoCurve2[0];
    $vector[1] = $cvOneCurve2[1] - $cvTwoCurve2[1];
    $vector[2] = $cvOneCurve2[2] - $cvTwoCurve2[2];

    // calculate a cv on the line between the two start cvs so that it will 
	// be tangent
    //
    float $cvNew2[3];
    $cvNew2[0] = $cvOneCurve2[0] + $vector[0];
    $cvNew2[1] = $cvOneCurve2[1] + $vector[1];
    $cvNew2[2] = $cvOneCurve2[2] + $vector[2];

    // create the degree 3 curve
    //
    string $resultCurve = eval("curve -p " + $cvLastCurve1[0] + " " + $cvLastCurve1[1] + " " + $cvLastCurve1[2] + " -p " + $cvNew1[0] + " " + $cvNew1[1] + " " + $cvNew1[2] + " -p " + $cvNew2[0] + " " + $cvNew2[1] + " " + $cvNew2[2] + " -p " + $cvOneCurve2[0] + " " + $cvOneCurve2[1] + " " + $cvOneCurve2[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 tangent 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;
}
