00001 #include "PolarGridGraphicsItem.h"
00002
00003 #include <cmath>
00004 #include <geometry/point.h>
00005
00006
00007 PolarGridGraphicsItem::PolarGridGraphicsItem(const std::vector< std::vector<double> > *grid, const std::vector<double> *phiEdges, const std::vector<double> *rhoEdges, QGraphicsItem* parent):
00008 QGraphicsItem(parent),
00009 m_grid(NULL),
00010 m_phiEdges(NULL),
00011 m_rhoEdges(NULL),
00012 m_maxValue(0),
00013 m_color(Qt::black)
00014 {
00015 if(grid && phiEdges && rhoEdges && grid->size() && grid->size() == phiEdges->size() - 1 && (*grid)[0].size() == rhoEdges->size() - 1){
00016 m_grid = grid;
00017 m_phiEdges = phiEdges;
00018 m_rhoEdges = rhoEdges;
00019 for(unsigned int i = 0; i < m_grid->size(); i ++){
00020 for(unsigned int j = 0; j < (*m_grid)[i].size(); j++){
00021 if((*grid)[i][j] > m_maxValue ) m_maxValue = (*grid)[i][j];
00022 }
00023 }
00024 }
00025
00026 setFlag(QGraphicsItem::ItemIsSelectable);
00027 }
00028
00029 void PolarGridGraphicsItem::setGrid(const std::vector< std::vector<double> > *grid, const std::vector<double> *phiEdges, const std::vector<double> *rhoEdges){
00030 if(grid && phiEdges && rhoEdges && grid->size() && grid->size() == phiEdges->size() - 1 && (*grid)[0].size() == rhoEdges->size() - 1){
00031 m_grid = grid;
00032 m_phiEdges = phiEdges;
00033 m_rhoEdges = rhoEdges;
00034 m_maxValue = 0.;
00035 for(unsigned int i = 0; i < m_grid->size(); i ++){
00036 for(unsigned int j = 0; j < (*m_grid)[i].size(); j++){
00037 if((*grid)[i][j] > m_maxValue ) m_maxValue = (*grid)[i][j];
00038 }
00039 }
00040 } else {
00041 m_grid = NULL;
00042 m_phiEdges = NULL;
00043 m_rhoEdges = NULL;
00044 return;
00045 }
00046 }
00047
00048 void PolarGridGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
00049 if(m_grid){
00050 painter->setPen(Qt::black);
00051 m_color.setHsvF(m_color.hueF(),1.f,m_color.valueF());
00052 double sweep = normAngle(2*M_PI/(m_phiEdges->size() - 1), 0.);
00053 double maxEdge = m_rhoEdges->back();
00054 double minEdge = m_rhoEdges->front();
00055 for(unsigned int i = 0; i < m_grid->size(); i ++){
00056 double start = normAngle((*m_phiEdges)[i], 0.);
00057 for(unsigned int j = 0; j < (*m_grid)[i].size(); j++){
00058 float value = m_maxValue == 0.? 0. : (*m_grid)[i][j]/(m_maxValue + 0.5);
00059 m_color.setHsvF(m_color.hueF(), m_color.saturationF(), 1 - value);
00060 double inner = double(128) * ((*m_rhoEdges)[j] - minEdge)/(maxEdge - minEdge);
00061 double outer = double(128) * ((*m_rhoEdges)[j + 1] - minEdge)/(maxEdge - minEdge);
00062
00063 QPainterPath path;
00064 path.moveTo(inner/2. * cos(start), -inner/2. * sin(start));
00065 path.arcTo(-outer/2., -outer/2., outer, outer, rad2deg(start), rad2deg(sweep));
00066 path.arcTo(-floor(inner/2), -floor(inner/2), floor(inner), floor(inner), rad2deg(start + sweep), rad2deg(-sweep));
00067 path.closeSubpath();
00068 painter->setBrush(m_color);
00069 painter->drawPath(path);
00070 }
00071 }
00072 painter->drawText(QRectF(-64, 135 - 64, 128, 25), Qt::AlignCenter, m_text);
00073 }
00074 }
00075
00076 QRectF PolarGridGraphicsItem::boundingRect() const{
00077 return QRectF(-64, -64, 128, 160);
00078 }
00079
00080 QPainterPath PolarGridGraphicsItem::shape() const{
00081 QPainterPath path;
00082 path.addEllipse(boundingRect());
00083 return path;
00084 }
00085
00086