网上很多方法,算法也比较简单,如果真的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;
}
图中是生成缓冲后减去缓冲之前的模型,顺便测了下包围盒。
//添加点、线缓冲
点缓冲其实就是根据点画圆。
TArray<FVector2D> UBFL_3DAnalysis::PointBuffer(FVector2D PList, float BufferValue, float BufferNumber)
{
// 1. vertex set
TArray<FVector2D>out;
int32 BufferNum = BufferNumber;
if (BufferNumber<=1)
{
return out;
}
float a0 = 360 / BufferNumber;//计算角度步长
for (float angle = 0; angle <360; angle += a0)
{
FVector2D p1 = FVector2D(BufferValue * UKismetMathLibrary::DegCos(angle),BufferValue*UKismetMathLibrary::DegSin(angle));
out.Add(p1);
}
return out;
}
//线缓冲
先缓冲我更具面的算法,重新计算起始点和结束点
TArray<FVector2D> UBFL_3DAnalysis::LineBuffer(TArray<FVector2D> PList, float BufferValue)
{
// 1. vertex set
TArray<FVector2D>out;
TArray<FVector2D>reserve;
// 2. edge set and normalize it
TArray<FVector2D> dpList, ndpList;
int count = PList.Num();
for (int i = 0; i < count; i++) {//获取头尾去掉的数据
int next = (i == (count - 1) ? 0 : (i + 1));
dpList.Add(PList[next] - PList[i]);//向量AB
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_in;
if (i == 0 || i == count-1)
{
sinTheta = 1.0;
orientVector = i == count - 1? ndpList[startIndex]:ndpList[endIndex];
temp_in.X = PList[i].X + SAFELINE / sinTheta * orientVector.Y;
temp_in.Y = PList[i].Y + SAFELINE / sinTheta * (orientVector.X*-1.0);
temp_out.X = PList[i].X + (SAFELINE) / sinTheta * (orientVector.Y*-1.0);
temp_out.Y = PList[i].Y + (SAFELINE) / sinTheta * orientVector.X;
}
else
{
temp_out.X = PList[i].X + SAFELINE / sinTheta * orientVector.X;
temp_out.Y = PList[i].Y + SAFELINE / sinTheta * orientVector.Y;
temp_in.X = PList[i].X + (-1.0 * SAFELINE) / sinTheta * orientVector.X;
temp_in.Y = PList[i].Y + (-1.0 * SAFELINE) / sinTheta * orientVector.Y;
}
out.Add(temp_out);
reserve.Insert(temp_in, 0);
}
out.Append(reserve);
return out;
}
最后如果需要把原来面扣掉可能需要计算多边形的正反面,也就是顺逆时针
bool UBFL_3DAnalysis::IsClickWise(TArray<FVector2D>PList)
{
//1. 找到横坐标最小的点,该点一定是多边形的最左侧的点,且为凸点
float min_x = PList[0].X;
int min_idx = 0;
int32 size = PList.Num();
for (int32 i = 1; i < size; i++) {
if (PList[i].X < min_x) {
min_x = PList[i].X;
min_idx = i;
}
}
//2. 判断该点与前一个点构成的向量 和 与后一个点构成的向量的叉积的正负
//2.1 获取该点前一个点和后一个点的下标
int prev_min_idx = 0, next_min_idx = 0;
if (0 == min_idx) { //如果该点是第一个点
prev_min_idx = size - 1;
next_min_idx = min_idx + 1;
}
else if (size - 1 == min_idx) { //如果该点是最后一个点
prev_min_idx = min_idx - 1;
next_min_idx = 0;
}
else {
prev_min_idx = min_idx - 1;
next_min_idx = min_idx + 1;
}
//2.2 计算叉积
double cross = (PList[prev_min_idx].X - PList[min_idx].X) * (PList[next_min_idx].Y - PList[min_idx].Y) -
(PList[next_min_idx].X - PList[min_idx].X) * (PList[prev_min_idx].Y - PList[min_idx].Y);
if (cross > 0)
return true;
else
return false;
}