1
0

MandelWidget.h 9.9 KB

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