From 6b75b7d61a5e3d8fba9ba47cc4196075ee0e19af Mon Sep 17 00:00:00 2001 From: koma Date: Tue, 5 Jul 2022 19:06:45 +0200 Subject: [PATCH] too much changes to describe --- Makefile | 4 +- graphics.cpp | 141 ++++++++++++++++++++++++++- graphics.h | 7 +- main.cpp | 5 + utils.cpp | 69 +++++++++---- utils.h | 271 +++++++++++++++++++++++++++------------------------ 6 files changed, 343 insertions(+), 154 deletions(-) diff --git a/Makefile b/Makefile index 99ed02a..691f716 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,4 @@ all: - g++ graphics.cpp main.cpp mandelbrot.cpp utils.cpp -o mandelbrot -lSDL2 -lmpfr + g++ -O3 graphics.cpp main.cpp mandelbrot.cpp utils.cpp -o mandelbrot -lSDL2 -lmpfr -lgmp -fopenmp -pthread +debug: + g++ graphics.cpp main.cpp mandelbrot.cpp utils.cpp -o mandelbrot -lSDL2 -lmpfr -lgmp -fopenmp -g -pthread diff --git a/graphics.cpp b/graphics.cpp index 3781352..6982408 100644 --- a/graphics.cpp +++ b/graphics.cpp @@ -1,12 +1,16 @@ #include "graphics.h" #include "utils.h" #include +#include #include #include +#include using namespace Mandelbrot; +using namespace mpfr; -Graphics::Graphics(int w, int h, std::string title) : w(w), h(h), title(title) +Graphics::Graphics(int w, int h, std::string title) : w(w), h(h), title(title), + from(Vec2mp(0.0, 0.0)), to(Vec2mp(0.0, 0.0)) { SDL_Window *window = SDL_CreateWindow(this->title.c_str(), SDL_WINDOWPOS_CENTERED, @@ -32,6 +36,7 @@ Graphics::Graphics(int w, int h, std::string title) : w(w), h(h), title(title) } this->renderer = r; + this->running = true; } void Graphics::drawPoint(const Vec2i &pos, const rgb &col) @@ -39,11 +44,137 @@ void Graphics::drawPoint(const Vec2i &pos, const rgb &col) SDL_RenderDrawPoint(this->renderer, pos.x, pos.y); } -void Graphics::plot(const Vec2bf &from, const Vec2bf &to) +void Graphics::plot(Mandelbrotc const &m) { - for (double i = 0.0; i < this->w; i++) { - for (double j = 0.0; j < this->h; j++) { - BigFloat f = (from.x - to.x) * from.x + (i / this->w); + // std::vector screen = std::vector(); + // std::vector screen1 = std::vector(); + // #pragma omp parallel sections + // { + // #pragma omp section + // { + // for (int i = 0; i < this->w/2; i+=2) { + // for (int j = 0; j < this->h/2; j+=2) { + // mpreal x = (mpfr::abs(from.x - to.x) * (i / this->w)) + from.x; + // mpreal y = (mpfr::abs(from.y - to.y) * (j / this->h)) + from.y; + // // std::cout << "px: " << x.to_str() << std::endl; + // // std::cout << "py: " << y.to_str() << std::endl; + // uint32_t max_iter = 50; + // uint32_t iter = mandelbrot(Vec2mp(x, y), max_iter); + // std::cout << "iter: " << iter << std::endl; + + // uint8_t color = 255 - (int)(iter*255/max_iter); + // screen.push_back(color); + // SDL_SetRenderDrawColor(this->renderer, color, color, color, 255); + // SDL_RenderDrawPoint(renderer, (int)i, (int)j); + // } + // } + // } + // #pragma omp section + // { + // for (int i = 1; i < this->w/2; i+=2) { + // for (int j = 1; j < this->h/2; j+=2) { + // mpreal x = (mpfr::abs(from.x - to.x) * (i / this->w)) + from.x; + // mpreal y = (mpfr::abs(from.y - to.y) * (j / this->h)) + from.y; + // // std::cout << "px: " << x.to_str() << std::endl; + // // std::cout << "py: " << y.to_str() << std::endl; + // uint32_t max_iter = 50; + // uint32_t iter = mandelbrot(Vec2mp(x, y), max_iter); + // std::cout << "iter: " << iter << std::endl; + + // uint8_t color = 255 - (int)(iter*255/max_iter); + // screen1.push_back(color); + // SDL_SetRenderDrawColor(this->renderer, color, color, color, 255); + // SDL_RenderDrawPoint(renderer, (int)i, (int)j); + // } + // } + // } + // } + // for (double i = 1.0; i < this->w/2; i+=2) { + // for (double j = 1.0; j < this->h/2; j+=2) { + // uint8_t color = screen[i*h/2+j]; + // SDL_SetRenderDrawColor(this->renderer, color, color, color, 255); + // SDL_RenderDrawPoint(renderer, (int)i, (int)j); + // } + // } + // for (double i = 0.0; i < this->w; i++) { + // for (double j = 0.0; j < this->h; j++) { + // mpreal x = (mpfr::abs(from.x - to.x) * (i / this->w)) + from.x; + // mpreal y = (mpfr::abs(from.y - to.y) * (j / this->h)) + from.y; + // uint32_t max_iter = 50; + // uint32_t iter = mandelbrot(Vec2mp(x, y), max_iter); + // //std::cout << "iter: " << iter << std::endl; + + // uint8_t color = 255 - (iter*255/max_iter); + // SDL_SetRenderDrawColor(this->renderer, color, color, color, 255); + // SDL_RenderDrawPoint(renderer, (int)i, (int)j); + // } + // } + + for (uint32_t i = 0; i < m.screen.size(); i+=2) { + for (uint32_t j = 0; j < m.screen[0].size(); j+=2) { + uint8_t color = 255 - (m.screen[i][j]*255/m.max_iter); + SDL_SetRenderDrawColor(this->renderer, color, color, color, 255); + SDL_RenderDrawPoint(renderer, (int)i, (int)j); } } } + +void Graphics::mainLoop() +{ + mpreal fx = mpreal(-2); + mpreal fy = mpreal(-1); + mpreal tx = mpreal(1); + mpreal ty = mpreal(1); + this->from = Vec2mp(fx, fy); + this->to = Vec2mp(tx, ty); + + + Mandelbrotc m = Mandelbrotc(from, to, Vec2i(this->w, this->h), 100); + m.thread_count = 5; + m.start_threads(); + + while (this->running) + { + + SDL_Event event; + while (SDL_PollEvent(&event)) + { + switch (event.type) + { + case SDL_QUIT: + running = false; + break; + case SDL_MOUSEWHEEL: + if (event.wheel.y > 0) { + std::cout << "wheel" << std::endl; + this->from.x += 0.2; + this->to.x -= 0.2; + this->from.y += 0.2; + this->to.y -= 0.2; + } + if (event.wheel.y < 0) { + std::cout << "wheel" << std::endl; + this->from.x -= 0.2; + this->to.x += 0.2; + this->from.y -= 0.2; + this->to.y += 0.2; + } + break; + case SDL_MOUSEMOTION: + break; + } + } + + std::cout << "mainloop range " << this->from.x << std::endl; + std::cout << "mainloop range " << this->from.y << std::endl; + std::cout << "mainloop range " << this->to.x << std::endl; + std::cout << "mainloop range " << this->to.y << std::endl; + //this->plot(this->from, this->to); + + std::cout << "done" << std::endl; + + SDL_RenderPresent(renderer); + SDL_SetRenderDrawColor(this->renderer, 255, 255, 255, 255); + SDL_Delay(100); + } +} diff --git a/graphics.h b/graphics.h index 10d30bc..a6191a7 100644 --- a/graphics.h +++ b/graphics.h @@ -4,6 +4,7 @@ #include #include #include "utils.h" +#include "mandelbrot.h" using namespace Util; @@ -15,9 +16,13 @@ namespace Mandelbrot { int w, h; SDL_Window *window; SDL_Renderer *renderer; + Vec2mp from; + Vec2mp to; + bool running; Graphics(int w, int h, std::string title); - void plot(const Vec2bf &from, const Vec2bf &to); + void plot(Mandelbrotc const &m); + void mainLoop(); void drawPoint(const Vec2i &pos, const rgb &col); }; } // namespace Mandelbrot diff --git a/main.cpp b/main.cpp index 4c82edf..047f04c 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,11 @@ #include "graphics.h" +using namespace Mandelbrot; + int main() { + Graphics g = Graphics(1800, 1200, "Mandelbrot set visualizer"); + g.mainLoop(); + return 0; } diff --git a/utils.cpp b/utils.cpp index cfbb93d..5d67bfd 100644 --- a/utils.cpp +++ b/utils.cpp @@ -1,4 +1,5 @@ #include "utils.h" +#include "./mpreal.h" using namespace Util; @@ -19,32 +20,62 @@ Vec2d::Vec2d(double x, double y) : x(x), y(y) { } -BigFloat::BigFloat(uint32_t prec) +//BigFloat::BigFloat(uint32_t prec) : precision(prec) +//{ +// mpfr_init2(this->val, prec); +//} +// +//BigFloat::~BigFloat() +//{ +// //if (this->val->_mpfr_d != nullptr) +// // mpfr_clear(this->val); +//} +// +//BigFloat BigFloat_d(double d, uint32_t prec) +//{ +// BigFloat bf = BigFloat(prec); +// bf.setValue(d); +// +// return bf; +//} +// +//BigFloat BigFloat::abs(const BigFloat &bf) +//{ +// BigFloat r = BigFloat(bf.precision); +// +// mpfr_abs(r.val, bf.val, MPFR_RNDD); +// +// return r; +//} +//void BigFloat::setValue(double val) +//{ +// mpfr_set_d(this->val, val, MPFR_RNDD); +//} +// +//std::string BigFloat::to_str() const +//{ +// char buf[this->precision/2]; +// +// mpfr_snprintf(buf, this->precision/2, "%.60RNf", this->val); +// +// std::string s(buf); +// +// return s; +//} + +void init(uint32_t prec) { - mpfr_init2(this->val, prec); +// mpfr::mpreal::set_default_prec(prec); } -void BigFloat::setValue(double val) +Vec2mp::Vec2mp(mpfr::mpreal x, mpfr::mpreal y) + : x(x), y(y) { - mpfr_set_d(this->val, val, MPFR_RNDD); } -std::string BigFloat::to_str() +Vec2mp::Vec2mp(double x, double y) + : x(mpfr::mpreal(x)), y(mpfr::mpreal(y)) { - char buf[this->precision/2]; - - mpfr_snprintf(buf, this->precision/2, "%.60RNf", this->val); - - std::string s(buf); - - return s; -} - -Vec2bf::Vec2bf(double x, double y, uint32_t prec) - : x(BigFloat(prec)), y(BigFloat(prec)) -{ - this->x.setValue(x); - this->y.setValue(y); } /* BigFloat operator + (BigFloat const &f); diff --git a/utils.h b/utils.h index da39918..093c38d 100644 --- a/utils.h +++ b/utils.h @@ -1,8 +1,9 @@ #pragma once #include -#include +#include "./mpreal.h" #include +#include namespace Util { @@ -35,134 +36,148 @@ public: Vec2d(double x, double y); }; -class BigFloat { +//class BigFloat { +//public: +// uint32_t precision; +// mpfr_t val; +// +// BigFloat(uint32_t prec); +// ~BigFloat(); +// static BigFloat BigFloat_d(double d, uint32_t prec); +// static BigFloat abs(const BigFloat &bf); +// void setValue(double val); +// std::string to_str() const; +// BigFloat operator / (const BigFloat &f); +// +// inline BigFloat operator - (const BigFloat &f) const +// { +// BigFloat res = BigFloat(f.precision); +// mpfr_sub(res.val, this->val, f.val, MPFR_RNDD); +// +// return res; +// } +// +// inline BigFloat operator - (const double d) const +// { +// // BigFloat bd = BigFloat(this->precision); +// // bd.setValue(d); +// BigFloat res = BigFloat(this->precision); +// mpfr_sub_d(res.val, this->val, d, MPFR_RNDD); +// +// return res; +// } +// +// inline BigFloat operator * (const BigFloat &f) const +// { +// BigFloat res = BigFloat(f.precision); +// mpfr_mul(res.val, this->val, f.val, MPFR_RNDD); +// +// return res; +// } +// +// inline BigFloat operator * (const double d) const +// { +// // BigFloat bd = BigFloat(this->precision); +// // bd.setValue(d); +// BigFloat res = BigFloat(this->precision); +// mpfr_mul_d(res.val, this->val, d, MPFR_RNDD); +// +// return res; +// } +// +// inline BigFloat operator + (const BigFloat &f) const +// { +// BigFloat res = BigFloat(f.precision); +// mpfr_add(res.val, this->val, f.val, MPFR_RNDD); +// +// return res; +// } +// +// inline BigFloat operator + (const double d) const +// { +// // BigFloat bd = BigFloat(this->precision); +// // bd.setValue(d); +// BigFloat res = BigFloat(this->precision); +// mpfr_add_d(res.val, this->val, d, MPFR_RNDD); +// +// return res; +// } +// +// bool operator < (const BigFloat &f) +// { +// if (mpfr_cmp(this->val, f.val) < 0) +// return true; +// return false; +// } +// +// bool operator > (const BigFloat &f) +// { +// if (mpfr_cmp(this->val, f.val) > 0) +// return true; +// return false; +// } +// +// bool operator <= (const BigFloat &f) +// { +// int v = mpfr_cmp(this->val, f.val); +// +// if (v < 0 || v == 0) +// return true; +// return false; +// } +// +// bool operator >= (const BigFloat &f) +// { +// int v = mpfr_cmp(this->val, f.val); +// +// if (v > 0 || v == 0) +// return true; +// return false; +// } +// +// bool operator < (const double d) +// { +// if (mpfr_cmp_d(this->val, d) < 0) +// return true; +// return false; +// } +// +// bool operator > (const double d) +// { +// if (mpfr_cmp_d(this->val, d) > 0) +// return true; +// return false; +// } +// +// bool operator <= (const double d) +// { +// int v = mpfr_cmp_d(this->val, d); +// +// //std::cout << "comp: " << v << std::endl; +// +// if (v < 0 || v == 0) +// return true; +// return false; +// } +// +// bool operator >= (const double d) +// { +// int v = mpfr_cmp_d(this->val, d); +// +// if (v > 0 || v == 0) +// return true; +// return false; +// } +//}; + +void init(uint32_t prec); + +class Vec2mp { public: - uint32_t precision; - mpfr_t val; - - BigFloat(uint32_t prec); - void setValue(double val); - std::string to_str(); - BigFloat operator / (const BigFloat &f); - - inline BigFloat operator - (const BigFloat &f) const - { - BigFloat res = BigFloat(f.precision); - mpfr_sub(res.val, this->val, f.val, MPFR_RNDD); - - return res; - } - - inline BigFloat operator - (const double d) const - { - BigFloat res = BigFloat(this->precision); - mpfr_sub_d(res.val, this->val, d, MPFR_RNDD); - - return res; - } - - inline BigFloat operator * (const BigFloat &f) const - { - BigFloat res = BigFloat(f.precision); - mpfr_mul(res.val, this->val, f.val, MPFR_RNDD); - - return res; - } - - inline BigFloat operator * (const double d) const - { - BigFloat res = BigFloat(this->precision); - mpfr_mul_d(res.val, this->val, d, MPFR_RNDD); - - return res; - } - - inline BigFloat operator + (const BigFloat &f) const - { - BigFloat res = BigFloat(f.precision); - mpfr_add(res.val, this->val, f.val, MPFR_RNDD); - - return res; - } - - inline BigFloat operator + (const double d) const - { - BigFloat res = BigFloat(this->precision); - mpfr_add_d(res.val, this->val, d, MPFR_RNDD); - - return res; - } - - inline bool operator < (const BigFloat &f) - { - if (mpfr_cmp(this->val, f.val) < 0) - return true; - return false; - } - - inline bool operator > (const BigFloat &f) - { - if (mpfr_cmp(this->val, f.val) > 0) - return true; - return false; - } - - inline bool operator <= (const BigFloat &f) - { - int v = mpfr_cmp(this->val, f.val); - - if (v < 0 || v == 0) - return true; - return false; - } - - inline bool operator >= (const BigFloat &f) - { - int v = mpfr_cmp(this->val, f.val); - - if (v > 0 || v == 0) - return true; - return false; - } - - inline bool operator < (const double d) - { - if (mpfr_cmp_d(this->val, d) < 0) - return true; - return false; - } - - inline bool operator > (const double d) - { - if (mpfr_cmp_d(this->val, d) > 0) - return true; - return false; - } - - inline bool operator <= (const double d) - { - int v = mpfr_cmp_d(this->val, d); - - if (v < 0 || v == 0) - return true; - return false; - } - - inline bool operator >= (const double d) - { - int v = mpfr_cmp_d(this->val, d); - - if (v > 0 || v == 0) - return true; - return false; - } -}; - -class Vec2bf { -public: - BigFloat x; - BigFloat y; - Vec2bf(double x, double y, uint32_t prec); + mpfr::mpreal x; + mpfr::mpreal y; + Vec2mp(mpfr::mpreal x, mpfr::mpreal y); + Vec2mp(double x, double y); }; }