网上很多方法,算法也比较简单,如果真的gis缓冲算法有很多其他参数
这里用比较简单的方法,把c++的改成ue的c++,顺便留下自己的学习记录
//等距离缓冲
TArray<FVector2D> UBFL_SpatialAnalysis::PolygonBuffer(TArray<FVector2D> PList , float BufferValue)
{
// 1. vertex set
TArray<FVector2D>out;
// 2. edge set and normalize it
TArray<FVector2D> dpList, ndpList;
int count = PList.Num()-1;
for (int i = 0; i < count; i++) {
int next = (i == (count - 1) ? 0 : (i + 1));
dpList.Add(PList[next] - PList[i]);
float unitLen = 1.0f / FMath::Sqrt(FVector2D::DotProduct(dpList[i],dpList[i]));
ndpList.Add(dpList[i] * unitLen);
}
// 3. compute Line
float SAFELINE = BufferValue;//负数为内缩, 正数为外扩。 需要注意算法本身并没有检测内缩多少后折线会自相交,那不是本代码的示范意图
for (int i = 0; i < count; i++) {
int startIndex = (i == 0 ? (count - 1) : (i - 1));
int endIndex = i;
float sinTheta = ndpList[startIndex].X * ndpList[endIndex].Y - ndpList[endIndex].X * ndpList[startIndex].Y;
FVector2D orientVector = ndpList[endIndex] - ndpList[startIndex];//i.e. PV2-V1P=PV2+PV1
FVector2D temp_out;
temp_out.X = PList[i].X + SAFELINE / sinTheta * orientVector.X;
temp_out.Y = PList[i].Y + SAFELINE / sinTheta * orientVector.Y;
out.Add(temp_out);
}
return out;
}
![](https://img.haomeiwen.com/i14201766/ce69d8151fbf084a.png)
图中是生成缓冲后减去缓冲之前的模型,顺便测了下包围盒
网友评论