使用OpenGL计算QT中2个点之间的线性插值的最快方法 [英] Fastest Method to compute Linear interpolation between 2 points in QT using OpenGL

查看:628
本文介绍了使用OpenGL计算QT中2个点之间的线性插值的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试借助顶点坐标对三角形进行插值.

I'm trying to interpolate a triangle with the help of vertex coordinates.

a
 |\
 | \ 
 |  \
 |    \
b|_ _ _ \c

我按(b,a),(a,c)和(c,b)的顺序对顶点进行插值. 这里的a,b和c是具有颜色值的3维坐标.

I'm interpolating the vertices in this order (b,a),(a,c)and (c,b). Here the a,b and c are the 3 dimensional coordinates with a color value.

a = (x1,y1,z1,c1);
b = (x2,y2,z2,c2);
c = (x3,y3,z3,c3);

用于计算计算的结构:

struct pointsInterpolateStruct{
    QList<double> x,y,z;
    QList<double> r, g, b, clr;
    void clear() {
        x.clear();
        y.clear();
        z.clear();
        r.clear();
        g.clear();
        b.clear();
        clr.clear();
    }
};

插值代码:

QList<double> x,y,z,clrs;

上面提到的列表已用于从包含a,b和c坐标的文件中读取值.

This above mentioned lists has been used to read the values from a file which contains the coordinates of a,b and c.

/**
     void interpolate();
     @param1 ipts is an object for the point interpolation struct which holds the x,y,z and color
     @param2  idx1 is the point A
     @param 3idx2 is the point B
     @return returns the interpolated values after filling the struct pointsInterpolateStruct
   */

void GLThread::interpolate(pointsInterpolateStruct *ipts,int idx1, int idx2) {
    int ipStep = 0;
    double delX, imX,iX,delY,imY,iY,delZ,imZ,iZ,delClr,imC,iC;
    ipStep = 5; // number of points needed between the 2 points
    delX = imX = iX = delY = imY = iY = delZ = imZ = iZ = delClr = imC = iC = 0;
    delX = (x.at(idx2) - x.at(idx1));
    imX = x.at(idx1);
    iX = (delX / (ipStep + 1));

    delY = (y.at(idx2) - y.at(idx1));
    imY = aParam->y.at(idx1);
    iY = (delY / (ipStep + 1));

    delZ = (z.at(idx2) - z.at(idx1));
    imZ = z.at(idx1);
    iZ = (delZ / (ipStep + 1));

    delClr = (clrs.at(idx2) - clrs.at(idx1));
    imC = clrs.at(idx1);
    iC = (delClr / (ipStep + 1));
    ipts->clear();
    int i = 0;
    while(i<= ipStep) {
        ipts->x.append((imX+ iX * i));
        ipts->y.append((imY+ iY * i));
        ipts->z.append((imZ+ iZ * i));
        ipts->clr.append((imC + iC * i));
        i++;
    }
}*

使用OpenGL可视化这些插值点:

所有点都填充到顶点和颜色缓冲区中,我正在使用以下格式绘制它.可视化非常快,即使是较大的点.

All the points are filled to vertices and color buffers and I'm drawing it using the below format. Visualization is very fast even for larger points.

void GLWidget::drawInterpolatedTriangle(void) {
            glEnableClientState(GL_COLOR_ARRAY);
            glEnableClientState(GL_VERTEX_ARRAY);
            glColorPointer(3, GL_FLOAT, 0, clr);
            glVertexPointer(3, GL_FLOAT, 0, vrt);
            glPushMatrix();
            glDrawArrays(GL_POLYGON, 0, vrtCnt);
            glPopMatrix();
            glDisableClientState(GL_VERTEX_ARRAY);
            glDisableClientState(GL_COLOR_ARRAY);
        }
    }
}

现在一切正常.我正在获得所需的输出.但是问题是,当我尝试对n个三角形(例如n = 40,000)执行相同操作时,即使我在QThread中调用了此函数,应用程序也崩溃了,并且我发现此方法效率不高.该方法需要大量时间进行计算.

Now everything working fine. I'm getting the desired output. But the problem is when I'm trying to do the same for 'n' number of triangles (say n = 40,000), the application gets crashed even if I called this function in a QThread and I found that this method is not an efficient method as it takes lot of time for computation.

请提出一种乐观的方法来执行此过程,以便以良好的性能获得更好的结果.

Please suggest an optimistic way to do this process so that I can achieve better results at good performance.

输出图像:

插值三角形(点视图)

网格视图

多边形视图

推荐答案

检查了应用程序使用的内存后,我发现程序的列表和数组中已存储了大量不需要的数据(即清除列表x,y,z,r,g,b和clrs以pointsInterpolateStruct表示).我已经立即清除了所有不需要的/未使用的数据,并尝试使用较大的三角形运行该应用程序.现在,我可以获得更好的性能.在可视化过程中,我没有做任何更改.

After examining the memory used by the application, I found that there's large number of unwanted data has been stored in the list and arrays in my program (i.e., clearing the list x,y,z,r,g,b and clrs in pointsInterpolateStruct). I have cleared all the unwanted / unused data instantly and tried to run the application with larger triangles. Now I can achieve better performance. I didn't changed anything in visualization process.

这篇关于使用OpenGL计算QT中2个点之间的线性插值的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆