From b44f8ee14484bce2bbd8b5accca39bf365ea7697 Mon Sep 17 00:00:00 2001 From: NADAL Jean-Baptiste Date: Tue, 30 Jan 2024 22:06:30 +0100 Subject: [PATCH] [FEAT] Fix tuple and color operators --- raytracing/src/color.cpp | 38 +++++++++++++-- raytracing/src/color.h | 12 +++-- raytracing/src/tuple.cpp | 103 ++++++++++++++++++++++++++++----------- raytracing/src/tuple.h | 15 ++++-- tests/01_tuples.cpp | 46 +++++++++++++++++ tests/02_colors.cpp | 27 ++++++++-- 6 files changed, 199 insertions(+), 42 deletions(-) diff --git a/raytracing/src/color.cpp b/raytracing/src/color.cpp index c7b34c4..674ebc4 100644 --- a/raytracing/src/color.cpp +++ b/raytracing/src/color.cpp @@ -39,7 +39,7 @@ using namespace Raytracer; 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); + // printf("%s red: %f green: %f, blue: %f\n", __PRETTY_FUNCTION__, m_red, m_green, m_blue); } /* ------------------------------------------------------------------------- */ @@ -79,7 +79,37 @@ const Color &Color::operator=(const Color &a_color) /* ------------------------------------------------------------------------- */ -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); +} + +/* ------------------------------------------------------------------------- */ + +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); +} + +/* ------------------------------------------------------------------------- */ + +const Color Color::operator*(double a_scalar) const +{ + return Color( + m_red * a_scalar, + m_green * a_scalar, + m_blue * a_scalar); +} + +/* ------------------------------------------------------------------------- */ + +const Color &Color::operator+=(const Color &a_color) { m_red += a_color.m_red; m_green += a_color.m_green; @@ -90,7 +120,7 @@ const Color &Color::operator+(const Color &a_color) /* ------------------------------------------------------------------------- */ -const Color &Color::operator-(const Color &a_color) +const Color &Color::operator-=(const Color &a_color) { m_red -= a_color.m_red; m_green -= a_color.m_green; @@ -101,7 +131,7 @@ const Color &Color::operator-(const Color &a_color) /* ------------------------------------------------------------------------- */ -const Color &Color::operator*(double a_scalar) +const Color &Color::operator*=(double a_scalar) { m_red *= a_scalar; m_green *= a_scalar; diff --git a/raytracing/src/color.h b/raytracing/src/color.h index 342e6f3..02ae963 100644 --- a/raytracing/src/color.h +++ b/raytracing/src/color.h @@ -36,10 +36,16 @@ namespace Raytracer 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); + + const Color operator+(const Color &a_color) const; + const Color operator-(const Color &a_color) const; + const Color operator*(double a_scalar) const; + + 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; diff --git a/raytracing/src/tuple.cpp b/raytracing/src/tuple.cpp index 6d71e17..6a087c4 100644 --- a/raytracing/src/tuple.cpp +++ b/raytracing/src/tuple.cpp @@ -82,30 +82,6 @@ const Tuple &Tuple::operator=(const Tuple &an_other) /* ------------------------------------------------------------------------- */ -const Tuple &Tuple::operator+(const Tuple &an_other) -{ - 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 *this; -} - -/* ------------------------------------------------------------------------- */ - -const Tuple &Tuple::operator-(const Tuple &an_other) -{ - 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 *this; -} - -/* ------------------------------------------------------------------------- */ - const Tuple &Tuple::operator-(void) { m_x = -m_x; @@ -118,7 +94,79 @@ const Tuple &Tuple::operator-(void) /* ------------------------------------------------------------------------- */ -const Tuple &Tuple::operator*(double a_scalar) +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 + ); +} + +/* ------------------------------------------------------------------------- */ + +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 + ); +} + +/* ------------------------------------------------------------------------- */ + +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 + ); +} + +/* ------------------------------------------------------------------------- */ + +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 + ); +} + +/* ------------------------------------------------------------------------- */ + +const Tuple &Tuple::operator+=(const Tuple &an_other) +{ + 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 *this; +} + +/* ------------------------------------------------------------------------- */ + +const Tuple &Tuple::operator-=(const Tuple &an_other) +{ + 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 *this; +} + +/* ------------------------------------------------------------------------- */ + +const Tuple &Tuple::operator*=(double a_scalar) { m_x *= a_scalar; m_y *= a_scalar; @@ -130,7 +178,7 @@ const Tuple &Tuple::operator*(double a_scalar) /* ------------------------------------------------------------------------- */ -const Tuple &Tuple::operator/(double a_scalar) +const Tuple &Tuple::operator/=(double a_scalar) { m_x /= a_scalar; m_y /= a_scalar; @@ -233,6 +281,5 @@ 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 - ); + 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 6d51a96..f23a79d 100644 --- a/raytracing/src/tuple.h +++ b/raytracing/src/tuple.h @@ -44,11 +44,18 @@ namespace Raytracer bool operator==(const Tuple &an_other) const; const Tuple &operator=(const Tuple &an_other); - const Tuple &operator+(const Tuple &an_other); - const Tuple &operator-(const Tuple &an_other); + const Tuple &operator-(void); - const Tuple &operator*(double a_scalar); - const Tuple &operator/(double a_scalar); + + const Tuple operator+(const Tuple &an_other) const; + const Tuple operator-(const Tuple &an_other) const; + const Tuple operator*(double a_scalar) const; + const Tuple operator/(double a_scalar) const; + + const Tuple &operator+=(const Tuple &an_other); + const Tuple &operator-=(const Tuple &an_other); + const Tuple &operator*=(double a_scalar); + const Tuple &operator/=(double a_scalar); static Tuple Point(double an_x, double an_y, double an_z); static Tuple Vector(double an_x, double an_y, double an_z); diff --git a/tests/01_tuples.cpp b/tests/01_tuples.cpp index 3a29afe..052912f 100644 --- a/tests/01_tuples.cpp +++ b/tests/01_tuples.cpp @@ -104,6 +104,18 @@ TEST_CASE("Adding two tuples", "[Tuple][Operations]") /* ------------------------------------------------------------------------- */ +TEST_CASE("Adding two tuples without modify a1", "[Tuple][Operations]") +{ + Tuple a1(3, -2, 5, 1); + Tuple a2(-2, 3, 1, 0); + + Tuple a3 = a1 + a2; + + REQUIRE((a1 + a2) == Tuple(1, 1, 6, 1)); +} + +/* ------------------------------------------------------------------------- */ + TEST_CASE("Subtracting two points", "[Tuple][Operations]") { Tuple p1 = Tuple::Point(3, 2, 1); @@ -114,6 +126,18 @@ TEST_CASE("Subtracting two points", "[Tuple][Operations]") /* ------------------------------------------------------------------------- */ +TEST_CASE("Subtracting two points without modify p1", "[Tuple][Operations]") +{ + Tuple p1 = Tuple::Point(3, 2, 1); + Tuple p2 = Tuple::Point(5, 6, 7); + + Tuple p3 = p1 - p2; + + REQUIRE((p1 - p2) == Tuple::Vector(-2, -4, -6)); +} + +/* ------------------------------------------------------------------------- */ + TEST_CASE("Subtracting a vector from a point", "[Tuple][Operations]") { Tuple p = Tuple::Point(3, 2, 1); @@ -162,6 +186,17 @@ TEST_CASE("Multiplying a tuple by a scalar", "[Tuple][Multiplication]") /* ------------------------------------------------------------------------- */ +TEST_CASE("Multiplying a tuple by a scalar without modify a", "[Tuple][Multiplication]") +{ + Tuple a(1, -2, 3, -4); + + Tuple b = a * 3.5; + + REQUIRE(a * 3.5 == Tuple(3.5, -7, 10.5, -14)); +} + +/* ------------------------------------------------------------------------- */ + TEST_CASE("Dividing a tuple by a scalar", "[Tuple][Multiplication]") { Tuple a(1, -2, 3, -4); @@ -171,6 +206,17 @@ TEST_CASE("Dividing a tuple by a scalar", "[Tuple][Multiplication]") /* ------------------------------------------------------------------------- */ +TEST_CASE("Dividing a tuple by a scalar without modify a", "[Tuple][Multiplication]") +{ + Tuple a(1, -2, 3, -4); + + Tuple b = a / 2; + + REQUIRE(a / 2 == Tuple(0.5, -1, 1.5, -2)); +} + +/* ------------------------------------------------------------------------- */ + TEST_CASE("Computing the magnitude of vector(1,0,0)", "[Tuple][Magnitude]") { Tuple v = Tuple::Vector(1, 0, 0); diff --git a/tests/02_colors.cpp b/tests/02_colors.cpp index 6b4a662..1e01e7e 100644 --- a/tests/02_colors.cpp +++ b/tests/02_colors.cpp @@ -1,5 +1,5 @@ /*! - * 01_colors.cpp + * 02_colors.cpp * * Copyright (c) 2015-2024, NADAL Jean-Baptiste. All rights reserved. * @@ -63,8 +63,6 @@ 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)); } @@ -92,9 +90,32 @@ TEST_CASE("Subtracting colors", "[Colors]") /* ------------------------------------------------------------------------- */ +TEST_CASE("Subtracting 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(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)); } + +/* ------------------------------------------------------------------------- */ + +TEST_CASE("Multiplying a color by a scalar without modify c", "[Colors]") +{ + Color c(0.2, 0.3, 0.4); + + Color c3 = c * 4; + + REQUIRE(c * 2 == Color(0.4, 0.6, 0.8)); +}