MandelWidget.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 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. double 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(double x, double y);
  125. std::pair<double, double> 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. };
  131. class Job : public QObject, public QRunnable
  132. {
  133. Q_OBJECT
  134. public:
  135. mnd::Generator* generator;
  136. const Gradient& gradient;
  137. int maxIter;
  138. TexGrid* grid;
  139. int level;
  140. GridIndex i, j;
  141. long calcState = 0;
  142. inline Job( mnd::Generator* generator,
  143. const Gradient& gradient,
  144. int maxIter,
  145. TexGrid* grid,
  146. int level, GridIndex i, GridIndex j,
  147. long calcState) :
  148. generator{ generator },
  149. gradient{ gradient },
  150. maxIter{ maxIter },
  151. grid{ grid },
  152. level{ level },
  153. i{ i }, j{ j },
  154. calcState{ calcState }
  155. {}
  156. void run() override;
  157. signals:
  158. void done(int level, GridIndex i, GridIndex j, long calcState, Bitmap<RGBColor>* bmp);
  159. };
  160. class Calcer : public QObject
  161. {
  162. Q_OBJECT
  163. /// tuple contains level, i, j of the job
  164. std::unordered_map<std::tuple<int, GridIndex, GridIndex>, Job*, TripleHash> jobs;
  165. QMutex jobsMutex;
  166. mnd::Generator* generator;
  167. std::unique_ptr<QThreadPool> threadPool;
  168. const Gradient& gradient;
  169. int maxIter;
  170. int currentLevel;
  171. volatile long calcState = 0;
  172. public:
  173. inline Calcer(mnd::Generator* generator, const Gradient& gradient, int maxIter) :
  174. jobsMutex{ QMutex::Recursive },
  175. generator{ generator },
  176. threadPool{ std::make_unique<QThreadPool>() },
  177. gradient{ gradient },
  178. maxIter{ maxIter }
  179. {
  180. threadPool->setMaxThreadCount(1);
  181. }
  182. void setMaxIter(int maxIter);
  183. void clearAll(void);
  184. void setGenerator(mnd::Generator* generator) { this->generator = generator; changeState(); }
  185. inline void changeState(void) { calcState++; }
  186. public slots:
  187. void calc(TexGrid& grid, int level, GridIndex i, GridIndex j, int priority);
  188. void setCurrentLevel(int level);
  189. void notFinished(int level, GridIndex i, GridIndex j);
  190. void redirect(int level, GridIndex i, GridIndex j, long calcState, Bitmap<RGBColor>* bmp);
  191. signals:
  192. void done(int level, GridIndex i, GridIndex j, Bitmap<RGBColor>* bmp);
  193. };
  194. class MandelView : public QObject
  195. {
  196. Q_OBJECT
  197. public:
  198. std::unique_ptr<Texture> empty;
  199. // a grid should not be deleted once constructed.
  200. // to free up memory one can call TexGrid::clearCells()
  201. std::unordered_map<int, TexGrid> levels;
  202. mnd::Generator* generator;
  203. Calcer calcer;
  204. MandelWidget& owner;
  205. int maxIter;
  206. int width;
  207. int height;
  208. public:
  209. static const int chunkSize = 256;
  210. MandelView(mnd::Generator* generator, MandelWidget& owner, int maxIter);
  211. int getLevel(double dpp);
  212. double getDpp(int level);
  213. TexGrid& getGrid(int level);
  214. inline int getMaxIter(void) const { return this->maxIter; }
  215. void setMaxIter(int maxIter);
  216. void setGenerator(mnd::Generator* generator);
  217. void clearCells(void);
  218. void garbageCollect(int level, GridIndex i, GridIndex j);
  219. GridElement* searchAbove(int level, GridIndex i, GridIndex j, int recursionLevel);
  220. GridElement* searchUnder(int level, GridIndex i, GridIndex j, int recursionLevel);
  221. void paint(const mnd::MandelViewport& mvp);
  222. public slots:
  223. void cellReady(int level, GridIndex i, GridIndex j, Bitmap<RGBColor>* bmp);
  224. signals:
  225. void redrawRequested(void);
  226. };
  227. class MandelWidget : public QOpenGLWidget
  228. {
  229. Q_OBJECT
  230. private:
  231. mnd::MandelContext& mndContext;
  232. bool smoothColoring = true;
  233. Gradient gradient;
  234. bool initialized = false;
  235. int maxIterations = 2000;
  236. volatile bool rubberbanding = false;
  237. QRectF rubberband;
  238. volatile bool dragging = false;
  239. int dragX, dragY;
  240. mnd::MandelViewport currentViewport;
  241. mnd::MandelViewport targetViewport;
  242. std::chrono::time_point<std::chrono::high_resolution_clock> lastAnimUpdate;
  243. std::unique_ptr<MandelView> mandelView;
  244. public:
  245. MandelWidget(mnd::MandelContext& ctxt, QWidget* parent = nullptr);
  246. ~MandelWidget(void) override;
  247. inline const Gradient& getGradient(void) const { return gradient; }
  248. void setGradient(Gradient g);
  249. inline bool getSmoothColoring(void) const { return smoothColoring; }
  250. void setSmoothColoring(bool sc);
  251. void initializeGL(void) override;
  252. void resizeGL(int w, int h) override;
  253. void paintGL() override;
  254. private:
  255. void updateAnimations(void);
  256. void drawRubberband(void);
  257. public:
  258. void zoom(float scale, float x = 0.5f, float y = 0.5f);
  259. void setViewport(const mnd::MandelViewport& viewport);
  260. void setMaxIterations(int maxIter);
  261. //void redraw();
  262. void requestRecalc(void);
  263. void resizeEvent(QResizeEvent* re) override;
  264. void mousePressEvent(QMouseEvent* me) override;
  265. void mouseMoveEvent(QMouseEvent* me) override;
  266. void mouseReleaseEvent(QMouseEvent* me) override;
  267. void wheelEvent(QWheelEvent * we) override;
  268. inline const mnd::MandelViewport& getViewport(void) const { return targetViewport; }
  269. signals:
  270. void needsUpdate(const mnd::MandelInfo vp);
  271. public slots:
  272. //void viewUpdated(Bitmap<RGBColor>* bitmap);
  273. };