多边形交叉与Boost::几何严重性能下降
Polygon intersection with Boost::geometry severe performance deterioration
我有一个粒子系统,我使用boost::geometry
将我的椭圆粒子近似为多边形,然后使用库的相交函数找到重叠区域。我正在计算一个"内"answers"外"椭圆(多边形)区域,为每个粒子-粒子相互作用分配一个"势"。
我的势函数是:
double Potential(Cell* current, Cell* next)
{
double areaRep, areaAtt;
double distance = Distance(current,next);
double A1 = current->getLength();
double B1 = A1/2.0;
double theta1 = current->getTheta(); //*180.0/M_PI
double x1 = current->getCurrX();
double y1 = current->getCurrY();
double A2 = next->getLength();
double B2 = A2/2.0;
double theta2 = next->getTheta();
double x2 = next->getCurrX();
double y2 = next->getCurrY();
polygon_2d poly1, poly2, poly3, poly4;
double lamda1, lamda2;
lamda1 = 0.0005; lamda2 = 0.00001;
if(distance < 2.0*1.5*A1) {
ellipse2poly(theta1, A1, B1, x1, y1, &poly1);
ellipse2poly(theta2, A2, B2, x2, y2, &poly2);
areaRep = getOverlapingAreaPoly(poly1,poly2);
ellipse2poly(theta1, 1.5*A1, 1.5*B1, x1, y1, &poly3);
ellipse2poly(theta2, 1.5*A2, 1.5*B2, x2, y2, &poly4);
areaAtt = getOverlapingAreaPoly(poly3, poly4);
return (lamda1*areaRep - lamda2*areaAtt);
}
else
return 0.0;
}
"多边形化"函数为:
int ellipse2poly(double theta, double A1, double B1, double H1, double K1, polygon_2d *po)
{
using namespace boost::geometry;
polygon_2d poly;
const int n = 20;
double angle = theta; // cell orientation
double a = A1; // Long semi-axis length
double b = B1; // short semi-axis length
double xc = H1; // current X position
double yc = K1; // current Y position
if(!n)
{
std::cout << "error ellipse(): n should be >0n" <<std::endl;
return 0;
}
double t = 0;
int i = 0;
double coor[2*n+1][2];
double x, y;
double step = M_PI/(double)n;
double sinphi = sin(angle);
double cosphi = cos(angle);
for(i=0; i<2*n+1; i++)
{
x = xc + a*cos(t)*cosphi - b*sin(t)*sinphi;
y = yc + a*cos(t)*sinphi + b*sin(t)*cosphi;
coor[i][0] = x;
coor[i][1] = y;
t += step;
}
assign_points(poly, coor);
correct(poly);
*po = poly;
return 1;
}
返回区域为:
double getOverlapingAreaPoly(polygon_2d poly, polygon_2d poly2)
{
point_2d cent; //centre of overlaping area
double overAreaPoly = 0.0;
typedef std::vector<polygon_2d > polygon_list;
polygon_list v;
intersection(poly,poly2,v);
for (polygon_list::const_iterator it = v.begin(); it != v.end(); ++it)
{
centroid(*it, cent);
overAreaPoly = area(*it);
}
return overAreaPoly;
}
该函数对每个细胞(粒子)调用,只要不是对同一个细胞(粒子)调用。以前,使用另一种方法,我的算法一次迭代大约需要43毫秒,一次迭代100个粒子。现在大约需要1分钟(!!),所以我想我做了一些可怕的错误!
我只在win7 64位下的MSVC2012中测试了这一点。我将报告Linux Mint与Qt 4.7.4。
编辑:我在Linux Mint上测试了Qt 4.7.4,它运行得非常合理;每次迭代可能需要90-100毫秒,这很好。我不知道win7怎么了…
我实际上已经修复了它。我在Visual Studio中开始了一个新项目,复制了所有的源文件和头文件,重新编译,现在一切都运行得很顺利。我想从根本上改变代码和增加/减少东西一定会有一些影响…
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- OpenMP阵列性能较差
- 递归列出所有目录中的C++与Python与Ruby的性能
- 函数何时会在c++中包含stack_Unwind_Resume调用
- 大小相等但成员数量不同的结构之间的性能差异
- 为什么constexpr的性能比正常表达式差
- Python中的for循环与C++有何不同
- 在类中使用随机生成器时出现性能问题
- 在main()之外初始化std::vector会导致性能下降(多线程)
- 海湾合作委员会 ARM 性能下降
- GCC 和 Clang 代码性能的巨大差异
- 在容量内调整矢量大小时的性能影响
- 在C++中释放内存期间,迭代器与指针有何不同
- 了解算法的性能差异(如果以不同的编程语言实现)
- 未达到的情况会影响开关外壳性能
- QStringList vs list<shared_ptr<QString>> 性能比较C++
- 擦除删除成语的性能增益从何而来
- 递归函数严重降低性能
- 多边形交叉与Boost::几何严重性能下降
- openMp:调用动态数组的共享引用时性能严重损失