Tanoda
FromThenTests.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;
11using System.Collections.Generic;
12using UnityEngine;
13using NUnit.Framework;
14
15namespace Leap.Unity.Tests {
16
24 public class FromThenTests {
25
26 public static float EPSILON = 0.0006f;
27
28 #region Vector3
29
30 private static Vector3 VEC_A = new Vector3(0.5f, 0.2f, 0.8f);
31 private static Vector3 VEC_B = new Vector3(0.13f, 0.98f, 3000f);
32
33 [Test]
34 public void FromVecAToVecB() {
35 Assert.That(AreVector3sEqual(VEC_A.Then(VEC_B.From(VEC_A)), VEC_B));
36 }
37
38 [Test]
39 public void ToVecBFromVecA() {
40 Assert.That(AreVector3sEqual(VEC_A.Then(VEC_A.To(VEC_B)), VEC_B));
41 }
42
43 [Test]
44 public void FromVecBToVecA() {
45 Assert.That(AreVector3sEqual(VEC_B.Then(VEC_A.From(VEC_B)), VEC_A));
46 }
47
48 [Test]
49 public void ToVecAFromVecB() {
50 Assert.That(AreVector3sEqual(VEC_B.Then(VEC_B.To(VEC_A)), VEC_A));
51 }
52
53 private static bool AreVector3sEqual(Vector3 a, Vector3 b) {
54 return (a - b).magnitude < EPSILON;
55 }
56
57 #endregion
58
59 #region Quaternion
60
61 private static Quaternion QUAT_A {
62 get { return Quaternion.AngleAxis(90f, Vector3.up); }
63 }
64 private static Quaternion QUAT_B {
65 get { return Quaternion.AngleAxis(43f, Vector3.one.normalized); }
66 }
67
68 [Test]
69 public void FromQuatAToQuatB() {
70 Assert.That(AreQuaternionsEqual(QUAT_A.Then(QUAT_B.From(QUAT_A)), QUAT_B));
71 }
72
73 [Test]
74 public void ToQuatAFromQuatB() {
75 Assert.That(AreQuaternionsEqual(QUAT_A.Then(QUAT_A.To(QUAT_B)), QUAT_B));
76 }
77
78 [Test]
79 public void FromQuatBToQuatA() {
80 Assert.That(AreQuaternionsEqual(QUAT_B.Then(QUAT_A.From(QUAT_B)), QUAT_A));
81 }
82
83 [Test]
84 public void ToQuatBFromQuatA() {
85 Assert.That(AreQuaternionsEqual(QUAT_B.Then(QUAT_B.To(QUAT_A)), QUAT_A));
86 }
87
88 private static bool AreQuaternionsEqual(Quaternion a, Quaternion b) {
89 return (a.ToAngleAxisVector() - b.ToAngleAxisVector()).magnitude < EPSILON;
90 }
91
92 #endregion
93
94 #region Pose
95
96 public static Pose POSE_A {
97 get { return new Pose(VEC_A, QUAT_A); }
98 }
99 public static Pose POSE_B {
100 get { return new Pose(VEC_B, QUAT_B); }
101 }
102
103 [Test]
104 public void FromPoseAToPoseB() {
105 Assert.That(ArePosesEqual(POSE_B.Then(POSE_A.From(POSE_B)), POSE_A));
106 }
107
108 [Test]
109 public void ToPoseAFromPoseB() {
110 Assert.That(ArePosesEqual(POSE_B.Then(POSE_B.To(POSE_A)), POSE_A));
111 }
112
113 [Test]
114 public void FromPoseBToPoseA() {
115 Assert.That(ArePosesEqual(POSE_A.Then(POSE_B.From(POSE_A)), POSE_B));
116 }
117
118 [Test]
119 public void ToPoseBFromPoseA() {
120 Assert.That(ArePosesEqual(POSE_A.Then(POSE_A.To(POSE_B)), POSE_B));
121 }
122
123 private bool ArePosesEqual(Pose a, Pose b) {
124 return AreVector3sEqual(a.position, b.position)
125 && AreQuaternionsEqual(a.rotation, b.rotation);
126 }
127
128 #endregion
129
130 #region Matrix4x4
131
132 private Matrix4x4 MAT_A {
133 get {
134 return Matrix4x4.TRS(Vector3.right * 100f,
135 Quaternion.AngleAxis(77f, Vector3.one),
136 Vector3.one * 35f);
137 }
138 }
139
140 private Matrix4x4 MAT_B {
141 get {
142 return Matrix4x4.TRS(Vector3.one * 20f,
143 Quaternion.AngleAxis(24f, Vector3.up),
144 Vector3.one * 2f);
145 }
146 }
147
148 [Test]
149 public void FromMatrixBToMatrixA() {
150 Assert.That(AreMatricesEqual(MAT_B.Then(MAT_A.From(MAT_B)), MAT_A));
151 }
152
153 [Test]
154 public void ToMatrixAFromMatrixB() {
155 Assert.That(AreMatricesEqual(MAT_B.Then(MAT_B.To(MAT_A)), MAT_A));
156 }
157
158 [Test]
159 public void FromMatrixAToMatrixB() {
160 Assert.That(AreMatricesEqual(MAT_A.Then(MAT_B.From(MAT_A)), MAT_B));
161 }
162
163 private static bool AreMatricesEqual(Matrix4x4 a, Matrix4x4 b) {
164 return AreVector3sEqual(a.GetVector3(), b.GetVector3())
165 && AreQuaternionsEqual(a.GetQuaternion(), b.GetQuaternion());
166 }
167
168 #endregion
169
170 }
171}
Tests for From(), To(), and Then() extension methods.
A position and rotation. You can multiply two poses; this acts like Matrix4x4 multiplication,...
Definition: Pose.cs:21
Quaternion rotation
Definition: Pose.cs:24
Vector3 position
Definition: Pose.cs:23