o0Project.cs 15 KB

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