MandelWidget.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #pragma once
  2. #include <QGLWidget>
  3. #include <QOpenGLWidget>
  4. #include <QThreadPool>
  5. #include <QMouseEvent>
  6. #include <QOpenGLContext>
  7. #include <QOpenGLFunctions>
  8. #include <QMutex>
  9. //#include <qopengl.h>
  10. //#include <qopenglfunctions.h>
  11. //#include <qopenglcontext.h>
  12. #include "Bitmap.h"
  13. #include "Gradient.h"
  14. #include <Mandel.h>
  15. #include <atomic>
  16. #include <tuple>
  17. #include <deque>
  18. #include <unordered_map>
  19. #include <unordered_set>
  20. using GridIndex = long long;
  21. Q_DECLARE_METATYPE(GridIndex)
  22. class Texture
  23. {
  24. GLuint id;
  25. public:
  26. Texture(const Bitmap<RGBColor>& pict, GLint param = GL_LINEAR);
  27. ~Texture(void);
  28. Texture(const Texture& other) = delete;
  29. Texture& operator=(const Texture& other) = delete;
  30. Texture(Texture&& other);
  31. Texture& operator=(Texture&& other);
  32. private:
  33. void bind(void) const;
  34. public:
  35. inline GLuint getId(void) const { return id; }
  36. void drawRect(float x, float y, float width, float height);
  37. };
  38. class TextureClip
  39. {
  40. std::shared_ptr<Texture> texture;
  41. float tx, ty, tw, th;
  42. public:
  43. inline TextureClip(std::shared_ptr<Texture> tex) :
  44. texture{ std::move(tex) },
  45. tx{ 0 }, ty{ 0 }, tw{ 1 }, th{ 1 }
  46. {}
  47. void drawRect(float x, float y, float width, float height);
  48. TextureClip clip(float x, float y, float w, float h);
  49. };
  50. struct PairHash {
  51. template <typename T1, typename T2>
  52. std::size_t operator () (const std::pair<T1, T2>& p) const {
  53. auto h1 = std::hash<T1>{}(p.first);
  54. auto h2 = std::hash<T2>{}(p.second);
  55. return (h1 ^ 234579245) * 23452354 + h2;
  56. }
  57. };
  58. struct TripleHash {
  59. template <typename T1, typename T2, typename T3>
  60. std::size_t operator () (const std::tuple<T1, T2, T3>& p) const {
  61. auto h1 = std::hash<T1>{}(std::get<0>(p));
  62. auto h2 = std::hash<T2>{}(std::get<1>(p));
  63. auto h3 = std::hash<T3>{}(std::get<2>(p));
  64. return (((h1 ^ 234579245) * 23452357 + h2) ^ 2345244345) * 23421 + h3;
  65. }
  66. };
  67. struct GridElement
  68. {
  69. bool enoughResolution;
  70. TextureClip img;
  71. inline GridElement(bool enoughResolution, TextureClip img) :
  72. enoughResolution{ enoughResolution },
  73. img{ std::move(img) }
  74. {}
  75. };
  76. class MandelV;
  77. class TexGrid
  78. {
  79. public:
  80. MandelV& owner;
  81. int level;
  82. double dpp;
  83. std::unordered_map<std::pair<GridIndex, GridIndex>, std::unique_ptr<GridElement>, PairHash> cells;
  84. public:
  85. //inline TexGrid(MandelV& owner) : level{ 1.0 }, owner{ owner } {}
  86. TexGrid(MandelV& owner, int level);
  87. std::pair<GridIndex, GridIndex> getCellIndices(double x, double y);
  88. std::pair<double, double> getPositions(GridIndex i, GridIndex j);
  89. GridElement* getCell(GridIndex i, GridIndex j);
  90. void setCell(GridIndex i, GridIndex j, std::unique_ptr<GridElement> tex);
  91. inline size_t countAllocatedCells(void) const { return cells.size(); }
  92. void clearCells(void);
  93. };
  94. class Job : public QObject, public QRunnable
  95. {
  96. Q_OBJECT
  97. public:
  98. mnd::MandelContext& mndContext;
  99. Gradient& gradient;
  100. int maxIter;
  101. TexGrid* grid;
  102. int level;
  103. GridIndex i, j;
  104. inline Job(mnd::MandelContext& mndContext,
  105. Gradient& gradient,
  106. int maxIter,
  107. TexGrid* grid,
  108. int level, GridIndex i, GridIndex j) :
  109. mndContext{ mndContext },
  110. gradient{ gradient },
  111. maxIter{ maxIter },
  112. grid{ grid },
  113. level{ level },
  114. i{ i }, j{ j }
  115. {}
  116. void run() override;
  117. signals:
  118. void done(int level, GridIndex i, GridIndex j, Bitmap<RGBColor>* bmp);
  119. };
  120. class Calcer : public QObject
  121. {
  122. Q_OBJECT
  123. /// tuple contains level, i, j of the job
  124. std::unordered_map<std::tuple<int, GridIndex, GridIndex>, Job*, TripleHash> jobs;
  125. QMutex jobsMutex;
  126. mnd::MandelContext& mndContext;
  127. std::unique_ptr<QThreadPool> threadPool;
  128. Gradient& gradient;
  129. int maxIter;
  130. int currentLevel;
  131. public:
  132. inline Calcer(mnd::MandelContext& mc, Gradient& gradient, int maxIter) :
  133. jobsMutex{ QMutex::Recursive },
  134. mndContext{ mc },
  135. threadPool{ std::make_unique<QThreadPool>() },
  136. gradient{ gradient },
  137. maxIter{ maxIter }
  138. {
  139. }
  140. void setMaxIter(int maxIter);
  141. void clearAll(void);
  142. public slots:
  143. void calc(TexGrid& grid, int level, GridIndex i, GridIndex j, int priority);
  144. void setCurrentLevel(int level);
  145. void notFinished(int level, GridIndex i, GridIndex j);
  146. void redirect(int level, GridIndex i, GridIndex j, Bitmap<RGBColor>* bmp);
  147. signals:
  148. void done(int level, GridIndex i, GridIndex j, Bitmap<RGBColor>* bmp);
  149. };
  150. class MandelV : public QObject
  151. {
  152. Q_OBJECT
  153. public:
  154. std::unique_ptr<Texture> empty;
  155. // a grid should not be deleted once constructed.
  156. // to free up memory one can call TexGrid::clearCells()
  157. std::unordered_map<int, TexGrid> levels;
  158. mnd::MandelContext& mndContext;
  159. Calcer calcer;
  160. Gradient& gradient;
  161. int maxIter;
  162. int width;
  163. int height;
  164. public:
  165. static const int chunkSize = 256;
  166. MandelV(mnd::MandelContext& mndContext, Gradient& gradient, int maxIter);
  167. int getLevel(double dpp);
  168. double getDpp(int level);
  169. TexGrid& getGrid(int level);
  170. inline int getMaxIter(void) const { return this->maxIter; }
  171. void setMaxIter(int maxIter);
  172. void clear(void);
  173. void garbageCollect(int level);
  174. GridElement* searchAbove(int level, GridIndex i, GridIndex j, int recursionLevel);
  175. std::unique_ptr<GridElement> searchUnder(int level, GridIndex i, GridIndex j, int recursionLevel);
  176. void paint(const mnd::MandelViewport& mvp);
  177. public slots:
  178. void cellReady(int level, GridIndex i, GridIndex j, Bitmap<RGBColor>* bmp);
  179. signals:
  180. void redrawRequested(void);
  181. };
  182. class MandelWidget : public QOpenGLWidget
  183. {
  184. Q_OBJECT
  185. private:
  186. mnd::MandelContext& mndContext;
  187. Gradient gradient;
  188. bool initialized = false;
  189. int maxIterations = 2000;
  190. volatile bool rubberbanding = false;
  191. QRectF rubberband;
  192. volatile bool dragging = false;
  193. int dragX, dragY;
  194. std::unique_ptr<Texture> tex;
  195. mnd::MandelViewport viewport;
  196. std::unique_ptr<MandelV> v;
  197. public:
  198. MandelWidget(mnd::MandelContext& ctxt, QWidget* parent = nullptr);
  199. ~MandelWidget(void) override;
  200. inline const Gradient& getGradient(void) const { return gradient; }
  201. void initializeGL(void) override;
  202. void resizeGL(int w, int h) override;
  203. void paintGL() override;
  204. void drawRubberband(void);
  205. void zoom(float scale, float x = 0.5f, float y = 0.5f);
  206. void setMaxIterations(int maxIter);
  207. //void redraw();
  208. void requestRecalc(void);
  209. void resizeEvent(QResizeEvent* re) override;
  210. void mousePressEvent(QMouseEvent* me) override;
  211. void mouseMoveEvent(QMouseEvent* me) override;
  212. void mouseReleaseEvent(QMouseEvent* me) override;
  213. void wheelEvent(QWheelEvent * we) override;
  214. inline const mnd::MandelViewport& getViewport(void) const { return viewport; }
  215. signals:
  216. void needsUpdate(const mnd::MandelInfo vp);
  217. public slots:
  218. void viewUpdated(Bitmap<RGBColor>* bitmap);
  219. };