o09Axis.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. using MathNet.Numerics.LinearAlgebra;
  7. using UnityEngine.UI;
  8. using MathNet.Numerics;
  9. using System.Runtime.InteropServices;
  10. public class o0Vector3Filter
  11. {
  12. Vector3 state = default;
  13. float Variance = 1;
  14. public Vector3 Update(Vector3 v)
  15. {
  16. if (state == default)
  17. return state = v;
  18. Variance += 10;
  19. float mVariance = 1;
  20. state = Vector3.Lerp(state, v, mVariance/ (Variance + mVariance));
  21. Variance = Variance * mVariance / (Variance + mVariance);
  22. return state;
  23. }
  24. }
  25. public class o0MagneticCalibraterEllipsoidFitting//默认在无磁干扰环境下,有磁干扰则无法保证效果
  26. {
  27. [JsonIgnore]
  28. public Vector3 _Center = Vector3.zero;
  29. [JsonIgnore]
  30. Matrix<double> _CorrectMatrix = null;
  31. public float[] Center
  32. {
  33. get
  34. {
  35. return new float[]{_Center.x, _Center.y, _Center.z};
  36. }
  37. set
  38. {
  39. _Center = new Vector3(value[0], value[1], value[2]);
  40. }
  41. }
  42. public double[] CorrectMatrix
  43. {
  44. get
  45. {
  46. if (_CorrectMatrix == null)
  47. return default;
  48. var m = new double[9];
  49. for (var i = 0; i < 3; ++i)
  50. for (var j = 0; j < 3; ++j)
  51. m[j + i * 3] = _CorrectMatrix[i,j];
  52. return m;
  53. }
  54. set
  55. {
  56. if (value == default)
  57. {
  58. _CorrectMatrix = null;
  59. return;
  60. }
  61. _CorrectMatrix = CreateMatrix.Dense<double>(3,3);
  62. for (var i = 0; i < 3; ++i)
  63. for (var j = 0; j < 3; ++j)
  64. _CorrectMatrix[i, j] = value[j + i * 3];
  65. }
  66. }
  67. public o0MagneticCalibraterEllipsoidFitting()
  68. {
  69. //Calibration = true;
  70. }
  71. // public o0MagneticCalibraterEllipsoidFitting(o0Project.Vector3f Center, double[] CorrectMatrix)
  72. public o0MagneticCalibraterEllipsoidFitting(float[] Center, double[] CorrectMatrix)
  73. {
  74. this.Center = Center;
  75. this.CorrectMatrix = CorrectMatrix;
  76. }
  77. [JsonIgnore]
  78. public List<Vector3> records = null;
  79. [JsonIgnore]
  80. public Vector3 _Radius = default;
  81. public float[] Radius
  82. {
  83. get
  84. {
  85. return new float[]{_Radius.x, _Radius.y, _Radius.z};
  86. }
  87. set
  88. {
  89. _Radius = new Vector3(value[0], value[1], value[2]);
  90. }
  91. }
  92. public List<Vector3> getRecords() {
  93. //Debug.LogWarning(records);
  94. return records;
  95. }
  96. [JsonIgnore]
  97. List<Vector3> BadRecords = null;
  98. [JsonIgnore]
  99. public bool Calibration
  100. {
  101. get
  102. {
  103. return records != null;
  104. }
  105. set
  106. {
  107. if (value == true)
  108. {
  109. records = new List<Vector3>();
  110. }
  111. else
  112. {
  113. try
  114. {
  115. int mag_data_counter = records.Count; //mag数据数量
  116. double mag_x, mag_y, mag_z;
  117. var mat_D = CreateMatrix.Dense<double>(mag_data_counter, 9);
  118. //读取mag
  119. for (int i = 0; i < mag_data_counter; i++)
  120. {
  121. //mag_x_y_z赋值
  122. mag_x = records[i].x;
  123. mag_y = records[i].y;
  124. mag_z = records[i].z;
  125. mat_D[i, 0] = mag_x * mag_x;
  126. mat_D[i, 1] = mag_y * mag_y;
  127. mat_D[i, 2] = mag_z * mag_z;
  128. mat_D[i, 3] = 2 * mag_x * mag_y;
  129. mat_D[i, 4] = 2 * mag_x * mag_z;
  130. mat_D[i, 5] = 2 * mag_y * mag_z;
  131. mat_D[i, 6] = 2 * mag_x;
  132. mat_D[i, 7] = 2 * mag_y;
  133. mat_D[i, 8] = 2 * mag_z;
  134. }
  135. var mat_DT = mat_D.Transpose();
  136. var mat_Ones = CreateMatrix.Dense<double>(mag_data_counter, 1, 1.0);
  137. var mat_Result = (mat_DT * mat_D).Inverse() * (mat_DT * mat_Ones);
  138. var mat_A_4x4 = CreateMatrix.Dense<double>(4, 4);
  139. mat_A_4x4[0, 0] = mat_Result[0, 0];
  140. mat_A_4x4[0, 1] = mat_Result[3, 0];
  141. mat_A_4x4[0, 2] = mat_Result[4, 0];
  142. mat_A_4x4[0, 3] = mat_Result[6, 0];
  143. mat_A_4x4[1, 0] = mat_Result[3, 0];
  144. mat_A_4x4[1, 1] = mat_Result[1, 0];
  145. mat_A_4x4[1, 2] = mat_Result[5, 0];
  146. mat_A_4x4[1, 3] = mat_Result[7, 0];
  147. mat_A_4x4[2, 0] = mat_Result[4, 0];
  148. mat_A_4x4[2, 1] = mat_Result[5, 0];
  149. mat_A_4x4[2, 2] = mat_Result[2, 0];
  150. mat_A_4x4[2, 3] = mat_Result[8, 0];
  151. mat_A_4x4[3, 0] = mat_Result[6, 0];
  152. mat_A_4x4[3, 1] = mat_Result[7, 0];
  153. mat_A_4x4[3, 2] = mat_Result[8, 0];
  154. mat_A_4x4[3, 3] = -1.0;
  155. var mat_Center = -((mat_A_4x4.SubMatrix(0, 3, 0, 3)).Inverse() * mat_Result.SubMatrix(6, 3, 0, 1));
  156. //椭球圆心 //分块,从0,0开始的3*3的矩阵
  157. var mat_T_4x4 = CreateMatrix.DenseIdentity<double>(4, 4);
  158. mat_T_4x4.SetSubMatrix(3, 1, 0, 3, mat_Center.Transpose());
  159. var mat_R = mat_T_4x4 * mat_A_4x4 * mat_T_4x4.Transpose();
  160. var evd = mat_R.SubMatrix(0, 3, 0, 3) / -mat_R[3, 3];
  161. var eig = evd.Evd();
  162. var mat_Eigval = CreateVector.Dense<double>(3);
  163. var mat_Evecs = eig.EigenVectors;
  164. mat_Eigval[0] = eig.EigenValues[0].Real; //特征值的实部
  165. mat_Eigval[1] = eig.EigenValues[1].Real;
  166. mat_Eigval[2] = eig.EigenValues[2].Real;
  167. var mat_Radii = mat_Eigval.Map(delegate (double x)
  168. {
  169. return 1.0 / Math.Sqrt(Math.Abs(x));
  170. }); //椭球半径,特征值倒数后开方
  171. var mat_Scale = CreateMatrix.DenseIdentity<double>(3, 3);
  172. mat_Scale[0, 0] = mat_Radii[0];
  173. mat_Scale[1, 1] = mat_Radii[1];
  174. mat_Scale[2, 2] = mat_Radii[2];
  175. //double min_Radii = mat_Radii.Minimum(); //返回最小的元素
  176. mat_Scale = mat_Scale.Inverse();// * min_Radii;
  177. var mat_Correct = mat_Evecs * mat_Scale * mat_Evecs.Transpose();
  178. //_Center = new Vector3((float)mat_Center[0], (float)mat_Center[1], (float)mat_Center[2]);
  179. Debug.Log("The Ellipsoid center is:" + mat_Center.ToString());
  180. Debug.Log("The Ellipsoid radii is:" + mat_Radii.ToString());
  181. Debug.Log("The scale matrix is:" + mat_Scale.ToString());
  182. Debug.Log("The correct matrix is:" + mat_Correct.ToString());
  183. _Center = new Vector3((float)mat_Center[0, 0], (float)mat_Center[1, 0], (float)mat_Center[2, 0]);
  184. _Radius = new Vector3((float)mat_Radii[0], (float)mat_Radii[1], (float)mat_Radii[2]);
  185. this._CorrectMatrix = mat_Correct;
  186. {
  187. BadRecords = new List<Vector3>();
  188. var AverageDistance = 0f;
  189. foreach (var i in records)
  190. {
  191. var v = i - new Vector3((float)mat_Center[0, 0], (float)mat_Center[1, 0], (float)mat_Center[2, 0]);
  192. var MathNetV = CreateVector.Dense<double>(3);
  193. MathNetV[0] = v.x;
  194. MathNetV[1] = v.y;
  195. MathNetV[2] = v.z;
  196. //MathNetV = (MathNetV * mat_Scale) * mat_Correct;
  197. MathNetV = (MathNetV) * mat_Correct;
  198. v = new Vector3((float)MathNetV[0], (float)MathNetV[1], (float)MathNetV[2]);
  199. AverageDistance += v.magnitude;
  200. }
  201. AverageDistance /= records.Count;
  202. foreach (var i in records)
  203. {
  204. var v = i - new Vector3((float)mat_Center[0, 0], (float)mat_Center[1, 0], (float)mat_Center[2, 0]);
  205. var MathNetV = CreateVector.Dense<double>(3);
  206. MathNetV[0] = v.x;
  207. MathNetV[1] = v.y;
  208. MathNetV[2] = v.z;
  209. //MathNetV = (MathNetV * mat_Scale) * mat_Correct;
  210. MathNetV = (MathNetV) * mat_Correct;
  211. v = new Vector3((float)MathNetV[0], (float)MathNetV[1], (float)MathNetV[2]);
  212. if (Math.Abs(v.magnitude - AverageDistance) > 0.1 * AverageDistance)
  213. {
  214. BadRecords.Add(i);
  215. }
  216. }
  217. Debug.Log("BadRecords: "+ BadRecords.Count);
  218. }
  219. }
  220. catch(NonConvergenceException)
  221. {
  222. Debug.Log("数据错误无法拟合");
  223. }
  224. records = null;
  225. }
  226. }
  227. }
  228. public Vector3 Update(Vector3 v)
  229. {
  230. // if (v.magnitude > 30)
  231. // Debug.Log(v);
  232. if (Calibration)
  233. {
  234. records.Add(v);
  235. return v;
  236. }
  237. if(_CorrectMatrix != null)
  238. {
  239. v -= _Center;
  240. var MathNetV = CreateVector.Dense<double>(3);
  241. MathNetV[0] = v.x;
  242. MathNetV[1] = v.y;
  243. MathNetV[2] = v.z;
  244. //MathNetV = (MathNetV * mat_Scale) * mat_Correct;
  245. MathNetV = (MathNetV) * _CorrectMatrix;
  246. v = new Vector3((float)MathNetV[0], (float)MathNetV[1], (float)MathNetV[2]);
  247. //Debug.Log(v.magnitude);
  248. return v;
  249. }
  250. return v;
  251. }
  252. public float CalibratCompletionPercentage()
  253. {
  254. return 0;
  255. }
  256. }
  257. public class o0MagneticCalibraterSimple//默认在无磁干扰环境下,有磁干扰则无法保证效果
  258. {
  259. [JsonIgnore]
  260. public Vector3 _Center = Vector3.zero;
  261. //Vector3 Center = new Vector3(0,0,0);
  262. [JsonIgnore]
  263. public Vector3 _Radius = new Vector3(2, 2, 2);
  264. public o0Project.Vector3f Center
  265. {
  266. get
  267. {
  268. return new o0Project.Vector3f(_Center.x, _Center.y, _Center.z);
  269. }
  270. set
  271. {
  272. _Center = new Vector3(value.x, value.y, value.z);
  273. }
  274. }
  275. public o0Project.Vector3f Radius
  276. {
  277. get
  278. {
  279. return new o0Project.Vector3f(_Radius.x, _Radius.y, _Radius.z);
  280. }
  281. set
  282. {
  283. _Radius = new Vector3(value.x, value.y, value.z);
  284. }
  285. }
  286. public o0MagneticCalibraterSimple()
  287. {
  288. //Calibration = true;
  289. }
  290. public o0MagneticCalibraterSimple(o0Project.Vector3f Center, o0Project.Vector3f Radius)
  291. {
  292. this.Center = Center;
  293. this.Radius = Radius;
  294. }
  295. [JsonIgnore]
  296. Vector3 Min = new Vector3(float.MinValue, float.MinValue, float.MinValue);
  297. [JsonIgnore]
  298. Vector3 Max = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
  299. [JsonIgnore]
  300. public bool Calibration
  301. {
  302. get
  303. {
  304. return !(Min == new Vector3(float.MinValue, float.MinValue, float.MinValue) && Max == new Vector3(float.MaxValue, float.MaxValue, float.MaxValue));
  305. }
  306. set
  307. {
  308. if (value == true)
  309. {
  310. Min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
  311. Max = new Vector3(float.MinValue, float.MinValue, float.MinValue);
  312. }
  313. else
  314. {
  315. Min = new Vector3(float.MinValue, float.MinValue, float.MinValue);
  316. Max = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
  317. }
  318. }
  319. }
  320. public Vector3 Update(Vector3 v)
  321. {
  322. if (v.magnitude > 30)
  323. Debug.Log(v);
  324. if (Calibration)
  325. {
  326. if (Min.x > v.x)
  327. Min.x = v.x;
  328. if (Min.y > v.y)
  329. Min.y = v.y;
  330. if (Min.z > v.z)
  331. Min.z = v.z;
  332. if (Max.x < v.x)
  333. Max.x = v.x;
  334. if (Max.y < v.y)
  335. Max.y = v.y;
  336. if (Max.z < v.z)
  337. Max.z = v.z;
  338. _Center = (Max + Min) / 2;
  339. _Radius = (Max - Min) / 2;
  340. return v;
  341. }
  342. v -= _Center;
  343. v = new Vector3(v.x / _Radius.x, v.y / _Radius.y, v.z / _Radius.z);
  344. return v;
  345. }
  346. public float CalibratCompletionPercentage()
  347. {
  348. return 0;
  349. }
  350. }
  351. public class o0MagneticCalibrater//默认在无磁干扰环境下,有磁干扰则无法保证效果
  352. {
  353. [JsonIgnore]
  354. public Vector3 _Center = Vector3.zero;
  355. //Vector3 Center = new Vector3(0,0,0);
  356. [JsonIgnore]
  357. public Vector3 _Radius = new Vector3(2, 2, 2);
  358. public o0Project.Vector3f Center
  359. {
  360. get
  361. {
  362. return new o0Project.Vector3f(_Center.x, _Center.y, _Center.z);
  363. }
  364. set
  365. {
  366. _Center = new Vector3(value.x, value.y, value.z);
  367. }
  368. }
  369. public o0Project.Vector3f Radius
  370. {
  371. get
  372. {
  373. return new o0Project.Vector3f(_Radius.x, _Radius.y, _Radius.z);
  374. }
  375. set
  376. {
  377. _Radius = new Vector3(value.x, value.y, value.z);
  378. }
  379. }
  380. public o0MagneticCalibrater()
  381. {
  382. //Calibration = true;
  383. }
  384. public o0MagneticCalibrater(o0Project.Vector3f Center, o0Project.Vector3f Radius)
  385. {
  386. this.Center = Center;
  387. this.Radius = Radius;
  388. }
  389. [JsonIgnore]
  390. HashSet<Vector3> Point = default;
  391. [JsonIgnore]
  392. int PointMaxCount = 50;
  393. [JsonIgnore]
  394. Dictionary<(Vector3, Vector3), float> Distance = default;
  395. public void AddPoint(Vector3 v)
  396. {
  397. if (Point.Contains(v))
  398. return;
  399. foreach (var i in Point)
  400. Distance.Add((i, v), Vector3.Distance(v, i));
  401. Point.Add(v);
  402. }
  403. public void RemovePoint(Vector3 v)
  404. {
  405. Point.Remove(v);
  406. foreach (var i in Point)
  407. {
  408. Distance.Remove((v, i));
  409. Distance.Remove((i, v));
  410. }
  411. }
  412. public float TotalDistance(Vector3 v)
  413. {
  414. float t = 0;
  415. foreach (var i in Point)
  416. {
  417. if (Distance.ContainsKey((i, v)))
  418. {
  419. t += Distance[(i, v)];
  420. continue;
  421. }
  422. else if (Distance.ContainsKey((v, i)))
  423. {
  424. t += Distance[(v, i)];
  425. continue;
  426. }
  427. }
  428. return t;
  429. }
  430. public Vector3 MinDistancePoint()
  431. {
  432. Vector3 minV = default;
  433. float minD = float.MaxValue;
  434. foreach (var i in Point)
  435. {
  436. float d = TotalDistance(i);
  437. if (minV == default || minD > d)
  438. {
  439. minD = d;
  440. minV = i;
  441. }
  442. }
  443. return minV;
  444. }
  445. public Vector3 RadiusScale()
  446. {
  447. Vector3 min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
  448. Vector3 max = new Vector3(float.MinValue, float.MinValue, float.MinValue);
  449. foreach (var i in Point)
  450. {
  451. if (min.x > i.x)
  452. min.x = i.x;
  453. if (min.y > i.y)
  454. min.y = i.y;
  455. if (min.z > i.z)
  456. min.z = i.z;
  457. if (max.x < i.x)
  458. max.x = i.x;
  459. if (max.y < i.y)
  460. max.y = i.y;
  461. if (max.z < i.z)
  462. max.z = i.z;
  463. }
  464. return (max - min) / 2;
  465. }
  466. [JsonIgnore]
  467. public bool Calibration
  468. {
  469. get
  470. {
  471. return Distance != null;
  472. }
  473. set
  474. {
  475. if (value == true)
  476. {
  477. Point = new HashSet<Vector3>();
  478. Distance = new Dictionary<(Vector3, Vector3), float>();
  479. }
  480. else
  481. {
  482. Distance = null;
  483. }
  484. }
  485. }
  486. [JsonIgnore]
  487. public System.Random r = new System.Random();
  488. public Vector3 Update(Vector3 v)
  489. {
  490. if (v.magnitude > 30)
  491. Debug.Log(v);
  492. if (Calibration)
  493. {
  494. AddPoint(v);
  495. if (Point.Count > PointMaxCount)
  496. {
  497. RemovePoint(MinDistancePoint());
  498. _Radius = RadiusScale();
  499. }
  500. Vector3 randomV = Point.ElementAt(r.Next(Point.Count));
  501. var scaledCenter = new Vector3(_Center.x / _Radius.x, _Center.y / _Radius.y, _Center.z / _Radius.z);
  502. var scaledV = new Vector3(randomV.x / _Radius.x, randomV.y / _Radius.y, randomV.z / _Radius.z);
  503. float diff = Vector3.Distance(scaledCenter, scaledV) - 1;
  504. scaledCenter += (scaledV - scaledCenter).normalized * diff * 0.1f;
  505. _Center = new Vector3(scaledCenter.x * _Radius.x, scaledCenter.y * _Radius.y, scaledCenter.z * _Radius.z);
  506. }
  507. /*
  508. if (diff > 0)
  509. {
  510. Center -= v * diff;
  511. }
  512. else
  513. {
  514. }/**/
  515. //Point.Add(v);
  516. //Debug.Log(v.magnitude);
  517. v -= _Center;
  518. v = new Vector3(v.x / _Radius.x, v.y / _Radius.y, v.z / _Radius.z);
  519. return v;
  520. }
  521. public float CalibratCompletionPercentage()
  522. {
  523. if (Point == null)
  524. return 0;
  525. List<float> ScaleDistance = new List<float>();
  526. foreach (var i in Point)
  527. {
  528. var v = i - _Center;
  529. ScaleDistance.Add(new Vector3(v.x / _Radius.x, v.y / _Radius.y, v.z / _Radius.z).magnitude);
  530. }
  531. while (ScaleDistance.Count < PointMaxCount)
  532. ScaleDistance.Add(0);
  533. float average = 0;
  534. foreach (var i in ScaleDistance)
  535. average += i;
  536. average /= ScaleDistance.Count;
  537. float variance = 0;
  538. foreach (var i in ScaleDistance)
  539. variance += Mathf.Pow(average - i, 2);
  540. variance /= ScaleDistance.Count;
  541. return Mathf.Pow((1 - variance / average), 10) * 100;
  542. //return variance;
  543. }
  544. }
  545. public class o0GyrCalibrater
  546. {
  547. [JsonIgnore]
  548. public Vector3 _Average = Vector3.zero;
  549. [JsonIgnore]
  550. public long Count = -1;
  551. [JsonIgnore]
  552. public bool Calibration
  553. {
  554. get
  555. {
  556. return Count != -1;
  557. }
  558. set
  559. {
  560. if (value)
  561. Count = 0;
  562. else
  563. Count = -1;
  564. }
  565. }
  566. public float[] Average
  567. {
  568. get
  569. {
  570. return new float[]{_Average.x, _Average.y, _Average.z};
  571. }
  572. set
  573. {
  574. _Average = new Vector3(value[0], value[1], value[2]);
  575. }
  576. }
  577. public o0GyrCalibrater()
  578. {
  579. }
  580. //[JsonConstructor, o0.BinarySerialization.Constructor]
  581. // public o0GyrCalibrater(o0Project.Vector3f Average)
  582. public o0GyrCalibrater(float[] Average)
  583. {
  584. this.Average = Average;
  585. }
  586. public Vector3 Update(Vector3 v)
  587. {
  588. if (Calibration)
  589. _Average += (v - _Average) / ++Count;
  590. v -= _Average;
  591. if (v.magnitude < 0.0003)
  592. return Vector3.zero;
  593. return v;
  594. }
  595. }
  596. public class o09Axis
  597. {
  598. public List<o0UIRawImageTester> Tester = new List<o0UIRawImageTester>();
  599. public List<Text> TextTester = new List<Text>();
  600. public GameObject AccMesh;
  601. public GameObject GryMesh;
  602. public GameObject MagMesh;
  603. public struct State
  604. {
  605. public Int32 TimeGap;
  606. public Vector3 Acc;
  607. public Vector3 AccSmooth;
  608. public float AccVariance;
  609. public Vector3 Gyr;
  610. public Vector3 Mag;
  611. public Vector3 MagSmooth;
  612. public Quaternion Qua;
  613. public Quaternion QuaSmooth;
  614. public float Variance;
  615. }
  616. public void SetIdentityAndSave()
  617. {
  618. setIdentity();
  619. TextTester[0].text = "AccIdentity:" + getAccIdentity();
  620. AccMesh.transform.localRotation = default;
  621. MagMesh.transform.localRotation = default;
  622. GryMesh.transform.localRotation = default;
  623. SaveIdentity();
  624. }
  625. public void LoadIdentity()
  626. {
  627. try {
  628. string magIdentityStr = PlayerPrefs.GetString("MagIdentity", "");
  629. if (magIdentityStr.Length > 0) {
  630. float[] arr = JsonConvert.DeserializeObject<float[]>(magIdentityStr);
  631. setMagIdentity(new Vector3(arr[0], arr[1], arr[2]));
  632. }
  633. string accIdentityStr = PlayerPrefs.GetString("AccIdentity", "");
  634. if (accIdentityStr.Length > 0) {
  635. float[] arr = JsonConvert.DeserializeObject<float[]>(accIdentityStr);
  636. setAccIdentity(new Vector3(arr[0], arr[1], arr[2]));
  637. }
  638. }
  639. catch (System.Exception e) { Debug.LogError(e.Message); }
  640. }
  641. private void SaveIdentity() {
  642. Vector3 m = getMagIdentity();
  643. Vector3 a = getAccIdentity();
  644. PlayerPrefs.SetString("MagIdentity",JsonConvert.SerializeObject(new float[]{
  645. m.x, m.y, m.z
  646. }));
  647. PlayerPrefs.SetString("AccIdentity", JsonConvert.SerializeObject(new float[]{
  648. a.x, a.y, a.z
  649. }));
  650. }
  651. int platformID = -1;
  652. void SetPlatformID()
  653. {
  654. if (Application.platform == RuntimePlatform.WindowsEditor) platformID = 1;
  655. else platformID = 2;
  656. }
  657. bool IsWindows()
  658. {
  659. if (platformID == -1) SetPlatformID();
  660. return platformID == 1;
  661. }
  662. public o09AxisCSBridge axisCSBridge = new o09AxisCSBridge();
  663. public Quaternion update(Vector3 AccOld, Vector3 GyrOld, Vector3 MagOld, long TimeGapOld) {
  664. if (axisCSBridge != null) {
  665. return axisCSBridge.Update_f(AccOld, GyrOld, MagOld, TimeGapOld);
  666. }
  667. if (IsWindows()) return Update_f(AccOld, GyrOld, MagOld, TimeGapOld);
  668. else return SO_Update_f(AccOld, GyrOld, MagOld, TimeGapOld);
  669. }
  670. private void setIdentity() {
  671. if (axisCSBridge != null) {
  672. axisCSBridge.SetIdentity();
  673. return;
  674. }
  675. if (IsWindows()) SetIdentity();
  676. else SO_SetIdentity();
  677. }
  678. public Vector3 getGyrOld() {
  679. if (axisCSBridge != null) {
  680. return axisCSBridge.GetGyrOld_f();
  681. }
  682. if (IsWindows()) return GetGyrOld_f();
  683. else return SO_GetGyrOld_f();
  684. }
  685. public State getLastState() {
  686. if (axisCSBridge != null) {
  687. return axisCSBridge.GetLastState_f();
  688. }
  689. if (IsWindows()) return GetLastState_f();
  690. else return SO_GetLastState_f();
  691. }
  692. private Vector3 getAccIdentity() {
  693. if (axisCSBridge != null) {
  694. return axisCSBridge.GetAccIdentity_f();
  695. }
  696. if (IsWindows()) return GetAccIdentity_f();
  697. else return SO_GetAccIdentity_f();
  698. }
  699. private Vector3 getMagIdentity() {
  700. if (axisCSBridge != null) {
  701. return axisCSBridge.GetMagIdentity_f();
  702. }
  703. if (IsWindows()) return GetMagIdentity_f();
  704. else return SO_GetMagIdentity_f();
  705. }
  706. private void setAccIdentity(Vector3 value) {
  707. if (axisCSBridge != null) {
  708. axisCSBridge.SetAccIdentity(new Vector3D(value));
  709. return;
  710. }
  711. if (IsWindows()) SetAccIdentity(new Vector3D(value));
  712. else SO_SetAccIdentity(new Vector3D(value));
  713. }
  714. private void setMagIdentity(Vector3 value) {
  715. if (axisCSBridge != null) {
  716. axisCSBridge.SetMagIdentity(new Vector3D(value));
  717. return;
  718. }
  719. if (IsWindows()) SetMagIdentity(new Vector3D(value));
  720. else SO_SetMagIdentity(new Vector3D(value));
  721. }
  722. [DllImport("o09Axis")]
  723. extern static Quaternion Update_f(Vector3 AccOld, Vector3 GyrOld, Vector3 MagOld, long TimeGapOld);
  724. [DllImport("o09Axis")]
  725. extern static void SetIdentity();
  726. [DllImport("o09Axis")]
  727. extern static Vector3 GetGyrOld_f();
  728. [DllImport("o09Axis")]
  729. extern static State GetLastState_f();
  730. [DllImport("o09Axis")]
  731. extern static Vector3 GetAccIdentity_f();
  732. [DllImport("o09Axis")]
  733. extern static Vector3 GetMagIdentity_f();
  734. [DllImport("o09Axis")]
  735. extern static void SetAccIdentity(Vector3D value);
  736. [DllImport("o09Axis")]
  737. extern static void SetMagIdentity(Vector3D value);
  738. [DllImport("libSharedObjectAxis")]
  739. extern static Quaternion SO_Update_f(Vector3 AccOld, Vector3 GyrOld, Vector3 MagOld, long TimeGapOld);
  740. [DllImport("libSharedObjectAxis")]
  741. extern static void SO_SetIdentity();
  742. [DllImport("libSharedObjectAxis")]
  743. extern static Vector3 SO_GetGyrOld_f();
  744. [DllImport("libSharedObjectAxis")]
  745. extern static State SO_GetLastState_f();
  746. [DllImport("libSharedObjectAxis")]
  747. extern static Vector3 SO_GetAccIdentity_f();
  748. [DllImport("libSharedObjectAxis")]
  749. extern static Vector3 SO_GetMagIdentity_f();
  750. [DllImport("libSharedObjectAxis")]
  751. extern static void SO_SetAccIdentity(Vector3D value);
  752. [DllImport("libSharedObjectAxis")]
  753. extern static void SO_SetMagIdentity(Vector3D value);
  754. public struct Vector3D {
  755. public double x;
  756. public double y;
  757. public double z;
  758. public Vector3D(Vector3 v) {
  759. this.x = v.x;
  760. this.y = v.y;
  761. this.z = v.z;
  762. }
  763. }
  764. }