public function testRay(ray : Ray,

transform : Matrix4x4 = null,

maxDistance : Number = Number.POSITIVE_INFINITY) : Boolean

{

var localOrigin : Vector4 = transform

? transform.transformVector(ray.origin, TMP_VECTOR4)

: ray.origin;

var ox : Number = localOrigin.x;

var oy : Number = localOrigin.y;

var oz : Number = localOrigin.z;

var localDirection : Vector4 = transform

? transform.deltaTransformVector(ray.direction, TMP_VECTOR4)

: ray.direction;

localDirection.normalize();

var dx : Number = 1.0 / localDirection.x;

var dy : Number = 1.0 / localDirection.y;

var dz : Number = 1.0 / localDirection.z;

var minX : Number = _min.x;

var minY : Number = _min.y;

var minZ : Number = _min.z;

var maxX : Number = _max.x;

var maxY : Number = _max.y;

var maxZ : Number = _max.z;

var tx1 : Number = (minX - ox) * dx;

var tx2 : Number = (maxX - ox) * dx;

var min : Number = tx1 < tx2 ? tx1 : tx2;

var max : Number = tx1 > tx2 ? tx1 : tx2;

var tmin : Number = min;

var tmax : Number = max;

var ty1 : Number = (minY - oy) * dy;

var ty2 : Number = (maxY - oy) * dy;

min = ty1 < ty2 ? ty1 : ty2;

max = ty1 > ty2 ? ty1 : ty2;

tmin = tmin > min ? tmin : min;

tmax = tmax < max ? tmax : max;

var tz1 : Number = (minZ - oz) * dz;

var tz2 : Number = (maxZ - oz) * dz;

min = tz1 < tz2 ? tz1 : tz2;

max = tz1 > tz2 ? tz1 : tz2;

tmin = tmin > min ? tmin : min;

tmax = tmax < max ? tmax : max;

return tmax >= Math.max(0, tmin) && tmin < maxDistance;

}