123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 |
- #include "FractalZoomWidget.h"
- #include <QOpenGLContext>
- #include <QOpenGLFunctions>
- SliceGrid::SliceGrid(FractalZoomWidget& owner, int level) :
- owner{ owner },
- level{ level },
- dpp{ owner.getDpp(level) }
- {
- }
- std::pair<GridIndex, GridIndex> SliceGrid::getCellIndices(mnd::Real x, mnd::Real y)
- {
- return {
- GridIndex(mnd::floor(x / dpp / FractalZoomWidget::chunkSize)),
- GridIndex(mnd::floor(y / dpp / FractalZoomWidget::chunkSize))
- };
- }
- std::pair<mnd::Real, mnd::Real> SliceGrid::getPositions(GridIndex x, GridIndex y)
- {
- return {
- mnd::Real(x) * dpp * FractalZoomWidget::chunkSize,
- mnd::Real(y) * dpp * FractalZoomWidget::chunkSize
- };
- }
- GridElement* SliceGrid::getCell(GridIndex i, GridIndex j)
- {
- auto cIt = cells.find({i, j});
- if (cIt != cells.end()) {
- return cIt->second.get();
- }
- else {
- return nullptr;
- }
- }
- void SliceGrid::setCell(GridIndex i, GridIndex j, std::unique_ptr<GridElement> tex)
- {
- cells[{i, j}] = std::move(tex);
- }
- void SliceGrid::clearCells(void)
- {
- cells.clear();
- }
- void SliceGrid::clearUncleanCells(void)
- {
- for (auto it = cells.begin(); it != cells.end();) {
- if (it->second->img->getRecalcPriority() > 1)
- cells.erase(it++);
- else ++it;
- }
- }
- Calcer::Calcer(FractalZoomWidget& owner) :
- jobsMutex{ QMutex::Recursive },
- threadPool{ new QThreadPool(this) },
- owner{ owner }
- {
- threadPool->setMaxThreadCount(1);
- }
- void Calcer::clearAll(void)
- {
- this->threadPool->clear();
- }
- void Calcer::calc(SliceGrid& grid, int level, GridIndex i, GridIndex j, int priority)
- {
- jobsMutex.lock();
- if (jobs.find({ level, i, j }) == jobs.end()) {
- CalcJob* job = new CalcJob(owner, owner.getGenerator(), &grid, level, i, j, calcState);
- connect(job, &CalcJob::done, this, &Calcer::redirect);
- connect(job, &CalcJob::failed, this, &Calcer::jobFailed);
- connect(job, &QObject::destroyed, this, [this, level, i, j] () { this->notFinished(level, i, j); });
- jobs.emplace(std::tuple{ level, i, j }, job);
- threadPool->start(job, priority);
- }
- jobsMutex.unlock();
- }
- void Calcer::setCurrentLevel(int level)
- {
- if (this->currentLevel != level) {
- this->currentLevel = level;
- std::vector<QRunnable*> toCancel;
- jobsMutex.lock();
- for (auto&[tup, job] : jobs) {
- auto& [level, i, j] = tup;
- if(level != currentLevel) {
- toCancel.push_back(job);
- }
- }
- jobsMutex.unlock();
- for (auto* job : toCancel) {
- if (threadPool->tryTake(job)) {
- delete job;
- }
- }
- }
- }
- void Calcer::notFinished(int level, GridIndex i, GridIndex j)
- {
- jobsMutex.lock();
- jobs.erase({ level, i, j });
- jobsMutex.unlock();
- }
- void Calcer::jobFailed(int level, GridIndex i, GridIndex j)
- {
- jobsMutex.lock();
- jobs.erase({ level, i, j });
- jobsMutex.unlock();
- }
- void Calcer::redirect(int level, GridIndex i, GridIndex j, Bitmap<float>* bmp)
- {
- jobsMutex.lock();
- jobs.erase({ level, i, j });
- jobsMutex.unlock();
- if (this->calcState == calcState) {
- emit done(level, i, j, bmp);
- }
- else {
- delete bmp;
- }
- }
- const int FractalZoomWidget::chunkSize = 256;
- FractalZoomWidget::FractalZoomWidget(QWidget* parent) :
- EscapeTimeVisualWidget{ parent },
- calcer{ *this }
- {
- qMetaTypeId<GridIndex>();
- connect(&calcer, &Calcer::done, this, &FractalZoomWidget::cellReady);
- mandelInfo.maxIter = 250;
- }
- void FractalZoomWidget::setViewport(const mnd::MandelViewport& viewport)
- {
- mandelInfo.view = viewport;
- mandelInfo.view.adjustAspectRatio(getResolutionX(), getResolutionY());
- update();
- }
- const mnd::MandelViewport& FractalZoomWidget::getViewport(void) const
- {
- return mandelInfo.view;
- }
- int FractalZoomWidget::getLevel(const mnd::Real& dpp) const
- {
- return int(mnd::log2(dpp / chunkSize));
- }
- mnd::Real FractalZoomWidget::getDpp(int level) const
- {
- return mnd::pow(mnd::Real(2), mnd::Real(level)) * chunkSize;
- }
- SliceGrid& FractalZoomWidget::getGrid(int level)
- {
- auto it = levels.find(level);
- if (it != levels.end()) {
- return it->second;
- }
- else {
- levels.insert(std::pair<int, SliceGrid>{ level, SliceGrid{ *this, level } });
- return levels.at(level);
- }
- }
- void FractalZoomWidget::clearCells(void)
- {
- for(auto& [level, grid] : this->levels) {
- grid.clearCells();
- }
- }
- void FractalZoomWidget::garbageCollect(int level, const GridIndex& /*i*/, const GridIndex& /*j*/)
- {
- for(auto& [l, grid] : levels) {
- int dist = ::abs(l - level);
- if (dist == 1) {
- grid.clearUncleanCells();
- }
- if (dist > 20) {
- grid.clearCells();
- }
- else if (dist > 10) {
- if (grid.countAllocatedCells() > 50)
- grid.clearCells();
- }
- else if (dist > 3) {
- if (grid.countAllocatedCells() > 150)
- grid.clearCells();
- }
- else if (dist > 0) {
- if (grid.countAllocatedCells() > 350)
- grid.clearCells();
- }
- else {
- if (grid.countAllocatedCells() > 2500)
- grid.clearCells();
- }
- }
- }
- GridElement* FractalZoomWidget::searchAbove(int level,
- const GridIndex &i, const GridIndex &j, int recursionLevel)
- {
- auto& grid = getGrid(level);
- auto& gridAbove = getGrid(level + 1);
- GridIndex ai = (i < 0 ? (i - 1) : i) / 2;
- GridIndex aj = (j < 0 ? (j - 1) : j) / 2;
- GridElement* above = gridAbove.getCell(ai, aj);
- if (above == nullptr && recursionLevel > 0) {
- auto abFound = searchAbove(level + 1, ai, aj, recursionLevel - 1);
- if (abFound)
- above = abFound;
- }
- if (above != nullptr) {
- auto newElement = std::make_unique<GridElement>(
- false, above->img->clip(short(i & 1), short(j & 1))
- );
- GridElement* ret = newElement.get();
- grid.setCell(i, j, std::move(newElement));
- return ret;
- }
- else {
- return nullptr;
- }
- }
- GridElement* FractalZoomWidget::searchUnder(int level,
- const GridIndex &i, const GridIndex &j, int recursionLevel)
- {
- if (recursionLevel == 0)
- return nullptr;
- auto& grid = getGrid(level);
- auto& gridUnder = getGrid(level - 1);
- GridIndex ai = i * 2;
- GridIndex aj = j * 2;
- GridElement* u00 = gridUnder.getCell(ai, aj);
- GridElement* u01 = gridUnder.getCell(ai, aj + 1);
- GridElement* u10 = gridUnder.getCell(ai + 1, aj);
- GridElement* u11 = gridUnder.getCell(ai + 1, aj + 1);
- /*if ( u00 == nullptr
- || u01 == nullptr
- || u10 == nullptr
- || u11 == nullptr) {
- auto abFound = searchUnder(level + 1, ai, aj, recursionLevel - 1);
- if (abFound)
- above = abFound;
- }*/
- if ( u00 != nullptr
- && u01 != nullptr
- && u10 != nullptr
- && u11 != nullptr) {
- auto newElement = std::make_unique<GridElement>(
- false, std::make_shared<QuadImage>(u00->img, u01->img, u10->img, u11->img)
- );
- GridElement* ret = newElement.get();
- grid.setCell(i, j, std::move(newElement));
- return ret;
- }
- else {
- return nullptr;
- }
- }
- const mnd::MandelInfo& FractalZoomWidget::getMandelInfo(void) const
- {
- return mandelInfo;
- }
- void FractalZoomWidget::setGenerator(mnd::MandelGenerator* gen)
- {
- bool changed = true;
- if (this->generator == gen)
- changed = false;
- this->generator = gen;
- if (changed) {
- clearCells();
- update();
- }
- }
- mnd::MandelGenerator* FractalZoomWidget::getGenerator(void) const
- {
- return generator;
- }
- void FractalZoomWidget::zoom(float factor)
- {
- mandelInfo.view.zoomCenter(factor);
- update();
- }
- void FractalZoomWidget::initializeGL(void)
- {
- EscapeTimeVisualWidget::initializeGL();
- Bitmap<float> empty{ 1, 1 };
- empty.get(0, 0) = 0.0f;
- emptyImage = new ETVImage(*this, empty);
- }
- void FractalZoomWidget::resizeGL(int w, int h)
- {
- EscapeTimeVisualWidget::resizeGL(w, h);
- mandelInfo.view.adjustAspectRatio(w, h);
- }
- void FractalZoomWidget::paintGL(void)
- {
- EscapeTimeVisualWidget::paintGL();
- /*ETVImage etvi{ *this };
- auto& gl = *this->context()->functions();
- gl.glClearColor(0.0, 0.2, 0.0, 1.0);
- gl.glClear(GL_COLOR_BUFFER_BIT);
- etvi.draw(100, 100, 700, 700);*/
- ////////////////////
- int width = getResolutionX();
- int height = getResolutionY();
- auto& mvp = mandelInfo.view;
- mnd::Real dpp = mvp.width / width;
- int level = getLevel(dpp) - 1;
- auto& grid = getGrid(level);
- mnd::Real gw = getDpp(level) * chunkSize;
- auto [left, top] = grid.getCellIndices(mvp.x, mvp.y);
- auto [right, bottom] = grid.getCellIndices(mvp.right(), mvp.bottom());
- garbageCollect(level, (left + right) / 2, (top + bottom) / 2);
- emit calcer.setCurrentLevel(level);
- mnd::Real w = width * gw / mvp.width;
- auto [realXLeft, realYTop] = grid.getPositions(left, top);
- realXLeft = ((realXLeft - mvp.x) * mnd::Real(width)) / mvp.width;
- realYTop = ((realYTop - mvp.y) * mnd::Real(height)) / mvp.height;
- for(GridIndex i = left; i <= right; i++) {
- for(GridIndex j = top; j <= bottom; j++) {
- mnd::Real x = w * int(i - left) + realXLeft;
- mnd::Real y = w * int(j - top) + realYTop;
- GridElement* t = grid.getCell(i, j);
- if (t == nullptr) {
- auto under = searchUnder(level, i, j, 1);
- if (under) {
- t = under;
- }
- else {
- auto above = searchAbove(level, i, j, 3);
- if (above) {
- t = above;
- }
- }
- }
- if (t != nullptr) {
- t->img->drawRect(float(x), float(y), float(w), float(w));
- /*glBegin(GL_LINE_LOOP);
- glVertex2f(float(x), float(y));
- glVertex2f(float(x) + float(w), float(y));
- glVertex2f(float(x) + float(w), float(y) + float(w));
- glVertex2f(float(x), float(y) + float(w));
- glEnd();*/
- if (!t->enoughResolution) {
- calcer.calc(grid, level, i, j, t->img->getRecalcPriority());
- }
- }
- else {
- calcer.calc(grid, level, i, j, 1000);
- if (emptyImage)
- emptyImage->draw(float(x), float(y), float(w), float(w));
- }
- }
- }
- }
- void FractalZoomWidget::cellReady(int level, GridIndex i, GridIndex j, Bitmap<float>* bmp)
- {
- this->getGrid(level).setCell(i, j,
- std::make_unique<GridElement>(true, std::make_shared<ImageClip>(std::make_shared<ETVImage>(*this, *bmp))));
- delete bmp;
- update();
- }
|