Search code examples
c++qtopengllinear-interpolationqglwidget

Fastest Method to compute Linear interpolation between 2 points in QT using OpenGL


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

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

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

Structure used to compute the calculation:

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

Interpolation Code:

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

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

Visualization of this interpolated points using 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);
        }
    }
}

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.

Output image :

Interpolated Triangle (point view)

Mesh View

Polygon View


Solution

  • 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.