Gl Graph

Gl Graph Source of mapquad.cpp


#include "mapquad.h"
 
MapQuad::MapQuad() : DrawableDataReader()
{
    data.reserve(5000);
}
 
MapQuad::MapQuad(std::string * filename, std::string * format, int maxline,std::unordered_map<std::string, std::string> * args)
    : DrawableDataReader(filename, format, maxline,args)
{
    data.reserve(maxline);
    glquaddata=GlQuadData(args);
}
 
 
void MapQuad::addData(std::vector<float> line)
{
    data.push_back(line);
}
 
int MapQuad::getElementNb() const
{
    lock();
    int nb = data.size();
    unlock();
    return nb;
}
 
 
 
void MapQuad::init()
{
    glquaddata.init(this->glVersion);
}
 
 
 
void MapQuad::draw()
{
    lock();
    glquaddata.draw();
    unlock();
}
 
void MapQuad::print()
{
    std::cout  << "MapQuad:" ;
    unsigned int sizei = data.size();
    for (unsigned int i = 0; i < sizei; i++)
    {
        unsigned int sizej = data[i].size();
        for (unsigned int j = 0; j < sizej; j++)
        {
            std::cout  << data[i][j] << " ";
        }
        std::cout  << std::endl;
    }
}
 
void MapQuad::onFileOpen()
{
 
}
 
static float getVal(std::string & element)
{
    float val;
    sscanf ( element.c_str(), "%f", &val );
    return val;
}
 
 
static void remove_repeated_space(std::string & l)
{
    // remove repeated space
    unsigned int len=l.size();
    for (unsigned int i=0; i<len; i++)
        if (l[i]==' ')
            for (unsigned int k=i+1; k<len; k++)
                if (l[k]==' ')
                    l.erase(k);
                else
                    break;
}
 
static void remove_useless_space(std::string & l)
{
    // remove starting space
    if (l[0]==' ')
        l.erase(0);
    // remove ending space
    if (l[l.size()-1]==' ')
        l.erase(l.size()-1);
}
 
 
bool MapQuad::onLineRead(std::string line)
{
    bool ret=true;
    std::vector<float> floatline;
 
    remove_repeated_space(line);
    remove_useless_space(line);
    // read string
    std::string::size_type start=0;
    std::string::size_type end=line.find(" ",0);
 
    while (end!=std::string::npos)
    {
        std::string element = line.substr(start, end-start);
        floatline.push_back(getVal(element));
        start=end+1;
        end=line.find(" ",end+1);
    }
    std::string element = line.substr(start, line.size()-start);
    floatline.push_back(getVal(element));
 
    addData(floatline);
    return ret;
}
 
 
float MapQuad::getVertexColor(Vec3f &vec)
{
    return max<float>(0.08,(vec.getZ()-vmin.getZ())/(vmax.getZ()-vmin.getZ()));
}
 
void MapQuad::pushQuad(Vec3f &pts,float sizex,float sizey)
{
    GlQuad quad;
    GlColorQuad colorQuad;
    quad.pts1=pts+Vec3f(sizex,sizey,0);
    quad.pts2=pts+Vec3f(-sizex,sizey,0);
    quad.pts3=pts+Vec3f(-sizex,-sizey,0);
    quad.pts4=pts+Vec3f(sizex,-sizey,0);
 
    float cpts1=getVertexColor(quad.pts1);
    float cpts2=getVertexColor(quad.pts2);
    float cpts3=getVertexColor(quad.pts3);
    float cpts4=getVertexColor(quad.pts4);
 
    colorQuad.colorPts1=Vec3f(cpts1,cpts1,cpts1);
    colorQuad.colorPts2=Vec3f(cpts2,cpts2,cpts2);
    colorQuad.colorPts3=Vec3f(cpts3,cpts3,cpts3);
    colorQuad.colorPts4=Vec3f(cpts4,cpts4,cpts4);
 
 
    glquaddata.addQuadVertexData(quad);
    glquaddata.addQuadColorData(colorQuad);
 
}
 
void MapQuad::buildMap()
{
    lock();
    unsigned int sizei = data.size();
    for (unsigned int i = 0; i < sizei; i++)
    {
        unsigned int sizej = data[i].size();
        for (unsigned int j = 0; j < sizej; j++)
        {
 
            Vec3f  pts(i+1,j+1,data[i][j]);
 
            pushQuad(pts,0.5f,0.5f);
        }
    }
    unlock();
}
 
void MapQuad::onFileClose()
{
    updateMinMax();
    buildMap();
    setChanged();
}