FractalZoomWidget.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. int exponent = (dpp / chunkSize).backend().exponent();
  140. // replace log2 with cheaper operation
  141. //return int(mnd::log2(dpp / chunkSize));
  142. return exponent + 1;
  143. }
  144. mnd::Real FractalZoomWidget::getDpp(int level) const
  145. {
  146. mnd::Real a = 1;
  147. a.backend().exponent() += level;
  148. return a * chunkSize;
  149. // return mnd::pow(mnd::Real(2), mnd::Real(level)) * chunkSize;
  150. }
  151. SliceGrid& FractalZoomWidget::getGrid(int level)
  152. {
  153. auto it = levels.find(level);
  154. if (it != levels.end()) {
  155. return it->second;
  156. }
  157. else {
  158. levels.insert(std::pair<int, SliceGrid>{ level, SliceGrid{ *this, level } });
  159. return levels.at(level);
  160. }
  161. }
  162. void FractalZoomWidget::clearCells(void)
  163. {
  164. for(auto& [level, grid] : this->levels) {
  165. grid.clearCells();
  166. }
  167. calcer.changeState();
  168. calcer.clearAll();
  169. update();
  170. }
  171. void FractalZoomWidget::garbageCollect(int level, const GridIndex& /*i*/, const GridIndex& /*j*/)
  172. {
  173. for(auto& [l, grid] : levels) {
  174. int dist = ::abs(l - level);
  175. if (dist == 1) {
  176. grid.clearUncleanCells();
  177. }
  178. if (dist > 20) {
  179. grid.clearCells();
  180. }
  181. else if (dist > 10) {
  182. if (grid.countAllocatedCells() > 50)
  183. grid.clearCells();
  184. }
  185. else if (dist > 3) {
  186. if (grid.countAllocatedCells() > 150)
  187. grid.clearCells();
  188. }
  189. else if (dist > 0) {
  190. if (grid.countAllocatedCells() > 350)
  191. grid.clearCells();
  192. }
  193. else {
  194. if (grid.countAllocatedCells() > 2500)
  195. grid.clearCells();
  196. }
  197. }
  198. }
  199. GridElement* FractalZoomWidget::searchAbove(int level,
  200. const GridIndex &i, const GridIndex &j, int recursionLevel)
  201. {
  202. auto& grid = getGrid(level);
  203. auto& gridAbove = getGrid(level + 1);
  204. GridIndex ai = (i < 0 ? (i - 1) : i) / 2;
  205. GridIndex aj = (j < 0 ? (j - 1) : j) / 2;
  206. GridElement* above = gridAbove.getCell(ai, aj);
  207. if (above == nullptr && recursionLevel > 0) {
  208. auto abFound = searchAbove(level + 1, ai, aj, recursionLevel - 1);
  209. if (abFound)
  210. above = abFound;
  211. }
  212. if (above != nullptr) {
  213. auto newElement = std::make_unique<GridElement>(
  214. false, above->img->clip(short(i & 1), short(j & 1))
  215. );
  216. GridElement* ret = newElement.get();
  217. grid.setCell(i, j, std::move(newElement));
  218. return ret;
  219. }
  220. else {
  221. return nullptr;
  222. }
  223. }
  224. GridElement* FractalZoomWidget::searchUnder(int level,
  225. const GridIndex &i, const GridIndex &j, int recursionLevel)
  226. {
  227. if (recursionLevel == 0)
  228. return nullptr;
  229. auto& grid = getGrid(level);
  230. auto& gridUnder = getGrid(level - 1);
  231. GridIndex ai = i * 2;
  232. GridIndex aj = j * 2;
  233. GridElement* u00 = gridUnder.getCell(ai, aj);
  234. GridElement* u01 = gridUnder.getCell(ai, aj + 1);
  235. GridElement* u10 = gridUnder.getCell(ai + 1, aj);
  236. GridElement* u11 = gridUnder.getCell(ai + 1, aj + 1);
  237. /*if ( u00 == nullptr
  238. || u01 == nullptr
  239. || u10 == nullptr
  240. || u11 == nullptr) {
  241. auto abFound = searchUnder(level + 1, ai, aj, recursionLevel - 1);
  242. if (abFound)
  243. above = abFound;
  244. }*/
  245. if ( u00 != nullptr
  246. && u01 != nullptr
  247. && u10 != nullptr
  248. && u11 != nullptr) {
  249. auto newElement = std::make_unique<GridElement>(
  250. false, std::make_shared<QuadImage>(u00->img, u01->img, u10->img, u11->img)
  251. );
  252. GridElement* ret = newElement.get();
  253. grid.setCell(i, j, std::move(newElement));
  254. return ret;
  255. }
  256. else {
  257. return nullptr;
  258. }
  259. }
  260. const mnd::MandelInfo& FractalZoomWidget::getMandelInfo(void) const
  261. {
  262. return mandelInfo;
  263. }
  264. mnd::MandelInfo& FractalZoomWidget::getMandelInfo(void)
  265. {
  266. return mandelInfo;
  267. }
  268. void FractalZoomWidget::setGenerator(mnd::MandelGenerator* gen)
  269. {
  270. bool changed = true;
  271. if (this->generator == gen)
  272. changed = false;
  273. this->generator = gen;
  274. if (changed) {
  275. clearCells();
  276. update();
  277. }
  278. }
  279. mnd::MandelGenerator* FractalZoomWidget::getGenerator(void) const
  280. {
  281. return generator;
  282. }
  283. void FractalZoomWidget::zoom(float factor)
  284. {
  285. mandelInfo.view.zoomCenter(factor);
  286. update();
  287. }
  288. void FractalZoomWidget::setSmoothColoring(bool smooth)
  289. {
  290. if (mandelInfo.smooth != smooth) {
  291. mandelInfo.smooth = smooth;
  292. clearCells();
  293. update();
  294. }
  295. }
  296. void FractalZoomWidget::setMaxIterations(int maxIterations)
  297. {
  298. if (mandelInfo.maxIter != maxIterations) {
  299. mandelInfo.maxIter = maxIterations;
  300. EscapeTimeVisualWidget::setMaxIterationCutoff(float(maxIterations));
  301. clearCells();
  302. update();
  303. }
  304. }
  305. void FractalZoomWidget::initializeGL(void)
  306. {
  307. EscapeTimeVisualWidget::initializeGL();
  308. Bitmap<float> empty{ 1, 1 };
  309. empty.get(0, 0) = 0.0f;
  310. emptyImage = new ETVImage(*this, empty);
  311. }
  312. void FractalZoomWidget::resizeGL(int w, int h)
  313. {
  314. EscapeTimeVisualWidget::resizeGL(w, h);
  315. mandelInfo.view.height = mandelInfo.view.width * h / w;
  316. }
  317. void FractalZoomWidget::paintGL(void)
  318. {
  319. EscapeTimeVisualWidget::paintGL();
  320. /*ETVImage etvi{ *this };
  321. auto& gl = *this->context()->functions();
  322. gl.glClearColor(0.0, 0.2, 0.0, 1.0);
  323. gl.glClear(GL_COLOR_BUFFER_BIT);
  324. etvi.draw(100, 100, 700, 700);*/
  325. ////////////////////
  326. int width = getResolutionX();
  327. int height = getResolutionY();
  328. auto& mvp = mandelInfo.view;
  329. mnd::Real dpp = mvp.width / width;
  330. int level = getLevel(dpp) - 1;
  331. auto& grid = getGrid(level);
  332. mnd::Real gw = getDpp(level) * chunkSize;
  333. auto [left, top] = grid.getCellIndices(mvp.x, mvp.y);
  334. auto [right, bottom] = grid.getCellIndices(mvp.right(), mvp.bottom());
  335. garbageCollect(level, (left + right) / 2, (top + bottom) / 2);
  336. emit calcer.setCurrentLevel(level);
  337. mnd::Real w = width * gw / mvp.width;
  338. auto [realXLeft, realYTop] = grid.getPositions(left, top);
  339. realXLeft = ((realXLeft - mvp.x) * mnd::Real(width)) / mvp.width;
  340. realYTop = ((realYTop - mvp.y) * mnd::Real(height)) / mvp.height;
  341. for(GridIndex i = left; i <= right; i++) {
  342. for(GridIndex j = top; j <= bottom; j++) {
  343. mnd::Real x = w * int(i - left) + realXLeft;
  344. mnd::Real y = w * int(j - top) + realYTop;
  345. GridElement* t = grid.getCell(i, j);
  346. if (t == nullptr) {
  347. auto under = searchUnder(level, i, j, 1);
  348. if (under) {
  349. t = under;
  350. }
  351. else {
  352. auto above = searchAbove(level, i, j, 3);
  353. if (above) {
  354. t = above;
  355. }
  356. }
  357. }
  358. if (t != nullptr) {
  359. if (!t->enoughResolution) {
  360. auto under = searchUnder(level, i, j, 1);
  361. if (under) {
  362. t = under;
  363. }
  364. calcer.calc(grid, level, i, j, t->img->getRecalcPriority());
  365. }
  366. t->img->drawRect(float(x), float(y), float(w), float(w));
  367. }
  368. else {
  369. calcer.calc(grid, level, i, j, 1000);
  370. if (emptyImage)
  371. emptyImage->draw(float(x), float(y), float(w), float(w));
  372. }
  373. }
  374. }
  375. }
  376. void FractalZoomWidget::cellReady(int level, GridIndex i, GridIndex j, Bitmap<float>* bmp)
  377. {
  378. this->getGrid(level).setCell(i, j,
  379. std::make_unique<GridElement>(true, std::make_shared<ImageClip>(std::make_shared<ETVImage>(*this, *bmp))));
  380. delete bmp;
  381. update();
  382. }