-- kenb 5/1/01

fn eulerAnglesYZX matrix = 
(
	xyDist = sqrt (matrix.row1.x * matrix.row1.x + matrix.row1.y * matrix.row1.y)
	
--	enough here to get angles?
	if xyDist > 0.001 then
	(
		y = atan2 matrix.row1.y matrix.row1.x
		x = atan2 -matrix.row1.z xyDist
		z = atan2 matrix.row2.z matrix.row3.z
	)
	else	-- forward is mostly Z, gimbal lock-
	(
		y = atan2 -matrix.row2.x matrix.row2.y
		x = atan2 -matrix.row1.z xyDist
		z = 0
	)

	return eulerAngles z x y
)

fn eulerAnglesX matrix =
(
	-- Xp x Xs
	p1 = Point3 0 (-matrix.row1.z) (matrix.row1.y)
	
	-- Zp x Xs
	p2 = Point3 (-matrix.row1.y) (matrix.row1.x) 0
	
--	print matrix
	
	d1 = dot p1 p1
	d2 = dot p2 p2
--	print (normalize p1)
--	print d1 
--	print (normalize p2)
--	print d2
	
	if d1 > d2 then
	(
		p1 = normalize p1
		a = dot p1 matrix.row2
	)
	else
	(
		p2 = normalize p2
		a = dot p2 matrix.row2
	)
--	print a
	b = acos a
	
	-- if b > 90 then b = b - 180
	-- if b < -90 then b = b + 180

--	print b
	return b
)



fn eulerAnglesYY matrix =
(
--	print matrix
	p1 = Point3 0 (matrix.row2.y ) (matrix.row3.y)
	
	p1 = normalize p1

--	print p1	
	a = acos p1.y
--	print a

	return a
)

