00001 #include "SIGEL_CommonGUI/SIG_VisualisationWidget.h"
00002
00003 #include <cmath>
00004
00005 namespace SIGEL_CommonGUI
00006 {
00007
00008 SIG_VisualisationWidget::SIG_VisualisationWidget( QWidget *parent=0,
00009 char const *name=0,
00010 WFlags f=0 )
00011 : QGLWidget(parent, name, 0, f),
00012 visualisation(0),
00013 floatingTextWidgets(),
00014 floatingTextsSize(),
00015 showAncorPointsState(0),
00016 yaw(0),
00017 pitch(30),
00018 distance(2),
00019 mouseXPos(0),
00020 mouseYPos(0),
00021 automaticRefresh(true),
00022 mouseSensity(0.5),
00023 pi( std::atan(1) * 4 )
00024 {
00025 floatingTextWidgets.setAutoDelete( true );
00026 };
00027
00028 SIG_VisualisationWidget::~SIG_VisualisationWidget()
00029 {
00030 delete visualisation;
00031 };
00032
00033 void SIG_VisualisationWidget::setRenderMode( const QString & string )
00034 {
00035 if (visualisation)
00036 {
00037 if (string == "Wireframe")
00038 visualisation->viewSettings.renderMode = SIGEL_Visualisation::SIG_ViewSettings::wireFrame;
00039 else if (string == "Flatshaded")
00040 visualisation->viewSettings.renderMode = SIGEL_Visualisation::SIG_ViewSettings::flatShaded;
00041 else if (string == "Gouraudshaded")
00042 visualisation->viewSettings.renderMode = SIGEL_Visualisation::SIG_ViewSettings::garoudShaded;
00043 if (automaticRefresh)
00044 updateGL();
00045 };
00046 };
00047
00048 void SIG_VisualisationWidget::setAmbientLighting( int newValue )
00049 {
00050 if (visualisation)
00051 {
00052 double const ambientSceneColor = double( newValue ) / 100;
00053
00054 visualisation->setAmbientSceneColor( ambientSceneColor,
00055 ambientSceneColor,
00056 ambientSceneColor );
00057
00058 if (automaticRefresh)
00059 updateGL();
00060 };
00061 };
00062
00063 void SIG_VisualisationWidget::setShowAncorPoints( int state )
00064 {
00065 if ( (showAncorPointsState < 2) && (state == 2) )
00066 for ( int i = 0; i < floatingTextWidgets.size(); i++ )
00067 floatingTextWidgets[i]->show();
00068
00069 if ( (showAncorPointsState == 2) && (state < 2) )
00070 for ( int i = 0; i < floatingTextWidgets.size(); i++ )
00071 floatingTextWidgets[i]->hide();
00072
00073 showAncorPointsState = state;
00074
00075 updateGL();
00076 };
00077
00078 void SIG_VisualisationWidget::setYaw( double yyaw )
00079 {
00080 yaw = yyaw;
00081 updateEyePoint();
00082 };
00083
00084 void SIG_VisualisationWidget::setPitch( double ppitch )
00085 {
00086 pitch = ppitch;
00087 updateEyePoint();
00088 };
00089
00090 void SIG_VisualisationWidget::setDistance( double ddistance )
00091 {
00092 distance = ddistance;
00093 updateEyePoint();
00094 };
00095
00096 void SIG_VisualisationWidget::updateEyePoint()
00097 {
00098 if (visualisation)
00099 {
00100 double const radEyeYaw = (yaw / 360) * 2 * pi;
00101 double const radEyePitch = (pitch / 360) * 2 * pi;
00102
00103 double const sinEyeYaw = std::sin(radEyeYaw);
00104 double const cosEyeYaw = std::cos(radEyeYaw);
00105 double const sinEyePitch = std::sin(radEyePitch);
00106 double const cosEyePitch = std::cos(radEyePitch);
00107
00108 double eyeX = sinEyeYaw * cosEyePitch * distance;
00109 double eyeY = sinEyePitch * distance;
00110 double eyeZ = cosEyeYaw * cosEyePitch * distance;
00111
00112 visualisation->viewSettings.eyePoint.set(0, eyeX);
00113 visualisation->viewSettings.eyePoint.set(1, eyeY);
00114 visualisation->viewSettings.eyePoint.set(2, eyeZ);
00115 visualisation->viewSettings.relativeEyePoint = true;
00116
00117
00118
00119
00120
00121 double const radUpYaw = ( radEyeYaw < pi ) ? radEyeYaw + pi : radEyeYaw - pi;
00122 double const radUpPitch = (pi / 2) - radEyePitch;
00123
00124 double const sinUpYaw = std::sin(radUpYaw);
00125 double const cosUpYaw = std::cos(radUpYaw);
00126 double const sinUpPitch = std::sin(radUpPitch);
00127 double const cosUpPitch = std::cos(radUpPitch);
00128
00129 double upX = sinUpYaw * cosUpPitch;
00130 double upY = sinUpPitch;
00131 double upZ = cosUpYaw * cosUpPitch;
00132
00133 visualisation->viewSettings.up.set(0, upX);
00134 visualisation->viewSettings.up.set(1, upY);
00135 visualisation->viewSettings.up.set(2, upZ);
00136
00137 if (automaticRefresh)
00138 updateGL();
00139 };
00140 };
00141
00142 void SIG_VisualisationWidget::initFloatingTextWidgets()
00143 {
00144 if (visualisation)
00145 {
00146 floatingTextWidgets.clear();
00147 floatingTextWidgets.resize( visualisation->floatingTexts.size() );
00148
00149 for (int i=0; i<floatingTextWidgets.size(); i++)
00150 {
00151 SIG_FloatingTextLabel *newLabel = new SIG_FloatingTextLabel( this );
00152 floatingTextWidgets.insert( i, newLabel );
00153 newLabel->raise();
00154 };
00155 };
00156 };
00157
00158 void SIG_VisualisationWidget::mousePressEvent( QMouseEvent *event )
00159 {
00160 mouseXPos = event->x();
00161 mouseYPos = event->y();
00162 };
00163
00164 void SIG_VisualisationWidget::mouseMoveEvent( QMouseEvent *event )
00165 {
00166 int deltaX = static_cast<int>( (event->x() - mouseXPos) * mouseSensity );
00167 int deltaY = static_cast<int>( (event->y() - mouseYPos) * mouseSensity );
00168
00169 if (event->state() & LeftButton)
00170 emit signalMouseRotation( deltaX, deltaY );
00171
00172 if (event->state() & RightButton)
00173 emit signalMouseZoom( deltaY );
00174
00175 mouseXPos = event->x();
00176 mouseYPos = event->y();
00177 };
00178
00179 void SIG_VisualisationWidget::initializeGL()
00180 {
00181 glClearColor( 1, 1, 1, 1 );
00182 };
00183
00184 void SIG_VisualisationWidget::resizeGL(int width, int height)
00185 {
00186 GLsizei wwidth = static_cast<GLsizei>(width);
00187 GLsizei hheight = static_cast<GLsizei>(height);
00188
00189 glViewport( 0, 0, wwidth, hheight );
00190
00191 if (visualisation)
00192 {
00193 visualisation->viewSettings.aspectRatio = static_cast<double>(width) / static_cast<double>(height);
00194 visualisation->updateAspectRatio();
00195 };
00196 };
00197
00198 void SIG_VisualisationWidget::paintGL()
00199 {
00200 if (visualisation)
00201 {
00202 visualisation->visualize();
00203
00204 if (showAncorPointsState == 2)
00205 {
00206 for (int i=0; i<floatingTextWidgets.size(); i++)
00207 {
00208 SIGEL_Visualisation::SIG_FloatingText *floatingText = visualisation->floatingTexts[i];
00209 if (floatingText->rendered)
00210 {
00211 SIG_FloatingTextLabel *floatingTextLabel = floatingTextWidgets[i];
00212
00213 floatingTextLabel->setText( floatingText->textLabel );
00214 QSize actSize = floatingTextLabel->sizeHint();
00215 if (actSize.width() > floatingTextsSize.width())
00216 floatingTextsSize = actSize;
00217
00218 floatingTextLabel->move( floatingText->xPos,
00219 this->height() - floatingText->yPos );
00220 };
00221 };
00222
00223 for (int i=0; i<floatingTextWidgets.size(); i++)
00224 {
00225 SIGEL_Visualisation::SIG_FloatingText *floatingText = visualisation->floatingTexts[i];
00226 SIG_FloatingTextLabel *floatingTextLabel = floatingTextWidgets[i];
00227 if (floatingText->rendered)
00228 {
00229 floatingTextLabel->resize( floatingTextsSize );
00230 floatingTextLabel->show();
00231 }
00232 else
00233 floatingTextLabel->hide();
00234 };
00235 };
00236 }
00237 else
00238 glClear(GL_COLOR_BUFFER_BIT);
00239 };
00240 }