MandelWidget.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. #pragma once
  2. #include <QGLWidget>
  3. #include <QOpenGLWidget>
  4. #include <QThreadPool>
  5. #include <QMouseEvent>
  6. #include <QOpenGLContext>
  7. #include <QOpenGLFunctions>
  8. #include <QOpenGLFunctions_2_0>
  9. #include <QOpenGLShaderProgram>
  10. #include <QMutex>
  11. #include <QPainter>
  12. //#include <qopengl.h>
  13. //#include <qopenglfunctions.h>
  14. //#include <qopenglcontext.h>
  15. #include <Mandel.h>
  16. #include "Bitmap.h"
  17. #include "Gradient.h"
  18. #include <atomic>
  19. #include <tuple>
  20. #include <deque>
  21. #include <chrono>
  22. #include <unordered_map>
  23. #include <unordered_set>
  24. using GridIndex = mnd::Integer;
  25. Q_DECLARE_METATYPE(GridIndex)
  26. Q_DECLARE_METATYPE(mnd::Real)
  27. class MandelView;
  28. class MandelWidget;
  29. class Texture
  30. {
  31. GLuint id;
  32. public:
  33. QOpenGLFunctions_3_0& gl;
  34. Texture(QOpenGLFunctions_3_0& gl, const Bitmap<RGBColor>& pict, GLint param = GL_LINEAR);
  35. ~Texture(void);
  36. Texture(const Texture& other) = delete;
  37. Texture& operator=(const Texture& other) = delete;
  38. Texture(Texture&& other);
  39. Texture& operator=(Texture&& other) = delete;
  40. private:
  41. void bind(void) const;
  42. public:
  43. inline GLuint getId(void) const { return id; }
  44. void drawRect(float x, float y, float width, float height);
  45. };
  46. class CellImage
  47. {
  48. public:
  49. CellImage(void) = default;
  50. CellImage(CellImage&& b) = default;
  51. CellImage(const CellImage& b) = delete;
  52. virtual ~CellImage(void);
  53. virtual void drawRect(float x, float y, float width, float height) = 0;
  54. virtual std::shared_ptr<CellImage> clip(short i, short j) = 0;
  55. virtual int getRecalcPriority(void) const = 0;
  56. };
  57. class TextureClip : public CellImage
  58. {
  59. std::shared_ptr<Texture> texture;
  60. float tx, ty, tw, th;
  61. public:
  62. inline TextureClip(std::shared_ptr<Texture> tex,
  63. float tx, float ty, float tw, float th) :
  64. texture{ std::move(tex) },
  65. tx{ tx }, ty{ ty }, tw{ tw }, th{ th }
  66. {}
  67. inline TextureClip(std::shared_ptr<Texture> tex) :
  68. TextureClip{ tex, 0.0f, 0.0f, 1.0f, 1.0f }
  69. {}
  70. TextureClip(TextureClip&&) = default;
  71. TextureClip(const TextureClip&) = delete;
  72. virtual ~TextureClip(void);
  73. void drawRect(float x, float y, float width, float height);
  74. TextureClip clip(float x, float y, float w, float h);
  75. std::shared_ptr<CellImage> clip(short i, short j);
  76. int getRecalcPriority(void) const;
  77. };
  78. class QuadImage : public CellImage
  79. {
  80. std::shared_ptr<CellImage> cells[2][2];
  81. public:
  82. inline QuadImage(std::shared_ptr<CellImage> i00,
  83. std::shared_ptr<CellImage> i01,
  84. std::shared_ptr<CellImage> i10,
  85. std::shared_ptr<CellImage> i11) :
  86. cells{ { std::move(i00), std::move(i01) }, { std::move(i10), std::move(i11) } }
  87. {}
  88. QuadImage(QuadImage&&) = default;
  89. QuadImage(const QuadImage&) = delete;
  90. virtual ~QuadImage(void);
  91. void drawRect(float x, float y, float width, float height);
  92. std::shared_ptr<CellImage> clip(short i, short j);
  93. int getRecalcPriority(void) const;
  94. };
  95. struct PairHash {
  96. template <typename T1, typename T2>
  97. std::size_t operator () (const std::pair<T1, T2>& p) const {
  98. auto h1 = std::hash<T1>{}(p.first);
  99. auto h2 = std::hash<T2>{}(p.second);
  100. //boost::hash_combine(h1, p.second);
  101. return (h1 ^ 234579245) * 23452354 + h2;
  102. }
  103. };
  104. struct TripleHash {
  105. template <typename T1, typename T2, typename T3>
  106. std::size_t operator () (const std::tuple<T1, T2, T3>& p) const {
  107. auto h1 = std::hash<T1>{}(std::get<0>(p));
  108. auto h2 = std::hash<T2>{}(std::get<1>(p));
  109. auto h3 = std::hash<T3>{}(std::get<2>(p));
  110. return (((h1 ^ 234579245) * 23452357 + h2) ^ 2345244345) * 23421 + h3;
  111. }
  112. };
  113. struct GridElement
  114. {
  115. bool enoughResolution;
  116. std::shared_ptr<CellImage> img;
  117. inline GridElement(bool enoughResolution, std::shared_ptr<CellImage> img) :
  118. enoughResolution{ enoughResolution },
  119. img{ std::move(img) }
  120. {}
  121. };
  122. class TexGrid
  123. {
  124. public:
  125. MandelView& owner;
  126. int level;
  127. mnd::Real dpp;
  128. std::unordered_map<std::pair<GridIndex, GridIndex>, std::unique_ptr<GridElement>, PairHash> cells;
  129. public:
  130. //inline TexGrid(MandelV& owner) : level{ 1.0 }, owner{ owner } {}
  131. TexGrid(MandelView& owner, int level);
  132. std::pair<GridIndex, GridIndex> getCellIndices(mnd::Real x, mnd::Real y);
  133. std::pair<mnd::Real, mnd::Real> getPositions(GridIndex i, GridIndex j);
  134. GridElement* getCell(GridIndex i, GridIndex j);
  135. void setCell(GridIndex i, GridIndex j, std::unique_ptr<GridElement> tex);
  136. inline size_t countAllocatedCells(void) const { return cells.size(); }
  137. void clearCells(void);
  138. void clearUncleanCells(void);
  139. };
  140. class Job : public QObject, public QRunnable
  141. {
  142. Q_OBJECT
  143. public:
  144. mnd::MandelGenerator* generator;
  145. const Gradient& gradient;
  146. MandelWidget& owner;
  147. TexGrid* grid;
  148. int level;
  149. GridIndex i, j;
  150. long calcState = 0;
  151. inline Job(mnd::MandelGenerator* generator,
  152. const Gradient& gradient,
  153. MandelWidget& owner,
  154. TexGrid* grid,
  155. int level, GridIndex i, GridIndex j,
  156. long calcState) :
  157. generator{ generator },
  158. gradient{ gradient },
  159. owner{ owner },
  160. grid{ grid },
  161. level{ level },
  162. i{ i }, j{ j },
  163. calcState{ calcState }
  164. {}
  165. void run() override;
  166. signals:
  167. void done(int level, GridIndex i, GridIndex j, long calcState, Bitmap<RGBColor>* bmp);
  168. };
  169. class Calcer : public QObject
  170. {
  171. Q_OBJECT
  172. /// tuple contains level, i, j of the job
  173. std::unordered_map<std::tuple<int, GridIndex, GridIndex>, Job*, TripleHash> jobs;
  174. QMutex jobsMutex;
  175. mnd::MandelGenerator* generator;
  176. std::unique_ptr<QThreadPool> threadPool;
  177. MandelWidget& owner;
  178. const Gradient& gradient;
  179. int currentLevel;
  180. volatile unsigned int calcState = 0;
  181. public:
  182. Calcer(mnd::MandelGenerator* generator, MandelWidget& owner);
  183. void clearAll(void);
  184. void setGenerator(mnd::MandelGenerator* 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::MandelGenerator* generator;
  203. Calcer calcer;
  204. MandelWidget& owner;
  205. int width;
  206. int height;
  207. public:
  208. static const int chunkSize;
  209. MandelView(mnd::MandelGenerator* generator, MandelWidget& owner);
  210. int getLevel(mnd::Real dpp);
  211. mnd::Real getDpp(int level);
  212. TexGrid& getGrid(int level);
  213. void setGenerator(mnd::MandelGenerator* generator);
  214. void clearCells(void);
  215. void garbageCollect(int level, GridIndex i, GridIndex j);
  216. GridElement* searchAbove(int level, GridIndex i, GridIndex j, int recursionLevel);
  217. GridElement* searchUnder(int level, GridIndex i, GridIndex j, int recursionLevel);
  218. void paint(const mnd::MandelViewport& mvp, QPainter& qp);
  219. public slots:
  220. void cellReady(int level, GridIndex i, GridIndex j, Bitmap<RGBColor>* bmp);
  221. signals:
  222. void redrawRequested(void);
  223. };
  224. class MandelWidget : public QOpenGLWidget
  225. {
  226. Q_OBJECT
  227. private:
  228. mnd::MandelContext& mndContext;
  229. mnd::MandelGenerator* generator;
  230. mnd::MandelInfo mandelInfo;
  231. bool initialized = false;
  232. Gradient gradient;
  233. volatile bool selectingPoint = false;
  234. float pointX;
  235. float pointY;
  236. volatile bool rubberbanding = false;
  237. QRectF rubberband;
  238. volatile bool dragging = false;
  239. int dragX, dragY;
  240. bool displayInfo = false;
  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. QOpenGLShaderProgram* program;
  246. public:
  247. MandelWidget(mnd::MandelContext& ctxt, mnd::MandelGenerator* generator, QWidget* parent = nullptr);
  248. ~MandelWidget(void) override;
  249. inline const Gradient& getGradient(void) const { return gradient; }
  250. void setGradient(Gradient g);
  251. inline bool getSmoothColoring(void) const { return mandelInfo.smooth; }
  252. void setSmoothColoring(bool sc);
  253. inline bool doesDisplayInfo(void) const { return displayInfo; }
  254. void setDisplayInfo(bool di);
  255. inline int getMaxIterations(void) const { return mandelInfo.maxIter; }
  256. void setMaxIterations(int maxIter);
  257. inline const mnd::MandelInfo& getMandelInfo(void) const { return mandelInfo; }
  258. inline mnd::MandelInfo& getMandelInfo(void) { return mandelInfo; }
  259. void setJuliaPos(const mnd::Real& x, const mnd::Real& y);
  260. const mnd::Real& getJuliaX(void) { return mandelInfo.juliaX; }
  261. const mnd::Real& getJuliaY(void) { return mandelInfo.juliaY; }
  262. inline mnd::MandelGenerator* getGenerator(void) const { return generator; }
  263. void setGenerator(mnd::MandelGenerator* generator);
  264. void clearAll(void);
  265. void initializeGL(void) override;
  266. void resizeGL(int w, int h) override;
  267. void paintGL() override;
  268. private:
  269. void updateAnimations(void);
  270. void drawRubberband(void);
  271. void drawInfo(void);
  272. void drawPoint(void);
  273. public:
  274. void zoom(float scale, float x = 0.5f, float y = 0.5f);
  275. void setViewport(const mnd::MandelViewport& viewport);
  276. void selectPoint(void);
  277. void stopSelectingPoint(void);
  278. void requestRecalc(void);
  279. void resizeEvent(QResizeEvent* re) override;
  280. void mousePressEvent(QMouseEvent* me) override;
  281. void mouseMoveEvent(QMouseEvent* me) override;
  282. void mouseReleaseEvent(QMouseEvent* me) override;
  283. void wheelEvent(QWheelEvent * we) override;
  284. inline const mnd::MandelViewport& getViewport(void) const { return targetViewport; }
  285. signals:
  286. void needsUpdate(const mnd::MandelInfo vp);
  287. void pointSelected(mnd::Real x, mnd::Real y);
  288. };