Biomechanical Joint Model
 Author: Anderson Maciel

come_discretizerqglwidget.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2004 by Anderson Maciel                                 *
00003  *   andi.maciel@gmail.com                                                   *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #include <come_discretizerqglwidget.h>
00021 #include <comediscretizer.h>
00022 #include <general/comescenario.h>
00023 #include <general/comesimulator.h>
00024 #include <qcheckbox.h>
00025 #include <qtimer.h>
00026 #include <qcursor.h>
00027 #include <qslider.h>
00028 #include <qpopupmenu.h>
00029 #include <qspinbox.h>
00030 #include <qstatusbar.h>
00031 #include <GL/glut.h>
00032 
00033 double oldClock;
00034 
00035 COME_DiscretizerQGLWidget::COME_DiscretizerQGLWidget( QWidget *parent, const char *name )
00036  : COME_QGLWidget( parent, name )
00037 {
00038         oldClock = 0.0;
00039 }
00040 
00041 
00042 COME_DiscretizerQGLWidget::~COME_DiscretizerQGLWidget()
00043 {
00044 }
00045 
00046 void
00047 COME_DiscretizerQGLWidget::draw(){
00048 
00049         //CGparameter 
00050         //cgGLSetParameter3fv(cgGetNamedParameter(fragmentProgram, "lightColor"), lightColor);
00051         glPushMatrix();
00052                         glTranslatef( 5, 0, 0  );
00053                         glutSolidSphere(1, 4, 4);
00054                 glPopMatrix();
00055         
00056         if( scene ){
00057                 glPushMatrix();
00058                         glTranslatef( 10, 0, 0  );
00059                         glutSolidSphere(1, 15, 15);
00060                 glPopMatrix();
00061                 glPushMatrix();
00062                         glTranslatef( 20, 0, 0  );
00063                         glutSolidSphere(1, 15, 15);
00064                 glPopMatrix();
00065                 
00066                 scene->lock();
00067 
00068                 //Main loop: For each organ display the object
00069                 int sphereName = 0;
00070                 list<COME_BioStructure*>::const_iterator iterOrgans;
00071                 for( iterOrgans = ((list<COME_BioStructure *>*)scene->getPatient(0)->getPtOrganList())->begin(); iterOrgans != ((list<COME_BioStructure *>*)scene->getPatient(0)->getPtOrganList())->end(); iterOrgans++  ){
00072                         list<COME_Molecule*>::const_iterator iterCells;
00073                 
00074                         //Draw the Sphere Model                 
00075                         if (((COME_Discretizer*)getGlobal())->cbSpheres->isChecked()){
00076                                 for ( iterCells = (*iterOrgans)->getTissue()->getShape()->begin(); iterCells != (*iterOrgans)->getTissue()->getShape()->end(); iterCells++ ){
00077                                         
00078                                         COME_Point3D position = (*iterCells)->getGlobalPosition();
00079                                         glPushName(sphereName++);
00080                                         glPushMatrix();
00081                                                 glTranslatef(position.getX(),position.getY(),position.getZ());
00082 
00083                                                 if( !(*iterCells)->isFixed() ){
00084                                                         glColor3f((*iterCells)->getMaterial()->getColor().getPointAsArray()[0],(*iterCells)->getMaterial()->getColor().getPointAsArray()[1],(*iterCells)->getMaterial()->getColor().getPointAsArray()[2]);      
00085                                                 } else if( (*iterCells)->getDescription() == "origin" ){
00086                                                         glColor3f((*iterCells)->getMaterial()->getColor().getPointAsArray()[0]*2.0,(*iterCells)->getMaterial()->getColor().getPointAsArray()[1]*2.0,(*iterCells)->getMaterial()->getColor().getPointAsArray()[2]*2.0);
00087                                                 } else {
00088                                                         glColor3f((*iterCells)->getMaterial()->getColor().getPointAsArray()[0]*0.2,(*iterCells)->getMaterial()->getColor().getPointAsArray()[1]*0.2,(*iterCells)->getMaterial()->getColor().getPointAsArray()[2]*0.2);
00089                                                 }
00090                                                 
00091                                                 glutSolidSphere((*iterCells)->getRadius()*((double)((COME_Discretizer*)getGlobal())->sliderScale->value()/100.0), 15, 15);
00092                                         glPopMatrix();
00093                                         glPopName();
00094                                         
00095                                         if( ((COME_Discretizer*)getGlobal())->cbLinks->isChecked() ){
00096                                                 list<COME_MoleculeLink*>::iterator iterThis;
00097                                                 for(iterThis = (*iterCells)->getConnectionList()->begin(); !( iterThis == (*iterCells)->getConnectionList()->end() ); iterThis++){
00098                                                         COME_Point3D position2 = (*iterThis)->getOtherElement((*iterCells))->getGlobalPosition();
00099                                                         glBegin(GL_LINES);
00100                                                                 glColor3f(0.9,0.9,0.9);
00101                                                                 glVertex3f(position.getX(),position.getY(),position.getZ());
00102                                                                 glVertex3f(position2.getX(),position2.getY(),position2.getZ());
00103                                                         glEnd();
00104                                                 }
00105                                         }
00106                                 }
00107                         }
00108 
00109                         // Draw a grid according to GUI parameters
00110                         if (((COME_Discretizer*)getGlobal())->cbGrid->isChecked()){
00111                                 
00112                                 double dist = ((double)((COME_Discretizer*)getGlobal())->sbDistance->value())/1000.0;
00113                                 COME_Point3D mins, maxs;
00114                                 COME_Mesh* mesh = (*iterOrgans)->getPointerToSurface();
00115                                 mesh->getEnvelop( mins, maxs );
00116                         
00117                                 if( ( ( ( maxs.getX()-mins.getX() ) * ( maxs.getY()-mins.getY() ) * ( maxs.getZ()-mins.getZ() ) ) / dist ) > MAX_LINES ){
00118                                         ((COME_Discretizer*)getGlobal())->statusBar()->message( "Grid too dense. Please increase preferred distance to avoid overload.", 500 );
00119                                 } else {
00120                                         for( double xMgrid = mins.getX(); xMgrid <= maxs.getX(); xMgrid += dist ){
00121                                                 for( double yMgrid = mins.getY(); yMgrid <= maxs.getY(); yMgrid += dist ){
00122                                                         glBegin(GL_LINES);
00123                                                                 glColor3f(0.5,0.5,0.5);
00124                                                                 glVertex3f(xMgrid, yMgrid, mins.getZ());
00125                                                                 glVertex3f(xMgrid, yMgrid, maxs.getZ());
00126                                                         glEnd();
00127                                                 }
00128                                                 for( double zMgrid = mins.getZ(); zMgrid <= maxs.getZ(); zMgrid += dist ){
00129                                                         glBegin(GL_LINES);
00130                                                                 glColor3f(0.9,0.9,0.9);
00131                                                                 glVertex3f(xMgrid, mins.getY(), zMgrid);
00132                                                                 glVertex3f(xMgrid, maxs.getY(), zMgrid);
00133                                                         glEnd();
00134                                                 }
00135                                         }
00136                                         for( double zMgrid = mins.getZ(); zMgrid <= maxs.getZ(); zMgrid += dist ){
00137                                                 for( double yMgrid = mins.getY(); yMgrid <= maxs.getY(); yMgrid += dist ){
00138                                                         glBegin(GL_LINES);
00139                                                                 glColor3f(0.5,0.5,0.5);
00140                                                                 glVertex3f(mins.getX(), yMgrid, zMgrid);
00141                                                                 glVertex3f(maxs.getX(), yMgrid, zMgrid);
00142                                                         glEnd();
00143                                                 }
00144                                         }
00145                                 }
00146                         }
00147 
00148                         //Draw the Mesh Model
00149                         if (((COME_Discretizer*)getGlobal())->cbMesh->isChecked()){
00150                                 
00151                                 COME_Mesh* mesh = (*iterOrgans)->getPointerToSurface();
00152                                 vector <COME_Face> faces = mesh->getFaces();
00153                                 vector<COME_Vertex3D>* allVertices = mesh->getVerticesGlobalPt();
00154 
00155                                 for (int i = 0; faces.size()> i; i++){
00156                                         
00157                                         glColor3f(faces[i].getColor()[0],faces[i].getColor()[1],faces[i].getColor()[2]);
00158                                         
00159                                         //Draw Mesh Triangles
00160                                         glBegin(GL_TRIANGLES);
00161                                                 for ( int j=0;j<3;j++){
00162                                                         int indexVertex = faces[i].getIndexVertex(j); 
00163                                                         COME_Vector3D normal = allVertices->at(indexVertex).getNormalGlobalPosition();
00164                                                         glNormal3f(normal.x,normal.y,normal.z);
00165                                                         //printf( "NORMALS: %f %f %f \n", normal.x,normal.y,normal.z );
00166                                                         COME_Vertex3D vertex = allVertices->at(indexVertex);
00167                                                         glVertex3d(vertex.x,vertex.y,vertex.z);
00168                                                 }
00169                                         glEnd();
00170                                         if(((COME_Discretizer*)getGlobal())->cbNormals->isChecked()){
00171                                                 for ( int k=0;k<3;k++){
00172                                                         int indexVertex = faces[i].getIndexVertex(k); 
00173                                                         COME_Vector3D normal = allVertices->at(indexVertex).getNormalGlobalPosition();
00174                                                         COME_Vertex3D vertex = allVertices->at(indexVertex);
00175                                                         glBegin(GL_LINES);
00176                                                         glColor3f(0.5,0.5,0.5);
00177                                                         glVertex3f(vertex.x,vertex.y,vertex.z);
00178                                                         glVertex3f(vertex.x+(8*normal.x),vertex.y+(8*normal.y), vertex.z+(8*normal.z));
00179                                                         glEnd();
00180                                                 }
00181                                         }
00182                                 }
00183                         }
00184                 }
00185                 scene->unlock();
00186                 
00187                 if (((COME_Discretizer*)getGlobal())->cbMakeMovie->isChecked())
00188                         //if( oldClock < ((COME_Discretizer*)getGlobal())->sim->getClock() ){
00189                                 saveSnapshot(true, true); // Warning, overwrite mode
00190                         //      oldClock = ((COME_Discretizer*)getGlobal())->sim->getClock();
00191                         //}
00192         }
00193         
00194 
00195 }
00196 
00197 void
00198 COME_DiscretizerQGLWidget::postSelection(const QPoint& point){
00199 
00200         if( selectedName() == -1)
00201         {
00202                 ((COME_Discretizer*)getGlobal())->statusBar()->message( "No object selected.", 1000 );
00203         }
00204         else {
00205                 
00206                 list<COME_Molecule*>::iterator iter;
00207                 int indNames = 0;
00208                 for (iter = scene->getPatient(0)->getOrgan( 0 )->getTissue()->getShape()->begin(); iter != scene->getPatient(0)->getOrgan( 0 )->getTissue()->getShape()->end(); iter++, indNames++){
00209                         
00210                         if( selectedName() == indNames ){
00211                                 if( !(*iter)->isFixed() ){
00212                                         (*iter)->setFixed(true);
00213                                 } else if( (*iter)->getDescription() == "origin" ){
00214                                         (*iter)->setFixed(false);
00215                                         (*iter)->setDescription("free");
00216                                 } else {
00217                                         (*iter)->setDescription("origin");
00218                                 }
00219                         }
00220                 }
00221                 
00222                 char chMessage[30];
00223                 sprintf( chMessage, "Molecule %d selected.", selectedName() );
00224                 ((COME_Discretizer*)getGlobal())->statusBar()->message( chMessage, 1000 );
00225         }
00226 
00227 }
00228 
00229 void
00230 COME_DiscretizerQGLWidget::drawWithNames(){
00231 
00232         draw();
00233 }

Generated on Thu Dec 1 10:13:32 2005 for COME - Biomechanical Joint Model by  doxygen 1.4.5