comparison Orchestland/Assets/LeapMotion/Scripts/Utils/LeapUnityExtensions.cs @ 1:f7675884f2a1

Add Orchestland project
author Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
date Fri, 17 Jul 2015 23:09:20 +0900
parents
children
comparison
equal deleted inserted replaced
0:347d21cdfc22 1:f7675884f2a1
1 /******************************************************************************\
2 * Copyright (C) Leap Motion, Inc. 2011-2014. *
3 * Leap Motion proprietary. Licensed under Apache 2.0 *
4 * Available at http://www.apache.org/licenses/LICENSE-2.0.html *
5 \******************************************************************************/
6
7 using UnityEngine;
8 using System.Collections;
9 using Leap;
10
11 namespace Leap {
12
13 /**
14 * Extends the Leap Motion Vector class to converting points and directions from the
15 * Leap Motion coordinate system into the Unity coordinate system.
16 */
17 public static class UnityVectorExtension {
18
19 // Leap coordinates are in mm and Unity is in meters. So scale by 1000.
20 /** Scale factor from Leap units (millimeters) to Unity units (meters). */
21 public const float INPUT_SCALE = 0.001f;
22 /** Constant used when converting from right-handed to left-handed axes.*/
23 public static readonly Vector3 Z_FLIP = new Vector3(1, 1, -1);
24
25 /**
26 * Converts a direction vector from Leap to Unity. (Does not scale.)
27 *
28 * Changes from the Leap Motion right-hand coordinate convention to the
29 * Unity left-handed convention by negating the z-coordinate.
30 *
31 * @param mirror If true, the vector is reflected along the z axis.
32 * @param leap_vector the Leap.Vector object to convert.
33 */
34 public static Vector3 ToUnity(this Vector leap_vector, bool mirror = false) {
35 if (mirror)
36 return ToVector3(leap_vector);
37
38 return FlipZ(ToVector3(leap_vector));
39 }
40
41 /**
42 * Converts a point from Leap to Unity. (Scales.)
43 *
44 * Changes from the Leap Motion right-hand coordinate convention to the
45 * Unity left-handed convention by negating the z-coordinate. Also scales
46 * from Leap Motion millimeter units to Unity meter units by multiplying
47 * the vector by .001.
48 *
49 * @param mirror If true, the vector is reflected along the z axis.
50 * @param leap_vector the Leap.Vector object to convert.
51 */
52 public static Vector3 ToUnityScaled(this Vector leap_vector, bool mirror = false) {
53 if (mirror)
54 return INPUT_SCALE * ToVector3(leap_vector);
55
56 return INPUT_SCALE * FlipZ(ToVector3(leap_vector));
57 }
58
59 private static Vector3 FlipZ(Vector3 vector) {
60 return Vector3.Scale(vector, Z_FLIP);
61 }
62
63 private static Vector3 ToVector3(Vector vector) {
64 return new Vector3(vector.x, vector.y, vector.z);
65 }
66 }
67
68 /**
69 * Extends the Leap Mition Matrix class to convert Leap Matrix objects to
70 * to Unity Quaternion rotations and translations.
71 */
72 public static class UnityMatrixExtension {
73 /** Up in the Leap coordinate system.*/
74 public static readonly Vector LEAP_UP = new Vector(0, 1, 0);
75 /** Forward in the Leap coordinate system.*/
76 public static readonly Vector LEAP_FORWARD = new Vector(0, 0, -1);
77 /** The origin point in the Leap coordinate system.*/
78 public static readonly Vector LEAP_ORIGIN = new Vector(0, 0, 0);
79
80 /**
81 * Converts a Leap Matrix object representing a rotation to a
82 * Unity Quaternion.
83 *
84 * @param matrix The Leap.Matrix to convert.
85 * @param mirror If true, the operation is reflected along the z axis.
86 */
87 public static Quaternion Rotation(this Matrix matrix, bool mirror = false) {
88 Vector3 up = matrix.TransformDirection(LEAP_UP).ToUnity(mirror);
89 Vector3 forward = matrix.TransformDirection(LEAP_FORWARD).ToUnity(mirror);
90 return Quaternion.LookRotation(forward, up);
91 }
92
93 /**
94 * Converts a Leap Matrix object representing a translation to a
95 * Unity Vector3 object.
96 *
97 * @param matrix The Leap.Matrix to convert.
98 * @param mirror If true, the operation is reflected along the z axis.
99 */
100 public static Vector3 Translation(this Matrix matrix, bool mirror = false) {
101 return matrix.TransformPoint(LEAP_ORIGIN).ToUnityScaled(mirror);
102 }
103 }
104 }