Skip to content

Commit

Permalink
Changed mandelbrot return type from 2d to 1d array
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenewald committed Dec 2, 2024
1 parent 49e83f1 commit 758c473
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 38 deletions.
30 changes: 7 additions & 23 deletions source/mandelbrot/mandelbrot_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,25 @@
#include <iostream>

namespace fractal {
std::array<float, 8> MandelbrotWindow::draw_coordinate_(
display_coordinate display_coord, const avx512_complex& complex_coords
)
{
static constexpr avx512_complex START{};
std::array<iteration_count, 8> iterations =
compute_iterations(START, complex_coords, MANDELBROT_MAX_ITERATIONS);

std::array<float, 8> ret{};
for (size_t i = 0; i < 8; i++) {
ret[i] = static_cast<float>(iterations[i]) / MANDELBROT_MAX_ITERATIONS;
display_coord.x++;
}
return ret;
}

std::unique_ptr<MandelbrotWindow::arr> MandelbrotWindow::calculate_(
std::unique_ptr<MandelbrotWindow::pixel_iteration_counts> MandelbrotWindow::calculate_(
const DisplayDomain& full_display_domain, const DisplayDomain& new_domain_selection
)
{
to_complex_.update_display_domain(new_domain_selection);

auto process_coordinates = [&](display_coordinate coord) {
return draw_coordinate_(coord, to_complex_.to_complex_projections(coord));
return compute_iterations(
{}, to_complex_.to_complex_projections(coord), MANDELBROT_MAX_ITERATIONS
);
};

auto ret = std::make_unique<arr>();
auto ret = std::make_unique<pixel_iteration_counts>();
auto process_chunk = [&](DisplayDomain::DisplayCoordinateIterator start,
DisplayDomain::DisplayCoordinateIterator end) {
for (auto it = start; it != end; it += 8) {
display_coordinate pos = *it;
std::array<float, 8> t = process_coordinates(pos);
for (size_t i = 0; i < 8; ++i) {
(*ret)[pos.x++][pos.y] = Percentage{t[i]};
}
std::array<iteration_count, 8> t = process_coordinates(*it);
std::copy(t.begin(), t.end(), ret->begin() + it.get_underlying());
}
};

Expand Down
9 changes: 3 additions & 6 deletions source/mandelbrot/mandelbrot_window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,16 @@

namespace fractal {
class MandelbrotWindow {
using arr = std::array<std::array<Percentage, WINDOW_HEIGHT + 8>, WINDOW_WIDTH + 8>;
DisplayToComplexCoordinates to_complex_;

static std::array<float, 8> draw_coordinate_(
display_coordinate display_coord, const avx512_complex& complex_coords
);

public:
MandelbrotWindow(
const DisplayDomain& display_domain, const complex_domain& complex_domain
);

std::unique_ptr<arr> calculate_(
using pixel_iteration_counts =
std::array<iteration_count, WINDOW_HEIGHT * WINDOW_WIDTH>;
std::unique_ptr<pixel_iteration_counts> calculate_(
const DisplayDomain& full_display_domain,
const DisplayDomain& new_domain_selection
);
Expand Down
23 changes: 15 additions & 8 deletions source/mandelbrot/window.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "config.hpp"
#include "graphics/aspect_ratio/aspect_ratio.hpp"
#include "graphics/color_conversions/color_conversions.hpp"
#include "graphics/display_event_observer.hpp"
Expand Down Expand Up @@ -33,6 +34,18 @@ class Window : public DisplayEventObserver {
);
}

void
set_pixel_colors(std::unique_ptr<MandelbrotWindow::pixel_iteration_counts> pixels)
{
for (auto it = DISPLAY_DOMAIN.begin(); it != DISPLAY_DOMAIN.end(); ++it) {
Percentage color(
static_cast<float>((*pixels)[it.get_underlying()])
/ MANDELBROT_MAX_ITERATIONS
);
set_pixel_color(*it, color);
}
}

Window(DisplayDomain display_domain, complex_domain complex_domain) :
DISPLAY_DOMAIN{display_domain}, complex_domain_{complex_domain},
mandelbrot_{display_domain, complex_domain}
Expand All @@ -42,10 +55,7 @@ class Window : public DisplayEventObserver {
display_domain.get_end_coordinate().y + 1u
);

auto res = mandelbrot_.calculate_(DISPLAY_DOMAIN, DISPLAY_DOMAIN);
for (display_coordinate pos : DISPLAY_DOMAIN) {
set_pixel_color(pos, (*res)[pos.x][pos.y]);
}
set_pixel_colors(mandelbrot_.calculate_(DISPLAY_DOMAIN, DISPLAY_DOMAIN));
}

void on_mouse_button_pressed(const sf::Event::MouseButtonEvent& event) override
Expand All @@ -60,10 +70,7 @@ class Window : public DisplayEventObserver {
DisplayDomain ends = calculate_rectangle_end_points(
{selection_start_x_, selection_start_y_}, {event.x, event.y}
);
auto res = mandelbrot_.calculate_(DISPLAY_DOMAIN, ends);
for (display_coordinate pos : DISPLAY_DOMAIN) {
set_pixel_color(pos, (*res)[pos.x][pos.y]);
}
set_pixel_colors(mandelbrot_.calculate_(DISPLAY_DOMAIN, ends));
}

std::optional<std::unique_ptr<sf::Drawable>> get_drawable() override
Expand Down
2 changes: 2 additions & 0 deletions source/units/display_domain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ class DisplayDomain {
}

bool operator==(const DisplayCoordinateIterator&) const = default;

uint32_t get_underlying() const { return current_coordinate_; }
};

uint32_t width() const { return END_COORDINATE.x - START_COORDINATE.x; }
Expand Down
2 changes: 1 addition & 1 deletion source/units/units.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Percentage {
assert_true(percentage <= 1.0f);
}

consteval explicit Percentage() : percentage_{0.0f} {}
constexpr explicit Percentage() : percentage_{0.0f} {}

float get_percentage() const { return percentage_; }
};
Expand Down

0 comments on commit 758c473

Please sign in to comment.