From 696cd7bd77a6f557d759be67ae864a55218e5c44 Mon Sep 17 00:00:00 2001
From: Yoel <s73017@beuth-hochschule.de>
Date: Wed, 2 Sep 2020 15:05:57 +0200
Subject: [PATCH] Implemented multithreading in setPixels-  and
 raytracing-method

---
 RayTracer/sampling/Image.cpp | 31 ++++++++++++++++++++++++-------
 RayTracer/sampling/Image.h   |  9 ++++++---
 2 files changed, 30 insertions(+), 10 deletions(-)

diff --git a/RayTracer/sampling/Image.cpp b/RayTracer/sampling/Image.cpp
index 09d0930..8655512 100644
--- a/RayTracer/sampling/Image.cpp
+++ b/RayTracer/sampling/Image.cpp
@@ -10,15 +10,32 @@ Image::Image(int width, int height) : width(width), height(height) {
 		vec.insert(vec.end(), color);
 	}
 }
+/*
+void Image::setPixels(std::shared_ptr<Sampler> sampler) {
+    Threadpool tp(4);
+    for (int x = 0; x != width; x++) {
+        for (int y = 0; y != height; y++) {
+            vec[width * y + x] = sampler->color(x, y);
+        }
+    }
+} */
 void Image::setPixel(int x, int y, Vec3 color) {
 	vec[width * y + x] = color;
 }
-void Image::setPixels(std::shared_ptr<Sampler> sampler) {
+void Image::setPixels(size_t threadcount, std::shared_ptr<Sampler> sampler) {
+	Threadpool tp(threadcount);
 	for (int x = 0; x != width; x++) {
 		for (int y = 0; y != height; y++) {
-			setPixel(x, y, sampler->color(x, y));
+			tp.queueTask(std::bind([this, x, y, sampler]() {
+				this->setPixelsTask(x, y, sampler);
+			}));
 		}
 	}
+	// std::cout << "Done queing" << std::endl;
+}
+
+void Image::setPixelsTask(int x, int y, std::shared_ptr<Sampler> sampler) {
+	setPixel(x, y, sampler->color(x, y));
 }
 Vec3 Image::operator[](const std::array<int, 2>& i) const {
 	return vec[width * i[1] + i[0]];
@@ -26,13 +43,13 @@ Vec3 Image::operator[](const std::array<int, 2>& i) const {
 Vec3& Image::operator[](const std::array<int, 2>& i) {
 	return vec[width * i[1] + i[0]];
 }
-Image raytrace(const cam::CamObs& cam, const std::shared_ptr<Sampler>& sampler,
-               size_t n) {
+Image raytrace(size_t threadcount, const cam::CamObs& cam,
+               const std::shared_ptr<Sampler>& sampler, size_t n) {
 	Image result(cam.width, cam.height);
 
-	result.setPixels(
-	    std::make_shared<StratifiedSampler>(StratifiedSampler(sampler, n))
-	    // sampler
+	result.setPixels(threadcount, std::make_shared<StratifiedSampler>(
+	                                  StratifiedSampler(sampler, n))
+	                 // sampler
 	);
 	return result;
 }
diff --git a/RayTracer/sampling/Image.h b/RayTracer/sampling/Image.h
index 673026d..1e99f19 100644
--- a/RayTracer/sampling/Image.h
+++ b/RayTracer/sampling/Image.h
@@ -12,7 +12,7 @@ class Image {
 	Image(int width, int height);
 
 	void setPixel(int x, int y, Vec3 color);
-	void setPixels(std::shared_ptr<Sampler> sampler);
+	void setPixels(size_t threadcount, std::shared_ptr<Sampler> sampler);
 
 	Vec3 operator[](const std::array<int, 2>& i) const;  // int x, int y
 	Vec3& operator[](const std::array<int, 2>& i);
@@ -20,11 +20,14 @@ class Image {
 	const int width;
 	const int height;
 
+   protected:
+	void setPixelsTask(int x, int y, std::shared_ptr<Sampler> sampler);
+
    private:
 	std::vector<Vec3> vec;
 };
 
-Image raytrace(const cam::CamObs& cam, const std::shared_ptr<Sampler>& sampler,
-               size_t n);
+Image raytrace(size_t threadcount, const cam::CamObs& cam,
+               const std::shared_ptr<Sampler>& sampler, size_t n);
 
 }  // namespace util
\ No newline at end of file
-- 
GitLab