Beispiel #1
0
        static void Main(string[] args)
        {
            using (var file = File.CreateText(@"..\..\..\Granite.Core\StructsGenerated.cs"))
            {
                var w = new CodeWriter(file);

                w.WriteLine("namespace Granite.Core");
                w.WriteLine("{");
                w.PushIndent();
                foreach (var v in Vector.Vectors)
                {
                    v.Build(w);
                    w.WriteLine("");
                }
                foreach (var m in Matrix.Matrices)
                {
                    m.Build(w);
                    w.WriteLine("");
                }
                foreach (var b in Box.Boxes)
                {
                    b.Build(w);
                    w.WriteLine("");
                }
                w.PopIndent();
                w.WriteLine("}");
            }
        }
Beispiel #2
0
        private void BuildToString(CodeWriter w)
        {
            w.WriteLine("public override string ToString()");
            w.WriteLine("{");
            w.PushIndent();
            w.Write("return ");
            for (int c = 0; c < Columns; c++)
            {
                if (c != 0)
                {
                    w.Write(" + ");
                }

                w.Write("\"[\"");
                for (int r = 0; r < Rows; r++)
                {
                    w.Write(" + {0}.ToString()", s_elementName[c, r]);
                }
                w.WriteLine(" + \"]\"");
            }
            w.WriteLine(";");
            w.PopIndent();
            w.WriteLine("}");
            w.WriteLine("");
        }
Beispiel #3
0
        private void BuildStaticValues(CodeWriter w)
        {
            if(Rows == Columns)
            {
                var vector = Vector.GetVector(ElementType, Rows);

                w.Write("public static readonly {0} Identity = new {0}(", Name);

                for (int c = 0; c < Columns; c++)
                {
                    if (c != 0) w.Write(", ");
                    w.Write("{0}.{1}", vector.Name, Vector.s_unitName[c]);
                }

                w.WriteLine(");");
                w.WriteLine("");
            }
        }
Beispiel #4
0
        private void BuildMatrixMultiply(CodeWriter w, Matrix m)
        {
            var resultMatrix = GetMatrix(ElementType, m.Columns, Rows);

            w.WriteLine("public static void Multiply(ref {0} left, ref {1} right, out {2} result)", Name, m.Name, resultMatrix.Name);
            w.WriteLine("{");
            w.PushIndent();

            w.WriteLine("result = new {0}(", resultMatrix.Name);
            w.PushIndent();

            for (int c = 0; c < resultMatrix.Columns; c++)
            {
                for (int r = 0; r < resultMatrix.Rows; r++)
                {
                    for (int i = 0; i < Columns; i++)
                    {
                        if (i != 0) w.Write(" + ");
                        w.Write("left.{0} * right.{1}", s_elementName[i, r], s_elementName[c, i]);
                    }

                    if (c == resultMatrix.Columns - 1 && r == resultMatrix.Rows - 1)
                    {
                        w.WriteLine("");
                    }
                    else
                    {
                        w.WriteLine(",");
                    }
                }
            }

            w.PopIndent();
            w.WriteLine(");");

            w.PopIndent();
            w.WriteLine("}");
            w.WriteLine("");
        }
Beispiel #5
0
        private void BuildBinaryOperator(CodeWriter w, string op)
        {
            var resultType = GetVector(ElementType.ArithmeticType, Dimensions);

            w.WriteLine("public static {0} operator {1}({2} a, {3} b)", resultType.Name, op, Name, resultType.Name);
            w.WriteLine("{");
            w.PushIndent();
            w.Write("return new {0}(", resultType.Name);
            for (int i = 0; i < Dimensions; i++)
            {
                if (i != 0) w.Write(", ");
                w.Write("a.{0} {1} b.{0}", s_fieldName[i], op);
            }
            w.WriteLine(");");
            w.PopIndent();
            w.WriteLine("}");
            w.WriteLine("");

            w.WriteLine("public static {0} operator {1}({2} a, {3} b)", resultType.Name, op, Name, ElementType.ArithmeticType.Name);
            w.WriteLine("{");
            w.PushIndent();
            w.Write("return new {0}(", resultType.Name);
            for (int i = 0; i < Dimensions; i++)
            {
                if (i != 0) w.Write(", ");
                w.Write("a.{0} {1} b", s_fieldName[i], op);
            }
            w.WriteLine(");");
            w.PopIndent();
            w.WriteLine("}");
            w.WriteLine("");
        }
Beispiel #6
0
 private void BuildToString(CodeWriter w)
 {
     w.WriteLine("public override string ToString()");
     w.WriteLine("{");
     w.PushIndent();
     w.Write("return Position.ToString() + Size.ToString();");
     w.PopIndent();
     w.WriteLine("}");
     w.WriteLine("");
 }
Beispiel #7
0
        private void BuildEqualsRelatedMembers(CodeWriter w)
        {
            w.WriteLine("public static bool operator ==({0} a, {0} b)", Name);
            w.WriteLine("{");
            w.PushIndent();
            w.WriteLine("return a.Position == b.Position && a.Size == b.Size;");
            w.PopIndent();
            w.WriteLine("}");
            w.WriteLine("");

            w.WriteLine("public static bool operator !=({0} a, {0} b)", Name);
            w.WriteLine("{");
            w.PushIndent();
            w.WriteLine("return a.Position != b.Position || a.Size != b.Size;");
            w.PopIndent();
            w.WriteLine("}");
            w.WriteLine("");

            w.WriteLine("public bool Equals({0} other)", Name);
            w.WriteLine("{");
            w.PushIndent();
            w.WriteLine("return Position == other.Position && Size == other.Size;");
            w.PopIndent();
            w.WriteLine("}");
            w.WriteLine("");

            w.WriteLine("public override bool Equals(object other)");
            w.WriteLine("{");
            w.PushIndent();
            w.WriteLine("if(!(other is {0})) return false;", Name);
            w.WriteLine("var b = ({0})other;", Name);
            w.WriteLine("return Position == b.Position && Size == b.Size;");
            w.PopIndent();
            w.WriteLine("}");
            w.WriteLine("");

            w.WriteLine("public override int GetHashCode()");
            w.WriteLine("{");
            w.PushIndent();
            w.WriteLine("return Position.GetHashCode() ^ Size.GetHashCode();");
            w.PopIndent();
            w.WriteLine("}");
            w.WriteLine("");
        }
Beispiel #8
0
        public void Build(CodeWriter w)
        {
            w.WriteLine("[System.Serializable]");
            w.WriteLine("[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]");
            w.WriteLine("public partial struct {0} : System.IEquatable<{0}>", Name);
            w.WriteLine("{");
            w.PushIndent();

            // Fields
            w.WriteLine("public readonly {0} Position;", Vector.Name);
            w.WriteLine("public readonly {0} Size;", Vector.Name);

            w.WriteLine("");

            // Constructors
            w.Write("public {0}({1} position, {1} size)", Name, Vector.Name);
            w.WriteLine("{");
            w.PushIndent();
            w.WriteLine("Position = position;");
            w.WriteLine("Size = size;");
            w.PopIndent();
            w.WriteLine("}");

            w.WriteLine("");

            if (Dimensions == 2)
            {
                w.Write("public {0}({1} x, {1} y, {1} sx, {1} sy)", Name, ElementType.Name);
                w.WriteLine("{");
                w.PushIndent();
                w.WriteLine("Position = new {0}(x, y);", Vector.Name);
                w.WriteLine("Size = new {0}(sx, sy);", Vector.Name);
                w.PopIndent();
                w.WriteLine("}");
            }
            else // 3
            {
                w.Write("public {0}({1} x, {1} y, {1} z, {1} sx, {1} sy, {1} sz)", Name, ElementType.Name);
                w.WriteLine("{");
                w.PushIndent();
                w.WriteLine("Position = new {0}(x, y, z);", Vector.Name);
                w.WriteLine("Size = new {0}(sx, sy, sz);", Vector.Name);
                w.PopIndent();
                w.WriteLine("}");
            }

            w.WriteLine("");

            // Casts
            foreach (var b in Boxes)
            {
                if (b != this && b.Dimensions == Dimensions)
                {
                    var isImplicit = ElementType.CanConvertImplicitlyTo(b.ElementType);
                    w.WriteLine("public static {0} operator {1}({2} v)",
                        isImplicit ? "implicit" : "explicit",
                        b.Name, Name
                    );
                    w.WriteLine("{");
                    w.PushIndent();
                    if (isImplicit)
                    {
                        w.WriteLine("return new {0}(v.Position, v.Size);", b.Name);
                    }
                    else
                    {
                        w.WriteLine("return new {0}(({1})v.Position, ({1})v.Size);", b.Name, b.Vector.Name);
                    }
                    w.PopIndent();
                    w.WriteLine("}");
                    w.WriteLine("");
                }
            }

            // ToString
            BuildToString(w);

            // Equals == != GetHashCode
            BuildEqualsRelatedMembers(w);

            // MinX, MaxX, etc
            BuildMinMax(w);

            // Center point
            BuildCenter(w);

            // Translate
            BuildTranslate(w);

            // End struct
            w.PopIndent();
            w.WriteLine("}");
            w.WriteLine("");
        }
Beispiel #9
0
        public void Build(CodeWriter w)
        {
            w.WriteLine("[System.Serializable]");
            w.WriteLine("[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]");
            w.WriteLine("public partial struct {0} : System.IEquatable<{0}>", Name);
            w.WriteLine("{");
            w.PushIndent();

            // Fields and properties
            BuildFieldsAndProperties(w);

            // Constructor
            BuildConstructor(w);

            // Equals == != etc
            BuildEqualsRelatedMembers(w);

            // ToString
            BuildToString(w);

            // Identity
            BuildStaticValues(w);

            // Multiply
            BuildMatrixMultiply(w);
            BuildVectorMultiply(w);

            // End struct
            w.PopIndent();
            w.WriteLine("}");
            w.WriteLine("");
        }
Beispiel #10
0
 private void BuildToString(CodeWriter w)
 {
     w.WriteLine("public override string ToString()");
     w.WriteLine("{");
     w.PushIndent();
     w.Write("return string.Format(\"[");
     for (int i = 0; i < Dimensions; i++)
     {
         if (i != 0) w.Write(", ");
         w.Write("{{{0}}}", i);
     }
     w.Write("]\", ");
     for (int i = 0; i < Dimensions; i++)
     {
         if (i != 0) w.Write(", ");
         w.Write(s_fieldName[i]);
     }
     w.WriteLine(");");
     w.PopIndent();
     w.WriteLine("}");
     w.WriteLine("");
 }
Beispiel #11
0
        private void BuildStaticValues(CodeWriter w)
        {
            w.WriteLine("public static readonly {0} {1} = new {0}({2});",
                Name, "Zero",
                string.Join(", ", Enumerable.Repeat(ElementType.Zero, Dimensions)));

            w.WriteLine("public static readonly {0} {1} = new {0}({2});",
                Name, "One",
                string.Join(", ", Enumerable.Repeat(ElementType.One, Dimensions)));

            for (int i = 0; i < Dimensions; i++)
            {
                w.WriteLine("public static readonly {0} {1} = new {0}({2});",
                    Name, s_unitName[i],
                    string.Join(", ", Enumerable.Range(0, Dimensions).Select(d => d == i ? ElementType.One : ElementType.Zero)));
            }
            w.WriteLine("");
        }
Beispiel #12
0
        private void BuildNegate(CodeWriter w)
        {
            if (ElementType.Signed)
            {
                var resultType = GetVector(ElementType.ArithmeticType, Dimensions);

                w.WriteLine("public static {0} operator -({1} v)", resultType.Name, Name);
                w.WriteLine("{");
                w.PushIndent();
                w.Write("return new {0} (", resultType.Name);
                for (int i = 0; i < Dimensions; i++)
                {
                    if (i != 0) w.Write(", ");
                    w.Write("-v.{0}", s_fieldName[i]);
                }
                w.WriteLine(");");
                w.PopIndent();
                w.WriteLine("}");
                w.WriteLine("");
            }
        }
Beispiel #13
0
        private void BuildLengthRelatedMembers(CodeWriter w)
        {
            var precisionVector = GetVector(ElementType.PrecisionType, Dimensions);
            var mathCast = (ElementType.PrecisionType == ElementType.DoubleType) ? "" : string.Format("({0})", ElementType.PrecisionType.Name);

            w.WriteLine("public {0} SquaredLength", ElementType.ArithmeticType.Name);
            w.WriteLine("{");
            w.PushIndent();
            w.Write("get { return ");
            for (int i = 0; i < Dimensions; i++)
            {
                if (i != 0) w.Write(" + ");
                w.Write("{0} * {0}", s_fieldName[i]);
            }
            w.WriteLine("; }");
            w.PopIndent();
            w.WriteLine("}");
            w.WriteLine("");

            w.WriteLine("public {0} Length", ElementType.PrecisionType.Name);
            w.WriteLine("{");
            w.PushIndent();
            w.Write("get {{ return {0}System.Math.Sqrt(", mathCast);
            for (int i = 0; i < Dimensions; i++)
            {
                if (i != 0) w.Write(" + ");
                w.Write("{0} * {0}", s_fieldName[i]);
            }
            w.WriteLine("); }");
            w.PopIndent();
            w.WriteLine("}");
            w.WriteLine("");

            w.WriteLine("public {0} Normalize()", precisionVector.Name);
            w.WriteLine("{");
            w.PushIndent();
            w.WriteLine("var l = Length;");
            w.Write("return new {0}(", precisionVector.Name);
            for (int i = 0; i < Dimensions; i++)
            {
                if (i != 0) w.Write(", ");
                w.Write("{0} / l", s_fieldName[i]);
            }
            w.WriteLine(");");
            w.PopIndent();
            w.WriteLine("}");
            w.WriteLine("");
        }
Beispiel #14
0
        private void BuildEqualsRelatedMembers(CodeWriter w)
        {
            w.WriteLine("public static bool operator ==({0} a, {0} b)", Name);
            w.WriteLine("{");
            w.PushIndent();
            w.Write("return ");
            for (int i = 0; i < Dimensions; i++)
            {
                if (i != 0) w.Write(" && ");
                w.Write("a.{0} == b.{0}", s_fieldName[i]);
            }
            w.WriteLine(";");
            w.PopIndent();
            w.WriteLine("}");
            w.WriteLine("");

            w.WriteLine("public static bool operator !=({0} a, {0} b)", Name);
            w.WriteLine("{");
            w.PushIndent();
            w.Write("return ");
            for (int i = 0; i < Dimensions; i++)
            {
                if (i != 0) w.Write(" || ");
                w.Write("a.{0} != b.{0}", s_fieldName[i]);
            }
            w.WriteLine(";");
            w.PopIndent();
            w.WriteLine("}");
            w.WriteLine("");

            w.WriteLine("public bool Equals({0} other)", Name);
            w.WriteLine("{");
            w.PushIndent();
            w.Write("return ");
            for (int i = 0; i < Dimensions; i++)
            {
                if (i != 0) w.Write(" && ");
                w.Write("{0} == other.{0}", s_fieldName[i]);
            }
            w.WriteLine(";");
            w.PopIndent();
            w.WriteLine("}");
            w.WriteLine("");

            w.WriteLine("public override bool Equals(object other)");
            w.WriteLine("{");
            w.PushIndent();
            w.WriteLine("if(!(other is {0})) return false;", Name);
            w.WriteLine("var v = ({0})other;", Name);
            w.Write("return ");
            for (int i = 0; i < Dimensions; i++)
            {
                if (i != 0) w.Write(" && ");
                w.Write("{0} == v.{0}", s_fieldName[i]);
            }
            w.WriteLine(";");
            w.PopIndent();
            w.WriteLine("}");
            w.WriteLine("");

            w.WriteLine("public override int GetHashCode()");
            w.WriteLine("{");
            w.PushIndent();
            w.Write("return ");
            for (int i = 0; i < Dimensions; i++)
            {
                if (i != 0) w.Write(" ^ ");
                w.Write("{0}.GetHashCode()", s_fieldName[i]);
            }
            w.WriteLine(";");
            w.PopIndent();
            w.WriteLine("}");
            w.WriteLine("");
        }
Beispiel #15
0
        private void BuildDotAndCrossProducts(CodeWriter w)
        {
            w.WriteLine("public static {0} Dot({1} a, {1} b)", ElementType.ArithmeticType.Name, Name);
            w.WriteLine("{");
            w.PushIndent();
            w.Write("return ");
            for (int i = 0; i < Dimensions; i++)
            {
                if (i != 0) w.Write(" + ");
                w.Write("a.{0} * b.{0}", s_fieldName[i]);
            }
            w.WriteLine(";");
            w.PopIndent();
            w.WriteLine("}");
            w.WriteLine("");

            if (Dimensions == 3)
            {
                var vector = Vector.GetVector(ElementType.ArithmeticType, Dimensions);

                w.WriteLine("public static {0} Cross({1} a, {1} b)", vector.Name, Name);
                w.WriteLine("{");
                w.PushIndent();
                w.WriteLine("return new {0}(", vector.Name);
                w.PushIndent();
                w.WriteLine("a.Y * b.Z - a.Z * b.Y,");
                w.WriteLine("a.Z * b.X - a.X * b.Z,");
                w.WriteLine("a.X * b.Y - a.Y * b.X");
                w.PopIndent();
                w.WriteLine(");");
                w.PopIndent();
                w.WriteLine("}");
                w.WriteLine("");
            }
        }
Beispiel #16
0
        private void BuildVectorMultiply(CodeWriter w)
        {
            var inVector = Vector.GetVector(ElementType, Columns);
            var outVector = Vector.GetVector(ElementType, Rows);

            w.WriteLine("public static {0} Multiply(ref {1} m, {2} v)", outVector.Name, Name, inVector.Name);
            w.WriteLine("{");
            w.PushIndent();

            w.WriteLine("return new {0}(", outVector.Name);
            w.PushIndent();

            for (int r = 0; r < Rows; r++)
            {
                if (r != 0) w.WriteLine(",");
                for (int c = 0; c < Columns; c++)
                {
                    if (c != 0) w.Write(" + ");
                    w.Write("m.{0} * v.{1}", s_elementName[c, r], Vector.s_fieldName[c]);
                }
            }

            w.WriteLine("");

            w.PopIndent();
            w.WriteLine(");");

            w.PopIndent();
            w.WriteLine("}");
            w.WriteLine("");
        }
Beispiel #17
0
        private void BuildConstructor(CodeWriter w)
        {
            var vector = Vector.GetVector(ElementType, Rows);

            w.Write("public {0}(", Name);
            for (int c = 0; c < Columns; c++)
            {
                if (c != 0) w.Write(", ");
                w.Write("{0} {1}", vector.Name, s_columnParameterName[c]);
            }
            w.WriteLine(")");
            w.WriteLine("{");
            w.PushIndent();

            for (int c = 0; c < Columns; c++)
            {
                for (int r = 0; r < Rows; r++)
                {
                    w.WriteLine("{0} = {1}.{2};", s_elementName[c, r], s_columnParameterName[c], Vector.s_fieldName[r]);
                }
            }

            w.PopIndent();
            w.WriteLine("}");
            w.WriteLine("");

            w.Write("public {0}(", Name);
            for (int c = 0; c < Columns; c++)
            {
                for (int r = 0; r < Rows; r++)
                {
                    if (r != 0 || c != 0) w.Write(", ");
                    w.Write("{0} {1}", ElementType.Name, s_elementParameterName[c, r]);
                }
            }
            w.WriteLine(")");
            w.WriteLine("{");
            w.PushIndent();

            for (int c = 0; c < Columns; c++)
            {
                for (int r = 0; r < Rows; r++)
                {
                    w.WriteLine("{0} = {1};", s_elementName[c, r], s_elementParameterName[c, r]);
                }
            }

            w.PopIndent();
            w.WriteLine("}");
            w.WriteLine("");
        }
Beispiel #18
0
        private void BuildEqualsRelatedMembers(CodeWriter w)
        {
            w.WriteLine("public static bool operator ==({0} a, {0} b)", Name);
            w.WriteLine("{");
            w.PushIndent();
            w.Write("return ");
            for (int c = 0; c < Columns; c++)
            {
                for (int r = 0; r < Rows; r++)
                {
                    if (c != 0 || r != 0) w.Write(" && ");
                    w.Write("a.{0} == b.{0}", s_elementName[c, r]);
                }
            }
            w.WriteLine(";");
            w.PopIndent();
            w.WriteLine("}");
            w.WriteLine("");

            w.WriteLine("public static bool operator !=({0} a, {0} b)", Name);
            w.WriteLine("{");
            w.PushIndent();
            w.Write("return ");
            for (int c = 0; c < Columns; c++)
            {
                for (int r = 0; r < Rows; r++)
                {
                    if (c != 0 || r != 0) w.Write(" || ");
                    w.Write("a.{0} != b.{0}", s_elementName[c, r]);
                }
            }
            w.WriteLine(";");
            w.PopIndent();
            w.WriteLine("}");
            w.WriteLine("");

            w.WriteLine("public bool Equals({0} other)", Name);
            w.WriteLine("{");
            w.PushIndent();
            w.Write("return ");
            for (int c = 0; c < Columns; c++)
            {
                for (int r = 0; r < Rows; r++)
                {
                    if (c != 0 || r != 0) w.Write(" && ");
                    w.Write("{0} == other.{0}", s_elementName[c, r]);
                }
            }
            w.WriteLine(";");
            w.PopIndent();
            w.WriteLine("}");
            w.WriteLine("");

            w.WriteLine("public override bool Equals(object other)");
            w.WriteLine("{");
            w.PushIndent();
            w.WriteLine("if(!(other is {0})) return false;", Name);
            w.WriteLine("var m = ({0})other;", Name);
            w.Write("return ");
            for (int c = 0; c < Columns; c++)
            {
                for (int r = 0; r < Rows; r++)
                {
                    if (c != 0 || r != 0) w.Write(" && ");
                    w.Write("{0} == m.{0}", s_elementName[c, r]);
                }
            }
            w.WriteLine(";");
            w.PopIndent();
            w.WriteLine("}");
            w.WriteLine("");

            w.WriteLine("public override int GetHashCode()");
            w.WriteLine("{");
            w.PushIndent();
            w.Write("return ");
            for (int c = 0; c < Columns; c++)
            {
                for (int r = 0; r < Rows; r++)
                {
                    if (c != 0 || r != 0) w.Write(" ^ ");
                    w.Write("{0}.GetHashCode()", s_elementName[c, r]);
                }
            }
            w.WriteLine(";");
            w.PopIndent();
            w.WriteLine("}");
            w.WriteLine("");
        }
Beispiel #19
0
        private void BuildCenter(CodeWriter w)
        {
            var vector = Vector.GetVector(ElementType.PrecisionType, Dimensions);

            w.WriteLine("public {0} Center {{", vector.Name);
            w.PushIndent();
            w.WriteLine("get {");
            w.PushIndent();
            w.WriteLine("return new {0}(", vector.Name);
            for (int i = 0; i < Dimensions; i++)
            {
                if (i != 0) w.WriteLine(",");
                w.Write("Position.{0} + Size.{0} / {1}", Vector.s_fieldName[i], ElementType.PrecisionType.Two);
            }
            w.WriteLine("");
            w.WriteLine(");");
            w.PopIndent();
            w.WriteLine("}");
            w.PopIndent();
            w.WriteLine("}");
            w.WriteLine("");
        }
Beispiel #20
0
        private void BuildFieldsAndProperties(CodeWriter w)
        {
            var columnVector = Vector.GetVector(ElementType, Rows);
            var rowVector = Vector.GetVector(ElementType, Columns);

            for (int c = 0; c < Columns; c++)
            {
                for (int r = 0; r < Rows; r++)
                {
                    w.WriteLine("public readonly {0} {1};", ElementType.Name, s_elementName[c, r]);
                }
            }

            w.WriteLine("");

            for (int c = 0; c < Columns; c++)
            {
                w.Write("public {0} {1} {{ get {{ return new {0}(", columnVector.Name, s_columnPropertyName[c]);
                for (int r = 0; r < Rows; r++)
                {
                    if (r != 0) w.Write(", ");
                    w.Write(s_elementName[c, r]);
                }
                w.WriteLine("); } }");
            }

            w.WriteLine("");

            for (int r = 0; r < Rows; r++)
            {
                w.Write("public {0} {1} {{ get {{ return new {0}(", rowVector.Name, s_rowPropertyName[r]);
                for (int c = 0; c < Columns; c++)
                {
                    if (c != 0) w.Write(", ");
                    w.Write(s_elementName[c, r]);
                }
                w.WriteLine("); } }");
            }

            w.WriteLine("");
        }
Beispiel #21
0
        private void BuildMinMax(CodeWriter w)
        {
            w.WriteLine("public {0} MinX {{ get {{ return System.Math.Min(Position.X, ({0})(Position.X + Size.X)); }} }}", ElementType.Name);
            w.WriteLine("public {0} MaxX {{ get {{ return System.Math.Max(Position.X, ({0})(Position.X + Size.X)); }} }}", ElementType.Name);

            w.WriteLine("public {0} MinY {{ get {{ return System.Math.Min(Position.Y, ({0})(Position.Y + Size.Y)); }} }}", ElementType.Name);
            w.WriteLine("public {0} MaxY {{ get {{ return System.Math.Max(Position.Y, ({0})(Position.Y + Size.Y)); }} }}", ElementType.Name);

            if (Dimensions == 3)
            {
                w.WriteLine("public {0} MinZ {{ get {{ return System.Math.Min(Position.Z, ({0})(Position.Z + Size.Z)); }} }}", ElementType.Name);
                w.WriteLine("public {0} MaxZ {{ get {{ return System.Math.Max(Position.Z, ({0})(Position.Z + Size.Z)); }} }}", ElementType.Name);
            }

            w.WriteLine("");
        }
Beispiel #22
0
 private void BuildMatrixMultiply(CodeWriter w)
 {
     foreach (var m in Matrices)
     {
         if (m.ElementType == ElementType && Columns == m.Rows)
         {
             BuildMatrixMultiply(w, m);
         }
     }
 }
Beispiel #23
0
        private void BuildTranslate(CodeWriter w)
        {
            var vector = Vector.GetVector(ElementType.ArithmeticType, Dimensions);
            var box = Box.GetBox(ElementType.ArithmeticType, Dimensions);

            w.WriteLine("public {0} Translate({1} v)", box.Name, vector.Name);
            w.WriteLine("{");
            w.PushIndent();
            w.WriteLine("return new {0}(Position + v, Size);", box.Name);
            w.PopIndent();
            w.WriteLine("}");
            w.WriteLine("");
        }
Beispiel #24
0
        public void Build(CodeWriter w)
        {
            w.WriteLine("[System.Serializable]");
            w.WriteLine("[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]");
            w.WriteLine("public partial struct {0} : System.IEquatable<{0}>", Name);
            w.WriteLine("{");
            w.PushIndent();

            // Fields
            for(int i = 0; i < Dimensions; i++)
            {
                w.WriteLine("public readonly {0} {1};", ElementType.Name, s_fieldName[i]);
            }

            w.WriteLine("");

            // Constructors
            w.Write("public {0}(", Name);
            for (int i = 0; i < Dimensions; i++)
            {
                if (i != 0) w.Write(", ");
                w.Write("{0} {1}", ElementType.Name, s_parameterName[i]);
            }
            w.WriteLine(")");
            w.WriteLine("{");
            w.PushIndent();
            for (int i = 0; i < Dimensions; i++)
            {
                w.WriteLine("{0} = {1};", s_fieldName[i], s_parameterName[i]);
            }
            w.PopIndent();
            w.WriteLine("}");

            w.WriteLine("");

            for (int d = 2; d < Dimensions; d++)
            {
                var vector = GetVector(ElementType, d);

                w.Write("public {0}({1} v", Name, vector.Name);
                for (int i = d; i < Dimensions; i++)
                {
                    w.Write(", {0} {1}", ElementType.Name, s_parameterName[i]);
                }
                w.WriteLine(")");
                w.WriteLine("{");
                w.PushIndent();
                for (int i = 0; i < d; i++)
                {
                    w.WriteLine("{0} = v.{1};", s_fieldName[i], s_fieldName[i]);
                }
                for (int i = d; i < Dimensions; i++)
                {
                    w.WriteLine("{0} = {1};", s_fieldName[i], s_parameterName[i]);
                }
                w.PopIndent();
                w.WriteLine("}");
                w.WriteLine("");
            }

            // Casts
            foreach (var c in Vectors)
            {
                if (c != this && c.Dimensions == Dimensions)
                {
                    var isImplicit = ElementType.CanConvertImplicitlyTo(c.ElementType);
                    w.WriteLine("public static {0} operator {1}({2} v)",
                        isImplicit ? "implicit" : "explicit",
                        c.Name, Name
                    );
                    w.WriteLine("{");
                    w.PushIndent();
                    w.Write("return new {0}(", c.Name);
                    for (int i = 0; i < Dimensions; i++)
                    {
                        if (i != 0) w.Write(", ");
                        if (isImplicit)
                        {
                            w.Write("v.{0}", s_fieldName[i]);
                        }
                        else
                        {
                            w.Write("({0})v.{1}", c.ElementType.Name, s_fieldName[i]);
                        }
                    }
                    w.WriteLine(");");
                    w.PopIndent();
                    w.WriteLine("}");
                    w.WriteLine("");
                }
            }

            // ToString
            BuildToString(w);

            // Equals == != GetHashCode
            BuildEqualsRelatedMembers(w);

            // Arithmetic
            BuildBinaryOperator(w, "+");
            BuildBinaryOperator(w, "-");
            BuildBinaryOperator(w, "*");
            BuildBinaryOperator(w, "/");

            // -
            BuildNegate(w);

            // Dot Cross
            BuildDotAndCrossProducts(w);

            // SquaredLength, Length, Normalize
            BuildLengthRelatedMembers(w);

            // Zero, One, UnitX...W
            BuildStaticValues(w);

            // End struct
            w.PopIndent();
            w.WriteLine("}");
            w.WriteLine("");
        }