]> rtime.felk.cvut.cz Git - eurobot/public.git/commitdiff
robomon: Map changed from rectangles to bitmap.
authorMichal Sojka <sojkam1@fel.cvut.cz>
Fri, 11 Feb 2011 16:26:41 +0000 (17:26 +0100)
committerMichal Sojka <sojkam1@fel.cvut.cz>
Fri, 11 Feb 2011 16:26:41 +0000 (17:26 +0100)
This should be faster. The map is not showed in appropriate scale now. Why??

Conflicts:

src/robomon/PlaygroundScene.cpp

src/robomon/Map.cpp [new file with mode: 0644]
src/robomon/Map.h [new file with mode: 0644]
src/robomon/PlaygroundScene.cpp
src/robomon/PlaygroundScene.h
src/robomon/RobomonAtlantis.cpp
src/robomon/RobomonAtlantis.h
src/robomon/robomon.pro

diff --git a/src/robomon/Map.cpp b/src/robomon/Map.cpp
new file mode 100644 (file)
index 0000000..9a93b7c
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * Map.cpp     11/01/31
+ *
+ * Draw a map on the playground.
+ *
+ * Copyright: (c) 2007 CTU Dragons
+ *            CTU FEE - Department of Control Engineering
+ * Authors: Michal Vokac, Michal Sojka
+ * License: GNU GPL v.2
+ */
+
+#include <math.h>
+#include <QPen>
+
+#include "PlaygroundScene.h"
+#include "Map.h"
+#include <map.h>
+#include <robodim.h>
+#include <iostream>
+
+Map::Map(const QPen &pen, const QBrush &brush) :
+       QObject(), QGraphicsItem()
+{
+       this->pen = pen;
+       this->brush = brush;
+       mapImage = QImage(MAP_WIDTH, MAP_HEIGHT, QImage::Format_ARGB32);
+       mapImage.fill(QColor(0, 0, 0, 200).rgba());
+       setVisible(false);
+}
+
+Map::~Map()
+{
+}
+
+QRectF Map::boundingRect() const
+{
+       return QRectF(0,0,PLAYGROUND_WIDTH_MM,PLAYGROUND_HEIGHT_MM);
+}
+
+void Map::paint(QPainter *painter,
+               const QStyleOptionGraphicsItem *option, QWidget *widget)
+{
+       Q_UNUSED(option);
+       Q_UNUSED(widget);
+       Q_UNUSED(painter);
+
+       painter->setPen(pen);
+       painter->setBrush(brush);
+       painter->drawImage(QPointF(0,0),mapImage) ;
+}
+
+void Map::setPixelColor(int x, int y, QColor color)
+{
+       QRgb value;
+
+       value = color.rgb();
+
+       mapImage.setPixel(x, y, value);
+}
\ No newline at end of file
diff --git a/src/robomon/Map.h b/src/robomon/Map.h
new file mode 100644 (file)
index 0000000..2f58958
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * Map.h       11/01/31
+ *
+ * Draw a map on the playground.
+ *
+ * Copyright: (c) 2007 CTU Dragons
+ *            CTU FEE - Department of Control Engineering
+ * Authors: Michal Vokac, Michal Sojka
+ * License: GNU GPL v.2
+ */
+
+#ifndef MAP_H
+#define MAP_H
+
+#include <QGraphicsItem>
+#include <QPainter>
+#include <QObject>
+
+class Map : public QObject, public QGraphicsItem
+{
+       Q_OBJECT
+       Q_INTERFACES(QGraphicsItem);
+public:
+       Map(const QPen &pen = QPen(), const QBrush &brush = QBrush());
+       ~Map();
+       QRectF boundingRect() const;
+       void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
+       void setPixelColor(int x, int y, QColor color);
+public slots:
+private:
+       QImage mapImage;
+       QPen pen;
+       QBrush brush;
+};
+
+#endif
index 82e409ed8fdf6e64264d15b0d69a0d592fa37ab8..8d3e4695a6bafd5103d0cb3c7d203eeccb64e013 100644 (file)
@@ -6,7 +6,7 @@
  * Copyright: (c) 2007 CTU Dragons
  *            CTU FEE - Department of Control Engineering
  * Authors: Martin Zidek, Michal Sojka, Tran Duy Khanh
- * Edit:               Petr Kubiznak
+ *          Michal Vokac, Petr Kubiznak
  * License: GNU GPL v.2
  */
 
@@ -14,6 +14,8 @@
 #include <QGraphicsSceneMouseEvent>
 #include <QGraphicsScene>
 #include <QGraphicsRectItem>
+#include <QImage>
+
 using namespace Qt;
 
 #include "PlaygroundScene.h"
@@ -22,16 +24,19 @@ using namespace Qt;
 #include "../robofsm/corns_configs.h"
 
 //margin around borders (just for better look)
-#define MARGIN                                                 40
+#define MARGIN 40
 
 //playground borders
-#define BORDER_WIDTH                           22
-#define BORDER_COLOR                           black
+#define BORDER_WIDTH   22
+#define BORDER_COLOR   black
 
 //playground
-#define PLAYGROUND_WIDTH               3000
-#define PLAYGROUND_HEIGHT              2100
-#define PLAYGROUND_COLOR               green
+/*FIXME update accordingly to playground dimmensions in robodim.h
+Why robomon playground do not accept defined values from robodim.h?
+*/
+#define PLAYGROUND_WIDTH       PLAYGROUND_WIDTH_MM
+#define PLAYGROUND_HEIGHT      PLAYGROUND_HEIGHT_MM
+#define PLAYGROUND_COLOR       green
 
 //squares
 #define SQUARE_WIDTH                   350
@@ -114,7 +119,6 @@ void PlaygroundScene::putQueen(QGraphicsEllipseItem *g, int centerX, int centerY
        g = addEllipse(QRect(centerX - FIGURE_WIDTH/2, centerY - FIGURE_WIDTH/2, FIGURE_WIDTH, FIGURE_WIDTH), QPen(), QBrush(QUEEN_COLOR));             
        g->setZValue(3);
 } 
-
 PlaygroundScene::PlaygroundScene(QObject *parent)
        : QGraphicsScene(parent)
 {
@@ -123,17 +127,17 @@ PlaygroundScene::PlaygroundScene(QObject *parent)
        
        /* All scene units are milimeters */
        setSceneRect(QRectF(QPointF(-MARGIN, -MARGIN), QPointF(PLAYGROUND_WIDTH+MARGIN, PLAYGROUND_HEIGHT+MARGIN)));
-       
+
        /* playground border */
        addRect(QRect(0, 0, -BORDER_WIDTH, PLAYGROUND_HEIGHT), QPen(), QBrush(BORDER_COLOR));           //left
        addRect(QRect(-BORDER_WIDTH, PLAYGROUND_HEIGHT, PLAYGROUND_WIDTH + 2*BORDER_WIDTH, BORDER_WIDTH), QPen(), QBrush(BORDER_COLOR));                //top
        addRect(QRect(PLAYGROUND_WIDTH, 0, BORDER_WIDTH, PLAYGROUND_HEIGHT), QPen(), QBrush(BORDER_COLOR));             //right
        addRect(QRect(-BORDER_WIDTH, -BORDER_WIDTH, PLAYGROUND_WIDTH + 2*BORDER_WIDTH, BORDER_WIDTH), QPen(), QBrush(BORDER_COLOR));            //bottom
-       
+
        /* playground */
        tempRect = addRect(QRect(0, 0, PLAYGROUND_WIDTH, PLAYGROUND_HEIGHT), QPen(), PLAYGROUND_COLOR);
        tempRect->setZValue(0);
-       
+
        /* squares */
        for(int i = 0; i<6; i++){
          for(int j = 0; j<6; j++){
@@ -225,8 +229,6 @@ PlaygroundScene::PlaygroundScene(QObject *parent)
        obstacle->setVisible(false);
        obstacle->setPos(QPointF(2000, 1000));
        this->addItem(obstacle);
-       
-       initMap();
 }
 
 PlaygroundScene::~PlaygroundScene()
@@ -255,7 +257,7 @@ void PlaygroundScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
 QPointF PlaygroundScene::scene2world(QPointF scenePos)
 {
        return QPointF((scenePos.x()) / 1000.0,
-                       (scenePos.y()) / 1000.0);
+                      (scenePos.y()) / 1000.0);
 }
 
 
@@ -279,35 +281,3 @@ void PlaygroundScene::showObstacle(int val)
        else
                obstacle->setVisible(false);
 }
-
-void PlaygroundScene::initMap()
-{
-#define MAP_RECT_SCALE 1.0
-       this->map = new QGraphicsItemGroup();
-       for(int i=0; i < MAP_WIDTH; i++) {
-               for(int j=0; j<MAP_HEIGHT; j++) {
-                       rects[i][j] = new QGraphicsRectItem(
-                               QRectF(i*MAP_CELL_SIZE_MM + MAP_CELL_SIZE_MM * (1 - MAP_RECT_SCALE)/2,
-                                      (MAP_HEIGHT-1-j)*MAP_CELL_SIZE_MM + MAP_CELL_SIZE_MM * (1 - MAP_RECT_SCALE)/2,
-                                      MAP_CELL_SIZE_MM * MAP_RECT_SCALE ,
-                                      MAP_CELL_SIZE_MM * MAP_RECT_SCALE));
-                       rects[i][j]->setPen(QPen(Qt::NoPen));
-                       rects[i][j]->setBrush(QBrush(Qt::lightGray));
-                       map->addToGroup(rects[i][j]);
-               }
-       }
-       map->setVisible(false);
-       map->setZValue(4);
-       addItem(map);
-}
-
-void PlaygroundScene::showMap(bool show)
-{
-       map->setVisible(show);
-}
-
-void PlaygroundScene::setMapColor(int x, int y, QColor color)
-{
-       color.setAlpha(200);
-       rects[x][y]->setBrush(QBrush(color));
-}
index 7562b0d10afc6d62daf6764f3935b617aa8b8dc5..6ae88be7de74df5a9c8f971b605fe51e53a4fae3 100644 (file)
@@ -13,6 +13,7 @@
 #define PLAYGROUND_SCENE_H
 
 #include <QGraphicsScene>
+#include <QPainter>
 #include <map.h>
 
 #define SIM_OBST_SIZE_M 0.3
@@ -26,8 +27,6 @@ public:
        ~PlaygroundScene();
        static QPointF scene2world(QPointF scenePos);
        static QPointF world2scene(QPointF worldPos);
-       void setMapColor(int x, int y, QColor color);
-
 signals:
        void obstacleChanged(QPointF pos);
        void mouseMoved(QPointF pos);
@@ -37,9 +36,6 @@ protected:
        virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent);
 
 private:
-       void initMap();
-       QGraphicsItemGroup *map;
-       QGraphicsRectItem *rects[MAP_WIDTH][MAP_HEIGHT];  // This must be slow - convert to pixmap. -- M.S.
        QGraphicsEllipseItem *obstacle;
        
        void paintCorns(int side_configuration, int center_configuration);
@@ -48,10 +44,9 @@ private:
        void putPawn(QGraphicsEllipseItem *g, int centerX, int centerY);
        void putKing(QGraphicsEllipseItem *g, int centerX, int centerY);
        void putQueen(QGraphicsEllipseItem *g, int centerX, int centerY);
-       
+
 public slots:
        void showObstacle(int val);
-       void showMap(bool show);
 };
 
 #endif
index 06849fcab4334fa87a44758068cc7ad181654869..fdf734f4cb3d9e90f3a27e791868ef4f18a2f6c5 100644 (file)
@@ -65,6 +65,7 @@ RobomonAtlantis::RobomonAtlantis(QWidget *parent)
        createOrte();
        createRobots();
        createActions();
+       createMap();
 
 //     connect(vidle, SIGNAL(valueChanged(int)),
 //             robotEstPosBest, SLOT(setVidle(int)));
@@ -277,46 +278,6 @@ void RobomonAtlantis::createPowerGroupBox()
        powerGroupBox->setLayout(layout);
 }
 
-#if 0
-void RobomonAtlantis::createMotorsGroupBox()
-{
-       enginesGroupBox = new QGroupBox(tr("Motors"));
-       QVBoxLayout *layout = new QVBoxLayout();
-       QHBoxLayout *layout1 = new QHBoxLayout();
-       QHBoxLayout *layout2 = new QHBoxLayout();
-
-       leftMotorSlider = new QSlider(Qt::Vertical);
-       rightMotorSlider = new QSlider(Qt::Vertical);
-       bothMotorsCheckBox = new QCheckBox(tr("Lock both"));
-       stopMotorsPushButton = new QPushButton(tr("Stop Motors"));
-
-       leftMotorSlider->setMinimum(-100);
-       leftMotorSlider->setMaximum(100);
-       leftMotorSlider->setTracking(false);
-       leftMotorSlider->setTickPosition(QSlider::TicksLeft);
-
-       rightMotorSlider->setMinimum(-100);
-       rightMotorSlider->setMaximum(100);
-       rightMotorSlider->setTracking(false);
-       rightMotorSlider->setTickPosition(QSlider::TicksRight);
-
-       stopMotorsPushButton->setMaximumWidth(90);
-
-       layout1->addWidget(leftMotorSlider);
-       layout1->addWidget(MiscGui::createLabel("0"));
-       layout1->addWidget(rightMotorSlider);
-
-       layout2->addWidget(bothMotorsCheckBox);
-
-       layout->addWidget(MiscGui::createLabel("100"));
-       layout->addLayout(layout1);
-       layout->addWidget(MiscGui::createLabel("-100"));
-       layout->addLayout(layout2);
-       layout->addWidget(stopMotorsPushButton);
-       enginesGroupBox->setLayout(layout);
-}
-#endif
-
 void RobomonAtlantis::createRobots()
 {
        robotRefPos = new Robot("Ref", QPen(Qt::darkBlue), QBrush(Qt::NoBrush));
@@ -356,6 +317,14 @@ void RobomonAtlantis::createRobots()
 
 }
 
+void RobomonAtlantis::createMap()
+{
+       mapImage = new Map(QPen(Qt::lightGray), QBrush(Qt::NoBrush));
+       mapImage->setZValue(3);
+
+       playgroundScene->addItem(mapImage);
+}
+
 /**********************************************************************
  * GUI actions
  **********************************************************************/
@@ -476,7 +445,7 @@ void RobomonAtlantis::showMap(bool show)
                        disconnect(mapTimer, SIGNAL(timeout()), this, SLOT(paintMap()));
                }
        }
-       playgroundScene->showMap(show);
+       mapImage->setVisible(show);
 }
 
 void RobomonAtlantis::paintMap()
@@ -521,7 +490,7 @@ void RobomonAtlantis::paintMap()
                         if (cell->flags & MAP_FLAG_DET_OBST)
                                 color = cyan;
 
-                        playgroundScene->setMapColor(i, j, color);
+                        mapImage->setPixelColor(i, j, color);
                 }
        }
 }
index 8dc2473ad03bc02dc62360491f4279c2ebc2f79c..f8756f46612e63d036ae3b4d03d68a774c817cdc 100644 (file)
@@ -19,6 +19,7 @@
 #include "PlaygroundScene.h"
 #include "playgroundview.h"
 #include "Robot.h"
+#include "Map.h"
 #include <roboorte_robottype.h>
 #include "trail.h"
 #include "hokuyoscan.h"
@@ -37,6 +38,7 @@ class QDial;
 class QSlider;
 class QProgressBar;
 class QFont;
+class QImage;
 
 class RobomonAtlantis : public QWidget
 {
@@ -55,7 +57,7 @@ signals:
        void motionStatusReceivedSignal();
        void actualPositionReceivedSignal();
        void powerVoltageReceivedSignal();
-       
+
 public slots:
        void showMap(bool show);
        void useOpenGL(bool use);
@@ -64,7 +66,7 @@ public slots:
        void resetTrails();
 private slots:
        /************************************************************
-        * GUI actions 
+        * GUI actions
         ************************************************************/
        void setVoltage33(int state);
        void setVoltage50(int state);
@@ -81,7 +83,7 @@ private slots:
        void setTeamColor(int plug);
 
        /************************************************************
-        * ORTE 
+        * ORTE
         ************************************************************/
        void motionStatusReceived();
        void actualPositionReceived();
@@ -108,6 +110,7 @@ private:
 
        void createRobots();
        void createActions();
+       void createMap();
 
        QVBoxLayout *leftLayout;
        QVBoxLayout *rightLayout;
@@ -167,6 +170,8 @@ public:
        Robot *robotEstPosBest;
        Robot *robotEstPosIndepOdo;
        Robot *robotEstPosOdo;
+
+       Map *mapImage;
 private:
        Trail *trailRefPos;
        Trail *trailEstPosBest;
@@ -183,7 +188,7 @@ private:
        void openSharedMemory();
        bool sharedMemoryOpened;
        QTimer *mapTimer;
-       
+
        /* obstacle simulation */
        double distanceToWallHokuyo(int beamnum);
        double distanceToObstacleHokuyo(int beamnum, Point obstacle, double obstacleSize);
@@ -193,7 +198,7 @@ private:
        Point simulatedObstacle;
 
        /************************************************************
-        * ORTE 
+        * ORTE
         ************************************************************/
        void createOrte();
 
index a76922e5281cfc0dbbc5dff56185ef4497674cf7..f647dd812b5cf6242ba33122a92ae8b2bbce9487 100644 (file)
@@ -5,6 +5,7 @@ SOURCES += main.cpp \
            RobomonTuning.cpp \
            PlaygroundScene.cpp \
            Robot.cpp \
+           Map.cpp \
            Widget.cpp \
            GlWidget.cpp \
            MiscGui.cpp \
@@ -26,6 +27,7 @@ HEADERS += MainWindow.h \
            RobomonTuning.h \
            PlaygroundScene.h \
            Robot.h \
+           Map.h \
            Painter.h \
            Widget.h \
            GlWidget.h \