MandelWidget.h 11 KB

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