/// <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++]); } } }
/// <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]); } } }