MandelWidget.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #pragma once
  2. #include <QGLWidget>
  3. #include <QOpenGLWidget>
  4. #include <QThread>
  5. #include <QThreadPool>
  6. #include <qopengl.h>
  7. #include <qopenglfunctions.h>
  8. #include <qopenglcontext.h>
  9. #include <qscrollarea.h>
  10. #include <qlabel.h>
  11. #include <qevent.h>
  12. #include <qrubberband.h>
  13. #include "Bitmap.h"
  14. #include "Gradient.h"
  15. #include <Mandel.h>
  16. #include <future>
  17. #include <thread>
  18. #include <mutex>
  19. #include <atomic>
  20. #include <tuple>
  21. #include <deque>
  22. #include <unordered_map>
  23. #include <unordered_set>
  24. class MandelWidget;
  25. class Texture
  26. {
  27. GLuint id;
  28. QOpenGLContext* context;
  29. public:
  30. Texture(const Bitmap<RGBColor>& pict);
  31. Texture(const Bitmap<RGBColor>& pict, QOpenGLContext* context);
  32. ~Texture(void);
  33. Texture(const Texture& other) = delete;
  34. Texture& operator=(const Texture& other) = delete;
  35. Texture(Texture&& other);
  36. Texture& operator=(Texture&& other);
  37. void bind(void) const;
  38. inline GLuint getId(void) const { return id; }
  39. void drawRect(float x, float y, float width, float height);
  40. };
  41. struct MandelClip
  42. {
  43. mnd::MandelViewport view;
  44. };
  45. struct PairHash {
  46. template <typename T1, typename T2>
  47. std::size_t operator () (const std::pair<T1, T2>& p) const {
  48. auto h1 = std::hash<T1>{}(p.first);
  49. auto h2 = std::hash<T2>{}(p.second);
  50. return (h1 ^ 234579245) * 23452354 + h2;
  51. }
  52. };
  53. struct TripleHash {
  54. template <typename T1, typename T2, typename T3>
  55. std::size_t operator () (const std::tuple<T1, T2, T3>& p) const {
  56. auto h1 = std::hash<T1>{}(std::get<0>(p));
  57. auto h2 = std::hash<T2>{}(std::get<1>(p));
  58. auto h3 = std::hash<T3>{}(std::get<2>(p));
  59. return (((h1 ^ 234579245) * 23452357 + h2) ^ 2345244345) * 23421 + h3;
  60. }
  61. };
  62. class TexGrid
  63. {
  64. public:
  65. double dpp;
  66. std::unordered_map<std::pair<int, int>, std::unique_ptr<Texture>, PairHash> cells;
  67. public:
  68. inline TexGrid(void) : dpp{ 1.0 } {}
  69. inline TexGrid(double dpp) : dpp{ dpp } {}
  70. std::pair<int, int> getCellIndices(double x, double y);
  71. std::pair<double, double> getPositions(int i, int j);
  72. Texture* getCell(int i, int j);
  73. void setCell(int i, int j, std::unique_ptr<Texture> tex);
  74. inline size_t countAllocatedCells(void) const { return cells.size(); }
  75. void clearCells(void);
  76. };
  77. class Job : public QObject, public QRunnable
  78. {
  79. Q_OBJECT
  80. public:
  81. mnd::MandelContext& mndContext;
  82. TexGrid* grid;
  83. int level;
  84. int i, j;
  85. inline Job(mnd::MandelContext& mndContext, TexGrid* grid, int level, int i, int j) :
  86. mndContext{ mndContext },
  87. grid{ grid },
  88. level{ level },
  89. i{ i }, j{ j }
  90. {}
  91. void run() override;
  92. signals:
  93. void done(int level, int i, int j, Bitmap<RGBColor>* bmp);
  94. };
  95. class Calcer : public QObject
  96. {
  97. Q_OBJECT
  98. std::unordered_set<std::tuple<int, int, int>, TripleHash> jobs;
  99. mnd::MandelContext& mndContext;
  100. std::unique_ptr<QThreadPool> threadPool;
  101. public:
  102. inline Calcer(mnd::MandelContext& mc) :
  103. mndContext{ mc },
  104. threadPool{ std::make_unique<QThreadPool>() }
  105. {
  106. }
  107. public slots:
  108. void calc(TexGrid& grid, int level, int i, int j);
  109. void redirect(int level, int i, int j, Bitmap<RGBColor>* bmp);
  110. signals:
  111. void done(int level, int i, int j, Bitmap<RGBColor>* bmp);
  112. };
  113. class MandelV : public QObject
  114. {
  115. Q_OBJECT
  116. public:
  117. std::unique_ptr<Texture> empty;
  118. std::unordered_map<int, TexGrid> levels;
  119. mnd::MandelContext& mndContext;
  120. std::unique_ptr<Calcer> calcThread;
  121. int width;
  122. int height;
  123. public:
  124. static const int chunkSize = 128;
  125. MandelV(mnd::MandelContext& mndContext);
  126. int getLevel(double dpp);
  127. double getDpp(int level);
  128. TexGrid& getGrid(int level);
  129. void garbageCollect(int level);
  130. void paint(const mnd::MandelViewport& mvp);
  131. public slots:
  132. void cellReady(int level, int i, int j, Bitmap<RGBColor>* bmp);
  133. signals:
  134. void redrawRequested(void);
  135. };
  136. class MandelView : public QObject
  137. {
  138. Q_OBJECT
  139. private:
  140. std::future<void> calc;
  141. QThread calcThread;
  142. std::mutex mut;
  143. std::condition_variable condVar;
  144. std::atomic<mnd::MandelInfo> toCalc;
  145. std::atomic_bool hasToCalc;
  146. std::atomic_bool finish;
  147. mnd::Generator* generator;
  148. Gradient& gradient;
  149. MandelWidget* mWidget;
  150. //QOpenGLContext* context;
  151. public:
  152. MandelView(mnd::Generator& generator, Gradient& gradient, MandelWidget* mWidget);
  153. ~MandelView(void);
  154. void setGenerator(mnd::Generator &value);
  155. void start();
  156. private slots:
  157. void loop();
  158. public slots:
  159. void adaptViewport(const mnd::MandelInfo vp);
  160. signals:
  161. void updated(Bitmap<RGBColor>* bitmap);
  162. };
  163. class MandelWidget : public QOpenGLWidget
  164. {
  165. Q_OBJECT
  166. private:
  167. //QScrollArea qsa;
  168. //QLabel ql;
  169. mnd::MandelContext& mndContext;
  170. Gradient gradient;
  171. bool initialized = false;
  172. int maxIterations = 2000;
  173. bool rubberbandDragging = false;
  174. QRectF rubberband;
  175. std::unique_ptr<Texture> tex;
  176. mnd::MandelViewport viewport;
  177. MandelView mv;
  178. std::unique_ptr<MandelV> v;
  179. public:
  180. MandelWidget(mnd::MandelContext& ctxt, QWidget* parent = nullptr);
  181. ~MandelWidget(void) override;
  182. /*inline MandelWidget(const MandelWidget& other) :
  183. mndContext{ other.mndContext },
  184. mv{ other.mndContext }
  185. {
  186. }*/
  187. inline const Gradient& getGradient(void) const { return gradient; }
  188. void initializeGL(void) override;
  189. void resizeGL(int w, int h) override;
  190. void paintGL() override;
  191. void drawRubberband(void);
  192. void zoom(float scale);
  193. void setMaxIterations(int maxIter);
  194. //void redraw();
  195. void requestRecalc(void);
  196. void resizeEvent(QResizeEvent* re) override;
  197. void mousePressEvent(QMouseEvent* me) override;
  198. void mouseMoveEvent(QMouseEvent* me) override;
  199. void mouseReleaseEvent(QMouseEvent* me) override;
  200. inline const mnd::MandelViewport& getViewport(void) const { return viewport; }
  201. signals:
  202. void needsUpdate(const mnd::MandelInfo vp);
  203. public slots:
  204. void viewUpdated(Bitmap<RGBColor>* bitmap);
  205. };