1
0

MandelWidget.h 10.0 KB

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