コード例 #1
0
ファイル: GridDataIO.cs プロジェクト: gicait/ur-scape
    public static void ParseBinValues(BinaryReader br, GridData grid)
    {
        // Allocate grid memory
        grid.InitGridValues(false);

        byte[] bytes = null;

        // Read values
        ReadArray(br, ref bytes, grid.values, 4);

        // Read if it has values mask
        var withMask = br.ReadBoolean();

        if (withMask)
        {
            grid.CreateMaskBuffer();

            // Read values mask
            ReadArray(br, ref bytes, grid.valuesMask);
        }

        // Read distribution values
        var count = br.ReadByte();

        if (count > 0)
        {
            int[] distributionValues = new int[count];
            ReadArray(br, ref bytes, distributionValues, 4);

            // Read max distribution value
            int maxDistributionValue = br.ReadInt32();
            grid.SetDistribution(distributionValues, maxDistributionValue);
        }
    }
コード例 #2
0
ファイル: WeightedMapLayer.cs プロジェクト: gicait/ur-scape
    //
    // Private Methods
    //

    private void InitArrays()
    {
        int length = grid.countX * grid.countY;

        if (length > 0)
        {
            if (grid.values == null || grid.values.Length != length)
            {
                grid.values     = new float[length];
                grid.valuesMask = GridData.CreateMaskBuffer(length);
                divisors        = new float[length];
            }
        }
        else
        {
            grid.values     = null;
            grid.valuesMask = null;
            divisors        = null;
        }
    }
コード例 #3
0
ファイル: GridDataIO.cs プロジェクト: gicait/ur-scape
    private static void ReadValues(StreamReader sr, string filename, GridData grid, List <float> valuesMap)
    {
        int count = grid.countX * grid.countY;

        float[] values   = new float[count];
        byte[]  masks    = GridData.CreateMaskBuffer(count); // masks array size needs to be multiple of 4
        float   minValue = float.MaxValue;
        float   maxValue = float.MinValue;

        string line;

        string[] cells;
        float    value;
        byte     mask;
        bool     hasMask     = false;
        int      index       = 0;
        var      cultureInfo = CultureInfo.InvariantCulture;

        if (valuesMap == null)
        {
            // Read each data row at a time with value
            while ((line = sr.ReadLine()) != null)
            {
                cells         = line.Split(',');
                value         = float.Parse(cells[0], cultureInfo);
                mask          = byte.Parse(cells[1]);
                values[index] = value;
                masks[index]  = mask;

                if (mask == 1)
                {
                    minValue = Mathf.Min(minValue, value);
                    maxValue = Mathf.Max(maxValue, value);
                }
                else
                {
                    hasMask = true;
                }

                index++;
            }
        }
        else
        {
            // Read each data row at a time and replace original value with value set for name from NAMETOVALUE
            while ((line = sr.ReadLine()) != null)
            {
                cells = line.Split(',');
                int valueAsId = int.Parse(cells[0]);
                mask          = byte.Parse(cells[1]);
                value         = mask == 1 ? valuesMap[valueAsId - 1] : -1;
                values[index] = value;
                masks[index]  = mask;

                if (mask == 1)
                {
                    minValue = Mathf.Min(minValue, value);
                    maxValue = Mathf.Max(maxValue, value);
                }
                else
                {
                    hasMask = true;
                }

                index++;
            }
        }

        if (index != count)
        {
            Debug.LogWarning(filename + " has " + index + " values instead of the expected " + count);
        }

        grid.minValue = minValue;
        grid.maxValue = maxValue;
        grid.values   = values;

        // Only copy mask if at least one value is 0
        if (hasMask)
        {
            grid.valuesMask = masks;
        }
    }