diff --git a/.vscode/settings.json b/.vscode/settings.json index d5acdbb..4edbc46 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,5 +3,76 @@ "cSpell.words": [ "NADAL", "Raytracer" - ] + ], + "files.associations": { + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "any": "cpp", + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "bitset": "cpp", + "chrono": "cpp", + "compare": "cpp", + "concepts": "cpp", + "condition_variable": "cpp", + "cstdint": "cpp", + "deque": "cpp", + "forward_list": "cpp", + "list": "cpp", + "map": "cpp", + "set": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "unordered_set": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "random": "cpp", + "ratio": "cpp", + "regex": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "fstream": "cpp", + "future": "cpp", + "initializer_list": "cpp", + "iomanip": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "mutex": "cpp", + "new": "cpp", + "numbers": "cpp", + "ostream": "cpp", + "semaphore": "cpp", + "shared_mutex": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "stop_token": "cpp", + "streambuf": "cpp", + "thread": "cpp", + "cinttypes": "cpp", + "typeinfo": "cpp", + "variant": "cpp" + } } \ No newline at end of file diff --git a/raytracing/CMakeLists.txt b/raytracing/CMakeLists.txt index 06f797d..171e19e 100644 --- a/raytracing/CMakeLists.txt +++ b/raytracing/CMakeLists.txt @@ -5,8 +5,11 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) project(raytracing) -add_library(raytracing +add_library(raytracing + + src/common.cpp src/tuple.cpp + src/color.cpp ) target_include_directories(${PROJECT_NAME} diff --git a/raytracing/include/raytracing.h b/raytracing/include/raytracing.h index f112444..d989f16 100644 --- a/raytracing/include/raytracing.h +++ b/raytracing/include/raytracing.h @@ -25,4 +25,6 @@ #pragma once +#include "common.h" #include "tuple.h" +#include "color.h" diff --git a/raytracing/src/color.cpp b/raytracing/src/color.cpp new file mode 100644 index 0000000..c7b34c4 --- /dev/null +++ b/raytracing/src/color.cpp @@ -0,0 +1,132 @@ +/*! + * color.cpp + * + * Copyright (c) 2024, NADAL Jean-Baptiste. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + * + * @Author: NADAL Jean-Baptiste + * @Date: 30/01/2024 + * + */ + +// This is an independent project of an individual developer. Dear PVS-Studio, please check it. +// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com + +/* ------------------------------------------------------------------------- */ + +#include "common.h" +#include "color.h" + +using namespace Raytracer; + +#include + +/* ------------------------------------------------------------------------- */ + +Color::Color(void) : m_red(0), m_green(0), m_blue(0) +{ + //printf("%s red: %f green: %f, blue: %f\n", __PRETTY_FUNCTION__, m_red, m_green, m_blue); +} + +/* ------------------------------------------------------------------------- */ + +Color::Color(const Color &a_copy) : m_red(a_copy.m_red), m_green(a_copy.m_green), m_blue(a_copy.m_blue) +{ +} + +/* ------------------------------------------------------------------------- */ + +Color::Color(double a_red, double a_green, double a_blue) : m_red(a_red), m_green(a_green), m_blue(a_blue) +{ +} + +/* ------------------------------------------------------------------------- */ + +bool Color::operator==(const Color &a_color) const +{ + if (double_equal(m_red, a_color.m_red) && double_equal(m_green, a_color.m_green) && double_equal(m_blue, a_color.m_blue)) + { + return true; + } + + return false; +} + +/* ------------------------------------------------------------------------- */ + +const Color &Color::operator=(const Color &a_color) +{ + m_red = a_color.m_red; + m_green = a_color.m_green; + m_blue = a_color.m_blue; + + return *this; +} + +/* ------------------------------------------------------------------------- */ + +const Color &Color::operator+(const Color &a_color) +{ + m_red += a_color.m_red; + m_green += a_color.m_green; + m_blue += a_color.m_blue; + + return *this; +} + +/* ------------------------------------------------------------------------- */ + +const Color &Color::operator-(const Color &a_color) +{ + m_red -= a_color.m_red; + m_green -= a_color.m_green; + m_blue -= a_color.m_blue; + + return *this; +} + +/* ------------------------------------------------------------------------- */ + +const Color &Color::operator*(double a_scalar) +{ + m_red *= a_scalar; + m_green *= a_scalar; + m_blue *= a_scalar; + + return *this; +} + +/* ------------------------------------------------------------------------- */ + +double Color::red(void) const +{ + return m_red; +} + +/* ------------------------------------------------------------------------- */ + +double Color::green(void) const +{ + return m_green; +} + +/* ------------------------------------------------------------------------- */ + +double Color::blue(void) const +{ + return m_blue; +} diff --git a/raytracing/src/color.h b/raytracing/src/color.h new file mode 100644 index 0000000..342e6f3 --- /dev/null +++ b/raytracing/src/color.h @@ -0,0 +1,56 @@ +/*! + * color.h + * + * Copyright (c) 2024, NADAL Jean-Baptiste. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + * + * @Author: NADAL Jean-Baptiste + * @Date: 30/01/2024 + * + */ + +#ifndef _RAYTRACER_COLOR_H +#define _RAYTRACER_COLOR_H + +namespace Raytracer +{ + class Color + { + public: + Color(void); + Color(const Color &a_copy); + Color(double a_red, double a_green, double a_blue); + + bool operator==(const Color &a_color) const; + const Color &operator=(const Color &an_other); + const Color &operator+(const Color &a_color); + const Color &operator-(const Color &a_color); + const Color &operator*(double a_scalar); + + // Access + double red(void) const; + double green(void) const; + double blue(void) const; + + private: + double m_red; + double m_green; + double m_blue; + }; +}; + +#endif diff --git a/raytracing/src/common.cpp b/raytracing/src/common.cpp new file mode 100644 index 0000000..bb7ebf3 --- /dev/null +++ b/raytracing/src/common.cpp @@ -0,0 +1,47 @@ +/*! + * common.cpp + * + * Copyright (c) 2024, NADAL Jean-Baptiste. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + * + * @Author: NADAL Jean-Baptiste + * @Date: 30/01/2024 + * + */ + +// This is an independent project of an individual developer. Dear PVS-Studio, please check it. +// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com + +/* ------------------------------------------------------------------------- */ + +#include + +#include "common.h" + +#define kEpsilon 0.00001 + +/* ------------------------------------------------------------------------- */ + +bool Raytracer::double_equal(double a, double b) +{ + if ((std::abs((a) - (b)) < kEpsilon)) + { + return true; + } + + return false; +} diff --git a/raytracing/src/common.h b/raytracing/src/common.h new file mode 100644 index 0000000..78a534e --- /dev/null +++ b/raytracing/src/common.h @@ -0,0 +1,34 @@ +/*! + * common.h + * + * Copyright (c) 2024, NADAL Jean-Baptiste. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + * + * @Author: NADAL Jean-Baptiste + * @Date: 30/01/2024 + * + */ + +#ifndef _RAYTRACER_COMMON_H +#define _RAYTRACER_COMMON_H + +namespace Raytracer +{ + bool double_equal(double a, double b); +}; + +#endif /* _RAYTRACER_COMMON_H */ diff --git a/raytracing/src/tuple.cpp b/raytracing/src/tuple.cpp index 8b457dc..6d71e17 100644 --- a/raytracing/src/tuple.cpp +++ b/raytracing/src/tuple.cpp @@ -31,9 +31,9 @@ #include #include +#include "common.h" #include "tuple.h" -#define kEpsilon 0.00001 #define square(a) (a) * (a) using namespace Raytracer; @@ -236,15 +236,3 @@ Tuple Tuple::cross(const Tuple &a_tuple) m_x * a_tuple.m_y - m_y * a_tuple.m_x ); } - -/* ------------------------------------------------------------------------- */ - -bool Tuple::double_equal(double a, double b) const -{ - if ((std::abs((a) - (b)) < kEpsilon)) - { - return true; - } - - return false; -} diff --git a/raytracing/src/tuple.h b/raytracing/src/tuple.h index 9b0f65c..6d51a96 100644 --- a/raytracing/src/tuple.h +++ b/raytracing/src/tuple.h @@ -69,14 +69,11 @@ namespace Raytracer Tuple cross(const Tuple &a_tuple); private: - bool double_equal(double a, double b) const; - double m_x; double m_y; double m_z; double m_w; }; - }; #endif /* _RAYTRACER_TUPLE_H */ diff --git a/tests/chapitre01_tuples.cpp b/tests/01_tuples.cpp similarity index 99% rename from tests/chapitre01_tuples.cpp rename to tests/01_tuples.cpp index d0a0ce4..3a29afe 100644 --- a/tests/chapitre01_tuples.cpp +++ b/tests/01_tuples.cpp @@ -1,5 +1,5 @@ /*! - * chapitre-01_tests.cpp + * 01_tuples.cpp * * Copyright (c) 2015-2024, NADAL Jean-Baptiste. All rights reserved. * @@ -19,7 +19,7 @@ * MA 02110-1301 USA * * @Author: NADAL Jean-Baptiste - * @Date: 07/02/2021 + * @Date: 29/01/2024 * */ diff --git a/tests/02_colors.cpp b/tests/02_colors.cpp new file mode 100644 index 0000000..6b4a662 --- /dev/null +++ b/tests/02_colors.cpp @@ -0,0 +1,100 @@ +/*! + * 01_colors.cpp + * + * Copyright (c) 2015-2024, NADAL Jean-Baptiste. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + * + * @Author: NADAL Jean-Baptiste + * @Date: 30/01/2024 + * + */ + +/*---------------------------------------------------------------------------*/ + +#include + +#include "color.h" + +using namespace Raytracer; + +/* ------------------------------------------------------------------------- */ + +TEST_CASE("colors are (red,green,blue) tuples", "[Colors]") +{ + Color c(-0.5, 0.4, 1.7); + + REQUIRE(c.red() == -0.5); + REQUIRE(c.green() == 0.4); + REQUIRE(c.blue() == 1.7); +} + +/* ------------------------------------------------------------------------- */ + +TEST_CASE("Colors could be copied", "[Colors]") +{ + Color c1(-0.5, 0.4, 1.7); + Color c2; + + c2 = c1; + + REQUIRE(c2.red() == -0.5); + REQUIRE(c2.green() == 0.4); + REQUIRE(c2.blue() == 1.7); +} + +/* ------------------------------------------------------------------------- */ + +TEST_CASE("Adding colors", "[Colors]") +{ + Color c1(0.9, 0.6, 0.75); + Color c2(0.7, 0.1, 0.25); + + //Color c3 = c1 + c2; + + REQUIRE((c1 + c2) == Color(1.6, 0.7, 1.0)); +} + +/* ------------------------------------------------------------------------- */ + +TEST_CASE("Adding colors without modify c1", "[Colors]") +{ + Color c1(0.9, 0.6, 0.75); + Color c2(0.7, 0.1, 0.25); + + Color c3 = c1 + c2; + + REQUIRE((c1 + c2) == Color(1.6, 0.7, 1.0)); +} + +/* ------------------------------------------------------------------------- */ + +TEST_CASE("Subtracting colors", "[Colors]") +{ + Color c1(0.9, 0.6, 0.75); + Color c2(0.7, 0.1, 0.25); + + REQUIRE((c1 - c2) == Color(0.2, 0.5, 0.5)); +} + +/* ------------------------------------------------------------------------- */ + +TEST_CASE("Multiplying a color by a scalar", "[Colors]") +{ + Color c(0.2, 0.3, 0.4); + + REQUIRE(c * 2 == Color(0.4, 0.6, 0.8)); +} diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 34b0dbf..1580d17 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -12,7 +12,8 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) add_executable(main_test main_test.cpp - chapitre01_tuples.cpp + 01_tuples.cpp + 02_colors.cpp ) include_directories("${CMAKE_SOURCE_DIR}/tests")