From 3d3b8b7f02c870c03ac475db6aee7f138abe8582 Mon Sep 17 00:00:00 2001 From: NADAL Jean-Baptiste Date: Tue, 30 Jan 2024 15:55:42 +0100 Subject: [PATCH] [FEAT] add dot and cross --- raytracing/src/tuple.cpp | 33 +++++++++++++++++++++++++++++++++ raytracing/src/tuple.h | 11 +++++++---- tests/chapitre01_tuples.cpp | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 4 deletions(-) diff --git a/raytracing/src/tuple.cpp b/raytracing/src/tuple.cpp index 0ff1e2d..8b457dc 100644 --- a/raytracing/src/tuple.cpp +++ b/raytracing/src/tuple.cpp @@ -70,6 +70,18 @@ bool Tuple::operator==(const Tuple &an_other) const /* ------------------------------------------------------------------------- */ +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; @@ -206,6 +218,27 @@ 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; +} + +/* ------------------------------------------------------------------------- */ + +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 + ); +} + +/* ------------------------------------------------------------------------- */ + bool Tuple::double_equal(double a, double b) const { if ((std::abs((a) - (b)) < kEpsilon)) diff --git a/raytracing/src/tuple.h b/raytracing/src/tuple.h index ae5a2af..9b0f65c 100644 --- a/raytracing/src/tuple.h +++ b/raytracing/src/tuple.h @@ -43,6 +43,7 @@ namespace Raytracer Tuple(double an_x, double an_y, double an_z, double an_w); 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); @@ -64,14 +65,16 @@ namespace Raytracer double magnitude(void) const; Tuple normalize(void); - protected: + double dot(const Tuple &a_tuple); + 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; - - private: - bool double_equal(double a, double b) const; }; }; diff --git a/tests/chapitre01_tuples.cpp b/tests/chapitre01_tuples.cpp index 1e303fa..d0a0ce4 100644 --- a/tests/chapitre01_tuples.cpp +++ b/tests/chapitre01_tuples.cpp @@ -64,6 +64,18 @@ TEST_CASE("a tuple with w=0 is a vector", "[Tuple]") /* ------------------------------------------------------------------------- */ +TEST_CASE("Tuple could be copy", "[Tuple]") +{ + Tuple p = Tuple::Point(4, -4, 3); + Tuple n; + + n = p; + + REQUIRE(n == p); +} + +/* ------------------------------------------------------------------------- */ + TEST_CASE("Point() creates tuples with w=1", "[Tuple][Point]") { Tuple p = Tuple::Point(4, -4, 3); @@ -231,3 +243,24 @@ TEST_CASE("The magnitude of a normalized vector", "[Tuple][Normalize]") REQUIRE(norm.magnitude() == 1); } + +/* ------------------------------------------------------------------------- */ + +TEST_CASE("The dot product of two tuples", "[Tuple][Dot]") +{ + Tuple a = Tuple::Vector(1, 2, 3); + Tuple b = Tuple::Vector(2, 3, 4); + + REQUIRE(a.dot(b) == 20); +} + +/* ------------------------------------------------------------------------- */ + +TEST_CASE("The cross product of two vector", "[Tuple][Cross]") +{ + 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)); +}