MandelWidget.h 6.7 KB

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