Gl Graph

Gl Graph Source of lines3d.cpp


#include "lines3d.h"
#include "pthread.h"
#include <unistd.h>
 
Lines3D::Lines3D()   : DrawableDataReader()
{
 
}
 
Lines3D::Lines3D(std::string * filename, std::string * format, int maxline,std::unordered_map<std::string, std::string> * args)    : DrawableDataReader(filename,format,maxline,args)
{
    gllinedata=GlLineStripData(args);
}
 
void Lines3D::updateMinMax(Vec3f &pts)
{
    if (ptsdata.size()==0)
    {
        vmin = pts;
        vmax = pts;
    }
    else
    {
        vmin.setMin(vmin,pts);
        vmax.setMax(vmax,pts);
    }
}
 
 
void Lines3D::addData(float x, float y, float z)
{
    setChanged();
    Vec3f pts(x,y,z);
    gllinedata.addLineVertexData(pts);
    ptsdata.push_back(pts);
    updateMinMax(pts);
}
 
void Lines3D::addData(float y)
{
    addData(ptsdata.size(), y, 0);
}
 
int Lines3D::getElementNb() const {
    lock();
    int nb=ptsdata.size();
    unlock();
    return nb;
}
 
void Lines3D::init()
{
    gllinedata.init(this->glVersion);
}
 
 
 
void Lines3D::draw()
{
    lock();
    gllinedata.draw();
    unlock();
}
 
void Lines3D::print()
{
    int size = ptsdata.size();
    for (int i = 0; i < size; i++)
    {
        std::cout << "x [" << i << "]=" << ptsdata[i].getX() << std::endl;
        std::cout << "y [" << i << "]=" << ptsdata[i].getY() << std::endl;
        std::cout << "z [" << i << "]=" << ptsdata[i].getZ() << std::endl;
    }
}
 
 
 
void Lines3D::onFileOpen() {
 
}
 
bool Lines3D::onLineRead(std::string  line) {
    bool ret= true;
    float x, y, z;
    const int nbargs = sscanf(line.c_str(), getFormat()->c_str(), &x, &y, &z);
    lock();
    if (nbargs == 1)
        addData(x);
    else if (nbargs == 3)
        addData(x, y, z);
    else
        ret= false;
    unlock();
    return ret;
}
 
void Lines3D::updateColor() {
    int size = ptsdata.size();
    lock();
    for (int i = 0; i < size; i++) {
        Vec3f color;
        color.setX (  (vmax.getX() -ptsdata[i].getX()) / (vmax.getX()-vmin.getX()));
        color.setY (  (vmax.getY() -ptsdata[i].getY()) / (vmax.getY()-vmin.getY()));
        color.setZ (  (vmax.getZ() -ptsdata[i].getZ()) / (vmax.getZ()-vmin.getZ()));
        gllinedata.addLineColorData(color);
    }
    unlock();
}
 
void Lines3D::onFileClose() {
    updateColor();
    setChanged();
}