Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "ParticleGridPainter.h"
00012
00013 #include "Architecture/Config/Config.h"
00014 #include "Messages/FastRobotPoseM.h"
00015
00016 #include <QtOpenGL>
00017 #include <GL/glut.h>
00018
00019
00020 #define THIS ParticleGridPainter
00021
00022 THIS::THIS() : PainterPlugin( )
00023 {
00024 setName ( "Particle Density" );
00025
00026 m_ParticleBinCount = Config::getInt( "Gui.SensorData3d.iParticleGridBins" );
00027 m_ParticleBinDisplaySize = Config::getFloat( "Gui.SensorData3d.fParticleGridBinDisplaySize" );
00028 m_ParticlePeekHeight = Config::getFloat( "Gui.SensorData3d.fParticleGridBinDisplayPeekHeight" );
00029 m_WeightGrid = 0;
00030 }
00031
00032
00033 THIS::~THIS()
00034 {
00035 if ( m_WeightGrid )
00036 {
00037 delete m_WeightGrid;
00038 }
00039 }
00040
00041
00042 void THIS::processMessage ( Message* newMessage )
00043 {
00044 PainterPlugin::processMessage ( newMessage );
00045 switch ( newMessage->getType() )
00046 {
00047 case MessageTypes::FAST_ROBOT_POSE_M:
00048 {
00049 FastRobotPoseM* message = Message::castTo<FastRobotPoseM> ( newMessage );
00050 if ( message )
00051 {
00052 m_RobotPose = message->getRobotPose();
00053 requestRedraw();
00054 }
00055 break;
00056 }
00057
00058 case MessageTypes::PARTICLE_DATA_M:
00059
00060 {
00061 ParticleDataM* message = Message::castTo<ParticleDataM> ( newMessage );
00062 if ( message )
00063 {
00064 m_ParticleData = * ( message->getParticles() );
00065 generateMesh();
00066 requestRedraw();
00067 }
00068 break;
00069 }
00070
00071 default:
00072 break;
00073
00074 }
00075 }
00076
00077 void THIS::generateMesh()
00078 {
00079 int meshSize = m_ParticleBinCount;
00080 int meshSizeHalf = meshSize / 2;
00081
00082 if ( !m_WeightGrid )
00083 {
00084 m_WeightGrid = new float[ meshSize * meshSize ];
00085 }
00086
00087 m_HeightPeek = 0.0;
00088
00089 float scaleFactor = m_ParticleBinDisplaySize;
00090
00091 float CenterX = m_RobotPose.x();
00092 float CenterY = m_RobotPose.y();
00093
00094
00095 memset ( m_WeightGrid, 0, sizeof ( m_WeightGrid ) * meshSize * meshSize );
00096
00097
00098 m_MinX = meshSize / 2;
00099 m_MinY = meshSize / 2;
00100 m_MaxX = m_MinX;
00101 m_MaxY = m_MinY;
00102 for ( unsigned int i = 0; i < m_ParticleData.size(); ++i )
00103 {
00104 if ( m_ParticleData[ i ].poseKind == ParticleDataM::Particle )
00105 {
00106 int meshX = ( ( int ( float ( m_ParticleData[i].x ) - CenterX + 0.5 ) ) + meshSizeHalf * scaleFactor );
00107 int meshY = ( ( int ( float ( m_ParticleData[i].y ) - CenterY + 0.5 ) ) + meshSizeHalf * scaleFactor );
00108 if ( meshX >= 0 && meshX < meshSize * scaleFactor && meshY >= 0 && meshY < meshSize * scaleFactor )
00109 {
00110 unsigned meshPositionX = unsigned ( meshX / scaleFactor );
00111 unsigned meshPositionY = unsigned ( meshY / scaleFactor );
00112
00113
00114 if ( m_MinX > meshPositionX ) m_MinX = meshPositionX;
00115 if ( m_MaxX < meshPositionX ) m_MaxX = meshPositionX;
00116 if ( m_MinY > meshPositionY ) m_MinY = meshPositionY;
00117 if ( m_MaxY < meshPositionY ) m_MaxY = meshPositionY;
00118 * ( m_WeightGrid + meshPositionY * meshSize + meshPositionX ) += m_ParticleData[i].weight;
00119
00120 if ( ( * ( m_WeightGrid + meshPositionY * meshSize + meshPositionX ) ) > m_HeightPeek )
00121 m_HeightPeek = * ( m_WeightGrid + meshPositionY * meshSize + meshPositionX );
00122 }
00123 }
00124 }
00125 }
00126
00127 void THIS::paint ( float next2DLayer )
00128 {
00129 if ( (!m_WeightGrid) || ( m_ParticleData.size() == 0 ) )
00130 {
00131 return;
00132 }
00133
00134 glPushMatrix();
00135 glTranslatef ( 0.0, 0.0, next2DLayer );
00136
00137
00138
00139 int meshSize = m_ParticleBinCount;
00140
00141 int meshSizeHalf = meshSize / 2;
00142
00143
00144 float scaleFactor = m_ParticleBinDisplaySize;
00145 float alpha = 0.8;
00146 float weightScaleFactor = 0.0;
00147
00148 float CenterX = m_RobotPose.x();
00149 float CenterY = m_RobotPose.y();
00150
00151 glPolygonMode ( GL_FRONT, GL_FILL );
00152 glPolygonMode ( GL_BACK, GL_LINE );
00153
00154
00155 glEnable ( GL_BLEND );
00156 glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
00157
00158 glTranslatef ( CenterX, CenterY, 0 );
00159
00160
00161 glTranslatef ( -meshSizeHalf * scaleFactor, -meshSizeHalf * scaleFactor, 0 );
00162 glColor3f ( 1.0, 0, 0 );
00163
00164
00165
00166 weightScaleFactor = m_ParticlePeekHeight / m_HeightPeek;
00167
00168
00169 for ( unsigned x = m_MinX; x < m_MaxX - 1; ++x )
00170 {
00171 glBegin ( GL_TRIANGLE_STRIP );
00172 for ( unsigned y = m_MinY; y < m_MaxY; ++y )
00173 {
00174
00175
00176 float curMesh = ( * ( m_WeightGrid + y * meshSize + x ) ) * weightScaleFactor;
00177 float h = curMesh;
00178 if ( curMesh > 0 )
00179 {
00180 float green = curMesh / m_ParticlePeekHeight * 1.5;
00181 if ( green > 1.0 )
00182 green = 1.0;
00183 glColor4f ( 1.0, green, 0, alpha );
00184 }
00185 else
00186 {
00187 glColor4f ( 1.0, 0, 0, 0 );
00188 }
00189 glVertex3f ( x * scaleFactor, y * scaleFactor, h );
00190
00191 curMesh = ( * ( m_WeightGrid + y * meshSize + x + 1 ) ) * weightScaleFactor;
00192 h = curMesh;
00193 if ( curMesh > 0 )
00194 {
00195 float green = curMesh / m_ParticlePeekHeight * 1.5;
00196 if ( green > 1.0 )
00197 green = 1.0;
00198 glColor4f ( 1.0, green, 0, alpha );
00199 }
00200 else
00201 {
00202 glColor4f ( 1.0, 0, 0, 0 );
00203 }
00204 glVertex3f ( ( x + 1 ) * scaleFactor, y * scaleFactor, h );
00205 }
00206 glEnd();
00207 }
00208
00209 glDisable ( GL_BLEND );
00210 glPopMatrix();
00211 }
00212
00213 #undef THIS