too much changes to describe

This commit is contained in:
Akos Horvath 2022-07-05 19:06:45 +02:00
parent f21fed9b27
commit 6b75b7d61a
6 changed files with 343 additions and 154 deletions

View File

@ -1,2 +1,4 @@
all: 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

View File

@ -1,12 +1,16 @@
#include "graphics.h" #include "graphics.h"
#include "utils.h" #include "utils.h"
#include <SDL2/SDL_render.h> #include <SDL2/SDL_render.h>
#include <cstdint>
#include <cstdlib> #include <cstdlib>
#include <iostream> #include <iostream>
#include <vector>
using namespace Mandelbrot; 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_Window *window = SDL_CreateWindow(this->title.c_str(),
SDL_WINDOWPOS_CENTERED, 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->renderer = r;
this->running = true;
} }
void Graphics::drawPoint(const Vec2i &pos, const rgb &col) 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); 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++) { // std::vector<uint8_t> screen = std::vector<uint8_t>();
for (double j = 0.0; j < this->h; j++) { // std::vector<uint8_t> screen1 = std::vector<uint8_t>();
BigFloat f = (from.x - to.x) * from.x + (i / this->w); // #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);
}
}

View File

@ -4,6 +4,7 @@
#include <cstdint> #include <cstdint>
#include <string> #include <string>
#include "utils.h" #include "utils.h"
#include "mandelbrot.h"
using namespace Util; using namespace Util;
@ -15,9 +16,13 @@ namespace Mandelbrot {
int w, h; int w, h;
SDL_Window *window; SDL_Window *window;
SDL_Renderer *renderer; SDL_Renderer *renderer;
Vec2mp from;
Vec2mp to;
bool running;
Graphics(int w, int h, std::string title); 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); void drawPoint(const Vec2i &pos, const rgb &col);
}; };
} // namespace Mandelbrot } // namespace Mandelbrot

View File

@ -1,6 +1,11 @@
#include "graphics.h" #include "graphics.h"
using namespace Mandelbrot;
int main() int main()
{ {
Graphics g = Graphics(1800, 1200, "Mandelbrot set visualizer");
g.mainLoop();
return 0; return 0;
} }

View File

@ -1,4 +1,5 @@
#include "utils.h" #include "utils.h"
#include "./mpreal.h"
using namespace Util; 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); BigFloat operator + (BigFloat const &f);

271
utils.h
View File

@ -1,8 +1,9 @@
#pragma once #pragma once
#include <cstdint> #include <cstdint>
#include <mpfr.h> #include "./mpreal.h"
#include <string> #include <string>
#include <iostream>
namespace Util { namespace Util {
@ -35,134 +36,148 @@ public:
Vec2d(double x, double y); 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: public:
uint32_t precision; mpfr::mpreal x;
mpfr_t val; mpfr::mpreal y;
Vec2mp(mpfr::mpreal x, mpfr::mpreal y);
BigFloat(uint32_t prec); Vec2mp(double x, double y);
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);
}; };
} }