Changeset 157:cad7f155bd52
- Timestamp:
- 03/20/2012 03:40:41 AM (14 months ago)
- Branch:
- default
- Location:
- src/examples
- Files:
-
- 2 added
- 6 modified
-
flowingcanvas/CMakeLists.txt (modified) (1 diff)
-
flowingcanvas/flowwidget.h (modified) (2 diffs)
-
flowingcanvas/framegrabber.cpp (added)
-
flowingcanvas/framegrabber.h (added)
-
flowingcanvas/interactivesimulator.h (modified) (7 diffs)
-
flowingcanvas/interactivesimulatorcpu.h (modified) (4 diffs)
-
flowingcanvas/main.cpp (modified) (10 diffs)
-
latticegas/fpscounter.h (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
src/examples/flowingcanvas/CMakeLists.txt
r156 r157 14 14 set(MY_HEADERS 15 15 flowwidget.h 16 framegrabber.h 16 17 interactivesimulator.h) 17 18 -
src/examples/flowingcanvas/flowwidget.h
r156 r157 32 32 void ping() 33 33 { 34 emit updateImage( (unsigned*)image.scanLine(0), image.width(), image.height());34 emit updateImage(&image); 35 35 // if (simParamsHost.dumpFrames) 36 36 // dumpFrame(); … … 45 45 46 46 signals: 47 void updateImage( unsigned *image, unsigned width, unsigned height);47 void updateImage(QImage*); 48 48 49 49 private: -
src/examples/flowingcanvas/interactivesimulator.h
r156 r157 3 3 4 4 #include <iostream> 5 #include <QImage> 5 6 #include <QObject> 6 7 #include <QRunnable> … … 22 23 void updateCam(char *rawFrame, unsigned width, unsigned height) 23 24 { 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(); 24 34 } 25 35 … … 27 37 { 28 38 if (newCameraFrame.tryAcquire()) 29 loadStates();39 readCam(); 30 40 if (newOutputFrameRequested.tryAcquire()) { 31 41 renderOutput(); … … 35 45 update(); 36 46 incFrames(); 37 ++t;38 47 } 39 48 40 void renderImage( unsigned *image, unsigned width, unsigned height)49 void renderImage(QImage *image) 41 50 { 42 51 if (!running) { … … 45 54 46 55 outputFrame = image; 47 outputFrameWidth = width;48 outputFrameHeight = height;49 56 newOutputFrameRequested.release(); 50 57 newOutputFrameAvailable.acquire(); 51 58 } 52 59 53 virtual void loadStates() = 0;60 virtual void readCam() = 0; 54 61 virtual void renderOutput() = 0; 55 62 virtual void update() = 0; … … 65 72 while (running) { 66 73 step(); 67 std::cout << t<< " " << fps() << " FPS\r";74 std::cout << getFrames() << " " << fps() << " FPS\r"; 68 75 } 69 std::cout << "run done\n";70 std::cout << "newOutputFrameRequested: " << newOutputFrameRequested.available() << "\n"71 << "newOutputFrameAvailable: " << newOutputFrameAvailable.available() << "\n"72 << "newCameraFrame: " << newCameraFrame.available() << "\n";73 76 } 74 77 75 78 void quit() 76 79 { 77 std::cout << "i've been told to quit\n";78 80 running = false; 79 81 } 80 82 81 83 protected: 82 int t;83 84 volatile bool running; 84 85 QSemaphore newOutputFrameRequested; … … 86 87 QSemaphore newCameraFrame; 87 88 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; 91 93 }; 92 94 -
src/examples/flowingcanvas/interactivesimulatorcpu.h
r156 r157 13 13 public: 14 14 typedef typename CELL_TYPE::Topology Topology; 15 typedef Grid<CELL_TYPE, Topology>GridType;15 typedef typename SerialSimulator<CELL_TYPE>::GridType GridType; 16 16 InteractiveSimulatorCPU(QObject *parent, Initializer<CELL_TYPE> *initializer) : 17 17 InteractiveSimulator(parent), … … 20 20 21 21 virtual ~InteractiveSimulatorCPU() 22 {} 23 24 virtual void readCam() 22 25 { 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(); 25 31 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 } 29 38 } 30 39 … … 33 42 { 34 43 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 38 46 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); 42 75 } 43 76 } 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 // } 44 90 } 45 91 … … 51 97 private: 52 98 SerialSimulator<CELL_TYPE> sim; 53 std::vector<unsigned> frame; 99 54 100 }; 55 101 -
src/examples/flowingcanvas/main.cpp
r156 r157 3 3 #include <QThreadPool> 4 4 #include <libgeodecomp/examples/flowingcanvas/flowwidget.h> 5 #include <libgeodecomp/examples/flowingcanvas/framegrabber.h> 5 6 #include <libgeodecomp/examples/flowingcanvas/interactivesimulatorcpu.h> 6 7 #include <libgeodecomp/io/simpleinitializer.h> … … 32 33 static const int TILE_WIDTH = 4; 33 34 static const int TILE_HEIGHT = 4; 34 35 35 36 static inline unsigned nanoSteps() 36 37 { … … 41 42 Coord<2> _pos = Coord<2>(), 42 43 bool _forceSet = false, 43 FloatCoord<2> _forceFixed = FloatCoord<2>()) 44 FloatCoord<2> _forceFixed = FloatCoord<2>()) : 45 cameraLevel(0) 44 46 { 45 47 pos[0] = _pos.x(); … … 57 59 pos[0] = oldSelf.pos[0]; 58 60 pos[1] = oldSelf.pos[1]; 61 cameraPixel = oldSelf.cameraPixel; 62 59 63 forceSet = oldSelf.forceSet; 60 64 if (forceSet) { … … 72 76 } 73 77 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 } 86 97 87 98 // float gradient[2]; … … 106 117 } 107 118 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; 123 142 float pos[2]; 124 143 bool forceSet; 125 144 126 float camera; 127 float smoothCam; 145 float cameraLevel; 128 146 float forceVario[2]; 129 147 float forceFixed[2]; … … 141 159 public: 142 160 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) 144 164 {} 145 165 … … 155 175 if ((c.x() == 100) && (c.y() >= 100) && (c.y() <= 200)) { 156 176 setForce = true; 177 force.c[1] = -1; 178 } 179 if ((c.x() == 200) && (c.y() >= 100) && (c.y() <= 200)) { 180 setForce = true; 157 181 force.c[1] = 1; 158 }159 if ((c.x() == 200) && (c.y() >= 100) && (c.y() <= 200)) {160 setForce = true;161 force.c[1] = -1;162 182 } 163 183 if ((c.y() == 100) && (c.x() >= 100) && (c.x() <= 200)) { … … 184 204 &flow, 185 205 new CanvasInitializer()); 206 FrameGrabber *grabber = new FrameGrabber(true, &flow); 186 207 187 208 QTimer *timerFlow = new QTimer(&flow); 188 //QTimer *timerGrab = new QTimer(&flow);209 QTimer *timerGrab = new QTimer(&flow); 189 210 QTimer *timerInfo = new QTimer(&flow); 190 211 191 212 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())); 193 214 QObject::connect(timerInfo, SIGNAL(timeout()), sim, SLOT(info())); 194 215 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*))); 199 222 QObject::connect(&app, SIGNAL(lastWindowClosed()), sim, SLOT(quit())); 200 223 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())); 202 225 QObject::connect(&app, SIGNAL(lastWindowClosed()), timerInfo, SLOT(stop())); 203 226 … … 206 229 207 230 208 //grabber->grab();209 timerFlow->start( 10);210 // timerGrab->start(5000);231 grabber->grab(); 232 timerFlow->start(50); 233 timerGrab->start(500); 211 234 timerInfo->start(5000); 212 235 flow.show(); -
src/examples/latticegas/fpscounter.h
r156 r157 20 20 } 21 21 22 long long getFrames() 23 { 24 return frames; 25 } 26 22 27 double fps() 23 28 {
