MandelWidget.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. //boost::hash_combine(h1, p.second);
  94. return (h1 ^ 234579245) * 23452354 + h2;
  95. }
  96. };
  97. struct TripleHash {
  98. template <typename T1, typename T2, typename T3>
  99. std::size_t operator () (const std::tuple<T1, T2, T3>& p) const {
  100. auto h1 = std::hash<T1>{}(std::get<0>(p));
  101. auto h2 = std::hash<T2>{}(std::get<1>(p));
  102. auto h3 = std::hash<T3>{}(std::get<2>(p));
  103. return (((h1 ^ 234579245) * 23452357 + h2) ^ 2345244345) * 23421 + h3;
  104. }
  105. };
  106. struct GridElement
  107. {
  108. bool enoughResolution;
  109. std::shared_ptr<CellImage> img;
  110. inline GridElement(bool enoughResolution, std::shared_ptr<CellImage> img) :
  111. enoughResolution{ enoughResolution },
  112. img{ std::move(img) }
  113. {}
  114. };
  115. class TexGrid
  116. {
  117. public:
  118. MandelView& owner;
  119. int level;
  120. mnd::Real dpp;
  121. std::unordered_map<std::pair<GridIndex, GridIndex>, std::unique_ptr<GridElement>, PairHash> cells;
  122. public:
  123. //inline TexGrid(MandelV& owner) : level{ 1.0 }, owner{ owner } {}
  124. TexGrid(MandelView& owner, int level);
  125. std::pair<GridIndex, GridIndex> getCellIndices(mnd::Real x, mnd::Real y);
  126. std::pair<mnd::Real, mnd::Real> getPositions(GridIndex i, GridIndex j);
  127. GridElement* getCell(GridIndex i, GridIndex j);
  128. void setCell(GridIndex i, GridIndex j, std::unique_ptr<GridElement> tex);
  129. inline size_t countAllocatedCells(void) const { return cells.size(); }
  130. void clearCells(void);
  131. void clearUncleanCells(void);
  132. };
  133. class Job : public QObject, public QRunnable
  134. {
  135. Q_OBJECT
  136. public:
  137. mnd::Generator* generator;
  138. const Gradient& gradient;
  139. MandelWidget& owner;
  140. TexGrid* grid;
  141. int level;
  142. GridIndex i, j;
  143. long calcState = 0;
  144. inline Job(mnd::Generator* generator,
  145. const Gradient& gradient,
  146. MandelWidget& owner,
  147. TexGrid* grid,
  148. int level, GridIndex i, GridIndex j,
  149. long calcState) :
  150. generator{ generator },
  151. gradient{ gradient },
  152. owner{ owner },
  153. grid{ grid },
  154. level{ level },
  155. i{ i }, j{ j },
  156. calcState{ calcState }
  157. {}
  158. void run() override;
  159. signals:
  160. void done(int level, GridIndex i, GridIndex j, long calcState, Bitmap<RGBColor>* bmp);
  161. };
  162. class Calcer : public QObject
  163. {
  164. Q_OBJECT
  165. /// tuple contains level, i, j of the job
  166. std::unordered_map<std::tuple<int, GridIndex, GridIndex>, Job*, TripleHash> jobs;
  167. QMutex jobsMutex;
  168. mnd::Generator* generator;
  169. std::unique_ptr<QThreadPool> threadPool;
  170. MandelWidget& owner;
  171. const Gradient& gradient;
  172. int maxIter;
  173. bool smooth;
  174. int currentLevel;
  175. volatile long calcState = 0;
  176. public:
  177. Calcer(mnd::Generator* generator, MandelWidget& owner, int maxIter, bool smooth);
  178. void setMaxIter(int maxIter);
  179. void clearAll(void);
  180. void setGenerator(mnd::Generator* generator) { this->generator = generator; changeState(); }
  181. inline void changeState(void) { calcState++; }
  182. public slots:
  183. void calc(TexGrid& grid, int level, GridIndex i, GridIndex j, int priority);
  184. void setCurrentLevel(int level);
  185. void notFinished(int level, GridIndex i, GridIndex j);
  186. void redirect(int level, GridIndex i, GridIndex j, long calcState, Bitmap<RGBColor>* bmp);
  187. signals:
  188. void done(int level, GridIndex i, GridIndex j, Bitmap<RGBColor>* bmp);
  189. };
  190. class MandelView : public QObject
  191. {
  192. Q_OBJECT
  193. public:
  194. std::unique_ptr<Texture> empty;
  195. // a grid should not be deleted once constructed.
  196. // to free up memory one can call TexGrid::clearCells()
  197. std::unordered_map<int, TexGrid> levels;
  198. mnd::Generator* generator;
  199. Calcer calcer;
  200. MandelWidget& owner;
  201. int maxIter;
  202. int width;
  203. int height;
  204. public:
  205. static const int chunkSize;
  206. MandelView(mnd::Generator* generator, MandelWidget& owner, int maxIter);
  207. int getLevel(mnd::Real dpp);
  208. mnd::Real getDpp(int level);
  209. TexGrid& getGrid(int level);
  210. inline int getMaxIter(void) const { return this->maxIter; }
  211. void setMaxIter(int maxIter);
  212. void setGenerator(mnd::Generator* generator);
  213. void clearCells(void);
  214. void garbageCollect(int level, GridIndex i, GridIndex j);
  215. GridElement* searchAbove(int level, GridIndex i, GridIndex j, int recursionLevel);
  216. GridElement* searchUnder(int level, GridIndex i, GridIndex j, int recursionLevel);
  217. void paint(const mnd::MandelViewport& mvp);
  218. public slots:
  219. void cellReady(int level, GridIndex i, GridIndex j, Bitmap<RGBColor>* bmp);
  220. signals:
  221. void redrawRequested(void);
  222. };
  223. class MandelWidget : public QOpenGLWidget
  224. {
  225. Q_OBJECT
  226. private:
  227. mnd::MandelContext& mndContext;
  228. bool smoothColoring = true;
  229. Gradient gradient;
  230. bool initialized = false;
  231. int maxIterations = 2000;
  232. volatile bool rubberbanding = false;
  233. QRectF rubberband;
  234. volatile bool dragging = false;
  235. int dragX, dragY;
  236. bool displayInfo = false;
  237. mnd::MandelViewport currentViewport;
  238. mnd::MandelViewport targetViewport;
  239. std::chrono::time_point<std::chrono::high_resolution_clock> lastAnimUpdate;
  240. std::unique_ptr<MandelView> mandelView;
  241. public:
  242. MandelWidget(mnd::MandelContext& ctxt, QWidget* parent = nullptr);
  243. ~MandelWidget(void) override;
  244. inline const Gradient& getGradient(void) const { return gradient; }
  245. void setGradient(Gradient g);
  246. inline bool getSmoothColoring(void) const { return smoothColoring; }
  247. void setSmoothColoring(bool sc);
  248. inline bool doesDisplayInfo(void) const { return displayInfo; }
  249. void setDisplayInfo(bool di);
  250. inline int getMaxIterations(void) const { return maxIterations; }
  251. void setMaxIterations(int maxIter);
  252. void initializeGL(void) override;
  253. void paintGL() override;
  254. private:
  255. void updateAnimations(void);
  256. void drawRubberband(void);
  257. void drawInfo(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 requestRecalc(void);
  262. void resizeEvent(QResizeEvent* re) override;
  263. void mousePressEvent(QMouseEvent* me) override;
  264. void mouseMoveEvent(QMouseEvent* me) override;
  265. void mouseReleaseEvent(QMouseEvent* me) override;
  266. void wheelEvent(QWheelEvent * we) override;
  267. inline const mnd::MandelViewport& getViewport(void) const { return targetViewport; }
  268. signals:
  269. void needsUpdate(const mnd::MandelInfo vp);
  270. };