00001 #include "PolarGridRenderer.h" 00002 00003 PolarGridRenderer::PolarGridRenderer(const std::vector< std::vector<double> > *grid, const std::vector<double> *phiEdges, const std::vector<double> *rhoEdges): 00004 m_grid(NULL), 00005 m_phiEdges(NULL), 00006 m_rhoEdges(NULL), 00007 m_GLUGrids(0), 00008 m_GLUSectors(0), 00009 m_position(0), 00010 m_maxValue(0), 00011 m_color(0.4f, 1.f, 0.5f, 1.0f) 00012 { 00013 if(grid && phiEdges && rhoEdges && grid->size() && grid->size() == phiEdges->size() - 1 && (*grid)[0].size() == rhoEdges->size() - 1){ 00014 m_grid = grid; 00015 m_phiEdges = phiEdges; 00016 m_rhoEdges = rhoEdges; 00017 for(unsigned int i = 0; i < m_grid->size(); i ++){ 00018 for(unsigned int j = 0; j < (*m_grid)[i].size(); j++){ 00019 if((*grid)[i][j] > m_maxValue ) m_maxValue = (*grid)[i][j]; 00020 } 00021 } 00022 } 00023 00024 if(m_grid){ 00025 m_GLUGrids.resize(m_grid->size() * (*m_grid)[0].size()); 00026 m_GLUSectors.resize(m_grid->size() * (*m_grid)[0].size()); 00027 for(unsigned int i = 0; i < m_GLUGrids.size(); i++){ 00028 m_GLUGrids[i] = gluNewQuadric(); 00029 m_GLUSectors[i] = gluNewQuadric(); 00030 } 00031 } 00032 00033 m_depth = 0.f; 00034 setSubdivision(6, 3); 00035 } 00036 00037 PolarGridRenderer::~PolarGridRenderer(){ 00038 for(unsigned int i = 0; i < m_GLUGrids.size(); i++){ 00039 gluDeleteQuadric(m_GLUGrids[i]); 00040 gluDeleteQuadric(m_GLUSectors[i]); 00041 } 00042 } 00043 00044 PolarGridRenderer::PolarGridRenderer(const PolarGridRenderer& _renderer): 00045 m_grid(_renderer.m_grid), 00046 m_phiEdges(_renderer.m_phiEdges), 00047 m_rhoEdges(_renderer.m_rhoEdges), 00048 m_GLUGrids(0), 00049 m_GLUSectors(0), 00050 m_maxValue(_renderer.m_maxValue), 00051 m_color(_renderer.m_color) 00052 { 00053 if(m_grid){ 00054 m_GLUGrids.resize(m_grid->size() * (*m_grid)[0].size()); 00055 m_GLUSectors.resize(m_grid->size() * (*m_grid)[0].size()); 00056 for(unsigned int i = 0; i < m_GLUGrids.size(); i++){ 00057 m_GLUGrids[i] = gluNewQuadric(); 00058 m_GLUSectors[i] = gluNewQuadric(); 00059 } 00060 } 00061 m_depth = _renderer.m_depth; 00062 _renderer.getSubdivision(m_subdivision[0], m_subdivision[1]); 00063 } 00064 00065 PolarGridRenderer& PolarGridRenderer::operator=(const PolarGridRenderer& _renderer){ 00066 setGrid(_renderer.getGrid(), _renderer.getPhiEdges(), _renderer.getRhoEdges()); 00067 m_color = _renderer.m_color; 00068 _renderer.getSubdivision(m_subdivision[0], m_subdivision[1]); 00069 m_depth = _renderer.m_depth; 00070 return *this; 00071 } 00072 00073 void PolarGridRenderer::setGrid(const std::vector< std::vector<double> > *grid, const std::vector<double> *phiEdges, const std::vector<double> *rhoEdges){ 00074 if(grid && phiEdges && rhoEdges && grid->size() && grid->size() == phiEdges->size() - 1 && (*grid)[0].size() == rhoEdges->size() - 1){ 00075 m_grid = grid; 00076 m_phiEdges = phiEdges; 00077 m_rhoEdges = rhoEdges; 00078 m_maxValue = 0.; 00079 for(unsigned int i = 0; i < m_grid->size(); i ++){ 00080 for(unsigned int j = 0; j < (*m_grid)[i].size(); j++){ 00081 if((*grid)[i][j] > m_maxValue ) m_maxValue = (*grid)[i][j]; 00082 } 00083 } 00084 } else { 00085 m_grid = NULL; 00086 m_phiEdges = NULL; 00087 m_rhoEdges = NULL; 00088 return; 00089 } 00090 00091 if(m_grid->size() * (*m_grid)[0].size() == m_GLUGrids.size()) return; 00092 00093 unsigned int i = m_grid->size() * (*m_grid)[0].size(); 00094 for(; i < m_GLUGrids.size(); i++){ 00095 gluDeleteQuadric(m_GLUGrids[i]); 00096 gluDeleteQuadric(m_GLUSectors[i]); 00097 } 00098 00099 i = m_GLUGrids.size(); 00100 m_GLUGrids.resize(m_grid->size() * (*m_grid)[0].size()); 00101 m_GLUSectors.resize(m_grid->size() * (*m_grid)[0].size()); 00102 for(; i < m_GLUGrids.size(); i++){ 00103 m_GLUGrids[i] = gluNewQuadric(); 00104 m_GLUSectors[i] = gluNewQuadric(); 00105 } 00106 00107 } 00108 00109 00110 void PolarGridRenderer::render(){ 00111 if(m_grid){ 00112 glPushMatrix(); 00113 glTranslatef(0.f, 0.f, m_depth); 00114 if(m_position){ 00115 glTranslatef(m_position->x, m_position->y, 0.f); 00116 glRotatef(rad2deg(m_position->theta), 0.f, 0.f, 1.f); 00117 } 00118 double sweep = 2*M_PI/(m_phiEdges->size() - 1); 00119 for(unsigned int i = 0; i < m_grid->size(); i ++){ 00120 for(unsigned int j = 0; j < (*m_grid)[i].size(); j++){ 00121 glPushMatrix(); 00122 m_color.lightness() = m_maxValue == 0.? 1. : 1. - 0.5*(*m_grid)[i][j]/m_maxValue; 00123 Color color(HSL2RGB(m_color)); 00124 double inner = (*m_rhoEdges)[j]; 00125 double outer = (*m_rhoEdges)[j+1]; 00126 double start = -(*m_phiEdges)[i] + M_PI_2; 00127 glColor4f(color.red(), color.green(), color.blue(), color.alpha()); 00128 gluQuadricDrawStyle(m_GLUSectors[i * (*m_grid)[i].size() + j], GLU_FILL); 00129 gluPartialDisk(m_GLUSectors[i * (*m_grid)[i].size() + j], inner, outer, m_subdivision[0], m_subdivision[1], rad2deg(start), rad2deg(-sweep)); 00130 glColor4f(0.f, 0.f, 0.f, 1.f); 00131 gluQuadricDrawStyle(m_GLUGrids[i * (*m_grid)[i].size() + j], GLU_SILHOUETTE); 00132 gluPartialDisk(m_GLUGrids[i * (*m_grid)[i].size() + j], inner, outer, m_subdivision[0], m_subdivision[1], rad2deg(start), rad2deg(-sweep)); 00133 glPopMatrix(); 00134 } 00135 } 00136 glColor4f(0.f,0.f,0.f,1.f); 00137 glBegin(GL_LINES); 00138 glVertex3f(0.f, 0.f, 0.f); 00139 glVertex3f((*m_rhoEdges)[m_rhoEdges->size() - 1] + 0.1f, 0.f, 0.f); 00140 glEnd(); 00141 glPopMatrix(); 00142 } 00143 }