MandelWidget.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #pragma once
  2. #include <QGLWidget>
  3. #include <QOpenGLWidget>
  4. #include <QThread>
  5. #include <qopengl.h>
  6. #include <qopenglfunctions.h>
  7. #include <qopenglcontext.h>
  8. #include <qscrollarea.h>
  9. #include <qlabel.h>
  10. #include <qevent.h>
  11. #include <qrubberband.h>
  12. #include "Bitmap.h"
  13. #include "Gradient.h"
  14. #include <Mandel.h>
  15. #include <future>
  16. #include <thread>
  17. #include <mutex>
  18. #include <atomic>
  19. #include <unordered_map>
  20. class MandelWidget;
  21. class Texture
  22. {
  23. GLuint id;
  24. QOpenGLContext* context;
  25. public:
  26. Texture(const Bitmap<RGBColor>& pict);
  27. Texture(const Bitmap<RGBColor>& pict, QOpenGLContext* context);
  28. ~Texture(void);
  29. Texture(const Texture& other) = delete;
  30. Texture& operator=(const Texture& other) = delete;
  31. Texture(Texture&& other) = default;
  32. Texture& operator=(Texture&& other) = default;
  33. void bind(void) const;
  34. inline GLuint getId(void) const { return id; }
  35. void drawRect(float x, float y, float width, float height);
  36. };
  37. struct MandelClip
  38. {
  39. mnd::MandelViewport view;
  40. };
  41. struct PairHash {
  42. template <typename T1, typename T2>
  43. std::size_t operator () (const std::pair<T1, T2>& p) const {
  44. auto h1 = std::hash<T1>{}(p.first);
  45. auto h2 = std::hash<T2>{}(p.second);
  46. return (h1 ^ 234579245) * 23452354 + h2;
  47. }
  48. };
  49. class TexGrid
  50. {
  51. double dpp;
  52. std::unordered_map<std::pair<int, int>, Texture, PairHash> cells;
  53. public:
  54. inline TexGrid(void) : dpp{ 1.0 } {}
  55. inline TexGrid(double dpp) : dpp{ dpp } {}
  56. std::pair<int, int> getCellIndices(double x, double y);
  57. std::pair<double, double> getPositions(int i, int j);
  58. Texture* getCell(int i, int j);
  59. };
  60. class MandelV
  61. {
  62. public:
  63. Texture empty;
  64. std::unordered_map<int, TexGrid> levels;
  65. public:
  66. MandelV(QOpenGLContext* context);
  67. int getLevel(double dpp);
  68. double getDpp(int level);
  69. TexGrid& getGrid(int level);
  70. void paint(const mnd::MandelViewport& mvp);
  71. };
  72. class MandelView : public QObject
  73. {
  74. Q_OBJECT
  75. private:
  76. std::future<void> calc;
  77. QThread calcThread;
  78. std::mutex mut;
  79. std::condition_variable condVar;
  80. std::atomic<mnd::MandelInfo> toCalc;
  81. std::atomic_bool hasToCalc;
  82. std::atomic_bool finish;
  83. mnd::Generator* generator;
  84. Gradient& gradient;
  85. MandelWidget* mWidget;
  86. //QOpenGLContext* context;
  87. public:
  88. MandelView(mnd::Generator& generator, Gradient& gradient, MandelWidget* mWidget);
  89. ~MandelView(void);
  90. void setGenerator(mnd::Generator &value);
  91. void start();
  92. private slots:
  93. void loop();
  94. public slots:
  95. void adaptViewport(const mnd::MandelInfo vp);
  96. signals:
  97. void updated(Bitmap<RGBColor>* bitmap);
  98. };
  99. class MandelWidget : public QOpenGLWidget
  100. {
  101. Q_OBJECT
  102. private:
  103. //QScrollArea qsa;
  104. //QLabel ql;
  105. mnd::MandelContext& mndContext;
  106. Gradient gradient;
  107. bool initialized = false;
  108. int maxIterations = 2000;
  109. bool rubberbandDragging = false;
  110. QRectF rubberband;
  111. std::unique_ptr<Texture> tex;
  112. mnd::MandelViewport viewport;
  113. MandelView mv;
  114. std::unique_ptr<MandelV> v;
  115. public:
  116. MandelWidget(mnd::MandelContext& ctxt, QWidget* parent = nullptr);
  117. ~MandelWidget(void) override;
  118. /*inline MandelWidget(const MandelWidget& other) :
  119. mndContext{ other.mndContext },
  120. mv{ other.mndContext }
  121. {
  122. }*/
  123. inline const Gradient& getGradient(void) const { return gradient; }
  124. void initializeGL(void) override;
  125. void resizeGL(int w, int h) override;
  126. void paintGL() override;
  127. void drawRubberband(void);
  128. void zoom(float scale);
  129. void setMaxIterations(int maxIter);
  130. //void redraw();
  131. void requestRecalc(void);
  132. void resizeEvent(QResizeEvent* re) override;
  133. void mousePressEvent(QMouseEvent* me) override;
  134. void mouseMoveEvent(QMouseEvent* me) override;
  135. void mouseReleaseEvent(QMouseEvent* me) override;
  136. inline const mnd::MandelViewport& getViewport(void) const { return viewport; }
  137. signals:
  138. void needsUpdate(const mnd::MandelInfo vp);
  139. public slots:
  140. void viewUpdated(Bitmap<RGBColor>* bitmap);
  141. };