Skip to content

Commit

Permalink
Fix master gui header file and import utils and const packages to mat…
Browse files Browse the repository at this point in the history
…rix_to_pixel
  • Loading branch information
Kevin-Duignan committed May 7, 2024
1 parent 78aeeee commit be08878
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 48 deletions.
83 changes: 40 additions & 43 deletions headers/gui.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "utils.hpp"
#include "matrix_to_pixel.hpp"
#include "utils.hpp"

#include <fmt/core.h>

Expand Down Expand Up @@ -75,32 +75,7 @@ class gui {
}
return event;
}
auto update_display(std::array<float, BUFFER_SIZE>& data) -> void
{
window.clear();

sf::RectangleShape (*draw_func)(std::array<float, BUFFER_SIZE>&, size_t, size_t);

// identify draw function
switch (current_draw_type) {
case draw_type::GREY:
draw_func = matrix_coords_to_greyscale_pixel;
break;
case draw_type::HSV:
draw_func = matrix_coords_to_HSV_pixel;
break;
case draw_type::VEL:
draw_func = matrix_coords_to_HSV_pixel;
break;
default:
draw_func = matrix_coords_to_greyscale_pixel;
break;

}
}

return event;
}
auto update_display(std::array<float, BUFFER_SIZE>& x_data, std::array<float, BUFFER_SIZE>& u_data, std::array<float, BUFFER_SIZE>& v_data) -> void
{
window.clear();
Expand Down Expand Up @@ -129,14 +104,6 @@ class gui {
return static_cast<sf::Uint8>(std::clamp(value, 0.0f, 1.0f) * 255);
}

/**
* @brief This function takes in a density-matrix with values from 0-1 and
* converts each value in the matrix to a value from 0-255 then prints it
* on a single pixel in an SFML window.
*
* @param window
* @param densityArray
*/
auto GreyScaleMatrixToSFML(std::array<float, BUFFER_SIZE>& data) -> void
{
for (size_t i = 1uL; i <= AXIS_SIZE; i++) {
Expand All @@ -153,13 +120,9 @@ class gui {
pixel.setFillColor(sf::Color(value, value, value));

window.draw(pixel);
}
}

// loop the matrix and apply the function to each pixel.
for (size_t i = 0uL; i < AXIS_SIZE + 2uL; i++) {
for (size_t j = 0uL; j < AXIS_SIZE + 2uL; j++) {
window.draw(draw_func(data, i, j));

}

auto HSV_to_SFML(std::array<float, BUFFER_SIZE>& data) -> void
{
Expand All @@ -181,10 +144,44 @@ class gui {
}
}
}

auto is_open() -> bool { return window.isOpen(); }

auto getRenderWindow() -> sf::RenderWindow& { return window; }
auto HSV_to_RGB(float H, float S, float V) -> sf::Color
{
float C = S * V;
float X = C * (1.0f - std::abs(fmodf(H / 60.0f, 2.0f) - 1.0f));
float m = V - C;
float Rs, Gs, Bs;

// Ensure H is within the range of 0 to 360
H = fmodf(H, 360.0f);
if (H < 0.0f)
H += 360.0f;

if (H >= 0.0f && H < 60.0f) {
Rs = C;
Gs = X;
Bs = 0.0f;
} else if (H >= 60.0f && H < 120.0f) {
Rs = X;
Gs = C;
Bs = 0.0f;
} else if (H >= 120.0f && H < 180.0f) {
Rs = 0.0f;
Gs = C;
Bs = X;
} else if (H >= 180.0f && H < 240.0f) {
Rs = 0.0f;
Gs = X;
Bs = C;
} else if (H >= 240.0f && H < 300.0f) {
Rs = X;
Gs = 0.0f;
Bs = C;
} else {
Rs = C;
Gs = 0.0f;
Bs = X;
}

return sf::Color(static_cast<sf::Uint8>((Rs + m) * 255.0f), static_cast<sf::Uint8>((Gs + m) * 255.0f), static_cast<sf::Uint8>((Bs + m) * 255.0f));
}
Expand Down
10 changes: 5 additions & 5 deletions headers/matrix_to_pixel.hpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
#pragma once
#include "const.hpp"
#include "utils.hpp"
#include <SFML/Graphics.hpp>
#include <array>
#include <cmath>

auto matrix_coords_to_greyscale_pixel(std::array<float, BUFFER_SIZE>& data, size_t i, size_t j) -> sf::RectangleShape;
auto matrix_coords_to_HSV_pixel(std::array<float, BUFFER_SIZE>& data, size_t i, size_t j) -> sf::RectangleShape;

namespace{ // Putting these methods in namespace makes them private.
// They are only used locally by the 2 functions (above) that we want to export.
namespace { // Putting these methods in namespace makes them private.
// They are only used locally by the 2 functions (above) that we want to export.
constexpr auto grey_scale(float value) -> sf::Uint8
{
return static_cast<sf::Uint8>(std::clamp(value, 0.0f, 1.0f) * 255);
}


auto HSV_to_RGB(float H, float S, float V) -> sf::Color
{
float C = S * V;
Expand Down Expand Up @@ -56,7 +57,6 @@ auto HSV_to_RGB(float H, float S, float V) -> sf::Color
}
}


/**
* @brief This function takes in a density-matrix with values from 0-1 and
* converts each value in the matrix to a value from 0-255 then prints it
Expand All @@ -76,7 +76,7 @@ auto matrix_coords_to_greyscale_pixel(std::array<float, BUFFER_SIZE>& data, size
auto value = grey_scale(data.at(IX(i, j)));

pixel.setFillColor(sf::Color(value, value, value));

return pixel;
}

Expand Down

0 comments on commit be08878

Please sign in to comment.