Esempio n. 1
0
        static void Main(string[] args)
        {
            string root = System.IO.Path.Combine("..", "..", "..", "Numerics");

            var integer_path = System.IO.Path.Combine(root, "Integer.cs");
            System.IO.File.Delete(integer_path);
            foreach (var size in Integer.Sizes)
            {
                var integer = new Integer(size, true);
                integer.Generate();

                System.IO.File.AppendAllText(integer_path, integer.Text);
                Console.WriteLine("Done - " + integer.Name);

                var uinteger = new Integer(size, false);
                uinteger.Generate();

                System.IO.File.AppendAllText(integer_path, uinteger.Text);
                Console.WriteLine("Done - " + uinteger.Name);
            }

            foreach (var type in Color.Types)
            {
                var color = new Color(type);
                color.Generate();

                var path = System.IO.Path.Combine(root, color.Name + ".cs");
                System.IO.File.WriteAllText(path, color.Text);
                Console.WriteLine("Done - " + color.Name);
            }

            foreach (var type in Quaternion.Types)
            {
                var quaternion = new Quaternion(type);
                quaternion.Generate();

                var path = System.IO.Path.Combine(root, "Geometry", quaternion.Name + ".cs");
                System.IO.File.WriteAllText(path, quaternion.Text);
                Console.WriteLine("Done - " + quaternion.Name);
            } 
            
            foreach (var type in Plane.Types)
            {
                var plane = new Plane(type);
                plane.Generate();

                var path = System.IO.Path.Combine(root, "Geometry", plane.Name + ".cs");
                System.IO.File.WriteAllText(path, plane.Text);
                Console.WriteLine("Done - " + plane.Name);
            }

            foreach (int dimension in Vector.Sizes)
            {
                foreach (var type in Vector.Types)
                {
                    var vector = new Vector(type, dimension);
                    vector.Generate();

                    var path = System.IO.Path.Combine(root, "Vectors", vector.Name + ".cs");
                    System.IO.File.WriteAllText(path, vector.Text);
                    Console.WriteLine("Done - " + vector.Name);
                }
            }

            foreach (int rows in Matrix.Sizes)
            {
                foreach (int columns in Matrix.Sizes)
                {
                    foreach (var type in Matrix.Types)
                    {
                        var matrix = new Matrix(type, rows, columns);
                        matrix.Generate();
                        var path = System.IO.Path.Combine(root, "Matrices", matrix.Name + ".cs");
                        System.IO.File.WriteAllText(path, matrix.Text);
                        Console.WriteLine("Done - " + matrix.Name);
                    }
                }
            }

            foreach (var type in Shapes.Types)
            {
                var rpath = System.IO.Path.Combine(root, "Geometry");

                var line1 = new Line(type, 1);
                line1.Generate();
                System.IO.File.WriteAllText(System.IO.Path.Combine(rpath, line1.Name + ".cs"), line1.Text);
                Console.WriteLine("Done - " + line1.Name);

                foreach (int dimension in Shapes.Sizes)
                {
                    var point = new Point(type, dimension);
                    point.Generate();
                    System.IO.File.WriteAllText(System.IO.Path.Combine(rpath, point.Name + ".cs"), point.Text);
                    Console.WriteLine("Done - " + point.Name);

                    var size = new Size(type, dimension);
                    size.Generate();
                    System.IO.File.WriteAllText(System.IO.Path.Combine(rpath, size.Name + ".cs"), size.Text);
                    Console.WriteLine("Done - " + size.Name);

                    var polygon = new Polygon(type, dimension);
                    polygon.Generate();
                    System.IO.File.WriteAllText(System.IO.Path.Combine(rpath, polygon.Name + ".cs"), polygon.Text);
                    Console.WriteLine("Done - " + polygon.Name);

                    var line = new Line(type, dimension);
                    line.Generate();
                    System.IO.File.WriteAllText(System.IO.Path.Combine(rpath, line.Name + ".cs"), line.Text);
                    Console.WriteLine("Done - " + line.Name);
                }

                var rectangle = new Rectangle(type);
                rectangle.Generate();
                System.IO.File.WriteAllText(System.IO.Path.Combine(rpath, rectangle.Name + ".cs"), rectangle.Text);
                Console.WriteLine("Done - " + rectangle.Name);

                var box = new Box(type);
                box.Generate();
                System.IO.File.WriteAllText(System.IO.Path.Combine(rpath, box.Name + ".cs"), box.Text);
                Console.WriteLine("Done - " + box.Name);

                var ellipse = new Ellipse(type);
                ellipse.Generate();
                System.IO.File.WriteAllText(System.IO.Path.Combine(rpath, ellipse.Name + ".cs"), ellipse.Text);
                Console.WriteLine("Done - " + ellipse.Name);

                var circle = new Circle(type);
                circle.Generate();
                System.IO.File.WriteAllText(System.IO.Path.Combine(rpath, circle.Name + ".cs"), circle.Text);
                Console.WriteLine("Done - " + circle.Name);

                var sphere = new Sphere(type);
                sphere.Generate();
                System.IO.File.WriteAllText(System.IO.Path.Combine(rpath, sphere.Name + ".cs"), sphere.Text);
                Console.WriteLine("Done - " + sphere.Name);
            }

            foreach (var type in Ray.Types)
            {
                var ray = new Ray(type);
                ray.Generate();

                var path = System.IO.Path.Combine(root, "Geometry", ray.Name + ".cs");
                System.IO.File.WriteAllText(path, ray.Text);
                Console.WriteLine("Done - " + ray.Name);
            }
        }
Esempio n. 2
0
        void Conversions()
        {
            WriteLine("#region Conversions");

            foreach (var type in Shapes.Types.Where(t => t != Type))
            {
                string imex = type.IsImplicitlyConvertibleTo(Type) ? "implicit" : "explicit";

                Sphere other = new Sphere(type);

                var casts = string.Format("({0})value.X, ({0})value.Y, ({0})value.Z, ({0})value.Radius", Type);

                WriteLine("/// <summary>");
                WriteLine("/// Defines an {0} conversion of a {1} value to a {2}.", imex, other, Name);
                WriteLine("/// </summary>");
                WriteLine("/// <param name=\"value\">The value to convert to a {0}.</param>", Name);
                WriteLine("/// <returns>A {0} that has all components equal to value.</returns>", Name);
                if (Type.IsCLSCompliant && !type.IsCLSCompliant)
                {
                    WriteLine("[CLSCompliant(false)]");
                }
                WriteLine("public static {0} operator {1}({2} value)", imex, Name, other);
                WriteLine("{");
                Indent();
                WriteLine("return new {0}({1});", Name, casts);
                Dedent();
                WriteLine("}");
            }

            WriteLine("#endregion");
        }
Esempio n. 3
0
File: Ray.cs Progetto: Frassle/Ibasa
        void Functions()
        {
            WriteLine("/// <summary>");
            WriteLine("/// Provides static methods for ray functions.");
            WriteLine("/// </summary>");
            WriteLine("public static partial class Ray");
            WriteLine("{");
            Indent();

            #region Binary
            WriteLine("#region Binary");
            WriteLine("/// <summary>");
            WriteLine("/// Writes the given <see cref=\"{0}\"/> to a Ibasa.IO.BinaryWriter.", Name);
            WriteLine("/// </summary>");
            WriteLine("public static void Write(this Ibasa.IO.BinaryWriter writer, {0} ray)", Name);
            WriteLine("{");
            Indent();
            WriteLine("Point.Write(writer, ray.Position);");
            WriteLine("Vector.Write(writer, ray.Direction);");
            Dedent();
            WriteLine("}");
            WriteLine("/// <summary>");
            WriteLine("/// Reads a <see cref=\"{0}\"/> to a Ibasa.IO.BinaryReader.", Name);
            WriteLine("/// </summary>");
            WriteLine("public static {0} Read{0}(this Ibasa.IO.BinaryReader reader)", Name);
            WriteLine("{");
            Indent();
            WriteLine("return new {0}(Point.ReadPoint3{1}(reader), Vector.ReadVector3{1}(reader));", Name, Type.Suffix);
            Dedent();
            WriteLine("}");
            WriteLine("#endregion");
            #endregion

            #region Equatable
            WriteLine("#region Equatable");
            WriteLine("/// <summary>");
            WriteLine("/// Returns a value that indicates whether two rays are equal.");
            WriteLine("/// </summary>");
            WriteLine("/// <param name=\"left\">The first ray to compare.</param>");
            WriteLine("/// <param name=\"right\">The second ray to compare.</param>");
            WriteLine("/// <returns>true if the left and right are equal; otherwise, false.</returns>");
            WriteLine("public static bool Equals({0} left, {0} right)", Name);
            WriteLine("{");
            Indent();
            WriteLine("return left == right;");
            Dedent();
            WriteLine("}");
            WriteLine("#endregion");
            #endregion

            #region Functions
            WriteLine("#region Functions");
            WriteLine("/// <summary>");
            WriteLine("/// Returns the point in the ray at position t.");
            WriteLine("/// </summary>");
            WriteLine("/// <param name=\"ray\">The ray to parametrize.</param>");
            WriteLine("/// <param name=\"t\">The paramater t.</param>");
            WriteLine("/// <returns>The point at t.</returns>");
            WriteLine("public static Point3{0} Parametrize({1} ray, {2} t)", Type.Suffix, Name, Type);
            WriteLine("{");
            Indent();
            WriteLine("return ray.Position + (t * ray.Direction);");
            Dedent();
            WriteLine("}");
            WriteLine("#endregion");
            #endregion

            #region Intersects
            WriteLine("#region Intersects");

            foreach(var type in Shapes.Types)
            {
                var box = new Box(type);

                WriteLine("/// <summary>");
                WriteLine("/// Determines whether a ray intersects the specified box.");
                WriteLine("/// </summary>");
                WriteLine("/// <param name=\"ray\">The ray which will be tested for intersection.</param>");
                WriteLine("/// <param name=\"box\">A box that will be tested for intersection.</param>");
                WriteLine("/// <returns>Distance at which the ray intersects the box or null if there is no intersection.</returns>");
                WriteLine("public static {0}? Intersects({1} ray, {2} box)", Type, Name, box);
                WriteLine("{");
                Indent();
                WriteLine("return null;");
                WriteLine("var invDir = Vector.Reciprocal(ray.Direction);");
                WriteLine("bool signX = invDir.X < 0;");
                WriteLine("bool signY = invDir.Y < 0;");
                WriteLine("bool signZ = invDir.Z < 0;");
                WriteLine("var min = signX ? box.Right : box.Left;");
                WriteLine("var max = signX ? box.Left : box.Right;");
                WriteLine("var txmin = (min - ray.Position.X) * invDir.X;");
                WriteLine("var txmax = (max - ray.Position.X) * invDir.X;");
                
                WriteLine("min = signY ? box.Top : box.Bottom;");
                WriteLine("max = signY ? box.Bottom : box.Top;");
                WriteLine("var tymin = (min - ray.Position.Y) * invDir.Y;");
                WriteLine("var tymax = (max - ray.Position.Y) * invDir.Y;");

                WriteLine("if ((txmin > tymax) || (tymin > txmax)) { return null; }");
                WriteLine("if (tymin > txmin) { txmin = tymin; }");
                WriteLine("if (tymax < txmax) { txmax = tymax; }");
                
                WriteLine("min = signZ ? box.Back : box.Front;");
                WriteLine("max = signZ ? box.Front : box.Back;");
                WriteLine("var tzmin = (min - ray.Position.Z) * invDir.Z;");
                WriteLine("var tzmax = (max - ray.Position.Z) * invDir.Z;");

                WriteLine("if ((txmin > tzmax) || (tzmin > txmax)) { return null; }");
                WriteLine("if (tzmin > txmin) { txmin = tzmin; }");
                WriteLine("if (tzmax < txmax) { txmax = tzmax; }");

                WriteLine("if (txmin < double.PositiveInfinity && txmax >= 0)");
                WriteLine("{");
                Indent();
                WriteLine("return ({0})txmin;", Type);
                Dedent();
                WriteLine("}");
                WriteLine("else");                
                WriteLine("{");
                Indent();
                WriteLine("return null;");
                Dedent();
                WriteLine("}");
                Dedent();
                WriteLine("}");

                var sphere = new Sphere(type);

                WriteLine("/// <summary>");
                WriteLine("/// Determines whether a ray intersects the specified sphere.");
                WriteLine("/// </summary>");
                WriteLine("/// <param name=\"ray\">The ray which will be tested for intersection.</param>");
                WriteLine("/// <param name=\"sphere\">A sphere that will be tested for intersection.</param>");
                WriteLine("/// <returns>Distance at which the ray intersects the sphere or null if there is no intersection.</returns>");
                WriteLine("public static {0}? Intersects({1} ray, {2} sphere)", Type, Name, sphere);
                WriteLine("{");
                Indent();
                WriteLine("var distance = sphere.Center - ray.Position;");
                WriteLine("var pyth = Vector.AbsoluteSquared(distance);");
    		    WriteLine("var rr = sphere.Radius * sphere.Radius;");
    		    WriteLine("if( pyth <= rr ) { return 0; }");
                WriteLine("double dot = Vector.Dot(distance, ray.Direction);");
    		    WriteLine("if( dot < 0 ) { return null; }");
    		    WriteLine("var temp = pyth - (dot * dot);");
    		    WriteLine("if( temp > rr ) { return null; }");
                WriteLine("return ({0})(dot - Functions.Sqrt(rr-temp));", Type);
                Dedent();
                WriteLine("}");
            }
            WriteLine("#endregion");
            #endregion

            Dedent();
            WriteLine("}");
        }