Exemplo n.º 1
0
 public double CalculateGaussianProbability(double x, double expectation, double dispersion)
 {
     return((1 / (Math.Sqrt(2 * Math.PI * dispersion))) * Math.Pow(Math.E, -((Math.Pow(x - expectation, 2)) / (2 * dispersion))));
 }
Exemplo n.º 2
0
        private static Color Interpolate(IEnumerable <ValueLocation <Color> > locations, UV p)
        {
            var num       = new double[4];
            var totalArea = 0.0;

            //var sw = new Stopwatch();
            //sw.Start();

            var locationArr = locations.ToArray();

            // Find uvs in the locations set that are within a certain domain
            // around p. Start with something small like +-0.1. If you don't
            // find enough sample points, keep increasing the size of the search
            // domain until you do.

            ValueLocation <Color>[] searchLocations = null;
            var          s    = 0.025;
            const double step = 0.025;

            while (s <= 1.0)
            {
                searchLocations = locationArr.Where(
                    l => l.Location.U <p.U + s &&
                                       l.Location.U> p.U - s &&
                    l.Location.V <p.V + s &&
                                  l.Location.V> p.V - s).ToArray();

                if (searchLocations.Count() >= 5)
                {
                    break;
                }
                s += step;
            }

            foreach (var loc in searchLocations)
            {
                var t = loc.Location;
                var d = DSCore.Math.Pow(t.U - p.U, 2) + DSCore.Math.Pow(t.V - p.V, 2);
                if (d == 0.0)
                {
                    return(loc.Value);
                }

                // 1/d^2 creates a suitable fall-off.
                // The higher the exponent, the more more pixelated the result.

                var w = 1 / Math.Pow(d, 2);

                num[0]    += loc.Value.Alpha * w;
                num[1]    += loc.Value.Red * w;
                num[2]    += loc.Value.Green * w;
                num[3]    += loc.Value.Blue * w;
                totalArea += w;
            }

            //sw.Stop();
            //Debug.WriteLine("{0} elapsed for interpolation at {1}.", sw.Elapsed, p);

            return(Color.ByARGB((int)(num[0] / totalArea),
                                (int)(num[1] / totalArea),
                                (int)(num[2] / totalArea),
                                (int)(num[3] / totalArea)));
        }
Exemplo n.º 3
0
        private static double StandardDeviation(IEnumerable <double> values)
        {
            double avg = values.Average();

            return(MathObj.Sqrt(values.Average(v => MathObj.Pow(v - avg, 2))));
        }
Exemplo n.º 4
0
        public static float[,] CalculateGaussianKernel(int W, double sigma)
        {
            float[,] kernel = new float[W, W];
            double mean = W / 2.0;
            float  sum  = 0.0f;

            for (int x = 0; x < W; ++x)
            {
                for (int y = 0; y < W; ++y)
                {
                    kernel[x, y] = (float)(Math.Exp(-0.5 * (Math.Pow((x - mean) / sigma, 2.0) + Math.Pow((y - mean) / sigma, 2.0)))
                                           / (2 * Math.PI * sigma * sigma));
                    sum += kernel[x, y];
                }
            }

            for (int x = 0; x < W; ++x)
            {
                for (int y = 0; y < W; ++y)
                {
                    kernel[x, y] *= (1.0f) / sum;
                }
            }

            return(kernel);
        }
Exemplo n.º 5
0
 public static float Pow(float val, float pow)
 {
     return((float)SMath.Pow(val, pow));
 }
Exemplo n.º 6
0
 public static double ExpoOut(double b, double c, double t, double d = 1)
 {
     return((t == d) ? b + c : c *(-Math.Pow(2, -10 * t / d) + 1) + b);
 }
Exemplo n.º 7
0
        public static SvdResult ComputeSvd(double[,] matrix)
        {
            double[,] U, V;
            double[] s;

            // Derived from LINPACK code.
            // Initialize.
            double[,] A = matrix.Copy();
            int m  = matrix.RowCount();
            int n  = matrix.ColumnCount();
            int nu = M.Min(m, n);

            s = new double[M.Min(m + 1, n)];
            U = new double[m, nu];
            V = new double[n, n];

            var  e     = new double[n];
            var  work  = new double[m];
            bool wantu = true;
            bool wantv = true;

            // Reduce A to bidiagonal form, storing the diagonal elements
            // in s and the super-diagonal elements in e.

            int nct = M.Min(m - 1, n);
            int nrt = M.Max(0, M.Min(n - 2, m));

            for (int k = 0; k < M.Max(nct, nrt); k++)
            {
                if (k < nct)
                {
                    // Compute the transformation for the k-th column and
                    // place the k-th diagonal in s[k].
                    // Compute 2-norm of k-th column without under/overflow.
                    s[k] = 0;
                    for (int i = k; i < m; i++)
                    {
                        s[k] = Functions.Hypotenuse(s[k], A[i, k]);
                    }
                    if (s[k] != 0.0)
                    {
                        if (A[k, k] < 0.0)
                        {
                            s[k] = -s[k];
                        }
                        for (int i = k; i < m; i++)
                        {
                            A[i, k] /= s[k];
                        }
                        A[k, k] += 1.0;
                    }
                    s[k] = -s[k];
                }
                for (int j = k + 1; j < n; j++)
                {
                    if ((k < nct) & (s[k] != 0.0))
                    {
                        // Apply the transformation.

                        double t = 0;
                        for (int i = k; i < m; i++)
                        {
                            t += A[i, k] * A[i, j];
                        }
                        t = (-t) / A[k, k];
                        for (int i = k; i < m; i++)
                        {
                            A[i, j] += t * A[i, k];
                        }
                    }

                    // Place the k-th row of A into e for the
                    // subsequent calculation of the row transformation.

                    e[j] = A[k, j];
                }
                if (wantu & (k < nct))
                {
                    // Place the transformation in U for subsequent back
                    // multiplication.

                    for (int i = k; i < m; i++)
                    {
                        U[i, k] = A[i, k];
                    }
                }
                if (k < nrt)
                {
                    // Compute the k-th row transformation and place the
                    // k-th super-diagonal in e[k].
                    // Compute 2-norm without under/overflow.
                    e[k] = 0;
                    for (int i = k + 1; i < n; i++)
                    {
                        e[k] = Functions.Hypotenuse(e[k], e[i]);
                    }
                    if (e[k] != 0.0)
                    {
                        if (e[k + 1] < 0.0)
                        {
                            e[k] = -e[k];
                        }
                        for (int i = k + 1; i < n; i++)
                        {
                            e[i] /= e[k];
                        }
                        e[k + 1] += 1.0;
                    }
                    e[k] = -e[k];
                    if ((k + 1 < m) & (e[k] != 0.0))
                    {
                        // Apply the transformation.

                        for (int i = k + 1; i < m; i++)
                        {
                            work[i] = 0.0;
                        }
                        for (int j = k + 1; j < n; j++)
                        {
                            for (int i = k + 1; i < m; i++)
                            {
                                work[i] += e[j] * A[i, j];
                            }
                        }
                        for (int j = k + 1; j < n; j++)
                        {
                            double t = (-e[j]) / e[k + 1];
                            for (int i = k + 1; i < m; i++)
                            {
                                A[i, j] += t * work[i];
                            }
                        }
                    }
                    if (wantv)
                    {
                        // Place the transformation in V for subsequent
                        // back multiplication.

                        for (int i = k + 1; i < n; i++)
                        {
                            V[i, k] = e[i];
                        }
                    }
                }
            }

            // Set up the final bidiagonal matrix or order p.

            int p = M.Min(n, m + 1);

            if (nct < n)
            {
                s[nct] = A[nct, nct];
            }
            if (m < p)
            {
                s[p - 1] = 0.0;
            }
            if (nrt + 1 < p)
            {
                e[nrt] = A[nrt, p - 1];
            }
            e[p - 1] = 0.0;

            // If required, generate U.

            if (wantu)
            {
                for (int j = nct; j < nu; j++)
                {
                    for (int i = 0; i < m; i++)
                    {
                        U[i, j] = 0.0;
                    }
                    U[j, j] = 1.0;
                }
                for (int k = nct - 1; k >= 0; k--)
                {
                    if (s[k] != 0.0)
                    {
                        for (int j = k + 1; j < nu; j++)
                        {
                            double t = 0;
                            for (int i = k; i < m; i++)
                            {
                                t += U[i, k] * U[i, j];
                            }
                            t = (-t) / U[k, k];
                            for (int i = k; i < m; i++)
                            {
                                U[i, j] += t * U[i, k];
                            }
                        }
                        for (int i = k; i < m; i++)
                        {
                            U[i, k] = -U[i, k];
                        }
                        U[k, k] = 1.0 + U[k, k];
                        for (int i = 0; i < k - 1; i++)
                        {
                            U[i, k] = 0.0;
                        }
                    }
                    else
                    {
                        for (int i = 0; i < m; i++)
                        {
                            U[i, k] = 0.0;
                        }
                        U[k, k] = 1.0;
                    }
                }
            }

            // If required, generate V.

            if (wantv)
            {
                for (int k = n - 1; k >= 0; k--)
                {
                    if ((k < nrt) & (e[k] != 0.0))
                    {
                        for (int j = k + 1; j < nu; j++)
                        {
                            double t = 0;
                            for (int i = k + 1; i < n; i++)
                            {
                                t += V[i, k] * V[i, j];
                            }
                            t = (-t) / V[k + 1, k];
                            for (int i = k + 1; i < n; i++)
                            {
                                V[i, j] += t * V[i, k];
                            }
                        }
                    }
                    for (int i = 0; i < n; i++)
                    {
                        V[i, k] = 0.0;
                    }
                    V[k, k] = 1.0;
                }
            }

            // Main iteration loop for the singular values.

            int    pp   = p - 1;
            int    iter = 0;
            double eps  = M.Pow(2.0, -52.0);

            while (p > 0)
            {
                int k, kase;

                // Here is where a test for too many iterations would go.

                // This section of the program inspects for
                // negligible elements in the s and e arrays.  On
                // completion the variables kase and k are set as follows.

                // kase = 1     if s(p) and e[k-1] are negligible and k<p
                // kase = 2     if s(k) is negligible and k<p
                // kase = 3     if e[k-1] is negligible, k<p, and
                //              s(k), ..., s(p) are not negligible (qr step).
                // kase = 4     if e(p-1) is negligible (convergence).

                for (k = p - 2; k >= -1; k--)
                {
                    if (k == -1)
                    {
                        break;
                    }
                    if (M.Abs(e[k]) <= eps * (M.Abs(s[k]) + M.Abs(s[k + 1])))
                    {
                        e[k] = 0.0;
                        break;
                    }
                }
                if (k == p - 2)
                {
                    kase = 4;
                }
                else
                {
                    int ks;
                    for (ks = p - 1; ks >= k; ks--)
                    {
                        if (ks == k)
                        {
                            break;
                        }
                        double t = (ks != p ? M.Abs(e[ks]) : 0.0) + (ks != k + 1 ? M.Abs(e[ks - 1]) : 0.0);
                        if (M.Abs(s[ks]) <= eps * t)
                        {
                            s[ks] = 0.0;
                            break;
                        }
                    }
                    if (ks == k)
                    {
                        kase = 3;
                    }
                    else if (ks == p - 1)
                    {
                        kase = 1;
                    }
                    else
                    {
                        kase = 2;
                        k    = ks;
                    }
                }
                k++;

                // Perform the task indicated by kase.
                switch (kase)
                {
                // Deflate negligible s(p).
                case 1:
                {
                    double f = e[p - 2];
                    e[p - 2] = 0.0;
                    for (int j = p - 2; j >= k; j--)
                    {
                        double t  = Functions.Hypotenuse(s[j], f);
                        double cs = s[j] / t;
                        double sn = f / t;
                        s[j] = t;
                        if (j != k)
                        {
                            f        = (-sn) * e[j - 1];
                            e[j - 1] = cs * e[j - 1];
                        }
                        if (wantv)
                        {
                            for (int i = 0; i < n; i++)
                            {
                                t           = cs * V[i, j] + sn * V[i, p - 1];
                                V[i, p - 1] = (-sn) * V[i, j] + cs * V[i, p - 1];
                                V[i, j]     = t;
                            }
                        }
                    }
                }
                break;

                // Split at negligible s(k).
                case 2:
                {
                    double f = e[k - 1];
                    e[k - 1] = 0.0;
                    for (int j = k; j < p; j++)
                    {
                        double t  = Functions.Hypotenuse(s[j], f);
                        double cs = s[j] / t;
                        double sn = f / t;
                        s[j] = t;
                        f    = (-sn) * e[j];
                        e[j] = cs * e[j];
                        if (wantu)
                        {
                            for (int i = 0; i < m; i++)
                            {
                                t           = cs * U[i, j] + sn * U[i, k - 1];
                                U[i, k - 1] = (-sn) * U[i, j] + cs * U[i, k - 1];
                                U[i, j]     = t;
                            }
                        }
                    }
                }
                break;

                // Perform one qr step.
                case 3:
                {
                    // Calculate the shift.

                    double scale =
                        M.Max(M.Max(M.Max(M.Max(M.Abs(s[p - 1]), M.Abs(s[p - 2])), M.Abs(e[p - 2])), M.Abs(s[k])),
                              M.Abs(e[k]));
                    double sp    = s[p - 1] / scale;
                    double spm1  = s[p - 2] / scale;
                    double epm1  = e[p - 2] / scale;
                    double sk    = s[k] / scale;
                    double ek    = e[k] / scale;
                    double b     = ((spm1 + sp) * (spm1 - sp) + epm1 * epm1) / 2.0;
                    double c     = (sp * epm1) * (sp * epm1);
                    double shift = 0.0;
                    if ((b != 0.0) | (c != 0.0))
                    {
                        shift = M.Sqrt(b * b + c);
                        if (b < 0.0)
                        {
                            shift = -shift;
                        }
                        shift = c / (b + shift);
                    }
                    double f = (sk + sp) * (sk - sp) + shift;
                    double g = sk * ek;

                    // Chase zeros.

                    for (int j = k; j < p - 1; j++)
                    {
                        double t  = Functions.Hypotenuse(f, g);
                        double cs = f / t;
                        double sn = g / t;
                        if (j != k)
                        {
                            e[j - 1] = t;
                        }
                        f        = cs * s[j] + sn * e[j];
                        e[j]     = cs * e[j] - sn * s[j];
                        g        = sn * s[j + 1];
                        s[j + 1] = cs * s[j + 1];
                        if (wantv)
                        {
                            for (int i = 0; i < n; i++)
                            {
                                t           = cs * V[i, j] + sn * V[i, j + 1];
                                V[i, j + 1] = (-sn) * V[i, j] + cs * V[i, j + 1];
                                V[i, j]     = t;
                            }
                        }
                        t        = Functions.Hypotenuse(f, g);
                        cs       = f / t;
                        sn       = g / t;
                        s[j]     = t;
                        f        = cs * e[j] + sn * s[j + 1];
                        s[j + 1] = (-sn) * e[j] + cs * s[j + 1];
                        g        = sn * e[j + 1];
                        e[j + 1] = cs * e[j + 1];
                        if (wantu && (j < m - 1))
                        {
                            for (int i = 0; i < m; i++)
                            {
                                t           = cs * U[i, j] + sn * U[i, j + 1];
                                U[i, j + 1] = (-sn) * U[i, j] + cs * U[i, j + 1];
                                U[i, j]     = t;
                            }
                        }
                    }
                    e[p - 2] = f;
                    iter     = iter + 1;
                }
                break;

                // Convergence.
                case 4:
                {
                    // Make the singular values positive.

                    if (s[k] <= 0.0)
                    {
                        s[k] = (s[k] < 0.0 ? -s[k] : 0.0);
                        if (wantv)
                        {
                            for (int i = 0; i <= pp; i++)
                            {
                                V[i, k] = -V[i, k];
                            }
                        }
                    }

                    // Order the singular values.

                    while (k < pp)
                    {
                        if (s[k] >= s[k + 1])
                        {
                            break;
                        }
                        double t = s[k];
                        s[k]     = s[k + 1];
                        s[k + 1] = t;
                        if (wantv && (k < n - 1))
                        {
                            for (int i = 0; i < n; i++)
                            {
                                t           = V[i, k + 1];
                                V[i, k + 1] = V[i, k];
                                V[i, k]     = t;
                            }
                        }
                        if (wantu && (k < m - 1))
                        {
                            for (int i = 0; i < m; i++)
                            {
                                t           = U[i, k + 1];
                                U[i, k + 1] = U[i, k];
                                U[i, k]     = t;
                            }
                        }
                        k++;
                    }
                    iter = 0;
                    p--;
                }
                break;
                }
            }

            //Complete -- Return SVD Result
            return(new SvdResult(U, s, V));
        }
Exemplo n.º 8
0
        protected override void LoadContent()
        {
            //Create sprite batch
            spriteBatch = new SpriteBatch(GraphicsDevice);

            //Create chunks
            Chunks = new Chunk[WorldMetrics.ChunkCountX, WorldMetrics.ChunkCountY];

            //Load ground and damage textures
            GroundTexture = Content.Load <Texture2D>("Environment/Blocks/Terrain/Terrain_SpriteSheet");
            BreakTextures = new Texture2D[5];

            for (int i = 1; i <= 5; i++)
            {
                BreakTextures[i - 1] = Content.Load <Texture2D>("Environment/Blocks/Damage/" + i.ToString());
            }

            //Load other textures
            Player            = Content.Load <Texture2D>("UI/Cursor");
            defaultFont       = Content.Load <SpriteFont>("Fonts/Arial");
            Cursor            = Content.Load <Texture2D>("UI/Cursor");
            BackgroundTexture = Content.Load <Texture2D>("Environment/Blocks/Background/Background");
            player            = new Player();


            //Block data
            Data = new BlockData[]
            {
                new BlockData("Stone", 61, 61, 61, 0, 900, 10),
                new BlockData("Copper", 197, 105, 70, 900, 910, 4),
                new BlockData("Iron", 151, 176, 212, 910, 920, 4),
                new BlockData("Aluminum", 173, 178, 183, 920, 930, 3),
                new BlockData("Coal", 33, 33, 33, 930, 940, 5),
                new BlockData("Lithium", 158, 183, 188, 940, 945, 2),
                new BlockData("Sulfur", 224, 250, 103, 945, 950, 1),
                new BlockData("Titanium", 242, 242, 242, 950, 955, 2),
                new BlockData("Chrome", 204, 202, 228, 955, 958, 1),
                new BlockData("Nickel", 229, 214, 132, 958, 968, 3),
                new BlockData("Silver", 215, 205, 243, 968, 971, 1),
                new BlockData("Tin", 144, 153, 184, 971, 981, 4),
                new BlockData("Tungsten", 44, 45, 51, 981, 986, 2),
                new BlockData("Lead", 51, 29, 60, 986, 996, 3),
                new BlockData("Iridium", 244, 243, 245, 996, 997, 0),
                new BlockData("Uranium", 146, 217, 136, 997, 998, 1),
                new BlockData("Gold", 239, 169, 46, 998, 1000, 1)
            };

            //Generate world :
            //Create random instance
            IOModule.Init();

            //string worldData = IOModule.ReadFull();
            string worldData = "";

            if (worldData == "")
            {
                NoiseGenerator.generateNoise(6, 45, 0.16, 0.45, seed);
                Random r = new Random(seed.GetHashCode());
                for (int ChunkX = 0; ChunkX < WorldMetrics.ChunkCountX; ChunkX++)
                {
                    for (int ChunkY = 0; ChunkY < WorldMetrics.ChunkCountY; ChunkY++)
                    {
                        //Loop through all chunks
                        Chunks[ChunkX, ChunkY] = new Chunk(WorldMetrics.ChunkSizeX, WorldMetrics.ChunkSizeY, ChunkX, ChunkY, this);
                        for (int x = 0; x < WorldMetrics.ChunkSizeX; x++)
                        {
                            for (int y = 0; y < WorldMetrics.ChunkSizeY; y++)
                            {
                                //Loop through all blocks and randomize thems

                                //Generate noise
                                float noise = (float)NoiseGenerator.Noise(ChunkX * WorldMetrics.ChunkSizeX + x, ChunkY * WorldMetrics.ChunkSizeY + y);

                                Chunks[ChunkX, ChunkY].Blocks[x, y].ID       = (noise >= 0.4f) ? 1 : 0;
                                Chunks[ChunkX, ChunkY].Blocks[x, y].Hardness = 6;
                                if (Chunks[ChunkX, ChunkY].GetBlock(x, y) == 0)
                                {
                                    CameraPosition  = new Vector2(ChunkX * WorldMetrics.ChunkSizeX + x, ChunkY * WorldMetrics.ChunkSizeY + y);
                                    CameraPosition -= new Vector2(graphics.PreferredBackBufferWidth / 2, graphics.PreferredBackBufferHeight / 2);
                                }
                                else
                                {
                                    int i = 1;
                                    foreach (BlockData bd in Data)
                                    {
                                        float rL = bd.RarityL / 1000f;
                                        float rR = bd.RarityR / 1000f;
                                        if (rL <= noise && rR > noise)
                                        {
                                            Chunks[ChunkX, ChunkY].Blocks[x, y].ID = i;
                                            if (i != 1)
                                            {
                                                PopulateOre(ChunkX * WorldMetrics.ChunkSizeX + x, ChunkY * WorldMetrics.ChunkSizeY + y, i, r);
                                            }
                                            break;
                                        }
                                        i++;
                                    }
                                }
                            }
                        }
                    }
                }

                //Loop through all chunks and smooth them using Cellular Automata
                for (int x = 0; x < WorldMetrics.ChunkCountX; x++)
                {
                    for (int y = 0; y < WorldMetrics.ChunkCountY; y++)
                    {
                        for (int i = 0; i < 2; i++)
                        {
                            //Chunks[x, y].Smooth(r);
                        }
                    }
                }
            }
            else
            {
                worldData = worldData.Replace("\r", "");

                string[] DataSplit = worldData.Split('\n');

                int index = 0;
                for (int ChunkX = 0; ChunkX < WorldMetrics.ChunkCountX; ChunkX++)
                {
                    for (int ChunkY = 0; ChunkY < WorldMetrics.ChunkCountY; ChunkY++)
                    {
                        string a = DataSplit[index];
                        index++;
                        //Loop through all chunks
                        Chunks[ChunkX, ChunkY] = new Chunk(WorldMetrics.ChunkSizeX, WorldMetrics.ChunkSizeY, ChunkX, ChunkY, this);
                        for (int x = 0; x < WorldMetrics.ChunkSizeX; x++)
                        {
                            string   Row     = DataSplit[index];
                            string[] RowCols = Row.Split(' ');
                            for (int y = 0; y < WorldMetrics.ChunkSizeY; y++)
                            {
                                Chunks[ChunkX, ChunkY].Blocks[x, y].ID       = System.Convert.ToInt32(RowCols[y]);
                                Chunks[ChunkX, ChunkY].Blocks[x, y].Hardness = 6;
                            }
                            index++;
                        }
                    }
                }
                string CamPos = DataSplit[index];
                float  xc     = (float)System.Convert.ToDouble(CamPos.Split(',')[0]);
                float  yc     = (float)System.Convert.ToDouble(CamPos.Split(',')[1]);
                CameraPosition = new Vector2(xc, yc);
            }


            //Recalculate block states
            for (int x = 0; x < WorldMetrics.ChunkCountX; x++)
            {
                for (int y = 0; y < WorldMetrics.ChunkCountY; y++)
                {
                    Chunks[x, y].Recalculate();
                }
            }
            ScreenWidthSquared = (int)Math.Pow(graphics.PreferredBackBufferWidth / 2f, 2) * 2f;
        }
Exemplo n.º 9
0
 public static float Length(this Vector2f value)
 {
     return((float)Math.Sqrt(Math.Pow(value.X, 2) + Math.Pow(value.Y, 2)));
 }
Exemplo n.º 10
0
 // For example, a delay of 10 msec would be 1200log2(.01) = -7973.
 public static double Timecent2Sec(short timecent)
 {
     return(Math.Pow(2, (double)timecent / 1200.0));
 }
Exemplo n.º 11
0
 // For example, a frequency of 10 mHz would be 1200log2(.01/8.176) = -11610.
 public static double AbsCent2Freq(short absCent)
 {
     return(8.176 * Math.Pow(2, (double)absCent / 1200.0));
 }
Exemplo n.º 12
0
 public static double Db2Gain(double db)
 {
     // different with common dB (db / 10.0)
     return(Math.Pow(10.0, (db / 20.0)));
 }
Exemplo n.º 13
0
 public static double Cent2Pitch(double cent)
 {
     return(Math.Pow(2, cent / 1200.0));
 }
        public void Refresh()
        {
            AudioSourceVisualizer audioSourceVisualizer;

            try {
                audioSourceVisualizer = target as AudioSourceVisualizer;
            }
            catch (System.Exception e) {
                Debug.Log("Not ready");
                return;
            }
            AudioSource audioSource = audioSourceVisualizer.source;
            AudioClip   clip        = audioSource.clip;

            if (clip == null)
            {
                return; //Not ready
            }

            //First get the RMS of the audio channel as a whole

            /*float[] intensityBins = new float[AudioSourceVisualizerEditor.samples];
             * audioSource.GetOutputData(intensityBins, 0);
             * float sum = 0;
             * foreach(float sample in intensityBins) {
             *  sum += sample * sample; // sum squared samples
             * }
             * float rms = Mathf.Sqrt(sum / AudioSourceVisualizerEditor.samples); // rms = square root of average*/


            float[] spectrumBins = new float[samples];
            audioSource.GetSpectrumData(spectrumBins, 0, FFTWindow.Hanning);

            System.Func <double, float> frequencyIntensity = (double freq) => {
                if (freq > clip.frequency || freq < 0)
                {
                    return(0); //No data for this frequency
                }

                //spectrumBins is in the range 0 - clip.frequency
                int binIdx = (int)Math.Truncate(freq / clip.frequency * samples);
                return(spectrumBins[binIdx]);
            };

            int idx = 0;

            foreach (VisualElement bin in bins)
            {
                double binRange = (double)idx / bins.Length;
                double sampleFreq;
                if (audioSourceVisualizer.useLogScale)
                {
                    double fMin10 = Math.Log10(frequencyMin);
                    double fMax10 = Math.Log10(clip.frequency);
                    //Map from linear space with bins.Length to log space with samples as length
                    sampleFreq = Math.Pow(10.0, binRange * (fMax10 - fMin10) + fMin10);
                }
                else
                {
                    //Linear from frequencyMin to frequencyMax
                    sampleFreq = binRange * (clip.frequency -
                                             frequencyMin) + frequencyMin;
                }

                float intensity = frequencyIntensity(sampleFreq);
                float db        = 20 * Mathf.Log10(intensity / 1.0e-6f); // calculate dB from linear getSpectrumData
                float linear    = Mathf.Clamp(db / 80.0f, 0.0f, 1.0f);

                bin.style.height          = linear * (AudioSourceVisualizerEditor.height - 2) + 2;
                bin.style.backgroundColor = new Color(linear, 68.0f / 255.0f, 136.0f / 255.0f, 1.0f);

                idx++;
            }

            //Update xAxis
            xAxis.useLogScale  = audioSourceVisualizer.useLogScale;
            xAxis.frequencyMin = frequencyMin;
            xAxis.frequencyMax = clip.frequency;
            if (GetVisualizerWidth() != lastWidth)
            {
                xAxis.Redraw(); //Let it know it needs to redraw bc the width changed
            }
            lastWidth = GetVisualizerWidth();
        }
Exemplo n.º 15
0
 /// <summary>
 /// Returns Face radius in ball model (Euclidean metric)
 /// </summary
 private double FaceRadius(double inRadiusEuclidean)
 {
     return((Math.Pow(RBall, 2) - Math.Pow(inRadiusEuclidean, 2)) / (2 * inRadiusEuclidean));
 }
Exemplo n.º 16
0
 public static Vector2f Pow(this Vector2f value, float e)
 {
     return(new((float)Math.Pow(value.X, e), (float)Math.Pow(value.Y, e)));
 }
Exemplo n.º 17
0
 public static double ExpoIn(double b, double c, double t, double d = 1)
 {
     return((t == 0) ? b : c *Math.Pow(2, 10 *(t / d - 1)) + b);
 }
Exemplo n.º 18
0
 /// <summary>
 /// One arguments function
 /// </summary>
 /// <param name="firstValue">
 /// This function calculates 2 of the power a first value
 /// </param>
 /// <returns></returns>
 public double Calculate(double firstValue)
 {
     return(Math.Pow(2, firstValue));
 }
Exemplo n.º 19
0
        static MyLodStrategy()
        {
            const int   MAX_LOD_COUNT = 8;
            const float LOD_TRANSITION_DISTANCE_THRESHOLD = 4;
            const float MIN_TRANSITION_DISTANCE           = 4;

            m_lodTransitionDistances = new float[MAX_LOD_COUNT];
            for (int i = 0; i < m_lodTransitionDistances.Length; i++)
            {
                float lodTranstionDistance = Math.Max(LOD_TRANSITION_DISTANCE_THRESHOLD * (float)Math.Pow(2, i), MIN_TRANSITION_DISTANCE);
                m_lodTransitionDistances[i] = lodTranstionDistance;
            }

            m_allPassIds = new List <int>();
            for (int i = 0; i < MyPassIdResolver.AllPassesCount; i++)
            {
                m_allPassIds.Add(i);
            }
        }
Exemplo n.º 20
0
 public static int SetLevels(int levels)
 {
     m_maxSubdivisions = (int)Math.Pow(2, levels);
     return(m_maxSubdivisions);
 }
Exemplo n.º 21
0
 private static float pow(float x, float y)
 {
     return((float)Math.Pow(x, y));
 }
Exemplo n.º 22
0
 private static int Math_Ldexp(ILuaState lua)
 {
     lua.PushNumber(lua.L_CheckNumber(1) * Math.Pow(2, lua.L_CheckNumber(2)));
     return(1);
 }
Exemplo n.º 23
0
        private void createFeatureEllipse(int index, CameraSpacePoint left, CameraSpacePoint right)
        {
            //calculate vectors between the reference and the left and right feature
            double leftLength  = Math.Sqrt(Math.Pow((left.X - _referencePoint.X), 2) + Math.Pow((left.Y - _referencePoint.Y), 2) + Math.Pow((left.Z - _referencePoint.Z), 2)) * 1000;
            double rightLength = Math.Sqrt(Math.Pow((right.X - _referencePoint.X), 2) + Math.Pow((right.Y - _referencePoint.Y), 2) + Math.Pow((right.Z - _referencePoint.Z), 2)) * 1000;

            //Calculates difference and scales up to 0 - 255 range (ish)
            double vectDiff = Math.Abs(leftLength - rightLength) * 50;

            if (vectDiff > 255)
            {
                vectDiff = 255;
            }

            Color featureColor = Colors.Black;

            featureColor.R = System.Convert.ToByte(vectDiff);
            featureColor.G = System.Convert.ToByte(vectDiff);
            featureColor.B = System.Convert.ToByte(vectDiff);

            for (int i = 0; i < 2; i++)
            {
                createEllipseInstance(featureColor);
            }
        }
Exemplo n.º 24
0
 private static int Math_Pow(ILuaState lua)
 {
     lua.PushNumber(Math.Pow(lua.L_CheckNumber(1),
                             lua.L_CheckNumber(2)));
     return(1);
 }
Exemplo n.º 25
0
        public void Refresh()
        {
            AudioSourceVisualizer audioSourceVisualizer;

            try {
                audioSourceVisualizer = target as AudioSourceVisualizer;
            }
            catch (System.Exception e) {
                Debug.Log("Not ready");
                return;
            }
            AudioSource audioSource = audioSourceVisualizer.source;

            //First get the RMS of the audio channel as a whole

            /*float[] intensityBins = new float[AudioSourceVisualizerEditor.samples];
             * audioSource.GetOutputData(intensityBins, 0);
             * float sum = 0;
             * foreach(float sample in intensityBins) {
             *  sum += sample * sample; // sum squared samples
             * }
             * float rms = Mathf.Sqrt(sum / AudioSourceVisualizerEditor.samples); // rms = square root of average*/


            float[] spectrumBins = new float[AudioSourceVisualizerEditor.samples];
            audioSource.GetSpectrumData(spectrumBins, 0, FFTWindow.Rectangular);

            int idx = 0;

            foreach (VisualElement bin in bins)
            {
                double binRange = (double)idx / bins.Length;
                int    binIdx   = (int)Math.Truncate(Math.Pow(10.0, binRange * 3.6 + 1.0) / Math.Pow(10.0, 4.6) * samples);

                float db     = 20 * Mathf.Log10(spectrumBins[binIdx] / 1.0e-6f); // calculate dB
                float linear = Mathf.Clamp(db / 80.0f, 0.0f, 1.0f);

                bin.style.height          = linear * (AudioSourceVisualizerEditor.height - 2) + 2;
                bin.style.backgroundColor = new Color(linear, 68.0f / 255.0f, 136.0f / 255.0f, 1.0f);

                idx++;
            }
        }
Exemplo n.º 26
0
 public static Vector3d Pow(Vector3d vector, float exponent)
 {
     return(new Vector3d(Math.Pow(vector.x, exponent), Math.Pow(vector.y, exponent), Math.Pow(vector.z, exponent)));
 }
Exemplo n.º 27
0
 /// <summary>
 /// Returns the distance between two points.
 /// </summary>
 /// <param name="a"></param>
 /// <param name="b"></param>
 /// <returns></returns>
 public static int Distance(Point a, Point b)
 {
     return((int)Maths.Sqrt(Maths.Pow((b.X - a.X), 2) + Maths.Pow((b.Y - a.Y), 2)));
 }
Exemplo n.º 28
0
 public double Pow(double x, double y) => M.Pow(x, y);
Exemplo n.º 29
0
 public static double Ldexp(object self, [DefaultProtocol] double x, [DefaultProtocol] IntegerValue y)
 {
     return(x * SM.Pow(2, y.IsFixnum ? (double)y.Fixnum : y.Bignum.ToFloat64()));
 }
Exemplo n.º 30
0
 private static int NumDiv(int levels)
 {
     return((int)Math.Pow(2, levels));
 }