165 lines
28 KiB
C#
165 lines
28 KiB
C#
|
|
using GeoJSON.Net.Geometry;
|
|||
|
|
using Mapbox.Utils;
|
|||
|
|
using Newtonsoft.Json;
|
|||
|
|
using Newtonsoft.Json.Linq;
|
|||
|
|
using System;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Diagnostics;
|
|||
|
|
using System.Globalization;
|
|||
|
|
using System.IO;
|
|||
|
|
using TurfCS;
|
|||
|
|
using UnityEngine.Networking;
|
|||
|
|
|
|||
|
|
public static class MockData
|
|||
|
|
{
|
|||
|
|
public static double totaldistance;
|
|||
|
|
public static List<Vector2d> vl;
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取模拟的骑行记录
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static List<Vector2d> GetRideList()
|
|||
|
|
{
|
|||
|
|
string json = "{'Type':'LineString','TotalDistance':19.17906,'List':[{'Point':[26.092100000000002,119.33686000000002],'Elevation':7.6198},{'Point':[26.09211,119.33749000000002],'Elevation':7.5859},{'Point':[26.092170000000003,119.33840000000001],'Elevation':7.421},{'Point':[26.092260000000003,119.33921000000001],'Elevation':7.3133},{'Point':[26.092270002098761,119.33999000005943],'Elevation':7.2394},{'Point':[26.092280000000002,119.34077],'Elevation':7.2701},{'Point':[26.092240000956789,119.34129500009136],'Elevation':7.3416},{'Point':[26.092200000000002,119.34182000000001],'Elevation':7.4177},{'Point':[26.092083338752424,119.34270666844074],'Elevation':7.5033},{'Point':[26.09196667208499,119.34359333511317],'Elevation':7.2596},{'Point':[26.09185,119.34448],'Elevation':6.8727},{'Point':[26.091680000000004,119.34505000000001],'Elevation':6.4546},{'Point':[26.091536672044878,119.34593333550309],'Elevation':6.0312},{'Point':[26.091393338710645,119.34681666884185],'Elevation':5.751},{'Point':[26.091250000000002,119.3477],'Elevation':5.6373},{'Point':[26.091230000000003,119.34794000000001],'Elevation':5.6052},{'Point':[26.091225001411136,119.3485800001182],'Elevation':5.5997},{'Point':[26.091220000000003,119.34922000000002],'Elevation':5.6907},{'Point':[26.091330000000003,119.34997000000001],'Elevation':6.0231},{'Point':[26.091440000000002,119.35021],'Elevation':6.4916},{'Point':[26.09157,119.35095000000001],'Elevation':7.0092},{'Point':[26.091600000000003,119.3516],'Elevation':7.3641},{'Point':[26.091767507390937,119.35244499640774],'Elevation':7.4909},{'Point':[26.091935009859512,119.353289995235],'Elevation':6.9736},{'Point':[26.092102507405667,119.35413499648182],'Elevation':6.7797},{'Point':[26.092270000000003,119.35498000000001],'Elevation':6.5711},{'Point':[26.092405001867032,119.35571999903352],'Elevation':6.4206},{'Point':[26.092540000000003,119.35646000000001],'Elevation':6.3653},{'Point':[26.09269,119.35732000000002],'Elevation':7.251},{'Point':[26.092710000000004,119.35761000000001],'Elevation':8.2263},{'Point':[26.092703339253426,119.35853666675888],'Elevation':9.8464},{'Point':[26.092696672586847,119.35946333341218],'Elevation':11.8545},{'Point':[26.09269,119.36039000000001],'Elevation':13.6159},{'Point':[26.092665001414368,119.36103000007358],'Elevation':15.1102},{'Point':[26.092640000000003,119.36167],'Elevation':16.3984},{'Point':[26.0925,119.36225],'Elevation':17.1179},{'Point':[26.092270000000003,119.36289000000001],'Elevation':17.3805},{'Point':[26.092200000000002,119.36334000000001],'Elevation':17.3022},{'Point':[26.092180000000003,119.36363000000001],'Elevation':17.2282},{'Point':[26.092190000000002,119.36396],'Elevation':17.2224},{'Point':[26.092290000000002,119.36435000000002],'Elevation':17.1729},{'Point':[26.092100000000002,119.36453000000002],'Elevation':17.1065},{'Point':[26.092000000000002,119.36473000000001],'Elevation':17.0503},{'Point':[26.091910000000002,119.36502000000002],'Elevation':17.1392},{'Point':[26.09193,119.36525],'Elevation':17.2636},{'Point':[26.092150000000004,119.36569000000001],'Elevation':17.4486},{'Point':[26.09233,119.36581000000001],'Elevation':17.7034},{'Point':[26.092540000000003,119.36588],'Elevation':18.1077},{'Point':[26.092750000000002,119.36589000000001],'Elevation':18.5083},{'Point':[26.092950000000002,119.36582000000001],'Elevation':18.892},{'Point':[26.093110000000003,119.36572000000001],'Elevation':19.2643},{'Point':[26.09335,119.36544],'Elevation':19.6256},{'Point':[26.094033333311039,119.36542333352864],'Elevation':20.1707},{'Point':[26.094716666620165,119.36540666686258],'Elevation':21.2487},{'Point':[26.0954,119.36539],'Elevation':22.396},{'Point':[26.09605,119.36548],'Elevation':23.9715},{'Point':[26.09615,119.36567000000001],'Elevation':25.3392},{'Point':[26.096753333417059,119.36562333380944],'Elevation':26.626},{'Point':[26.097356666819095,119.36557666713746],'Elevation':28.1709},{'Point':[26.09796,119.36553],'Elevation':30.077},{'Point':[26.098260000000003,119.36556000000002],'Elevation':32.1094},{'Point':[26.09888000013564,119.36572999910875],'E
|
|||
|
|
|
|||
|
|
using (JsonTextReader reader = new JsonTextReader(new StringReader(json)))
|
|||
|
|
{
|
|||
|
|
JObject jo = (JObject)JToken.ReadFrom(reader);
|
|||
|
|
totaldistance = (double)jo["TotalDistance"];
|
|||
|
|
vl = new List<Vector2d>();
|
|||
|
|
foreach (var point in jo["List"])
|
|||
|
|
{
|
|||
|
|
var s = point["Point"];
|
|||
|
|
Vector2d points = new Vector2d(Math.Round(Convert.ToDouble(s[0]), 5), Math.Round(Convert.ToDouble(s[1]), 5));
|
|||
|
|
vl.Add(points);
|
|||
|
|
}
|
|||
|
|
return vl;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static List<Vector2d> GetRideList(string json)
|
|||
|
|
{
|
|||
|
|
using (JsonTextReader reader = new JsonTextReader(new StringReader(json)))
|
|||
|
|
{
|
|||
|
|
JObject jo = (JObject)JToken.ReadFrom(reader);
|
|||
|
|
vl = new List<Vector2d>();
|
|||
|
|
foreach (var point in jo["List"])
|
|||
|
|
{
|
|||
|
|
var s = point["Point"];
|
|||
|
|
Vector2d points = new Vector2d(Math.Round(Convert.ToDouble(s[0]), 5), Math.Round(Convert.ToDouble(s[1]), 5));
|
|||
|
|
vl.Add(points);
|
|||
|
|
}
|
|||
|
|
return vl;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public static Vector2d Along(double distance)
|
|||
|
|
{
|
|||
|
|
var list = vl.Select(p => new GeoJSON.Net.Geometry.GeographicPosition(p.x, p.y));
|
|||
|
|
LineString lineString = new LineString(list);
|
|||
|
|
var pt1 = Turf.Along(lineString, distance);
|
|||
|
|
var ll =((GeographicPosition)((GeoJSON.Net.Geometry.Point)pt1.Geometry).Coordinates);
|
|||
|
|
return new Vector2d(ll.Latitude, ll.Longitude);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 计算速度
|
|||
|
|
/// 参考 http://bikecalculator.com/index.html
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="elevationv">海拔</param>
|
|||
|
|
/// <param name="gradev">坡度%</param>
|
|||
|
|
/// <param name="powerv">功率</param>
|
|||
|
|
/// <param name="rweightv">体重</param>
|
|||
|
|
/// <param name="bweightv">自行车重量</param>
|
|||
|
|
/// <returns>km/h</returns>
|
|||
|
|
public static double CalculateSpeed(double elevationv, double gradev, double powerv, double rweightv, double bweightv)
|
|||
|
|
{
|
|||
|
|
if (powerv < 0)
|
|||
|
|
{
|
|||
|
|
return 0;
|
|||
|
|
}
|
|||
|
|
if (gradev < -10)
|
|||
|
|
{
|
|||
|
|
gradev = -10;
|
|||
|
|
}
|
|||
|
|
var tireValues = new double[] { 0.005, 0.004, 0.012 };
|
|||
|
|
var aeroValues = new double[] { 0.388, 0.445, 0.420, 0.300, 0.233, 0.200 };
|
|||
|
|
var units = 0;
|
|||
|
|
|
|||
|
|
//温度
|
|||
|
|
var temperaturev = 25;
|
|||
|
|
|
|||
|
|
//海拔
|
|||
|
|
//var elevationv = 100;
|
|||
|
|
|
|||
|
|
//坡度
|
|||
|
|
//var gradev = 6 * 0.01 * -1;
|
|||
|
|
|
|||
|
|
//风,转换成m/s
|
|||
|
|
var headwindv = 0 / 3.6;
|
|||
|
|
|
|||
|
|
var frontalArea = aeroValues[0];
|
|||
|
|
|
|||
|
|
var transv = 0.95; // no one knows what this is, so why bother presenting a choice?
|
|||
|
|
var rollingRes = tireValues[0];
|
|||
|
|
|
|||
|
|
//应该是空气密度
|
|||
|
|
var density = (1.293 - 0.00426 * temperaturev) * Math.Exp(-elevationv / 7000.0);
|
|||
|
|
|
|||
|
|
|
|||
|
|
//全空气阻力参数
|
|||
|
|
var a2 = 0.5 * frontalArea * density; // full air resistance parameter
|
|||
|
|
var twt = 9.8 * (rweightv + bweightv); // total weight in newtons
|
|||
|
|
var tres = twt * (gradev * 0.01 + rollingRes); // gravity and rolling resistance
|
|||
|
|
|
|||
|
|
//var powerv = 195;
|
|||
|
|
|
|||
|
|
var v = Newton(a2, headwindv, tres, transv, powerv) * 3.6;// convert to km/h
|
|||
|
|
|
|||
|
|
//Trace.WriteLine($"速度为:{ v }");
|
|||
|
|
|
|||
|
|
var t1 = makeDecimal2(v * (units == 1 ? 1.609 : 1.0));
|
|||
|
|
return double.Parse(t1, CultureInfo.InvariantCulture);
|
|||
|
|
}
|
|||
|
|
private static double Newton(double aero, double hw, double tr, double tran, double p)
|
|||
|
|
{
|
|||
|
|
var vel = 20D;
|
|||
|
|
var MAX = 10;
|
|||
|
|
var TOL = 0.05;
|
|||
|
|
for (int i = 0; i < MAX; i++)
|
|||
|
|
{
|
|||
|
|
var tv = vel + hw;
|
|||
|
|
var aeroEff = (tv > 0.0) ? aero : -aero; // wind in face, must reverse effect
|
|||
|
|
var f = vel * (aeroEff * tv * tv + tr) - tran * p; // the function
|
|||
|
|
var fp = aeroEff * (3.0 * vel + hw) * tv + tr; // the derivative
|
|||
|
|
var vNew = vel - f / fp;
|
|||
|
|
if (Math.Abs(vNew - vel) < TOL)
|
|||
|
|
{
|
|||
|
|
return vNew; // success
|
|||
|
|
}
|
|||
|
|
vel = vNew;
|
|||
|
|
}
|
|||
|
|
return 0.0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private static string makeDecimal2(double value)
|
|||
|
|
{
|
|||
|
|
if (value != 0)
|
|||
|
|
{
|
|||
|
|
var x = Math.Round(value * 100);
|
|||
|
|
int d;
|
|||
|
|
if (x < 100)
|
|||
|
|
{
|
|||
|
|
d = 0;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
d = Convert.ToInt32(Math.Floor(x / 100), CultureInfo.InvariantCulture);
|
|||
|
|
};
|
|||
|
|
var c = x % 100;
|
|||
|
|
var g = (c >= 10) ? "" : "0";
|
|||
|
|
return "" + d + "." + g + c;
|
|||
|
|
}
|
|||
|
|
return value.ToString(CultureInfo.InvariantCulture);
|
|||
|
|
}
|
|||
|
|
}
|