MandelWidget.h 9.4 KB

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