From f7d5b04e07a874903969d8c55b78ab727d6a1769 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sat, 14 May 2016 23:36:50 +0200 Subject: Implement basic multi threading --- CMakeLists.txt | 2 +- example.cc | 27 ++++++++++++++++++++++----- src/color.cc | 19 ------------------- src/color.h | 26 +++++++++++++------------- 4 files changed, 36 insertions(+), 38 deletions(-) delete mode 100644 src/color.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 46df0da..ccbfde7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,11 +14,11 @@ add_executable( example example.cc src/imgen.cc - src/color.cc src/ppm_pixel_stream.cc ) target_link_libraries( example m + pthread ) diff --git a/example.cc b/example.cc index c60ee6a..d815e07 100644 --- a/example.cc +++ b/example.cc @@ -2,8 +2,9 @@ #include #include +#include -int minkowski_metric(float p, int refX, int refY, int x, int y) { +int minkowski_metric(double p, int refX, int refY, int x, int y) { return static_cast(std::nearbyint( std::pow( std::pow(std::abs(refX - x), p) + std::pow(std::abs(refY - y), p), @@ -20,8 +21,12 @@ int euclidean_metric(int refX, int refY, int x, int y) { return minkowski_metric(2, refX, refY, x, y); } -int main(int, char*[]) { - std::array ref{ +void generate_minkowski_voronoi( + const double lower, + const double upper, + const double epsilon +) { + constexpr std::array ref{ imgen::colored_vector{ 0, 0, imgen::red() }, imgen::colored_vector{ 240, 200, imgen::green() }, imgen::colored_vector{-100, 230, imgen::blue() }, @@ -33,9 +38,9 @@ int main(int, char*[]) { imgen::colored_vector{-240, -20, imgen::olive() } }; - for ( float p = 0.6; p < 2.0; p = p + 0.005 ) { + for ( double p = lower; p < upper; p = p + epsilon ) { imgen::write_ppm( - "vonoroi_" + std::to_string(p) + ".ppm", + "voronoi_" + std::to_string(p) + ".ppm", 512, 512, [&ref, p](std::ptrdiff_t x, std::ptrdiff_t y) -> imgen::color { @@ -66,3 +71,15 @@ int main(int, char*[]) { ); } } + +int main(int, char*[]) { + std::thread worker1([](){ generate_minkowski_voronoi(0.8, 1.1, 0.002); }); + std::thread worker2([](){ generate_minkowski_voronoi(1.102, 1.4, 0.002); }); + std::thread worker3([](){ generate_minkowski_voronoi(1.402, 1.7, 0.002); }); + std::thread worker4([](){ generate_minkowski_voronoi(1.702, 2.0, 0.002); }); + + worker1.join(); + worker2.join(); + worker3.join(); + worker4.join(); +} diff --git a/src/color.cc b/src/color.cc deleted file mode 100644 index 9af409a..0000000 --- a/src/color.cc +++ /dev/null @@ -1,19 +0,0 @@ -#include "color.h" - -namespace imgen { - -color red() { return color{255, 0, 0}; } -color lime() { return color{ 0, 255, 0}; } -color blue() { return color{ 0, 0, 255}; } -color yellow() { return color{255, 255, 0}; } -color cyan() { return color{ 0, 255, 255}; } -color magenta() { return color{255, 0, 255}; } -color silver() { return color{192, 192, 192}; } -color maroon() { return color{128, 0, 0}; } -color olive() { return color{128, 128, 0}; } -color green() { return color{ 0, 128, 0}; } -color purple() { return color{128, 0, 128}; } -color teal() { return color{ 0, 128, 128}; } -color navy() { return color{ 0, 0, 128}; } - -} diff --git a/src/color.h b/src/color.h index 2c35465..514c739 100644 --- a/src/color.h +++ b/src/color.h @@ -7,18 +7,18 @@ namespace imgen { using color = std::tuple; -color red(); -color lime(); -color blue(); -color yellow(); -color cyan(); -color magenta(); -color silver(); -color maroon(); -color olive(); -color green(); -color purple(); -color teal(); -color navy(); +constexpr color red() { return color{255, 0, 0}; } +constexpr color lime() { return color{ 0, 255, 0}; } +constexpr color blue() { return color{ 0, 0, 255}; } +constexpr color yellow() { return color{255, 255, 0}; } +constexpr color cyan() { return color{ 0, 255, 255}; } +constexpr color magenta() { return color{255, 0, 255}; } +constexpr color silver() { return color{192, 192, 192}; } +constexpr color maroon() { return color{128, 0, 0}; } +constexpr color olive() { return color{128, 128, 0}; } +constexpr color green() { return color{ 0, 128, 0}; } +constexpr color purple() { return color{128, 0, 128}; } +constexpr color teal() { return color{ 0, 128, 128}; } +constexpr color navy() { return color{ 0, 0, 128}; } } -- cgit v1.2.3