Exemplo n.º 1
0
 public void RayInterSection_OnSpherePointingIn_OneIntersection()
 {
     var sphere = new BoundingSphere(new Point3D(0.2, 0.3, 0), 2.0);
     var ray = new Ray3D(sphere.Center + new Vector3D(0, 0, sphere.Radius), new Vector3D(0.01, 0.09, -0.87));
     Point3D[] result;
     Assert.IsTrue(sphere.RayIntersection(ray, out result));
     Assert.AreEqual(2, result.Length, "Number of intersections should be 1");
     Assert.AreEqual(sphere.Radius, sphere.Center.DistanceTo(result[0]), 1e-6, "Point 1 is not on sphere.");
     Assert.AreEqual(0, ray.GetNearest(result[0]).DistanceTo(result[0]), 1e-6, "Point 1 is not on ray.");
     Assert.AreEqual(sphere.Radius, sphere.Center.DistanceTo(result[1]), 1e-6, "Point 2 is not on sphere.");
     Assert.AreEqual(0, ray.GetNearest(result[1]).DistanceTo(result[1]), 1e-6, "Point 2 is not on ray.");
 }
Exemplo n.º 2
0
        public void RayInterSection_OnSpherePointingIn_OneIntersection()
        {
            var sphere = new BoundingSphere(new Point3D(0.2, 0.3, 0), 2.0);
            var ray    = new Ray3D(sphere.Center + new Vector3D(0, 0, sphere.Radius), new Vector3D(0.01, 0.09, -0.87));

            Point3D[] result;
            Assert.IsTrue(sphere.RayIntersection(ray, out result));
            Assert.AreEqual(2, result.Length, "Number of intersections should be 1");
            Assert.AreEqual(sphere.Radius, sphere.Center.DistanceTo(result[0]), 1e-6, "Point 1 is not on sphere.");
            Assert.AreEqual(0, ray.GetNearest(result[0]).DistanceTo(result[0]), 1e-6, "Point 1 is not on ray.");
            Assert.AreEqual(sphere.Radius, sphere.Center.DistanceTo(result[1]), 1e-6, "Point 2 is not on sphere.");
            Assert.AreEqual(0, ray.GetNearest(result[1]).DistanceTo(result[1]), 1e-6, "Point 2 is not on ray.");
        }
Exemplo n.º 3
0
        public void RayInterSection_OutsideSpherePointingToCenter_TwoIntersections()
        {
            var sphere = new BoundingSphere(new Point3D(0.2, 0.3, 0), 1.0);
            var p = new Point3D(12, 23, 32);
            var ray = new Ray3D(p, sphere.Center - p);

            Point3D[] result;
            Assert.IsTrue(sphere.RayIntersection(ray, out result));
            Assert.AreEqual(sphere.Radius, sphere.Center.DistanceTo(result[0]), 1e-6, "Point 1 is not on sphere.");
            Assert.AreEqual(0, ray.GetNearest(result[0]).DistanceTo(result[0]), 1e-6, "Point 1" + result[0] + " is not on ray.");
            Assert.AreEqual(sphere.Radius, sphere.Center.DistanceTo(result[1]), 1e-6, "Point 2 is not on sphere.");
            Assert.AreEqual(0, ray.GetNearest(result[1]).DistanceTo(result[1]), 1e-6, "Point 2 " + result[0] + " is not on ray.");
            Assert.IsTrue(ray.Origin.DistanceTo(result[0]) < ray.Origin.DistanceTo(result[1]), "The points should be sorted by distance from ray origin.");
        }
Exemplo n.º 4
0
        public void RayInterSection_OutsideSpherePointingToCenter_TwoIntersections()
        {
            var sphere = new BoundingSphere(new Point3D(0.2, 0.3, 0), 1.0);
            var p      = new Point3D(12, 23, 32);
            var ray    = new Ray3D(p, sphere.Center - p);

            Point3D[] result;
            Assert.IsTrue(sphere.RayIntersection(ray, out result));
            Assert.AreEqual(sphere.Radius, sphere.Center.DistanceTo(result[0]), 1e-6, "Point 1 is not on sphere.");
            Assert.AreEqual(0, ray.GetNearest(result[0]).DistanceTo(result[0]), 1e-6, "Point 1" + result[0] + " is not on ray.");
            Assert.AreEqual(sphere.Radius, sphere.Center.DistanceTo(result[1]), 1e-6, "Point 2 is not on sphere.");
            Assert.AreEqual(0, ray.GetNearest(result[1]).DistanceTo(result[1]), 1e-6, "Point 2 " + result[0] + " is not on ray.");
            Assert.IsTrue(ray.Origin.DistanceTo(result[0]) < ray.Origin.DistanceTo(result[1]), "The points should be sorted by distance from ray origin.");
        }
Exemplo n.º 5
0
 public void GetNearest_PointOnRay()
 {
     var ray = new Ray3D(new Point3D(0, 0, 0), new Vector3D(1, 2, 3));
     var p0 = new Point3D(0.1, 0.2, 0.3);
     var p = ray.GetNearest(p0);
     Assert.AreEqual(0, p0.DistanceTo(p), 1e-12);
 }
Exemplo n.º 6
0
        public void GetNearest_PointOnRay()
        {
            var ray = new Ray3D(new Point3D(0, 0, 0), new Vector3D(1, 2, 3));
            var p0  = new Point3D(0.1, 0.2, 0.3);
            var p   = ray.GetNearest(p0);

            Assert.AreEqual(0, p0.DistanceTo(p), 1e-12);
        }
Exemplo n.º 7
0
        private Point3D?GetNearestPoint(Point position, Point3D hitPlaneOrigin, Vector3D hitPlaneNormal)
        {
            var hpp = this.GetHitPlanePoint(position, hitPlaneOrigin, hitPlaneNormal);

            if (hpp == null)
            {
                return(null);
            }

            var ray = new Ray3D(this.ToWorld(this.Position), this.ToWorld(this.Direction));

            return(ray.GetNearest(hpp.Value));
        }
Exemplo n.º 8
0
        public void RayInterSection_OutsideAndTangentToSphere_OneIntersection()
        {
            var sphere = new BoundingSphere(new Point3D(0.2, 0.3, 0), 1.0);
            var p = new Point3D(sphere.Center.X + sphere.Radius, sphere.Center.Y + sphere.Radius, sphere.Center.Z + 50);
            var p2 = new Point3D(sphere.Center.X + sphere.Radius, sphere.Center.Y, sphere.Center.Z);

            // ray tangent to sphere
            var ray = new Ray3D(p, p2 - p);
            Point3D[] result;
            Assert.IsTrue(sphere.RayIntersection(ray, out result), "No intersection");
            Assert.AreEqual(1, result.Length, "One intersection");
            Assert.AreEqual(sphere.Radius, result[0].DistanceTo(sphere.Center), 1e-8);
            Assert.AreEqual(0, ray.GetNearest(result[0]).DistanceTo(result[0]), 1e-6, "Point 1 is not on ray.");
        }
Exemplo n.º 9
0
        public void RayInterSection_OutsideAndTangentToSphere_OneIntersection()
        {
            var sphere = new BoundingSphere(new Point3D(0.2, 0.3, 0), 1.0);
            var p      = new Point3D(sphere.Center.X + sphere.Radius, sphere.Center.Y + sphere.Radius, sphere.Center.Z + 50);
            var p2     = new Point3D(sphere.Center.X + sphere.Radius, sphere.Center.Y, sphere.Center.Z);

            // ray tangent to sphere
            var ray = new Ray3D(p, p2 - p);

            Point3D[] result;
            Assert.IsTrue(sphere.RayIntersection(ray, out result), "No intersection");
            Assert.AreEqual(1, result.Length, "One intersection");
            Assert.AreEqual(sphere.Radius, result[0].DistanceTo(sphere.Center), 1e-8);
            Assert.AreEqual(0, ray.GetNearest(result[0]).DistanceTo(result[0]), 1e-6, "Point 1 is not on ray.");
        }
        /// <summary>
        /// Gets the nearest point on the translation axis.
        /// </summary>
        /// <param name="position">
        /// The position (in screen coordinates).
        /// </param>
        /// <param name="hitPlaneOrigin">
        /// The hit plane origin (world coordinate system).
        /// </param>
        /// <param name="hitPlaneNormal">
        /// The hit plane normal (world coordinate system).
        /// </param>
        /// <returns>
        /// The nearest point (world coordinates) or null if no point could be found.
        /// </returns>
        private Point3D? GetNearestPoint(Point position, Point3D hitPlaneOrigin, Vector3D hitPlaneNormal)
        {
            var hpp = this.GetHitPlanePoint(position, hitPlaneOrigin, hitPlaneNormal);
            if (hpp == null)
            {
                return null;
            }

            var ray = new Ray3D(this.ToWorld(this.Position), this.ToWorld(this.Direction));
            return ray.GetNearest(hpp.Value);
        }