MandelWidget.h 9.2 KB

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