Quaternion.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /* The MIT License
  2. *
  3. * Copyright (c) 2010 Intel Corporation.
  4. * All rights reserved.
  5. *
  6. * Based on the convexdecomposition library from
  7. * <http://codesuppository.googlecode.com> by John W. Ratcliff and Stan Melax.
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. using System;
  28. namespace OpenSim.Region.PhysicsModules.ConvexDecompositionDotNet
  29. {
  30. public class Quaternion : float4
  31. {
  32. public Quaternion()
  33. {
  34. x = y = z = 0.0f;
  35. w = 1.0f;
  36. }
  37. public Quaternion(float3 v, float t)
  38. {
  39. v = float3.normalize(v);
  40. w = (float)Math.Cos(t / 2.0f);
  41. v = v * (float)Math.Sin(t / 2.0f);
  42. x = v.x;
  43. y = v.y;
  44. z = v.z;
  45. }
  46. public Quaternion(float _x, float _y, float _z, float _w)
  47. {
  48. x = _x;
  49. y = _y;
  50. z = _z;
  51. w = _w;
  52. }
  53. public float angle()
  54. {
  55. return (float)Math.Acos(w) * 2.0f;
  56. }
  57. public float3 axis()
  58. {
  59. float3 a = new float3(x, y, z);
  60. if (Math.Abs(angle()) < 0.0000001f)
  61. return new float3(1f, 0f, 0f);
  62. return a * (1 / (float)Math.Sin(angle() / 2.0f));
  63. }
  64. public float3 xdir()
  65. {
  66. return new float3(1 - 2 * (y * y + z * z), 2 * (x * y + w * z), 2 * (x * z - w * y));
  67. }
  68. public float3 ydir()
  69. {
  70. return new float3(2 * (x * y - w * z), 1 - 2 * (x * x + z * z), 2 * (y * z + w * x));
  71. }
  72. public float3 zdir()
  73. {
  74. return new float3(2 * (x * z + w * y), 2 * (y * z - w * x), 1 - 2 * (x * x + y * y));
  75. }
  76. public float3x3 getmatrix()
  77. {
  78. return new float3x3(xdir(), ydir(), zdir());
  79. }
  80. public static implicit operator float3x3(Quaternion q)
  81. {
  82. return q.getmatrix();
  83. }
  84. public static Quaternion operator *(Quaternion a, Quaternion b)
  85. {
  86. Quaternion c = new Quaternion();
  87. c.w = a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z;
  88. c.x = a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y;
  89. c.y = a.w * b.y - a.x * b.z + a.y * b.w + a.z * b.x;
  90. c.z = a.w * b.z + a.x * b.y - a.y * b.x + a.z * b.w;
  91. return c;
  92. }
  93. public static float3 operator *(Quaternion q, float3 v)
  94. {
  95. // The following is equivalent to:
  96. //return (q.getmatrix() * v);
  97. float qx2 = q.x * q.x;
  98. float qy2 = q.y * q.y;
  99. float qz2 = q.z * q.z;
  100. float qxqy = q.x * q.y;
  101. float qxqz = q.x * q.z;
  102. float qxqw = q.x * q.w;
  103. float qyqz = q.y * q.z;
  104. float qyqw = q.y * q.w;
  105. float qzqw = q.z * q.w;
  106. return new float3((1 - 2 * (qy2 + qz2)) * v.x + (2 * (qxqy - qzqw)) * v.y + (2 * (qxqz + qyqw)) * v.z, (2 * (qxqy + qzqw)) * v.x + (1 - 2 * (qx2 + qz2)) * v.y + (2 * (qyqz - qxqw)) * v.z, (2 * (qxqz - qyqw)) * v.x + (2 * (qyqz + qxqw)) * v.y + (1 - 2 * (qx2 + qy2)) * v.z);
  107. }
  108. public static Quaternion operator +(Quaternion a, Quaternion b)
  109. {
  110. return new Quaternion(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
  111. }
  112. public static Quaternion operator *(Quaternion a, float b)
  113. {
  114. return new Quaternion(a.x *b, a.y *b, a.z *b, a.w *b);
  115. }
  116. public static Quaternion normalize(Quaternion a)
  117. {
  118. float m = (float)Math.Sqrt(a.w * a.w + a.x * a.x + a.y * a.y + a.z * a.z);
  119. if (m < 0.000000001f)
  120. {
  121. a.w = 1;
  122. a.x = a.y = a.z = 0;
  123. return a;
  124. }
  125. return a * (1f / m);
  126. }
  127. public static float dot(Quaternion a, Quaternion b)
  128. {
  129. return (a.w * b.w + a.x * b.x + a.y * b.y + a.z * b.z);
  130. }
  131. public static Quaternion slerp(Quaternion q1, Quaternion q2, float amount)
  132. {
  133. float angle = (q1.x * q2.x) + (q1.y * q2.y) + (q1.z * q2.z) + (q1.w * q2.w);
  134. if (angle < 0f)
  135. {
  136. q1.x = -q1.x;
  137. q1.y = -q1.y;
  138. q1.z = -q1.z;
  139. q1.w = -q1.w;
  140. angle = -angle;
  141. }
  142. float scale;
  143. float invscale;
  144. if ((angle + 1.0) > 0.0005)
  145. {
  146. if ((1f - angle) >= 0.0005)
  147. {
  148. // slerp
  149. float theta = (float)Math.Acos(angle);
  150. float invsintheta = 1.0f / (float)Math.Sin(theta);
  151. scale = (float)Math.Sin(theta * (1.0 - amount)) * invsintheta;
  152. invscale = (float)Math.Sin(theta * amount) * invsintheta;
  153. }
  154. else
  155. {
  156. // lerp
  157. scale = 1.0f - amount;
  158. invscale = amount;
  159. }
  160. }
  161. else
  162. {
  163. q2.x = -q1.y;
  164. q2.y = q1.x;
  165. q2.z = -q1.w;
  166. q2.w = q1.z;
  167. scale = (float)Math.Sin(Math.PI * (0.5f - amount));
  168. invscale = (float)Math.Sin(Math.PI * amount);
  169. }
  170. return new Quaternion(
  171. q1.x * scale + q2.x * invscale,
  172. q1.y * scale + q2.y * invscale,
  173. q1.z * scale + q2.z * invscale,
  174. q1.w * scale + q2.w * invscale
  175. );
  176. }
  177. public static Quaternion Interpolate(Quaternion q0, Quaternion q1, float alpha)
  178. {
  179. return slerp(q0, q1, alpha);
  180. }
  181. public static Quaternion Inverse(Quaternion q)
  182. {
  183. return new Quaternion(-q.x, -q.y, -q.z, q.w);
  184. }
  185. public static Quaternion YawPitchRoll(float yaw, float pitch, float roll)
  186. {
  187. roll *= (3.14159264f / 180.0f);
  188. yaw *= (3.14159264f / 180.0f);
  189. pitch *= (3.14159264f / 180.0f);
  190. return new Quaternion(new float3(0.0f, 0.0f, 1.0f), yaw) * new Quaternion(new float3(1.0f, 0.0f, 0.0f), pitch) * new Quaternion(new float3(0.0f, 1.0f, 0.0f), roll);
  191. }
  192. public static float Yaw(Quaternion q)
  193. {
  194. float3 v = q.ydir();
  195. return (v.y == 0.0 && v.x == 0.0) ? 0.0f : (float)Math.Atan2(-v.x, v.y) * (180.0f / 3.14159264f);
  196. }
  197. public static float Pitch(Quaternion q)
  198. {
  199. float3 v = q.ydir();
  200. return (float)Math.Atan2(v.z, Math.Sqrt(v.x * v.x + v.y * v.y)) * (180.0f / 3.14159264f);
  201. }
  202. public static float Roll(Quaternion q)
  203. {
  204. q = new Quaternion(new float3(0.0f, 0.0f, 1.0f), -Yaw(q) * (3.14159264f / 180.0f)) * q;
  205. q = new Quaternion(new float3(1.0f, 0.0f, 0.0f), -Pitch(q) * (3.14159264f / 180.0f)) * q;
  206. return (float)Math.Atan2(-q.xdir().z, q.xdir().x) * (180.0f / 3.14159264f);
  207. }
  208. }
  209. }