MandelWidget.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. 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. mnd::Real juliaX;
  147. mnd::Real juliaY;
  148. long calcState = 0;
  149. inline Job(mnd::MandelGenerator* generator,
  150. const Gradient& gradient,
  151. MandelWidget& owner,
  152. TexGrid* grid,
  153. int level, GridIndex i, GridIndex j,
  154. const mnd::Real& juliaX, const mnd::Real& juliaY,
  155. long calcState) :
  156. generator{ generator },
  157. gradient{ gradient },
  158. owner{ owner },
  159. grid{ grid },
  160. level{ level },
  161. i{ i }, j{ j },
  162. juliaX{ juliaX }, juliaY{ juliaY },
  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. public:
  177. mnd::Real juliaX;
  178. mnd::Real juliaY;
  179. private:
  180. std::unique_ptr<QThreadPool> threadPool;
  181. MandelWidget& owner;
  182. const Gradient& gradient;
  183. int maxIter;
  184. bool smooth;
  185. int currentLevel;
  186. volatile long calcState = 0;
  187. public:
  188. Calcer(mnd::MandelGenerator* generator, MandelWidget& owner, int maxIter, bool smooth);
  189. void setMaxIter(int maxIter);
  190. void clearAll(void);
  191. void setGenerator(mnd::MandelGenerator* generator) { this->generator = generator; changeState(); }
  192. void setJuliaPos(const mnd::Real& x, const mnd::Real& y);
  193. inline void changeState(void) { calcState++; }
  194. public slots:
  195. void calc(TexGrid& grid, int level, GridIndex i, GridIndex j, int priority);
  196. void setCurrentLevel(int level);
  197. void notFinished(int level, GridIndex i, GridIndex j);
  198. void redirect(int level, GridIndex i, GridIndex j, long calcState, Bitmap<RGBColor>* bmp);
  199. signals:
  200. void done(int level, GridIndex i, GridIndex j, Bitmap<RGBColor>* bmp);
  201. };
  202. class MandelView : public QObject
  203. {
  204. Q_OBJECT
  205. public:
  206. std::unique_ptr<Texture> empty;
  207. // a grid should not be deleted once constructed.
  208. // to free up memory one can call TexGrid::clearCells()
  209. std::unordered_map<int, TexGrid> levels;
  210. mnd::MandelGenerator* generator;
  211. Calcer calcer;
  212. MandelWidget& owner;
  213. int maxIter;
  214. int width;
  215. int height;
  216. public:
  217. static const int chunkSize;
  218. MandelView(mnd::MandelGenerator* generator, MandelWidget& owner, int maxIter);
  219. int getLevel(mnd::Real dpp);
  220. mnd::Real getDpp(int level);
  221. TexGrid& getGrid(int level);
  222. inline int getMaxIter(void) const { return this->maxIter; }
  223. void setMaxIter(int maxIter);
  224. void setGenerator(mnd::MandelGenerator* generator);
  225. void clearCells(void);
  226. void garbageCollect(int level, GridIndex i, GridIndex j);
  227. GridElement* searchAbove(int level, GridIndex i, GridIndex j, int recursionLevel);
  228. GridElement* searchUnder(int level, GridIndex i, GridIndex j, int recursionLevel);
  229. void paint(const mnd::MandelViewport& mvp);
  230. public slots:
  231. void cellReady(int level, GridIndex i, GridIndex j, Bitmap<RGBColor>* bmp);
  232. signals:
  233. void redrawRequested(void);
  234. };
  235. class MandelWidget : public QOpenGLWidget
  236. {
  237. Q_OBJECT
  238. private:
  239. mnd::MandelContext& mndContext;
  240. mnd::MandelGenerator* generator;
  241. bool smoothColoring = true;
  242. Gradient gradient;
  243. bool initialized = false;
  244. int maxIterations = 2000;
  245. volatile bool selectingPoint = false;
  246. float pointX;
  247. float pointY;
  248. volatile bool rubberbanding = false;
  249. QRectF rubberband;
  250. volatile bool dragging = false;
  251. int dragX, dragY;
  252. bool displayInfo = false;
  253. mnd::MandelViewport currentViewport;
  254. mnd::MandelViewport targetViewport;
  255. std::chrono::time_point<std::chrono::high_resolution_clock> lastAnimUpdate;
  256. std::unique_ptr<MandelView> mandelView;
  257. public:
  258. MandelWidget(mnd::MandelContext& ctxt, mnd::MandelGenerator* generator, QWidget* parent = nullptr);
  259. ~MandelWidget(void) override;
  260. inline const Gradient& getGradient(void) const { return gradient; }
  261. void setGradient(Gradient g);
  262. inline bool getSmoothColoring(void) const { return smoothColoring; }
  263. void setSmoothColoring(bool sc);
  264. inline bool doesDisplayInfo(void) const { return displayInfo; }
  265. void setDisplayInfo(bool di);
  266. inline int getMaxIterations(void) const { return maxIterations; }
  267. void setMaxIterations(int maxIter);
  268. void setJuliaPos(const mnd::Real& x, const mnd::Real& y);
  269. const mnd::Real& getJuliaX(void) { return mandelView->calcer.juliaX; }
  270. const mnd::Real& getJuliaY(void) { return mandelView->calcer.juliaY; }
  271. inline mnd::MandelGenerator* getGenerator(void) const { return generator; }
  272. void setGenerator(mnd::MandelGenerator* generator);
  273. void initializeGL(void) override;
  274. void paintGL() override;
  275. private:
  276. void updateAnimations(void);
  277. void drawRubberband(void);
  278. void drawInfo(void);
  279. void drawPoint(void);
  280. public:
  281. void zoom(float scale, float x = 0.5f, float y = 0.5f);
  282. void setViewport(const mnd::MandelViewport& viewport);
  283. void selectPoint(void);
  284. void requestRecalc(void);
  285. void resizeEvent(QResizeEvent* re) override;
  286. void mousePressEvent(QMouseEvent* me) override;
  287. void mouseMoveEvent(QMouseEvent* me) override;
  288. void mouseReleaseEvent(QMouseEvent* me) override;
  289. void wheelEvent(QWheelEvent * we) override;
  290. inline const mnd::MandelViewport& getViewport(void) const { return targetViewport; }
  291. signals:
  292. void needsUpdate(const mnd::MandelInfo vp);
  293. void pointSelected(mnd::Real x, mnd::Real y);
  294. };