Compare commits
10 Commits
cdadabb149
...
32f93dcea5
Author | SHA1 | Date | |
---|---|---|---|
32f93dcea5 | |||
a561882a53 | |||
f1da5df0bc | |||
9d08e7574f | |||
a50c52566d | |||
572e28c32f | |||
874a8f21f8 | |||
32c36e65c1 | |||
d4cc9853cf | |||
b8e9ea816d |
BIN
NotoSansMono-Regular.ttf
Normal file
BIN
NotoSansMono-Regular.ttf
Normal file
Binary file not shown.
330
graphics.cpp
330
graphics.cpp
@ -18,8 +18,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "graphics.h"
|
||||
#include "utils.h"
|
||||
#include "mpreal.h"
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_pixels.h>
|
||||
#include <SDL2/SDL_render.h>
|
||||
#include <SDL2/SDL_surface.h>
|
||||
#include <SDL2/SDL_ttf.h>
|
||||
#include <SDL2/SDL_video.h>
|
||||
#include <math.h>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
@ -33,13 +39,18 @@ Graphics::Graphics(int w, int h, std::string title) : w(w), h(h), title(title),
|
||||
m_end(Vec2i(0, 0)), mouseDown(false)
|
||||
{
|
||||
|
||||
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
|
||||
std::cout << "Could not initialize SDL. "
|
||||
<< SDL_GetError() << std::endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
SDL_Window *window = SDL_CreateWindow(this->title.c_str(),
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
this->w, this->h, 0);
|
||||
|
||||
if (!window)
|
||||
{
|
||||
if (!window) {
|
||||
std::cout << "Could not create window. "
|
||||
<< SDL_GetError() << std::endl;
|
||||
exit(EXIT_FAILURE);
|
||||
@ -49,17 +60,126 @@ Graphics::Graphics(int w, int h, std::string title) : w(w), h(h), title(title),
|
||||
|
||||
SDL_Renderer *r = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
||||
|
||||
if (!r)
|
||||
{
|
||||
if (!r) {
|
||||
std::cout << "Could not create renderer. "
|
||||
<< SDL_GetError() << std::endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
this->renderer = r;
|
||||
|
||||
SDL_Surface* screensurface = SDL_GetWindowSurface(this->window);
|
||||
|
||||
if(!screensurface) {
|
||||
std::cout << "Could not create screen surface. "
|
||||
<< SDL_GetError() << std::endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
this->screenSurface = screensurface;
|
||||
|
||||
SDL_Surface* surface = SDL_CreateRGBSurface(0, this->w, this->h,
|
||||
32, 0, 0, 0, 0);
|
||||
|
||||
if(!surface) {
|
||||
std::cout << "Could not create surface. "
|
||||
<< SDL_GetError() << std::endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
this->surface = surface;
|
||||
|
||||
SDL_PixelFormat* format = this->surface->format;
|
||||
this->pformat = format;
|
||||
this->running = true;
|
||||
}
|
||||
|
||||
Graphics::Graphics(int w, int h, std::string title, int tcount, int maxiter)
|
||||
: w(w), h(h), title(title), from(Vec2mp(0.0, 0.0)),
|
||||
to(Vec2mp(0.0, 0.0)), m_start(Vec2i(0, 0)), m_end(Vec2i(0, 0)),
|
||||
mouseDown(false), tcount(tcount), maxiter(maxiter)
|
||||
{
|
||||
|
||||
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
|
||||
std::cout << "Could not initialize SDL. "
|
||||
<< SDL_GetError() << std::endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
SDL_Window *window = SDL_CreateWindow(this->title.c_str(),
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
this->w, this->h, 0);
|
||||
|
||||
if (!window) {
|
||||
std::cout << "Could not create window. "
|
||||
<< SDL_GetError() << std::endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
this->window = window;
|
||||
|
||||
SDL_Renderer *r = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
||||
|
||||
if (!r) {
|
||||
std::cout << "Could not create renderer. "
|
||||
<< SDL_GetError() << std::endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
this->renderer = r;
|
||||
|
||||
SDL_Surface* screensurface = SDL_GetWindowSurface(this->window);
|
||||
|
||||
if(!screensurface) {
|
||||
std::cout << "Could not create screen surface. "
|
||||
<< SDL_GetError() << std::endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
this->screenSurface = screensurface;
|
||||
|
||||
SDL_Surface* surface = SDL_CreateRGBSurface(0, this->w, this->h,
|
||||
32, 0, 0, 0, 0);
|
||||
|
||||
if(!surface) {
|
||||
std::cout << "Could not create surface. "
|
||||
<< SDL_GetError() << std::endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
this->surface = surface;
|
||||
|
||||
SDL_PixelFormat* format = this->surface->format;
|
||||
this->pformat = format;
|
||||
this->running = true;
|
||||
}
|
||||
void Graphics::initFont(const std::string path, uint16_t size)
|
||||
{
|
||||
TTF_Init();
|
||||
|
||||
TTF_Font* font = TTF_OpenFont(path.c_str(), size);
|
||||
|
||||
if(!font)
|
||||
{
|
||||
std::cout << "Could not load font. "
|
||||
<< SDL_GetError() << std::endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
this->font = font;
|
||||
}
|
||||
|
||||
void Graphics::drawText(const std::string string)
|
||||
{
|
||||
SDL_Color textColor = {0, 0, 0, 255};
|
||||
|
||||
this->textSurface = TTF_RenderText_Solid(this->font, string.c_str(),
|
||||
textColor);
|
||||
|
||||
SDL_BlitSurface(this->textSurface, NULL, this->surface, NULL);
|
||||
}
|
||||
|
||||
void Graphics::drawPoint(const Vec2i &pos, const rgb &col)
|
||||
{
|
||||
SDL_RenderDrawPoint(this->renderer, pos.x, pos.y);
|
||||
@ -124,88 +244,73 @@ rgb hsv2rgb(hsv in)
|
||||
//std::cout << "ret" << out.g << out.b << out.b << std::endl;
|
||||
return out;
|
||||
}
|
||||
|
||||
void Graphics::setPixel(uint16_t x, uint16_t y, uint32_t pixel)
|
||||
{
|
||||
uint32_t* const target_pixel = (uint32_t*) ((uint8_t*) surface->pixels
|
||||
+ y * surface->pitch
|
||||
+ x * surface->format->BytesPerPixel);
|
||||
|
||||
*target_pixel = pixel;
|
||||
}
|
||||
|
||||
void Graphics::plot(Mandelbrotc const &m)
|
||||
{
|
||||
// std::vector<uint8_t> screen = std::vector<uint8_t>();
|
||||
// std::vector<uint8_t> screen1 = std::vector<uint8_t>();
|
||||
// #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.s.x; i++) {
|
||||
for (uint32_t j = 0; j < m.s.y; j++) {
|
||||
//uint8_t color = 255 - (m.screen[i][j]*255/m.max_iter);
|
||||
uint8_t iter = m.screen[i][j];
|
||||
hsv h;
|
||||
h.h = 255*(double)iter/(double)m.max_iter;
|
||||
h.s = 255;
|
||||
if (iter < m. max_iter)
|
||||
h.v = 255;
|
||||
else
|
||||
h.v = 0;
|
||||
rgb r;
|
||||
r = hsv2rgb(h);
|
||||
//std::cout << "rgb" << r.r << r.g << r.b << std::endl;
|
||||
SDL_SetRenderDrawColor(this->renderer, r.r, r.g, r.b, 255);
|
||||
SDL_RenderDrawPoint(renderer, (int)i, (int)j);
|
||||
|
||||
uint8_t c = 255 - (m.screen[i][j]*255.0/(double)m.max_iter);
|
||||
// SDL_SetRenderDrawColor(this->renderer, color, color, color, 255);
|
||||
// SDL_RenderDrawPoint(renderer, (int)i, (int)j);
|
||||
|
||||
uint32_t pixel = SDL_MapRGB(this->pformat, c, c, c);
|
||||
this->setPixel(i, j, pixel);
|
||||
|
||||
// double iter = m.screen[i][j];
|
||||
|
||||
// if (i < m.s.x-1 && j < m.s.y-1) {
|
||||
// double iter1 = m.screen[i+1][j+1];
|
||||
|
||||
// hsv h;
|
||||
// h.h = 255*(double)iter/(double)m.max_iter;
|
||||
// h.s = 255;
|
||||
// if (iter < m. max_iter)
|
||||
// h.v = 255;
|
||||
// else
|
||||
// h.v = 0;
|
||||
// rgb r;
|
||||
// r = hsv2rgb(h);
|
||||
|
||||
// hsv h1;
|
||||
// h1.h = 255*(double)iter1/(double)m.max_iter;
|
||||
// h1.s = 255;
|
||||
// if (iter1 < m. max_iter)
|
||||
// h1.v = 255;
|
||||
// else
|
||||
// h1.v = 0;
|
||||
// rgb r1;
|
||||
// r1 = hsv2rgb(h1);
|
||||
|
||||
// uint8_t ri = linear_interpolate(r.r, r1.r, (int)iter % 1);
|
||||
// uint8_t gi = linear_interpolate(r.g, r1.g, (int)iter % 1);
|
||||
// uint8_t bi = linear_interpolate(r.b, r1.b, (int)iter % 1);
|
||||
|
||||
// SDL_SetRenderDrawColor(this->renderer, ri, gi, bi, 255);
|
||||
// SDL_RenderDrawPoint(renderer, (int)i, (int)j);
|
||||
// } else {
|
||||
// hsv h;
|
||||
// h.h = 255*(double)iter/(double)m.max_iter;
|
||||
// h.s = 255;
|
||||
// if (iter < m. max_iter)
|
||||
// h.v = 255;
|
||||
// else
|
||||
// h.v = 0;
|
||||
// rgb r;
|
||||
// r = hsv2rgb(h);
|
||||
|
||||
// SDL_SetRenderDrawColor(this->renderer, r.r, r.g, r.b, 255);
|
||||
// SDL_RenderDrawPoint(renderer, (int)i, (int)j);
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -213,28 +318,29 @@ void Graphics::plot(Mandelbrotc const &m)
|
||||
|
||||
void Graphics::drawSelectionRectangle()
|
||||
{
|
||||
uint32_t pixel = SDL_MapRGB(this->pformat, 0, 0, 0);
|
||||
|
||||
if (this->mouseDown) {
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
||||
if (m_start.x < m_end.x) {
|
||||
for (int i = m_start.x; i < m_end.x; i++) {
|
||||
SDL_RenderDrawPoint(renderer, i, m_start.y);
|
||||
SDL_RenderDrawPoint(renderer, i, m_end.y);
|
||||
this->setPixel(i, m_start.y, pixel);
|
||||
this->setPixel(i, m_end.y, pixel);
|
||||
}
|
||||
} else {
|
||||
for (int i = m_end.x; i < m_start.x; i++) {
|
||||
SDL_RenderDrawPoint(renderer, i, m_start.y);
|
||||
SDL_RenderDrawPoint(renderer, i, m_end.y);
|
||||
this->setPixel(i, m_start.y, pixel);
|
||||
this->setPixel(i, m_end.y, pixel);
|
||||
}
|
||||
}
|
||||
if (m_start.y < m_end.y) {
|
||||
for (int i = m_start.y; i < m_end.y; i++) {
|
||||
SDL_RenderDrawPoint(renderer, m_start.x, i);
|
||||
SDL_RenderDrawPoint(renderer, m_end.x, i);
|
||||
this->setPixel(m_start.x, i, pixel);
|
||||
this->setPixel(m_end.x, i, pixel);
|
||||
}
|
||||
} else {
|
||||
for (int i = m_end.y; i < m_start.y; i++) {
|
||||
SDL_RenderDrawPoint(renderer, m_start.x, i);
|
||||
SDL_RenderDrawPoint(renderer, m_end.x, i);
|
||||
this->setPixel(m_start.x, i, pixel);
|
||||
this->setPixel(m_end.x, i, pixel);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -261,6 +367,8 @@ void Graphics::zoom(Mandelbrotc &m)
|
||||
|
||||
void Graphics::mainLoop()
|
||||
{
|
||||
this->initFont("NotoSansMono-Regular.ttf", 16);
|
||||
|
||||
mpreal fx = mpreal(-2);
|
||||
mpreal fy = mpreal(-1);
|
||||
mpreal tx = mpreal(1);
|
||||
@ -270,13 +378,17 @@ void Graphics::mainLoop()
|
||||
|
||||
Vec2i m_start = Vec2i(0, 0);
|
||||
Vec2i m_end = Vec2i(0, 0);
|
||||
|
||||
std::chrono::time_point<std::chrono::system_clock> start, end;
|
||||
std::chrono::duration<double> elapsed_seconds;
|
||||
|
||||
Mandelbrotc m = Mandelbrotc(from, to, Vec2i(this->w, this->h), 100);
|
||||
m.thread_count = 16;
|
||||
Mandelbrotc m = Mandelbrotc(from, to, Vec2i(this->w, this->h), maxiter);
|
||||
m.thread_count = this->tcount;
|
||||
m.start_threads(true);
|
||||
|
||||
while (this->running)
|
||||
{
|
||||
start = std::chrono::system_clock::now();
|
||||
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event))
|
||||
@ -317,13 +429,18 @@ void Graphics::mainLoop()
|
||||
this->m_end.x = event.motion.x;
|
||||
this->m_end.y = event.motion.y;
|
||||
break;
|
||||
case SDL_KEYUP:
|
||||
if (event.key.keysym.scancode == SDL_SCANCODE_R) {
|
||||
mpreal::set_default_prec(mpfr::digits2bits(20));
|
||||
m.f.x = mpreal(-2.0);
|
||||
m.f.y = mpreal(-1.0);
|
||||
m.t.x = mpreal(1.0);
|
||||
m.t.y = mpreal(1.0);
|
||||
m.diff = mpreal(0.0001);
|
||||
}
|
||||
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;
|
||||
|
||||
m.start_threads(false);
|
||||
this->plot(m);
|
||||
@ -331,11 +448,16 @@ void Graphics::mainLoop()
|
||||
|
||||
this->drawSelectionRectangle();
|
||||
|
||||
SDL_RenderPresent(renderer);
|
||||
SDL_SetRenderDrawColor(this->renderer, 255, 255, 255, 255);
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
//SDL_Delay(10);
|
||||
this->drawText("from x:" + m.f.x.toString() + " y: " + m.f.y.toString() +
|
||||
"to x:" + m.t.x.toString() + " y: " + m.t.y.toString());
|
||||
|
||||
SDL_BlitSurface(this->surface, NULL, this->screenSurface, NULL);
|
||||
SDL_UpdateWindowSurface(this->window);
|
||||
|
||||
end = std::chrono::system_clock::now();
|
||||
elapsed_seconds = end - start;
|
||||
|
||||
//std::cout << "FPS: " << 1 / elapsed_seconds.count() << std::endl;
|
||||
}
|
||||
m.stop_threads();
|
||||
}
|
||||
|
14
graphics.h
14
graphics.h
@ -20,8 +20,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_pixels.h>
|
||||
#include <SDL2/SDL_ttf.h>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
#include "utils.h"
|
||||
#include "mandelbrot.h"
|
||||
|
||||
@ -34,9 +37,16 @@ namespace Mandelbrot {
|
||||
std::string title;
|
||||
int w, h;
|
||||
SDL_Window *window;
|
||||
TTF_Font* font;
|
||||
SDL_Renderer *renderer;
|
||||
SDL_Surface *textSurface;
|
||||
SDL_Surface *surface;
|
||||
SDL_Surface *screenSurface;
|
||||
SDL_PixelFormat* pformat;
|
||||
Vec2mp from;
|
||||
Vec2mp to;
|
||||
int tcount;
|
||||
int maxiter;
|
||||
bool running;
|
||||
|
||||
bool mouseDown;
|
||||
@ -44,6 +54,10 @@ namespace Mandelbrot {
|
||||
Vec2i m_end;
|
||||
|
||||
Graphics(int w, int h, std::string title);
|
||||
Graphics(int w, int h, std::string title, int tcount, int maxiter);
|
||||
void initFont(const std::string path, uint16_t size);
|
||||
void drawText(const std::string string);
|
||||
void setPixel(uint16_t x, uint16_t y, uint32_t pixel);
|
||||
void plot(Mandelbrotc const &m);
|
||||
void mainLoop();
|
||||
void drawPoint(const Vec2i &pos, const rgb &col);
|
||||
|
56
main.cpp
56
main.cpp
@ -17,13 +17,65 @@ You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <unistd.h>
|
||||
#include "graphics.h"
|
||||
|
||||
using namespace Mandelbrot;
|
||||
|
||||
int main()
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
Graphics g = Graphics(1800, 1200, "Mandelbrot set visualizer");
|
||||
char c;
|
||||
std::string targ, iarg;
|
||||
int tcount = 0;
|
||||
int maxiter = 0;
|
||||
|
||||
while ((c = getopt(argc, argv, "ht:i:")) != -1) {
|
||||
switch (c) {
|
||||
case 'h':
|
||||
std::cout << "Usage: " << argv[0] << " [-t] [thread count] " <<
|
||||
"[-i] [max iterations]" << std::endl;
|
||||
exit(0);
|
||||
case 't':
|
||||
targ = optarg;
|
||||
break;
|
||||
case 'i':
|
||||
iarg = optarg;
|
||||
break;
|
||||
case ':':
|
||||
std::cout << "option needs a value." << std::endl;
|
||||
exit(1);
|
||||
case '?':
|
||||
if (optopt == 't' || optopt == 'i')
|
||||
fprintf(stderr, "Option -%c requires an argument.\n",
|
||||
optopt);
|
||||
else if (isprint (optopt))
|
||||
fprintf(stderr, "Unknown option `-%c'.\n", optopt);
|
||||
else
|
||||
fprintf(stderr,
|
||||
"Unknown option character `\\x%x'.\n",
|
||||
optopt);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!targ.empty() && !iarg.empty()) {
|
||||
try {
|
||||
tcount = std::stoi(targ);
|
||||
maxiter = std::stoi(iarg);
|
||||
} catch(...) {
|
||||
std::cout << "Invalid arguments." << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
if (tcount == 0 || tcount < 0)
|
||||
tcount = 4;
|
||||
|
||||
if (maxiter == 0 || maxiter < 0)
|
||||
maxiter = 100;
|
||||
|
||||
Graphics g = Graphics(1800, 1200, "Mandelbrot set visualizer", tcount, maxiter);
|
||||
g.mainLoop();
|
||||
|
||||
return 0;
|
||||
|
@ -18,22 +18,25 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "mandelbrot.h"
|
||||
#include "mpreal.h"
|
||||
#include "utils.h"
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <limits>
|
||||
|
||||
using namespace mpfr;
|
||||
|
||||
Mandelbrotc::Mandelbrotc(Vec2mp const &f, Vec2mp const &t, Vec2i const &s, uint32_t mi)
|
||||
: f(f), t(t), s(s), max_iter(mi)
|
||||
{
|
||||
mpreal::set_default_prec(digits2bits(100));
|
||||
this->threads = std::vector<std::thread>();
|
||||
//this->screen = std::vector<std::vector<uint8_t>>();
|
||||
this->screen = new uint8_t*[s.x];
|
||||
this->screen = new double*[s.x];
|
||||
for(int i = 0; i < s.x; ++i)
|
||||
this->screen[i] = new uint8_t[s.y];
|
||||
this->screen[i] = new double[s.y];
|
||||
|
||||
this->done = false;
|
||||
this->diff = mpreal(0.00001);
|
||||
}
|
||||
|
||||
void Mandelbrotc::start_threads(bool first)
|
||||
@ -70,14 +73,16 @@ void Mandelbrotc::calc(const uint8_t tid)
|
||||
}
|
||||
}
|
||||
|
||||
this->updatePrec();
|
||||
|
||||
//std::cout << "thread " << tid << " done" << std::endl;
|
||||
if (tid == thread_count-1)
|
||||
this->done = true;
|
||||
}
|
||||
|
||||
uint32_t Mandelbrotc::mandelbrot(const Vec2mp &n)
|
||||
double Mandelbrotc::mandelbrot(const Vec2mp &n)
|
||||
{
|
||||
uint32_t iter = 0;
|
||||
double iter = 0.0;
|
||||
|
||||
// BigFloat x = BigFloat(n.x.precision);
|
||||
// x.setValue(0.0);
|
||||
@ -88,15 +93,66 @@ uint32_t Mandelbrotc::mandelbrot(const Vec2mp &n)
|
||||
mpreal y = mpreal(0.0);
|
||||
mpreal x2 = mpreal(0.0);
|
||||
mpreal y2 = mpreal(0.0);
|
||||
mpreal xtemp = mpreal(0.0);
|
||||
|
||||
while (x2 + y2 <= 4 && iter < max_iter) {
|
||||
y = 2.0 * x * y + n.y;
|
||||
x = x2 - y2 + n.x;
|
||||
x2 = x * x;
|
||||
y2 = y * y;
|
||||
// while (x2 + y2 <= (1 << 16) && iter < max_iter) {
|
||||
// y = 2.0 * x * y + n.y;
|
||||
// x = x2 - y2 + n.x;
|
||||
// x2 = x * x;
|
||||
// y2 = y * y;
|
||||
|
||||
// iter++;
|
||||
// }
|
||||
|
||||
while (x*x + y*y <= (1 << 16) && iter < max_iter) {
|
||||
xtemp = x*x - y*y + n.x;
|
||||
y = 2*x*y + n.y;
|
||||
x = xtemp;
|
||||
|
||||
iter++;
|
||||
}
|
||||
|
||||
return iter;//+ 1 - log(log2(abs((int)x2)));
|
||||
if (iter == max_iter)
|
||||
return max_iter;
|
||||
|
||||
double log_zn = (mpfr::log(x*x + y*y) / 2).toDouble();
|
||||
double nu = log(log_zn / log(2)) / log(2);
|
||||
|
||||
iter = iter + 1 - nu;
|
||||
|
||||
return iter;
|
||||
//return iter + 1 - log(log2(x2.toDouble()+y2.toDouble()));
|
||||
}
|
||||
|
||||
void Mandelbrotc::updatePrec()
|
||||
{
|
||||
//std::cout << "before updatePrec: " << this->f.x.get_prec() << std::endl;
|
||||
mpfr_prec_t bprec = this->f.x.get_prec();
|
||||
mpfr_prec_t aprec;
|
||||
|
||||
std::cout << "before updatePrec def: " << mpfr::bits2digits(
|
||||
bprec) << std::endl;
|
||||
|
||||
if (mpfr::abs(mpfr::abs(this->f.x) - mpfr::abs(this->t.x)) < diff ||
|
||||
(mpfr::abs(this->f.x) - mpfr::abs(this->t.y)) < diff) {
|
||||
|
||||
aprec = mpfr::digits2bits(mpfr::bits2digits(bprec)+5);
|
||||
|
||||
std::cout << "true" << std::endl;
|
||||
}
|
||||
else {
|
||||
return;
|
||||
}
|
||||
|
||||
this->f.x.set_prec(aprec);
|
||||
this->f.y.set_prec(aprec);
|
||||
this->t.x.set_prec(aprec);
|
||||
this->t.y.set_prec(aprec);
|
||||
|
||||
//std::cout << "after updatePrec: " << this->f.x.get_prec() << std::endl;
|
||||
std::cout << "after updatePrec def: " << mpfr::bits2digits(
|
||||
this->f.x.get_prec()) << std::endl;
|
||||
|
||||
diff = mpfr::abs(mpfr::abs(this->f.x) - mpfr::abs(this->t.x));
|
||||
diff *= 0.01;
|
||||
}
|
||||
|
@ -32,16 +32,18 @@ class Mandelbrotc {
|
||||
public:
|
||||
Vec2mp f, t;
|
||||
uint8_t thread_count;
|
||||
mpfr::mpreal diff;
|
||||
std::vector<std::thread> threads;
|
||||
Vec2i s;
|
||||
//std::vector<std::vector<uint8_t>> screen;
|
||||
uint8_t **screen;
|
||||
double **screen;
|
||||
bool done;
|
||||
uint32_t max_iter;
|
||||
|
||||
Mandelbrotc(Vec2mp const &f, Vec2mp const &t, Vec2i const &s, uint32_t mi);
|
||||
void start_threads(bool first);
|
||||
void stop_threads();
|
||||
uint32_t mandelbrot(const Vec2mp &n);
|
||||
double mandelbrot(const Vec2mp &n);
|
||||
void calc(const uint8_t tid);
|
||||
void updatePrec();
|
||||
};
|
||||
|
@ -84,6 +84,11 @@ void init(uint32_t prec)
|
||||
// mpfr::mpreal::set_default_prec(prec);
|
||||
}
|
||||
|
||||
double Util::linear_interpolate(double v0, double v1, float t)
|
||||
{
|
||||
return (1 - t) * v0 + t * v1;
|
||||
}
|
||||
|
||||
Vec2mp::Vec2mp(mpfr::mpreal x, mpfr::mpreal y)
|
||||
: x(x), y(y)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user