Ejemplo n.º 1
0
        public override void Export(AssetList asset, string path)
        {
            MaterialList materials = (asset as MaterialList);
            MAT          mat       = new MAT();

            foreach (Material material in materials.Entries)
            {
                if (material == null)
                {
                    continue;
                }
                if (material.Texture == null)
                {
                    continue;
                }

                mat.Materials.Add(
                    new MATMaterial(
                        material.Name,
                        material.Texture.Name
                        )
                    );
            }

            mat.Save(path);
        }
Ejemplo n.º 2
0
    public static VEC LU_Solve(MAT A1, VEC b1)
    {
        MAT A = new MAT(A1);
        VEC b = new VEC(b1);
        int n = A.dim();

        A = luFact(A);
        MAT L = new MAT(n);
        MAT U = new MAT(n);

        for (int i = 1; i < n; i++)
        {
            for (int j = 0; j < i; j++)
            {
                L[i][j] = A[i][j];
            }
        }
        for (int i = 0; i < n; i++)
        {
            L[i][i] = 1;
        }

        for (int i = 0; i < n; i++)
        {
            for (int j = i; j < n; j++)
            {
                U[i][j] = A[i][j];
            }
        }
        VEC y = fwdSubs(L, b);
        VEC x = bckSubs(U, y);

        return(x);
    }
Ejemplo n.º 3
0
        public override AssetList ImportMany(string path)
        {
            MaterialList materials = new MaterialList();
            MAT          mat       = MAT.Load(path);

            foreach (MATMaterial material in mat.Materials)
            {
                if (material.Texture == Path.GetFileNameWithoutExtension(material.Texture))
                {
                    //materials.Entries.Add(
                    //    new Material
                    //    {
                    //        Name = material.Name,
                    //        Texture = SceneManager.Current.Content.Load<Texture, TIFImporter>(material.Texture, Path.GetDirectoryName(path))
                    //    }
                    //);

                    //materials.Entries[materials.Entries.Count - 1].SupportingDocuments["Source"] = material;
                }
                else
                {
                    materials.Entries.Add(
                        new Material
                    {
                        Name    = material.Name,
                        Texture = SceneManager.Current.Content.Load <Texture, PIXImporter>(material.Texture, Path.GetDirectoryName(path), true)
                    }
                        );
                }
            }

            return(materials);
        }
Ejemplo n.º 4
0
 public MAT(MAT m1)
 {
     n  = m1.dim();
     va = new VEC[n];
     for (int i = 0; i < n; i++)
     {
         va[i] = new VEC(m1[i]);
     }
 }
Ejemplo n.º 5
0
 public VECM3(int n)
 {
     dim = n;
     val = new MAT[dim];
     for (int i = 0; i < dim; i++)
     {
         val[i] = new MAT(3);
     }
 }
Ejemplo n.º 6
0
 public VECM3(int n, MAT[] a)
 {
     dim = n;
     val = new MAT[dim];
     for (int i = 0; i < dim; i++)
     {
         val[i] = new MAT(a[i]);
     }
 }
Ejemplo n.º 7
0
 public VECM3(VECM3 v1)
 {
     dim = v1.len();
     val = new MAT[dim];
     for (int i = 0; i < dim; i++)
     {
         val[i] = new MAT(v1[i]);
     }
 }
Ejemplo n.º 8
0
    public static MAT unit(int n)
    {
        MAT A = new MAT(n);

        for (int i = 0; i < n; i++)
        {
            A[i][i] = 1;
        }
        return(A);
    }
Ejemplo n.º 9
0
    public static MAT operator /(MAT m1, double a)
    {
        int n = m1.dim();
        MAT m = new MAT(n);

        for (int i = 0; i < n; i++)
        {
            m[i] = m1[i] / a;
        }
        return(m);
    }
Ejemplo n.º 10
0
    public static MAT operator *(double a, MAT m1)
    {
        int n = m1.dim();
        MAT m = new MAT(n);

        for (int i = 0; i < n; i++)
        {
            m[i] = m1[i] * a;
        }
        return(m);
    }
Ejemplo n.º 11
0
    public static MAT operator +(MAT m1, MAT m2)
    {
        int n = m1.dim();
        MAT m = new MAT(n);

        for (int i = 0; i < n; i++)
        {
            m[i] = m1[i] + m2[i];
        }
        return(m);
    }
Ejemplo n.º 12
0
    public static MAT operator *(VECM3 v1, VECM3 v2)
    {
        MAT s = new MAT(3);
        int n = v1.len();

        for (int i = 0; i < n; i++)
        {
            s += v1[i] * v2[i];
        }
        return(s);
    }
Ejemplo n.º 13
0
    public MATM3 tpose()
    {
        int   dim = this.dim();
        MATM3 A   = new MATM3(dim);

        for (int i = 0; i < dim; i++)
        {
            for (int j = 0; j < dim; j++)
            {
                A[i][j] = new MAT(this[j][i]);
            }
        }
        return(A);
    }
Ejemplo n.º 14
0
    private void LoadTextures(IEnumerable <string> matFilenames)
    {
        var textures = new Dictionary <string, Texture2D[]>();

        foreach (var matFilename in matFilenames)
        {
            if (textures.ContainsKey(matFilename.ToLower()))
            {
                Debug.LogWarning("Texture " + matFilename + " defined twice.");
                continue;
            }
            var mat     = new MAT();
            var matPath = @"3do\mat\" + matFilename;
            if (!_gobManager.Exists(matPath))
            {
                matPath = @"mat\" + matFilename;
            }

            mat.ParseMat(_cmp, _gobManager.GetStream(matPath));
            textures.Add(matFilename.ToLower(), new[] { mat.Textures[0] });
        }

        var rects = Atlas.PackTextures(textures.Values.SelectMany(x => x).ToArray(), 0);

        var rectsOffset = 0;

        foreach (var matFilename in textures.Keys)
        {
            var numTextures = textures[matFilename].Length;
            var subrects    = new Rect[numTextures];

            Array.Copy(rects, rectsOffset, subrects, 0, subrects.Length);

            var sizes = new List <Vector2>();
            foreach (var texture2D in textures[matFilename])
            {
                sizes.Add(new Vector2(texture2D.width, texture2D.height));
            }

            _materialLookup[matFilename] = new TexInfo
            {
                Rects = subrects,
                Sizes = sizes.ToArray()
            };

            rectsOffset += numTextures;
        }
    }
Ejemplo n.º 15
0
    public static VEC fwdSubs(MAT m1, VEC b)
    {
        int len = b.len();
        VEC v   = new VEC(len);

        for (int i = 0; i < len; i++)
        {
            double s = 0;
            for (int j = 0; j < i; j++)
            {
                s += m1[i][j] * v[j];
            }
            v[i] = (b[i] - s) / m1[i][i];
        }
        return(v);
    }
Ejemplo n.º 16
0
        public override void Export(AssetList asset, string path)
        {
            var materials = (asset as MaterialList);
            var mat       = new MAT();

            foreach (Material material in materials.Entries)
            {
                mat.Materials.Add(
                    new MATMaterial(
                        material.Name,
                        material.Texture.Name
                        )
                    );
            }

            mat.Save(path);
        }
Ejemplo n.º 17
0
        public static Matrix Mul(this ILinAlg ila, params Matrix[] mats)
        {
            ILinAlgMat[] MATS = new ILinAlgMat[mats.Length];
            for (int i = 0; i < mats.Length; i++)
            {
                MATS[i] = ila.ToILMat(mats[i]);
            }
            ILinAlgMat MUL = ila.Mul(MATS);
            Matrix     mul = ToMatrix(MUL);

            MUL.Dispose();
            foreach (var MAT in MATS)
            {
                MAT.Dispose();
            }
            return(mul);
        }
Ejemplo n.º 18
0
    public static MAT InverseM3(MAT A)
    {
        MAT I = unit(3);
        int n = A.dim();

        A = numericalFunctions.luFact(A);
        MAT L = new MAT(n);
        MAT U = new MAT(n);

        for (int i = 1; i < n; i++)
        {
            for (int j = 0; j < i; j++)
            {
                L[i][j] = A[i][j];
            }
        }
        for (int i = 0; i < n; i++)
        {
            L[i][i] = 1;
        }
        for (int i = 0; i < n; i++)
        {
            for (int j = i; j < n; j++)
            {
                U[i][j] = A[i][j];
            }
        }

        MAT yT = new MAT(3);

        for (int i = 0; i < 3; i++)
        {
            yT[i] = numericalFunctions.LU_Solve(L, I[i]);
        }
        MAT y  = yT.tpose();
        MAT xT = new MAT(3);

        for (int i = 0; i < 3; i++)
        {
            xT[i] = numericalFunctions.LU_Solve(U, yT[i]);
        }
        return(xT.tpose());
    }
Ejemplo n.º 19
0
    public static VEC bckSubs(MAT m1, VEC b)
    {
        int len = b.len();
        VEC v   = new VEC(len);

        for (int i = 0; i < len; i++)
        {
            v[i] = b[i];
        }
        for (int i = len - 1; i >= 0; i--)
        {
            v[i] = v[i] / m1[i][i];
            for (int j = i - 1; j >= 0; j--)
            {
                v[j] = v[j] - (m1[j][i] * v[i]);
            }
        }
        return(v);
    }
Ejemplo n.º 20
0
    public static MAT operator *(MAT m1, MAT m2)
    {
        int    n = m1.dim();
        MAT    m = new MAT(n);
        double s;

        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                s = 0;
                for (int k = 0; k < n; k++)
                {
                    s += m1[i][k] * m2[k][j];
                }
                m[i][j] = s;
            }
        }
        return(m);
    }
Ejemplo n.º 21
0
    public static MATM3 operator *(MATM3 m1, MATM3 m2)
    {
        int   n = m1.dim();
        MATM3 m = new MATM3(n);
        MAT   s;

        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                s = new MAT(3);
                for (int k = 0; k < n; k++)
                {
                    s += m1[i][k] * m2[k][j];
                }
                m[i][j] = s;
            }
        }
        return(m);
    }
Ejemplo n.º 22
0
    public static MAT luFact(MAT m1)
    {
        int n = m1.dim();

        for (int i = 0; i < n; i++)
        {
            for (int j = i + 1; j < n; j++)
            {
                m1[j][i] /= m1[i][i];
            }
            for (int j = i + 1; j < n; j++)
            {
                for (int k = i + 1; k < n; k++)
                {
                    m1[j][k] -= m1[j][i] * m1[i][k];
                }
            }
        }
        return(m1);
    }
Ejemplo n.º 23
0
    /*
     * Return vector of coefficients of Gear's Method with certain order number
     */
    private VEC GetAlphaOfGearsMethod(int order)
    {
        MAT A = new MAT(order + 1);
        VEC b = new VEC(order + 1);

        for (int i = 0; i < order + 1; i++)
        {
            b[i]        = 1;
            A[i][order] = i;
        }

        for (int k = 0; k <= order; k++)
        {
            for (int j = 0; j < order; j++)
            {
                A[k][j] = Math.Pow(-j, k);
            }
        }
        VEC v = numericalFunctions.LU_Solve(A, b);

        return(v);
    }
Ejemplo n.º 24
0
    public static VECV3 LU_Solve(MATM3 A1, VECV3 b1)
    {
        MATM3 A = new MATM3(A1);

        VECV3 b = new VECV3(b1);
        int   n = A.dim();

        A = luFact(A);

        MATM3 L = new MATM3(n);
        MATM3 U = new MATM3(n);

        for (int i = 1; i < n; i++)
        {
            for (int j = 0; j < i; j++)
            {
                L[i][j] = A[i][j];
            }
        }
        for (int i = 0; i < n; i++)
        {
            L[i][i] = MAT.unit(3);
        }

        for (int i = 0; i < n; i++)
        {
            for (int j = i; j < n; j++)
            {
                U[i][j] = A[i][j];
            }
        }
        VECV3 y = fwdSubs(L, b);
        VECV3 x = bckSubs(U, y);

        return(x);
    }
Ejemplo n.º 25
0
    /*
     * void UpdateTrapezoidalMethod(double h)
     *
     * Trapezoidal method on velocity
     * Trapezoidal method on position
     *
     * Is the most accurate method
     */
    public void UpdateTrapezoidalMethod(double h)
    {
        if (validCount <= 0)
        {
            return;
        }

        // Map valid star index into validIndex
        int[] validIndex = new int[validCount];
        int   indexTmp   = 0;

        for (int i = 0; i < maxStarNum; i++)
        {
            if (validMap[i] == true)
            {
                validIndex[indexTmp] = i;
                indexTmp++;
            }
        }

        // copy velocity and position of all stars into arrays
        VEC[] velAll     = new VEC[validCount];
        VEC[] new_velAll = new VEC[validCount];

        VEC[] posAll     = new VEC[validCount];
        VEC[] new_posAll = new VEC[validCount];

        VEC massAll = new VEC(validCount);

        for (int i = 0; i < validCount; i++)
        {
            velAll[i]     = new VEC(s[validIndex[i]].vel);
            new_velAll[i] = new VEC(s[validIndex[i]].vel);

            posAll[i]     = new VEC(s[validIndex[i]].pos);
            new_posAll[i] = new VEC(s[validIndex[i]].pos);

            massAll[i] = s[validIndex[i]].mass;
        }

        double e_threshold = 1E-9; // the error threshold of performing Newton’s Method
        double err         = 1.0 + e_threshold;
        int    maxIterNum  = 100;  // Newton’s Method might not converge, we need a max iteration number to terminate.

        while (err > e_threshold & maxIterNum > 0)
        {
            VECV3 F = new VECV3(2 * validCount);

            for (int i = 0; i < validCount; i++)
            {
                VEC a = new VEC(3);
                for (int j = 0; j < validCount; j++)
                {
                    if (i != j)
                    {
                        a += 0.5 * VEC.Normalize(new_posAll[j] - new_posAll[i]) * massAll[j] / DistancePow2(new_posAll[i], new_posAll[j])
                             + 0.5 * VEC.Normalize(posAll[j] - posAll[i]) * massAll[j] / DistancePow2(posAll[i], posAll[j]);
                    }
                }
                F[i] = G * a - (new_velAll[i] - velAll[i]) / h;
            }

            for (int i = validCount; i < 2 * validCount; i++)
            {
                F[i] = (new_posAll[i - validCount] - posAll[i - validCount]) / h - 0.5 * (new_velAll[i - validCount] + velAll[i - validCount]);
            }

            MATM3 JF = new MATM3(2 * validCount);
            MAT   tmp;
            for (int i = 0; i < validCount; i++)
            {
                tmp = new MAT(3);
                for (int k = 0; k < 3; k++)
                {
                    tmp[k][k] = -1.0 / h;
                }
                JF[i][i] = tmp;
            }
            for (int i = 0; i < validCount; i++)
            {
                for (int j_inJF = validCount; j_inJF < 2 * validCount; j_inJF++)
                {
                    int j = j_inJF - validCount;
                    tmp = new MAT(3);
                    if (i != j)
                    {
                        for (int a = 0; a < 3; a++)
                        {
                            for (int b = 0; b < 3; b++)
                            {
                                if (a == b)
                                {
                                    tmp[a][b] = massAll[j] * G *
                                                (-3 * Math.Pow(DistancePow2(new_posAll[j], new_posAll[i]), -2.5) * (new_posAll[j][a] - new_posAll[i][a]) * new_posAll[j][a]
                                                 + Math.Pow(DistancePow2(new_posAll[j], new_posAll[i]), -1.5));
                                }
                                else
                                {
                                    tmp[a][b] = massAll[j] * G * -3 * Math.Pow(DistancePow2(new_posAll[j], new_posAll[i]), -2.5) * (new_posAll[j][a] - new_posAll[i][a]) * (new_posAll[j][b] - new_posAll[i][b]);
                                }
                            }
                        }
                    }
                    else
                    {
                        for (int a = 0; a < 3; a++)
                        {
                            for (int b = 0; b < 3; b++)
                            {
                                if (a == b)
                                {
                                    double sInTmp = 0;
                                    for (int k = 0; k < validCount; k++)
                                    {
                                        if (k != j)
                                        {
                                            sInTmp += massAll[k] *
                                                      (3 * Math.Pow(DistancePow2(new_posAll[k], new_posAll[j]), -2.5) * (new_posAll[k][a] - new_posAll[j][a]) * (new_posAll[k][a] - new_posAll[i][a])
                                                       - Math.Pow(DistancePow2(new_posAll[k], new_posAll[j]), -1.5));
                                        }
                                    }
                                    tmp[a][a] = G * sInTmp;
                                }
                                else
                                {
                                    double sInTmp = 0;
                                    for (int k = 0; k < validCount; k++)
                                    {
                                        if (k != j)
                                        {
                                            sInTmp += massAll[k] *
                                                      (3 * Math.Pow(DistancePow2(new_posAll[k], new_posAll[j]), -2.5) * (new_posAll[k][a] - new_posAll[j][a]) * (new_posAll[k][b] * new_posAll[i][b]));
                                        }
                                    }
                                    tmp[a][a] = G * sInTmp;
                                }
                            }
                        }
                    }
                    JF[i][j_inJF] = 0.5 * tmp;
                }
            }

            for (int i = validCount; i < 2 * validCount; i++)
            {
                for (int j = 0; j < validCount; j++)
                {
                    if (i - validCount == j)
                    {
                        tmp = new MAT(3);
                        for (int k = 0; k < 3; k++)
                        {
                            tmp[k][k] = -0.5;
                        }
                        JF[i][j] = tmp;
                    }
                }
            }

            for (int i = validCount; i < 2 * validCount; i++)
            {
                for (int j = validCount; j < 2 * validCount; j++)
                {
                    if (i == j)
                    {
                        tmp = new MAT(3);
                        for (int k = 0; k < 3; k++)
                        {
                            tmp[k][k] = 1.0 / h;
                        }
                        JF[i][j] = tmp;
                    }
                }
            }

            VECV3 delta = numericalFunctions.LU_Solve(JF, -F);

            for (int i = 0; i < validCount; i++)
            {
                new_velAll[i] += delta[i];
                new_posAll[i] += delta[i + validCount];
            }
            err = VECV3.L2_norm_special(F);

            maxIterNum--;
            if (maxIterNum == 0)
            {
                Debug.Log("Max iter number reached. Err: " + err + "  Using Forward method instead in this period.");
                for (int i = 0; i < numOfForwardMethodInstead; i++)
                {
                    UpdateForwardMethod(h / numOfForwardMethodInstead);
                }
                return;
            }
        }

        // Apply new position and velocity to Star objects
        for (int i = 0; i < validCount; i++)
        {
            if (maxIterNum != 0)
            {
                s[validIndex[i]].vel = new_velAll[i];
                s[validIndex[i]].pos = new_posAll[i];
            }
        }

        // Check if any pair of stars have distance smaller than minDistanceToDistroy.
        // If so, merge two stars.
        for (int i = 0; i < validCount; i++)
        {
            for (int j = i + 1; j < validCount; j++)
            {
                if (DistanceBetweenStarsPow2(s[validIndex[i]], s[validIndex[j]]) < minDistanceToDistroy)
                {
                    s[validIndex[j]].pos  += (s[validIndex[i]].pos - s[validIndex[j]].pos) * s[validIndex[i]].mass / (s[validIndex[j]].mass + s[validIndex[i]].mass);
                    s[validIndex[j]].vel   = (s[validIndex[j]].vel * s[validIndex[j]].mass + s[validIndex[i]].vel * s[validIndex[i]].mass) / (s[validIndex[j]].mass + s[validIndex[i]].mass);
                    s[validIndex[j]].mass += s[validIndex[i]].mass;
                    DestroyStar(s[validIndex[i]], 0);
                    continue;
                }
            }
        }
    }
Ejemplo n.º 26
0
    /*
     * void UpdateBackwardMethod(double h)
     *
     * Backward method on velocity
     * Trapezoidal method on position
     */
    public void UpdateBackwardMethod(double h)
    {
        if (validCount <= 0)
        {
            return;
        }
        int[] validIndex = new int[validCount];
        int   indexTmp   = 0;

        for (int i = 0; i < maxStarNum; i++)
        {
            if (validMap[i] == true)
            {
                validIndex[indexTmp] = i;
                indexTmp++;
            }
        }

        VEC[] velAll     = new VEC[validCount];
        VEC[] new_velAll = new VEC[validCount];

        VEC[] posAll     = new VEC[validCount];
        VEC[] new_posAll = new VEC[validCount];

        VEC massAll = new VEC(validCount);

        for (int i = 0; i < validCount; i++)
        {
            velAll[i]     = new VEC(s[validIndex[i]].vel);
            new_velAll[i] = new VEC(s[validIndex[i]].vel);

            posAll[i]     = new VEC(s[validIndex[i]].pos);
            new_posAll[i] = new VEC(s[validIndex[i]].pos);

            massAll[i] = s[validIndex[i]].mass;
        }

        double e_threshold = 1E-7;
        double err         = 1.0 + e_threshold;
        int    maxIterNum  = 100;

        while (err > e_threshold & maxIterNum > 0)
        {
            VECV3 F = new VECV3(2 * validCount);
            for (int i = 0; i < validCount; i++)
            {
                VEC a = new VEC(3);
                for (int j = 0; j < validCount; j++)
                {
                    if (i != j)
                    {
                        a += VEC.Normalize(new_posAll[j] - new_posAll[i]) * massAll[j] / DistancePow2(new_posAll[i], new_posAll[j]);
                    }
                }
                F[i] = G * a - (new_velAll[i] - velAll[i]) / h;
            }
            for (int i = validCount; i < 2 * validCount; i++)
            {
                F[i] = (new_posAll[i - validCount] - posAll[i - validCount]) / h - 0.5 * (new_velAll[i - validCount] + velAll[i - validCount]);
            }

            MATM3 JF = new MATM3(2 * validCount);
            MAT   tmp;
            for (int i = 0; i < validCount; i++)
            {
                tmp = new MAT(3);
                for (int k = 0; k < 3; k++)
                {
                    tmp[k][k] = -1.0 / h;
                }
                JF[i][i] = tmp;
            }
            for (int i = 0; i < validCount; i++)
            {
                for (int j_inJF = validCount; j_inJF < 2 * validCount; j_inJF++)
                {
                    int j = j_inJF - validCount;
                    tmp = new MAT(3);
                    if (i != j)
                    {
                        for (int a = 0; a < 3; a++)
                        {
                            for (int b = 0; b < 3; b++)
                            {
                                if (a == b)
                                {
                                    tmp[a][b] = massAll[j] * G *
                                                (-3 * Math.Pow(DistancePow2(new_posAll[j], new_posAll[i]), -2.5) * (new_posAll[j][a] - new_posAll[i][a]) * new_posAll[j][a]
                                                 + Math.Pow(DistancePow2(new_posAll[j], new_posAll[i]), -1.5));
                                }
                                else
                                {
                                    tmp[a][b] = massAll[j] * G * -3 * Math.Pow(DistancePow2(new_posAll[j], new_posAll[i]), -2.5) * (new_posAll[j][a] - new_posAll[i][a]) * (new_posAll[j][b] - new_posAll[i][b]);
                                }
                            }
                        }
                    }
                    else
                    {
                        for (int a = 0; a < 3; a++)
                        {
                            for (int b = 0; b < 3; b++)
                            {
                                if (a == b)
                                {
                                    double sInTmp = 0;
                                    for (int k = 0; k < validCount; k++)
                                    {
                                        if (k != j)
                                        {
                                            sInTmp += massAll[k] *
                                                      (3 * Math.Pow(DistancePow2(new_posAll[k], new_posAll[j]), -2.5) * (new_posAll[k][a] - new_posAll[j][a]) * (new_posAll[k][a] - new_posAll[i][a])
                                                       - Math.Pow(DistancePow2(new_posAll[k], new_posAll[j]), -1.5));
                                        }
                                    }
                                    tmp[a][a] = G * sInTmp;
                                }
                                else
                                {
                                    double sInTmp = 0;
                                    for (int k = 0; k < validCount; k++)
                                    {
                                        if (k != j)
                                        {
                                            sInTmp += massAll[k] *
                                                      (3 * Math.Pow(DistancePow2(new_posAll[k], new_posAll[j]), -2.5) * (new_posAll[k][a] - new_posAll[j][a]) * (new_posAll[k][b] * new_posAll[i][b]));
                                        }
                                    }
                                    tmp[a][a] = G * sInTmp;
                                }
                            }
                        }
                    }
                    JF[i][j_inJF] = tmp;
                }
            }

            for (int i = validCount; i < 2 * validCount; i++)
            {
                for (int j = 0; j < validCount; j++)
                {
                    if (i - validCount == j)
                    {
                        tmp = new MAT(3);
                        for (int k = 0; k < 3; k++)
                        {
                            tmp[k][k] = -0.5;
                        }
                        JF[i][j] = tmp;
                    }
                }
            }

            for (int i = validCount; i < 2 * validCount; i++)
            {
                for (int j = validCount; j < 2 * validCount; j++)
                {
                    if (i == j)
                    {
                        tmp = new MAT(3);
                        for (int k = 0; k < 3; k++)
                        {
                            tmp[k][k] = 1.0 / h;
                        }
                        JF[i][j] = tmp;
                    }
                }
            }
            VECV3 delta = numericalFunctions.LU_Solve(JF, -F);
            for (int i = 0; i < validCount; i++)
            {
                new_velAll[i] += delta[i];
                new_posAll[i] += delta[i + validCount];
            }
            err = VECV3.L2_norm_special(F);

            maxIterNum--;
            if (maxIterNum == 0)
            {
                Debug.Log("Max iter number reached. Err: " + err + "  Using Forward method instead in this period.");
                UpdateForwardMethod(h / 2.0);
                UpdateForwardMethod(h / 2.0);
                return;
            }
        }
        for (int i = 0; i < validCount; i++)
        {
            if (maxIterNum != 0)
            {
                s[validIndex[i]].vel = new_velAll[i];
                s[validIndex[i]].pos = new_posAll[i];
            }
        }

        for (int i = 0; i < validCount; i++)
        {
            for (int j = i + 1; j < validCount; j++)
            {
                if (DistanceBetweenStarsPow2(s[validIndex[i]], s[validIndex[j]]) < minDistanceToDistroy)
                {
                    s[validIndex[j]].pos  += (s[validIndex[i]].pos - s[validIndex[j]].pos) * s[validIndex[i]].mass / (s[validIndex[j]].mass + s[validIndex[i]].mass);
                    s[validIndex[j]].vel   = (s[validIndex[j]].vel * s[validIndex[j]].mass + s[validIndex[i]].vel * s[validIndex[i]].mass) / (s[validIndex[j]].mass + s[validIndex[i]].mass);
                    s[validIndex[j]].mass += s[validIndex[i]].mass;
                    DestroyStar(s[validIndex[i]], 0);
                    continue;
                }
            }
        }
    }
Ejemplo n.º 27
0
    public static MAT operator /(MAT m1, MAT m2)
    {
        MAT m2Inverse = InverseM3(m2);

        return(m1 * m2Inverse);
    }
Ejemplo n.º 28
0
    public static VEC operator /(VEC v1, MAT m1)
    {
        MAT m2Inverse = InverseM3(m1);

        return(v1 * m2Inverse);
    }
Ejemplo n.º 29
0
        public static void ProcessTEX(string PathIn, string PathOut, out CpsxTex[] psxTex)
        {
            string pathIn = PathIn.Substring(0, PathIn.LastIndexOf("\\") + 1);
            string fileIn = PathIn.Replace(pathIn, "");

            bool bNonCars = fileIn.Contains(".MOT");

            if (!Directory.Exists(PathOut))
            {
                Directory.CreateDirectory(PathOut);
            }
            if (!Directory.Exists(PathOut + "\\TIFFRGB"))
            {
                Directory.CreateDirectory(PathOut + "\\TIFFRGB");
            }

            MAT m = new MAT();

            using (BinaryReader br = new BinaryReader(new FileStream(pathIn + fileIn, FileMode.Open)))
            {
                int     iTexCount = br.ReadInt32();
                Color[] colours   = new Color[16];

                psxTex = new CpsxTex[iTexCount];

                int k = 0;

                while (br.BaseStream.Position < br.BaseStream.Length)
                {
                    if (br.ReadInt32() != 0)
                    {
                        Console.WriteLine("int not 0");
                    }

                    int width  = br.ReadInt16();
                    int height = br.ReadInt16();

                    // Palette
                    for (int i = 0; i < 16; i++)
                    {
                        colours[i] = pixelToRGB(br.ReadUInt16());
                    }

                    Bitmap bmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format16bppRgb565);

                    // Pixels
                    int x = 0;
                    int y = height - 1;
                    int j = 0;

                    psxTex[k].Width  = width;
                    psxTex[k].Height = height;

                    for (int i = 0; i < ((width * height) / 2); i++)
                    {
                        int b = br.ReadByte();

                        int idxA = (b & 0xF0) >> 4;
                        int idxB = (b & 0xF);

                        bmp.SetPixel(x, y, colours[idxB]);
                        bmp.SetPixel(x + 1, y, colours[idxA]);

                        j += 2;

                        if (j % width == 0)
                        {
                            x = 0; y--;
                        }
                        else
                        {
                            x += 2;
                        }
                    }

                    psxTex[k].Name = "psx" + fileIn.Replace(".TEX", "").Replace(".MOT", "").Replace(".MAT", "") + (bNonCars ? "nc" : "") + k.ToString().PadLeft(2, "0"[0]);
                    m.Materials.Add(new MATMaterial(psxTex[k].Name, psxTex[k].Name));
                    bmp.Save(PathOut + "\\TIFFRGB\\" + psxTex[k].Name + ".tif", System.Drawing.Imaging.ImageFormat.Tiff);
                    k++;
                }
            }

            m.Save(PathOut + (bNonCars ? "nc" : "") + fileIn.Replace(".TEX", ".mat").Replace(".MOT", ".mat"));
        }