Exemplo n.º 1
0
        public void Hit()
        {
            var sphere        = new Sphere();
            var intersection1 = new Intersection(1, sphere);
            var intersection2 = new Intersection(2, sphere);

            var intersections = new Intersections(intersection1, intersection2);

            Assert.Equal(intersection1, intersections.Hit());

            intersection1 = new Intersection(-1, sphere);
            intersection2 = new Intersection(1, sphere);

            intersections = new Intersections(intersection1, intersection2);

            Assert.Equal(intersection2, intersections.Hit());

            intersection1 = new Intersection(-2, sphere);
            intersection2 = new Intersection(-1, sphere);

            intersections = new Intersections(intersection1, intersection2);

            Assert.Null(intersections.Hit());

            intersection1 = new Intersection(5, sphere);
            intersection2 = new Intersection(7, sphere);
            var intersection3 = new Intersection(-3, sphere);
            var intersection4 = new Intersection(2, sphere);

            intersections = new Intersections(intersection1, intersection2, intersection3, intersection4);

            Assert.Equal(intersection4, intersections.Hit());
        }
Exemplo n.º 2
0
        public void HitWhenAllDistancesNegative()
        {
            var s  = new Sphere();
            var i1 = new Intersection(-2, s);
            var i2 = new Intersection(-1, s);
            var xs = new Intersections(i1, i2);
            var h  = xs.Hit();

            Assert.AreEqual(null, h);
        }
Exemplo n.º 3
0
        public void HitWhenAllDistancesPositive()
        {
            var s  = new Sphere();
            var i1 = new Intersection(1, s);
            var i2 = new Intersection(2, s);
            var xs = new Intersections(i1, i2);
            var h  = xs.Hit();

            Assert.AreEqual(i1, h);
        }
Exemplo n.º 4
0
        public void HitIsAlwaysLowestNonNegativeIntersection()
        {
            var s  = new Sphere();
            var i1 = new Intersection(5, s);
            var i2 = new Intersection(7, s);
            var i3 = new Intersection(-3, s);
            var i4 = new Intersection(2, s);
            var xs = new Intersections(i1, i2, i3, i4);
            var h  = xs.Hit();

            Assert.AreEqual(i4, h);
        }
Exemplo n.º 5
0
        public void TheHitWhenAllIntersectionsHaveNegativeT()
        {
            var s  = new Sphere();
            var i1 = new Intersection(-1, s);
            var i2 = new Intersection(-2, s);
            var xs = new Intersections(new List <Intersection> {
                i2, i1
            });
            var i = xs.Hit();

            Assert.IsNull(i);
        }
Exemplo n.º 6
0
        public void TheHitWhenSomeIntersectionsHaveNegativeT()
        {
            var s  = new Sphere();
            var i1 = new Intersection(-1, s);
            var i2 = new Intersection(1, s);
            var xs = new Intersections(new List <Intersection> {
                i2, i1
            });
            var i = xs.Hit();

            Assert.AreEqual(i2, i);
        }
Exemplo n.º 7
0
        public void TheHitWhenAllIntersectionsHavePositiveT()
        {
            var s  = new Sphere();
            var i1 = new Intersection(1, s);
            var i2 = new Intersection(2, s);
            var xs = new Intersections(new List <Intersection> {
                i2, i1
            });
            var i = xs.Hit();

            Assert.AreEqual(i1, i);
        }
Exemplo n.º 8
0
        public void LightingSpherePictureTest()
        {
            int    size      = 400;
            var    rayOrigin = Helper.CreatePoint(0, 0, -5);
            double wallZ     = 10;
            double wallSize  = 7;
            var    pixelSize = wallSize / size;
            var    half      = wallSize / 2;

            var canvas = new Canvas(size, size);
            var sphere = Helper.Sphere();

            sphere.Material.Pattern = new SolidPattern(new Color(1, 0.2, 1));

            var light = new PointLight(Helper.CreatePoint(-10, 10, -10), Color.White);

            for (int y = 0; y < size; y++)
            {
                double worldY = half - pixelSize * y;
                for (int x = 0; x < size - 1; x++)
                {
                    // compute the world x coordinate (left = -half, right = half)
                    double worldX = -half + pixelSize * x;
                    // describe the point on the wall that the ray will target
                    var position      = Helper.CreatePoint(worldX, worldY, wallZ);
                    var ray           = Helper.Ray(rayOrigin, (position - rayOrigin).Normalize());
                    var intersections = new Intersections();
                    sphere.Intersect(ref ray.Origin, ref ray.Direction, intersections);
                    var hit = intersections.Hit();

                    if (hit != null)
                    {
                        var point     = ray.Position(hit.T);
                        var hitObject = hit.Object;
                        var normal    = hitObject.NormalAt(point);
                        var eye       = -ray.Direction;
                        var color     = hitObject.Material.Lighting(light, ref point, ref eye, ref normal, 1);

                        canvas.SetPixel(x, y, color);
                    }
                }
            }

            var tmpFile = Path.GetTempFileName();
            var ppmFile = Path.ChangeExtension(tmpFile, "ppm");

            canvas.SavePPM(ppmFile);

//            Helper.Display(ppmFile);
            File.Delete(tmpFile);
            File.Delete(ppmFile);
        }
Exemplo n.º 9
0
        public void GivenIntersectionHit_WhenAllIntersectionsHaveNegativeTimeValue_ThenReturnLowestPositiveIntersection()
        {
            // Arrange
            var sphere        = new Sphere();
            var i1            = new Intersection(-2, sphere);
            var i2            = new Intersection(-1, sphere);
            var intersections = new Intersections(i1, i2);

            // Act
            var hit = intersections.Hit();

            // Assert
            Assert.Null(hit);
        }
Exemplo n.º 10
0
        public void TheHitIsAllwaysTheLowestNonNegativeIntersection()
        {
            var s  = new Sphere();
            var i1 = new Intersection(5, s);
            var i2 = new Intersection(7, s);
            var i3 = new Intersection(-3, s);
            var i4 = new Intersection(2, s);
            var xs = new Intersections(new List <Intersection> {
                i1, i2, i3, i4
            });
            var i = xs.Hit();

            Assert.AreEqual(i4, i);
        }
Exemplo n.º 11
0
        public void TestHitAllNegativeDistance()
        {
            var sphere = new Sphere();

            var i1 = new Intersection(-1, sphere);
            var i2 = new Intersection(-2, sphere);

            var intersections = new Intersections
            {
                i2,
                i1
            };

            Assert.IsNull(intersections.Hit()?.Shape);
        }
Exemplo n.º 12
0
        public void TestHitNegativeDistance()
        {
            var sphere = new Sphere();

            var i1 = new Intersection(-1, sphere);
            var i2 = new Intersection(1, sphere);

            var intersections = new Intersections
            {
                i2,
                i1
            };

            Assert.AreEqual(i2, intersections.Hit());
        }
Exemplo n.º 13
0
        public void GivenIntersectionHit_WhenIntersectionsHaveMixedTimeValue_ThenReturnLowestPositiveIntersection()
        {
            // Arrange
            var sphere        = new Sphere();
            var i1            = new Intersection(5, sphere);
            var i2            = new Intersection(7, sphere);
            var i3            = new Intersection(-3, sphere);
            var i4            = new Intersection(2, sphere);
            var intersections = new Intersections(i1, i2, i3, i4);

            // Act
            var hit = intersections.Hit();

            // Assert
            Assert.Equal(i4, hit);
        }
Exemplo n.º 14
0
        public void TestHitMultiple()
        {
            var sphere = new Sphere();

            var i1 = new Intersection(5, sphere);
            var i2 = new Intersection(7, sphere);
            var i3 = new Intersection(-2, sphere);
            var i4 = new Intersection(3, sphere);

            var intersections = new Intersections
            {
                i1,
                i2,
                i3,
                i4
            };

            Assert.AreEqual(i4, intersections.Hit());
        }
Exemplo n.º 15
0
        private void SpherePictureTest(Matrix transform)
        {
            int    size      = 400;
            var    rayOrigin = Helper.CreatePoint(0, 0, -5);
            double wallZ     = 10;
            double wallSize  = 7;
            var    pixelSize = wallSize / size;
            var    half      = wallSize / 2;

            var canvas = new Canvas(size, size);
            var color  = new Color(1, 0, 0);
            var sphere = Helper.Sphere();

            sphere.Transform = transform;

            for (int y = 0; y < size; y++)
            {
                double worldY = half - pixelSize * y;
                for (int x = 0; x < size - 1; x++)
                {
                    // compute the world x coordinate (left = -half, right = half)
                    double worldX = -half + pixelSize * x;
                    // describe the point on the wall that the ray will target
                    var position      = Helper.CreatePoint(worldX, worldY, wallZ);
                    var r             = Helper.Ray(rayOrigin, (position - rayOrigin).Normalize());
                    var intersections = new Intersections();
                    sphere.Intersect(ref r.Origin, ref r.Direction, intersections);
                    if (intersections.Hit() != null)
                    {
                        canvas.SetPixel(x, y, color);
                    }
                }
            }

            var tmpFile = Path.GetTempFileName();
            var ppmFile = Path.ChangeExtension(tmpFile, "ppm");

            canvas.SavePPM(ppmFile);
//            Helper.Display(ppmFile);
            File.Delete(tmpFile);
            File.Delete(ppmFile);
        }