Skip to content

Commit

Permalink
Merge pull request #10 from MonashDeepNeuron/SFML---colour
Browse files Browse the repository at this point in the history
Master
  • Loading branch information
Kevin-Duignan committed Mar 12, 2024
2 parents ae6b4ac + 2931294 commit ef80876
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 80 deletions.
139 changes: 132 additions & 7 deletions headers/gui.hpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#pragma once

#include "const.hpp"
#include "utils.hpp"

#include <fmt/core.h>

#include <SFML/Graphics.hpp>

#include <cstddef>
#include <array>
#include <iostream>
#include <string>
#include <tuple>
Expand All @@ -24,6 +22,13 @@ class gui {
sf::RenderWindow window;

public:
enum class draw_type : short { GREY,
HSV,
VEL };

draw_type current_draw_type = draw_type::GREY;

draw_type get_current_draw_type() const { return current_draw_type; }
gui()
: window(sf::VideoMode(screen_width, screen_height), window_text)
{
Expand Down Expand Up @@ -51,18 +56,138 @@ class gui {
window.close();
}

if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::C) {
// Toggle to the next draw type
switch (current_draw_type) {
case draw_type::GREY:
current_draw_type = draw_type::HSV;
break;
case draw_type::HSV:
current_draw_type = draw_type::VEL;
break;
case draw_type::VEL:
current_draw_type = draw_type::GREY;
break;
}
}

return event;
}

template <typename DrawFunction>
auto update_display(DrawFunction draw_function, const std::array<float, BUFFER_SIZE>& data) -> void
auto update_display(std::array<float, BUFFER_SIZE>& data, draw_type type) -> void
{
window.clear();
draw_function(window, data);
switch (type) {
case draw_type::GREY:
GreyScaleMatrixToSFML(data);
break;
case draw_type::HSV:
HSV_to_SFML(data);
break;
case draw_type::VEL:
HSV_to_SFML(data);
break;
default:
GreyScaleMatrixToSFML(data);
}
window.display();
}

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

auto getRenderWindow() -> sf::RenderWindow& { return window; }

constexpr auto grey_scale(float value) -> sf::Uint8
{
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 = 0uL; i < AXIS_SIZE + 2uL; i++) {
for (size_t j = 0uL; j < AXIS_SIZE + 2uL; j++) {

auto xpos = static_cast<float>(j * CELL_SIZE);
auto ypos = static_cast<float>(i * CELL_SIZE);

sf::RectangleShape pixel(sf::Vector2f(CELL_SIZE, CELL_SIZE));
pixel.setPosition(xpos, ypos);

auto value = grey_scale(data.at(IX(i, j)));

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

window.draw(pixel);
}
}
}

auto HSV_to_SFML(std::array<float, BUFFER_SIZE>& data) -> void
{
for (size_t i = 0uL; i < AXIS_SIZE + 2uL; i++) {
for (size_t j = 0uL; j < AXIS_SIZE + 2uL; j++) {

auto xpos = static_cast<float>(j * CELL_SIZE);
auto ypos = static_cast<float>(i * CELL_SIZE);

sf::RectangleShape pixel(sf::Vector2f(CELL_SIZE, CELL_SIZE));
pixel.setPosition(xpos, ypos);

auto value = data.at(IX(i, j));
auto color = HSV_to_RGB(value * 360.0f, 1.0f, 1.0f); // Assuming value is in the range [0, 1]

pixel.setFillColor(color);

window.draw(pixel);
}
}
}

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));
}
};
7 changes: 3 additions & 4 deletions headers/matrix_change.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <fmt/core.h>

#include <array>
#include <cstddef>
#include <iostream>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -68,7 +67,7 @@ class DensitySolver {
// _M_add_source();
swap(m_x0, m_x);
_M_diffuse();
// test_display();
test_display();
swap(m_x0, m_x);
_M_advect();
}
Expand All @@ -77,8 +76,8 @@ class DensitySolver {
{
fmt::println("Size {}", static_cast<int>(m_x.size()));

for (int i = 0; i < AXIS_SIZE + 2; ++i) {
for (int j = 0; j < AXIS_SIZE + 2; ++j) {
for (size_t i = 0; i < AXIS_SIZE + 2; ++i) {
for (size_t j = 0; j < AXIS_SIZE + 2; ++j) {
fmt::println("{}:{} ", m_x[IX(i, j)], IX(i, j));
}

Expand Down
3 changes: 0 additions & 3 deletions headers/utils.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
#pragma once

#include "const.hpp"
#include "utils.hpp"

#include <concepts>
#include <cstddef>
#include <type_traits>

constexpr auto IX(size_t i, size_t j) -> size_t { return (i + (AXIS_SIZE + 2) * j); }

Expand Down
70 changes: 4 additions & 66 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,53 +13,9 @@
using std::size_t;
using ms = std::chrono::milliseconds;

constexpr auto grey_scale(float value) -> sf::Uint8
{
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(sf::RenderWindow& window, std::array<float, BUFFER_SIZE> const& densityArray) -> void
{
for (auto i { 0uL }; i < AXIS_SIZE + 2uL; i++) {
for (auto j { 0uL }; j < AXIS_SIZE + 2uL; j++) {

auto xpos = static_cast<float>(j * CELL_SIZE);
auto ypos = static_cast<float>(i * CELL_SIZE);

sf::RectangleShape pixel(sf::Vector2f(CELL_SIZE, CELL_SIZE));
pixel.setPosition(xpos, ypos);

auto value = grey_scale(densityArray[IX(i, j)]);

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

window.draw(pixel);
}
}
}

auto main() -> int
{

// std::array<float, BUFFER_SIZE> densityArray = {
// 0.2f, 0.8f, 0.5f, 0.3f, 0.7f, 0.1f, 0.4f, 0.6f, 0.9f, 0.2f, 0.6f, 0.3f,
// 0.8f, 0.1f, 0.5f, 0.9f, 0.2f, 0.7f, 0.4f, 0.3f, 0.4f, 0.7f, 0.2f, 0.9f,
// 0.3f, 0.6f, 0.8f, 0.5f, 0.1f, 0.7f, 0.9f, 0.2f, 0.7f, 0.5f, 0.1f, 0.8f,
// 0.3f, 0.6f, 0.4f, 0.2f, 0.5f, 0.6f, 0.1f, 0.7f, 0.4f, 0.2f, 0.9f, 0.8f,
// 0.3f, 0.5f, 0.1f, 0.4f, 0.9f, 0.2f, 0.8f, 0.7f, 0.5f, 0.3f, 0.6f, 0.9f,
// 0.8f, 0.5f, 0.3f, 0.6f, 0.9f, 0.4f, 0.7f, 0.2f, 0.1f, 0.8f, 0.3f, 0.9f,
// 0.6f, 0.4f, 0.2f, 0.5f, 0.1f, 0.8f, 0.7f, 0.4f, 0.7f, 0.1f, 0.4f, 0.8f,
// 0.6f, 0.3f, 0.2f, 0.9f, 0.5f, 0.1f, 0.2f, 0.3f, 0.5f, 0.1f, 0.7f, 0.9f,
// 0.6f, 0.4f, 0.8f, 0.3f};

/*
main tells gui about the fluids, and asks it for events
gui draws the fluids, and tells main about events
Expand All @@ -74,23 +30,12 @@ auto main() -> int
// Create an event manager and pass the GUI window to its constructor
auto my_event_manager = EventManager(fluid_gui);

// Create SFML print manager
// auto printing = sfml_printing();

// Main loop
auto ds = DensitySolver<BUFFER_SIZE>(0.0001f, 0.1f);

// Example of one mouse click event
// ds.add_density(1, 53);

// Example of a mouse drag event
// ds.add_density(1, 53);
// ds.add_density(1, 54);
// ds.add_density(1, 55);
// ds.add_density(1, 56);
// ds.add_density(1, 57);
// ds.add_density(1, 58);
// ds.add_density(1, 59);
// ds.add_density(1, 60);
// ds.add_density(1, 61);

auto event_mouse_click = 0uL;

while (fluid_gui.is_open()) {
Expand All @@ -105,17 +50,10 @@ auto main() -> int
ds.add_density(10, event_mouse_click);
}

// An example of three mouse presses at three individual spots across an
// even velocity grid
// ds.add_density(1, 142);
// ds.add_density(1, 289);
// ds.add_density(1, 435);

fluid_gui.update_display(GreyScaleMatrixToSFML, ds.x());
fluid_gui.update_display(ds.x(), fluid_gui.get_current_draw_type());

std::this_thread::sleep_for(ms(5));

// ds.test_display();
ds.dens_step();
}

Expand Down

0 comments on commit ef80876

Please sign in to comment.