簡體   English   中英

帶有Boost :: Geometry的多邊形交集嚴重降低了性能

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

我有一個粒子系統,我正在使用boost::geometry將橢圓形粒子近似為多邊形,然后使用庫的交集函數來查找重疊區域。 我正在計算一個“內部”和“外部”橢圓(多邊形)區域,以便為每個粒子與粒子之間的相互作用分配一個“電勢”。

我的潛在功能是這樣的:

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

返回的區域是:

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

只要不是每個單元格(粒子)都調用該函數,就可以調用該函數。 以前,使用另一種方法,我的算法的一次迭代對於100個粒子的一次迭代大約需要43毫秒。 現在大約需要1分鍾(!!!),所以我想我做錯了什么!

我僅在Win7 64位下的MSVC2012中對此進行了測試。 我將使用Qt 4.7.4向Linux Mint報告。

編輯:我已經在Linux Mint上使用Qt 4.7.4進行了測試,它運行得非常合理。 每次迭代可能需要90-100毫秒,這很好。 我不知道win7有什么問題...

我已經修好了。 我在Visual Studio中啟動了一個新項目,並復制了所有源文件和頭文件,重新編譯后,一切現在都運行順利。 我想從根本上更改代碼和添加/減少內容一定會產生一些影響...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM