MandelWidget.h 8.1 KB

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