// =========================================================================== // Copyright 2018 Autodesk, Inc. All rights reserved. // // Use of this software is subject to the terms of the Autodesk license // agreement provided at the time of installation or download, or which // otherwise accompanies this software in either electronic or hard copy form. // =========================================================================== // // // Creation Date: July 30, 1997 // // // // // float [] lineIntersection(float $p1[], float $v1[], // float $p2[], float $v2[]) // // // Returns the intersection point of two 3D lines. Each line is // described by a 3D point and direction. // // // 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 // // // float[] : Intersection point of the lines // // // // float $p1[3] = {0,1,0}; // float $v1[3] = {1,0,0}; // float $p2[3] = {0,0,0}; // float $v2[3] = {0,1,0}; // lineIntersection($p1, $v1, $p2, $v2); // // Results: 0 1 0 // // // // 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((uiRes("m_lineIntersection.kWarningNeed3Values1")) ); if( size($v1) != 3 ) warning((uiRes("m_lineIntersection.kWarningNeed3Values2")) ); if( size($p2) != 3 ) warning((uiRes("m_lineIntersection.kWarningNeed3Values3")) ); if( size($v2) != 3 ) warning((uiRes("m_lineIntersection.kWarningNeed3Values4")) ); // 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; }