Home | History | Annotate | Download | only in mandelbrot
      1 // This file is part of Eigen, a lightweight C++ template library
      2 // for linear algebra.
      3 //
      4 // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1 (at) gmail.com>
      5 //
      6 // This Source Code Form is subject to the terms of the Mozilla
      7 // Public License v. 2.0. If a copy of the MPL was not distributed
      8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
      9 
     10 #ifndef MANDELBROT_H
     11 #define MANDELBROT_H
     12 
     13 #include <Eigen/Core>
     14 #include <QtGui/QApplication>
     15 #include <QtGui/QWidget>
     16 #include <QtCore/QThread>
     17 
     18 class MandelbrotWidget;
     19 
     20 class MandelbrotThread : public QThread
     21 {
     22     friend class MandelbrotWidget;
     23     MandelbrotWidget *widget;
     24     long long total_iter;
     25     int id, max_iter;
     26     bool single_precision;
     27 
     28   public:
     29     MandelbrotThread(MandelbrotWidget *w, int i) : widget(w), id(i) {}
     30     void run();
     31     template<typename Real> void render(int img_width, int img_height);
     32 };
     33 
     34 class MandelbrotWidget : public QWidget
     35 {
     36     Q_OBJECT
     37 
     38     friend class MandelbrotThread;
     39     Eigen::Vector2d center;
     40     double xradius;
     41     int size;
     42     unsigned char *buffer;
     43     QPoint lastpos;
     44     int draft;
     45     MandelbrotThread **threads;
     46     int threadcount;
     47 
     48   protected:
     49     void resizeEvent(QResizeEvent *);
     50     void paintEvent(QPaintEvent *);
     51     void mousePressEvent(QMouseEvent *event);
     52     void mouseMoveEvent(QMouseEvent *event);
     53 
     54   public:
     55     MandelbrotWidget() : QWidget(), center(0,0), xradius(2),
     56                          size(0), buffer(0), draft(16)
     57     {
     58       setAutoFillBackground(false);
     59       threadcount = QThread::idealThreadCount();
     60       threads = new MandelbrotThread*[threadcount];
     61       for(int th = 0; th < threadcount; th++) threads[th] = new MandelbrotThread(this, th);
     62     }
     63     ~MandelbrotWidget()
     64     {
     65       if(buffer) delete[]buffer;
     66       for(int th = 0; th < threadcount; th++) delete threads[th];
     67       delete[] threads;
     68     }
     69 };
     70 
     71 #endif // MANDELBROT_H
     72