Пример #1
0
        public void CanCalculateMagnitudeOfVector()
        {
            // Arrange
            var v = new Vector(6, 8, 0);

            // Act
            var mag = v.Magnitude;

            // Assert
            Assert.AreEqual(10, mag);
        }
Пример #2
0
        public void CanCalculateMagnitudeOfVector_2()
        {
            // Arrange
            var v = new Vector(1, 1, 1);

            // Act
            var mag = v.Magnitude;

            // Assert
            Assert.AreEqual(Math.Sqrt(3), mag);
        }
Пример #3
0
        public void CanCalculateNormalToSphere()
        {
            // Arrange
            var sphere = new Sphere() { Center = new Vector(0, 0, 0), Radius = 1 };
            var position = new Vector(1,0,0);

            // Act
            var normal = sphere.Normal(position);

            // Assert
            Assert.NotNull(normal);
            Assert.IsTrue(normal.Equals(new Vector(1,0,0)));
        }
Пример #4
0
        public void CanCalculateCrossProduct()
        {
            // Arrange
            var v1 = new Vector(1, 2, 3);
            var v2 = new Vector(2, 3, 4);

            // Act
            var cross = v1.Cross(v2);
            var cross_static = Vector.Cross(v1, v2);

            // Assert
            Assert.AreEqual(new Vector(-1, 2, -1), cross);
            Assert.AreEqual(new Vector(-1, 2, -1), cross_static);
        }
Пример #5
0
        public void CanCalculateDotProduct()
        {
            // Arrange
            var v1 = new Vector(1, 2, 3);
            var v2 = new Vector(2, 3, 4);

            // Act
            var dot = v1.Dot(v2);
            var dot_static = Vector.Dot(v1, v2);

            // Assert
            Assert.AreEqual(20, dot);
            Assert.AreEqual(20, dot_static);
        }
Пример #6
0
        public void CanGetRayDirection()
        {
            // Arrange
            var pos = new Vector(0, 0, 0);
            var dir = new Vector(0, 0, 1);
            var screen = new Screen(400, 300);
            var camera = new Camera(pos, dir, screen);

            // Act
            var ray = camera.GetRayDirection(100, 100);

            // Assert
            Assert.Less(Math.Abs((-1.0 / 8.0) - ray.X), 0.005);
            Assert.Less(Math.Abs((1.0 / 12.0) - ray.Y), 0.005);
            Assert.Less(Math.Abs((1.0) - ray.Z), 0.02);
        }
Пример #7
0
        public void CanAddVectors()
        {
            // Arrange
            var v1 = new Vector(1, 1, 1);
            var v2 = new Vector(2, 3, 4);

            // Act
            var sum_member = v1.Plus(v2);
            var sum_static = Vector.Plus(v1, v2);
            var sum_operator = v1 + v2;

            // Assert
            var result = new Vector(3,4,5);
            Assert.IsTrue(sum_member.Equals(result));
            Assert.IsTrue(sum_static.Equals(result));
            Assert.IsTrue(sum_operator.Equals(result));
        }
Пример #8
0
        public void CanConstructNewCamera()
        {
            // Arrange
            var pos = new Vector(0, 0, 0);
            var dir = new Vector(0, 0, 1);
            var screen = new Screen(100, 100);

            // Act
            var camera = new Camera(pos, dir, screen);

            // Assert
            Assert.AreEqual(pos, camera.Position);

            // Forward is a unit vector pointing in 'lookAt' from 'position'
            Assert.IsTrue(camera.Forward.Equals(new Vector(0, 0, 1)));

            // Right
            Assert.IsTrue(camera.Right.Equals(new Vector(1,0,0)));

            // Up
            Assert.IsTrue(camera.Up.Equals(new Vector(0,1,0)));
        }
Пример #9
0
 /// <summary>
 /// Subtract a vector from the current vector
 /// </summary>
 /// <param name="v">The vector to subtract</param>
 /// <returns>The resultant vector</returns>
 public Vector Minus(Vector v)
 {
     return new Vector(this.X - v.X, this.Y - v.Y, this.Z - v.Z);
 }
Пример #10
0
 /// <summary>
 /// Calculate the cross-product of this vector with another
 /// </summary>
 /// <param name="v">The vector to cross with</param>
 /// <returns>The resultant vector</returns>
 public Vector Cross(Vector v)
 {
     return new Vector(((this.Y * v.Z) - (this.Z * v.Y)),
                       ((this.Z * v.X) - (this.X * v.Z)),
                       ((this.X * v.Y) - (this.Y * v.X)));
 }
Пример #11
0
 /// <summary>
 /// Calculate the dot-product of this vector with another
 /// </summary>
 /// <param name="v">The vector to dot with</param>
 /// <returns>The result</returns>
 public double Dot(Vector v)
 {
     return (this.X * v.X) + (this.Y * v.Y) + (this.Z * v.Z);
 }
Пример #12
0
 /// <summary>
 /// Add two vectors together
 /// </summary>
 /// <param name="v1">The first vector</param>
 /// <param name="v2">The second vector</param>
 /// <returns>The resultant vector</returns>
 public static Vector Plus(Vector v1, Vector v2)
 {
     return v1.Plus(v2);
 }
Пример #13
0
 /// <summary>
 /// Multiply a vector by a constant value
 /// </summary>
 /// <param name="v">The vector to multiply</param>
 /// <param name="n">The value to multiply by</param>
 /// <returns>The resultant vector</returns>
 public static Vector Times(double n, Vector v)
 {
     return v.Times(n);
 }
Пример #14
0
 /// <summary>
 /// Determinue if two vectors are equal
 /// </summary>
 /// <param name="v1">The first vector</param>
 /// <param name="v2">The second vector</param>
 /// <returns>True if the vectors are equal</returns>
 public static bool Equals(Vector v1, Vector v2)
 {
     return v1.Equals(v2);
 }
Пример #15
0
 /// <summary>
 /// Subtract one vector from another
 /// </summary>
 /// <param name="v1">The first vector</param>
 /// <param name="v2">The second vector</param>
 /// <returns>The resultant vector</returns>
 public static Vector Minus(Vector v1, Vector v2)
 {
     return v1.Minus(v2);
 }
Пример #16
0
        /// <summary>
        /// Get the color of an object at a point of intersection by tracing back to the light sources.
        /// This creates shadows and adds color if the lights are off-white.
        /// </summary>
        /// <param name="element">The object being intersected</param>
        /// <param name="position">The point of intersection on the object</param>
        /// <param name="norm">The normal at the point of intersection</param>
        /// <param name="reflectDirection">The direction of the light reflection off of the surface</param>
        /// <param name="scene">The scene description</param>
        /// <returns>The color of the object due to the scenes lighting</returns>
        private Color GetNaturalColor(SceneObject element, Vector position, Vector norm, Vector reflectDirection, Scene scene)
        {
            var ret = new Color(0, 0, 0);
            foreach (Light light in scene.Lights)
            {
                // Direction from the point of intersction to the light source
                Vector lightDirection = light.Position - position;
                Vector lightUnitVector = lightDirection.Normalise;

                // Perform an intersection test in the direction of the light source to determine if we are in a shadow
                double intersectionDistance = scene.TestRay(new Ray() { Start = position, Direction = lightUnitVector });
                bool isInShadow = !((intersectionDistance > lightDirection.Magnitude) || (intersectionDistance == 0));

                if (!isInShadow)
                {
                    // Check the light is shining on the front of the object.  Use the angle to determine
                    // the brightness of the light.
                    double angleOfIllumination = Vector.Dot(lightUnitVector, norm);
                    Color lightColor = angleOfIllumination > 0 ? angleOfIllumination * light.Color : new Color(0, 0, 0);

                    // Determine of the light is in the direction of a reflection, leading to a specular effect
                    double specular = Vector.Dot(lightUnitVector, reflectDirection.Normalise);

                    // Calculate color of object based on surface qualities
                    Color specularColor = specular > 0 ? Math.Pow(specular, element.Surface.Roughness) * light.Color : new Color(0, 0, 0);
                    ret = ret + (element.Surface.Diffuse(position) * lightColor) + (element.Surface.Specular(position) * specularColor);
                }
            }

            return ret;
        }
Пример #17
0
 /// <summary>
 /// Calculate the dot-product of two vectors
 /// </summary>
 /// <param name="v1">The first vector</param>
 /// <param name="v2">The second vector</param>
 /// <returns>The result</returns>
 public static double Dot(Vector v1, Vector v2)
 {
     return v1.Dot(v2);
 }
Пример #18
0
 /// <summary>
 /// Add a vector to the current vector
 /// </summary>
 /// <param name="v">The vector to add</param>
 /// <returns>The resultant vector</returns>
 public Vector Plus(Vector v)
 {
     return new Vector(this.X + v.X, this.Y + v.Y, this.Z + v.Z);
 }
Пример #19
0
        public void CanSubtractVectors()
        {
            // Arrange
            var v1 = new Vector(2, 3, 4);
            var v2 = new Vector(1, 1, 1);

            // Act
            var sum_member = v1.Minus(v2);
            var sum_static = Vector.Minus(v1, v2);
            var sum_operator = v1 - v2;

            // Assert
            var result = new Vector(1, 2, 3);
            Assert.IsTrue(sum_member.Equals(result));
            Assert.IsTrue(sum_static.Equals(result));
            Assert.IsTrue(sum_operator.Equals(result));
        }
Пример #20
0
        public void CanMultiplyVectorByConstant()
        {
            // Arrange
            var v = new Vector(2, 3, 4);

            // Act
            var sum_member = v.Times(2);
            var sum_static = Vector.Times(2,v);
            var sum_operator = 2 * v;

            // Assert
            var result = new Vector(4, 6, 8);
            Assert.IsTrue(sum_member.Equals(result));
            Assert.IsTrue(sum_static.Equals(result));
            Assert.IsTrue(sum_operator.Equals(result));
        }
Пример #21
0
        public void DifferentVectorsAreNotEqual()
        {
            // Arrange
            var v1 = new Vector(1, 1, 1);
            var v2 = new Vector(2, 2, 2);

            // Act
            var b = (v1.Equals(v2));

            // Assert
            Assert.IsFalse(b);
        }
Пример #22
0
        public void IdenticalVectorsAreEqual()
        {
            // Arrange
            var v1 = new Vector(1, 1, 1);
            var v2 = new Vector(1, 1, 1);

            // Act
            var b = (v1.Equals(v2));

            // Assert
            Assert.IsTrue(b);
        }
Пример #23
0
        public void ObjectAndVectorsAreNotEqual()
        {
            // Arrange
            var v1 = new Vector(1, 1, 1);
            var obj = new Object(); ;

            // Act
            var b = (v1.Equals(obj));

            // Assert
            Assert.IsFalse(b);
        }
Пример #24
0
        public void IdenticalVectorsAreEqual_Static()
        {
            // Arrange
            var v1 = new Vector(1, 1, 1);
            var v2 = new Vector(1, 1, 1);

            // Act
            var b = Vector.Equals(v1, v2);

            // Assert
            Assert.IsTrue(b);
        }
Пример #25
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="element"></param>
 /// <param name="position"></param>
 /// <param name="norm"></param>
 /// <param name="rayDirection"></param>
 /// <param name="scene"></param>
 /// <param name="depth"></param>
 /// <returns></returns>
 private Color GetReflectionColor(SceneObject element, Vector position, Vector norm, Vector rayDirection, Scene scene, int depth)
 {
     return element.Surface.Reflectiveness(position) * this.TraceRay(new Ray() { Start = position, Direction = rayDirection }, scene, depth + 1);
 }
Пример #26
0
        public void CanNormaliseVector()
        {
            // Arrange
            var v = new Vector(3, 4, 0);

            // Act
            var norm = v.Normalise;

            // Assert
            // using * 0.2 so that we get the same double inaccuracy
            var result = new Vector(3.0 *0.2, 4.0 * 0.2, 0.0);
            Assert.AreEqual(result, norm);
        }
Пример #27
0
        public void CanConvertToNiceString()
        {
            // Arrange
            var v1 = new Vector(1, 2, 3);

            // Act
            var str = v1.ToString();

            // Assert
            Assert.AreEqual("X=1 Y=2 Z=3", str);
        }
Пример #28
0
 /// <summary>
 /// Calculate the cross-product of two vectors
 /// </summary>
 /// <param name="v1">The first vector</param>
 /// <param name="v2">The second vector</param>
 /// <returns>The resultant vector</returns>
 public static Vector Cross(Vector v1, Vector v2)
 {
     return v1.Cross(v2);
 }
Пример #29
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="element"></param>
        /// <param name="position"></param>
        /// <param name="norm"></param>
        /// <param name="rayDirection"></param>
        /// <param name="scene"></param>
        /// <returns></returns>
        private Color GetNaturalColor(SceneObject element, Vector position, Vector norm, Vector rayDirection, Scene scene)
        {
            var ret = new Color(0, 0, 0);
            foreach (Light light in scene.Lights)
            {
                Vector lightDirection = light.Position - position;
                Vector lightUnitVector = lightDirection.Normalise;
                double intersectionDistance = scene.TestRay(new Ray() { Start = position, Direction = lightUnitVector });
                bool isInShadow = !((intersectionDistance > lightDirection.Magnitude) || (intersectionDistance == 0));

                if (!isInShadow)
                {
                    double angleOfIllumination = Vector.Dot(lightUnitVector, norm);
                    Color lcolor = angleOfIllumination > 0 ? angleOfIllumination * light.Color : new Color(0, 0, 0);
                    double specular = Vector.Dot(lightUnitVector, rayDirection.Normalise);
                    Color scolor = specular > 0 ? Color.Times(Math.Pow(specular, element.Surface.Roughness), light.Color) : new Color(0, 0, 0);
                    ret = ret + (element.Surface.Diffuse(position) * lcolor) + (element.Surface.Specular(position) * scolor);
                }
            }

            return ret;
        }
Пример #30
0
        /// <summary>
        /// Calculate the color being reflected at a point of intersection
        /// </summary>
        /// <param name="element">The object being intersected</param>
        /// <param name="position">The position on the objects surface</param>
        /// <param name="norm">The normal at the point of intersection</param>
        /// <param name="reflectDirection">The direction of the reflected ray</param>
        /// <param name="scene">The scene description</param>
        /// <param name="depth">The current depth of recursion</param>
        /// <returns>The color being reflected</returns>
        private Color GetReflectionColor(SceneObject element, Vector position, Vector norm, Vector reflectDirection, Scene scene, int depth)
        {
            // Only calculate reflected color if the surface is reflective
            var reflectiveness = element.Surface.Reflectiveness(position);
            if (reflectiveness > 0)
            {
                return reflectiveness * this.TraceRay(new Ray() { Start = position, Direction = reflectDirection }, scene, depth + 1);
            }

            return new Color(0, 0, 0);
        }