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. 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. if (records.Count > 3600) records.RemoveAt(0);
  236. return v;
  237. }
  238. if(_CorrectMatrix != null)
  239. {
  240. v -= _Center;
  241. var MathNetV = CreateVector.Dense<double>(3);
  242. MathNetV[0] = v.x;
  243. MathNetV[1] = v.y;
  244. MathNetV[2] = v.z;
  245. //MathNetV = (MathNetV * mat_Scale) * mat_Correct;
  246. MathNetV = (MathNetV) * _CorrectMatrix;
  247. v = new Vector3((float)MathNetV[0], (float)MathNetV[1], (float)MathNetV[2]);
  248. //Debug.Log(v.magnitude);
  249. return v;
  250. }
  251. return v;
  252. }
  253. public float CalibratCompletionPercentage()
  254. {
  255. return 0;
  256. }
  257. }
  258. public class o0MagneticCalibraterSimple//默认在无磁干扰环境下,有磁干扰则无法保证效果
  259. {
  260. [JsonIgnore]
  261. public Vector3 _Center = Vector3.zero;
  262. //Vector3 Center = new Vector3(0,0,0);
  263. [JsonIgnore]
  264. public Vector3 _Radius = new Vector3(2, 2, 2);
  265. public o0Project.Vector3f Center
  266. {
  267. get
  268. {
  269. return new o0Project.Vector3f(_Center.x, _Center.y, _Center.z);
  270. }
  271. set
  272. {
  273. _Center = new Vector3(value.x, value.y, value.z);
  274. }
  275. }
  276. public o0Project.Vector3f Radius
  277. {
  278. get
  279. {
  280. return new o0Project.Vector3f(_Radius.x, _Radius.y, _Radius.z);
  281. }
  282. set
  283. {
  284. _Radius = new Vector3(value.x, value.y, value.z);
  285. }
  286. }
  287. public o0MagneticCalibraterSimple()
  288. {
  289. //Calibration = true;
  290. }
  291. public o0MagneticCalibraterSimple(o0Project.Vector3f Center, o0Project.Vector3f Radius)
  292. {
  293. this.Center = Center;
  294. this.Radius = Radius;
  295. }
  296. [JsonIgnore]
  297. Vector3 Min = new Vector3(float.MinValue, float.MinValue, float.MinValue);
  298. [JsonIgnore]
  299. Vector3 Max = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
  300. [JsonIgnore]
  301. public bool Calibration
  302. {
  303. get
  304. {
  305. return !(Min == new Vector3(float.MinValue, float.MinValue, float.MinValue) && Max == new Vector3(float.MaxValue, float.MaxValue, float.MaxValue));
  306. }
  307. set
  308. {
  309. if (value == true)
  310. {
  311. Min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
  312. Max = new Vector3(float.MinValue, float.MinValue, float.MinValue);
  313. }
  314. else
  315. {
  316. Min = new Vector3(float.MinValue, float.MinValue, float.MinValue);
  317. Max = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
  318. }
  319. }
  320. }
  321. public Vector3 Update(Vector3 v)
  322. {
  323. if (v.magnitude > 30)
  324. Debug.Log(v);
  325. if (Calibration)
  326. {
  327. if (Min.x > v.x)
  328. Min.x = v.x;
  329. if (Min.y > v.y)
  330. Min.y = v.y;
  331. if (Min.z > v.z)
  332. Min.z = v.z;
  333. if (Max.x < v.x)
  334. Max.x = v.x;
  335. if (Max.y < v.y)
  336. Max.y = v.y;
  337. if (Max.z < v.z)
  338. Max.z = v.z;
  339. _Center = (Max + Min) / 2;
  340. _Radius = (Max - Min) / 2;
  341. return v;
  342. }
  343. v -= _Center;
  344. v = new Vector3(v.x / _Radius.x, v.y / _Radius.y, v.z / _Radius.z);
  345. return v;
  346. }
  347. public float CalibratCompletionPercentage()
  348. {
  349. return 0;
  350. }
  351. }
  352. public class o0MagneticCalibrater//默认在无磁干扰环境下,有磁干扰则无法保证效果
  353. {
  354. [JsonIgnore]
  355. public Vector3 _Center = Vector3.zero;
  356. //Vector3 Center = new Vector3(0,0,0);
  357. [JsonIgnore]
  358. public Vector3 _Radius = new Vector3(2, 2, 2);
  359. public o0Project.Vector3f Center
  360. {
  361. get
  362. {
  363. return new o0Project.Vector3f(_Center.x, _Center.y, _Center.z);
  364. }
  365. set
  366. {
  367. _Center = new Vector3(value.x, value.y, value.z);
  368. }
  369. }
  370. public o0Project.Vector3f Radius
  371. {
  372. get
  373. {
  374. return new o0Project.Vector3f(_Radius.x, _Radius.y, _Radius.z);
  375. }
  376. set
  377. {
  378. _Radius = new Vector3(value.x, value.y, value.z);
  379. }
  380. }
  381. public o0MagneticCalibrater()
  382. {
  383. //Calibration = true;
  384. }
  385. public o0MagneticCalibrater(o0Project.Vector3f Center, o0Project.Vector3f Radius)
  386. {
  387. this.Center = Center;
  388. this.Radius = Radius;
  389. }
  390. [JsonIgnore]
  391. HashSet<Vector3> Point = default;
  392. [JsonIgnore]
  393. int PointMaxCount = 50;
  394. [JsonIgnore]
  395. Dictionary<(Vector3, Vector3), float> Distance = default;
  396. public void AddPoint(Vector3 v)
  397. {
  398. if (Point.Contains(v))
  399. return;
  400. foreach (var i in Point)
  401. Distance.Add((i, v), Vector3.Distance(v, i));
  402. Point.Add(v);
  403. }
  404. public void RemovePoint(Vector3 v)
  405. {
  406. Point.Remove(v);
  407. foreach (var i in Point)
  408. {
  409. Distance.Remove((v, i));
  410. Distance.Remove((i, v));
  411. }
  412. }
  413. public float TotalDistance(Vector3 v)
  414. {
  415. float t = 0;
  416. foreach (var i in Point)
  417. {
  418. if (Distance.ContainsKey((i, v)))
  419. {
  420. t += Distance[(i, v)];
  421. continue;
  422. }
  423. else if (Distance.ContainsKey((v, i)))
  424. {
  425. t += Distance[(v, i)];
  426. continue;
  427. }
  428. }
  429. return t;
  430. }
  431. public Vector3 MinDistancePoint()
  432. {
  433. Vector3 minV = default;
  434. float minD = float.MaxValue;
  435. foreach (var i in Point)
  436. {
  437. float d = TotalDistance(i);
  438. if (minV == default || minD > d)
  439. {
  440. minD = d;
  441. minV = i;
  442. }
  443. }
  444. return minV;
  445. }
  446. public Vector3 RadiusScale()
  447. {
  448. Vector3 min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
  449. Vector3 max = new Vector3(float.MinValue, float.MinValue, float.MinValue);
  450. foreach (var i in Point)
  451. {
  452. if (min.x > i.x)
  453. min.x = i.x;
  454. if (min.y > i.y)
  455. min.y = i.y;
  456. if (min.z > i.z)
  457. min.z = i.z;
  458. if (max.x < i.x)
  459. max.x = i.x;
  460. if (max.y < i.y)
  461. max.y = i.y;
  462. if (max.z < i.z)
  463. max.z = i.z;
  464. }
  465. return (max - min) / 2;
  466. }
  467. [JsonIgnore]
  468. public bool Calibration
  469. {
  470. get
  471. {
  472. return Distance != null;
  473. }
  474. set
  475. {
  476. if (value == true)
  477. {
  478. Point = new HashSet<Vector3>();
  479. Distance = new Dictionary<(Vector3, Vector3), float>();
  480. }
  481. else
  482. {
  483. Distance = null;
  484. }
  485. }
  486. }
  487. [JsonIgnore]
  488. public System.Random r = new System.Random();
  489. public Vector3 Update(Vector3 v)
  490. {
  491. if (v.magnitude > 30)
  492. Debug.Log(v);
  493. if (Calibration)
  494. {
  495. AddPoint(v);
  496. if (Point.Count > PointMaxCount)
  497. {
  498. RemovePoint(MinDistancePoint());
  499. _Radius = RadiusScale();
  500. }
  501. Vector3 randomV = Point.ElementAt(r.Next(Point.Count));
  502. var scaledCenter = new Vector3(_Center.x / _Radius.x, _Center.y / _Radius.y, _Center.z / _Radius.z);
  503. var scaledV = new Vector3(randomV.x / _Radius.x, randomV.y / _Radius.y, randomV.z / _Radius.z);
  504. float diff = Vector3.Distance(scaledCenter, scaledV) - 1;
  505. scaledCenter += (scaledV - scaledCenter).normalized * diff * 0.1f;
  506. _Center = new Vector3(scaledCenter.x * _Radius.x, scaledCenter.y * _Radius.y, scaledCenter.z * _Radius.z);
  507. }
  508. /*
  509. if (diff > 0)
  510. {
  511. Center -= v * diff;
  512. }
  513. else
  514. {
  515. }/**/
  516. //Point.Add(v);
  517. //Debug.Log(v.magnitude);
  518. v -= _Center;
  519. v = new Vector3(v.x / _Radius.x, v.y / _Radius.y, v.z / _Radius.z);
  520. return v;
  521. }
  522. public float CalibratCompletionPercentage()
  523. {
  524. if (Point == null)
  525. return 0;
  526. List<float> ScaleDistance = new List<float>();
  527. foreach (var i in Point)
  528. {
  529. var v = i - _Center;
  530. ScaleDistance.Add(new Vector3(v.x / _Radius.x, v.y / _Radius.y, v.z / _Radius.z).magnitude);
  531. }
  532. while (ScaleDistance.Count < PointMaxCount)
  533. ScaleDistance.Add(0);
  534. float average = 0;
  535. foreach (var i in ScaleDistance)
  536. average += i;
  537. average /= ScaleDistance.Count;
  538. float variance = 0;
  539. foreach (var i in ScaleDistance)
  540. variance += Mathf.Pow(average - i, 2);
  541. variance /= ScaleDistance.Count;
  542. return Mathf.Pow((1 - variance / average), 10) * 100;
  543. //return variance;
  544. }
  545. }
  546. public class o0GyrCalibrater
  547. {
  548. [JsonIgnore]
  549. public Vector3 _Average = Vector3.zero;
  550. [JsonIgnore]
  551. public long Count = -1;
  552. [JsonIgnore]
  553. public bool Calibration
  554. {
  555. get
  556. {
  557. return Count != -1;
  558. }
  559. set
  560. {
  561. if (value)
  562. Count = 0;
  563. else
  564. Count = -1;
  565. }
  566. }
  567. public float[] Average
  568. {
  569. get
  570. {
  571. return new float[]{_Average.x, _Average.y, _Average.z};
  572. }
  573. set
  574. {
  575. _Average = new Vector3(value[0], value[1], value[2]);
  576. }
  577. }
  578. public o0GyrCalibrater()
  579. {
  580. }
  581. //[JsonConstructor, o0.BinarySerialization.Constructor]
  582. // public o0GyrCalibrater(o0Project.Vector3f Average)
  583. public o0GyrCalibrater(float[] Average)
  584. {
  585. this.Average = Average;
  586. }
  587. public Vector3 Update(Vector3 v)
  588. {
  589. if (Calibration)
  590. _Average += (v - _Average) / ++Count;
  591. v -= _Average;
  592. if (v.magnitude < 0.0003)
  593. return Vector3.zero;
  594. return v;
  595. }
  596. }
  597. public class o09Axis
  598. {
  599. public List<o0UIRawImageTester> Tester = new List<o0UIRawImageTester>();
  600. public List<Text> TextTester = new List<Text>();
  601. public GameObject AccMesh;
  602. public GameObject GryMesh;
  603. public GameObject MagMesh;
  604. public struct State
  605. {
  606. public Int32 TimeGap;
  607. public Vector3 Acc;
  608. public Vector3 AccSmooth;
  609. public float AccVariance;
  610. public Vector3 Gyr;
  611. public Vector3 Mag;
  612. public Vector3 MagSmooth;
  613. public Quaternion Qua;
  614. public Quaternion QuaSmooth;
  615. public float Variance;
  616. }
  617. public void SetIdentityAndSave()
  618. {
  619. setIdentity();
  620. TextTester[0].text = "AccIdentity:" + getAccIdentity();
  621. AccMesh.transform.localRotation = default;
  622. MagMesh.transform.localRotation = default;
  623. GryMesh.transform.localRotation = default;
  624. SaveIdentity();
  625. }
  626. public void LoadIdentity()
  627. {
  628. try {
  629. string magIdentityStr = PlayerPrefs.GetString("MagIdentity", "");
  630. if (magIdentityStr.Length > 0) {
  631. float[] arr = JsonConvert.DeserializeObject<float[]>(magIdentityStr);
  632. setMagIdentity(new Vector3(arr[0], arr[1], arr[2]));
  633. }
  634. string accIdentityStr = PlayerPrefs.GetString("AccIdentity", "");
  635. if (accIdentityStr.Length > 0) {
  636. float[] arr = JsonConvert.DeserializeObject<float[]>(accIdentityStr);
  637. setAccIdentity(new Vector3(arr[0], arr[1], arr[2]));
  638. }
  639. }
  640. catch (System.Exception e) { Debug.LogError(e.Message); }
  641. }
  642. private void SaveIdentity() {
  643. Vector3 m = getMagIdentity();
  644. Vector3 a = getAccIdentity();
  645. PlayerPrefs.SetString("MagIdentity",JsonConvert.SerializeObject(new float[]{
  646. m.x, m.y, m.z
  647. }));
  648. PlayerPrefs.SetString("AccIdentity", JsonConvert.SerializeObject(new float[]{
  649. a.x, a.y, a.z
  650. }));
  651. }
  652. int platformID = -1;
  653. void SetPlatformID()
  654. {
  655. if (Application.platform == RuntimePlatform.WindowsEditor) platformID = 1;
  656. else platformID = 2;
  657. }
  658. bool IsWindows()
  659. {
  660. if (platformID == -1) SetPlatformID();
  661. return platformID == 1;
  662. }
  663. o09AxisCSBridge axisCSBridge = new o09AxisCSBridge();
  664. public Quaternion update(Vector3 AccOld, Vector3 GyrOld, Vector3 MagOld, long TimeGapOld) {
  665. if (axisCSBridge != null) {
  666. return axisCSBridge.Update_f(AccOld, GyrOld, MagOld, TimeGapOld);
  667. }
  668. if (IsWindows()) return Update_f(AccOld, GyrOld, MagOld, TimeGapOld);
  669. else return SO_Update_f(AccOld, GyrOld, MagOld, TimeGapOld);
  670. }
  671. private void setIdentity() {
  672. if (axisCSBridge != null) {
  673. axisCSBridge.SetIdentity();
  674. return;
  675. }
  676. if (IsWindows()) SetIdentity();
  677. else SO_SetIdentity();
  678. }
  679. public Vector3 getGyrOld() {
  680. if (axisCSBridge != null) {
  681. return axisCSBridge.GetGyrOld_f();
  682. }
  683. if (IsWindows()) return GetGyrOld_f();
  684. else return SO_GetGyrOld_f();
  685. }
  686. public State getLastState() {
  687. if (axisCSBridge != null) {
  688. return axisCSBridge.GetLastState_f();
  689. }
  690. if (IsWindows()) return GetLastState_f();
  691. else return SO_GetLastState_f();
  692. }
  693. private Vector3 getAccIdentity() {
  694. if (axisCSBridge != null) {
  695. return axisCSBridge.GetAccIdentity_f();
  696. }
  697. if (IsWindows()) return GetAccIdentity_f();
  698. else return SO_GetAccIdentity_f();
  699. }
  700. private Vector3 getMagIdentity() {
  701. if (axisCSBridge != null) {
  702. return axisCSBridge.GetMagIdentity_f();
  703. }
  704. if (IsWindows()) return GetMagIdentity_f();
  705. else return SO_GetMagIdentity_f();
  706. }
  707. private void setAccIdentity(Vector3 value) {
  708. if (axisCSBridge != null) {
  709. axisCSBridge.SetAccIdentity(new Vector3D(value));
  710. return;
  711. }
  712. if (IsWindows()) SetAccIdentity(new Vector3D(value));
  713. else SO_SetAccIdentity(new Vector3D(value));
  714. }
  715. private void setMagIdentity(Vector3 value) {
  716. if (axisCSBridge != null) {
  717. axisCSBridge.SetMagIdentity(new Vector3D(value));
  718. return;
  719. }
  720. if (IsWindows()) SetMagIdentity(new Vector3D(value));
  721. else SO_SetMagIdentity(new Vector3D(value));
  722. }
  723. [DllImport("o09Axis")]
  724. extern static Quaternion Update_f(Vector3 AccOld, Vector3 GyrOld, Vector3 MagOld, long TimeGapOld);
  725. [DllImport("o09Axis")]
  726. extern static void SetIdentity();
  727. [DllImport("o09Axis")]
  728. extern static Vector3 GetGyrOld_f();
  729. [DllImport("o09Axis")]
  730. extern static State GetLastState_f();
  731. [DllImport("o09Axis")]
  732. extern static Vector3 GetAccIdentity_f();
  733. [DllImport("o09Axis")]
  734. extern static Vector3 GetMagIdentity_f();
  735. [DllImport("o09Axis")]
  736. extern static void SetAccIdentity(Vector3D value);
  737. [DllImport("o09Axis")]
  738. extern static void SetMagIdentity(Vector3D value);
  739. [DllImport("libSharedObjectAxis")]
  740. extern static Quaternion SO_Update_f(Vector3 AccOld, Vector3 GyrOld, Vector3 MagOld, long TimeGapOld);
  741. [DllImport("libSharedObjectAxis")]
  742. extern static void SO_SetIdentity();
  743. [DllImport("libSharedObjectAxis")]
  744. extern static Vector3 SO_GetGyrOld_f();
  745. [DllImport("libSharedObjectAxis")]
  746. extern static State SO_GetLastState_f();
  747. [DllImport("libSharedObjectAxis")]
  748. extern static Vector3 SO_GetAccIdentity_f();
  749. [DllImport("libSharedObjectAxis")]
  750. extern static Vector3 SO_GetMagIdentity_f();
  751. [DllImport("libSharedObjectAxis")]
  752. extern static void SO_SetAccIdentity(Vector3D value);
  753. [DllImport("libSharedObjectAxis")]
  754. extern static void SO_SetMagIdentity(Vector3D value);
  755. public struct Vector3D {
  756. public double x;
  757. public double y;
  758. public double z;
  759. public Vector3D(Vector3 v) {
  760. this.x = v.x;
  761. this.y = v.y;
  762. this.z = v.z;
  763. }
  764. }
  765. }