コード例 #1
0
        public void SubdividingGroupPartitionsItsChildren()
        {
            var s1 = new shape.Sphere(transform.Translation(-2, -2, 0));
            var s2 = new shape.Sphere(transform.Translation(-2, 2, 0));
            var s3 = new shape.Sphere(transform.Scaling(4, 4, 4));
            var g  = new shape.Group(new List <shape.Shape>()
            {
                s1, s2, s3
            });

            g.Divide(1);

            Assert.Equal(s3, g[0]);
            var subgroup = g[1];

            Assert.IsType <shape.Group>(subgroup);

            var sg = subgroup as shape.Group;

            Assert.Equal(2, sg.Count());

            Assert.IsType <shape.Group>(sg[0]);
            Assert.Single((sg[0] as shape.Group).Children);
            Assert.Equal(s1, (sg[0] as shape.Group)[0]);

            Assert.IsType <shape.Group>(sg[1]);
            Assert.Single((sg[1] as shape.Group).Children);
            Assert.Equal(s2, (sg[1] as shape.Group)[0]);
        }
コード例 #2
0
        public void CreatingNewGroup()
        {
            var g = new shape.Group();

            Assert.Equal(Matrix.GetIdentity(), g.Transform);
            Assert.True(g.IsEmpty());
        }
コード例 #3
0
        public void SubdividingGroupWithTooFewChildren()
        {
            var s1       = new shape.Sphere(transform.Translation(-2, 0, 0));
            var s2       = new shape.Sphere(transform.Translation(2, 1, 0));
            var s3       = new shape.Sphere(transform.Translation(2, -1, 4));
            var subgroup = new shape.Group(new List <shape.Shape>()
            {
                s1, s2, s3
            });
            var s4 = new shape.Sphere();
            var g  = new shape.Group(new List <shape.Shape>()
            {
                subgroup, s4
            });

            g.Divide(3);

            Assert.Equal(subgroup, g[0]);
            Assert.Equal(s4, g[1]);
            var sg = (g[0] as shape.Group);

            Assert.Equal(2, sg.Count());

            Assert.IsType <shape.Group>(sg[0]);
            Assert.Single((sg[0] as shape.Group).Children);
            Assert.Equal(s1, (sg[0] as shape.Group)[0]);

            Assert.IsType <shape.Group>(sg[1]);
            Assert.Equal(2, (sg[1] as shape.Group).Count());
            Assert.Equal(new List <shape.Shape>()
            {
                s2, s3
            }, (sg[1] as shape.Group).Children);
        }
コード例 #4
0
        public void IntersectingRayWithEmptyGroup()
        {
            var g  = new shape.Group();
            var r  = new Ray(pt.Point(0, 0, 0), pt.Vector(0, 0, 0));
            var xs = g.Intersect(r);

            Assert.Empty(xs);
        }
コード例 #5
0
        public void AddingChildToGroup()
        {
            var g = new shape.Group();
            var s = new shape.TestShape();

            g.Add(s);

            Assert.NotEmpty(g.Children);
            Assert.Contains(s, g.Children);
            Assert.Equal(g, s.Parent);
        }
コード例 #6
0
        public void IntersectingRayGrouTestChildrenIfBoxHit()
        {
            var child = new shape.TestShape();
            var shape = new shape.Group();

            shape.Add(child);
            var r = new Ray(pt.Point(0, 0, -5), pt.Vector(0, 0, 1));

            _ = shape.Intersect(r);
            Assert.NotNull(child.SavedRay);
        }
コード例 #7
0
        public void IntersectingATransformedGroup()
        {
            var g = new shape.Group(transform.Scaling(2, 2, 2));
            var s = new shape.Sphere(transform.Translation(5, 0, 0));

            g.Add(s);

            var r  = new Ray(pt.Point(10, 0, -10), pt.Vector(0, 0, 1));
            var xs = g.Intersect(r);

            Assert.Equal(2, xs.Count());
        }
コード例 #8
0
        private shapes.Group Hexagon()
        {
            var hex = new shapes.Group();

            for (int i = 0; i <= 5; i++)
            {
                var side = HexagonSide();
                side.Transform = transform.RotationY(i * Math.PI / 3);
                hex.Add(side);
            }
            return(hex);
        }
コード例 #9
0
        public void FindingNormalOnChildObject()
        {
            var g1 = new shape.Group(transform.RotationY(Math.PI / 2));
            var g2 = new shape.Group(transform.Scaling(1, 2, 3));

            g1.Add(g2);
            var s = new shape.Sphere(transform.Translation(5, 0, 0));

            g2.Add(s);
            var n = s.NormalAt(pt.Point(1.7311, 1.1547, -5.5774));

            //TODO precision error
            CustomAssert.Equal(pt.Vector(0.2856, 0.4286, -0.8572), n, 4);
        }
コード例 #10
0
        public void ConvertingNormalFromObjectToWorldSpace()
        {
            var g1 = new shape.Group(transform.RotationY(Math.PI / 2));
            var g2 = new shape.Group(transform.Scaling(1, 2, 3));

            g1.Add(g2);
            var s = new shape.Sphere(transform.Translation(5, 0, 0));

            g2.Add(s);
            var value = Math.Sqrt(3) / 3;
            var n     = s.NormalToWorld(pt.Vector(value, value, value));

            CustomAssert.Equal(pt.Vector(0.2857, 0.4286, -0.8571), n, 4);
        }
コード例 #11
0
        public void ConvertingPointFromWorldToObjectSpace()
        {
            var g1 = new shape.Group(transform.RotationY(Math.PI / 2));
            var g2 = new shape.Group(transform.Scaling(2, 2, 2));

            g1.Add(g2);

            var s = new shape.Sphere(transform.Translation(5, 0, 0));

            g2.Add(s);

            var p = s.WorldToObject(pt.Point(-2, 0, -10));

            CustomAssert.Equal(pt.Point(0, 0, -1), p, 5);
        }
コード例 #12
0
        public void SubGroupFromListChildren()
        {
            var s1   = new shape.Sphere();
            var s2   = new shape.Sphere();
            var g    = new shape.Group();
            var list = new List <shape.Shape>()
            {
                s1, s2
            };

            g.Subgroup(list);

            Assert.Single(g.Children);
            Assert.IsType <shape.Group>(g.Children[0]);
            Assert.Equal(list, (g.Children[0] as shape.Group).Children);
        }
コード例 #13
0
        public void PartitioningGroupChildren()
        {
            var s1 = new shape.Sphere(transform.Translation(-2, 0, 0));
            var s2 = new shape.Sphere(transform.Translation(2, 0, 0));
            var s3 = new shape.Sphere();
            var g  = new shape.Group();

            g.Add(new List <shape.Shape>()
            {
                s1, s2, s3
            });
            (List <shape.Shape> left, List <shape.Shape> right) = g.Partition();

            Assert.Equal(1, g.Count());
            Assert.Single(left);
            Assert.Equal(s1, left[0]);
            Assert.Single(right);
            Assert.Equal(s2, right[0]);
        }
コード例 #14
0
        public void IntersectingRayWithNonemptyGroup()
        {
            var g  = new shape.Group();
            var s1 = new shape.Sphere();
            var s2 = new shape.Sphere(transform.Translation(0, 0, -3));
            var s3 = new shape.Sphere(transform.Translation(5, 0, 0));

            g.Add(s1);
            g.Add(s2);
            g.Add(s3);

            var r  = new Ray(pt.Point(0, 0, -5), pt.Vector(0, 0, 1));
            var xs = g.Intersect(r);

            Assert.Equal(4, xs.Count());
            Assert.Equal(s2, xs[0].Object);
            Assert.Equal(s2, xs[1].Object);
            Assert.Equal(s1, xs[2].Object);
            Assert.Equal(s1, xs[3].Object);
        }
コード例 #15
0
        public void GroupHasBoundingBoxContainsItsChildren()
        {
            var s = new shape.Sphere()
            {
                Transform = transform.Translation(2, 5, -3) * transform.Scaling(2, 2, 2)
            };
            var c = new shape.Cylinder()
            {
                Minimum   = -2,
                Maximum   = 2,
                Transform = transform.Translation(-4, -1, 4) * transform.Scaling(0.5, 1, 0.5)
            };
            var shape = new shape.Group();

            shape.Add(s);
            shape.Add(c);

            var box = shape.Bounds();

            Assert.Equal(pt.Point(-4.5, -3, -5), box.Minimum);
            Assert.Equal(pt.Point(4, 7, 4.5), box.Maximum);
        }
コード例 #16
0
        public RTF.Canvas CreateWorld()
        {
            var camera = new RTF.Camera(100, 100, 0.785)
            {
                Transform = transform.ViewTransform(
                    point.Point(-6, 6, -10),
                    point.Point(6, 0, 6),
                    point.Vector(-0.45, 1, 0))
            };

            var light1 = new RTF.Light(point.Point(50, 100, -50), RTF.Color.White);
            var light2 = new RTF.Light(point.Point(-400, 50, -10), RTF.Color.White);

            var whiteMaterial = new RTF.Material()
            {
                Color = RTF.Color.White,
                Diffuse = 0.7,
                Ambient = 0.1,
                Specular = 0,
                Reflective = 0.1
            };
            var blueMaterial = new RTF.Material()
            {
                Color = new RTF.Color(0.537, 0.831, 0.914),
                Diffuse = 0.7,
                Ambient = 0.1,
                Specular = 0,
                Reflective = 0.1
            };
            var redMaterial = new RTF.Material()
            {
                Color = new RTF.Color(0.941, 0.322, 0.388),
                Diffuse = 0.7,
                Ambient = 0.1,
                Specular = 0,
                Reflective = 0.1
            };
            var purpleMaterial = new RTF.Material()
            {
                Color = new RTF.Color(0.373, 0.404, 0.550),
                Diffuse = 0.7,
                Ambient = 0.1,
                Specular = 0,
                Reflective = 0.1
            };

            var standardTransform = transform.Translation(1, -1, 1) * transform.Scaling(0.5, 0.5, 0.5);
            var largeObject = standardTransform * transform.Scaling(3.5, 3.5, 3.5);
            var mediumObject = standardTransform * transform.Scaling(3, 3, 3);
            var smallObject = standardTransform * transform.Scaling(2, 2, 2);

            var whitebackdrop = new shapes.Plane()
            {
                Material = new RTF.Material()
                {
                    Color = RTF.Color.White,
                    Ambient = 1,
                    Diffuse = 0,
                    Specular = 0
                },
                Transform = transform.RotationX(Math.PI / 2) * transform.Translation(0, 0, 500)
            };

            var group = new shapes.Group(
                new List<shapes.Shape>()
                {
                    new shapes.Sphere()
                    {
                        Material = new RTF.Material()
                        {
                            Color = purpleMaterial.Color,
                            Diffuse = 0.2,
                            Ambient = 0,
                            Specular = 1,
                            Shininess = 200,
                            Reflective = 0.7,
                            Transparency = 0.7,
                            RefractiveIndex = 1.5
                        },
                        Transform = largeObject
                    },
                    new shapes.Cube(whiteMaterial, mediumObject * transform.Translation(4, 0, 0)),
                    new shapes.Cube(blueMaterial, largeObject * transform.Translation(8.5, 1.5, -0.5)),
                    new shapes.Cube(redMaterial, largeObject * transform.Translation(0, 0, 4)),
                    new shapes.Cube(whiteMaterial, smallObject * transform.Translation(4, 0, 4)),
                    new shapes.Cube(purpleMaterial, mediumObject * transform.Translation(7.5, 0.5, 4)),
                    new shapes.Cube(whiteMaterial, mediumObject * transform.Translation(-0.25, 0.25, 8)),
                    new shapes.Cube(blueMaterial,  largeObject * transform.Translation(4, 1, 7.5)),
                    new shapes.Cube(redMaterial,  mediumObject * transform.Translation(10, 2, 7.5)),
                    new shapes.Cube(whiteMaterial,  smallObject * transform.Translation(8, 2, 12)),
                    new shapes.Cube(whiteMaterial,  smallObject * transform.Translation(20, 1, 9)),
                    new shapes.Cube(blueMaterial,  largeObject * transform.Translation(-0.5, -5, 0.25)),
                    new shapes.Cube(redMaterial,  largeObject * transform.Translation(4, -4, 0)),
                    new shapes.Cube(whiteMaterial,  largeObject * transform.Translation(8.5, -4, 0)),
                    new shapes.Cube(whiteMaterial,  largeObject * transform.Translation(0, -4, 4)),
                    new shapes.Cube(purpleMaterial,  largeObject * transform.Translation(-0.5, -4.5, 8)),
                    new shapes.Cube(whiteMaterial,  largeObject * transform.Translation(0, -8, 4)),
                    new shapes.Cube(whiteMaterial,  largeObject * transform.Translation(-0.5, -8.5, 8))
                }
            );


            var world = new RTF.World()
            {
                Lights = new List<RTF.Light>() { light1, light2 },

                Objects = new List<shapes.Shape>()
                {
                    group, whitebackdrop
                }
            };

            return RTF.Canvas.Render(camera, world);
        }
コード例 #17
0
        public void ShapeParentIsNothing()
        {
            var g = new shape.Group();

            Assert.Null(g.Parent);
        }