FractalZoomWidget.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #ifndef FRACTALZOOMWIDGET_H
  2. #define FRACTALZOOMWIDGET_H
  3. #include "EscapeTimeVisualWidget.h"
  4. #include "FractalWidgetUtils.h"
  5. #include "Bitmap.h"
  6. #include <QThreadPool>
  7. #include <QMutex>
  8. class FractalZoomWidget;
  9. ///
  10. /// \brief represents a grid of images at a certain depth
  11. /// in the fractal.
  12. ///
  13. class SliceGrid
  14. {
  15. public:
  16. FractalZoomWidget& owner;
  17. int level;
  18. mnd::Real dpp;
  19. std::unordered_map<std::pair<GridIndex, GridIndex>, std::unique_ptr<GridElement>, IndexPairHash> cells;
  20. public:
  21. SliceGrid(FractalZoomWidget& owner, int level);
  22. std::pair<GridIndex, GridIndex> getCellIndices(mnd::Real x, mnd::Real y);
  23. std::pair<mnd::Real, mnd::Real> getPositions(GridIndex i, GridIndex j);
  24. GridElement* getCell(GridIndex i, GridIndex j);
  25. void setCell(GridIndex i, GridIndex j, std::unique_ptr<GridElement> tex);
  26. inline size_t countAllocatedCells(void) const { return cells.size(); }
  27. void clearCells(void);
  28. void clearUncleanCells(void);
  29. };
  30. class Calcer : public QObject
  31. {
  32. Q_OBJECT
  33. /// tuple contains level, i, j of the job
  34. std::unordered_map<std::tuple<int, GridIndex, GridIndex>, CalcJob*, IndexTripleHash> jobs;
  35. QMutex jobsMutex;
  36. QThreadPool* threadPool;
  37. FractalZoomWidget& owner;
  38. int currentLevel;
  39. volatile unsigned int calcState = 0;
  40. public:
  41. Calcer(FractalZoomWidget& owner);
  42. void clearAll(void);
  43. inline void changeState(void) { calcState++; }
  44. public slots:
  45. void calc(SliceGrid& grid, int level, GridIndex i, GridIndex j, int priority);
  46. void setCurrentLevel(int level);
  47. void notFinished(int level, GridIndex i, GridIndex j);
  48. void jobFailed(int level, GridIndex i, GridIndex j);
  49. void redirect(int level, GridIndex i, GridIndex j, Bitmap<float>* bmp);
  50. signals:
  51. void done(int level, GridIndex i, GridIndex j, Bitmap<float>* bmp);
  52. };
  53. class FractalZoomWidget :
  54. public EscapeTimeVisualWidget
  55. {
  56. Q_OBJECT
  57. // a grid should not be deleted once constructed.
  58. // to free up memory one can call SliceGrid::clearCells()
  59. std::unordered_map<int, SliceGrid> levels;
  60. mnd::MandelGenerator* generator;
  61. Calcer calcer;
  62. ETVImage* emptyImage;
  63. protected:
  64. mnd::MandelInfo mandelInfo;
  65. public:
  66. static const int chunkSize;
  67. FractalZoomWidget(QWidget* parent = nullptr);
  68. Q_PROPERTY(mnd::MandelViewport viewport READ getViewport WRITE setViewport)
  69. virtual void setViewport(const mnd::MandelViewport& viewport);
  70. virtual const mnd::MandelViewport& getViewport(void) const;
  71. int getLevel(const mnd::Real& dpp) const;
  72. mnd::Real getDpp(int level) const;
  73. SliceGrid& getGrid(int level);
  74. void clearCells(void);
  75. void garbageCollect(int level,
  76. const GridIndex& i, const GridIndex& j);
  77. GridElement* searchAbove(int level,
  78. const GridIndex& i, const GridIndex& j,
  79. int recursionLevel);
  80. GridElement* searchUnder(int level,
  81. const GridIndex& i, const GridIndex& j,
  82. int recursionLevel);
  83. const mnd::MandelInfo& getMandelInfo(void) const;
  84. mnd::MandelInfo& getMandelInfo(void);
  85. void setGenerator(mnd::MandelGenerator*);
  86. mnd::MandelGenerator* getGenerator(void) const;
  87. virtual void zoom(float factor);
  88. virtual void setSmoothColoring(bool smooth);
  89. virtual void setMaxIterations(int maxIterations);
  90. virtual void initializeGL(void) override;
  91. virtual void resizeGL(int w, int h) override;
  92. virtual void paintGL(void) override;
  93. protected slots:
  94. void cellReady(int level, GridIndex i, GridIndex j, Bitmap<float>* bmp);
  95. };
  96. #endif // FRACTALZOOMWIDGET_H