|
|
@@ -0,0 +1,74 @@
|
|
|
+using System;
|
|
|
+using Newtonsoft.Json;
|
|
|
+using Newtonsoft.Json.Linq;
|
|
|
+using o0._9Axis;
|
|
|
+using MathNet.Numerics.LinearAlgebra;
|
|
|
+
|
|
|
+public class MagJsonConverter : JsonConverter
|
|
|
+{
|
|
|
+ public override bool CanConvert(Type objectType)
|
|
|
+ {
|
|
|
+ return typeof(MagnetometerAutoCalibrater) == objectType;
|
|
|
+ }
|
|
|
+
|
|
|
+ public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
|
|
+ {
|
|
|
+ var jo = JObject.Load(reader);
|
|
|
+ var tg = new MagnetometerAutoCalibrater();
|
|
|
+ tg.MaxCount = jo.GetValue("MaxCount").ToObject<int>();
|
|
|
+ tg.Lock = jo.GetValue("Lock").ToObject<bool>();
|
|
|
+ tg.Variance = jo.GetValue("Variance").ToObject<double>();
|
|
|
+ tg.EllipsoidFitting = ToEllipsoidFitting(jo.GetValue("EllipsoidFitting"));
|
|
|
+ tg.FitThreshold = jo.GetValue("FitThreshold").ToObject<int>();
|
|
|
+ tg.FitCountLeft = jo.GetValue("FitCountLeft").ToObject<int>();
|
|
|
+ tg.NewBlockAccumulation = jo.GetValue("NewBlockAccumulation").ToObject<double>();
|
|
|
+ tg.NewBlock = jo.GetValue("NewBlock").ToObject<int>();
|
|
|
+ tg.CountPerLength = jo.GetValue("CountPerLength").ToObject<double>();
|
|
|
+ foreach (JProperty item in jo.GetValue("VectorByBlock"))
|
|
|
+ tg.VectorByBlock[ToVectorInt(JArray.Parse(item.Name))] = ToVectorDouble(item.Value as JArray);
|
|
|
+ tg.LastTimestamp = jo.GetValue("LastTimestamp").ToObject<long>();
|
|
|
+ return tg;
|
|
|
+ }
|
|
|
+
|
|
|
+ public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) {
|
|
|
+ var jo = JObject.FromObject(value);
|
|
|
+ var tg = value as MagnetometerAutoCalibrater;
|
|
|
+ jo.Remove("Complete");
|
|
|
+ jo.Remove("EllipsoidFitting");
|
|
|
+ var EllipsoidFitting = tg.EllipsoidFitting;
|
|
|
+ JObject _EllipsoidFitting = new JObject();
|
|
|
+ _EllipsoidFitting.Add("Center", JToken.FromObject(EllipsoidFitting.Center));
|
|
|
+ _EllipsoidFitting.Add("CorrectMatrixArray", JToken.FromObject(EllipsoidFitting.CorrectMatrixArray));
|
|
|
+ _EllipsoidFitting.Add("Radius", JToken.FromObject(EllipsoidFitting.Radius));
|
|
|
+ jo.Add("EllipsoidFitting", _EllipsoidFitting);
|
|
|
+ jo.WriteTo(writer);
|
|
|
+ }
|
|
|
+
|
|
|
+ private EllipsoidFitting ToEllipsoidFitting(JToken jToken)
|
|
|
+ {
|
|
|
+ var Center = ToVectorDouble(jToken["Center"] as JArray);
|
|
|
+ var CorrectMatrix = ToMatrixDouble(jToken["CorrectMatrixArray"] as JArray);
|
|
|
+ var Radius = ToVectorDouble(jToken["Radius"] as JArray);
|
|
|
+ return new EllipsoidFitting(Center, CorrectMatrix, Radius);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Matrix<double> ToMatrixDouble(JArray arr)
|
|
|
+ {
|
|
|
+ int rcCount = arr.Count;
|
|
|
+ Matrix<double> matrix = CreateMatrix.Dense<double>(rcCount, rcCount);
|
|
|
+ for (var i = 0; i < rcCount; ++i)
|
|
|
+ for (var j = 0; j < rcCount; ++j)
|
|
|
+ matrix[i, j] = arr[i][j].Value<double>();
|
|
|
+ return matrix;
|
|
|
+ }
|
|
|
+
|
|
|
+ private o0.Geometry.Vector<double> ToVectorDouble(JArray arr)
|
|
|
+ {
|
|
|
+ return new o0.Geometry.Vector<double>(arr[0].Value<double>(), arr[1].Value<double>(), arr[2].Value<double>());
|
|
|
+ }
|
|
|
+
|
|
|
+ private o0.Geometry.Vector<int> ToVectorInt(JArray arr)
|
|
|
+ {
|
|
|
+ return new o0.Geometry.Vector<int>(arr[0].Value<int>(), arr[1].Value<int>(), arr[2].Value<int>());
|
|
|
+ }
|
|
|
+}
|