Exemplo n.º 1
0
        /// <summary>
        /// Fills this VectorHand with data read from the provided byte array, starting at
        /// the provided offset.
        /// </summary>
        public void ReadBytes(byte[] bytes, ref int offset)
        {
            if (bytes.Length - offset < numBytesRequired)
            {
                throw new System.IndexOutOfRangeException(
                          "Not enough room to read bytes for VectorHand encoding starting at offset "
                          + offset + " for array of size " + bytes + "; need at least "
                          + numBytesRequired + " bytes from the offset position.");
            }

            // Chirality.
            isLeft = bytes[offset++] == 0x00;

            // Palm position and rotation.
            for (int i = 0; i < 3; i++)
            {
                palmPos[i] = Convert.ToSingle(
                    BitConverterNonAlloc.ToInt16(bytes, ref offset))
                             / 4096f;
            }
            palmRot = Utils.DecompressBytesToQuat(bytes, ref offset);

            // Palm-local bone joint positions.
            for (int i = 0; i < NUM_JOINT_POSITIONS; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    jointPositions[i][j] = VectorHandExtensions.ByteToFloat(bytes[offset++]);
                }
            }
        }
        public void TestToUInt16()
        {
            UInt16 expected = BitConverter.ToUInt16(_bytes, 0);
            UInt16 actual   = BitConverterNonAlloc.ToUInt16(_bytes, 0);

            Assert.That(actual, Is.EqualTo(expected));
        }
        public void TestToInt32()
        {
            Int32 expected = BitConverter.ToInt32(_bytes, 0);
            Int32 actual   = BitConverterNonAlloc.ToInt32(_bytes, 0);

            Assert.That(actual, Is.EqualTo(expected));
        }
Exemplo n.º 4
0
        //Fills a 170-byte array, representing the pose and position of both hands
        void fillByteArray(byte[] handData)
        {
            if (handData == null || handData.Length != data.Length)
            {
                handData = new byte[data.Length];
            }
            int index = 0;

            //Left Hand
            for (int i = 0; i < 3; i++)
            {
                BitConverterNonAlloc.GetBytes(Convert.ToInt16(LPos[i] * 4096f), handData, ref index);
            }

            Utils.CompressQuatToBytes(LRot, handData, ref index);

            for (int i = 0; i < 75;)
            {
                handData[index++] = LBones[i++];
            }

            //Right Hand
            for (int i = 0; i < 3; i++)
            {
                BitConverterNonAlloc.GetBytes(Convert.ToInt16(RPos[i] * 4096f), handData, ref index);
            }

            Utils.CompressQuatToBytes(RRot, handData, ref index);

            for (int i = 0; i < 75;)
            {
                handData[index++] = RBones[i++];
            }
        }
        public void TestToDouble()
        {
            Double expected = BitConverter.ToDouble(_bytes, 0);
            Double actual   = BitConverterNonAlloc.ToDouble(_bytes, 0);

            Assert.That(actual, Is.EqualTo(expected));
        }
Exemplo n.º 6
0
        //Constructs an interpolable representation from a byte array
        public override void fillEncoding(byte[] handData)
        {
            if (handData == null || handData.Length != data.Length)
            {
                handData = new byte[data.Length];
            }
            data = handData;
            int index = 0;

            //Left Hand
            LPos = new Vector3((BitConverterNonAlloc.ToInt16(data, ref index)) / 4096f,
                               (BitConverterNonAlloc.ToInt16(data, ref index)) / 4096f,
                               (BitConverterNonAlloc.ToInt16(data, ref index)) / 4096f);

            LRot = Utils.DecompressBytesToQuat(data, ref index);

            for (int i = 0; i < 75;)
            {
                LBones[i++] = data[index++];
            }

            //Right Hand
            RPos = new Vector3((BitConverterNonAlloc.ToInt16(data, ref index)) / 4096f,
                               (BitConverterNonAlloc.ToInt16(data, ref index)) / 4096f,
                               (BitConverterNonAlloc.ToInt16(data, ref index)) / 4096f);

            RRot = Utils.DecompressBytesToQuat(data, ref index);

            for (int i = 0; i < 75;)
            {
                RBones[i++] = data[index++];
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Read a bool from the state.
        /// </summary>
        /// <returns>Bool value</returns>
        public bool ReadBool()
        {
            // Read into the shared buffer
            ReadBytes(sharedBuffer, 0, sizeof(bool));

            // Convert to bool
            return(BitConverterNonAlloc.GetBool(sharedBuffer));
        }
Exemplo n.º 8
0
        /// <summary>
        /// Read a float from the state.
        /// </summary>
        /// <returns>Float value</returns>
        public float ReadFloat()
        {
            // Read into the shared buffer
            ReadBytes(sharedBuffer, 0, sizeof(float));

            // Convert to float
            return(BitConverterNonAlloc.GetFloat(sharedBuffer));
        }
Exemplo n.º 9
0
        /// <summary>
        /// Read an int from the state.
        /// </summary>
        /// <returns>Int value</returns>
        public int Read32()
        {
            // Read into the shared buffer
            ReadBytes(sharedBuffer, 0, sizeof(int));

            // Convert to int
            return(BitConverterNonAlloc.GetInt(sharedBuffer));
        }
Exemplo n.º 10
0
        /// <summary>
        /// Read a short from the state.
        /// </summary>
        /// <returns>Short value</returns>
        public short Read16()
        {
            // Read into the shared buffer
            ReadBytes(sharedBuffer, 0, sizeof(short));

            // Convert to short
            return(BitConverterNonAlloc.GetShort(sharedBuffer));
        }
Exemplo n.º 11
0
        /// <summary>
        /// Write a bool to the state.
        /// </summary>
        /// <param name="value">bool value</param>
        public void Write(bool value)
        {
            // Use the shared buffer instead of allocating a new array
            BitConverterNonAlloc.GetBytes(sharedBuffer, value);

            // Write all bytes
            Write(sharedBuffer, 0, sizeof(bool));
        }
    public void TestFromInt32() {
      Int32 value = (Int32)UnityEngine.Random.Range(float.MinValue, float.MaxValue);
      var actual = BitConverter.GetBytes(value);

      int offset = 0;
      BitConverterNonAlloc.GetBytes(value, _bytes, ref offset);

      Assert.That(offset, Is.EqualTo(actual.Length));
      Assert.That(_bytes.Take(offset), Is.EquivalentTo(actual));
    }
Exemplo n.º 13
0
        /// <summary>
        /// Fills the provided byte array with a compressed, 86-byte form of this VectorHand,
        /// starting at the provided offset.
        ///
        /// Throws an IndexOutOfRangeException if the provided byte array doesn't have enough
        /// space (starting from the offset) to write the number of bytes required.
        /// </summary>
        public void FillBytes(byte[] bytesToFill, ref int offset)
        {
            if (_backingJointPositions == null)
            {
                throw new System.InvalidOperationException(
                          "Joint positions array is null. You must fill a VectorHand with data before "
                          + "you can use it to fill byte representations.");
            }

            if (bytesToFill.Length - offset < numBytesRequired)
            {
                throw new System.IndexOutOfRangeException(
                          "Not enough room to fill bytes for VectorHand encoding starting at offset "
                          + offset + " for array of size " + bytesToFill.Length + "; need at least "
                          + numBytesRequired + " bytes from the offset position.");
            }

            // Chirality.
            bytesToFill[offset++] = isLeft ? (byte)0x00 : (byte)0x01;

            // Palm position, each component compressed
            for (int i = 0; i < 3; i++)
            {
                BitConverterNonAlloc.GetBytes(Convert.ToInt16(palmPos[i] * 4096f),
                                              bytesToFill,
                                              ref offset);
            }

            // Palm rotation.
            Utils.CompressQuatToBytes(palmRot, bytesToFill, ref offset);

            // Joint positions.
            for (int j = 0; j < NUM_JOINT_POSITIONS; j++)
            {
                for (int i = 0; i < 3; i++)
                {
                    bytesToFill[offset++] =
                        VectorHandExtensions.FloatToByte(jointPositions[j][i]);
                }
            }
        }