// =========================================================================== // 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 17, 1998 // global proc xyzRotation( float $theta, float $axis[], float $rotation[] ) // // Description: // Given an angle for rotation (in radians) and an axis about which to // do the rotation, return the rotation about the X,Y,Z axes (in $rotation). // { // set up the xyzw quaternion values // $theta *= 0.5; float $w = cos($theta); float $factor = sin($theta); float $axisLen2 = dotProduct( $axis, $axis, 0 ); if ( $axisLen2 != 1.0 && $axisLen2 != 0.0 ) $factor /= sqrt($axisLen2); float $x = $factor * $axis[0]; float $y = $factor * $axis[1]; float $z = $factor * $axis[2]; // setup rotation in a matrix // float $matrix[]; float $ww = $w*$w; float $xx = $x*$x; float $yy = $y*$y; float $zz = $z*$z; float $s = 2.0 / ($ww + $xx + $yy + $zz); float $xy = $x*$y; float $xz = $x*$z; float $yz = $y*$z; float $wx = $w*$x; float $wy = $w*$y; float $wz = $w*$z; $matrix[0] = 1.0 - $s * ($yy + $zz); $matrix[1] = $s * ($xy + $wz); $matrix[2] = $s * ($xz - $wy); $matrix[5] = 1.0 - $s * ($xx + $zz); $matrix[6] = $s * ($yz + $wx); $matrix[9] = $s * ($yz - $wx); $matrix[10] = 1.0 - $s * ($xx + $yy); // get x,y,z values for rotation // float $solution1[]; float $solution2[]; float $cosB = sqrt($matrix[0]*$matrix[0] + $matrix[1]*$matrix[1]); if ( $cosB > 1.0e-10 ) { float $a, $b, $c; float $pi = 3.14159265; $solution1[0] = $a = atan2( $matrix[6], $matrix[10]); $solution1[1] = $b = atan2(-$matrix[2], $cosB); $solution1[2] = $c = atan2( $matrix[1], $matrix[0]); $solution2[0] = $a + (($a < $pi) ? $pi : -$pi); $solution2[1] = (($b > -$pi) ? $pi : -$pi) - $b; $solution2[2] = $c + (($c < $pi) ? $pi : -$pi); if ( abs($solution2[0]) + abs($solution2[1]) + abs($solution2[2]) < abs($solution1[0]) + abs($solution1[1]) + abs($solution1[2]) ) { $rotation = $solution2; } else { $rotation = $solution1; } } else { $rotation[0] = atan2(-$matrix[9], $matrix[5]); $rotation[1] = atan2(-$matrix[2], $cosB); $rotation[2] = 0.0; } }