Changeset 157:cad7f155bd52

Show
Ignore:
Timestamp:
03/20/2012 03:40:41 AM (14 months ago)
Author:
Andreas Schaefer <gentryx@…>
Branch:
default
Message:

flowing canvas demo is awfully slow, but advection forces are looking good

Location:
src/examples
Files:
2 added
6 modified

Legend:

Unmodified
Added
Removed
  • src/examples/flowingcanvas/CMakeLists.txt

    r156 r157  
    1414set(MY_HEADERS 
    1515  flowwidget.h 
     16  framegrabber.h 
    1617  interactivesimulator.h) 
    1718 
  • src/examples/flowingcanvas/flowwidget.h

    r156 r157  
    3232    void ping() 
    3333    { 
    34         emit updateImage((unsigned*)image.scanLine(0), image.width(), image.height());         
     34        emit updateImage(&image); 
    3535        // if (simParamsHost.dumpFrames)  
    3636        //     dumpFrame(); 
     
    4545 
    4646signals: 
    47     void updateImage(unsigned *image, unsigned width, unsigned height); 
     47    void updateImage(QImage*); 
    4848 
    4949private: 
  • src/examples/flowingcanvas/interactivesimulator.h

    r156 r157  
    33 
    44#include <iostream> 
     5#include <QImage> 
    56#include <QObject> 
    67#include <QRunnable> 
     
    2223    void updateCam(char *rawFrame, unsigned width, unsigned height) 
    2324    { 
     25        // fixme: we could get raceconditions here as the simulator 
     26        // may not have consumed the last camera frame 
     27        int frameSize = 3 * width * height; 
     28        cameraFrameWidth  = width; 
     29        cameraFrameHeight = height; 
     30        cameraFrame.resize(frameSize); 
     31        std::copy(rawFrame, rawFrame + frameSize, &cameraFrame[0]); 
     32 
     33        newCameraFrame.release(); 
    2434    } 
    2535 
     
    2737    { 
    2838        if (newCameraFrame.tryAcquire())  
    29             loadStates(); 
     39            readCam(); 
    3040        if (newOutputFrameRequested.tryAcquire()) { 
    3141            renderOutput(); 
     
    3545        update(); 
    3646        incFrames(); 
    37         ++t; 
    3847    } 
    3948 
    40     void renderImage(unsigned *image, unsigned width, unsigned height)  
     49    void renderImage(QImage *image)  
    4150    { 
    4251        if (!running) { 
     
    4554 
    4655        outputFrame = image; 
    47         outputFrameWidth = width; 
    48         outputFrameHeight = height; 
    4956        newOutputFrameRequested.release(); 
    5057        newOutputFrameAvailable.acquire(); 
    5158    } 
    5259 
    53     virtual void loadStates() = 0; 
     60    virtual void readCam() = 0; 
    5461    virtual void renderOutput() = 0; 
    5562    virtual void update() = 0; 
     
    6572        while (running) { 
    6673            step(); 
    67             std::cout << t << " " << fps() << " FPS\r"; 
     74            std::cout << getFrames() << " " << fps() << " FPS\r"; 
    6875        } 
    69         std::cout << "run done\n"; 
    70         std::cout << "newOutputFrameRequested: " << newOutputFrameRequested.available() << "\n" 
    71                   << "newOutputFrameAvailable: " << newOutputFrameAvailable.available() << "\n" 
    72                   << "newCameraFrame:          " << newCameraFrame.available() << "\n"; 
    7376    } 
    7477 
    7578    void quit() 
    7679    { 
    77         std::cout << "i've been told to quit\n"; 
    7880        running = false; 
    7981    } 
    8082 
    8183protected: 
    82     int t; 
    8384    volatile bool running; 
    8485    QSemaphore newOutputFrameRequested; 
     
    8687    QSemaphore newCameraFrame; 
    8788 
    88     unsigned *outputFrame; 
    89     volatile unsigned outputFrameWidth; 
    90     volatile unsigned outputFrameHeight; 
     89    QImage *outputFrame; 
     90    SuperVector<unsigned char> cameraFrame; 
     91    int cameraFrameWidth; 
     92    int cameraFrameHeight; 
    9193}; 
    9294 
  • src/examples/flowingcanvas/interactivesimulatorcpu.h

    r156 r157  
    1313public: 
    1414    typedef typename CELL_TYPE::Topology Topology; 
    15     typedef Grid<CELL_TYPE, Topology> GridType; 
     15    typedef typename SerialSimulator<CELL_TYPE>::GridType GridType; 
    1616    InteractiveSimulatorCPU(QObject *parent, Initializer<CELL_TYPE> *initializer) : 
    1717        InteractiveSimulator(parent), 
     
    2020 
    2121    virtual ~InteractiveSimulatorCPU() 
     22    {} 
     23 
     24    virtual void readCam() 
    2225    { 
    23         std::cout << "InteractiveSimulatorCPU dying\n"; 
    24     } 
     26        Coord<2> dim = sim.getInitializer()->gridDimensions(); 
     27        // fixme: ugly hack 
     28        GridType *grid = (GridType*)sim.getGrid(); 
     29        float factorX = 1.0 * cameraFrameWidth  / dim.x(); 
     30        float factorY = 1.0 * cameraFrameHeight / dim.y(); 
    2531 
    26     virtual void loadStates() 
    27     { 
    28         std::cout << "loadStates()\n"; 
     32        for (int y = 0; y < dim.y(); ++y) { 
     33            for (int x = 0; x < dim.x(); ++x) { 
     34                Coord<2> c(x, y); 
     35                (*grid)[c].readCam(&cameraFrame[0], factorX, factorY, cameraFrameWidth, cameraFrameHeight); 
     36            } 
     37        } 
    2938    } 
    3039 
     
    3342    { 
    3443        Coord<2> dim = sim.getInitializer()->gridDimensions(); 
    35         const typename SerialSimulator<CELL_TYPE>::GridType *grid = sim.getGrid(); 
    36         int maxX = std::min((int)outputFrameWidth,  dim.x()); 
    37         int maxY = std::min((int)outputFrameHeight, dim.y()); 
     44        const GridType *grid = sim.getGrid(); 
     45        
    3846 
    39         for (int y = 0; y < maxY; ++y) { 
    40             for (int x = 0; x < maxX; ++x) { 
    41                 outputFrame[y * outputFrameWidth + x] = (*grid)[Coord<2>(x, y)].toColor(); 
     47        int spacingX = 10; 
     48        int spacingY = 10; 
     49        float factorX = 1.0 * outputFrame->width()  / dim.x(); 
     50        float factorY = 1.0 * outputFrame->height() / dim.y(); 
     51 
     52        QPainter p(outputFrame); 
     53        p.setBrush(QBrush(Qt::black)); 
     54        p.drawRect(0, 0, outputFrame->width(), outputFrame->height()); 
     55        p.setBrush(QBrush(Qt::white)); 
     56        p.setPen(QPen(Qt::white)); 
     57 
     58        for (int y = 0; y < dim.y(); y += spacingY) { 
     59            for (int x = 0; x < dim.x(); x += spacingX) { 
     60                int startX = (x + 0.5) * factorX; 
     61                int startY = (y + 0.5) * factorY; 
     62                // float force0 = (*grid)[Coord<2>(x, y)].forceFixed[0]; 
     63                // float force1 = (*grid)[Coord<2>(x, y)].forceFixed[1]; 
     64                // float force0 = (*grid)[Coord<2>(x, y)].forceVario[0]; 
     65                // float force1 = (*grid)[Coord<2>(x, y)].forceVario[1]; 
     66                float force0 = (*grid)[Coord<2>(x, y)].forceVario[0] * 0.5 + 0.5 * (*grid)[Coord<2>(x, y)].forceFixed[0]; 
     67                float force1 = (*grid)[Coord<2>(x, y)].forceVario[1] * 0.5 + 0.5 * (*grid)[Coord<2>(x, y)].forceFixed[1]; 
     68                int offsetX = force0 * spacingX * factorX * 0.8; 
     69                int offsetY = force1 * spacingY * factorY * 0.8; 
     70                int endX = startX + offsetX; 
     71                int endY = startY + offsetY; 
     72                p.drawLine(startX, startY, endX, endY);  
     73                QRectF rec(endX - spacingX * 0.1, endY - spacingY * 0.1, spacingX * 0.2, spacingY * 0.2); 
     74                p.drawPie(rec, 0, 5760); 
    4275            } 
    4376        } 
     77 
     78        // for (int y = 0; y < dim.y(); ++y) { 
     79        //     for (int x = 0; x < dim.x(); ++x) { 
     80        //         unsigned col = (0xff << 24) + ((int)((*grid)[Coord<2>(x, y)].cameraLevel * 250) << 16); 
     81        //         outputFrame->setPixel(x, y, col); 
     82        //     } 
     83        // } 
     84         
     85        // for (int y = 0; y < dim.y(); ++y) { 
     86        //     for (int x = 0; x < dim.x(); ++x) { 
     87        //         outputFrame->setPixel(x, y, (*grid)[Coord<2>(x, y)].cameraPixel); 
     88        //     } 
     89        // } 
    4490    } 
    4591 
     
    5197private: 
    5298    SerialSimulator<CELL_TYPE> sim; 
    53     std::vector<unsigned> frame; 
     99 
    54100}; 
    55101 
  • src/examples/flowingcanvas/main.cpp

    r156 r157  
    33#include <QThreadPool> 
    44#include <libgeodecomp/examples/flowingcanvas/flowwidget.h> 
     5#include <libgeodecomp/examples/flowingcanvas/framegrabber.h> 
    56#include <libgeodecomp/examples/flowingcanvas/interactivesimulatorcpu.h> 
    67#include <libgeodecomp/io/simpleinitializer.h> 
     
    3233    static const int TILE_WIDTH = 4; 
    3334    static const int TILE_HEIGHT = 4; 
    34  
     35     
    3536    static inline unsigned nanoSteps() 
    3637    { 
     
    4142        Coord<2> _pos = Coord<2>(),  
    4243        bool _forceSet = false, 
    43         FloatCoord<2> _forceFixed = FloatCoord<2>())  
     44        FloatCoord<2> _forceFixed = FloatCoord<2>()) : 
     45        cameraLevel(0) 
    4446    { 
    4547        pos[0] = _pos.x(); 
     
    5759        pos[0] = oldSelf.pos[0]; 
    5860        pos[1] = oldSelf.pos[1]; 
     61        cameraPixel = oldSelf.cameraPixel; 
     62 
    5963        forceSet = oldSelf.forceSet; 
    6064        if (forceSet) { 
     
    7276        } 
    7377 
    74         // camera = std::min(0, oldSelf.camera - 1); 
    75         // smoothCam = (hood[Coord<2>(0, -1)].smoothCam + 
    76         //              hood[Coord<2>(-1, 0)].smoothCam + 
    77         //              hood[Coord<2>(1,  0)].smoothCam + 
    78         //              hood[Coord<2>(0,  1)].smoothCam) * 0.25; 
    79  
    80         // if ((camera < hood[Coord<2>(0, -1)].camera) || 
    81         //     (camera < hood[Coord<2>(-1, 0)].camera) || 
    82         //     (camera < hood[Coord<2>( 1, 0)].camera) || 
    83         //     (camera < hood[Coord<2>(0,  1)].camera)) { 
    84         //     smoothCam = camera; 
    85         // } 
     78        cameraLevel = (hood[Coord<2>(0, -1)].cameraLevel + 
     79                       hood[Coord<2>(-1, 0)].cameraLevel + 
     80                       hood[Coord<2>(1,  0)].cameraLevel + 
     81                       hood[Coord<2>(0,  1)].cameraLevel) * 0.25; 
     82         
     83        float gradientX = hood[Coord<2>(1, 0)].cameraLevel - hood[Coord<2>(-1, 0)].cameraLevel; 
     84        float gradientY = hood[Coord<2>(0, 1)].cameraLevel - hood[Coord<2>(0, -1)].cameraLevel; 
     85        forceVario[0] = 0; 
     86        if ((gradientX > 0.011) || (gradientX < -0.011)) { 
     87            forceVario[0] = 0.01 / gradientX; 
     88        } else { 
     89            forceVario[0] = 0; 
     90        } 
     91 
     92        if ((gradientY > 0.011) || (gradientY < -0.011)) { 
     93            forceVario[1] = 0.01 / gradientY; 
     94        } else { 
     95            forceVario[1] = 0; 
     96        } 
    8697 
    8798//         float gradient[2]; 
     
    106117    } 
    107118 
    108     unsigned toColor() const  
    109     { 
    110         unsigned a = 0xff; 
    111         unsigned r = 128 + 120 * forceFixed[0]; 
    112         unsigned g = 0; 
    113         unsigned b = 0; 
    114         return 
    115             (a << 24) + 
    116             (r << 16) + 
    117             (g <<  8) + 
    118             (b <<  0); 
    119     } 
    120  
    121 private: 
    122     char color[TILE_HEIGHT][TILE_WIDTH][3]; 
     119    void readCam(unsigned char *frame, const float& factorX, const float& factorY, const int& width, const int& height) 
     120    { 
     121        int posX = pos[0] * factorX; 
     122        int posY = pos[1] * factorY; 
     123         
     124        int index = posY * width * 3 + posX * 3; 
     125        cameraPixel = (0xff << 24) +  
     126            (frame[index + 0] << 16) + 
     127            (frame[index + 1] <<  8) + 
     128            (frame[index + 2] <<  0); 
     129 
     130        int val = (int)frame[index + 0] + (int)frame[index + 1] + (int)frame[index + 2]; 
     131        if (val < 500) { 
     132            cameraLevel = 1.0; 
     133        } else { 
     134            cameraLevel = std::max(0.0, cameraLevel - 0.05); 
     135        } 
     136    } 
     137 
     138    // fixme: 
     139// private: 
     140    unsigned color[TILE_HEIGHT][TILE_WIDTH]; 
     141    unsigned cameraPixel; 
    123142    float pos[2]; 
    124143    bool forceSet; 
    125144 
    126     float camera; 
    127     float smoothCam; 
     145    float cameraLevel; 
    128146    float forceVario[2]; 
    129147    float forceFixed[2]; 
     
    141159public: 
    142160    CanvasInitializer() : 
    143         SimpleInitializer<CanvasCell>(Coord<2>(640, 360), 100) 
     161        // SimpleInitializer<CanvasCell>(Coord<2>(240, 135), 100) 
     162        SimpleInitializer<CanvasCell>(Coord<2>(320, 180), 100) 
     163        // SimpleInitializer<CanvasCell>(Coord<2>(640, 360), 100) 
    144164    {} 
    145165 
     
    155175            if ((c.x() == 100) && (c.y() >= 100) && (c.y() <= 200)) { 
    156176                setForce = true; 
     177                force.c[1] = -1; 
     178            } 
     179            if ((c.x() == 200) && (c.y() >= 100) && (c.y() <= 200)) { 
     180                setForce = true; 
    157181                force.c[1] = 1; 
    158             } 
    159             if ((c.x() == 200) && (c.y() >= 100) && (c.y() <= 200)) { 
    160                 setForce = true; 
    161                 force.c[1] = -1; 
    162182            } 
    163183            if ((c.y() == 100) && (c.x() >= 100) && (c.x() <= 200)) { 
     
    184204        &flow, 
    185205        new CanvasInitializer()); 
     206    FrameGrabber *grabber = new FrameGrabber(true, &flow); 
    186207 
    187208    QTimer *timerFlow = new QTimer(&flow); 
    188     // QTimer *timerGrab = new QTimer(&flow); 
     209    QTimer *timerGrab = new QTimer(&flow); 
    189210    QTimer *timerInfo = new QTimer(&flow); 
    190211 
    191212    QObject::connect(timerInfo, SIGNAL(timeout()),           &flow,   SLOT(info())); 
    192     // QObject::connect(timerInfo, SIGNAL(timeout()),           grabber, SLOT(info())); 
     213    QObject::connect(timerInfo, SIGNAL(timeout()),           grabber, SLOT(info())); 
    193214    QObject::connect(timerInfo, SIGNAL(timeout()),           sim,     SLOT(info())); 
    194215    QObject::connect(timerFlow, SIGNAL(timeout()),           &flow,   SLOT(ping())); 
    195     // QObject::connect(timerGrab, SIGNAL(timeout()),           grabber, SLOT(grab())); 
    196  
    197     QObject::connect(&flow,     SIGNAL(updateImage(unsigned*, unsigned, unsigned)), 
    198                      sim,       SLOT(renderImage(unsigned*, unsigned, unsigned))); 
     216    QObject::connect(timerGrab, SIGNAL(timeout()),           grabber, SLOT(grab())); 
     217 
     218    QObject::connect(grabber,   SIGNAL(newFrame(char*, unsigned, unsigned)),  
     219                     sim,       SLOT(updateCam( char*, unsigned, unsigned))); 
     220    QObject::connect(&flow,     SIGNAL(updateImage(QImage*)), 
     221                     sim,       SLOT(renderImage(QImage*))); 
    199222    QObject::connect(&app,      SIGNAL(lastWindowClosed()),  sim,       SLOT(quit())); 
    200223    QObject::connect(&app,      SIGNAL(lastWindowClosed()),  timerFlow, SLOT(stop())); 
    201     // QObject::connect(&app,      SIGNAL(lastWindowClosed()),  timerGrab, SLOT(stop())); 
     224    QObject::connect(&app,      SIGNAL(lastWindowClosed()),  timerGrab, SLOT(stop())); 
    202225    QObject::connect(&app,      SIGNAL(lastWindowClosed()),  timerInfo, SLOT(stop())); 
    203226 
     
    206229 
    207230 
    208     // grabber->grab(); 
    209     timerFlow->start(10); 
    210     // timerGrab->start(5000); 
     231    grabber->grab(); 
     232    timerFlow->start(50); 
     233    timerGrab->start(500); 
    211234    timerInfo->start(5000); 
    212235    flow.show(); 
  • src/examples/latticegas/fpscounter.h

    r156 r157  
    2020    } 
    2121 
     22    long long getFrames() 
     23    { 
     24        return frames; 
     25    } 
     26 
    2227    double fps() 
    2328    {