public bool Intersect(
            NuGenRay3D ray, ref double t, ref double u, ref double v, ref NuGenVec3D normal
            )
        {
            NuGenVec3D e1 = p1 - p0;
            NuGenVec3D e2 = p2 - p0;
            NuGenVec3D p = NuGenVec3D.Cross(ray.Direction, e2);

            double a = NuGenVec3D.Dot(e1, p);

            if (a > -NuGenVector.TINY_DOUBLE && a < NuGenVector.TINY_DOUBLE) return false;

            double f = 1.0 / a;
            NuGenVec3D s = ray.Point - p0;
            u = f * NuGenVec3D.Dot(s, p);

            if (u < 0.0 || u > 1.0) return false;
            NuGenVec3D q = NuGenVec3D.Cross(s, e1);
            v = f * NuGenVec3D.Dot(ray.Direction, q);

            if (v < 0.0 || (u + v) > 1.0) return false;
            t = f * NuGenVec3D.Dot(e2, q);
            normal = NuGenVec3D.Cross(e1, e2); normal.Normalize();
            return true;
        }
Example #2
0
        public NuGenRot3D(NuGenVec3D from, NuGenVec3D to)
        {
            from.Normalize();
            to.Normalize();

            double cost =
                NuGenVec3D.Dot(from, to) /
                Math.Sqrt(NuGenVec3D.Dot(from, to) * NuGenVec3D.Dot(to, to));

            if (cost > 0.99999)
            {
                r = 1;
                v = new NuGenVec3D(0, 0, 0);
            }

            else if (cost < -0.99999)
            {
                NuGenVec3D frm = from.Normalized;
                v = NuGenVec3D.Cross(frm, NuGenVec3D.UnitX);

                if (v.Length < 0.00001)
                {
                    v = NuGenVec3D.Cross(frm, NuGenVec3D.UnitY);
                }
                r = 0;
                v.Normalize();
            }

            else
            {
                r  = Math.Sqrt(0.5 * (1.0 + cost));
                v  = NuGenVec3D.Cross(from, to);
                v *= Math.Sqrt((0.5 * (1.0 - cost)) / NuGenVec3D.Dot(v, v));
            }
        }
Example #3
0
        public bool Intersect(NuGenRay3D ray, ref double t, ref NuGenVec3D normal)
        {
            NuGenVec3D l = center - ray.Point;

            double s  = NuGenVec3D.Dot(l, ray.Direction);
            double l2 = l.SquaredLength;
            double rr = radius * radius;

            if (s < 0.0 && l2 > rr)
            {
                return(false);
            }

            double m2 = l2 - s * s;

            if (m2 > rr)
            {
                return(false);
            }

            double q = Math.Sqrt(rr - m2);

            if (l2 > rr)
            {
                t = s - q;
            }

            else
            {
                t = s + q;
            }
            normal = (ray.Point + ray.Direction * t) - center;
            normal.Normalize();
            return(true);
        }
Example #4
0
        public NuGenRot3D(NuGenVec3D from, NuGenVec3D to)
        {
            from.Normalize();
            to.Normalize();

            double cost =
                NuGenVec3D.Dot(from, to) /
                Math.Sqrt(NuGenVec3D.Dot(from, to) * NuGenVec3D.Dot(to, to));

            if (cost > 0.99999)
            {
                r = 1;
                v = new NuGenVec3D(0, 0, 0);
            }

            else if (cost < -0.99999)
            {
                NuGenVec3D frm = from.Normalized;
                v = NuGenVec3D.Cross(frm, NuGenVec3D.UnitX);

                if (v.Length < 0.00001) v = NuGenVec3D.Cross(frm, NuGenVec3D.UnitY);
                r = 0;
                v.Normalize();
            }

            else
            {
                r = Math.Sqrt(0.5 * (1.0 + cost));
                v = NuGenVec3D.Cross(from, to);
                v *= Math.Sqrt((0.5 * (1.0 - cost)) / NuGenVec3D.Dot(v, v));
            }
        }
Example #5
0
        public bool Intersect(NuGenRay3D ray, ref double t, ref NuGenVec3D normal)
        {
            NuGenVec3D l = center - ray.Point;

            double s = NuGenVec3D.Dot(l, ray.Direction);
            double l2 = l.SquaredLength;
            double rr = radius * radius;

            if (s < 0.0 && l2 > rr) return false;

            double m2 = l2 - s * s;

            if (m2 > rr) return false;

            double q = Math.Sqrt(rr - m2);

            if (l2 > rr) t = s - q;

            else t = s + q;
            normal = (ray.Point + ray.Direction * t) - center;
            normal.Normalize();
            return true;
        }
Example #6
0
        public bool Intersect(
            NuGenRay3D ray, ref double t, ref double u, ref double v, ref NuGenVec3D normal
            )
        {
            NuGenVec3D e1 = p1 - p0;
            NuGenVec3D e2 = p2 - p0;
            NuGenVec3D p  = NuGenVec3D.Cross(ray.Direction, e2);

            double a = NuGenVec3D.Dot(e1, p);

            if (a > -NuGenVector.TINY_DOUBLE && a < NuGenVector.TINY_DOUBLE)
            {
                return(false);
            }

            double     f = 1.0 / a;
            NuGenVec3D s = ray.Point - p0;

            u = f * NuGenVec3D.Dot(s, p);

            if (u < 0.0 || u > 1.0)
            {
                return(false);
            }
            NuGenVec3D q = NuGenVec3D.Cross(s, e1);

            v = f * NuGenVec3D.Dot(ray.Direction, q);

            if (v < 0.0 || (u + v) > 1.0)
            {
                return(false);
            }
            t      = f * NuGenVec3D.Dot(e2, q);
            normal = NuGenVec3D.Cross(e1, e2); normal.Normalize();
            return(true);
        }
Example #7
0
 public NuGenRot3D(double radians, NuGenVec3D axis)
 {
     axis.Normalize();
     r = Math.Cos(radians / 2.0);
     v = axis.Normalized * Math.Sin(radians / 2.0);
 }
Example #8
0
 public NuGenRot3D(double radians, NuGenVec3D axis)
 {
     axis.Normalize();
     r = Math.Cos(radians / 2.0);
     v = axis.Normalized * Math.Sin(radians / 2.0);
 }