Sid133
Sid133

Reputation: 364

OpenGL - Plotting Points from external file

AM trying to plot some points from a CSV file. Since the file size is large(>2GB), loading the file contents to the vector std::vector<std::vector<std::string> >parsedCsv threw an out of memory exception.

So I thought, instead of reading the file to a vector and then plotting it, Is is possible to plot it directly from the CSV. Am looking for some modification below on glVertex3f(x,y,z)

    std::ifstream  data("D:\\Files\\Dummy2.csv");
    std::string line;
    while (std::getline(data, line))
    {
        std::stringstream lineStream(line);
        std::string cell;
        std::vector<std::string> parsedRow;
        while (std::getline(lineStream, cell, ','))
        {
            glBegin(GL_POINTS);
            glColor3f(0.0f, 1.0f, 1.0f);
            glVertex3f(----how to represent the points--?)
            glEnd();
        }

The CSV file is already of desired format :

x1,y1,z1    
x2,y2,z2    
x3,y3,z3    
-------
----
--  

Any suggestions ?

Upvotes: 1

Views: 645

Answers (1)

Rabbid76
Rabbid76

Reputation: 210877

You can use stof to convert the string values to floating point numbers. Push the numbers of the cells to the vector. The components of the vertex vertex coordinate are stored in the vector and can be be drawn by glVertex3fv:

std::ifstream data("D:\\Files\\Dummy2.csv");
std::string line;
while (std::getline(data, line))
{
    std::stringstream lineStream(line);

    std::string cell;
    std::vector<float> parsedRow;
    while (std::getline(lineStream, cell, ','))
        parsedRow.push_back(std::stof(cell));

    if (parsedRow.size() == 3)
    {
        glBegin(GL_POINTS);
        glColor3f(0.0f, 1.0f, 1.0f);
        glVertex3fv(parsedRow.data());
        glEnd();
    }
}

Note, if stof can not performed a conversion, then an invalid argument exception is thrown.

Upvotes: 1

Related Questions