MandelWidget.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. void clearCells(void);
  75. };
  76. class Job : public QObject, public QRunnable
  77. {
  78. Q_OBJECT
  79. public:
  80. mnd::MandelContext& mndContext;
  81. TexGrid* grid;
  82. int level;
  83. int i, j;
  84. inline Job(mnd::MandelContext& mndContext, TexGrid* grid, int level, int i, int j) :
  85. mndContext{ mndContext },
  86. grid{ grid },
  87. level{ level },
  88. i{ i }, j{ j }
  89. {}
  90. void run() override;
  91. signals:
  92. void done(int level, int i, int j, Bitmap<RGBColor>* bmp);
  93. };
  94. class Calcer : public QObject
  95. {
  96. Q_OBJECT
  97. std::unordered_set<std::tuple<int, int, int>, TripleHash> jobs;
  98. mnd::MandelContext& mndContext;
  99. std::unique_ptr<QThreadPool> threadPool;
  100. public:
  101. inline Calcer(mnd::MandelContext& mc) :
  102. mndContext{ mc },
  103. threadPool{ std::make_unique<QThreadPool>() }
  104. {
  105. }
  106. public slots:
  107. void calc(TexGrid& grid, int level, int i, int j);
  108. void redirect(int level, int i, int j, Bitmap<RGBColor>* bmp);
  109. signals:
  110. void done(int level, int i, int j, Bitmap<RGBColor>* bmp);
  111. };
  112. class MandelV : public QObject
  113. {
  114. Q_OBJECT
  115. public:
  116. std::unique_ptr<Texture> empty;
  117. std::unordered_map<int, TexGrid> levels;
  118. mnd::MandelContext& mndContext;
  119. std::unique_ptr<Calcer> calcThread;
  120. int width;
  121. int height;
  122. public:
  123. static const int chunkSize = 128;
  124. MandelV(mnd::MandelContext& mndContext);
  125. int getLevel(double dpp);
  126. double getDpp(int level);
  127. TexGrid& getGrid(int level);
  128. void paint(const mnd::MandelViewport& mvp);
  129. public slots:
  130. void cellReady(int level, int i, int j, Bitmap<RGBColor>* bmp);
  131. signals:
  132. void redrawRequested(void);
  133. };
  134. class MandelView : public QObject
  135. {
  136. Q_OBJECT
  137. private:
  138. std::future<void> calc;
  139. QThread calcThread;
  140. std::mutex mut;
  141. std::condition_variable condVar;
  142. std::atomic<mnd::MandelInfo> toCalc;
  143. std::atomic_bool hasToCalc;
  144. std::atomic_bool finish;
  145. mnd::Generator* generator;
  146. Gradient& gradient;
  147. MandelWidget* mWidget;
  148. //QOpenGLContext* context;
  149. public:
  150. MandelView(mnd::Generator& generator, Gradient& gradient, MandelWidget* mWidget);
  151. ~MandelView(void);
  152. void setGenerator(mnd::Generator &value);
  153. void start();
  154. private slots:
  155. void loop();
  156. public slots:
  157. void adaptViewport(const mnd::MandelInfo vp);
  158. signals:
  159. void updated(Bitmap<RGBColor>* bitmap);
  160. };
  161. class MandelWidget : public QOpenGLWidget
  162. {
  163. Q_OBJECT
  164. private:
  165. //QScrollArea qsa;
  166. //QLabel ql;
  167. mnd::MandelContext& mndContext;
  168. Gradient gradient;
  169. bool initialized = false;
  170. int maxIterations = 2000;
  171. bool rubberbandDragging = false;
  172. QRectF rubberband;
  173. std::unique_ptr<Texture> tex;
  174. mnd::MandelViewport viewport;
  175. MandelView mv;
  176. std::unique_ptr<MandelV> v;
  177. public:
  178. MandelWidget(mnd::MandelContext& ctxt, QWidget* parent = nullptr);
  179. ~MandelWidget(void) override;
  180. /*inline MandelWidget(const MandelWidget& other) :
  181. mndContext{ other.mndContext },
  182. mv{ other.mndContext }
  183. {
  184. }*/
  185. inline const Gradient& getGradient(void) const { return gradient; }
  186. void initializeGL(void) override;
  187. void resizeGL(int w, int h) override;
  188. void paintGL() override;
  189. void drawRubberband(void);
  190. void zoom(float scale);
  191. void setMaxIterations(int maxIter);
  192. //void redraw();
  193. void requestRecalc(void);
  194. void resizeEvent(QResizeEvent* re) override;
  195. void mousePressEvent(QMouseEvent* me) override;
  196. void mouseMoveEvent(QMouseEvent* me) override;
  197. void mouseReleaseEvent(QMouseEvent* me) override;
  198. inline const mnd::MandelViewport& getViewport(void) const { return viewport; }
  199. signals:
  200. void needsUpdate(const mnd::MandelInfo vp);
  201. public slots:
  202. void viewUpdated(Bitmap<RGBColor>* bitmap);
  203. };