o0Project.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using UnityEngine;
  7. namespace o0Project
  8. {
  9. static public class o0
  10. {
  11. public static Quaternion FormQuaternion(Vector3 Direction1InIdentity, Vector3 Direction2InIdentity, Vector3 Direction1InQuaternion, Vector3 Direction2InQuaternion,float rate)
  12. {
  13. var quaIdentity = Quaternion.LookRotation(Direction1InIdentity, Direction2InIdentity);
  14. var quaReal = Quaternion.LookRotation(Direction1InQuaternion, Direction2InQuaternion);
  15. var quaIdentity2 = Quaternion.LookRotation(Direction2InIdentity, Direction1InIdentity);
  16. var quaReal2 = Quaternion.LookRotation(Direction2InQuaternion, Direction1InQuaternion);
  17. return Quaternion.Slerp(quaIdentity * Quaternion.Inverse(quaReal), quaIdentity2 * Quaternion.Inverse(quaReal2), rate);
  18. }
  19. public static Quaternion FormQuaternion(Quaternion original, Vector3 DirectionIdentity, Vector3 Direction1New, float rate)
  20. {
  21. var global = original * Direction1New;
  22. Vector3 DirectionIdentityNormalized = DirectionIdentity.normalized;
  23. float angle = Vector3.Angle(global, DirectionIdentityNormalized);
  24. //Vector3 gVector = Vector3.RotateTowards(acceleratorGlobal, downVector, angle / 180 * Mathf.PI * Mathf.Pow(overallGVectorCredibility, 1f) * rotationUnCredibility, 1);//偏移量,从陀螺仪数据往加速计数据的偏移量
  25. Vector3 gVector = Vector3.RotateTowards(global, DirectionIdentityNormalized, angle / 180 * Mathf.PI * rate, 1);//偏移量,从陀螺仪数据往加速计数据的偏移量
  26. var newQua = new Quaternion();
  27. newQua.SetFromToRotation(original * Direction1New, gVector);
  28. return newQua * original;//一定要反过来乘
  29. }
  30. }
  31. static public class Extension
  32. {
  33. public static Quaternion Inverse(this Quaternion qua)
  34. {
  35. return new Quaternion(-qua.x,-qua.y,-qua.z,qua.w);
  36. }
  37. public static Quaternion ToQuaternion(this MatrixF2D m)
  38. {
  39. if(m.Width != 4 || m.Height!=4)
  40. throw new Exception("o0 FMatrix m is not 4*4 transfer matrix");
  41. Quaternion q = new Quaternion();
  42. q.w = Mathf.Sqrt(Mathf.Max(0, 1 + m[0, 0] + m[1, 1] + m[2, 2])) / 2;
  43. q.x = Mathf.Sqrt(Mathf.Max(0, 1 + m[0, 0] - m[1, 1] - m[2, 2])) / 2;
  44. q.y = Mathf.Sqrt(Mathf.Max(0, 1 - m[0, 0] + m[1, 1] - m[2, 2])) / 2;
  45. q.z = Mathf.Sqrt(Mathf.Max(0, 1 - m[0, 0] - m[1, 1] + m[2, 2])) / 2;
  46. q.x *= Mathf.Sign(q.x * (m[2, 1] - m[1, 2]));
  47. q.y *= Mathf.Sign(q.y * (m[0, 2] - m[2, 0]));
  48. q.z *= Mathf.Sign(q.z * (m[1, 0] - m[0, 1]));
  49. return q;
  50. }
  51. public static MatrixF2D ToMatrix(Vector3 right, Vector3 up, Vector3 forward, Vector3 position)
  52. {
  53. var m = MatrixF2D.Identity(4);
  54. m.SetColumn(0, new float[] { right.x,right.y,right.z,0});
  55. m.SetColumn(1, new float[] { up.x, up.y, up.z, 0 });
  56. m.SetColumn(2, new float[] { forward.x, forward.y, forward.z, 0 });
  57. m.SetColumn(3, new float[] { position.x, position.y, position.z, 0 });
  58. m[3, 3] = 1;
  59. return m;
  60. }
  61. public static MatrixF2D ToMatrix(Quaternion q)
  62. {
  63. return ToMatrix(q * Vector3.right, q * Vector3.up, q * Vector3.forward, Vector3.zero);
  64. }
  65. }
  66. public class MatrixF2D
  67. {
  68. private int _Width;
  69. public int Width { get { return _Width; } }
  70. public int Height { get { return _Element.Length / Width; } }
  71. private float[] _Element;
  72. public float[] Element { get { return _Element; } }
  73. public float this[int y, int x] {
  74. get {
  75. x %= Width;
  76. if (x < 0) x += Width;
  77. y %= Height;
  78. if (y < 0) y += Height;
  79. return _Element[x + y * Width];
  80. }
  81. set{
  82. x %= Width;
  83. if (x < 0) x += Width;
  84. y %= Height;
  85. if (y < 0) y += Height;
  86. _Element[x + y * Width] = value;
  87. }
  88. }
  89. public float[] GetRow(int y)
  90. {
  91. y %= Height;
  92. if (y < 0) y += Height;
  93. float[] l = new float[Width];
  94. for (var i = 0; i < Width; ++i)
  95. l[i] = this[y, i];
  96. return l;
  97. }
  98. public void SetRow(int y, IEnumerable<float> value)
  99. {
  100. if (value.Count() != Width)
  101. throw new Exception("o0 FMatrix width different");
  102. for (var i = 0; i < Width; ++i)
  103. this[y, i] = value.ElementAt(i);
  104. }
  105. public float[] GetColumn(int x)
  106. {
  107. x %= Width;
  108. if (x < 0) x += Width;
  109. float[] l = new float[Height];
  110. for (var i = 0; i < Height; ++i)
  111. l[i] = this[i, x];
  112. return l;
  113. }
  114. public void SetColumn(int x, IEnumerable<float> value)
  115. {
  116. if (value.Count() != Height)
  117. throw new Exception("o0 FMatrix width different");
  118. for (var i = 0; i < Height; ++i)
  119. this[i, x] = value.ElementAt(i);
  120. }
  121. public MatrixF2D(int width = 2, int height = 2)
  122. {
  123. _Width = width;
  124. _Element = new float[width * height];
  125. }
  126. public MatrixF2D(int width, float[] element)
  127. {
  128. _Width = width;
  129. _Element = new float[element.Length];
  130. for (var i = 0; i < element.Length; ++i)
  131. _Element[i] = element[i];
  132. }
  133. public MatrixF2D(MatrixF2D b)
  134. {
  135. _Width = b.Width;
  136. _Element = new float[b.Element.Length];
  137. for (var i = 0; i < _Element.Length; ++i)
  138. _Element[i] = b.Element[i];
  139. }
  140. public static MatrixF2D operator +(MatrixF2D a, MatrixF2D b)
  141. {
  142. if (a.Width != b.Width || a.Height != b.Height)
  143. throw new Exception("o0 FMatrix size different");
  144. var c = new MatrixF2D(a.Width, a.Height);
  145. for (var i = 0; i < a.Element.Length; ++i)
  146. c.Element[i] = a.Element[i] + b.Element[i];
  147. return c;
  148. }
  149. public static MatrixF2D operator -(MatrixF2D a, MatrixF2D b)
  150. {
  151. if (a.Width != b.Width || a.Height != b.Height)
  152. throw new Exception("o0 FMatrix size different");
  153. var c = new MatrixF2D(a.Width, a.Height);
  154. for (var i = 0; i < a.Element.Length; ++i)
  155. c.Element[i] = a.Element[i] - b.Element[i];
  156. return c;
  157. }
  158. public static MatrixF2D operator *(MatrixF2D a, MatrixF2D b)
  159. {
  160. if (a.Width != b.Height)
  161. throw new Exception("o0 FMatrix 当矩阵A的列数(column)等于矩阵B的行数(row)时,A与B可以相乘");
  162. var c = new MatrixF2D(b.Width,a.Height);
  163. for (var x = 0;x<c.Width;++x)
  164. for (var y = 0; y < c.Height; ++y)
  165. for (var z = 0; z < a.Width; ++z)
  166. {
  167. //c[x, y] += a[y, z] * b[z, x];
  168. c[y, x] += a[y,z] * b[z,x];
  169. }
  170. return c;
  171. }
  172. public static MatrixF2D operator *(MatrixF2D a, float b)
  173. {
  174. var c = new MatrixF2D(a);
  175. for (var x = 0; x < c.Width; ++x)
  176. for (var y = 0; y < c.Height; ++y)
  177. c[y,x] *= b;
  178. return c;
  179. }
  180. public static MatrixF2D operator *(float b, MatrixF2D a)
  181. {
  182. return a*b;
  183. }
  184. public static MatrixF2D operator /(MatrixF2D a, float b)
  185. {
  186. return a*(1/b);
  187. }
  188. public MatrixF2D T
  189. {
  190. get
  191. {
  192. var matrix = new MatrixF2D(Height, Width);
  193. for (var x = 0; x < Width; ++x)
  194. for (var y = 0; y < Height; ++y)
  195. matrix[x,y] = this[y,x];
  196. return matrix;
  197. }
  198. }
  199. public float trace//tr
  200. {
  201. get
  202. {
  203. if (Width != Height)
  204. throw new Exception("o0 FMatrix 非实对称矩阵");
  205. float x=0;
  206. for (var i = 0; i < Width; ++i)
  207. x += this[i, i];
  208. return x;
  209. }
  210. }
  211. public float det//行列式
  212. {
  213. get
  214. {
  215. if (Width != Height)
  216. throw new Exception("o0 FMatrix 非实对称矩阵");
  217. float d = 0;
  218. for (var x = 0; x < Width; ++x)
  219. {
  220. float m = 1;
  221. for (var y = 0; y < Width; ++y)
  222. m *= this[y,x + y];
  223. d += m;
  224. }
  225. for (var x = 0; x < Width; ++x)
  226. {
  227. float m = 1;
  228. for (var y = 0; y < Width; ++y)
  229. m *= this[y,x - y];
  230. d -= m;
  231. }
  232. return d;
  233. }
  234. }
  235. public MatrixF2D Inverse
  236. {
  237. get
  238. {
  239. if (Width != Height)//还需要额外判断条件
  240. throw new Exception("o0 FMatrix 非实对称矩阵");
  241. var n = Width;
  242. int i;
  243. int j;
  244. int k;
  245. var Metrix = new MatrixF2D(this);
  246. for (k = 0; k < n; k++)
  247. {
  248. for (i = 0; i < n; i++)
  249. {
  250. if (i != k) Metrix.Element[i * n + k] = -Metrix.Element[i * n + k] / Metrix.Element[k * n + k];
  251. }
  252. Metrix.Element[k * n + k] = 1 / Metrix.Element[k * n + k];
  253. for (i = 0; i < n; i++) {
  254. if (i != k) {
  255. for (j = 0; j < n; j++) {
  256. if (j != k) Metrix.Element[i * n + j] += Metrix.Element[k * n + j] * Metrix.Element[i * n + k];
  257. }
  258. }
  259. }
  260. for (j = 0; j < n; j++) { if (j != k) Metrix.Element[k * n + j] *= Metrix.Element[k * n + k]; }
  261. }
  262. return Metrix;
  263. }
  264. }
  265. public override string ToString()
  266. {
  267. var s = "";
  268. for (var y = 0; y < Height; ++y)
  269. {
  270. if (y == 0)
  271. s += "[";
  272. else
  273. s += ",\n";
  274. for (var x = 0; x < Width; ++x)
  275. {
  276. if (x == 0)
  277. s += "[";
  278. else
  279. s += ",\t";
  280. s += this[y,x].ToString("0.00000");
  281. }
  282. s += "]";
  283. }
  284. s += "]";
  285. return s;
  286. }
  287. public static MatrixF2D Identity(int size)
  288. {
  289. var m = new MatrixF2D(4,4);
  290. for (var i = 0; i < m.Width; ++i)
  291. m[i, i] = 1;
  292. return m;
  293. }
  294. }
  295. }