1
0

MandelWidget.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 <QMutex>
  10. #include <QPainter>
  11. //#include <qopengl.h>
  12. //#include <qopenglfunctions.h>
  13. //#include <qopenglcontext.h>
  14. #include <Mandel.h>
  15. #include "Bitmap.h"
  16. #include "Gradient.h"
  17. #include <atomic>
  18. #include <tuple>
  19. #include <deque>
  20. #include <chrono>
  21. #include <unordered_map>
  22. #include <unordered_set>
  23. using GridIndex = mnd::Integer;
  24. Q_DECLARE_METATYPE(GridIndex)
  25. Q_DECLARE_METATYPE(mnd::Real)
  26. class MandelView;
  27. class MandelWidget;
  28. class Texture
  29. {
  30. GLuint id;
  31. public:
  32. QOpenGLFunctions_2_0& gl;
  33. Texture(QOpenGLFunctions_2_0& gl, const Bitmap<RGBColor>& pict, GLint param = GL_LINEAR);
  34. ~Texture(void);
  35. Texture(const Texture& other) = delete;
  36. Texture& operator=(const Texture& other) = delete;
  37. Texture(Texture&& other);
  38. Texture& operator=(Texture&& other) = delete;
  39. private:
  40. void bind(void) const;
  41. public:
  42. inline GLuint getId(void) const { return id; }
  43. void drawRect(float x, float y, float width, float height);
  44. };
  45. class CellImage
  46. {
  47. public:
  48. CellImage(void) = default;
  49. CellImage(CellImage&& b) = default;
  50. CellImage(const CellImage& b) = delete;
  51. virtual ~CellImage(void);
  52. virtual void drawRect(float x, float y, float width, float height) = 0;
  53. virtual std::shared_ptr<CellImage> clip(short i, short j) = 0;
  54. virtual int getRecalcPriority(void) const = 0;
  55. };
  56. class TextureClip : public CellImage
  57. {
  58. std::shared_ptr<Texture> texture;
  59. float tx, ty, tw, th;
  60. public:
  61. inline TextureClip(std::shared_ptr<Texture> tex,
  62. float tx, float ty, float tw, float th) :
  63. texture{ std::move(tex) },
  64. tx{ tx }, ty{ ty }, tw{ tw }, th{ th }
  65. {}
  66. inline TextureClip(std::shared_ptr<Texture> tex) :
  67. TextureClip{ tex, 0.0f, 0.0f, 1.0f, 1.0f }
  68. {}
  69. TextureClip(TextureClip&&) = default;
  70. TextureClip(const TextureClip&) = delete;
  71. virtual ~TextureClip(void);
  72. void drawRect(float x, float y, float width, float height);
  73. TextureClip clip(float x, float y, float w, float h);
  74. std::shared_ptr<CellImage> clip(short i, short j);
  75. int getRecalcPriority(void) const;
  76. };
  77. class QuadImage : public CellImage
  78. {
  79. std::shared_ptr<CellImage> cells[2][2];
  80. public:
  81. inline QuadImage(std::shared_ptr<CellImage> i00,
  82. std::shared_ptr<CellImage> i01,
  83. std::shared_ptr<CellImage> i10,
  84. std::shared_ptr<CellImage> i11) :
  85. cells{ { std::move(i00), std::move(i01) }, { std::move(i10), std::move(i11) } }
  86. {}
  87. QuadImage(QuadImage&&) = default;
  88. QuadImage(const QuadImage&) = delete;
  89. virtual ~QuadImage(void);
  90. void drawRect(float x, float y, float width, float height);
  91. std::shared_ptr<CellImage> clip(short i, short j);
  92. int getRecalcPriority(void) const;
  93. };
  94. struct PairHash {
  95. template <typename T1, typename T2>
  96. std::size_t operator () (const std::pair<T1, T2>& p) const {
  97. auto h1 = std::hash<T1>{}(p.first);
  98. auto h2 = std::hash<T2>{}(p.second);
  99. //boost::hash_combine(h1, p.second);
  100. return (h1 ^ 234579245) * 23452354 + h2;
  101. }
  102. };
  103. struct TripleHash {
  104. template <typename T1, typename T2, typename T3>
  105. std::size_t operator () (const std::tuple<T1, T2, T3>& p) const {
  106. auto h1 = std::hash<T1>{}(std::get<0>(p));
  107. auto h2 = std::hash<T2>{}(std::get<1>(p));
  108. auto h3 = std::hash<T3>{}(std::get<2>(p));
  109. return (((h1 ^ 234579245) * 23452357 + h2) ^ 2345244345) * 23421 + h3;
  110. }
  111. };
  112. struct GridElement
  113. {
  114. bool enoughResolution;
  115. std::shared_ptr<CellImage> img;
  116. inline GridElement(bool enoughResolution, std::shared_ptr<CellImage> img) :
  117. enoughResolution{ enoughResolution },
  118. img{ std::move(img) }
  119. {}
  120. };
  121. class TexGrid
  122. {
  123. public:
  124. MandelView& owner;
  125. int level;
  126. mnd::Real dpp;
  127. std::unordered_map<std::pair<GridIndex, GridIndex>, std::unique_ptr<GridElement>, PairHash> cells;
  128. public:
  129. //inline TexGrid(MandelV& owner) : level{ 1.0 }, owner{ owner } {}
  130. TexGrid(MandelView& owner, int level);
  131. std::pair<GridIndex, GridIndex> getCellIndices(mnd::Real x, mnd::Real y);
  132. std::pair<mnd::Real, mnd::Real> getPositions(GridIndex i, GridIndex j);
  133. GridElement* getCell(GridIndex i, GridIndex j);
  134. void setCell(GridIndex i, GridIndex j, std::unique_ptr<GridElement> tex);
  135. inline size_t countAllocatedCells(void) const { return cells.size(); }
  136. void clearCells(void);
  137. void clearUncleanCells(void);
  138. };
  139. class Job : public QObject, public QRunnable
  140. {
  141. Q_OBJECT
  142. public:
  143. mnd::MandelGenerator* generator;
  144. const Gradient& gradient;
  145. MandelWidget& owner;
  146. TexGrid* grid;
  147. int level;
  148. GridIndex i, j;
  149. long calcState = 0;
  150. inline Job(mnd::MandelGenerator* generator,
  151. const Gradient& gradient,
  152. MandelWidget& owner,
  153. TexGrid* grid,
  154. int level, GridIndex i, GridIndex j,
  155. long calcState) :
  156. generator{ generator },
  157. gradient{ gradient },
  158. owner{ owner },
  159. grid{ grid },
  160. level{ level },
  161. i{ i }, j{ j },
  162. calcState{ calcState }
  163. {}
  164. void run() override;
  165. signals:
  166. void done(int level, GridIndex i, GridIndex j, long calcState, Bitmap<RGBColor>* bmp);
  167. };
  168. class Calcer : public QObject
  169. {
  170. Q_OBJECT
  171. /// tuple contains level, i, j of the job
  172. std::unordered_map<std::tuple<int, GridIndex, GridIndex>, Job*, TripleHash> jobs;
  173. QMutex jobsMutex;
  174. mnd::MandelGenerator* generator;
  175. std::unique_ptr<QThreadPool> threadPool;
  176. MandelWidget& owner;
  177. const Gradient& gradient;
  178. int currentLevel;
  179. volatile unsigned int calcState = 0;
  180. public:
  181. Calcer(mnd::MandelGenerator* generator, MandelWidget& owner);
  182. void clearAll(void);
  183. void setGenerator(mnd::MandelGenerator* generator) { this->generator = generator; changeState(); }
  184. inline void changeState(void) { calcState++; }
  185. public slots:
  186. void calc(TexGrid& grid, int level, GridIndex i, GridIndex j, int priority);
  187. void setCurrentLevel(int level);
  188. void notFinished(int level, GridIndex i, GridIndex j);
  189. void redirect(int level, GridIndex i, GridIndex j, long calcState, Bitmap<RGBColor>* bmp);
  190. signals:
  191. void done(int level, GridIndex i, GridIndex j, Bitmap<RGBColor>* bmp);
  192. };
  193. class MandelView : public QObject
  194. {
  195. Q_OBJECT
  196. public:
  197. std::unique_ptr<Texture> empty;
  198. // a grid should not be deleted once constructed.
  199. // to free up memory one can call TexGrid::clearCells()
  200. std::unordered_map<int, TexGrid> levels;
  201. mnd::MandelGenerator* generator;
  202. Calcer calcer;
  203. MandelWidget& owner;
  204. int width;
  205. int height;
  206. public:
  207. static const int chunkSize;
  208. MandelView(mnd::MandelGenerator* generator, MandelWidget& owner);
  209. int getLevel(mnd::Real dpp);
  210. mnd::Real getDpp(int level);
  211. TexGrid& getGrid(int level);
  212. void setGenerator(mnd::MandelGenerator* 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, QPainter& qp);
  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. mnd::MandelGenerator* generator;
  229. mnd::MandelInfo mandelInfo;
  230. bool initialized = false;
  231. Gradient gradient;
  232. volatile bool selectingPoint = false;
  233. float pointX;
  234. float pointY;
  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::MandelGenerator* 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 mandelInfo.smooth; }
  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 mandelInfo.maxIter; }
  254. void setMaxIterations(int maxIter);
  255. inline const mnd::MandelInfo& getMandelInfo(void) const { return mandelInfo; }
  256. inline mnd::MandelInfo& getMandelInfo(void) { return mandelInfo; }
  257. void setJuliaPos(const mnd::Real& x, const mnd::Real& y);
  258. const mnd::Real& getJuliaX(void) { return mandelInfo.juliaX; }
  259. const mnd::Real& getJuliaY(void) { return mandelInfo.juliaY; }
  260. inline mnd::MandelGenerator* getGenerator(void) const { return generator; }
  261. void setGenerator(mnd::MandelGenerator* generator);
  262. void clearAll(void);
  263. void initializeGL(void) override;
  264. void resizeGL(int w, int h) override;
  265. void paintGL() override;
  266. private:
  267. void updateAnimations(void);
  268. void drawRubberband(void);
  269. void drawInfo(void);
  270. void drawPoint(void);
  271. public:
  272. void zoom(float scale, float x = 0.5f, float y = 0.5f);
  273. void setViewport(const mnd::MandelViewport& viewport);
  274. void selectPoint(void);
  275. void stopSelectingPoint(void);
  276. void requestRecalc(void);
  277. void resizeEvent(QResizeEvent* re) override;
  278. void mousePressEvent(QMouseEvent* me) override;
  279. void mouseMoveEvent(QMouseEvent* me) override;
  280. void mouseReleaseEvent(QMouseEvent* me) override;
  281. void wheelEvent(QWheelEvent * we) override;
  282. inline const mnd::MandelViewport& getViewport(void) const { return targetViewport; }
  283. signals:
  284. void needsUpdate(const mnd::MandelInfo vp);
  285. void pointSelected(mnd::Real x, mnd::Real y);
  286. };