简体   繁体   English

带有Boost :: Geometry的多边形交集严重降低了性能

[英]Polygon intersection with Boost::geometry severe performance deterioration

I have a particle system and I am using boost::geometry to approximate my elliptical particles as polygons and then use the intersection function of the library to find the overlap area. 我有一个粒子系统,我正在使用boost::geometry将椭圆形粒子近似为多边形,然后使用库的交集函数来查找重叠区域。 I am calculating an "inner" and "outer" ellipse(polygon) area to assign a "potential" for each particle-particle interaction. 我正在计算一个“内部”和“外部”椭圆(多边形)区域,以便为每个粒子与粒子之间的相互作用分配一个“电势”。

My potential function is this: 我的潜在功能是这样的:

    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;
}

The "polygonizing" function is: “多边形化”功能是:

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 >0\n" <<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;
}

And the returned area is: 返回的区域是:

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;
}

The function is called for every cell (particle) as long as it is not for the same one. 只要不是每个单元格(粒子)都调用该函数,就可以调用该函数。 Previously, using another method, one iteration of my algorithm would take approximately 43 ms for one iteration for 100 particles. 以前,使用另一种方法,我的算法的一次迭代对于100个粒子的一次迭代大约需要43毫秒。 Now it takes approximately 1 min(!!!), so I guess I have done something horribly wrong! 现在大约需要1分钟(!!!),所以我想我做错了什么!

I have tested this only in MSVC2012 under win7 64bit. 我仅在Win7 64位下的MSVC2012中对此进行了测试。 I will report back for Linux Mint with Qt 4.7.4. 我将使用Qt 4.7.4向Linux Mint报告。

EDIT: I have tested on Linux Mint with Qt 4.7.4 and it is running very reasonably; 编辑:我已经在Linux Mint上使用Qt 4.7.4进行了测试,它运行得非常合理。 maybe 90-100 ms per iteration which is fine. 每次迭代可能需要90-100毫秒,这很好。 I don't know what is wrong in win7... 我不知道win7有什么问题...

I have actually fixed it. 我已经修好了。 I started a new project in Visual Studio and copied all source and header files, recompiled and everything runs smoothly now. 我在Visual Studio中启动了一个新项目,并复制了所有源文件和头文件,重新编译后,一切现在都运行顺利。 I guess radically changing code and adding / subtracting stuff must have some impact... 我想从根本上更改代码和添加/减少内容一定会产生一些影响...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM