Gl Graph

Gl Graph Source of mapabstract.cpp


#include "mapabstract.h"
 
MapAbstract::MapAbstract() : DrawableDataReader()
{
    rawdata.reserve(5000);
}
 
MapAbstract::MapAbstract(std::string * filename, std::string * format, int maxline,std::unordered_map<std::string, std::string> * args)
    : DrawableDataReader(filename, format, maxline,args)
{
    rawdata.reserve(maxline);
    glquaddata=GlQuadData(args);
}
 
 
void MapAbstract::addData(float x, float y, float z)
{
    rawdata.push_back(Vec3f(x, y, z));
}
 
int MapAbstract::getElementNb() const
{
    lock();
    int nb = rawdata.size();
    unlock();
    return nb;
}
 
void MapAbstract::init()
{
    glquaddata.init(this->glVersion);
}
 
 
 
void MapAbstract::draw()
{
    lock();
    glquaddata.draw();
    unlock();
}
 
void MapAbstract::print()
{
    unsigned int size = rawdata.size();
    for (unsigned int i = 0; i < size; i++)
    {
        std::cout << "x [" << i << "]=" << rawdata[i].getX() << std::endl;
        std::cout << "y [" << i << "]=" << rawdata[i].getY() << std::endl;
        std::cout << "z [" << i << "]=" << rawdata[i].getZ() << std::endl;
    }
}
 
void MapAbstract::onFileOpen()
{
 
}
 
bool MapAbstract::onLineRead(std::string line)
{
    bool ret=true;
    float x, y, z;
    const int nbargs = sscanf(line.c_str(), getFormat()->c_str(), &x, &y, &z);
 
    if (nbargs == 3)
        addData(x, y, z);
    else
        ret= false;
 
    return ret;
}
 
 
 
bool MapAbstract::isInMap(int x, int y)
{
    return 0 <= x && x < xsize && 0 <= y && y<ysize;
}
 
 
void MapAbstract::processVertex(Vec3f &vecin,Vec3f &vecout)
{
    vecout=vecin;
}
 
void MapAbstract::processVertexColor(Vec3f &vecin,Vec3f &vecout)
{
    float c=((vecin.getZ()-min.getZ())/(max.getZ()-min.getZ()));
    vecout=Vec3f(c,c,c);
}
 
 
 
void MapAbstract::setNormalVertex(int x, int y,Vec3f &vec)
{
 
    //     pt1
    //pt2  pt3 pt4
    //     pt5
    if (isInMap(x, y))
    {
        int index3 = getIndex( x,  y);
        Vec3f X;
        Vec3f X1;
        Vec3f X2;
        Vec3f Y;
        Vec3f Y1;
        Vec3f Y2;
        Vec3f pt1;
        Vec3f pt2;
        Vec3f pt3 = rawdata[index3];
        Vec3f pt4;
        Vec3f pt5;
        Vec3f cross;
 
        // X axes
        if (isInMap(x - 1, y))
        {
            int index2 = getIndex(x - 1, y);
            pt2 = rawdata[index2];
            X1.sub(pt2, pt3);
        }
        else
            X1 = Vec3f(0, 0, 0);
        if (isInMap(x + 1, y))
        {
            int index4 = getIndex(x + 1, y);
            pt4 = rawdata[index4];
            X2.sub(pt3, pt4);
        }
        else
            X2 = Vec3f(0, 0, 0);
        X.add(X1, X2);
 
        // Y axes
        if (isInMap(x, y - 1))
        {
            int index1 = getIndex(x, y - 1);
            pt1 = rawdata[index1];
            Y1.sub(pt1, pt3);
        }
        else
            Y1 = Vec3f(0, 0, 0);
        if (isInMap(x, y + 1))
        {
            int index5 = getIndex(x, y + 1);
            pt5 = rawdata[index5];
            Y2.sub(pt3, pt5);
        }
        else
            Y2 = Vec3f(0, 0, 0);
        Y.add(Y1, Y2);
 
 
        cross.cross(X, Y);
        cross.normalize();
        vec=cross;
    }
    else
        std::cout << "pushNormalVertex data layout error : size are not properly define" << std::endl;
}
 
 
 
 
 
 
void MapAbstract::setVertex(int x, int y,Vec3f &vec)
{
    if (isInMap(x, y))
    {
        Vec3f rawvec=rawdata[getIndex( x,  y)];
        processVertex(rawvec,vec);
    }
    else
        std::cout << "data layout error : size are not properly define" << std::endl;
}
 
void MapAbstract::setColor(int x, int y,Vec3f &vec)
{
    if (isInMap(x, y))
    {
        processVertexColor(rawdata[getIndex( x,  y)],vec);
    }
    else
        std::cout << "data layout error : size are not properly define" << std::endl;
}
 
 
 
 
 
 
void MapAbstract::pushMapVertex()
{
    for (int x = 0; x < xsize - 1; x++)
        for (int y = 0; y < ysize - 1; y++)
        {
            GlQuad quad;
            setVertex(x, y,quad.pts1);
            setVertex(x + 1, y,quad.pts2);
            setVertex(x + 1, y + 1,quad.pts3);
            setVertex(x, y + 1,quad.pts4);
            quadlist.push_back(quad);
            glquaddata.addQuadVertexData(quad);
        }
}
 
void MapAbstract::pushMapColor()
{
    for (int x = 0; x < xsize - 1; x++)
        for (int y = 0; y < ysize - 1; y++)
        {
            GlColorQuad color;
            setColor(x, y,color.colorPts1);
            setColor(x + 1, y,color.colorPts2);
            setColor(x + 1, y + 1,color.colorPts3);
            setColor(x, y + 1,color.colorPts4);
            glquaddata.addQuadColorData(color);
        }
}
 
void MapAbstract::pushMapNormalVertex()
{
    for (int x = 0; x < xsize - 1; x++)
        for (int y = 0; y < ysize - 1; y++)
        {
            GlNormalQuad normal;
            setNormalVertex(x, y,normal.normalPts1);
            setNormalVertex(x + 1, y,normal.normalPts2);
            setNormalVertex(x + 1, y + 1,normal.normalPts3);
            setNormalVertex(x, y + 1,normal.normalPts4);
            glquaddata.addQuadNormalData(normal);
        }
}
 
void MapAbstract::pushMap()
{
    pushMapVertex();
    pushMapColor();
    pushMapNormalVertex();
}
 
unsigned int MapAbstract::getIndex(int x, int y)
{
    unsigned int index;
    if(layoutXY)
        index = y * xsize + x;
    else
        index = x * ysize + y;
    return index;
}
 
void MapAbstract::buildMap()
{
    lock();
    float x = rawdata[0].getX();
    float y = rawdata[0].getY();
    xsize = 1;
    xsize = 1;
    unsigned int s=rawdata.size();
    if (x == rawdata[1].getX()) // data layout : row fist (ie X is constant )
    {
        for (unsigned int i = 1; i < s; i++)
            if (x == rawdata[i].getX())
                xsize++;
            else
                break;
        ysize = s / xsize;
        layoutXY=true;
        pushMap();
    }
    else if (y == rawdata[1].getY())// data layout : column fist (ie Y is constant )
    {
        for (unsigned int i = 1; i < s; i++)
            if (y == rawdata[i].getY())
                ysize++;
            else
                break;
        xsize = s / ysize;
        layoutXY=false;
        pushMap();
    }
    unlock();
}
 
void MapAbstract::onFileClose()
{
    updateMinMax();
    buildMap();
    setChanged();
}