MandelWidget.h 9.3 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. //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. int maxIter;
  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. int maxIter,
  147. TexGrid* grid,
  148. int level, GridIndex i, GridIndex j,
  149. long calcState) :
  150. generator{ generator },
  151. gradient{ gradient },
  152. maxIter{ maxIter },
  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. const Gradient& gradient;
  171. int maxIter;
  172. int currentLevel;
  173. volatile long calcState = 0;
  174. public:
  175. inline Calcer(mnd::Generator* generator, const Gradient& gradient, int maxIter) :
  176. jobsMutex{ QMutex::Recursive },
  177. generator{ generator },
  178. threadPool{ std::make_unique<QThreadPool>() },
  179. gradient{ gradient },
  180. maxIter{ maxIter }
  181. {
  182. threadPool->setMaxThreadCount(1);
  183. }
  184. void setMaxIter(int maxIter);
  185. void clearAll(void);
  186. void setGenerator(mnd::Generator* generator) { this->generator = generator; changeState(); }
  187. inline void changeState(void) { calcState++; }
  188. public slots:
  189. void calc(TexGrid& grid, int level, GridIndex i, GridIndex j, int priority);
  190. void setCurrentLevel(int level);
  191. void notFinished(int level, GridIndex i, GridIndex j);
  192. void redirect(int level, GridIndex i, GridIndex j, long calcState, Bitmap<RGBColor>* bmp);
  193. signals:
  194. void done(int level, GridIndex i, GridIndex j, Bitmap<RGBColor>* bmp);
  195. };
  196. class MandelView : public QObject
  197. {
  198. Q_OBJECT
  199. public:
  200. std::unique_ptr<Texture> empty;
  201. // a grid should not be deleted once constructed.
  202. // to free up memory one can call TexGrid::clearCells()
  203. std::unordered_map<int, TexGrid> levels;
  204. mnd::Generator* generator;
  205. Calcer calcer;
  206. MandelWidget& owner;
  207. int maxIter;
  208. int width;
  209. int height;
  210. public:
  211. static const int chunkSize;
  212. MandelView(mnd::Generator* generator, MandelWidget& owner, int maxIter);
  213. int getLevel(mnd::Real dpp);
  214. mnd::Real getDpp(int level);
  215. TexGrid& getGrid(int level);
  216. inline int getMaxIter(void) const { return this->maxIter; }
  217. void setMaxIter(int maxIter);
  218. void setGenerator(mnd::Generator* generator);
  219. void clearCells(void);
  220. void garbageCollect(int level, GridIndex i, GridIndex j);
  221. GridElement* searchAbove(int level, GridIndex i, GridIndex j, int recursionLevel);
  222. GridElement* searchUnder(int level, GridIndex i, GridIndex j, int recursionLevel);
  223. void paint(const mnd::MandelViewport& mvp);
  224. public slots:
  225. void cellReady(int level, GridIndex i, GridIndex j, Bitmap<RGBColor>* bmp);
  226. signals:
  227. void redrawRequested(void);
  228. };
  229. class MandelWidget : public QOpenGLWidget
  230. {
  231. Q_OBJECT
  232. private:
  233. mnd::MandelContext& mndContext;
  234. bool smoothColoring = true;
  235. Gradient gradient;
  236. bool initialized = false;
  237. int maxIterations = 2000;
  238. volatile bool rubberbanding = false;
  239. QRectF rubberband;
  240. volatile bool dragging = false;
  241. int dragX, dragY;
  242. bool displayInfo = false;
  243. mnd::MandelViewport currentViewport;
  244. mnd::MandelViewport targetViewport;
  245. std::chrono::time_point<std::chrono::high_resolution_clock> lastAnimUpdate;
  246. std::unique_ptr<MandelView> mandelView;
  247. public:
  248. MandelWidget(mnd::MandelContext& ctxt, QWidget* parent = nullptr);
  249. ~MandelWidget(void) override;
  250. inline const Gradient& getGradient(void) const { return gradient; }
  251. void setGradient(Gradient g);
  252. inline bool getSmoothColoring(void) const { return smoothColoring; }
  253. void setSmoothColoring(bool sc);
  254. inline bool doesDisplayInfo(void) const { return displayInfo; }
  255. void setDisplayInfo(bool di);
  256. void initializeGL(void) override;
  257. void paintGL() override;
  258. private:
  259. void updateAnimations(void);
  260. void drawRubberband(void);
  261. void drawInfo(void);
  262. public:
  263. void zoom(float scale, float x = 0.5f, float y = 0.5f);
  264. void setViewport(const mnd::MandelViewport& viewport);
  265. void setMaxIterations(int maxIter);
  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. };