MandelWidget.h 6.8 KB

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