o0Project.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. public class Variance
  10. {
  11. List<float> _Value = new List<float>();
  12. int RecordCount;
  13. public Variance(int RecordCount = 100)
  14. {
  15. this.RecordCount = RecordCount;
  16. }
  17. public void Update(float value)
  18. {
  19. if (_Value.Count >= RecordCount)
  20. _Value.RemoveAt(0);
  21. _Value.Add(value);
  22. }
  23. public float Value
  24. {
  25. get
  26. {
  27. float average = 0;
  28. foreach (var i in _Value)
  29. average += i;
  30. average /= _Value.Count;
  31. float variance = 0;
  32. foreach (var i in _Value)
  33. variance += Mathf.Pow(average - i, 2);
  34. variance /= _Value.Count;
  35. return variance;
  36. }
  37. }
  38. public float StandardDeviation
  39. {
  40. get
  41. {
  42. return Mathf.Sqrt(Value);
  43. }
  44. }
  45. }
  46. public class Vector3f
  47. {
  48. public float x;
  49. public float y;
  50. public float z;
  51. public Vector3f() { }
  52. public Vector3f(Vector3f v)
  53. {
  54. x = v[0];
  55. y = v[1];
  56. z = v[2];
  57. }
  58. public Vector3f(float[] v)
  59. {
  60. x = v[0];
  61. y = v[1];
  62. z = v[2];
  63. }
  64. public Vector3f(float x = 0, float y = 0, float z = 0)
  65. {
  66. this.x = x;
  67. this.y = y;
  68. this.z = z;
  69. }
  70. public Vector3f assign(Vector3f v)
  71. {
  72. x = v.x;
  73. y = v.y;
  74. z = v.z;
  75. return this;
  76. }
  77. public Vector3f assign(float x, float y, float z)
  78. {
  79. this.x = x;
  80. this.y = y;
  81. this.z = z;
  82. return this;
  83. }
  84. public float length()//length of vector
  85. {
  86. return Mathf.Sqrt(x * x + y * y + z * z);
  87. }
  88. public Vector3f setNormalize()
  89. {
  90. return assign(this / length());
  91. }
  92. public Vector3f setVertical(Vector3f b)
  93. {
  94. return assign(this[1] * b[2] - this[2] * b[1],
  95. this[2] * b[0] - this[0] * b[2],
  96. this[0] * b[1] - this[1] * b[0]);
  97. }
  98. public float angle(Vector3f b)
  99. {
  100. return Mathf.Asin(Mathf.Sqrt(Mathf.Pow(this[0] - b[0], 2) + Mathf.Pow(this[1] - b[1], 2) + Mathf.Pow(this[2] - b[2], 2)) / 2) * 2;
  101. }
  102. public Vector3f setRotate(Vector3f shaft, float angle)
  103. {
  104. float cosAngle = Mathf.Cos(angle);
  105. float sinAngle = Mathf.Sin(angle);
  106. float rotateDate = (shaft[0] * this[0] + shaft[1] * this[1] + shaft[2] * this[2]) * (1 - cosAngle);
  107. Vector3f outerProduct = (new Vector3f(this)).setVertical(shaft);
  108. return assign(this[0] * cosAngle + outerProduct[0] * sinAngle + shaft[0] * rotateDate,
  109. this[1] * cosAngle + outerProduct[1] * sinAngle + shaft[1] * rotateDate,
  110. this[2] * cosAngle + outerProduct[2] * sinAngle + shaft[2] * rotateDate);
  111. }
  112. public float this[int index]
  113. {
  114. get
  115. {
  116. switch (index)
  117. {
  118. default:
  119. case 0:
  120. return x;
  121. case 1:
  122. return y;
  123. case 2:
  124. return z;
  125. }
  126. }
  127. set
  128. {
  129. switch (index)
  130. {
  131. default:
  132. case 0:
  133. x = value;
  134. break;
  135. case 1:
  136. y = value;
  137. break;
  138. case 2:
  139. z = value;
  140. break;
  141. }
  142. }
  143. }
  144. public static Vector3f operator +(Vector3f a, Vector3f b)
  145. {
  146. return new Vector3f(a.x + b.x, a.y + b.y, a.z + b.z);
  147. }
  148. public static Vector3f operator -(Vector3f a, Vector3f b)
  149. {
  150. return new Vector3f(a.x - b.x, a.y - b.y, a.z - b.z);
  151. }
  152. public static Vector3f operator *(Vector3f a, float b)
  153. {
  154. return new Vector3f(a.x * b, a.y * b, a.z * b);
  155. }
  156. public static Vector3f operator /(Vector3f a, float b)
  157. {
  158. return new Vector3f(a.x / b, a.y / b, a.z / b);
  159. }
  160. }
  161. static public class o0
  162. {
  163. public static Quaternion FormQuaternion(Vector3 Direction1InIdentity, Vector3 Direction2InIdentity, Vector3 Direction1InQuaternion, Vector3 Direction2InQuaternion,float rate)
  164. {
  165. var quaIdentity = Quaternion.LookRotation(Direction1InIdentity, Direction2InIdentity);
  166. var quaReal = Quaternion.LookRotation(Direction1InQuaternion, Direction2InQuaternion);
  167. var quaIdentity2 = Quaternion.LookRotation(Direction2InIdentity, Direction1InIdentity);
  168. var quaReal2 = Quaternion.LookRotation(Direction2InQuaternion, Direction1InQuaternion);
  169. return Quaternion.Slerp(quaIdentity * Quaternion.Inverse(quaReal), quaIdentity2 * Quaternion.Inverse(quaReal2), rate);
  170. }
  171. public static Quaternion FormQuaternion(Quaternion original, Vector3 DirectionIdentity, Vector3 Direction1New, float rate)
  172. {
  173. var global = original * Direction1New;
  174. Vector3 DirectionIdentityNormalized = DirectionIdentity.normalized;
  175. float angle = Vector3.Angle(global, DirectionIdentityNormalized);
  176. //Vector3 gVector = Vector3.RotateTowards(acceleratorGlobal, downVector, angle / 180 * Mathf.PI * Mathf.Pow(overallGVectorCredibility, 1f) * rotationUnCredibility, 1);//偏移量,从陀螺仪数据往加速计数据的偏移量
  177. Vector3 gVector = Vector3.RotateTowards(global, DirectionIdentityNormalized, angle / 180 * Mathf.PI * rate, 1);//偏移量,从陀螺仪数据往加速计数据的偏移量
  178. var newQua = new Quaternion();
  179. newQua.SetFromToRotation(original * Direction1New, gVector);
  180. return newQua * original;//一定要反过来乘
  181. }
  182. }
  183. static public class Extension
  184. {
  185. public static Quaternion Inverse(this Quaternion qua)
  186. {
  187. return new Quaternion(-qua.x,-qua.y,-qua.z,qua.w);
  188. }
  189. public static Quaternion ToQuaternion(this MatrixF2D m)
  190. {
  191. if(m.Width != 4 || m.Height!=4)
  192. throw new Exception("o0 FMatrix m is not 4*4 transfer matrix");
  193. Quaternion q = new Quaternion();
  194. q.w = Mathf.Sqrt(Mathf.Max(0, 1 + m[0, 0] + m[1, 1] + m[2, 2])) / 2;
  195. q.x = Mathf.Sqrt(Mathf.Max(0, 1 + m[0, 0] - m[1, 1] - m[2, 2])) / 2;
  196. q.y = Mathf.Sqrt(Mathf.Max(0, 1 - m[0, 0] + m[1, 1] - m[2, 2])) / 2;
  197. q.z = Mathf.Sqrt(Mathf.Max(0, 1 - m[0, 0] - m[1, 1] + m[2, 2])) / 2;
  198. q.x *= Mathf.Sign(q.x * (m[2, 1] - m[1, 2]));
  199. q.y *= Mathf.Sign(q.y * (m[0, 2] - m[2, 0]));
  200. q.z *= Mathf.Sign(q.z * (m[1, 0] - m[0, 1]));
  201. return q;
  202. }
  203. public static MatrixF2D ToMatrix(Vector3 right, Vector3 up, Vector3 forward, Vector3 position)
  204. {
  205. var m = MatrixF2D.Identity(4);
  206. m.SetColumn(0, new float[] { right.x,right.y,right.z,0});
  207. m.SetColumn(1, new float[] { up.x, up.y, up.z, 0 });
  208. m.SetColumn(2, new float[] { forward.x, forward.y, forward.z, 0 });
  209. m.SetColumn(3, new float[] { position.x, position.y, position.z, 0 });
  210. m[3, 3] = 1;
  211. return m;
  212. }
  213. public static MatrixF2D ToMatrix(Quaternion q)
  214. {
  215. return ToMatrix(q * Vector3.right, q * Vector3.up, q * Vector3.forward, Vector3.zero);
  216. }
  217. }
  218. public class MatrixF2D
  219. {
  220. private int _Width;
  221. public int Width { get { return _Width; } }
  222. public int Height { get { return _Element.Length / Width; } }
  223. private float[] _Element;
  224. public float[] Element { get { return _Element; } }
  225. public float this[int y, int x] {
  226. get {
  227. x %= Width;
  228. if (x < 0) x += Width;
  229. y %= Height;
  230. if (y < 0) y += Height;
  231. return _Element[x + y * Width];
  232. }
  233. set{
  234. x %= Width;
  235. if (x < 0) x += Width;
  236. y %= Height;
  237. if (y < 0) y += Height;
  238. _Element[x + y * Width] = value;
  239. }
  240. }
  241. public float[] GetRow(int y)
  242. {
  243. y %= Height;
  244. if (y < 0) y += Height;
  245. float[] l = new float[Width];
  246. for (var i = 0; i < Width; ++i)
  247. l[i] = this[y, i];
  248. return l;
  249. }
  250. public void SetRow(int y, IEnumerable<float> value)
  251. {
  252. if (value.Count() != Width)
  253. throw new Exception("o0 FMatrix width different");
  254. for (var i = 0; i < Width; ++i)
  255. this[y, i] = value.ElementAt(i);
  256. }
  257. public float[] GetColumn(int x)
  258. {
  259. x %= Width;
  260. if (x < 0) x += Width;
  261. float[] l = new float[Height];
  262. for (var i = 0; i < Height; ++i)
  263. l[i] = this[i, x];
  264. return l;
  265. }
  266. public void SetColumn(int x, IEnumerable<float> value)
  267. {
  268. if (value.Count() != Height)
  269. throw new Exception("o0 FMatrix width different");
  270. for (var i = 0; i < Height; ++i)
  271. this[i, x] = value.ElementAt(i);
  272. }
  273. public MatrixF2D(int width = 2, int height = 2)
  274. {
  275. _Width = width;
  276. _Element = new float[width * height];
  277. }
  278. public MatrixF2D(int width, float[] element)
  279. {
  280. _Width = width;
  281. _Element = new float[element.Length];
  282. for (var i = 0; i < element.Length; ++i)
  283. _Element[i] = element[i];
  284. }
  285. public MatrixF2D(MatrixF2D b)
  286. {
  287. _Width = b.Width;
  288. _Element = new float[b.Element.Length];
  289. for (var i = 0; i < _Element.Length; ++i)
  290. _Element[i] = b.Element[i];
  291. }
  292. public static MatrixF2D operator +(MatrixF2D a, MatrixF2D b)
  293. {
  294. if (a.Width != b.Width || a.Height != b.Height)
  295. throw new Exception("o0 FMatrix size different");
  296. var c = new MatrixF2D(a.Width, a.Height);
  297. for (var i = 0; i < a.Element.Length; ++i)
  298. c.Element[i] = a.Element[i] + b.Element[i];
  299. return c;
  300. }
  301. public static MatrixF2D operator -(MatrixF2D a, MatrixF2D b)
  302. {
  303. if (a.Width != b.Width || a.Height != b.Height)
  304. throw new Exception("o0 FMatrix size different");
  305. var c = new MatrixF2D(a.Width, a.Height);
  306. for (var i = 0; i < a.Element.Length; ++i)
  307. c.Element[i] = a.Element[i] - b.Element[i];
  308. return c;
  309. }
  310. public static MatrixF2D operator *(MatrixF2D a, MatrixF2D b)
  311. {
  312. if (a.Width != b.Height)
  313. throw new Exception("o0 FMatrix 当矩阵A的列数(column)等于矩阵B的行数(row)时,A与B可以相乘");
  314. var c = new MatrixF2D(b.Width,a.Height);
  315. for (var x = 0;x<c.Width;++x)
  316. for (var y = 0; y < c.Height; ++y)
  317. for (var z = 0; z < a.Width; ++z)
  318. {
  319. //c[x, y] += a[y, z] * b[z, x];
  320. c[y, x] += a[y,z] * b[z,x];
  321. }
  322. return c;
  323. }
  324. public static MatrixF2D operator *(MatrixF2D a, float b)
  325. {
  326. var c = new MatrixF2D(a);
  327. for (var x = 0; x < c.Width; ++x)
  328. for (var y = 0; y < c.Height; ++y)
  329. c[y,x] *= b;
  330. return c;
  331. }
  332. public static MatrixF2D operator *(float b, MatrixF2D a)
  333. {
  334. return a*b;
  335. }
  336. public static MatrixF2D operator /(MatrixF2D a, float b)
  337. {
  338. return a*(1/b);
  339. }
  340. public MatrixF2D T
  341. {
  342. get
  343. {
  344. var matrix = new MatrixF2D(Height, Width);
  345. for (var x = 0; x < Width; ++x)
  346. for (var y = 0; y < Height; ++y)
  347. matrix[x,y] = this[y,x];
  348. return matrix;
  349. }
  350. }
  351. public float trace//tr
  352. {
  353. get
  354. {
  355. if (Width != Height)
  356. throw new Exception("o0 FMatrix 非实对称矩阵");
  357. float x=0;
  358. for (var i = 0; i < Width; ++i)
  359. x += this[i, i];
  360. return x;
  361. }
  362. }
  363. public float det//行列式
  364. {
  365. get
  366. {
  367. if (Width != Height)
  368. throw new Exception("o0 FMatrix 非实对称矩阵");
  369. float d = 0;
  370. for (var x = 0; x < Width; ++x)
  371. {
  372. float m = 1;
  373. for (var y = 0; y < Width; ++y)
  374. m *= this[y,x + y];
  375. d += m;
  376. }
  377. for (var x = 0; x < Width; ++x)
  378. {
  379. float m = 1;
  380. for (var y = 0; y < Width; ++y)
  381. m *= this[y,x - y];
  382. d -= m;
  383. }
  384. return d;
  385. }
  386. }
  387. public MatrixF2D Inverse
  388. {
  389. get
  390. {
  391. if (Width != Height)//还需要额外判断条件
  392. throw new Exception("o0 FMatrix 非实对称矩阵");
  393. var n = Width;
  394. int i;
  395. int j;
  396. int k;
  397. var Metrix = new MatrixF2D(this);
  398. for (k = 0; k < n; k++)
  399. {
  400. for (i = 0; i < n; i++)
  401. {
  402. if (i != k) Metrix.Element[i * n + k] = -Metrix.Element[i * n + k] / Metrix.Element[k * n + k];
  403. }
  404. Metrix.Element[k * n + k] = 1 / Metrix.Element[k * n + k];
  405. for (i = 0; i < n; i++) {
  406. if (i != k) {
  407. for (j = 0; j < n; j++) {
  408. if (j != k) Metrix.Element[i * n + j] += Metrix.Element[k * n + j] * Metrix.Element[i * n + k];
  409. }
  410. }
  411. }
  412. for (j = 0; j < n; j++) { if (j != k) Metrix.Element[k * n + j] *= Metrix.Element[k * n + k]; }
  413. }
  414. return Metrix;
  415. }
  416. }
  417. public override string ToString()
  418. {
  419. var s = "";
  420. for (var y = 0; y < Height; ++y)
  421. {
  422. if (y == 0)
  423. s += "[";
  424. else
  425. s += ",\n";
  426. for (var x = 0; x < Width; ++x)
  427. {
  428. if (x == 0)
  429. s += "[";
  430. else
  431. s += ",\t";
  432. s += this[y,x].ToString("0.00000");
  433. }
  434. s += "]";
  435. }
  436. s += "]";
  437. return s;
  438. }
  439. public static MatrixF2D Identity(int size)
  440. {
  441. var m = new MatrixF2D(4,4);
  442. for (var i = 0; i < m.Width; ++i)
  443. m[i, i] = 1;
  444. return m;
  445. }
  446. }
  447. }