Exemple #1
0
        // NOTE: This function doesn't calculate the normal because it's easily derived for a sphere (p - center).
        public bool IntersectRaySphere(RayD ray, out double tmin, out double tmax)
        {
            tmin = 0;
            tmax = 0;
            Vector3D CO = ray.Position - Center;

            double a = ray.Direction.Dot(ray.Direction);
            double b = 2.0f * CO.Dot(ray.Direction);
            double c = CO.Dot(CO) - (Radius * Radius);

            double discriminant = b * b - 4.0f * a * c;

            if (discriminant < 0.0f)
            {
                return(false);
            }

            tmin = (-b - Math.Sqrt(discriminant)) / (2.0 * a);
            tmax = (-b + Math.Sqrt(discriminant)) / (2.0 * a);
            if (tmin > tmax)
            {
                double temp = tmin;
                tmin = tmax;
                tmax = temp;
            }

            return(true);
        }
        public bool Intersect(LineD line, ref Vector3D p1, ref Vector3D p2, ref Vector3 n1, ref Vector3 n2)
        {
            RayD ray = new RayD(line.From, line.Direction);

            if (Intersect(ray, ref p1, ref p2, ref n1, ref n2))
            {
                Vector3D p1Dir = p1 - line.From;
                Vector3D p2Dir = p2 - line.From;
                double   p1Len = p1Dir.Normalize();
                double   p2Len = p2Dir.Normalize();

                if (Vector3D.Dot(line.Direction, p1Dir) < 0.9)
                {
                    return(false);
                }

                if (Vector3D.Dot(line.Direction, p2Dir) < 0.9)
                {
                    return(false);
                }

                if (line.Length < p1Len)
                {
                    return(false);
                }

                return(true);
            }

            return(false);
        }
Exemple #3
0
        /// <summary>
        /// Checks whether the current BoundingFrustumD intersects the specified Ray.
        /// </summary>
        /// <param name="ray">The Ray to check for intersection.</param>
        public double?Intersects(RayD ray)
        {
            double?result;

            this.Intersects(ref ray, out result);
            return(result);
        }
Exemple #4
0
        public double?Intersects(RayD ray)
        {
            double?nullable;

            this.Intersects(ref ray, out nullable);
            return(nullable);
        }
        /// <summary>
        /// Check intersection between line and bounding sphere
        /// We don't use BoundingSphere.Contains(Ray ...) because ray doesn't have an end, but line does, so we need
        /// to check if line really intersects the sphere.
        /// </summary>
        public static bool IsLineIntersectingBoundingSphere(ref LineD line, ref BoundingSphereD boundingSphere)
        {
            //  Create temporary ray and do intersection. But we can't rely only on it, because ray doesn't have end, yet our line does, so we
            //  need to check if ray-bounding_sphere intersection lies in the range of our line
            VRageMath.RayD ray = new VRageMath.RayD(line.From, line.Direction);
            double?        intersectionDistance = boundingSphere.Intersects(ray);

            if (intersectionDistance.HasValue == false)
            {
                //  No intersection between ray/line and bounding sphere
                return(false);
            }
            else
            {
                if (intersectionDistance.Value <= line.Length)
                {
                    //  Intersection between ray/line and bounding sphere IS withing the range of the line
                    return(true);
                }
                else
                {
                    //  Intersection between ray/line and bounding sphere IS NOT withing the range of the line
                    return(false);
                }
            }
        }
        public bool Intersect(LineD line, ref Vector3D p1, ref Vector3D p2, ref Vector3 n1, ref Vector3 n2)
        {
            RayD ray = new RayD(line.From, line.Direction);

            if (!this.Intersect(ray, ref p1, ref p2, ref n1, ref n2))
            {
                return(false);
            }
            Vector3D vectord  = p1 - line.From;
            Vector3D vectord2 = p2 - line.From;
            double   num      = vectord.Normalize();

            vectord2.Normalize();
            if (Vector3D.Dot(line.Direction, vectord) < 0.9)
            {
                return(false);
            }
            if (Vector3D.Dot(line.Direction, vectord2) < 0.9)
            {
                return(false);
            }
            if (line.Length < num)
            {
                return(false);
            }
            return(true);
        }
        public void OverlapAllLineSegment <T>(ref LineD line, List <MyLineSegmentOverlapResult <T> > elementsList, uint requiredFlags)
        {
            elementsList.Clear();

            if (_root == NullNode)
            {
                return;
            }

            using (m_rwLock.AcquireSharedUsing())
            {
                Stack <int> stack = GetStack();
                stack.Push(_root);

                BoundingBoxD bbox = BoundingBoxD.CreateInvalid();
                bbox.Include(ref line);

                var ray = new RayD(line.From, line.Direction);

                while (stack.Count > 0)
                {
                    int nodeId = stack.Pop();
                    if (nodeId == NullNode)
                    {
                        continue;
                    }

                    DynamicTreeNode node = _nodes[nodeId];

                    if (node.Aabb.Intersects(bbox))
                    {
                        double?distance = node.Aabb.Intersects(ray);
                        if (distance.HasValue && distance.Value <= line.Length && distance.Value >= 0)
                        {
                            if (node.IsLeaf())
                            {
                                uint flags = GetUserFlag(nodeId);
                                if ((flags & requiredFlags) == requiredFlags)
                                {
                                    elementsList.Add(new MyLineSegmentOverlapResult <T>
                                    {
                                        Element  = GetUserData <T>(nodeId),
                                        Distance = distance.Value
                                    });
                                }
                            }
                            else
                            {
                                stack.Push(node.Child1);
                                stack.Push(node.Child2);
                            }
                        }
                    }
                }

                PushStack(stack);
            }
        }
Exemple #8
0
        private static RayD ComputeIntersectionLine(ref PlaneD p1, ref PlaneD p2)
        {
            RayD ray = new RayD();

            ray.Direction = Vector3D.Cross(p1.Normal, p2.Normal);
            double num = ray.Direction.LengthSquared();

            ray.Position = Vector3D.Cross(-p1.D * p2.Normal + p2.D * p1.Normal, ray.Direction) / num;
            return(ray);
        }
Exemple #9
0
        private static RayD ComputeIntersectionLine(ref PlaneD p1, ref PlaneD p2)
        {
            RayD yd = new RayD {
                Direction = Vector3D.Cross(p1.Normal, p2.Normal)
            };
            double num = yd.Direction.LengthSquared();

            yd.Position = (Vector3D)(Vector3D.Cross((Vector3D)((-p1.D * p2.Normal) + (p2.D * p1.Normal)), yd.Direction) / num);
            return(yd);
        }
Exemple #10
0
        public void BoundingBoxIntersectKeen()
        {
            var point  = new VRageMath.Vector3D(5d, 3.5d, 4d);
            var vector = new VRageMath.Vector3D(-0.03598167d, 0.0110336d, 0.9992915d);
            var box    = new VRageMath.BoundingBoxD(new VRageMath.Vector3D(3d, 3d, 2d), new VRageMath.Vector3D(7d, 4d, 6d));
            var ray    = new VRageMath.RayD(point, vector);

            double?f = box.Intersects(ray);

            Assert.AreEqual(0, f, "Should Equal");
        }
Exemple #11
0
        private void SetMatrix(ref MatrixD value)
        {
            this.matrix             = value;
            this.planes[2].Normal.X = -value.M14 - value.M11;
            this.planes[2].Normal.Y = -value.M24 - value.M21;
            this.planes[2].Normal.Z = -value.M34 - value.M31;
            this.planes[2].D        = -value.M44 - value.M41;
            this.planes[3].Normal.X = -value.M14 + value.M11;
            this.planes[3].Normal.Y = -value.M24 + value.M21;
            this.planes[3].Normal.Z = -value.M34 + value.M31;
            this.planes[3].D        = -value.M44 + value.M41;
            this.planes[4].Normal.X = -value.M14 + value.M12;
            this.planes[4].Normal.Y = -value.M24 + value.M22;
            this.planes[4].Normal.Z = -value.M34 + value.M32;
            this.planes[4].D        = -value.M44 + value.M42;
            this.planes[5].Normal.X = -value.M14 - value.M12;
            this.planes[5].Normal.Y = -value.M24 - value.M22;
            this.planes[5].Normal.Z = -value.M34 - value.M32;
            this.planes[5].D        = -value.M44 - value.M42;
            this.planes[0].Normal.X = -value.M13;
            this.planes[0].Normal.Y = -value.M23;
            this.planes[0].Normal.Z = -value.M33;
            this.planes[0].D        = -value.M43;
            this.planes[1].Normal.X = -value.M14 + value.M13;
            this.planes[1].Normal.Y = -value.M24 + value.M23;
            this.planes[1].Normal.Z = -value.M34 + value.M33;
            this.planes[1].D        = -value.M44 + value.M43;
            for (int index = 0; index < 6; ++index)
            {
                double num = this.planes[index].Normal.Length();
                this.planes[index].Normal = this.planes[index].Normal / num;
                this.planes[index].D      = this.planes[index].D / num;
            }
            RayD intersectionLine1 = BoundingFrustumD.ComputeIntersectionLine(ref this.planes[0], ref this.planes[2]);

            this.cornerArray[0] = BoundingFrustumD.ComputeIntersection(ref this.planes[4], ref intersectionLine1);
            this.cornerArray[3] = BoundingFrustumD.ComputeIntersection(ref this.planes[5], ref intersectionLine1);
            RayD intersectionLine2 = BoundingFrustumD.ComputeIntersectionLine(ref this.planes[3], ref this.planes[0]);

            this.cornerArray[1] = BoundingFrustumD.ComputeIntersection(ref this.planes[4], ref intersectionLine2);
            this.cornerArray[2] = BoundingFrustumD.ComputeIntersection(ref this.planes[5], ref intersectionLine2);
            intersectionLine2   = BoundingFrustumD.ComputeIntersectionLine(ref this.planes[2], ref this.planes[1]);
            this.cornerArray[4] = BoundingFrustumD.ComputeIntersection(ref this.planes[4], ref intersectionLine2);
            this.cornerArray[7] = BoundingFrustumD.ComputeIntersection(ref this.planes[5], ref intersectionLine2);
            intersectionLine2   = BoundingFrustumD.ComputeIntersectionLine(ref this.planes[1], ref this.planes[3]);
            this.cornerArray[5] = BoundingFrustumD.ComputeIntersection(ref this.planes[4], ref intersectionLine2);
            this.cornerArray[6] = BoundingFrustumD.ComputeIntersection(ref this.planes[5], ref intersectionLine2);
        }
Exemple #12
0
        private void SetMatrix(ref MatrixD value)
        {
            this.matrix             = value;
            this.planes[2].Normal.X = -value.M14 - value.M11;
            this.planes[2].Normal.Y = -value.M24 - value.M21;
            this.planes[2].Normal.Z = -value.M34 - value.M31;
            this.planes[2].D        = -value.M44 - value.M41;
            this.planes[3].Normal.X = -value.M14 + value.M11;
            this.planes[3].Normal.Y = -value.M24 + value.M21;
            this.planes[3].Normal.Z = -value.M34 + value.M31;
            this.planes[3].D        = -value.M44 + value.M41;
            this.planes[4].Normal.X = -value.M14 + value.M12;
            this.planes[4].Normal.Y = -value.M24 + value.M22;
            this.planes[4].Normal.Z = -value.M34 + value.M32;
            this.planes[4].D        = -value.M44 + value.M42;
            this.planes[5].Normal.X = -value.M14 - value.M12;
            this.planes[5].Normal.Y = -value.M24 - value.M22;
            this.planes[5].Normal.Z = -value.M34 - value.M32;
            this.planes[5].D        = -value.M44 - value.M42;
            this.planes[0].Normal.X = -value.M13;
            this.planes[0].Normal.Y = -value.M23;
            this.planes[0].Normal.Z = -value.M33;
            this.planes[0].D        = -value.M43;
            this.planes[1].Normal.X = -value.M14 + value.M13;
            this.planes[1].Normal.Y = -value.M24 + value.M23;
            this.planes[1].Normal.Z = -value.M34 + value.M33;
            this.planes[1].D        = -value.M44 + value.M43;
            for (int i = 0; i < 6; i++)
            {
                double num2 = this.planes[i].Normal.Length();
                this.planes[i].Normal = (Vector3D)(this.planes[i].Normal / num2);
                this.planes[i].D     /= num2;
            }
            RayD ray = ComputeIntersectionLine(ref this.planes[0], ref this.planes[2]);

            this.cornerArray[0] = ComputeIntersection(ref this.planes[4], ref ray);
            this.cornerArray[3] = ComputeIntersection(ref this.planes[5], ref ray);
            RayD yd2 = ComputeIntersectionLine(ref this.planes[3], ref this.planes[0]);

            this.cornerArray[1] = ComputeIntersection(ref this.planes[4], ref yd2);
            this.cornerArray[2] = ComputeIntersection(ref this.planes[5], ref yd2);
            yd2 = ComputeIntersectionLine(ref this.planes[2], ref this.planes[1]);
            this.cornerArray[4] = ComputeIntersection(ref this.planes[4], ref yd2);
            this.cornerArray[7] = ComputeIntersection(ref this.planes[5], ref yd2);
            yd2 = ComputeIntersectionLine(ref this.planes[1], ref this.planes[3]);
            this.cornerArray[5] = ComputeIntersection(ref this.planes[4], ref yd2);
            this.cornerArray[6] = ComputeIntersection(ref this.planes[5], ref yd2);
        }
        public bool Intersect(ref LineD line, out LineD intersectedLine)
        {
            double num;
            double num2;
            RayD   ray = new RayD(line.From, line.Direction);

            if (!this.Intersect(ref ray, out num, out num2))
            {
                intersectedLine = line;
                return(false);
            }
            num  = Math.Max(num, 0.0);
            num2 = Math.Min(num2, line.Length);
            intersectedLine.From      = line.From + ((Vector3D)(line.Direction * num));
            intersectedLine.To        = line.From + ((Vector3D)(line.Direction * num2));
            intersectedLine.Direction = line.Direction;
            intersectedLine.Length    = num2 - num;
            return(true);
        }
Exemple #14
0
        public double?Intersects(ref LineD line)
        {
            if (Contains(ref line.From))
            {
                RayD   ray = new RayD(line.To, -line.Direction);
                double?f   = Intersects(ref ray);
                if (f.HasValue)
                {
                    double v = line.Length - f.Value;

                    if (v < 0)
                    {
                        return(null);
                    }
                    if (v > line.Length)
                    {
                        return(null);
                    }

                    return(v);
                }
                return(null);
            }
            else
            {
                RayD   ray = new RayD(line.From, line.Direction);
                double?f   = Intersects(ref ray);
                if (f.HasValue)
                {
                    if (f.Value < 0)
                    {
                        return(null);
                    }
                    if (f.Value > line.Length)
                    {
                        return(null);
                    }

                    return(f.Value);
                }
                return(null);
            }
        }
        public double?Intersects(ref LineD line)
        {
            if (this.Contains(ref line.From))
            {
                RayD   yd       = new RayD(line.To, -line.Direction);
                double?nullable = this.Intersects(ref yd);
                if (!nullable.HasValue)
                {
                    return(null);
                }
                double num = line.Length - nullable.Value;
                if (num < 0.0)
                {
                    return(null);
                }
                if (num > line.Length)
                {
                    return(null);
                }
                return(new double?(num));
            }
            RayD   ray       = new RayD(line.From, line.Direction);
            double?nullable2 = this.Intersects(ref ray);

            if (!nullable2.HasValue)
            {
                return(null);
            }
            if (nullable2.Value < 0.0)
            {
                return(null);
            }
            if (nullable2.Value > line.Length)
            {
                return(null);
            }
            return(new double?(nullable2.Value));
        }
        public bool Intersect(ref RayD ray, out double tmin, out double tmax)
        {
            double num  = 1.0 / ray.Direction.X;
            double num2 = 1.0 / ray.Direction.Y;
            double num3 = 1.0 / ray.Direction.Z;
            double num4 = (this.Min.X - ray.Position.X) * num;
            double num5 = (this.Max.X - ray.Position.X) * num;
            double num6 = (this.Min.Y - ray.Position.Y) * num2;
            double num7 = (this.Max.Y - ray.Position.Y) * num2;
            double num8 = (this.Min.Z - ray.Position.Z) * num3;
            double num9 = (this.Max.Z - ray.Position.Z) * num3;

            tmin = Math.Max(Math.Max(Math.Min(num4, num5), Math.Min(num6, num7)), Math.Min(num8, num9));
            tmax = Math.Min(Math.Min(Math.Max(num4, num5), Math.Max(num6, num7)), Math.Max(num8, num9));
            if (tmax < 0.0)
            {
                return(false);
            }
            if (tmin > tmax)
            {
                return(false);
            }
            return(true);
        }
Exemple #17
0
        public bool IntersectRaySphere(RayD ray, out double tmin, out double tmax)
        {
            tmin = 0.0;
            tmax = 0.0;
            Vector3D v    = ray.Position - this.Center;
            double   num  = ray.Direction.Dot(ray.Direction);
            double   num2 = 2.0 * v.Dot(ray.Direction);
            double   num3 = v.Dot(v) - (this.Radius * this.Radius);
            double   d    = (num2 * num2) - ((4.0 * num) * num3);

            if (d < 0.0)
            {
                return(false);
            }
            tmin = (-num2 - Math.Sqrt(d)) / (2.0 * num);
            tmax = (-num2 + Math.Sqrt(d)) / (2.0 * num);
            if (tmin > tmax)
            {
                double num5 = tmin;
                tmin = tmax;
                tmax = num5;
            }
            return(true);
        }
Exemple #18
0
        private static Vector3D ComputeIntersection(ref PlaneD plane, ref RayD ray)
        {
            double num = (-plane.D - Vector3D.Dot(plane.Normal, ray.Position)) / Vector3D.Dot(plane.Normal, ray.Direction);

            return(ray.Position + ray.Direction * num);
        }
Exemple #19
0
        public void Intersects(ref RayD ray, out double?result)
        {
            ContainmentType type;

            this.Contains(ref ray.Position, out type);
            if (type == ContainmentType.Contains)
            {
                result = 0.0;
            }
            else
            {
                double minValue = double.MinValue;
                double maxValue = double.MaxValue;
                result = 0;
                foreach (PlaneD ed in this.planes)
                {
                    double   num3;
                    double   num4;
                    Vector3D normal = ed.Normal;
                    Vector3D.Dot(ref ray.Direction, ref normal, out num3);
                    Vector3D.Dot(ref ray.Position, ref normal, out num4);
                    num4 += ed.D;
                    if (Math.Abs(num3) < 9.99999974737875E-06)
                    {
                        if (num4 <= 0.0)
                        {
                            continue;
                        }
                        return;
                    }
                    double num5 = -num4 / num3;
                    if (num3 < 0.0)
                    {
                        if (num5 > maxValue)
                        {
                            return;
                        }
                        if (num5 > minValue)
                        {
                            minValue = num5;
                        }
                    }
                    else
                    {
                        if (num5 < minValue)
                        {
                            return;
                        }
                        if (num5 < maxValue)
                        {
                            maxValue = num5;
                        }
                    }
                }
                double num6 = (minValue >= 0.0) ? minValue : maxValue;
                if (num6 >= 0.0)
                {
                    result = new double?(num6);
                }
            }
        }
        public double?Intersects(ref RayD ray)
        {
            MatrixD  xd       = Matrix.CreateFromQuaternion(this.Orientation);
            Vector3D vectord  = this.Center - ray.Position;
            double   minValue = double.MinValue;
            double   maxValue = double.MaxValue;
            double   num3     = Vector3D.Dot(xd.Right, vectord);
            double   num4     = Vector3D.Dot(xd.Right, ray.Direction);

            if ((num4 >= -9.9999996826552254E-21) && (num4 <= 9.9999996826552254E-21))
            {
                if (((-num3 - this.HalfExtent.X) > 0.0) || ((-num3 + this.HalfExtent.X) < 0.0))
                {
                    return(null);
                }
            }
            else
            {
                double num5 = (num3 - this.HalfExtent.X) / num4;
                double num6 = (num3 + this.HalfExtent.X) / num4;
                if (num5 > num6)
                {
                    double num7 = num5;
                    num5 = num6;
                    num6 = num7;
                }
                if (num5 > minValue)
                {
                    minValue = num5;
                }
                if (num6 < maxValue)
                {
                    maxValue = num6;
                }
                if ((maxValue < 0.0) || (minValue > maxValue))
                {
                    return(null);
                }
            }
            num3 = Vector3.Dot((Vector3)xd.Up, (Vector3)vectord);
            num4 = Vector3.Dot((Vector3)xd.Up, (Vector3)ray.Direction);
            if ((num4 >= -9.9999996826552254E-21) && (num4 <= 9.9999996826552254E-21))
            {
                if (((-num3 - this.HalfExtent.Y) > 0.0) || ((-num3 + this.HalfExtent.Y) < 0.0))
                {
                    return(null);
                }
            }
            else
            {
                double num8 = (num3 - this.HalfExtent.Y) / num4;
                double num9 = (num3 + this.HalfExtent.Y) / num4;
                if (num8 > num9)
                {
                    double num10 = num8;
                    num8 = num9;
                    num9 = num10;
                }
                if (num8 > minValue)
                {
                    minValue = num8;
                }
                if (num9 < maxValue)
                {
                    maxValue = num9;
                }
                if ((maxValue < 0.0) || (minValue > maxValue))
                {
                    return(null);
                }
            }
            num3 = Vector3.Dot((Vector3)xd.Forward, (Vector3)vectord);
            num4 = Vector3.Dot((Vector3)xd.Forward, (Vector3)ray.Direction);
            if ((num4 >= -9.9999996826552254E-21) && (num4 <= 9.9999996826552254E-21))
            {
                if (((-num3 - this.HalfExtent.Z) > 0.0) || ((-num3 + this.HalfExtent.Z) < 0.0))
                {
                    return(null);
                }
            }
            else
            {
                double num11 = (num3 - this.HalfExtent.Z) / num4;
                double num12 = (num3 + this.HalfExtent.Z) / num4;
                if (num11 > num12)
                {
                    double num13 = num11;
                    num11 = num12;
                    num12 = num13;
                }
                if (num11 > minValue)
                {
                    minValue = num11;
                }
                if (num12 < maxValue)
                {
                    maxValue = num12;
                }
                if ((maxValue < 0.0) || (minValue > maxValue))
                {
                    return(null);
                }
            }
            return(new double?(minValue));
        }
        public double?Intersects(Ray ray)
        {
            RayD yd = new RayD(ray.Position, ray.Direction);

            return(this.Intersects(yd));
        }
Exemple #22
0
        // Determine whether the given ray intersects this box. If so, returns
        // the parametric value of the point of first intersection; otherwise
        // returns null.
        public double?Intersects(ref RayD ray)
        {
            MatrixD R = Matrix.CreateFromQuaternion(Orientation);

            Vector3D TOrigin = Center - ray.Position;

            double t_min = -double.MaxValue;
            double t_max = double.MaxValue;

            // X-case
            double axisDotOrigin = Vector3D.Dot(R.Right, TOrigin);
            double axisDotDir    = Vector3D.Dot(R.Right, ray.Direction);

            if (axisDotDir >= -RAY_EPSILON && axisDotDir <= RAY_EPSILON)
            {
                if ((-axisDotOrigin - HalfExtent.X) > 0.0 || (-axisDotOrigin + HalfExtent.X) < 0.0f)
                {
                    return(null);
                }
            }
            else
            {
                double t1 = (axisDotOrigin - HalfExtent.X) / axisDotDir;
                double t2 = (axisDotOrigin + HalfExtent.X) / axisDotDir;

                if (t1 > t2)
                {
                    double temp = t1;
                    t1 = t2;
                    t2 = temp;
                }

                if (t1 > t_min)
                {
                    t_min = t1;
                }

                if (t2 < t_max)
                {
                    t_max = t2;
                }

                if (t_max < 0.0f || t_min > t_max)
                {
                    return(null);
                }
            }

            // Y-case
            axisDotOrigin = Vector3.Dot(R.Up, TOrigin);
            axisDotDir    = Vector3.Dot(R.Up, ray.Direction);

            if (axisDotDir >= -RAY_EPSILON && axisDotDir <= RAY_EPSILON)
            {
                if ((-axisDotOrigin - HalfExtent.Y) > 0.0 || (-axisDotOrigin + HalfExtent.Y) < 0.0f)
                {
                    return(null);
                }
            }
            else
            {
                double t1 = (axisDotOrigin - HalfExtent.Y) / axisDotDir;
                double t2 = (axisDotOrigin + HalfExtent.Y) / axisDotDir;

                if (t1 > t2)
                {
                    double temp = t1;
                    t1 = t2;
                    t2 = temp;
                }

                if (t1 > t_min)
                {
                    t_min = t1;
                }

                if (t2 < t_max)
                {
                    t_max = t2;
                }

                if (t_max < 0.0f || t_min > t_max)
                {
                    return(null);
                }
            }

            // Z-case
            axisDotOrigin = Vector3.Dot(R.Forward, TOrigin);
            axisDotDir    = Vector3.Dot(R.Forward, ray.Direction);

            if (axisDotDir >= -RAY_EPSILON && axisDotDir <= RAY_EPSILON)
            {
                if ((-axisDotOrigin - HalfExtent.Z) > 0.0 || (-axisDotOrigin + HalfExtent.Z) < 0.0f)
                {
                    return(null);
                }
            }
            else
            {
                double t1 = (axisDotOrigin - HalfExtent.Z) / axisDotDir;
                double t2 = (axisDotOrigin + HalfExtent.Z) / axisDotDir;

                if (t1 > t2)
                {
                    double temp = t1;
                    t1 = t2;
                    t2 = temp;
                }

                if (t1 > t_min)
                {
                    t_min = t1;
                }

                if (t2 < t_max)
                {
                    t_max = t2;
                }

                if (t_max < 0.0f || t_min > t_max)
                {
                    return(null);
                }
            }

            return(t_min);
        }
        public void Intersects(ref RayD ray, out double?result)
        {
            result = 0;
            double num      = 0.0;
            double maxValue = double.MaxValue;

            if (Math.Abs(ray.Direction.X) < 9.99999997475243E-07)
            {
                if ((ray.Position.X < this.Min.X) || (ray.Position.X > this.Max.X))
                {
                    return;
                }
            }
            else
            {
                double num3 = 1.0 / ray.Direction.X;
                double num4 = (this.Min.X - ray.Position.X) * num3;
                double num5 = (this.Max.X - ray.Position.X) * num3;
                if (num4 > num5)
                {
                    double num6 = num4;
                    num4 = num5;
                    num5 = num6;
                }
                num      = MathHelper.Max(num4, num);
                maxValue = MathHelper.Min(num5, maxValue);
                if (num > maxValue)
                {
                    return;
                }
            }
            if (Math.Abs(ray.Direction.Y) < 9.99999997475243E-07)
            {
                if ((ray.Position.Y < this.Min.Y) || (ray.Position.Y > this.Max.Y))
                {
                    return;
                }
            }
            else
            {
                double num7 = 1.0 / ray.Direction.Y;
                double num8 = (this.Min.Y - ray.Position.Y) * num7;
                double num9 = (this.Max.Y - ray.Position.Y) * num7;
                if (num8 > num9)
                {
                    double num10 = num8;
                    num8 = num9;
                    num9 = num10;
                }
                num      = MathHelper.Max(num8, num);
                maxValue = MathHelper.Min(num9, maxValue);
                if (num > maxValue)
                {
                    return;
                }
            }
            if (Math.Abs(ray.Direction.Z) < 9.99999997475243E-07)
            {
                if ((ray.Position.Z < this.Min.Z) || (ray.Position.Z > this.Max.Z))
                {
                    return;
                }
            }
            else
            {
                double num11 = 1.0 / ray.Direction.Z;
                double num12 = (this.Min.Z - ray.Position.Z) * num11;
                double num13 = (this.Max.Z - ray.Position.Z) * num11;
                if (num12 > num13)
                {
                    double num14 = num12;
                    num12 = num13;
                    num13 = num14;
                }
                num = MathHelper.Max(num12, num);
                double num15 = MathHelper.Min(num13, maxValue);
                if (num > num15)
                {
                    return;
                }
            }
            result = new double?(num);
        }
        public bool Intersect(ref LineD line, out double t1, out double t2)
        {
            RayD ray = new RayD(line.From, line.Direction);

            return(this.Intersect(ref ray, out t1, out t2));
        }
Exemple #25
0
 public double?Intersects(RayD ray)
 {
     return(ray.Intersects(this));
 }
Exemple #26
0
        /// <summary>
        /// Checks whether the current BoundingFrustumD intersects a Ray.
        /// </summary>
        /// <param name="ray">The Ray to check for intersection with.</param><param name="result">[OutAttribute] Distance at which the ray intersects the BoundingFrustumD or null if there is no intersection.</param>
        public void Intersects(ref RayD ray, out double?result)
        {
            ContainmentType result1;

            this.Contains(ref ray.Position, out result1);
            if (result1 == ContainmentType.Contains)
            {
                result = new double?(0.0f);
            }
            else
            {
                double num1 = double.MinValue;
                double num2 = double.MaxValue;
                result = new double?();
                foreach (PlaneD plane in this.planes)
                {
                    Vector3D vector2 = plane.Normal;
                    double   result2;
                    Vector3D.Dot(ref ray.Direction, ref vector2, out result2);
                    double result3;
                    Vector3D.Dot(ref ray.Position, ref vector2, out result3);
                    result3 += plane.D;
                    if ((double)Math.Abs(result2) < 9.99999974737875E-06)
                    {
                        if ((double)result3 > 0.0)
                        {
                            return;
                        }
                    }
                    else
                    {
                        double num3 = -result3 / result2;
                        if ((double)result2 < 0.0)
                        {
                            if ((double)num3 > (double)num2)
                            {
                                return;
                            }
                            if ((double)num3 > (double)num1)
                            {
                                num1 = num3;
                            }
                        }
                        else
                        {
                            if ((double)num3 < (double)num1)
                            {
                                return;
                            }
                            if ((double)num3 < (double)num2)
                            {
                                num2 = num3;
                            }
                        }
                    }
                }
                double num4 = (double)num1 >= 0.0 ? num1 : num2;
                if ((double)num4 < 0.0)
                {
                    return;
                }
                result = new double?(num4);
            }
        }
        public double?Intersects(Ray ray)
        {
            RayD r = new RayD((Vector3D)ray.Position, (Vector3D)ray.Direction);

            return(Intersects(r));
        }
Exemple #28
0
        public void BoundingBoxIntersectKeen()
        {
            var point = new VRageMath.Vector3D(5d, 3.5d, 4d);
            var vector = new VRageMath.Vector3D(-0.03598167d, 0.0110336d, 0.9992915d);
            var box = new VRageMath.BoundingBoxD(new VRageMath.Vector3D(3d, 3d, 2d), new VRageMath.Vector3D(7d, 4d, 6d));
            var ray = new VRageMath.RayD(point, vector);

            double? f = box.Intersects(ray);

            Assert.AreEqual(0, f, "Should Equal");
        }
Exemple #29
0
 public double?Intersects(RayD ray) =>
 ray.Intersects(this);
        public bool Intersect(RayD ray, ref Vector3D p1, ref Vector3D p2, ref Vector3 n1, ref Vector3 n2)
        {
            Vector3D v        = this.P1 - this.P0;
            Vector3D vectord2 = ray.Position - this.P0;
            double   num      = v.Dot(ray.Direction);
            double   num2     = v.Dot(vectord2);
            double   num3     = v.Dot(v);
            double   num4     = (num3 > 0.0) ? (num / num3) : 0.0;
            double   num5     = (num3 > 0.0) ? (num2 / num3) : 0.0;
            Vector3D vectord3 = ray.Direction - ((Vector3D)(v * num4));
            Vector3D vectord4 = vectord2 - ((Vector3D)(v * num5));
            double   num6     = vectord3.Dot(vectord3);
            double   num7     = 2.0 * vectord3.Dot(vectord4);
            double   num8     = vectord4.Dot(vectord4) - (this.Radius * this.Radius);

            if (num6 == 0.0)
            {
                BoundingSphereD ed;
                BoundingSphereD ed2;
                double          num9;
                double          num10;
                double          num11;
                double          num12;
                ed.Center  = this.P0;
                ed.Radius  = this.Radius;
                ed2.Center = this.P1;
                ed2.Radius = this.Radius;
                if (!ed.IntersectRaySphere(ray, out num9, out num10) || !ed2.IntersectRaySphere(ray, out num11, out num12))
                {
                    return(false);
                }
                if (num9 < num11)
                {
                    p1 = ray.Position + ((Vector3D)(ray.Direction * num9));
                    n1 = (Vector3)(p1 - this.P0);
                    n1.Normalize();
                }
                else
                {
                    p1 = ray.Position + ((Vector3D)(ray.Direction * num11));
                    n1 = (Vector3)(p1 - this.P1);
                    n1.Normalize();
                }
                if (num10 > num12)
                {
                    p2 = ray.Position + ((Vector3D)(ray.Direction * num10));
                    n2 = (Vector3)(p2 - this.P0);
                    n2.Normalize();
                }
                else
                {
                    p2 = ray.Position + ((Vector3D)(ray.Direction * num12));
                    n2 = (Vector3)(p2 - this.P1);
                    n2.Normalize();
                }
                return(true);
            }
            double d = (num7 * num7) - ((4.0 * num6) * num8);

            if (d < 0.0)
            {
                return(false);
            }
            double num14 = (-num7 - Math.Sqrt(d)) / (2.0 * num6);
            double num15 = (-num7 + Math.Sqrt(d)) / (2.0 * num6);

            if (num14 > num15)
            {
                double num16 = num14;
                num14 = num15;
                num15 = num16;
            }
            double num17 = (num14 * num4) + num5;

            if (num17 < 0.0)
            {
                BoundingSphereD ed3;
                double          num18;
                double          num19;
                ed3.Center = this.P0;
                ed3.Radius = this.Radius;
                if (!ed3.IntersectRaySphere(ray, out num18, out num19))
                {
                    return(false);
                }
                p1 = ray.Position + ((Vector3D)(ray.Direction * num18));
                n1 = (Vector3)(p1 - this.P0);
                n1.Normalize();
            }
            else if (num17 > 1.0)
            {
                BoundingSphereD ed4;
                double          num20;
                double          num21;
                ed4.Center = this.P1;
                ed4.Radius = this.Radius;
                if (!ed4.IntersectRaySphere(ray, out num20, out num21))
                {
                    return(false);
                }
                p1 = ray.Position + ((Vector3D)(ray.Direction * num20));
                n1 = (Vector3)(p1 - this.P1);
                n1.Normalize();
            }
            else
            {
                p1 = ray.Position + ((Vector3D)(ray.Direction * num14));
                Vector3 vector = (Vector3)(this.P0 + (v * num17));
                n1 = ((Vector3)p1) - vector;
                n1.Normalize();
            }
            double num22 = (num15 * num4) + num5;

            if (num22 < 0.0)
            {
                BoundingSphereD ed5;
                double          num23;
                double          num24;
                ed5.Center = this.P0;
                ed5.Radius = this.Radius;
                if (!ed5.IntersectRaySphere(ray, out num23, out num24))
                {
                    return(false);
                }
                p2 = ray.Position + ((Vector3D)(ray.Direction * num24));
                n2 = (Vector3)(p2 - this.P0);
                n2.Normalize();
            }
            else if (num22 > 1.0)
            {
                BoundingSphereD ed6;
                double          num25;
                double          num26;
                ed6.Center = this.P1;
                ed6.Radius = this.Radius;
                if (!ed6.IntersectRaySphere(ray, out num25, out num26))
                {
                    return(false);
                }
                p2 = ray.Position + ((Vector3D)(ray.Direction * num26));
                n2 = (Vector3)(p2 - this.P1);
                n2.Normalize();
            }
            else
            {
                p2 = ray.Position + ((Vector3D)(ray.Direction * num15));
                Vector3D vectord5 = this.P0 + ((Vector3D)(v * num22));
                n2 = (Vector3)(p2 - vectord5);
                n2.Normalize();
            }
            return(true);
        }
        /// <summary>
        /// Checks whether the current BoundingBox intersects a Ray.
        /// </summary>
        /// <param name="ray">The Ray to check for intersection with.</param><param name="result">[OutAttribute] Distance at which the ray intersects the BoundingBox, or null if there is no intersection.</param>
        public void Intersects(ref RayD ray, out double?result)
        {
            result = new double?();
            double num1 = 0.0f;
            double num2 = double.MaxValue;

            if ((double)Math.Abs(ray.Direction.X) < 9.99999997475243E-07)
            {
                if ((double)ray.Position.X < (double)this.Min.X || (double)ray.Position.X > (double)this.Max.X)
                {
                    return;
                }
            }
            else
            {
                double num3 = 1f / ray.Direction.X;
                double num4 = (this.Min.X - ray.Position.X) * num3;
                double num5 = (this.Max.X - ray.Position.X) * num3;
                if ((double)num4 > (double)num5)
                {
                    double num6 = num4;
                    num4 = num5;
                    num5 = num6;
                }
                num1 = MathHelper.Max(num4, num1);
                num2 = MathHelper.Min(num5, num2);
                if ((double)num1 > (double)num2)
                {
                    return;
                }
            }
            if ((double)Math.Abs(ray.Direction.Y) < 9.99999997475243E-07)
            {
                if ((double)ray.Position.Y < (double)this.Min.Y || (double)ray.Position.Y > (double)this.Max.Y)
                {
                    return;
                }
            }
            else
            {
                double num3 = 1f / ray.Direction.Y;
                double num4 = (this.Min.Y - ray.Position.Y) * num3;
                double num5 = (this.Max.Y - ray.Position.Y) * num3;
                if ((double)num4 > (double)num5)
                {
                    double num6 = num4;
                    num4 = num5;
                    num5 = num6;
                }
                num1 = MathHelper.Max(num4, num1);
                num2 = MathHelper.Min(num5, num2);
                if ((double)num1 > (double)num2)
                {
                    return;
                }
            }
            if ((double)Math.Abs(ray.Direction.Z) < 9.99999997475243E-07)
            {
                if ((double)ray.Position.Z < (double)this.Min.Z || (double)ray.Position.Z > (double)this.Max.Z)
                {
                    return;
                }
            }
            else
            {
                double num3 = 1f / ray.Direction.Z;
                double num4 = (this.Min.Z - ray.Position.Z) * num3;
                double num5 = (this.Max.Z - ray.Position.Z) * num3;
                if ((double)num4 > (double)num5)
                {
                    double num6 = num4;
                    num4 = num5;
                    num5 = num6;
                }
                num1 = MathHelper.Max(num4, num1);
                double num7 = MathHelper.Min(num5, num2);
                if ((double)num1 > (double)num7)
                {
                    return;
                }
            }
            result = new double?(num1);
        }