FractalZoomWidget.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. #include "FractalZoomWidget.h"
  2. #include <QOpenGLContext>
  3. #include <QOpenGLFunctions>
  4. SliceGrid::SliceGrid(FractalZoomWidget& owner, int level) :
  5. owner{ owner },
  6. level{ level },
  7. dpp{ owner.getDpp(level) }
  8. {
  9. }
  10. std::pair<GridIndex, GridIndex> SliceGrid::getCellIndices(mnd::Real x, mnd::Real y)
  11. {
  12. return {
  13. GridIndex(mnd::floor(x / dpp / FractalZoomWidget::chunkSize)),
  14. GridIndex(mnd::floor(y / dpp / FractalZoomWidget::chunkSize))
  15. };
  16. }
  17. std::pair<mnd::Real, mnd::Real> SliceGrid::getPositions(GridIndex x, GridIndex y)
  18. {
  19. return {
  20. mnd::Real(x) * dpp * FractalZoomWidget::chunkSize,
  21. mnd::Real(y) * dpp * FractalZoomWidget::chunkSize
  22. };
  23. }
  24. GridElement* SliceGrid::getCell(GridIndex i, GridIndex j)
  25. {
  26. auto cIt = cells.find({i, j});
  27. if (cIt != cells.end()) {
  28. return cIt->second.get();
  29. }
  30. else {
  31. return nullptr;
  32. }
  33. }
  34. void SliceGrid::setCell(GridIndex i, GridIndex j, std::unique_ptr<GridElement> tex)
  35. {
  36. cells[{i, j}] = std::move(tex);
  37. }
  38. void SliceGrid::clearCells(void)
  39. {
  40. cells.clear();
  41. }
  42. void SliceGrid::clearUncleanCells(void)
  43. {
  44. for (auto it = cells.begin(); it != cells.end();) {
  45. if (it->second->img->getRecalcPriority() > 1)
  46. cells.erase(it++);
  47. else ++it;
  48. }
  49. }
  50. Calcer::Calcer(FractalZoomWidget& owner) :
  51. jobsMutex{ QMutex::Recursive },
  52. threadPool{ new QThreadPool(this) },
  53. owner{ owner }
  54. {
  55. threadPool->setMaxThreadCount(1);
  56. }
  57. void Calcer::clearAll(void)
  58. {
  59. this->threadPool->clear();
  60. }
  61. void Calcer::calc(SliceGrid& grid, int level, GridIndex i, GridIndex j, int priority)
  62. {
  63. jobsMutex.lock();
  64. if (jobs.find({ level, i, j }) == jobs.end()) {
  65. CalcJob* job = new CalcJob(owner, owner.getGenerator(), &grid, level, i, j, calcState);
  66. connect(job, &CalcJob::done, this, &Calcer::redirect);
  67. connect(job, &CalcJob::failed, this, &Calcer::jobFailed);
  68. connect(job, &QObject::destroyed, this, [this, level, i, j] () { this->notFinished(level, i, j); });
  69. jobs.emplace(std::tuple{ level, i, j }, job);
  70. threadPool->start(job, priority);
  71. }
  72. jobsMutex.unlock();
  73. }
  74. void Calcer::setCurrentLevel(int level)
  75. {
  76. if (this->currentLevel != level) {
  77. this->currentLevel = level;
  78. std::vector<QRunnable*> toCancel;
  79. jobsMutex.lock();
  80. for (auto&[tup, job] : jobs) {
  81. auto& [level, i, j] = tup;
  82. if(level != currentLevel) {
  83. toCancel.push_back(job);
  84. }
  85. }
  86. jobsMutex.unlock();
  87. for (auto* job : toCancel) {
  88. if (threadPool->tryTake(job)) {
  89. delete job;
  90. }
  91. }
  92. }
  93. }
  94. void Calcer::notFinished(int level, GridIndex i, GridIndex j)
  95. {
  96. jobsMutex.lock();
  97. jobs.erase({ level, i, j });
  98. jobsMutex.unlock();
  99. }
  100. void Calcer::jobFailed(int level, GridIndex i, GridIndex j)
  101. {
  102. jobsMutex.lock();
  103. jobs.erase({ level, i, j });
  104. jobsMutex.unlock();
  105. }
  106. void Calcer::redirect(int level, GridIndex i, GridIndex j, Bitmap<float>* bmp)
  107. {
  108. jobsMutex.lock();
  109. jobs.erase({ level, i, j });
  110. jobsMutex.unlock();
  111. if (this->calcState == calcState) { // TODO remove invalid results correctly
  112. emit done(level, i, j, bmp);
  113. }
  114. else {
  115. delete bmp;
  116. }
  117. }
  118. const int FractalZoomWidget::chunkSize = 256;
  119. FractalZoomWidget::FractalZoomWidget(QWidget* parent) :
  120. EscapeTimeVisualWidget{ parent },
  121. calcer{ *this }
  122. {
  123. qMetaTypeId<GridIndex>();
  124. connect(&calcer, &Calcer::done, this, &FractalZoomWidget::cellReady);
  125. setMaxIterations(250);
  126. }
  127. void FractalZoomWidget::setViewport(const mnd::MandelViewport& viewport)
  128. {
  129. mandelInfo.view = viewport;
  130. mandelInfo.view.adjustAspectRatio(getResolutionX(), getResolutionY());
  131. update();
  132. }
  133. const mnd::MandelViewport& FractalZoomWidget::getViewport(void) const
  134. {
  135. return mandelInfo.view;
  136. }
  137. int FractalZoomWidget::getLevel(const mnd::Real& dpp) const
  138. {
  139. return int(mnd::log2(dpp / chunkSize));
  140. }
  141. mnd::Real FractalZoomWidget::getDpp(int level) const
  142. {
  143. return mnd::pow(mnd::Real(2), mnd::Real(level)) * chunkSize;
  144. }
  145. SliceGrid& FractalZoomWidget::getGrid(int level)
  146. {
  147. auto it = levels.find(level);
  148. if (it != levels.end()) {
  149. return it->second;
  150. }
  151. else {
  152. levels.insert(std::pair<int, SliceGrid>{ level, SliceGrid{ *this, level } });
  153. return levels.at(level);
  154. }
  155. }
  156. void FractalZoomWidget::clearCells(void)
  157. {
  158. for(auto& [level, grid] : this->levels) {
  159. grid.clearCells();
  160. }
  161. }
  162. void FractalZoomWidget::garbageCollect(int level, const GridIndex& /*i*/, const GridIndex& /*j*/)
  163. {
  164. for(auto& [l, grid] : levels) {
  165. int dist = ::abs(l - level);
  166. if (dist == 1) {
  167. grid.clearUncleanCells();
  168. }
  169. if (dist > 20) {
  170. grid.clearCells();
  171. }
  172. else if (dist > 10) {
  173. if (grid.countAllocatedCells() > 50)
  174. grid.clearCells();
  175. }
  176. else if (dist > 3) {
  177. if (grid.countAllocatedCells() > 150)
  178. grid.clearCells();
  179. }
  180. else if (dist > 0) {
  181. if (grid.countAllocatedCells() > 350)
  182. grid.clearCells();
  183. }
  184. else {
  185. if (grid.countAllocatedCells() > 2500)
  186. grid.clearCells();
  187. }
  188. }
  189. }
  190. GridElement* FractalZoomWidget::searchAbove(int level,
  191. const GridIndex &i, const GridIndex &j, int recursionLevel)
  192. {
  193. auto& grid = getGrid(level);
  194. auto& gridAbove = getGrid(level + 1);
  195. GridIndex ai = (i < 0 ? (i - 1) : i) / 2;
  196. GridIndex aj = (j < 0 ? (j - 1) : j) / 2;
  197. GridElement* above = gridAbove.getCell(ai, aj);
  198. if (above == nullptr && recursionLevel > 0) {
  199. auto abFound = searchAbove(level + 1, ai, aj, recursionLevel - 1);
  200. if (abFound)
  201. above = abFound;
  202. }
  203. if (above != nullptr) {
  204. auto newElement = std::make_unique<GridElement>(
  205. false, above->img->clip(short(i & 1), short(j & 1))
  206. );
  207. GridElement* ret = newElement.get();
  208. grid.setCell(i, j, std::move(newElement));
  209. return ret;
  210. }
  211. else {
  212. return nullptr;
  213. }
  214. }
  215. GridElement* FractalZoomWidget::searchUnder(int level,
  216. const GridIndex &i, const GridIndex &j, int recursionLevel)
  217. {
  218. if (recursionLevel == 0)
  219. return nullptr;
  220. auto& grid = getGrid(level);
  221. auto& gridUnder = getGrid(level - 1);
  222. GridIndex ai = i * 2;
  223. GridIndex aj = j * 2;
  224. GridElement* u00 = gridUnder.getCell(ai, aj);
  225. GridElement* u01 = gridUnder.getCell(ai, aj + 1);
  226. GridElement* u10 = gridUnder.getCell(ai + 1, aj);
  227. GridElement* u11 = gridUnder.getCell(ai + 1, aj + 1);
  228. /*if ( u00 == nullptr
  229. || u01 == nullptr
  230. || u10 == nullptr
  231. || u11 == nullptr) {
  232. auto abFound = searchUnder(level + 1, ai, aj, recursionLevel - 1);
  233. if (abFound)
  234. above = abFound;
  235. }*/
  236. if ( u00 != nullptr
  237. && u01 != nullptr
  238. && u10 != nullptr
  239. && u11 != nullptr) {
  240. auto newElement = std::make_unique<GridElement>(
  241. false, std::make_shared<QuadImage>(u00->img, u01->img, u10->img, u11->img)
  242. );
  243. GridElement* ret = newElement.get();
  244. grid.setCell(i, j, std::move(newElement));
  245. return ret;
  246. }
  247. else {
  248. return nullptr;
  249. }
  250. }
  251. const mnd::MandelInfo& FractalZoomWidget::getMandelInfo(void) const
  252. {
  253. return mandelInfo;
  254. }
  255. void FractalZoomWidget::setGenerator(mnd::MandelGenerator* gen)
  256. {
  257. bool changed = true;
  258. if (this->generator == gen)
  259. changed = false;
  260. this->generator = gen;
  261. if (changed) {
  262. clearCells();
  263. update();
  264. }
  265. }
  266. mnd::MandelGenerator* FractalZoomWidget::getGenerator(void) const
  267. {
  268. return generator;
  269. }
  270. void FractalZoomWidget::zoom(float factor)
  271. {
  272. mandelInfo.view.zoomCenter(factor);
  273. update();
  274. }
  275. void FractalZoomWidget::setSmoothColoring(bool smooth)
  276. {
  277. if (mandelInfo.smooth != smooth) {
  278. mandelInfo.smooth = smooth;
  279. clearCells();
  280. update();
  281. }
  282. }
  283. void FractalZoomWidget::setMaxIterations(int maxIterations)
  284. {
  285. if (mandelInfo.maxIter != maxIterations) {
  286. mandelInfo.maxIter = maxIterations;
  287. EscapeTimeVisualWidget::setMaxIterationCutoff(float(maxIterations));
  288. clearCells();
  289. update();
  290. }
  291. }
  292. void FractalZoomWidget::initializeGL(void)
  293. {
  294. EscapeTimeVisualWidget::initializeGL();
  295. Bitmap<float> empty{ 1, 1 };
  296. empty.get(0, 0) = 0.0f;
  297. emptyImage = new ETVImage(*this, empty);
  298. }
  299. void FractalZoomWidget::resizeGL(int w, int h)
  300. {
  301. EscapeTimeVisualWidget::resizeGL(w, h);
  302. mandelInfo.view.adjustAspectRatio(w, h);
  303. }
  304. void FractalZoomWidget::paintGL(void)
  305. {
  306. EscapeTimeVisualWidget::paintGL();
  307. /*ETVImage etvi{ *this };
  308. auto& gl = *this->context()->functions();
  309. gl.glClearColor(0.0, 0.2, 0.0, 1.0);
  310. gl.glClear(GL_COLOR_BUFFER_BIT);
  311. etvi.draw(100, 100, 700, 700);*/
  312. ////////////////////
  313. int width = getResolutionX();
  314. int height = getResolutionY();
  315. auto& mvp = mandelInfo.view;
  316. mnd::Real dpp = mvp.width / width;
  317. int level = getLevel(dpp) - 1;
  318. auto& grid = getGrid(level);
  319. mnd::Real gw = getDpp(level) * chunkSize;
  320. auto [left, top] = grid.getCellIndices(mvp.x, mvp.y);
  321. auto [right, bottom] = grid.getCellIndices(mvp.right(), mvp.bottom());
  322. garbageCollect(level, (left + right) / 2, (top + bottom) / 2);
  323. emit calcer.setCurrentLevel(level);
  324. mnd::Real w = width * gw / mvp.width;
  325. auto [realXLeft, realYTop] = grid.getPositions(left, top);
  326. realXLeft = ((realXLeft - mvp.x) * mnd::Real(width)) / mvp.width;
  327. realYTop = ((realYTop - mvp.y) * mnd::Real(height)) / mvp.height;
  328. for(GridIndex i = left; i <= right; i++) {
  329. for(GridIndex j = top; j <= bottom; j++) {
  330. mnd::Real x = w * int(i - left) + realXLeft;
  331. mnd::Real y = w * int(j - top) + realYTop;
  332. GridElement* t = grid.getCell(i, j);
  333. if (t == nullptr) {
  334. auto under = searchUnder(level, i, j, 1);
  335. if (under) {
  336. t = under;
  337. }
  338. else {
  339. auto above = searchAbove(level, i, j, 3);
  340. if (above) {
  341. t = above;
  342. }
  343. }
  344. }
  345. if (t != nullptr) {
  346. t->img->drawRect(float(x), float(y), float(w), float(w));
  347. /*glBegin(GL_LINE_LOOP);
  348. glVertex2f(float(x), float(y));
  349. glVertex2f(float(x) + float(w), float(y));
  350. glVertex2f(float(x) + float(w), float(y) + float(w));
  351. glVertex2f(float(x), float(y) + float(w));
  352. glEnd();*/
  353. if (!t->enoughResolution) {
  354. calcer.calc(grid, level, i, j, t->img->getRecalcPriority());
  355. }
  356. }
  357. else {
  358. calcer.calc(grid, level, i, j, 1000);
  359. if (emptyImage)
  360. emptyImage->draw(float(x), float(y), float(w), float(w));
  361. }
  362. }
  363. }
  364. }
  365. void FractalZoomWidget::cellReady(int level, GridIndex i, GridIndex j, Bitmap<float>* bmp)
  366. {
  367. this->getGrid(level).setCell(i, j,
  368. std::make_unique<GridElement>(true, std::make_shared<ImageClip>(std::make_shared<ETVImage>(*this, *bmp))));
  369. delete bmp;
  370. update();
  371. }