Skip to content

Commit

Permalink
Merge pull request #232 from Saverio976/dev
Browse files Browse the repository at this point in the history
Release 0.5
  • Loading branch information
Saverio976 authored May 11, 2023
2 parents c9af370 + b291c01 commit 8d575be
Show file tree
Hide file tree
Showing 108 changed files with 8,838 additions and 220 deletions.
55 changes: 55 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,23 @@ if (CMAKE_BUILD_TYPE STREQUAL Release)
)
endif()

# OBJ
add_library(Obj SHARED)
set_target_properties(Obj PROPERTIES
CXX_STANDARD 20
PREFIX ""
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/plugins/Entities"
)
if (CMAKE_BUILD_TYPE STREQUAL Release)
set_target_properties(Obj PROPERTIES
UNITY_BUILD true
UNITY_BUILD_MODE BATCH
)
endif()

# --------- Filters plugins ---------

# SSAAx4
add_library(SSAAx4 SHARED)
set_target_properties(SSAAx4 PROPERTIES
CXX_STANDARD 20
Expand All @@ -247,6 +263,20 @@ if (CMAKE_BUILD_TYPE STREQUAL Release)
)
endif()

# BlackAndWhite
add_library(BlackAndWhite SHARED)
set_target_properties(BlackAndWhite PROPERTIES
CXX_STANDARD 20
PREFIX ""
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/plugins/Filters"
)
if (CMAKE_BUILD_TYPE STREQUAL Release)
set_target_properties(BlackAndWhite PROPERTIES
UNITY_BUILD true
UNITY_BUILD_MODE BATCH
)
endif()

# --------- Materials plugins ---------
add_library(PlainMaterial SHARED)
set_target_properties(PlainMaterial PROPERTIES
Expand All @@ -261,6 +291,18 @@ if (CMAKE_BUILD_TYPE STREQUAL Release)
)
endif()

add_library(RefractionMaterial SHARED)
set_target_properties(RefractionMaterial PROPERTIES
CXX_STANDARD 20
PREFIX ""
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/plugins/Materials"
)
if (CMAKE_BUILD_TYPE STREQUAL Release)
set_target_properties(RefractionMaterial PROPERTIES
UNITY_BUILD true
UNITY_BUILD_MODE BATCH
)
endif()

add_library(TransparencyMaterial SHARED)
set_target_properties(TransparencyMaterial PROPERTIES
Expand Down Expand Up @@ -301,6 +343,19 @@ if (CMAKE_BUILD_TYPE STREQUAL Release)
)
endif()

add_library(MirrorMaterial SHARED)
set_target_properties(MirrorMaterial PROPERTIES
CXX_STANDARD 20
PREFIX ""
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/plugins/Materials"
)
if (CMAKE_BUILD_TYPE STREQUAL Release)
set_target_properties(MirrorMaterial PROPERTIES
UNITY_BUILD true
UNITY_BUILD_MODE BATCH
)
endif()

# -----------------------------------
# --------- Main executable ---------

Expand Down
25 changes: 25 additions & 0 deletions PluginsExt/BlackAndWhite/BlackAndWhiteCreator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
** EPITECH PROJECT, 2023
** Raytracer
** File description:
** BlackAndWhiteCreator.cpp
*/

#include "BlackAndWhiteCreator.hpp"
#include "BlackAndWhiteFilter.hpp"

namespace RayTracer::PluginsExt::BlackAndWhite {
BlackAndWhiteCreator::~BlackAndWhiteCreator()
{
for (auto element : _elements) {
delete element;
}
}

Filters::IFilter *BlackAndWhiteCreator::create(const Scenes::ISetting &config, ILogger &logger)
{
BlackAndWhiteFilter *element = new BlackAndWhiteFilter(config, logger);
_elements.push_back(element);
return element;
}
}
30 changes: 30 additions & 0 deletions PluginsExt/BlackAndWhite/BlackAndWhiteCreator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
** EPITECH PROJECT, 2023
** Raytracer
** File description:
** BlackAndWhiteCreator.hpp
*/

#ifndef SPHERE_CREATOR_HPP_
#define SPHERE_CREATOR_HPP_

#include <vector>
#include <memory>
#include "IFilter.hpp"
#include "IFilterCreator.hpp"
#include "ILogger.hpp"
#include "ISetting.hpp"
#include "BlackAndWhiteFilter.hpp"

namespace RayTracer::PluginsExt::BlackAndWhite {
class BlackAndWhiteCreator : public Plugins::Filters::IFilterCreator {
public:
~BlackAndWhiteCreator();
Filters::IFilter *create(const Scenes::ISetting &config, ILogger &logger) final;

private:
std::vector<BlackAndWhiteFilter *> _elements;
};
}

#endif
123 changes: 123 additions & 0 deletions PluginsExt/BlackAndWhite/BlackAndWhiteFilter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@

/*
** EPITECH PROJECT, 2023
** Raytracer
** File description:
** BlackAndWhileFilter.hpp
*/

#include <algorithm>
#include <exception>
#include <functional>
#include <future>
#include <map>
#include <stdexcept>
#include <string>
#include <vector>
#include "Color.hpp"
#include "ILogger.hpp"
#include "IFilter.hpp"
#include "ISetting.hpp"
#include "Image.hpp"
#include "BlackAndWhiteFilter.hpp"
#include "Vector2i.hpp"

namespace RayTracer::PluginsExt::BlackAndWhite {
BlackAndWhiteFilter::BlackAndWhiteFilter(const Scenes::ISetting &config, ILogger &logger):
_logger(logger),
_method(*config.get("method"))
{
std::vector<std::string> availibleModes = {
"R", "G", "B", "Max", "Min", "Average"
};
try {
_maxThread = static_cast<int>(*config.get("maxThreads"));
_maxThread = (_maxThread == -1) ? std::thread::hardware_concurrency() : _maxThread;
} catch (const Scenes::ISetting::IParsingException &e) {
_maxThread = std::thread::hardware_concurrency();
} catch (const Scenes::ISetting::ITypeException &e) {
_maxThread = std::thread::hardware_concurrency();
}
_maxThread = (_maxThread <= 0) ? 1 : _maxThread;
_maxThread = std::thread::hardware_concurrency();
_logger.info("BlackAndWhile Max threads : " + std::to_string(_maxThread));
if (std::find(availibleModes.begin(), availibleModes.end(), _method) == availibleModes.end()) {
std::string message = "BlackAndWhile method " + _method + " is not available (choose between:";
for (auto it : availibleModes) {
message += " " + it;
}
message += ")";
_logger.fatal(message);
throw std::runtime_error(message);
}
}

void BlackAndWhiteFilter::waitOnePlace()
{
while (_futures.size() >= _maxThread) {
for (auto it = _futures.begin() ; it != _futures.end(); it++) {
if ((*it).wait_for(std::chrono::milliseconds(1)) == std::future_status::ready) {
_futures.erase(it);
break;
}
}
}
}

void BlackAndWhiteFilter::waitAllFinisehd()
{
while (_futures.size() > 0) {
for (auto it = _futures.begin() ; it != _futures.end(); it++) {
if ((*it).wait_for(std::chrono::milliseconds(1)) == std::future_status::ready) {
_futures.erase(it);
break;
}
}
}
}

void BlackAndWhiteFilter::apply(Images::Image &image)
{
_logger.info("Applying BlackAndWhile...");

for (int x = 0; x < image.getSize().getX(); x++) {
this->waitOnePlace();
std::string method = _method;
_futures.push_back(std::async(std::launch::async, [&image, x, method]() {
for (int y = 0; y < image.getSize().getY(); y++) {
image[y][x] = BlackAndWhiteFilter::getColorsMean(image[y][x], method);
}
}));
}
this->waitAllFinisehd();
_logger.info("BlackAndWhile applied.");
}

Images::Color BlackAndWhiteFilter::getColorsMean(const Images::Color &color, const std::string &method)
{
static std::map<std::string, std::function<Images::Color(const Images::Color &)>> _methods = {
{"R", [](const Images::Color &color) {
return Images::Color(color[Images::Color::Types::RED], color[Images::Color::Types::RED], color[Images::Color::Types::RED], color[Images::Color::Types::ALPHA]);
}},
{"G", [](const Images::Color &color) {
return Images::Color(color[Images::Color::Types::GREEN], color[Images::Color::Types::GREEN], color[Images::Color::Types::GREEN], color[Images::Color::Types::ALPHA]);
}},
{"B", [](const Images::Color &color) {
return Images::Color(color[Images::Color::Types::BLUE], color[Images::Color::Types::BLUE], color[Images::Color::Types::BLUE], color[Images::Color::Types::ALPHA]);
}},
{"Max", [](const Images::Color &color) {
double max = std::max(std::max(color[Images::Color::Types::RED], color[Images::Color::Types::GREEN]), color[Images::Color::Types::BLUE]);
return Images::Color(max, max, max, color[Images::Color::Types::ALPHA]);
}},
{"Min", [](const Images::Color &color) {
double min = std::min(std::min(color[Images::Color::Types::RED], color[Images::Color::Types::GREEN]), color[Images::Color::Types::BLUE]);
return Images::Color(min, min, min, color[Images::Color::Types::ALPHA]);
}},
{"Average", [](const Images::Color &color) {
double sum = color[Images::Color::Types::RED] + color[Images::Color::Types::GREEN] + color[Images::Color::Types::BLUE];
return Images::Color(sum / 3, sum / 3, sum / 3, color[Images::Color::Types::ALPHA]);
}}
};
return _methods[method](color);
}
}
50 changes: 50 additions & 0 deletions PluginsExt/BlackAndWhite/BlackAndWhiteFilter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
** EPITECH PROJECT, 2023
** Raytracer
** File description:
** BlackAndWhiteFilter.hpp
*/

#ifndef BASICENTITY_HPP_
#define BASICENTITY_HPP_

#include <future>
#include <string>
#include <vector>
#include "Color.hpp"
#include "ILogger.hpp"
#include "IFilter.hpp"
#include "ISetting.hpp"
#include "Image.hpp"

namespace RayTracer::PluginsExt::BlackAndWhite {
class BlackAndWhiteFilter : public Filters::IFilter {
public:
BlackAndWhiteFilter(const Scenes::ISetting &config, ILogger &logger);
void apply(Images::Image &image) final;

private:
/**
* @brief Wait one place in the vector of futures
*/
void waitOnePlace();
/**
* @brief Wait all places in the vector of futures
*/
void waitAllFinisehd();
/**
* @brief Mean of the colors
*
* @param colors The colors to mean
*
* @return The mean
*/
static Images::Color getColorsMean(const Images::Color &color, const std::string &method);
ILogger &_logger;
int _maxThread;
std::vector<std::future<void>> _futures;
std::string _method;
};
}

#endif
7 changes: 7 additions & 0 deletions PluginsExt/BlackAndWhite/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.13)

target_sources(BlackAndWhite PRIVATE
EntryPoint.cpp
BlackAndWhiteFilter.cpp
BlackAndWhiteCreator.cpp
)
23 changes: 23 additions & 0 deletions PluginsExt/BlackAndWhite/EntryPoint.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
** EPITECH PROJECT, 2023
** raytracer
** File description:
** EntryPoint.cpp
*/

#include <vector>
#include "BlackAndWhiteFilter.hpp"
#include "Api.hpp"
#include "BlackAndWhiteCreator.hpp"

extern "C" {
void *getCreator(void)
{
return new RayTracer::PluginsExt::BlackAndWhite::BlackAndWhiteCreator();
}

void deleteCreator(void *creator)
{
delete static_cast<RayTracer::PluginsExt::BlackAndWhite::BlackAndWhiteCreator *>(creator);
}
}
4 changes: 4 additions & 0 deletions PluginsExt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ add_subdirectory(Cylinder)
add_subdirectory(LimitedCylinder)
add_subdirectory(AntiAliasing)
add_subdirectory(PlainMaterial)
add_subdirectory(RefractionMaterial)
add_subdirectory(TransparencyMaterial)
add_subdirectory(Triangle)
add_subdirectory(Obj)
add_subdirectory(ChessBoardMaterial)
add_subdirectory(ZebraMaterial)
add_subdirectory(MirrorMaterial)
add_subdirectory(Torus)
add_subdirectory(BlackAndWhite)
7 changes: 7 additions & 0 deletions PluginsExt/MirrorMaterial/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.13)

target_sources(MirrorMaterial PRIVATE
MirrorMaterial.cpp
MirrorCreator.cpp
EntryPoint.cpp
)
22 changes: 22 additions & 0 deletions PluginsExt/MirrorMaterial/EntryPoint.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
** EPITECH PROJECT, 2023
** raytracer
** File description:
** EntryPoint.cpp
*/

#include <vector>
#include "Api.hpp"
#include "MirrorCreator.hpp"

extern "C" {
void *getCreator(void)
{
return new RayTracer::PluginsExt::Mirror::MirrorCreator();
}

void deleteCreator(void *creator)
{
delete static_cast<RayTracer::PluginsExt::Mirror::MirrorCreator *>(creator);
}
}
Loading

0 comments on commit 8d575be

Please sign in to comment.