00001 #include "LaserReadingRenderer.h"
00002
00003 LaserReadingRenderer::LaserReadingRenderer(const std::vector<Point2D> *_points, const std::vector<double> &normals):
00004 m_laserPoints(_points),
00005 m_laserNormals(normals),
00006 m_GLUPoints(0),
00007 m_laserPose(NULL),
00008 m_color(0.0f, 0.0f, 0.0f, 1.0f)
00009 {
00010 m_list = glGenLists(1);
00011 if(m_laserPoints){
00012 m_GLUPoints.resize(m_laserPoints->size());
00013 for(unsigned int i = 0; i < m_GLUPoints.size(); i++){
00014 m_GLUPoints[i] = gluNewQuadric();
00015 }
00016 }
00017
00018 m_pointSize = 0.02f;
00019 setSubdivision(6, 3);
00020 m_depth = 0.f;
00021 m_listValid=false;
00022 }
00023
00024 LaserReadingRenderer::~LaserReadingRenderer(){
00025 for(unsigned int i = 0; i < m_GLUPoints.size(); i++){
00026 gluDeleteQuadric(m_GLUPoints[i]);
00027 }
00028 glDeleteLists(m_list,1);
00029 }
00030
00031 LaserReadingRenderer::LaserReadingRenderer(const LaserReadingRenderer& _renderer):
00032 m_laserPoints(_renderer.getLaserPoints()),
00033 m_laserNormals(_renderer.getLaserNormals()),
00034 m_GLUPoints(0),
00035 m_laserPose(_renderer.m_laserPose),
00036 m_color(_renderer.m_color)
00037 {
00038 m_list = glGenLists(1);
00039 if(m_laserPoints){
00040 m_GLUPoints.resize(m_laserPoints->size());
00041 for(unsigned int i = 0; i < m_GLUPoints.size(); i++){
00042 m_GLUPoints[i] = gluNewQuadric();
00043 }
00044 }
00045 m_pointSize = _renderer.getSize();
00046 _renderer.getSubdivision(m_subdivision[0], m_subdivision[1]);
00047 m_depth = _renderer.m_depth;
00048 m_listValid=false;
00049 }
00050
00051 LaserReadingRenderer& LaserReadingRenderer::operator=(const LaserReadingRenderer& _renderer){
00052 setLaserPoints(_renderer.getLaserPoints(), _renderer.getLaserNormals());
00053 m_laserPose = _renderer.m_laserPose;
00054 m_color = _renderer.getColor();
00055 m_pointSize = _renderer.getSize();
00056 _renderer.getSubdivision(m_subdivision[0], m_subdivision[1]);
00057 m_depth = _renderer.m_depth;
00058 m_listValid=false;
00059 return *this;
00060 }
00061
00062 void LaserReadingRenderer::setLaserPoints(const std::vector<Point2D> *_points, const std::vector<double>& normals){
00063 m_laserPoints = _points;
00064 m_laserPose = NULL;
00065
00066 if(!m_laserPoints) return;
00067
00068 if (normals.size() == m_laserPoints->size()){
00069 m_laserNormals = normals;
00070 } else {
00071 m_laserNormals.clear();
00072 }
00073 if(m_laserPoints->size() == m_GLUPoints.size()) return;
00074
00075 unsigned int i = m_laserPoints ? m_laserPoints->size() : 0;
00076 for(; i < m_GLUPoints.size(); i++){
00077 gluDeleteQuadric(m_GLUPoints[i]);
00078 }
00079
00080 i = m_GLUPoints.size();
00081 if(m_laserPoints){
00082 m_GLUPoints.resize(m_laserPoints->size());
00083 for(; i < m_GLUPoints.size(); i++){
00084 m_GLUPoints[i] = gluNewQuadric();
00085 }
00086 }
00087 m_listValid=false;
00088 }
00089
00090 void LaserReadingRenderer::render(){
00091 if(!m_listValid)
00092 makeList();
00093 glCallList(m_list);
00094 }
00095
00096 void LaserReadingRenderer::makeList(){
00097
00098 glNewList(m_list, GL_COMPILE);
00099 if(!m_laserPoints) {
00100 glEndList();
00101
00102 return;
00103 }
00104 glPushMatrix();
00105 glColor4f(m_color.red(), m_color.green(), m_color.blue(), m_color.alpha());
00106 glTranslatef(0.f, 0.f, m_depth);
00107 if(m_laserPose){
00108
00109 glTranslatef(m_laserPose->x, m_laserPose->y, 0.f);
00110 glRotatef(rad2deg(m_laserPose->theta),0.f,0.f,1.f);
00111 glBegin(GL_TRIANGLES);
00112 glVertex3f( 0.0f, -0.2f, 0.f);
00113 glVertex3f( 0.4f, 0.0f, 0.f);
00114 glVertex3f( 0.0f, 0.2f, 0.f);
00115 glEnd();
00116
00117 }
00118
00119 glBegin(GL_POINTS);
00120 for (unsigned int index = 0; m_laserPoints && index < m_laserPoints->size(); index++) {
00121 glVertex3f((*m_laserPoints)[index].x, (*m_laserPoints)[index].y, 0.f);
00122 }
00123 glEnd();
00124
00125 for (unsigned int index = 0; m_laserPoints && m_laserNormals.size() && index < m_laserPoints->size(); index++) {
00126 glPushMatrix();
00127 if(m_laserNormals.size()){
00128 glTranslated((*m_laserPoints)[index].x, (*m_laserPoints)[index].y, 0.f);
00129 glBegin(GL_LINES);
00130 glVertex3f(0.f, 0.f, 0.f);
00131 glVertex3f(cos(m_laserNormals[index]), sin(m_laserNormals[index]), 0.f);
00132 glEnd();
00133 }
00134
00135 glPopMatrix();
00136 }
00137 glPopMatrix();
00138 glEndList();
00139
00140 }