Exemplo n.º 1
0
    // Creates a new honeycomb in the game, initialized with the given data.
    public void InitializeWithData(HoneycombData data)
    {
        // Destroy all old game objects.
        foreach (Strip strip in this.strips)
        {
            GameObject.Destroy(strip);
        }
        this.strips.Clear();

        // Compute the honeycomb properties.
        float stripWidth = Util.edgeLengthToStripWidth(this.edgeLength);

        // Compute the position of the first strip.
        // The center of each strip is the middle of the strip.
        // Each strip runs parallel to the y-axis. Their centers are in a line parallel to the x-axis.
        Vector3 nextPosition = this.transform.position;

        nextPosition.x -= (data.strips.Length - 1f) * 0.5f * stripWidth;
        // Create the strips.
        for (int i = 0; i < data.strips.Length; ++i)
        {
            StripData stripData = data.strips[i];
            Strip     strip     = Instantiate <Strip>(this.stripPrefab, nextPosition, Quaternion.identity);
            strip.stripNumber = i;
            this.strips.Add(strip);
            strip.InitializeWithData(stripData, this.edgeLength);
            // Set the next hex's position.
            nextPosition.x += stripWidth;
        }
    }
Exemplo n.º 2
0
        private void DecodeCompressed(StripData strip)
        {
            _numBitPerPaletteEntry = strip.CodecId - strip.ParamSubtraction;
            _substrationVariable   = 1;
            _bitStreamManager      = new BitStreamManager(strip.ImageData);

            _currentLine   = 0;
            _currentColumn = 0;
            bool finishDecode = false;

            //if (strip.CodecId >= 0x54 && strip.CodecId <= 0x58) Debugger.Break();
            //if (strip.CodecId >= 0x40 && strip.CodecId <= 0x44) Debugger.Break();

            //Read palette and Draw the first pixel.
            _paletteIndex = _bitStreamManager.ReadByte();

            SetCurrentColor();

            _resultBitmap.SetPixel(_currentOffset + _currentColumn, _currentLine, _currentColor);

            while (!finishDecode)
            {
                var changeAction = _bitStreamManager.ReadBit();
                //changeAction = false (0): Draw next pixel with current palette index.
                //                          Otherwise changeAction is true and we need the next bit to decide what to do.

                //changeAction = true (1): We need the next bit to discover what to do.
                if (changeAction)
                {
                    if (strip.CompressionType == CompressionTypes.Method1)
                    {
                        DecodeCode1Specifics();
                    }
                    else if (strip.CompressionType == CompressionTypes.Method2)
                    {
                        DecodeCode2Specifics();
                    }
                }
                else
                {
                    DrawNextPixel();
                }


                if ((_currentColumn == 7 && _currentLine == (_height - 1)) || _bitStreamManager.EndOfStream)
                {
                    finishDecode = true;
                }
            }

            UsedIndexes.Sort();
        }
Exemplo n.º 3
0
 public void SetData(StripData data)
 {
     if (this.hexes.Count != data.hexes.Length)
     {
         Debug.LogError("Cannot set strip data: data has wrong number of hexes.");
         return;
     }
     for (int i = 0; i < this.hexes.Count; ++i)
     {
         HexData hexData = data.hexes[i];
         this.hexes[i].SetData(hexData);
     }
 }
Exemplo n.º 4
0
 // Sets the honeycomb's current data. Assumes the data has the same honeycomb size as the current honeycomb.
 // To change the size of the honeycomb, use InitializeWithData instead.
 public void SetData(HoneycombData data)
 {
     if (this.strips.Count != data.strips.Length)
     {
         Debug.LogError("Cannot set honeycomb data: data has wrong number of strips.");
         return;
     }
     for (int i = 0; i < this.strips.Count; ++i)
     {
         StripData stripData = data.strips[i];
         this.strips[i].SetData(stripData);
     }
 }
Exemplo n.º 5
0
        private StripData GetNextStrip()
        {
            StripData strip = null;

            switch (EncodeSettings)
            {
            case EncodeTypeSettings.AutoDetect:
                strip = EncodeUncompressed();

                StripData alternateMethod = EncodeCompressedMethod1(true);
                if (alternateMethod.ImageData.Length < strip.ImageData.Length)
                {
                    strip = alternateMethod;
                }

                alternateMethod = EncodeCompressedMethod1(false);
                if (alternateMethod.ImageData.Length < strip.ImageData.Length)
                {
                    strip = alternateMethod;
                }

                alternateMethod = EncodeCompressedMethod2();
                if (alternateMethod.ImageData.Length < strip.ImageData.Length)
                {
                    strip = alternateMethod;
                }
                break;

            case EncodeTypeSettings.Uncompressed:
                strip = EncodeUncompressed();
                break;

            case EncodeTypeSettings.Method1Horizontal:
                strip = EncodeCompressedMethod1(true);
                break;

            case EncodeTypeSettings.Method1Vertical:
                strip = EncodeCompressedMethod1(false);
                break;

            case EncodeTypeSettings.Method2:
                strip = EncodeCompressedMethod2();
                break;
            }

            return(strip);
        }
Exemplo n.º 6
0
        public void Add(StripData stripdata)
        {
            float[] tof      = stripdata.tof;
            float[] amp      = stripdata.amp;
            double  startpos = stripdata.startpos;

            inc = stripdata.inc;
            int cellnum = stripdata.cellnum;
            int id      = stripdata.id;
            int port    = stripdata.port;

            if (id == 1 << curgate && curport == port)
            {
                Array.Copy(tof, 0, toflist, index, cellnum);
                Array.Copy(amp, 0, amplist, index, cellnum);
                index  += cellnum;
                maxpos += inc * cellnum;
            }
            UpdatePic(maxpos);
        }
Exemplo n.º 7
0
        private void DecodeUncompressed(StripData strip)
        {
            _bitStreamManager = new BitStreamManager(strip.ImageData);

            bool finishDecode = false;

            _paletteIndex = _bitStreamManager.ReadByte();
            SetCurrentColor();
            _resultBitmap.SetPixel(_currentOffset, 0, _currentColor);

            while (!finishDecode)
            {
                _paletteIndex = _bitStreamManager.ReadByte();
                SetCurrentColor();
                DrawNextPixel();
                if ((_currentColumn == 7 && _currentLine == (_height - 1)) || _bitStreamManager.EndOfStream)
                {
                    finishDecode = true;
                }
            }
        }
Exemplo n.º 8
0
    // stripCount should be an odd number...?
    public static HoneycombData Generate(int stripCount, int maxLength)
    {
        HoneycombData honey = new HoneycombData();

        honey.strips = new StripData[stripCount];

        for (int s = 0; s < stripCount; ++s)
        {
            int       length = GetStripLength(s, stripCount, maxLength);
            StripData strip  = new StripData();
            strip.hexes     = new HexData[length];
            honey.strips[s] = strip;

            for (int i = 0; i < length; ++i)
            {
                HexData hex = new HexData();
                strip.hexes[i] = hex;
            }
        }

        return(honey);
    }
Exemplo n.º 9
0
    public void InitializeWithData(StripData data, float edgeLength)
    {
        float hexHeight = Util.edgeLengthToHexHeight(edgeLength);

        // Compute the position of the first strip.
        // The center of each hex is the middle of the hex.
        // Their centers are in a line parallel to the y-axis.
        Vector3 nextPosition = this.transform.position;

        nextPosition.y -= (data.hexes.Length - 1f) * 0.5f * hexHeight;
        // Create the hexes.
        for (int i = 0; i < data.hexes.Length; ++i)
        {
            HexData hexData = data.hexes[i];
            Hex     hex     = Instantiate <Hex>(hexPrefab, nextPosition, Quaternion.identity);
            hex.stripNumber = this.stripNumber;
            hex.hexNumber   = i;
            this.hexes.Add(hex);
            hex.InitializeWithData(hexData);
            // Set the next hex's position.
            nextPosition.y += hexHeight;
        }
    }
Exemplo n.º 10
0
        private StripData EncodeUncompressed()
        {
            var strip     = new StripData();
            var bitStream = new BitStreamManager();

            strip.CodecId = 0x01;

            _renderdingDirection = strip.RenderdingDirection;
            _currentLocation.X   = 0;
            _currentLocation.Y   = 0;

            int firstPaletteIndex = FindTableIndex(_imageToEncode.GetPixel(_currentOffset + 0, 0));

            bitStream.AddByte((byte)firstPaletteIndex);

            while (!(_currentLocation.X == 7 && _currentLocation.Y == (_height - 1)))
            {
                var paletteIndex = FindTableIndex(GetNextPixel());
                bitStream.AddByte((byte)paletteIndex);
            }

            strip.ImageData = bitStream.ToByteArray();
            return(strip);
        }
Exemplo n.º 11
0
    public SimDataQuad(Vector3 pos, float gridResolution, Vector3 boundingBoxMin, Vector3 boundingBoxMax, SurfaceDelimitation[] surfaceLimits)
    {
        this.pos            = pos;
        this.gridResolution = gridResolution;
        this.boundingBoxMin = boundingBoxMin;
        this.boundingBoxMax = boundingBoxMax;

        // Only pos used is pos.z
        // only boundingBoxMin used is .z

        float halfGridResolution = gridResolution / 2.0f;
        int   simStripCount      = surfaceLimits.Length;

        // Allocate all strips data
        stripToVerticeData = new StripData[simStripCount];

        // Compute reference offset
        int referenceOfs = Mathf.CeilToInt((pos.z - boundingBoxMin.z - halfGridResolution) / gridResolution);

        // Evaluate each strip
        simSize      = 0;
        maxStripSize = 0;
        for (int i = 0; i < simStripCount; i++)
        {
            float backZ  = surfaceLimits[i].back;
            float frontZ = surfaceLimits[i].front;

            if (i > 0)
            {
                backZ  = Mathf.Min(backZ, surfaceLimits[i - 1].back);
                frontZ = Mathf.Max(frontZ, surfaceLimits[i - 1].front);
            }
            if (i < simStripCount - 1)
            {
                backZ  = Mathf.Min(backZ, surfaceLimits[i + 1].back);
                frontZ = Mathf.Max(frontZ, surfaceLimits[i + 1].front);
            }

            int nbBackPoints  = Mathf.CeilToInt((pos.z - backZ - halfGridResolution) / gridResolution);
            int nbFrontPoints = Mathf.CeilToInt((frontZ - pos.z - halfGridResolution) / gridResolution);

            stripToVerticeData[i].localOffset  = referenceOfs - nbBackPoints;
            stripToVerticeData[i].globalOffset = simSize;
            stripToVerticeData[i].count        = nbBackPoints + nbFrontPoints + 2;
            simSize += stripToVerticeData[i].count;

            maxStripSize = Mathf.Max(maxStripSize, stripToVerticeData[i].localOffset + stripToVerticeData[i].count);
        }

        // Allocate for the whole sim
        // Array Length == Nb Vertex/Values Total
        //speedY = new float[simSize];
        neighbor = new Neighbor[simSize];

        // Init sim
        for (int x = 0; x < simStripCount; x++)
        {
            for (int z = 0; z < maxStripSize; z++)
            {
                int index = GetVerticeIndex(x, z);
                if (index >= 0)
                {
                    neighbor[index].prevZ = GetVerticeIndex(x, z - 1, index);
                    neighbor[index].nextZ = GetVerticeIndex(x, z + 1, index);
                    neighbor[index].prevX = GetVerticeIndex(x - 1, z, index);
                    neighbor[index].nextX = GetVerticeIndex(x + 1, z, index);
                    //Debug.Log("index " + index + " = " + prevIndexZ[index] + ", " + nextIndexZ[index] + ", " + prevIndexX[index] + ", " + nextIndexX[index]);
                }
            }
        }

        for (int x = 0; x < simStripCount; x++)
        {
            for (int z = 0; z < maxStripSize; z++)
            {
                int index = GetVerticeIndex(x, z);
                if (index >= 0)
                {
                    if ((neighbor[index].prevZ != index) && (neighbor[index].prevZ != neighbor[index].nextZ))
                    {
                        neighbor[index].prevZ = GetVerticeIndex(x, z - 1, index);
                    }
                    neighbor[index].nextZ = GetVerticeIndex(x, z + 1, index);
                    neighbor[index].prevX = GetVerticeIndex(x - 1, z, index);
                    neighbor[index].nextX = GetVerticeIndex(x + 1, z, index);
                }
            }
        }
    }
Exemplo n.º 12
0
        private StripData EncodeCompressedMethod2()
        {
            var strip     = new StripData();
            var bitStream = new BitStreamManager();

            bool transparent;
            int  paletteSize;

            VerifyStrip(out transparent, out paletteSize, true);

            bool alternateCode = EncodeSettings == EncodeTypeSettings.Method2AlternateCode;

            strip.CodecId = (byte)(GetSubstractionCode(transparent, true, CompressionTypes.Method2, alternateCode) + paletteSize);
            if (strip.CompressionType == CompressionTypes.Unknow)
            {
                strip.CodecId = (byte)(GetSubstractionCode(transparent, true, CompressionTypes.Method2, !alternateCode) + paletteSize);
            }

            _renderdingDirection = strip.RenderdingDirection;
            _currentLocation.X   = 0;
            _currentLocation.Y   = 0;

            //Color currentColor = GetNextPixel(); //pq isso fica menos errado!?
            Color currentColor        = _imageToEncode.GetPixel(_currentOffset + 0, 0);
            int   currentPaletteIndex = FindTableIndex(currentColor);

            bitStream.AddByte((byte)currentPaletteIndex);

            byte accumulatedSameColor = 0;

            while (!(_currentLocation.X == 7 && _currentLocation.Y == (_height - 1)))
            {
                var newPaletteIndex = FindTableIndex(GetNextPixel());

                if (newPaletteIndex == currentPaletteIndex)
                {
                    accumulatedSameColor++; //Add one to the count of pixels with the same color.
                    if (accumulatedSameColor == 255)
                    {
                        //If we have pixels with the previous color drawed before the color change
                        //We need to write that information to the data, before process the new color.
                        WriteAccumulatedColor(bitStream, accumulatedSameColor);
                        accumulatedSameColor = 0;
                    }
                }
                else
                {
                    if (accumulatedSameColor > 0)
                    {
                        //If we have pixels with the previous color drawed before the color change
                        //We need to write that information to the data, before process the new color.
                        WriteAccumulatedColor(bitStream, accumulatedSameColor);
                        accumulatedSameColor = 0;
                    }


                    /*
                     * Then discory the new bit code.
                     *
                     * 10: Read a new palette index from the bitstream (i.e., the number of bits specified by the parameter), and draw the next pixel.
                     * 11: Read the next 3 bit value, and perform an action, depending on the value:
                     *     000 (0): Decrease current palette index by 4.
                     *     001 (1): Decrease current palette index by 3.
                     *     010 (2): Decrease current palette index by 2.
                     *     011 (3): Decrease current palette index by 1.
                     *     100 (4): Read next 8 bits. Draw the number of pixels specified by these 8 bits with the current palette index (somewhat similar to RLE).
                     *     101 (5): Increase current palette index by 1.
                     *     110 (6): Increase current palette index by 2.
                     *     111 (7): Increase current palette index by 3.
                     */

                    bitStream.AddBit(true); //Add the change action bit

                    if ((newPaletteIndex < (currentPaletteIndex - 4)) || (newPaletteIndex > (currentPaletteIndex + 3)))
                    {
                        //reset the negation value, read the next index from the palette,
                        //and draw the pixel.
                        bitStream.AddBit(false);
                        bitStream.AddByte((byte)newPaletteIndex, paletteSize);
                    }
                    else
                    {
                        //Add the read code bit
                        bitStream.AddBit(true);
                        if (newPaletteIndex == (currentPaletteIndex - 4)) //000 (0): Decrease current palette index by 4.
                        {
                            //Add the code 0
                            bitStream.AddByte(0, 3);
                        }
                        else if (newPaletteIndex == (currentPaletteIndex - 3)) //001 (1): Decrease current palette index by 3.
                        {
                            //Add the code 1
                            bitStream.AddByte(1, 3);
                        }
                        else if (newPaletteIndex == (currentPaletteIndex - 2)) //010 (2): Decrease current palette index by 2.
                        {
                            //Add the code 2
                            bitStream.AddByte(2, 3);
                        }
                        else if (newPaletteIndex == (currentPaletteIndex - 1)) //011 (3): Decrease current palette index by 1.
                        {
                            //Add the code 3
                            bitStream.AddByte(3, 3);
                        }
                        else if (newPaletteIndex == (currentPaletteIndex + 1)) //101 (5): Increase current palette index by 1.
                        {
                            //Add the code 5
                            bitStream.AddByte(5, 3);
                        }
                        else if (newPaletteIndex == (currentPaletteIndex + 2)) //110 (6): Increase current palette index by 2.
                        {
                            //Add the code 6
                            bitStream.AddByte(6, 3);
                        }
                        else if (newPaletteIndex == (currentPaletteIndex + 3)) //111 (7): Increase current palette index by 3.
                        {
                            //Add the code 7
                            bitStream.AddByte(7, 3);
                        }
                        else
                        {
                            throw new ImageEncodeException("nao podia ter caido aqui");
                        }
                    }

                    currentPaletteIndex = newPaletteIndex;
                }
            }

            //if the loop terminated with accumulated bits pending, add these to the stream.
            if (accumulatedSameColor > 0)
            {
                //If we have pixels with the previous color drawed before the color change
                //We need to write that information to the data, before process the new color.
                WriteAccumulatedColor(bitStream, accumulatedSameColor);
            }

            strip.ImageData = bitStream.ToByteArray();
            return(strip);
        }
Exemplo n.º 13
0
        private StripData EncodeCompressedMethod1(bool horizontalDirection)
        {
            var strip     = new StripData();
            var bitStream = new BitStreamManager();

            bool transparent;
            int  paletteSize;

            VerifyStrip(out transparent, out paletteSize, true);

            strip.CodecId = (byte)(GetSubstractionCode(transparent, horizontalDirection, CompressionTypes.Method1) + paletteSize);

            _renderdingDirection = strip.RenderdingDirection;
            _currentLocation.X   = 0;
            _currentLocation.Y   = 0;

            int negationValue = 1;

            //Color currentColor = GetNextPixel(); //pq isso fica menos errado!?
            Color currentColor        = _imageToEncode.GetPixel(_currentOffset + 0, 0);
            int   currentPaletteIndex = FindTableIndex(currentColor);

            bitStream.AddByte((byte)currentPaletteIndex);


            while (!(_currentLocation.X == 7 && _currentLocation.Y == (_height - 1)))
            {
                var newPaletteIndex = FindTableIndex(GetNextPixel());

                if (newPaletteIndex == currentPaletteIndex)
                {
                    bitStream.AddBit(false); //continue Drawing the same color.
                }
                else
                {
                    bitStream.AddBit(true); //discover what to do.

                    if (newPaletteIndex == (currentPaletteIndex - negationValue))
                    {
                        //subtract the negation value from the palette and draw the next pixel;
                        bitStream.AddBit(true);
                        bitStream.AddBit(false);
                    }
                    else if (newPaletteIndex == (currentPaletteIndex - (negationValue * -1)))
                    {
                        //invert the negation value, subtract it from the palette
                        //and draw the next pixel;
                        negationValue *= -1;
                        bitStream.AddBit(true);
                        bitStream.AddBit(true);
                    }
                    else
                    {
                        //reset the negation value, read the next index from the palette,
                        //and draw the pixel.
                        negationValue = 1;
                        bitStream.AddBit(false);
                        bitStream.AddByte((byte)newPaletteIndex, paletteSize);
                    }

                    currentPaletteIndex = newPaletteIndex;
                }
            }

            strip.ImageData = bitStream.ToByteArray();
            return(strip);
        }