2021년 4학년 1학기 기업연계프로젝트2 컴퓨터소프트웨어공학과 <원광투어팀> 팀장 : 송유진 팀원 : 김나영, 이경희, 한유진
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

217 lines
6.2 KiB

5 years ago
  1. //========= Copyright 2016-2020, HTC Corporation. All rights reserved. ===========
  2. using HTC.UnityPlugin.Utility;
  3. using System;
  4. using UnityEngine;
  5. namespace HTC.UnityPlugin.PoseTracker
  6. {
  7. /// <summary>
  8. /// Describe a pose by position and rotation
  9. /// </summary>
  10. [Obsolete("Use HTC.UnityPlugin.Utility.RigidPose instead")]
  11. [Serializable]
  12. public struct Pose
  13. {
  14. public Vector3 pos;
  15. public Quaternion rot;
  16. public static Pose identity
  17. {
  18. get { return new Pose(Vector3.zero, Quaternion.identity); }
  19. }
  20. public Pose(Vector3 pos, Quaternion rot)
  21. {
  22. this.pos = pos;
  23. this.rot = rot;
  24. }
  25. public Pose(Transform t, bool useLocal = false)
  26. {
  27. if (t == null)
  28. {
  29. pos = Vector3.zero;
  30. rot = Quaternion.identity;
  31. }
  32. else if (!useLocal)
  33. {
  34. pos = t.position;
  35. rot = t.rotation;
  36. }
  37. else
  38. {
  39. pos = t.localPosition;
  40. rot = t.localRotation;
  41. }
  42. }
  43. public override bool Equals(object o)
  44. {
  45. if (o is Pose)
  46. {
  47. Pose t = (Pose)o;
  48. return pos == t.pos && rot == t.rot;
  49. }
  50. return false;
  51. }
  52. public override int GetHashCode()
  53. {
  54. return pos.GetHashCode() ^ rot.GetHashCode();
  55. }
  56. public static bool operator ==(Pose a, Pose b)
  57. {
  58. return
  59. a.pos.x == b.pos.x &&
  60. a.pos.y == b.pos.y &&
  61. a.pos.z == b.pos.z &&
  62. a.rot.x == b.rot.x &&
  63. a.rot.y == b.rot.y &&
  64. a.rot.z == b.rot.z &&
  65. a.rot.w == b.rot.w;
  66. }
  67. public static bool operator !=(Pose a, Pose b)
  68. {
  69. return !(a == b);
  70. }
  71. public static Pose operator *(Pose a, Pose b)
  72. {
  73. return new Pose
  74. {
  75. rot = a.rot * b.rot,
  76. pos = a.pos + a.rot * b.pos
  77. };
  78. }
  79. public void Multiply(Pose a, Pose b)
  80. {
  81. rot = a.rot * b.rot;
  82. pos = a.pos + a.rot * b.pos;
  83. }
  84. public void Inverse()
  85. {
  86. rot = Quaternion.Inverse(rot);
  87. pos = -(rot * pos);
  88. }
  89. public Pose GetInverse()
  90. {
  91. var t = new Pose(pos, rot);
  92. t.Inverse();
  93. return t;
  94. }
  95. public Vector3 InverseTransformPoint(Vector3 point)
  96. {
  97. return Quaternion.Inverse(rot) * (point - pos);
  98. }
  99. public Vector3 TransformPoint(Vector3 point)
  100. {
  101. return pos + (rot * point);
  102. }
  103. public static Pose Lerp(Pose a, Pose b, float t)
  104. {
  105. return new Pose(Vector3.Lerp(a.pos, b.pos, t), Quaternion.Slerp(a.rot, b.rot, t));
  106. }
  107. public void Lerp(Pose to, float t)
  108. {
  109. pos = Vector3.Lerp(pos, to.pos, t);
  110. rot = Quaternion.Slerp(rot, to.rot, t);
  111. }
  112. public static Pose LerpUnclamped(Pose a, Pose b, float t)
  113. {
  114. return new Pose(Vector3.LerpUnclamped(a.pos, b.pos, t), Quaternion.SlerpUnclamped(a.rot, b.rot, t));
  115. }
  116. public void LerpUnclamped(Pose to, float t)
  117. {
  118. pos = Vector3.LerpUnclamped(pos, to.pos, t);
  119. rot = Quaternion.SlerpUnclamped(rot, to.rot, t);
  120. }
  121. public static void SetPose(Transform target, Pose pose, Transform origin = null)
  122. {
  123. if (origin != null && origin != target.parent)
  124. {
  125. pose = new Pose(origin) * pose;
  126. pose.pos.Scale(origin.localScale);
  127. target.position = pose.pos;
  128. target.rotation = pose.rot;
  129. }
  130. else
  131. {
  132. target.localPosition = pose.pos;
  133. target.localRotation = pose.rot;
  134. }
  135. }
  136. // proper following duration is larger then 0.02 second, depends on the update rate
  137. public static void SetRigidbodyVelocity(Rigidbody rigidbody, Vector3 from, Vector3 to, float duration)
  138. {
  139. var diffPos = to - from;
  140. if (Mathf.Approximately(diffPos.sqrMagnitude, 0f))
  141. {
  142. rigidbody.velocity = Vector3.zero;
  143. }
  144. else
  145. {
  146. rigidbody.velocity = diffPos / duration;
  147. }
  148. }
  149. // proper folloing duration is larger then 0.02 second, depends on the update rate
  150. public static void SetRigidbodyAngularVelocity(Rigidbody rigidbody, Quaternion from, Quaternion to, float duration, bool overrideMaxAngularVelocity = true)
  151. {
  152. float angle;
  153. Vector3 axis;
  154. var fromToRot = to * Quaternion.Inverse(from);
  155. fromToRot.ToAngleAxis(out angle, out axis);
  156. while (angle > 180f) { angle -= 360f; }
  157. if (Mathf.Approximately(angle, 0f) || float.IsNaN(axis.x) || float.IsNaN(axis.y) || float.IsNaN(axis.z))
  158. {
  159. rigidbody.angularVelocity = Vector3.zero;
  160. }
  161. else
  162. {
  163. angle *= Mathf.Deg2Rad / duration; // convert to radius speed
  164. if (overrideMaxAngularVelocity && rigidbody.maxAngularVelocity < angle) { rigidbody.maxAngularVelocity = angle; }
  165. rigidbody.angularVelocity = axis * angle;
  166. }
  167. }
  168. public static Pose FromToPose(Pose from, Pose to)
  169. {
  170. var invRot = Quaternion.Inverse(from.rot);
  171. return new Pose(invRot * (to.pos - from.pos), invRot * to.rot);
  172. }
  173. public static implicit operator RigidPose(Pose v)
  174. {
  175. return new RigidPose(v.pos, v.rot);
  176. }
  177. public static implicit operator Pose(RigidPose v)
  178. {
  179. return new Pose(v.pos, v.rot);
  180. }
  181. public override string ToString()
  182. {
  183. return "p" + pos.ToString() + "r" + rot.ToString();
  184. }
  185. public string ToString(string format)
  186. {
  187. return "p" + pos.ToString(format) + "r" + rot.ToString(format);
  188. }
  189. }
  190. }