using Assets.Scripts.Apis.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Assets.Scenes.Ride.Scripts.Model { public class Route { /// /// 路线实例 /// public MapRoute RouteInstance { get; private set; } /// /// 从服务端获取的Json数据 /// public MapDataModel JsonData { get; set; } /// /// 当前路书的排行榜信息 /// public List RecordRankings { get; set; } private List _Point = null; /// /// 点位信息 /// public List Point { get { if (_Point == null) { _Point = JsonData.List.Select(n => n.Point.Reverse().ToArray()).ToList(); } return _Point; } } private List _Elevation { get; set; } /// /// 海拔信息 /// public List Elevation { get { if (_Elevation == null) { _Elevation = JsonData.List.Select(n => n.Elevation).ToList(); } return _Elevation; } } private List _SlopeGrade { get; set; } /// /// 坡度信息 /// public List SlopeGrade { get { if (_SlopeGrade == null) { //_SlopeGrade = CalPatch(); _SlopeGrade = JsonData.List.Select(d => d.Grade).ToList(); } return _SlopeGrade; } } private List _StepDistance { get; set; } /// /// 每两点的距离 /// public List StepDistance { get { if (_StepDistance == null) { _StepDistance = JsonData.List.Select(n => n.Distance).ToList(); } return _StepDistance; } } public double? _TotalDistance { get; set; } public double TotalDistance { get { if (_TotalDistance == null) { _TotalDistance = GetTotalDistance(); } return (double)_TotalDistance; } } /// /// 每一小步到起点的距离 /// private List _StepToStartDistance; public List StepToStartDistance { get { if (_StepToStartDistance == null) { _StepToStartDistance = GetStepToStartDistance(); } return _StepToStartDistance; } } public Route(MapDataModel data) { JsonData = data; } public Route(MapDataModel data, MapRoute mapRoute) { RouteInstance = mapRoute; JsonData = data; } /// /// km /// /// public double GetTotalDistance() { var total = 0d; for (int i = 0; i < JsonData.List.Count - 1; i++) { var current = JsonData.List[i]; var next = JsonData.List[i + 1]; total += TurfHelper.GetDistances(new double[] { current.Point[1], current.Point[0] }, new double[] { next.Point[1], next.Point[0] }); } return total / 1000; } /// /// 根据下标获取海拔 /// /// public double GetElevationByIndex(int index) { if (index > this.Elevation.Count) { return this.Elevation.Last(); } else { return this.Elevation[index]; } } /// /// 根据下标获取距离的小段 /// /// public double GetDistanceStepByIndex(int index) { if (index > this.StepDistance.Count) { return this.StepDistance.Last(); } else { return this.StepDistance[index]; } } /// /// 根据索引获取坡度 /// /// 索引 /// 是否需要平均坡度 /// public double GetPatchByIndex(int index, bool average, double endDistance = 0, double cacheDistance = 5) { double slop = 0; if (average) { //每一小段的距离 double step = GetDistanceStepByIndex(index); //每一段到起点的距离 double stepToStart = GetStepToStartDistanceByIndex(index); //当前骑行距离与前一段的绝对值之差 double absoluteValue = step - (stepToStart - endDistance); //绝对值之差是否大于多少米,如果大于则返回原始坡度,如果小于则返回平均坡度 if (absoluteValue < cacheDistance && cacheDistance < step) { if (index >= this.SlopeGrade.Count - 1) { double slope = this.SlopeGrade.Last(); //if (slope > 10) //{ // slop = 10; //} //else if (slope < -10) //{ // slop = -10; //} //else { slop = slope / 2; } } else { double slope = this.SlopeGrade[index]; //if (slope > 10) //{ // slop = 10; //} //else if (slope < -10) //{ // slop = -10; //} //else { slop = slope / 2; } } } } else { if (index >= this.SlopeGrade.Count - 1) { slop = this.SlopeGrade.Last(); } else { slop = this.SlopeGrade[index]; } } //if (slop > 10) //{ // return 10; //} //if (slop < -10) //{ // return -10; //} return slop; } /// /// 根据距离获取点位下标 /// /// public int GetStepIndex(double distance) { distance *= 1000;//将距离转化为米 var totalDistance = 0D; var index = 0; foreach (var item in this.StepDistance) { totalDistance += item; if (totalDistance > distance) { return index; } index++; } if (distance > this.StepDistance[this.StepDistance.Count - 1]) { return this.StepDistance.Count - 1; } return 0; } /// /// 通过索引获取当前到起点的距离(单位m) /// /// /// public double GetStepToStartDistanceByIndex(int index) { if (index >= this.StepToStartDistance.Count - 1) { return this.StepToStartDistance.Last(); } else { return this.StepToStartDistance[index]; } } /// /// 计算每一段路程的斜率 /// private List CalPatch() { if (this.Elevation != null) { List grade = new List(); for (int i = 0; i < this.Point.Count - 1; i++) { var a = this.Elevation[i + 1] - this.Elevation[i]; if (a == 0) { grade.Add(0); continue; } //勾股定理 var c = this.StepDistance[i];//如果车停了下来,c为0 if (c == 0 && i > 0) { grade.Add(grade[grade.Count - 1]); } else { var b = Math.Sqrt(c * c - a * a); if (b == 0)//如果b等于,这就是垂直的墙壁 { grade.Add(grade[grade.Count - 1]); } else { grade.Add(a / b * 100); } } } //检是否有NaN的值 for (int i = 0; i < grade.Count; i++) { if (double.IsNaN(grade[i]) && i != 0) { grade[i] = grade[i - 1]; } else if (double.IsNaN(grade[i]) && i == 0) { grade[i] = 0; } } return grade; } return null; } /// /// 获取每一段到起点的距离 /// /// private List GetStepToStartDistance() { if (StepDistance != null) { List stepToStartDis = new List(); double start = 0; for (int i = 0; i < StepDistance.Count; i++) { start += StepDistance[i]; stepToStartDis.Add(start); } return stepToStartDis; } return null; } } }