diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..c4fdf5b --- /dev/null +++ b/.clang-format @@ -0,0 +1,32 @@ +--- +# We'll use defaults from the LLVM style, but with 4 columns indentation. +BasedOnStyle: LLVM +IndentWidth: 4 +# use \n instead of \r\n +UseCRLF: true +# spaces, not tabs! +UseTab: Never +--- +Language: Cpp +Standard: c++17 +AccessModifierOffset: -4 +# Force pointers to the type for C++. +DerivePointerAlignment: false +PointerAlignment: Left +# Use 100 columns +ColumnLimit: 100 +AlignConsecutiveStyle: Consecutive +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortBlocksOnASingleLine: Never +AlwaysBreakAfterReturnType: None +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: None +AllowShortIfStatementsOnASingleLine: Never +SplitEmptyFunction: true +BreakBeforeBraces: Allman +SeparateDefinitionBlocks: Always +NamespaceIndentation: All +IndentAccessModifiers: false +ReferenceAlignment: Right +BreakConstructorInitializers: AfterColon +... diff --git a/.vscode/settings.json b/.vscode/settings.json index 4edbc46..22e610b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -74,5 +74,6 @@ "cinttypes": "cpp", "typeinfo": "cpp", "variant": "cpp" - } + }, + "editor.formatOnSave": true } \ No newline at end of file diff --git a/raytracing/CMakeLists.txt b/raytracing/CMakeLists.txt index 704a4a0..e3a14aa 100644 --- a/raytracing/CMakeLists.txt +++ b/raytracing/CMakeLists.txt @@ -11,6 +11,7 @@ add_library(raytracing src/tuple.cpp src/color.cpp src/canvas.cpp + src/matrix.cpp ) target_include_directories(${PROJECT_NAME} diff --git a/raytracing/include/raytracing.h b/raytracing/include/raytracing.h index d989f16..39d8fb8 100644 --- a/raytracing/include/raytracing.h +++ b/raytracing/include/raytracing.h @@ -25,6 +25,7 @@ #pragma once -#include "common.h" -#include "tuple.h" #include "color.h" +#include "common.h" +#include "matrix.h" +#include "tuple.h" diff --git a/raytracing/src/canvas.cpp b/raytracing/src/canvas.cpp index eeb5a4b..e0bf8da 100644 --- a/raytracing/src/canvas.cpp +++ b/raytracing/src/canvas.cpp @@ -28,8 +28,8 @@ /* ------------------------------------------------------------------------- */ -#include "common.h" #include "canvas.h" +#include "common.h" using namespace Raytracer; @@ -46,8 +46,12 @@ Canvas::Canvas(uint16_t a_width, uint16_t a_height) : m_width(a_width), m_height m_pixels = std::vector>(m_width, std::vector(m_height)); for (int i = 0; i < m_width; ++i) + { for (int j = 0; j < m_height; ++j) + { m_pixels[i][j] == Color(0, 0, 0); + } + } } /* ------------------------------------------------------------------------- */ diff --git a/raytracing/src/canvas.h b/raytracing/src/canvas.h index ecd0c89..64d560e 100644 --- a/raytracing/src/canvas.h +++ b/raytracing/src/canvas.h @@ -61,6 +61,6 @@ namespace Raytracer uint16_t m_height; std::vector> m_pixels; }; -}; +}; // namespace Raytracer #endif // _RAYTRACER_CANVAS_H diff --git a/raytracing/src/color.cpp b/raytracing/src/color.cpp index 427b049..9327f4a 100644 --- a/raytracing/src/color.cpp +++ b/raytracing/src/color.cpp @@ -31,8 +31,8 @@ #include #include -#include "common.h" #include "color.h" +#include "common.h" using namespace Raytracer; @@ -45,13 +45,15 @@ Color::Color(void) : m_red(0), m_green(0), m_blue(0) /* ------------------------------------------------------------------------- */ -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(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) +Color::Color(double a_red, double a_green, double a_blue) : + m_red(a_red), m_green(a_green), m_blue(a_blue) { } @@ -59,7 +61,8 @@ Color::Color(double a_red, double a_green, double a_blue) : m_red(a_red), m_gree 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)) + 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; } @@ -82,30 +85,21 @@ const Color &Color::operator=(const Color &a_color) const Color Color::operator+(const Color &a_color) const { - return Color( - m_red + a_color.m_red, - m_green + a_color.m_green, - m_blue + a_color.m_blue); + return Color(m_red + a_color.m_red, m_green + a_color.m_green, m_blue + a_color.m_blue); } /* ------------------------------------------------------------------------- */ const Color Color::operator-(const Color &a_color) const { - return Color( - m_red - a_color.m_red, - m_green - a_color.m_green, - m_blue - a_color.m_blue); + return Color(m_red - a_color.m_red, m_green - a_color.m_green, m_blue - a_color.m_blue); } /* ------------------------------------------------------------------------- */ const Color Color::operator*(double a_scalar) const { - return Color( - m_red * a_scalar, - m_green * a_scalar, - m_blue * a_scalar); + return Color(m_red * a_scalar, m_green * a_scalar, m_blue * a_scalar); } /* ------------------------------------------------------------------------- */ diff --git a/raytracing/src/color.h b/raytracing/src/color.h index 308c752..4ec9f84 100644 --- a/raytracing/src/color.h +++ b/raytracing/src/color.h @@ -71,6 +71,6 @@ namespace Raytracer double m_green; double m_blue; }; -}; +}; // namespace Raytracer #endif // _RAYTRACER_COLOR_H diff --git a/raytracing/src/matrix.cpp b/raytracing/src/matrix.cpp new file mode 100644 index 0000000..87d9b71 --- /dev/null +++ b/raytracing/src/matrix.cpp @@ -0,0 +1,55 @@ +/*! + * matrix.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: 01/02/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 + +#include "common.h" +#include "matrix.h" + +using namespace Raytracer; + +/* ------------------------------------------------------------------------- */ + +Matrix::Matrix(void) +{ +} + +/* ------------------------------------------------------------------------- */ + +Matrix::Matrix(const Matrix &a_copy) +{ +} + +/* ------------------------------------------------------------------------- */ + +Matrix::Matrix(uint8_t a_nb_row, uint8_t a_nb_col) +{ +} diff --git a/raytracing/src/matrix.h b/raytracing/src/matrix.h new file mode 100644 index 0000000..78428d0 --- /dev/null +++ b/raytracing/src/matrix.h @@ -0,0 +1,48 @@ +/*! + * matrix.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: 01/02/2024 + * + */ + +#ifndef _RAYTRACER_MATRIX_H +#define _RAYTRACER_MATRIX_H + +/* ------------------------------------------------------------------------- */ + +#include + +/* ------------------------------------------------------------------------- */ + +namespace Raytracer +{ + class Matrix + { + public: + Matrix(void); + Matrix(const Matrix &a_copy); + Matrix(uint8_t a_nb_row, uint8_t a_nb_col); + + private: + }; +}; // namespace Raytracer + +#endif // _RAYTRACER_MATRIX_H diff --git a/raytracing/src/tuple.cpp b/raytracing/src/tuple.cpp index 6a087c4..6639f39 100644 --- a/raytracing/src/tuple.cpp +++ b/raytracing/src/tuple.cpp @@ -46,13 +46,15 @@ Tuple::Tuple(void) : m_x(0.0), m_y(0.0), m_z(0.0), m_w(0.0) /* ------------------------------------------------------------------------- */ -Tuple::Tuple(const Tuple &a_copy) : m_x(a_copy.m_x), m_y(a_copy.m_y), m_z(a_copy.m_z), m_w(a_copy.m_w) +Tuple::Tuple(const Tuple &a_copy) : + m_x(a_copy.m_x), m_y(a_copy.m_y), m_z(a_copy.m_z), m_w(a_copy.m_w) { } /* ------------------------------------------------------------------------- */ -Tuple::Tuple(double a_x, double a_y, double a_z, double a_w) : m_x(a_x), m_y(a_y), m_z(a_z), m_w(a_w) +Tuple::Tuple(double a_x, double a_y, double a_z, double a_w) : + m_x(a_x), m_y(a_y), m_z(a_z), m_w(a_w) { } @@ -60,7 +62,8 @@ Tuple::Tuple(double a_x, double a_y, double a_z, double a_w) : m_x(a_x), m_y(a_y bool Tuple::operator==(const Tuple &an_other) const { - if (double_equal(m_x, an_other.m_x) && double_equal(m_y, an_other.m_y) && double_equal(m_z, an_other.m_z) && double_equal(m_w, an_other.m_w)) + if (double_equal(m_x, an_other.m_x) && double_equal(m_y, an_other.m_y) && + double_equal(m_z, an_other.m_z) && double_equal(m_w, an_other.m_w)) { return true; } @@ -96,48 +99,28 @@ const Tuple &Tuple::operator-(void) const Tuple Tuple::operator+(const Tuple &an_other) const { - return Tuple( - m_x + an_other.m_x, - m_y + an_other.m_y, - m_z + an_other.m_z, - m_w + an_other.m_w - ); + return Tuple(m_x + an_other.m_x, m_y + an_other.m_y, m_z + an_other.m_z, m_w + an_other.m_w); } /* ------------------------------------------------------------------------- */ const Tuple Tuple::operator-(const Tuple &an_other) const { - return Tuple( - m_x - an_other.m_x, - m_y - an_other.m_y, - m_z - an_other.m_z, - m_w - an_other.m_w - ); + return Tuple(m_x - an_other.m_x, m_y - an_other.m_y, m_z - an_other.m_z, m_w - an_other.m_w); } /* ------------------------------------------------------------------------- */ const Tuple Tuple::operator*(double a_scalar) const { - return Tuple( - m_x * a_scalar, - m_y * a_scalar, - m_z * a_scalar, - m_w * a_scalar - ); + return Tuple(m_x * a_scalar, m_y * a_scalar, m_z * a_scalar, m_w * a_scalar); } /* ------------------------------------------------------------------------- */ const Tuple Tuple::operator/(double a_scalar) const { - return Tuple( - m_x / a_scalar, - m_y / a_scalar, - m_z / a_scalar, - m_w / a_scalar - ); + return Tuple(m_x / a_scalar, m_y / a_scalar, m_z / a_scalar, m_w / a_scalar); } /* ------------------------------------------------------------------------- */ @@ -248,8 +231,7 @@ bool Tuple::is_vector(void) double Tuple::magnitude(void) const { - return std::sqrt( - square(m_x) + square(m_y) + square(m_z) + square(m_w)); + return std::sqrt(square(m_x) + square(m_y) + square(m_z) + square(m_w)); } /* ------------------------------------------------------------------------- */ @@ -258,9 +240,7 @@ Tuple Tuple::normalize(void) { double the_magnitude = magnitude(); - return Tuple(m_x / the_magnitude, - m_y / the_magnitude, - m_z / the_magnitude, + return Tuple(m_x / the_magnitude, m_y / the_magnitude, m_z / the_magnitude, m_w / the_magnitude); } @@ -268,18 +248,13 @@ Tuple Tuple::normalize(void) double Tuple::dot(const Tuple &a_tuple) { - return m_x * a_tuple.m_x + - m_y * a_tuple.m_y + - m_z * a_tuple.m_z + - m_w * a_tuple.m_w; + return m_x * a_tuple.m_x + m_y * a_tuple.m_y + m_z * a_tuple.m_z + m_w * a_tuple.m_w; } /* ------------------------------------------------------------------------- */ Tuple Tuple::cross(const Tuple &a_tuple) { - return Vector( - m_y * a_tuple.m_z - m_z * a_tuple.m_y, - m_z * a_tuple.m_x - m_x * a_tuple.m_z, - m_x * a_tuple.m_y - m_y * a_tuple.m_x); + return Vector(m_y * a_tuple.m_z - m_z * a_tuple.m_y, m_z * a_tuple.m_x - m_x * a_tuple.m_z, + m_x * a_tuple.m_y - m_y * a_tuple.m_x); } diff --git a/raytracing/src/tuple.h b/raytracing/src/tuple.h index f23a79d..a01a899 100644 --- a/raytracing/src/tuple.h +++ b/raytracing/src/tuple.h @@ -81,6 +81,6 @@ namespace Raytracer double m_z; double m_w; }; -}; +}; // namespace Raytracer #endif /* _RAYTRACER_TUPLE_H */ diff --git a/tests/01_tuples.cpp b/tests/01_tuples.cpp index 7a8722c..47d60e6 100644 --- a/tests/01_tuples.cpp +++ b/tests/01_tuples.cpp @@ -37,276 +37,276 @@ using namespace Raytracer; TEST_CASE("[Tuple] a tuple with w=1.0 is a point", "[Tuple]") { - Tuple a(4.3, -4.2, 3.1, 1.0); + Tuple a(4.3, -4.2, 3.1, 1.0); - REQUIRE(a.x() == 4.3); - REQUIRE(a.y() == -4.2); - REQUIRE(a.z() == 3.1); - REQUIRE(a.w() == 1.0); + REQUIRE(a.x() == 4.3); + REQUIRE(a.y() == -4.2); + REQUIRE(a.z() == 3.1); + REQUIRE(a.w() == 1.0); - REQUIRE(a.is_point() == true); - REQUIRE(a.is_vector() == false); + REQUIRE(a.is_point() == true); + REQUIRE(a.is_vector() == false); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Tuple] a tuple with w=0 is a vector", "[Tuple]") { - Tuple a(4.3, -4.2, 3.1, 0.0); + Tuple a(4.3, -4.2, 3.1, 0.0); - REQUIRE(a.x() == 4.3); - REQUIRE(a.y() == -4.2); - REQUIRE(a.z() == 3.1); - REQUIRE(a.w() == 0.0); + REQUIRE(a.x() == 4.3); + REQUIRE(a.y() == -4.2); + REQUIRE(a.z() == 3.1); + REQUIRE(a.w() == 0.0); - REQUIRE(a.is_point() == false); + REQUIRE(a.is_point() == false); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Tuple] Tuple could be copy", "[Tuple]") { - Tuple p = Tuple::Point(4, -4, 3); - Tuple n; + Tuple p = Tuple::Point(4, -4, 3); + Tuple n; - n = p; + n = p; - REQUIRE(n == p); + REQUIRE(n == p); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Tuple] Point() creates tuples with w=1", "[Tuple][Point]") { - Tuple p = Tuple::Point(4, -4, 3); + Tuple p = Tuple::Point(4, -4, 3); - REQUIRE(p == Tuple(4, -4, 3, 1)); + REQUIRE(p == Tuple(4, -4, 3, 1)); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Tuple] Vector() creates tuples with w=0", "[Tuple][Vector]") { - Tuple v = Tuple::Vector(4, -4, 3); + Tuple v = Tuple::Vector(4, -4, 3); - REQUIRE(v == Tuple(4, -4, 3, 0)); + REQUIRE(v == Tuple(4, -4, 3, 0)); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Tuple] Adding two tuples", "[Tuple][Operations]") { - Tuple a1(3, -2, 5, 1); - Tuple a2(-2, 3, 1, 0); + Tuple a1(3, -2, 5, 1); + Tuple a2(-2, 3, 1, 0); - REQUIRE((a1 + a2) == Tuple(1, 1, 6, 1)); + REQUIRE((a1 + a2) == Tuple(1, 1, 6, 1)); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Tuple] Adding two tuples without modify a1", "[Tuple][Operations]") { - Tuple a1(3, -2, 5, 1); - Tuple a2(-2, 3, 1, 0); + Tuple a1(3, -2, 5, 1); + Tuple a2(-2, 3, 1, 0); - Tuple a3 = a1 + a2; + Tuple a3 = a1 + a2; - REQUIRE((a1 + a2) == Tuple(1, 1, 6, 1)); + REQUIRE((a1 + a2) == Tuple(1, 1, 6, 1)); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Tuple] Subtracting two points", "[Tuple][Operations]") { - Tuple p1 = Tuple::Point(3, 2, 1); - Tuple p2 = Tuple::Point(5, 6, 7); + Tuple p1 = Tuple::Point(3, 2, 1); + Tuple p2 = Tuple::Point(5, 6, 7); - REQUIRE((p1 - p2) == Tuple::Vector(-2, -4, -6)); + REQUIRE((p1 - p2) == Tuple::Vector(-2, -4, -6)); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Tuple] Subtracting two points without modify p1", "[Tuple][Operations]") { - Tuple p1 = Tuple::Point(3, 2, 1); - Tuple p2 = Tuple::Point(5, 6, 7); + Tuple p1 = Tuple::Point(3, 2, 1); + Tuple p2 = Tuple::Point(5, 6, 7); - Tuple p3 = p1 - p2; + Tuple p3 = p1 - p2; - REQUIRE((p1 - p2) == Tuple::Vector(-2, -4, -6)); + REQUIRE((p1 - p2) == Tuple::Vector(-2, -4, -6)); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Tuple] Subtracting a vector from a point", "[Tuple][Operations]") { - Tuple p = Tuple::Point(3, 2, 1); - Tuple v = Tuple::Vector(5, 6, 7); + Tuple p = Tuple::Point(3, 2, 1); + Tuple v = Tuple::Vector(5, 6, 7); - REQUIRE((p - v) == Tuple::Point(-2, -4, -6)); + REQUIRE((p - v) == Tuple::Point(-2, -4, -6)); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Tuple] Subtracting two vectors", "[Tuple][Operations]") { - Tuple v1 = Tuple::Vector(3, 2, 1); - Tuple v2 = Tuple::Vector(5, 6, 7); + Tuple v1 = Tuple::Vector(3, 2, 1); + Tuple v2 = Tuple::Vector(5, 6, 7); - REQUIRE((v1 - v2) == Tuple::Vector(-2, -4, -6)); + REQUIRE((v1 - v2) == Tuple::Vector(-2, -4, -6)); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Tuple] Subtracting a vector from the zero vector", "[Tuple][Operations]") { - Tuple zero = Tuple::Vector(0, 0, 0); - Tuple v = Tuple::Vector(1, -2, 3); + Tuple zero = Tuple::Vector(0, 0, 0); + Tuple v = Tuple::Vector(1, -2, 3); - REQUIRE((zero - v) == Tuple::Vector(-1, 2, -3)); + REQUIRE((zero - v) == Tuple::Vector(-1, 2, -3)); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Tuple] Negative a tuple", "[Tuple][Operations]") { - Tuple a(1, -2, 3, -4); + Tuple a(1, -2, 3, -4); - REQUIRE(-a == Tuple(-1, 2, -3, 4)); + REQUIRE(-a == Tuple(-1, 2, -3, 4)); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Tuple] Multiplying a tuple by a scalar", "[Tuple][Multiplication]") { - Tuple a(1, -2, 3, -4); + Tuple a(1, -2, 3, -4); - REQUIRE(a * 3.5 == Tuple(3.5, -7, 10.5, -14)); + REQUIRE(a * 3.5 == Tuple(3.5, -7, 10.5, -14)); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Tuple] Multiplying a tuple by a scalar without modify a", "[Tuple][Multiplication]") { - Tuple a(1, -2, 3, -4); + Tuple a(1, -2, 3, -4); - Tuple b = a * 3.5; + Tuple b = a * 3.5; - REQUIRE(a * 3.5 == Tuple(3.5, -7, 10.5, -14)); + REQUIRE(a * 3.5 == Tuple(3.5, -7, 10.5, -14)); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Tuple] Dividing a tuple by a scalar", "[Tuple][Multiplication]") { - Tuple a(1, -2, 3, -4); + Tuple a(1, -2, 3, -4); - REQUIRE(a / 2 == Tuple(0.5, -1, 1.5, -2)); + REQUIRE(a / 2 == Tuple(0.5, -1, 1.5, -2)); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Tuple] Dividing a tuple by a scalar without modify a", "[Tuple][Multiplication]") { - Tuple a(1, -2, 3, -4); + Tuple a(1, -2, 3, -4); - Tuple b = a / 2; + Tuple b = a / 2; - REQUIRE(a / 2 == Tuple(0.5, -1, 1.5, -2)); + REQUIRE(a / 2 == Tuple(0.5, -1, 1.5, -2)); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Tuple] Computing the magnitude of vector(1,0,0)", "[Tuple][Magnitude]") { - Tuple v = Tuple::Vector(1, 0, 0); + Tuple v = Tuple::Vector(1, 0, 0); - REQUIRE(v.magnitude() == 1); + REQUIRE(v.magnitude() == 1); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Tuple] Computing the magnitude of vector(0,1,0)", "[Tuple][Magnitude]") { - Tuple v = Tuple::Vector(0, 1, 0); + Tuple v = Tuple::Vector(0, 1, 0); - REQUIRE(v.magnitude() == 1); + REQUIRE(v.magnitude() == 1); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Tuple] Computing the magnitude of vector(0,0,1)", "[Tuple][Magnitude]") { - Tuple v = Tuple::Vector(0, 0, 1); + Tuple v = Tuple::Vector(0, 0, 1); - REQUIRE(v.magnitude() == 1); + REQUIRE(v.magnitude() == 1); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Tuple] Computing the magnitude of vector(1,2,3)", "[Tuple][Magnitude]") { - Tuple v = Tuple::Vector(1, 2, 3); + Tuple v = Tuple::Vector(1, 2, 3); - REQUIRE(v.magnitude() == sqrt(14)); + REQUIRE(v.magnitude() == sqrt(14)); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Tuple] Computing the magnitude of vector(-1,-2,-3)", "[Tuple][Magnitude]") { - Tuple v = Tuple::Vector(-1, -2, -3); + Tuple v = Tuple::Vector(-1, -2, -3); - REQUIRE(v.magnitude() == sqrt(14)); + REQUIRE(v.magnitude() == sqrt(14)); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Tuple] Normalize vector(4,0,0) gives (1,0,0)", "[Tuple][Normalize]") { - Tuple v = Tuple::Vector(4, 0, 0); + Tuple v = Tuple::Vector(4, 0, 0); - REQUIRE(v.normalize() == Tuple::Vector(1, 0, 0)); + REQUIRE(v.normalize() == Tuple::Vector(1, 0, 0)); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Tuple] Normalize vector(1,2,3)", "[Tuple][Normalize]") { - Tuple v = Tuple::Vector(1, 2, 3); + Tuple v = Tuple::Vector(1, 2, 3); - REQUIRE(v.normalize() == Tuple::Vector(1 / sqrtf(14), 2 / sqrtf(14), 3 / sqrtf(14))); + REQUIRE(v.normalize() == Tuple::Vector(1 / sqrtf(14), 2 / sqrtf(14), 3 / sqrtf(14))); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Tuple] The magnitude of a normalized vector", "[Tuple][Normalize]") { - Tuple norm; - Tuple v = Tuple::Vector(1, 2, 3); + Tuple norm; + Tuple v = Tuple::Vector(1, 2, 3); - norm = v.normalize(); + norm = v.normalize(); - REQUIRE(norm.magnitude() == 1); + REQUIRE(norm.magnitude() == 1); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Tuple] The dot product of two tuples", "[Tuple][Dot]") { - Tuple a = Tuple::Vector(1, 2, 3); - Tuple b = Tuple::Vector(2, 3, 4); + Tuple a = Tuple::Vector(1, 2, 3); + Tuple b = Tuple::Vector(2, 3, 4); - REQUIRE(a.dot(b) == 20); + REQUIRE(a.dot(b) == 20); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Tuple] The cross product of two vector", "[Tuple][Cross]") { - Tuple a = Tuple::Vector(1, 2, 3); - Tuple b = Tuple::Vector(2, 3, 4); + Tuple a = Tuple::Vector(1, 2, 3); + Tuple b = Tuple::Vector(2, 3, 4); - REQUIRE(a.cross(b) == Tuple::Vector(-1, 2, -1)); - REQUIRE(b.cross(a) == Tuple::Vector(1, -2, 1)); + REQUIRE(a.cross(b) == Tuple::Vector(-1, 2, -1)); + REQUIRE(b.cross(a) == Tuple::Vector(1, -2, 1)); } diff --git a/tests/02_colors.cpp b/tests/02_1_colors.cpp similarity index 99% rename from tests/02_colors.cpp rename to tests/02_1_colors.cpp index d8186c9..3d5d2df 100644 --- a/tests/02_colors.cpp +++ b/tests/02_1_colors.cpp @@ -127,6 +127,5 @@ TEST_CASE("[Color] Multiplying a colors", "[Colors]") Color c1(1, 0.2, 0.4); Color c2(0.9, 1, 0.1); - REQUIRE((c1 * c2) == Color(0.9, 0.2, 0.04)); } diff --git a/tests/03_canvas.cpp b/tests/02_2_canvas.cpp similarity index 98% rename from tests/03_canvas.cpp rename to tests/02_2_canvas.cpp index 8286b32..768e221 100644 --- a/tests/03_canvas.cpp +++ b/tests/02_2_canvas.cpp @@ -41,8 +41,12 @@ TEST_CASE("[Canvas] Creating a canvas", "[Canvas]") REQUIRE(c.height() == 20); for (int i = 0; i < 10; ++i) + { for (int j = 0; j < 20; ++j) + { REQUIRE(c.pixel_at(2, 3) == Color(0, 0, 0)); + } + } } /* ------------------------------------------------------------------------- */ diff --git a/tests/03_matrix.cpp b/tests/03_matrix.cpp new file mode 100644 index 0000000..e69de29 diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 16c3feb..3042c83 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -11,8 +11,9 @@ add_executable(main_test main_test.cpp 01_tuples.cpp - 02_colors.cpp - 03_canvas.cpp + 02_1_colors.cpp + 02_2_canvas.cpp + 03_matrix.cpp ) include_directories("${CMAKE_SOURCE_DIR}/tests")