Tanoda
TransformCopyTest.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;
10using System.Linq;
11using System.Reflection;
12using System.Collections;
13using NUnit.Framework;
14
15namespace Leap.Unity.Tests {
16
19
20 protected override Frame createFrame() {
21 _originalFrame = TestHandFactory.MakeTestFrame(0, true, true);
22 return _originalFrame.TransformedCopy(LeapTransform.Identity);
23 }
24
25 [Test]
26 public void IdsAreSame() {
27 Assert.That(_frame.Hands.Count, Is.EqualTo(_originalFrame.Hands.Count));
28
29 for (int i = 0; i < _frame.Hands.Count; i++) {
30 Hand oldHand = _originalFrame.Hands[i];
31 Hand newHand = _frame.Hands[i];
32 Assert.That(oldHand.Id, Is.EqualTo(newHand.Id));
33
34 for (int j = 0; j < 5; j++) {
35 Finger oldFinger = oldHand.Fingers[j];
36 Finger newFinger = newHand.Fingers[j];
37 Assert.That(oldFinger.Id, Is.EqualTo(newFinger.Id));
38 }
39 }
40 }
41
42 [Test]
43 public void AreBinaryEqual() {
44 assertObjectsEqual("Frame", _originalFrame, _frame);
45 }
46
47 private void assertObjectsEqual(string objectName, object a, object b) {
48 if ((a == null) != (b == null)) {
49 Assert.Fail("For " + objectName + ", one object was null an the other was not.");
50 return;
51 }
52
53 Type typeA = a.GetType();
54 Type typeB = b.GetType();
55
56 if (typeA != typeB) {
57 Assert.Fail("For " + objectName + ", object Type " + typeA + " is not equal to type " + typeB + ".");
58 }
59
60 if (typeA.IsValueType) {
61 Assert.That(a, Is.EqualTo(b), objectName);
62 return;
63 }
64
65 if (a is IList) {
66 IList aList = a as IList;
67 IList bList = b as IList;
68
69 Assert.That(aList.Count, Is.EqualTo(bList.Count), objectName + ".Count");
70
71 for (int i = 0; i < aList.Count; i++) {
72 assertObjectsEqual(objectName + "[" + i + "]", aList[i], bList[i]);
73 }
74 } else {
75 FieldInfo[] fields = typeA.GetFields(BindingFlags.Public | BindingFlags.Instance);
76 foreach (FieldInfo field in fields) {
77 assertObjectsEqual(objectName + "." + field.Name, field.GetValue(a), field.GetValue(b));
78 }
79
80 PropertyInfo[] properties = typeA.GetProperties(BindingFlags.Public | BindingFlags.Instance);
81 foreach (PropertyInfo property in properties) {
82 if (property.GetIndexParameters().Length == 0) {
83 object propA;
84 try {
85 propA = property.GetValue(a, null);
86 } catch (Exception exceptionA) {
87 try {
88 property.GetValue(b, null);
89 Assert.Fail("For " + objectName + ", one property threw an exception where the other did not.");
90 return;
91 } catch (Exception exceptionB) {
92 Assert.That(exceptionA.GetType(), Is.EqualTo(exceptionB.GetType()), "For " + objectName + ", both properties threw exceptions but their types were different.");
93 return;
94 }
95 }
96
97 object propB = property.GetValue(b, null);
98
99 assertObjectsEqual(objectName + "." + property.Name, propA, propB);
100 }
101 }
102 }
103 }
104 }
105
107 protected static Vector translation = Vector.Forward;
109
110 protected override Frame createFrame() {
111 _originalFrame = TestHandFactory.MakeTestFrame(0, true, true);
113 return _originalFrame.TransformedCopy(forwardTransform);
114 }
115
116 [Test]
117 public void IsTranslated() {
118 for (int i = 0; i < _frame.Hands.Count; i++) {
119 Hand oldHand = _originalFrame.Hands[i];
120 Hand newHand = _frame.Hands[i];
121
122 assertVectorsEqual(oldHand.PalmPosition + translation, newHand.PalmPosition, "Palm Position");
123
124 for (int j = 0; j < 5; j++) {
125 Finger oldFinger = oldHand.Fingers[j];
126 Finger newFinger = newHand.Fingers[j];
127
128 assertVectorsEqual(oldFinger.TipPosition + translation, newFinger.TipPosition, oldFinger.Type.ToString());
129 }
130 }
131 }
132 }
133}
The Finger class represents a tracked finger.
Definition: Finger.cs:20
override string ToString()
A string containing a brief, human readable description of the Finger object.
Definition: Finger.cs:87
int Id
A unique ID assigned to this Finger object, whose value remains the same across consecutive frames wh...
Definition: Finger.cs:110
Finger.FingerType Type
The type of this finger.
Definition: Finger.cs:95
Vector TipPosition
The tip position of this Finger.
Definition: Finger.cs:122
The Frame class represents a set of hand and finger tracking data detected in a single frame.
Definition: Frame.cs:24
List< Hand > Hands
The list of Hand objects detected in this frame, given in arbitrary order. The list can be empty if n...
Definition: Frame.cs:156
The Hand class reports the physical characteristics of a detected hand.
Definition: Hand.cs:26
Vector PalmPosition
The center position of the palm.
Definition: Hand.cs:165
int Id
A unique ID assigned to this Hand object, whose value remains the same across consecutive frames whil...
Definition: Hand.cs:152
List< Finger > Fingers
The list of Finger objects detected in this frame that are attached to this hand, given in order from...
Definition: Hand.cs:159
void assertVectorsEqual(Vector a, Vector b, string vectorName="Vector")
The LeapQuaternion struct represents a rotation in three-dimensional space.
static readonly LeapQuaternion Identity
The identity quaternion.
The LeapTransform class represents a transform in three dimensional space.
static readonly LeapTransform Identity
The identity transform.
The Vector struct represents a three-component mathematical vector or point such as a direction or po...
Definition: Vector.cs:36
static readonly Vector Forward
The unit vector pointing forward along the negative z-axis: (0, 0, -1)
Definition: Vector.cs:338