// 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:  July 30, 1997
//<doc>
//<name lineIntersection>
//<owner "Alias Unsupported">
//
//<synopsis>
//		float [] lineIntersection(float $p1[], float $v1[],
//			float $p2[], float $v2[])
//
//<description>
//		Returns the intersection point of two 3D lines.  Each line is
//		described by a 3D point and direction.
//
//<flags>
//		float[]	$p1	Starting point of first line
//		float[]	$v1	Vector direction of first line
//		float[]	$p2	Starting point of second line
//		float[]	$v2	Vector direction of second line
//
//<returns>
//		float[] : Intersection point of the lines
//
//</doc>
//
global proc float [] lineIntersection( 
	float $p1[], float $v1[],	// 1st line, described by a point + direction
	float $p2[], float $v2[])	// 2nd line, described by a point + direction
{
	if( size($p1) != 3 )
		warning("lineIntersection(): You must give a float array of 3 values as the 1st argument\n");
	if( size($v1) != 3 )
		warning("lineIntersection(): You must give a float array of 3 values as the 2nd argument\n");
	if( size($p2) != 3 )
		warning("lineIntersection(): You must give a float array of 3 values as the 3rd argument\n");
	if( size($v2) != 3 )
		warning("lineIntersection(): You must give a float array of 3 values as the 4th argument\n");

	// Ad = A + (B - A).(a - a.b b)/(1 - a.b)2
	// Bd = B + (A - B).(b - a.b a)/(1 - a.b)2

	float $A[3]; float $a[3];
	float $B[3]; float $b[3];
	float $C[3]; float $D[3];
	float $Ad[3]; float $Bd[3];
	copyArray( $p1, $A, 3 ); copyArray( $p2, $B, 3 );
	copyArray( $p1, $A, 3 ); copyArray( $p2, $B, 3 );
	copyArray( $v1, $a, 3 ); copyArray( $v2, $b, 3 );
		
	float $c = dotProduct( $v1, $v2, 0 );
	float $h = 1.0 - $c * $c;

	$D[0]=$a[0]-$c*$b[0]; 
	$D[1]=$a[1]-$c*$b[1]; 
	$D[2]=$a[2]-$c*$b[2];
	$C[0]=$B[0]-$A[0]; 
	$C[1]=$B[1]-$A[1]; 
	$C[2]=$B[2]-$A[2];
	float $s = dotProduct( $C, $D, 0 ) / $h;

	$D[0]=$b[0]-$c*$a[0]; 
	$D[1]=$b[1]-$c*$a[1]; 
	$D[2]=$b[2]-$c*$a[2];
	$C[0]=$A[0]-$B[0]; 
	$C[1]=$A[1]-$B[1]; 
	$C[2]=$A[2]-$B[2];
	float $t = dotProduct( $C, $D, 0 ) / $h;

	$Ad[0]=$A[0]+$s*$a[0]; 
	$Ad[1]=$A[1]+$s*$a[1]; 
	$Ad[2]=$A[2]+$s*$a[2];
	$Bd[0]=$B[0]+$t*$b[0]; 
	$Bd[1]=$B[1]+$t*$b[1]; 
	$Bd[2]=$B[2]+$t*$b[2];

	// Intersection of the two lines is the mid point between Ad and Bd
	//
	float $intersection[3];
	$intersection = midPoint2Pts( $Ad, $Bd );

	return $intersection;
}
