1
0

MandelWidget.h 8.6 KB

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