MandelWidget.h 11 KB

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