I am doing a project on CalculiX data visualizor,using Qt.I 've to draw the structure and after giving force the displacement should be shawn as variation in color.I chose HSV coloring,but while executing I got an error message:"QColor::from Hsv:HSV parameters out of range".The code is:
DataViz1::DataViz1(QWidget *parent) :
QWidget(parent),
ui(new Ui::DataViz1)
{
DArea = new QGLScreen(this);
DArea-setGeometry(QRect(10,10,700,600));
//TODO This values are feeded by user
dfile="/home/41407/color.txt";//input file with displacement
mfile="/home/41407/mesh21.txt";//input file
nodeId="*NODE";
elId="*ELEMENT";
DataId="displ";
parseMfile();
parseDfile();
DArea->Nodes=Nodes;
DArea->Elements=Elements;
DArea->Data=Data;
DArea->fillColorArray();
//printf("Colr is %d",DArea->pickColor(-11.02,0));fflush(stdout);
ui->setupUi(this);
}
DataViz1::~DataViz1()
{
delete ui;
}
void DataViz1::parseMfile()
{
QFile file(mfile);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
int node_end=0;
QTextStream in(&file);
in.skipWhiteSpace();
while (!in.atEnd()) {
QString line = in.readLine();
if(line.startsWith(nodeId))//Node block in Mfile
{
while(1)
{
line = in.readLine();
if(line.startsWith(elId))
{
break;
}
Nodes<
while(1)
{
line = in.readLine();
Elements<<line;
//printf("Element is %s\n",line.toLocal8Bit().constData());fflush(stdout);
if(in.atEnd()) break;
}
}
}
}
void DataViz1::parseDfile()
{
QFile file(dfile);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
int node_end=0;
QTextStream in(&file);
in.skipWhiteSpace();
while (!in.atEnd()) {
QString line = in.readLine();
if(line.startsWith(DataId))
{
continue;
}
line = in.readLine();
Data<
}
/......................................................................../
include "qglscreen.h"
include
GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat LightPosition[]= { 0.0f, 0.0f, 2.0f, 1.0f };
QGLScreen::QGLScreen(QWidget *parent):QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
{
clearColor = Qt::black;
xRot = 0;
yRot = 0;
zRot = 0;
ifdef QT_OPENGL_ES_2
program = 0;
endif
//TODO user input
ElType="HE8";
DType="SolidFrame";
axis="X";
}
QGLScreen::~QGLScreen()
{
}
QSize QGLScreen::minimumSizeHint() const
{
return QSize(50, 50);
}
QSize QGLScreen::sizeHint() const
{
return QSize(200, 200);
}
void QGLScreen::setClearColor(const QColor &color)
{
clearColor = color;
updateGL();
}
void QGLScreen::initializeGL()
{
xRot=0;
yRot=0;
zRot=0;
scaling = 1.0;
/* select clearing (background) color */
glClearColor (0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// glViewport(0,0,10,10);
glOrtho(-10.0, +10.0, -10.0, +10.0, -10.0,+10.0);
glEnable (GL_LINE_SMOOTH);
glHint (GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
}
void QGLScreen::wheel1()
{
scaling1 += .0025;
count2++;
update();
}
void QGLScreen::wheel2()
{
if(count2-14)
{
scaling1 -= .0025;
count2--;
update();
}
}
void QGLScreen::drawModel(int x1,int y1,int x2,int y2)
{
makeCurrent();
QStringList Cnode,Celement;
for (int i = 0; i < Elements.size(); ++i)
{
Celement=Elements.at(i).split(",");
// printf("Element is %s",Celement.at(0).toLocal8Bit().constData());fflush(stdout);
//printf("Node at el is %s\n",(findNode(Celement.at(1).toInt())).at(1).toLocal8Bit().constData());
fflush(stdout);
if(ElType=="HE8")
{
//First four nodes
float ENX1=(findNode(Celement.at(1).toInt())).at(1).toDouble();
float ENX2=(findNode(Celement.at(2).toInt())).at(1).toDouble();
float ENX3=(findNode(Celement.at(3).toInt())).at(1).toDouble();
float ENX4=(findNode(Celement.at(4).toInt())).at(1).toDouble();
float ENY1=(findNode(Celement.at(1).toInt())).at(2).toDouble();
float ENY2=(findNode(Celement.at(2).toInt())).at(2).toDouble();
float ENY3=(findNode(Celement.at(3).toInt())).at(2).toDouble();
float ENY4=(findNode(Celement.at(4).toInt())).at(2).toDouble();
float ENZ1=(findNode(Celement.at(1).toInt())).at(3).toDouble();
float ENZ2=(findNode(Celement.at(2).toInt())).at(3).toDouble();
float ENZ3=(findNode(Celement.at(3).toInt())).at(3).toDouble();
float ENZ4=(findNode(Celement.at(4).toInt())).at(3).toDouble();
//Second four Nodes
float ENX5=(findNode(Celement.at(5).toInt())).at(1).toDouble();
float ENX6=(findNode(Celement.at(6).toInt())).at(1).toDouble();
float ENX7=(findNode(Celement.at(7).toInt())).at(1).toDouble();
float ENX8=(findNode(Celement.at(8).toInt())).at(1).toDouble();
float ENY5=(findNode(Celement.at(5).toInt())).at(2).toDouble();
float ENY6=(findNode(Celement.at(6).toInt())).at(2).toDouble();
float ENY7=(findNode(Celement.at(7).toInt())).at(2).toDouble();
float ENY8=(findNode(Celement.at(8).toInt())).at(2).toDouble();
float ENZ5=(findNode(Celement.at(5).toInt())).at(3).toDouble();
float ENZ6=(findNode(Celement.at(6).toInt())).at(3).toDouble();
float ENZ7=(findNode(Celement.at(7).toInt())).at(3).toDouble();
float ENZ8=(findNode(Celement.at(8).toInt())).at(3).toDouble();
//Identify Colors
GLfloat ENC[8][3];
for(int k=1;k<8;k++)
{
int hsv=pickColor(findData(Celement.at(k).toInt()).toDouble(),0);
//printf("hsv is %d=",hsv);fflush(stdout);
getRGB(hsv);
//printf("%d*%d*%d\n",red,green,blue);
//ENC[k]={red,green,blue};
ENC[k][0]=red;
ENC[k][1]=green;
ENC[k][2]=blue;
}
//Plot the first four direct loop
if(DType=="WireFrame"){
glBegin(GL_LINE_LOOP);
glColor3f(255,0,0);
glVertex3f(ENX1,ENY1,ENZ1);
glColor3f(255,0,0);
glVertex3f(ENX2,ENY2,ENZ2);
glColor3f(255,0,0);
glVertex3f(ENX3,ENY3,ENZ3);
glColor3f(255,0,0);
glVertex3f(ENX4,ENY4,ENZ4);
glEnd();
//Plot the second four direct loop
glBegin(GL_LINE_LOOP);
glColor3f(0,0,255);
glVertex3f(ENX5,ENY5,ENZ5);
glColor3f(0,0,255);
glVertex3f(ENX6,ENY6,ENZ6);
glColor3f(0,0,255);
glVertex3f(ENX7,ENY7,ENZ7);
glColor3f(0,0,255);
glVertex3f(ENX8,ENY8,ENZ8);
glEnd();
//Plot the interconnections
glBegin(GL_LINE);
glColor3f(150,150,150);
glVertex3f(ENX1,ENY1,ENZ1);
glVertex3f(ENX5,ENY5,ENZ5);
glEnd();
glBegin(GL_LINE);
glColor3f(150,150,150);
glVertex3f(ENX2,ENY2,ENZ2);
glVertex3f(ENX6,ENY6,ENZ6);
glEnd();
glBegin(GL_LINE);
glColor3f(150,150,150);
glVertex3f(ENX3,ENY3,ENZ3);
glVertex3f(ENX7,ENY7,ENZ7);
glEnd();
glBegin(GL_LINE);
glColor3f(150,150,150);
glVertex3f(ENX4,ENY4,ENZ4);
glVertex3f(ENX8,ENY8,ENZ8);
glEnd();
}
if(DType=="SolidFrame")
{
glBegin(GL_QUADS);
glColor3fv(ENC[1]);
glVertex3f(ENX1,ENY1,ENZ1);
glColor3fv(ENC[2]);
glVertex3f(ENX2,ENY2,ENZ2);
glColor3fv(ENC[3]);
glVertex3f(ENX3,ENY3,ENZ3);
glColor3fv(ENC[4]);
glVertex3f(ENX4,ENY4,ENZ4);
glEnd();
//break;
glBegin(GL_QUADS);
glColor3fv(ENC[5]);
glVertex3f(ENX5,ENY5,ENZ5);
glColor3fv(ENC[6]);
glVertex3f(ENX6,ENY6,ENZ6);
glColor3fv(ENC[7]);
glVertex3f(ENX7,ENY7,ENZ7);
glColor3fv(ENC[8]);
glVertex3f(ENX8,ENY8,ENZ8);
glEnd();
glBegin(GL_QUAD_STRIP);
glColor3fv(ENC[1]);
glVertex3f(ENX1,ENY1,ENZ1);
glColor3fv(ENC[5]);
glVertex3f(ENX5,ENY5,ENZ5);
glColor3fv(ENC[2]);
glVertex3f(ENX2,ENY2,ENZ2);
glColor3fv(ENC[6]);
glVertex3f(ENX6,ENY6,ENZ6);
glEnd();
glBegin(GL_QUAD_STRIP);
glColor3fv(ENC[3]);
glVertex3f(ENX3,ENY3,ENZ3);
glColor3fv(ENC[7]);
glVertex3f(ENX7,ENY7,ENZ7);
glColor3fv(ENC[4]);
glVertex3f(ENX4,ENY4,ENZ4);
glColor3fv(ENC[8]);
glVertex3f(ENX8,ENY8,ENZ8);
glEnd();
glBegin(GL_QUAD_STRIP);
glColor3fv(ENC[2]);
glVertex3f(ENX2,ENY2,ENZ2);
glColor3fv(ENC[6]);
glVertex3f(ENX6,ENY6,ENZ6);
glColor3fv(ENC[3]);
glVertex3f(ENX3,ENY3,ENZ3);
glColor3fv(ENC[7]);
glVertex3f(ENX7,ENY7,ENZ7);
glEnd();
glBegin(GL_QUAD_STRIP);
glColor3fv(ENC[1]);
glVertex3f(ENX1,ENY1,ENZ1);
glColor3fv(ENC[5]);
glVertex3f(ENX5,ENY5,ENZ5);
glColor3fv(ENC[4]);
glVertex3f(ENX4,ENY4,ENZ4);
glColor3fv(ENC[8]);
glVertex3f(ENX8,ENY8,ENZ8);
glEnd();
}
}
}
}
QStringList QGLScreen::findNode(int element)
{
QStringList Temp;
for (int i = 0; i < Nodes.size(); ++i)
{
Temp=Nodes.at(i).split(",");
if(Temp.at(0).toInt()==element)
{
break;
}
}
return Temp;
}
QString QGLScreen::findData(int Node)
{
QString Temp;
QRegExp sep("\s+");
for (int i = 0; i < Data.size(); ++i)
{
if((Data.at(i).split("\t")).at(0).section(sep,1,1).toInt()==Node)
{
if(axis=="X")
{
Temp=Data.at(i).split("\t").at(0).section(sep,2,2);
}
if(axis=="Y")
{
Temp=Data.at(i).split("\t").at(0).section(sep,3,3);
}
if(axis=="Z")
{
Temp=Data.at(i).split("\t").at(0).section(sep,4,4);
}
break;
}
}
return Temp;
}
void QGLScreen::fillColorArray()
{
QString Temp1,Temp2,Temp3;
double d1s=0,d2s=0,d3s=0,d1l=0,d2l=0,d3l=0,diff=0;
QRegExp sep("\\s+");
for (int i = 0; i < Data.size(); ++i)
{
Temp1=(Data.at(i).split("\t")).at(0).section(sep,2,2);
if(d1s>Temp1.toDouble())
{
d1s=Temp1.toDouble();
}
if(d1l<Temp1.toDouble())
{
d1l=Temp1.toDouble();
}
Temp2=(Data.at(i).split("\t")).at(0).section(sep,3,3);
if(d2s>Temp2.toDouble())
{
d2s=Temp2.toDouble();
}
if(d2l<Temp2.toDouble())
{
d2l=Temp2.toDouble();
}
Temp3=(Data.at(i).split("\t")).at(0).section(sep,4,4);
if(d3s>Temp3.toDouble())
{
d3s=Temp3.toDouble();
}
if(d3l<Temp3.toDouble())
{
d3l=Temp3.toDouble();
}
// printf("data is %s",Temp.toLocal8Bit().constData());fflush(stdout);
}
color[0][0]=d1l;
for(int i=1;i<360;i++)
{
//printf("Large is%f small is %f",d1l,d1s);
diff=d1l-d1s;
if(d1l==0&&d1s<0)
color[0][i]=color[0][i-1]-diff/360;
else if(d1l>0&&d1s==0)
color[0][i]=color[0][i-1]+diff/360;
else if(d1l>0&&d1s<0)
color[0][i]=color[0][i-1]-diff/360;
diff=d2l-d2s;
if(d2l==0&&d2s<0)
color[1][i]=color[1][i-1]-diff/360;
else if(d2l>0&&d2s==0)
color[1][i]=color[1][i-1]+diff/360;
else if(d2l>0&&d2s<0)
color[1][i]=color[1][i-1]-diff/360;
diff=d3l-d3s;
if(d3l==0&&d3s<0)
color[2][i]=color[2][i-1]-diff/360;
else if(d3l>0&&d3s==0)
color[2][i]=color[2][i-1]+diff/360;
else if(d3l>0&&d3s<0)
color[2][i]=color[2][i-1]-diff/360;
}
//for(int i=0;i<360;i++) printf("%d %f %f %f\n",i,color[0][i],color[1][i],color[2][i]);
}
int QGLScreen::pickColor(double data,int Did)
{
int i,pos;
if(axis=="X")Did=0;
if(axis=="Y")Did=1;
if(axis=="Z")Did=2;
//printf("%f data is",data);fflush(stdout);
for(int i=0;i<360;i++)
{
if(color[Did][i]<data && data>color[Did][i+1])
{
//printf("Orginal dat is %f Data found is %f and pos %d\n",data,color[Did][i],i);fflush(stdout);
pos=i;
break;
}
}
return pos;
}
void QGLScreen::getRGB(int hsv)
{
QColor c;
c.setHsv(hsv,255,255,255);
QColor r=QColor::fromHsv(hsv,255,255);
red=r.red();
green=r.green();
blue=r.blue();
}
void QGLScreen::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushAttrib(GL_ALL_ATTRIB_BITS);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
GLfloat x = 3.0 * GLfloat(width()) / height();
glOrtho(-x, +x, -3.0, +3.0, 4.0, 15.0);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glTranslatef(0.0, 0.0, -10.0);
glScalef(scaling, scaling, scaling);
glRotatef(xRot, 1.0, 0.0, 0.0);
glRotatef(yRot, 0.0, 1.0, 0.0);
glRotatef(zRot, 0.0, 0.0, 1.0);
drawModel(0,0,1,1);
/* don't wait!
* start processing buffered OpenGL routines
*/
glFlush ();
}
/void QGLScreen::zoom1()
{
scaling+=.05;
update();
}/
void QGLScreen::resizeGL(int width, int height)
{
int side = qMin(width, height);
glViewport((width - side) / 2, (height - side) / 2, side, side);
#if !defined(QT_OPENGL_ES_2)
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
#ifndef QT_OPENGL_ES
glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0);
#else
glOrthof(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0);
#endif
glMatrixMode(GL_MODELVIEW);
#endif
}
void QGLScreen::mousePressEvent(QMouseEvent *event)
{
lastPos = event-pos();
}
void QGLScreen::mouseMoveEvent(QMouseEvent *event)
{
GLfloat dx = GLfloat(event->x() - lastPos.x()) / width();
GLfloat dy = GLfloat(event->y() - lastPos.y()) / height();
if (event->buttons() & Qt::LeftButton) {
xRot+= 180 * dy;
yRot += 180 * dx;
update();
} else if (event->buttons() & Qt::RightButton) {
xRot += 180 * dy;
yRot += 180 * dx;
update();
}
lastPos = event->pos();
}
void QGLScreen::mouseReleaseEvent(QMouseEvent * /* event */)
{
emit clicked();
}