Tanoda
VectorHandTests.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System.Linq;
10using System.Collections.Generic;
11using UnityEngine;
12using NUnit.Framework;
13
14namespace Leap.Unity.Tests {
15 using Encoding;
16
17 public class VectorHandTests {
18
19 [Test]
20 public void EncodeDecodeTest() {
21 const float TOLERANCE = 0.01f; //1 cm for all positions
22
23 Frame frame = TestHandFactory.MakeTestFrame(0, includeLeftHand: true, includeRightHand: true, unitType: TestHandFactory.UnitType.UnityUnits);
24
25 foreach (var hand in frame.Hands) {
26
27 byte[] bytes;
28 {
29 VectorHand vHand = new VectorHand();
30 bytes = new byte[vHand.numBytesRequired];
31
32 //Encode the hand into the vHand representation
33 vHand.Encode(hand);
34
35 //Then convert the vHand into a binary representation
36 vHand.FillBytes(bytes);
37 }
38
39 Hand result;
40 {
41 VectorHand vHand = new VectorHand();
42
43 //Convert the binary representation back into a vHand
44 int offset = 0;
45 vHand.ReadBytes(bytes, ref offset);
46
47 //Decode the vHand back into a normal Leap Hand
48 result = new Hand();
49 vHand.Decode(result);
50 }
51
52 Assert.That(result.IsLeft, Is.EqualTo(hand.IsLeft));
53 Assert.That((result.PalmPosition - hand.PalmPosition).Magnitude, Is.LessThan(TOLERANCE));
54
55 foreach (var resultFinger in result.Fingers) {
56 var finger = hand.Fingers.Single(f => f.Type == resultFinger.Type);
57
58 for (int i = 0; i < 4; i++) {
59 Bone resultBone = resultFinger.bones[i];
60 Bone bone = finger.bones[i];
61
62 Assert.That((resultBone.NextJoint - bone.NextJoint).Magnitude, Is.LessThan(TOLERANCE));
63 Assert.That((resultBone.PrevJoint - bone.PrevJoint).Magnitude, Is.LessThan(TOLERANCE));
64 Assert.That((resultBone.Center - bone.Center).Magnitude, Is.LessThan(TOLERANCE));
65 }
66 }
67 }
68 }
69 }
70}
The Bone class represents a tracked bone.
Definition: Bone.cs:26
Vector Center
The midpoint of the bone.
Definition: Bone.cs:96
Vector PrevJoint
The base of the bone, closest to the wrist. In anatomical terms, this is the proximal end of the bone...
Definition: Bone.cs:83
Vector NextJoint
The end of the bone, closest to the finger tip. In anatomical terms, this is the distal end of the bo...
Definition: Bone.cs:90
The Frame class represents a set of hand and finger tracking data detected in a single frame.
Definition: Frame.cs:24
The Hand class reports the physical characteristics of a detected hand.
Definition: Hand.cs:26
bool IsLeft
Identifies whether this Hand is a left hand.
Definition: Hand.cs:296
Vector PalmPosition
The center position of the palm.
Definition: Hand.cs:165
A Vector-based encoding of a Leap Hand.
Definition: VectorHand.cs:36
void FillBytes(byte[] bytesToFill, ref int offset)
Fills the provided byte array with a compressed, 86-byte form of this VectorHand, starting at the pro...
Definition: VectorHand.cs:271
int numBytesRequired
The number of bytes required to encode a VectorHand into its byte representation. The byte representa...
Definition: VectorHand.cs:222
void Encode(Hand fromHand)
Definition: VectorHand.cs:88
void ReadBytes(byte[] bytes, int offset=0)
Fills this VectorHand with data read from the provided byte array, starting at the provided offset.
Definition: VectorHand.cs:229
void Decode(Hand intoHand)
Definition: VectorHand.cs:109