好久没写了.最近在尝试重写lgsvl导入地图数据的方式,地图同学提供的opendrive车道线计算不准,所以直接让他们导出经纬度的高精地图json数据,但是这种数据只有车道边界线,没有车道中心线, 基于只是想小改而非大改的前提下,还是要算出车道中心线.搞个小demo传上来,代码写的很拙劣,凑合看吧
private List<Vector3> Fix(List<Vector3> _long, List<Vector3> _short)
{
if (_long.Count < 2 || _short.Count < 2) return new List<Vector3>();
float LongDis = 0, ShortDis = 0;
for (int i = 0; i < _long.Count - 1; i++)
{
LongDis += Vector3.Distance(_long[i], _long[i + 1]);
}
for (int i = 0; i < _short.Count - 1; i++)
{
ShortDis += Vector3.Distance(_short[i], _short[i + 1]);
}
List<Vector3> center = new List<Vector3>
{
(_long[0] + _short[0]) / 2,
(_long[_long.Count-1] + _short[_short.Count-1]) / 2,
};
if (_long.Count > 2)
{
float longdis=0;
for (int i = 1; i < _long.Count - 1; i++)
{
longdis += Vector3.Distance(_long[i - 1], _long[i]) / LongDis * ShortDis;
float shortdis = 0;
int j = 1;
while (shortdis < longdis)
{
var dis = Vector3.Distance(_short[j - 1], _short[j]);
if (shortdis + dis > longdis)
{
var offset = shortdis + dis - longdis;
//求出对应比例的中间点
var target = (_short[j] - _short[j - 1]) / (Vector3.Distance(_short[j], _short[j - 1])) * offset + _short[j - 1];
center.Insert(center.Count - 1, (_long[i] + target) / 2);
break;
}
shortdis += dis;
j++;
}
}
}
return center;
}