Mandelbrot Example

Files:

The Mandelbrot example shows how to use a worker thread to perform heavy computations without blocking the main thread's event loop.

The heavy computation here is the Mandelbrot set, probably the world's most famous fractal. These days, while sophisticated programs such as XaoS that provide real-time zooming in the Mandelbrot set, the standard Mandelbrot algorithm is just slow enough for our purposes.

Screenshot of the Mandelbrot example

Screenshot of the Mandelbrot example

In real life, the approach described here is applicable to a large set of problems, including synchronous network I/O and database access, where the user interface must remain responsive while some heavy operation is taking place. The network/blockingfortuneclient example shows the same principle at work in a TCP client.

The Mandelbrot application supports zooming and scrolling using the mouse or the keyboard. To avoid freezing the main thread's event loop (and, as a consequence, the application's user interface), we put all the fractal computation in a separate worker thread. The thread emits a signal when it is done rendering the fractal.

During the time where the worker thread is recomputing the fractal to reflect the new zoom factor position, the main thread simply scales the previously rendered pixmap to provide immediate feedback. The result doesn't look as good as what the worker thread eventually ends up providing, but at least it makes the application more responsive. The sequence of screenshots below shows the original image, the scaled image, and the rerendered image.

Similarly, when the user scrolls, the previous pixmap is scrolled immediately, revealing unpainted areas beyond the edge of the pixmap, while the image is rendered by the worker thread.

The application consists of two classes:

  • RenderThread is a QThread subclass that renders the Mandelbrot set.
  • MandelbrotWidget is a QWidget subclass that shows the Mandelbrot set on screen and lets the user zoom and scroll.

If you are not already familiar with Qt's thread support, we recommend that you start by reading the Thread Support in Qt overview.

RenderThread Class Definition

We'll start with the definition of the RenderThread class:

  1. class RenderThread : public QThread
  2.  {
  3.     Q_OBJECT
  4.  
  5. public:
  6.     RenderThread(QObject *parent = 0);
  7.     ~RenderThread();
  8.  
  9.     void render(double centerX, double centerY, double scaleFactor,
  10.                 QSize resultSize);
  11.  
  12. signals:
  13.     void renderedImage(const QImage &image, double scaleFactor);
  14.  
  15. protected:
  16.     void run();
  17.  
  18. private:
  19.     uint rgbFromWaveLength(double wave);
  20.  
  21.     QMutex mutex;
  22.     QWaitCondition condition;
  23.     double centerX;
  24.     double centerY;
  25.     double scaleFactor;
  26.     QSize resultSize;
  27.     bool restart;
  28.     bool abort;
  29.  
  30.     enum  { ColormapSize = 512 };
  31.     uint colormap[ColormapSize];
  32. };

The class inherits QThread so that it gains the ability to run in a separate thread. Apart from the constructor and destructor, render() is the only public function. Whenever the thread is done rendering an image, it emits the renderedImage() signal.

The protected run() function is reimplemented from QThread. It is automatically called when the thread is started.

In the private section, we have a QMutex, a QWaitCondition, and a few other data members. The mutex protects the other data member.

RenderThread Class Implementation

  1. RenderThread::RenderThread(QObject *parent)
  2.     : QThread(parent)
  3.  {
  4.     restart = false;
  5.     abort = false;
  6.  
  7.     for (int i = 0; i < ColormapSize; ++i)
  8.         colormap[i] = rgbFromWaveLength(380.0 + (i * 400.0 / ColormapSize));
  9. }

In the constructor, we initialize the restart and abort variables to false. These variables control the flow of the run() function.

We also initialize the colormap array, which contains a series of RGB colors.

  1. RenderThread::~RenderThread()
  2.  {
  3.     mutex.lock();
  4.     abort = true;
  5.     condition.wakeOne();
  6.     mutex.unlock();
  7.  
  8.     wait();
  9. }

The destructor can be called at any point while the thread is active. We set abort to true to tell run() to stop running as soon as possible. We also call QWaitCondition::wakeOne() to wake up the thread if it's sleeping. (As we will see when we review run(), the thread is put to sleep when it has nothing to do.)

The important thing to notice here is that run() is executed in its own thread (the worker thread), whereas the RenderThread constructor and destructor (as well as the render() function) are called by the thread that created the worker thread. Therefore, we need a mutex to protect accesses to the abort and condition variables, which might be accessed at any time by run().

At the end of the destructor, we call QThread::wait() to wait until run() has exited before the base class destructor is invoked.

  1. void RenderThread::render(double centerX, double centerY, double scaleFactor,
  2.                           QSize resultSize)
  3.  {
  4.     QMutexLocker locker(&mutex);
  5.  
  6.     this->centerX = centerX;
  7.     this->centerY = centerY;
  8.     this->scaleFactor = scaleFactor;
  9.     this->resultSize = resultSize;
  10.  
  11.     if (!isRunning())  {
  12.         start(LowPriority);
  13.     } else  {
  14.         restart = true;
  15.         condition.wakeOne();
  16.     }
  17. }

The render() function is called by the MandelbrotWidget whenever it needs to generate a new image of the Mandelbrot set. The centerX, centerY, and scaleFactor parameters specify the portion of the fractal to render; resultSize specifies the size of the resulting QImage.

The function stores the parameters in member variables. If the thread isn't already running, it starts it; otherwise, it sets restart to true (telling run() to stop any unfinished computation and start again with the new parameters) and wakes up the thread, which might be sleeping.

  1. void RenderThread::run()
  2.  {
  3.     forever  {
  4.         mutex.lock();
  5.         QSize resultSize = this->resultSize;
  6.         double scaleFactor = this->scaleFactor;
  7.         double centerX = this->centerX;
  8.         double centerY = this->centerY;
  9.         mutex.unlock();

run() is quite a big function, so we'll break it down into parts.

The function body is an infinite loop which starts by storing the rendering parameters in local variables. As usual, we protect accesses to the member variables using the class's mutex. Storing the member variables in local variables allows us to minimize the amout of code that needs to be protected by a mutex. This ensures that the main thread will never have to block for too long when it needs to access RenderThread's member variables (e.g., in render()).

The forever keyword is, like foreach, a Qt pseudo-keyword.

  1.         int halfWidth = resultSize.width() / 2;
  2.         int halfHeight = resultSize.height() / 2;
  3.         QImage image(resultSize, QImage::Format_RGB32);
  4.  
  5.         const int NumPasses = 8;
  6.         int pass = 0;
  7.         while (pass < NumPasses)  {
  8.             const int MaxIterations = (1 << (2 * pass + 6)) + 32;
  9.             const int Limit = 4;
  10.             bool allBlack = true;
  11.  
  12.             for (int y = -halfHeight; y < halfHeight; ++y)  {
  13.                 if (restart)
  14.                     break;
  15.                 if (abort)
  16.                     return;
  17.  
  18.                 uint *scanLine =
  19.                         reinterpret_cast<uint *>(image.scanLine(y + halfHeight));
  20.                 double ay = centerY + (y * scaleFactor);
  21.  
  22.                 for (int x = -halfWidth; x < halfWidth; ++x)  {
  23.                     double ax = centerX + (x * scaleFactor);
  24.                     double a1 = ax;
  25.                     double b1 = ay;
  26.                     int numIterations = 0;
  27.  
  28.                     do  {
  29.                         ++numIterations;
  30.                         double a2 = (a1 * a1) - (b1 * b1) + ax;
  31.                         double b2 = (2 * a1 * b1) + ay;
  32.                         if ((a2 * a2) + (b2 * b2) > Limit)
  33.                             break;
  34.  
  35.                         ++numIterations;
  36.                         a1 = (a2 * a2) - (b2 * b2) + ax;
  37.                         b1 = (2 * a2 * b2) + ay;
  38.                         if ((a1 * a1) + (b1 * b1) > Limit)
  39.                             break;
  40.                     } while (numIterations < MaxIterations);
  41.  
  42.                     if (numIterations < MaxIterations)  {
  43.                         *scanLine++ = colormap[numIterations % ColormapSize];
  44.                         allBlack = false;
  45.                     } else  {
  46.                         *scanLine++ = qRgb(0, 0, 0);
  47.                     }
  48.                 }
  49.             }
  50.  
  51.             if (allBlack && pass == 0)  {
  52.                 pass = 4;
  53.             } else  {
  54.                 if (!restart)
  55.                     emit renderedImage(image, scaleFactor);
  56.                 ++pass;
  57.             }
  58.         }

Then comes the core of the algorithm. Instead of trying to create a perfect Mandelbrot set image, we do multiple passes and generate more and more precise (and computationally expensive) approximations of the fractal.

If we discover inside the loop that restart has been set to true (by render()), we break out of the loop immediately, so that the control quickly returns to the very top of the outer loop (the forever loop) and we fetch the new rendering parameters. Similarly, if we discover that abort has been set to true (by the RenderThread destructor), we return from the function immediately, terminating the thread.

The core algorithm is beyond the scope of this tutorial.

  1.         mutex.lock();
  2.         if (!restart)
  3.             condition.wait(&mutex);
  4.         restart = false;
  5.         mutex.unlock();
  6.     }
  7. }

Once we're done with all the iterations, we call QWaitCondition::wait() to put the thread to sleep by calling, unless restart is true. There's no use in keeping a worker thread looping indefinitely while there's nothing to do.

  1.                 uint RenderThread::rgbFromWaveLength(double wave)
  2.  {
  3.     double r = 0.0;
  4.     double g = 0.0;
  5.     double b = 0.0;
  6.  
  7.     if (wave >= 380.0 && wave <= 440.0)  {
  8.         r = -1.0 * (wave - 440.0) / (440.0 - 380.0);
  9.         b = 1.0;
  10.     } else if (wave >= 440.0 && wave <= 490.0)  {
  11.         g = (wave - 440.0) / (490.0 - 440.0);
  12.         b = 1.0;
  13.     } else if (wave >= 490.0 && wave <= 510.0)  {
  14.         g = 1.0;
  15.         b = -1.0 * (wave - 510.0) / (510.0 - 490.0);
  16.     } else if (wave >= 510.0 && wave <= 580.0)  {
  17.         r = (wave - 510.0) / (580.0 - 510.0);
  18.         g = 1.0;
  19.     } else if (wave >= 580.0 && wave <= 645.0)  {
  20.         r = 1.0;
  21.         g = -1.0 * (wave - 645.0) / (645.0 - 580.0);
  22.     } else if (wave >= 645.0 && wave <= 780.0)  {
  23.         r = 1.0;
  24.     }
  25.  
  26.     double s = 1.0;
  27.     if (wave > 700.0)
  28.         s = 0.3 + 0.7 * (780.0 - wave) / (780.0 - 700.0);
  29.     else if (wave <  420.0)
  30.         s = 0.3 + 0.7 * (wave - 380.0) / (420.0 - 380.0);
  31.  
  32.     r = pow(r * s, 0.8);
  33.     g = pow(g * s, 0.8);
  34.     b = pow(b * s, 0.8);
  35.     return qRgb(int(r * 255), int(g * 255), int(b * 255));
  36. }

The rgbFromWaveLength() function is a helper function that converts a wave length to a RGB value compatible with 32-bit QImages. It is called from the constructor to initialize the colormap array with pleasing colors.

MandelbrotWidget Class Defintion

The MandelbrotWidget class uses RenderThread to draw the Mandelbrot set on screen. Here's the class definition:

  1. class MandelbrotWidget : public QWidget
  2.  {
  3.     Q_OBJECT
  4.  
  5. public:
  6.     MandelbrotWidget(QWidget *parent = 0);
  7.  
  8. protected:
  9.     void paintEvent(QPaintEvent *event);
  10.     void resizeEvent(QResizeEvent *event);
  11.     void keyPressEvent(QKeyEvent *event);
  12.     void wheelEvent(QWheelEvent *event);
  13.     void mousePressEvent(QMouseEvent *event);
  14.     void mouseMoveEvent(QMouseEvent *event);
  15.     void mouseReleaseEvent(QMouseEvent *event);
  16.  
  17. private slots:
  18.     void updatePixmap(const QImage &image, double scaleFactor);
  19.  
  20. private:
  21.     void zoom(double zoomFactor);
  22.     void scroll(int deltaX, int deltaY);
  23.  
  24.     RenderThread thread;
  25.     QPixmap pixmap;
  26.     QPoint pixmapOffset;
  27.     QPoint lastDragPos;
  28.     double centerX;
  29.     double centerY;
  30.     double pixmapScale;
  31.     double curScale;
  32. };

The widget reimplements many event handlers from QWidget. In addition, it has an updatePixmap() slot that we'll connect to the worker thread's renderedImage() signal to update the display whenever we receive new data from the thread.

Among the private variables, we have thread of type RenderThread and pixmap, which contains the last rendered image.

MandelbrotWidget Class Implementation

  1. const double DefaultCenterX = -0.637011f;
  2. const double DefaultCenterY = -0.0395159f;
  3. const double DefaultScale = 0.00403897f;
  4.  
  5. const double ZoomInFactor = 0.8f;
  6. const double ZoomOutFactor = 1 / ZoomInFactor;
  7. const int ScrollStep = 20;

The implementation starts with a few contants that we'll need later on.

  1. MandelbrotWidget::MandelbrotWidget(QWidget *parent)
  2.     : QWidget(parent)
  3.  {
  4.     centerX = DefaultCenterX;
  5.     centerY = DefaultCenterY;
  6.     pixmapScale = DefaultScale;
  7.     curScale = DefaultScale;
  8.  
  9.     qRegisterMetaType<QImage>("QImage");
  10.     connect(&thread, SIGNAL(renderedImage(QImage,double)),
  11.             this, SLOT(updatePixmap(QImage,double)));
  12.  
  13.     setWindowTitle(tr("Mandelbrot"));
  14. #ifndef QT_NO_CURSOR
  15.     setCursor(Qt::CrossCursor);
  16. #endif
  17.     resize(550, 400);
  18. }

The interesting part of the constructor is the qRegisterMetaType() and QObject::connect() calls. Let's start with the connect() call.

Although it looks like a standard signal-slot connection between two QObjects, because the signal is emitted in a different thread than the receiver lives in, the connection is effectively a queued connection. These connections are asynchronous (i.e., non-blocking), meaning that the slot will be called at some point after the emit statement. What's more, the slot will be invoked in the thread in which the receiver lives. Here, the signal is emitted in the worker thread, and the slot is executed in the GUI thread when control returns to the event loop.

With queued connections, Qt must store a copy of the arguments that were passed to the signal so that it can pass them to the slot later on. Qt knows how to take of copy of many C++ and Qt types, but QImage isn't one of them. We must therefore call the template function qRegisterMetaType() before we can use QImage as parameter in queued connections.

  1. void MandelbrotWidget::paintEvent(QPaintEvent * /* event */)
  2.  {
  3.     QPainter painter(this);
  4.     painter.fillRect(rect(), Qt::black);
  5.  
  6.     if (pixmap.isNull())  {
  7.         painter.setPen(Qt::white);
  8.         painter.drawText(rect(), Qt::AlignCenter,
  9.                          tr("Rendering initial image, please wait..."));
  10.         return;
  11.     }

In paintEvent(), we start by filling the background with black. If we have nothing yet to paint (pixmap is null), we print a message on the widget asking the user to be patient and return from the function immediately.

  1.     if (curScale == pixmapScale)  {
  2.         painter.drawPixmap(pixmapOffset, pixmap);
  3.     } else  {
  4.         double scaleFactor = pixmapScale / curScale;
  5.         int newWidth = int(pixmap.width() * scaleFactor);
  6.         int newHeight = int(pixmap.height() * scaleFactor);
  7.         int newX = pixmapOffset.x() + (pixmap.width() - newWidth) / 2;
  8.         int newY = pixmapOffset.y() + (pixmap.height() - newHeight) / 2;
  9.  
  10.         painter.save();
  11.         painter.translate(newX, newY);
  12.         painter.scale(scaleFactor, scaleFactor);
  13.         QRectF exposed = painter.matrix().inverted().mapRect(rect()).adjusted(-1, -1, 1, 1);
  14.         painter.drawPixmap(exposed, pixmap, exposed);
  15.         painter.restore();
  16.     }

If the pixmap has the right scale factor, we draw the pixmap directly onto the widget. Otherwise, we scale and translate the coordinate system before we draw the pixmap. By reverse mapping the widget's rectangle using the scaled painter matrix, we also make sure that only the exposed areas of the pixmap are drawn. The calls to QPainter::save() and QPainter::restore() make sure that any painting performed afterwards uses the standard coordinate system.

  1.     QString text = tr("Use mouse wheel or the '+' and '-' keys to zoom. "
  2.                       "Press and hold left mouse button to scroll.");
  3.     QFontMetrics metrics = painter.fontMetrics();
  4.     int textWidth = metrics.width(text);
  5.  
  6.     painter.setPen(Qt::NoPen);
  7.     painter.setBrush(QColor(0, 0, 0, 127));
  8.     painter.drawRect((width() - textWidth) / 2 - 5, 0, textWidth + 10,
  9.                      metrics.lineSpacing() + 5);
  10.     painter.setPen(Qt::white);
  11.     painter.drawText((width() - textWidth) / 2,
  12.                      metrics.leading() + metrics.ascent(), text);
  13. }

At the end of the paint event handler, we draw a text string and a semi-transparent rectangle on top of the fractal.

  1. void MandelbrotWidget::resizeEvent(QResizeEvent * /* event */)
  2.  {
  3.     thread.render(centerX, centerY, curScale, size());
  4. }

Whenever the user resizes the widget, we call render() to start generating a new image, with the same centerX, centerY, and curScale parameters but with the new widget size.

Notice that we rely on resizeEvent() being automatically called by Qt when the widget is shown the first time to generate the image the very first time.

  1. void MandelbrotWidget::keyPressEvent(QKeyEvent *event)
  2.  {
  3.     switch (event->key())  {
  4.     case Qt::Key_Plus:
  5.         zoom(ZoomInFactor);
  6.         break;
  7.     case Qt::Key_Minus:
  8.         zoom(ZoomOutFactor);
  9.         break;
  10.     case Qt::Key_Left:
  11.         scroll(-ScrollStep, 0);
  12.         break;
  13.     case Qt::Key_Right:
  14.         scroll(+ScrollStep, 0);
  15.         break;
  16.     case Qt::Key_Down:
  17.         scroll(0, -ScrollStep);
  18.         break;
  19.     case Qt::Key_Up:
  20.         scroll(0, +ScrollStep);
  21.         break;
  22.     default:
  23.         QWidget::keyPressEvent(event);
  24.     }
  25. }

The key press event handler provides a few keyboard bindings for the benefit of users who don't have a mouse. The zoom() and scroll() functions will be covered later.

  1. void MandelbrotWidget::wheelEvent(QWheelEvent *event)
  2.  {
  3.     int numDegrees = event->delta() / 8;
  4.     double numSteps = numDegrees / 15.0f;
  5.     zoom(pow(ZoomInFactor, numSteps));
  6. }

The wheel event handler is reimplemented to make the mouse wheel control the zoom level. QWheelEvent::delta() returns the angle of the wheel mouse movement, in eights of a degree. For most mice, one wheel step corresponds to 15 degrees. We find out how many mouse steps we have and determine the zoom factor in consequence. For example, if we have two wheel steps in the positive direction (i.e., +30 degrees), the zoom factor becomes ZoomInFactor to the second power, i.e. 0.8 * 0.8 = 0.64.

  1. void MandelbrotWidget::mousePressEvent(QMouseEvent *event)
  2.  {
  3.     if (event->button() == Qt::LeftButton)
  4.         lastDragPos = event->pos();
  5. }

When the user presses the left mouse button, we store the mouse pointer position in lastDragPos.

  1. void MandelbrotWidget::mouseMoveEvent(QMouseEvent *event)
  2.  {
  3.     if (event->buttons() & Qt::LeftButton)  {
  4.         pixmapOffset += event->pos() - lastDragPos;
  5.         lastDragPos = event->pos();
  6.         update();
  7.     }
  8. }

When the user moves the mouse pointer while the left mouse button is pressed, we adjust pixmapOffset to paint the pixmap at a shifted position and call QWidget::update() to force a repaint.

  1. void MandelbrotWidget::mouseReleaseEvent(QMouseEvent *event)
  2.  {
  3.     if (event->button() == Qt::LeftButton)  {
  4.         pixmapOffset += event->pos() - lastDragPos;
  5.         lastDragPos = QPoint();
  6.  
  7.         int deltaX = (width() - pixmap.width()) / 2 - pixmapOffset.x();
  8.         int deltaY = (height() - pixmap.height()) / 2 - pixmapOffset.y();
  9.         scroll(deltaX, deltaY);
  10.     }
  11. }

When the left mouse button is released, we update pixmapOffset just like we did on a mouse move and we reset lastDragPos to a default value. Then, we call scroll() to render a new image for the new position. (Adjusting pixmapOffset isn't sufficient because areas revealed when dragging the pixmap are drawn in black.)

  1. void MandelbrotWidget::updatePixmap(const QImage &image, double scaleFactor)
  2.  {
  3.     if (!lastDragPos.isNull())
  4.         return;
  5.  
  6.     pixmap = QPixmap::fromImage(image);
  7.     pixmapOffset = QPoint();
  8.     lastDragPos = QPoint();
  9.     pixmapScale = scaleFactor;
  10.     update();
  11. }

The updatePixmap() slot is invoked when the worker thread has finished rendering an image. We start by checking whether a drag is in effect and do nothing in that case. In the normal case, we store the image in pixmap and reinitialize some of the other members. At the end, we call QWidget::update() to refresh the display.

At this point, you might wonder why we use a QImage for the parameter and a QPixmap for the data member. Why not stick to one type? The reason is that QImage is the only class that supports direct pixel manipulation, which we need in the worker thread. On the other hand, before an image can be drawn on screen, it must be converted into a pixmap. It's better to do the conversion once and for all here, rather than in paintEvent().

  1. void MandelbrotWidget::zoom(double zoomFactor)
  2.  {
  3.     curScale *= zoomFactor;
  4.     update();
  5.     thread.render(centerX, centerY, curScale, size());
  6. }

In zoom(), we recompute curScale. Then we call QWidget::update() to draw a scaled pixmap, and we ask the worker thread to render a new image corresponding to the new curScale value.

  1. void MandelbrotWidget::scroll(int deltaX, int deltaY)
  2.  {
  3.     centerX += deltaX * curScale;
  4.     centerY += deltaY * curScale;
  5.     update();
  6.     thread.render(centerX, centerY, curScale, size());
  7. }

scroll() is similar to zoom(), except that the affected parameters are centerX and centerY.

The main() Function

The application's multithreaded nature has no impact on its main() function, which is as simple as usual:

  1. int main(int argc, char *argv[])
  2.  {
  3.     QApplication app(argc, argv);
  4.     MandelbrotWidget widget;
  5.     widget.show();
  6.     return app.exec();
  7. }
Notes provided by the Qt Community

No notes