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); } }
void Conversions() { WriteLine("#region Conversions"); foreach (var type in Shapes.Types.Where(t => t != Type)) { string imex = type.IsImplicitlyConvertibleTo(Type) ? "implicit" : "explicit"; Size other = new Size(type, Dimension); var casts = string.Join(", ", Components.Select(component => string.Format("({0})value.{1}", Type, component))); 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"); }
void Functions() { WriteLine("/// <summary>"); WriteLine("/// Provides static methods for size functions."); WriteLine("/// </summary>"); WriteLine("public static partial class Size"); WriteLine("{"); Indent(); var result = new Size(Type.PositiveType, Dimension); #region Binary WriteLine("#region Binary"); WriteLine("/// <summary>"); WriteLine("/// Writes the given <see cref=\"{0}\"/> to an <see cref=\"Ibasa.IO.BinaryWriter\">.", Name); WriteLine("/// </summary>"); WriteLine("public static void Write(this Ibasa.IO.BinaryWriter writer, {0} size)", Name); WriteLine("{"); Indent(); for (int i = 0; i < Dimension; ++i) { WriteLine("writer.Write(size.{0});", Components[i]); } Dedent(); WriteLine("}"); WriteLine("/// <summary>"); WriteLine("/// Reads a <see cref=\"{0}\"/> from an <see cref=\"Ibasa.IO.BinaryWriter\">.", Name); WriteLine("/// </summary>"); WriteLine("public static {0} Read{0}(this Ibasa.IO.BinaryReader reader)", Name); WriteLine("{"); Indent(); WriteLine("return new {0}({1});", Name, string.Join(", ", Components.Select(c => string.Format("reader.Read{0}()", Type.CLRName)))); Dedent(); WriteLine("}"); WriteLine("#endregion"); #endregion #region Operations WriteLine("#region Operations"); WriteLine("/// <summary>"); WriteLine("/// Returns the product of a size and scalar."); WriteLine("/// </summary>"); WriteLine("/// <param name=\"size\">The size to multiply.</param>"); WriteLine("/// <param name=\"scalar\">The scalar to multiply.</param>"); WriteLine("/// <returns>The product of the left and right parameters.</returns>"); WriteLine("public static {0} Multiply({1} size, {2} scalar)", result, Name, result.Type); WriteLine("{"); Indent(); WriteLine("Contract.Requires(0 <= scalar);"); WriteLine("return new {0}({1});", result, string.Join(", ", Components.Select(component => string.Format("size.{0} * scalar", component)))); Dedent(); WriteLine("}"); WriteLine("/// <summary>"); WriteLine("/// Divides a size by a scalar and returns the result."); WriteLine("/// </summary>"); WriteLine("/// <param name=\"size\">The size to be divided (the dividend).</param>"); WriteLine("/// <param name=\"scalar\">The scalar to divide by (the divisor).</param>"); WriteLine("/// <returns>The result of dividing left by right (the quotient).</returns>"); WriteLine("public static {0} Divide({1} size, {2} scalar)", result, Name, result.Type); WriteLine("{"); Indent(); WriteLine("Contract.Requires(0 <= scalar);"); WriteLine("return new {0}({1});", result, string.Join(", ", Components.Select(component => string.Format("size.{0} / scalar", component)))); Dedent(); WriteLine("}"); WriteLine("#endregion"); #endregion #region Equatable WriteLine("#region Equatable"); WriteLine("/// <summary>"); WriteLine("/// Returns a value that indicates whether two sizes are equal."); WriteLine("/// </summary>"); WriteLine("/// <param name=\"left\">The first size to compare.</param>"); WriteLine("/// <param name=\"right\">The second size 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 Per component WriteLine("#region Per component"); WriteLine("#region Transform"); foreach (var type in Shapes.Types) { var transform = new Size(type, Dimension); WriteLine("/// <summary>"); WriteLine("/// Transforms the components of a size and returns the result."); WriteLine("/// </summary>"); WriteLine("/// <param name=\"value\">The size to transform.</param>"); WriteLine("/// <param name=\"transformer\">A transform function to apply to each component.</param>"); WriteLine("/// <returns>The result of transforming each component of value.</returns>"); if (!Type.IsCLSCompliant) { WriteLine("[CLSCompliant(false)]"); } WriteLine("public static {0} Transform({1} value, Func<{2}, {3}> transformer)", transform, Name, Type, transform.Type); WriteLine("{"); Indent(); WriteLine("return new {0}({1});", transform, string.Join(", ", Components.Select(component => string.Format("transformer(value.{0})", component)))); Dedent(); WriteLine("}"); } WriteLine("#endregion"); WriteLine("/// <summary>"); WriteLine("/// Returns a size that contains the lowest value from each pair of components."); WriteLine("/// </summary>"); WriteLine("/// <param name=\"value1\">The first size.</param>"); WriteLine("/// <param name=\"value2\">The second size.</param>"); WriteLine("/// <returns>The lowest of each component in left and the matching component in right.</returns>"); if (!Type.IsCLSCompliant) { WriteLine("[CLSCompliant(false)]"); } WriteLine("public static {0} Min({0} value1, {0} value2)", Name); WriteLine("{"); Indent(); WriteLine("return new {0}({1});", Name, string.Join(", ", Components.Select(component => string.Format("Functions.Min(value1.{0}, value2.{0})", component)))); Dedent(); WriteLine("}"); WriteLine("/// <summary>"); WriteLine("/// Returns a size that contains the highest value from each pair of components."); WriteLine("/// </summary>"); WriteLine("/// <param name=\"value1\">The first size.</param>"); WriteLine("/// <param name=\"value2\">The second size.</param>"); WriteLine("/// <returns>The highest of each component in left and the matching component in right.</returns>"); if (!Type.IsCLSCompliant) { WriteLine("[CLSCompliant(false)]"); } WriteLine("public static {0} Max({0} value1, {0} value2)", Name); WriteLine("{"); Indent(); WriteLine("return new {0}({1});", Name, string.Join(", ", Components.Select(component => string.Format("Functions.Max(value1.{0}, value2.{0})", component)))); Dedent(); WriteLine("}"); WriteLine("/// <summary>"); WriteLine("/// Constrains each component to a given range."); WriteLine("/// </summary>"); WriteLine("/// <param name=\"value\">A size to constrain.</param>"); WriteLine("/// <param name=\"min\">The minimum values for each component.</param>"); WriteLine("/// <param name=\"max\">The maximum values for each component.</param>"); WriteLine("/// <returns>A size with each component constrained to the given range.</returns>"); if (!Type.IsCLSCompliant) { WriteLine("[CLSCompliant(false)]"); } WriteLine("public static {0} Clamp({0} value, {0} min, {0} max)", Name); WriteLine("{"); Indent(); WriteLine("return new {0}({1});", Name, string.Join(", ", Components.Select(component => string.Format("Functions.Clamp(value.{0}, min.{0}, max.{0})", component)))); Dedent(); WriteLine("}"); if(Type.IsReal) { WriteLine("/// <summary>"); WriteLine("/// Constrains each component to the range 0 to 1."); WriteLine("/// </summary>"); WriteLine("/// <param name=\"value\">A size to saturate.</param>"); WriteLine("/// <returns>A size with each component constrained to the range 0 to 1.</returns>"); if (!Type.IsCLSCompliant) { WriteLine("[CLSCompliant(false)]"); } WriteLine("public static {0} Saturate({0} value)", Name); WriteLine("{"); Indent(); WriteLine("return new {0}({1});", Name, string.Join(", ", Components.Select(component => string.Format("Functions.Saturate(value.{0})", component)))); Dedent(); WriteLine("}"); WriteLine("/// <summary>"); WriteLine("/// Returns a size where each component is the smallest integral value that"); WriteLine("/// is greater than or equal to the specified component."); WriteLine("/// </summary>"); WriteLine("/// <param name=\"value\">A size.</param>"); WriteLine("/// <returns>The ceiling of value.</returns>"); if (!Type.IsCLSCompliant) { WriteLine("[CLSCompliant(false)]"); } WriteLine("public static {0} Ceiling({1} value)", result, Name); WriteLine("{"); Indent(); WriteLine("return new {0}({1});", result, string.Join(", ", Components.Select(component => string.Format("Functions.Ceiling(value.{0})", component)))); Dedent(); WriteLine("}"); WriteLine("/// <summary>"); WriteLine("/// Returns a size where each component is the largest integral value that"); WriteLine("/// is less than or equal to the specified component."); WriteLine("/// </summary>"); WriteLine("/// <param name=\"value\">A size.</param>"); WriteLine("/// <returns>The floor of value.</returns>"); if (!Type.IsCLSCompliant) { WriteLine("[CLSCompliant(false)]"); } WriteLine("public static {0} Floor({1} value)", result, Name); WriteLine("{"); Indent(); WriteLine("return new {0}({1});", result, string.Join(", ", Components.Select(component => string.Format("Functions.Floor(value.{0})", component)))); Dedent(); WriteLine("}"); WriteLine("/// <summary>"); WriteLine("/// Returns a size where each component is the integral part of the specified component."); WriteLine("/// </summary>"); WriteLine("/// <param name=\"value\">A size.</param>"); WriteLine("/// <returns>The integral of value.</returns>"); if (!Type.IsCLSCompliant) { WriteLine("[CLSCompliant(false)]"); } WriteLine("public static {0} Truncate({1} value)", result, Name); WriteLine("{"); Indent(); WriteLine("return new {0}({1});", result, string.Join(", ", Components.Select(component => string.Format("Functions.Truncate(value.{0})", component)))); Dedent(); WriteLine("}"); WriteLine("/// <summary>"); WriteLine("/// Returns a size where each component is the fractional part of the specified component."); WriteLine("/// </summary>"); WriteLine("/// <param name=\"value\">A size.</param>"); WriteLine("/// <returns>The fractional of value.</returns>"); if (!Type.IsCLSCompliant) { WriteLine("[CLSCompliant(false)]"); } WriteLine("public static {0} Fractional({1} value)", result, Name); WriteLine("{"); Indent(); WriteLine("return new {0}({1});", result, string.Join(", ", Components.Select(component => string.Format("Functions.Fractional(value.{0})", component)))); Dedent(); WriteLine("}"); WriteLine("/// <summary>"); WriteLine("/// Returns a size where each component is rounded to the nearest integral value."); WriteLine("/// </summary>"); WriteLine("/// <param name=\"value\">A size.</param>"); WriteLine("/// <returns>The result of rounding value.</returns>"); if (!Type.IsCLSCompliant) { WriteLine("[CLSCompliant(false)]"); } WriteLine("public static {0} Round({1} value)", result, Name); WriteLine("{"); Indent(); WriteLine("return new {0}({1});", result, string.Join(", ", Components.Select(component => string.Format("Functions.Round(value.{0})", component)))); Dedent(); WriteLine("}"); WriteLine("/// <summary>"); WriteLine("/// Returns a size where each component is rounded to the nearest integral value."); WriteLine("/// </summary>"); WriteLine("/// <param name=\"value\">A size.</param>"); WriteLine("/// <param name=\"digits\">The number of fractional digits in the return value.</param>"); WriteLine("/// <returns>The result of rounding value.</returns>"); if (!Type.IsCLSCompliant) { WriteLine("[CLSCompliant(false)]"); } WriteLine("public static {0} Round({1} value, int digits)", result, Name); WriteLine("{"); Indent(); WriteLine("return new {0}({1});", result, string.Join(", ", Components.Select(component => string.Format("Functions.Round(value.{0}, digits)", component)))); Dedent(); WriteLine("}"); WriteLine("/// <summary>"); WriteLine("/// Returns a size where each component is rounded to the nearest integral value."); WriteLine("/// </summary>"); WriteLine("/// <param name=\"value\">A size.</param>"); WriteLine("/// <param name=\"mode\">Specification for how to round value if it is midway between two other numbers.</param>"); WriteLine("/// <returns>The result of rounding value.</returns>"); if (!Type.IsCLSCompliant) { WriteLine("[CLSCompliant(false)]"); } WriteLine("public static {0} Round({1} value, MidpointRounding mode)", result, Name); WriteLine("{"); Indent(); WriteLine("return new {0}({1});", result, string.Join(", ", Components.Select(component => string.Format("Functions.Round(value.{0}, mode)", component)))); Dedent(); WriteLine("}"); WriteLine("/// <summary>"); WriteLine("/// Returns a size where each component is rounded to the nearest integral value."); WriteLine("/// </summary>"); WriteLine("/// <param name=\"value\">A size.</param>"); WriteLine("/// <param name=\"digits\">The number of fractional digits in the return value.</param>"); WriteLine("/// <param name=\"mode\">Specification for how to round value if it is midway between two other numbers.</param>"); WriteLine("/// <returns>The result of rounding value.</returns>"); if (!Type.IsCLSCompliant) { WriteLine("[CLSCompliant(false)]"); } WriteLine("public static {0} Round({1} value, int digits, MidpointRounding mode)", result, Name); WriteLine("{"); Indent(); WriteLine("return new {0}({1});", result, string.Join(", ", Components.Select(component => string.Format("Functions.Round(value.{0}, digits, mode)", component)))); Dedent(); WriteLine("}"); } WriteLine("#endregion"); #endregion Dedent(); WriteLine("}"); }
void Operations() { var result = new Size(Type.PositiveType, Dimension); WriteLine("#region Operations"); WriteLine("/// <summary>"); WriteLine("/// Returns the product of a size and scalar."); WriteLine("/// </summary>"); WriteLine("/// <param name=\"left\">The size to multiply.</param>"); WriteLine("/// <param name=\"right\">The scalar to multiply.</param>"); WriteLine("/// <returns>The product of the left and right parameters.</returns>"); WriteLine("public static {0} operator *({1} left, {2} right)", result, Name, result.Type); WriteLine("{"); Indent(); WriteLine("Contract.Requires(0 <= right);"); WriteLine("return Size.Multiply(left, right);"); Dedent(); WriteLine("}"); WriteLine("/// <summary>"); WriteLine("/// Returns the product of a scalar and size."); WriteLine("/// </summary>"); WriteLine("/// <param name=\"left\">The scalar to multiply.</param>"); WriteLine("/// <param name=\"right\">The size to multiply.</param>"); WriteLine("/// <returns>The product of the left and right parameters.</returns>"); WriteLine("public static {0} operator *({1} left, {2} right)", result, result.Type, Name); WriteLine("{"); Indent(); WriteLine("Contract.Requires(0 <= left);"); WriteLine("return Size.Multiply(right, left);"); Dedent(); WriteLine("}"); WriteLine("/// <summary>"); WriteLine("/// Divides a size by a scalar and returns the result."); WriteLine("/// </summary>"); WriteLine("/// <param name=\"left\">The size to be divided (the dividend).</param>"); WriteLine("/// <param name=\"right\">The scalar to divide by (the divisor).</param>"); WriteLine("/// <returns>The result of dividing left by right (the quotient).</returns>"); WriteLine("public static {0} operator /({1} left, {2} right)", result, Name, result.Type); WriteLine("{"); Indent(); WriteLine("Contract.Requires(0 <= right);"); WriteLine("return Size.Divide(left, right);"); Dedent(); WriteLine("}"); WriteLine("#endregion"); }