[FEAT] Add creation of Point and Vector

This commit is contained in:
2024-01-29 22:54:22 +01:00
parent 2c4d63d622
commit b212ca2bac
8 changed files with 18115 additions and 62 deletions

2
.vscode/launch.json vendored
View File

@@ -8,7 +8,7 @@
"name": "(gdb) Lancer",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/rc_tests",
"program": "${workspaceFolder}/build/raytracing_tests",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",

View File

@@ -1,35 +1,19 @@
cmake_minimum_required(VERSION 3.14)
project(raytracing_challenge)
# GoogleTest requires at least C++14
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/refs/tags/v1.14.0.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
enable_testing()
include_directories("${CMAKE_SOURCE_DIR}/tests")
include_directories("${CMAKE_SOURCE_DIR}/src")
add_executable(
rc_tests
raytracing_tests
src/tuple.cpp
tests/main.cpp
tests/chapitre01_tuples.cpp
)
target_link_libraries(
rc_tests
GTest::gtest_main
)
include(GoogleTest)
gtest_discover_tests(rc_tests)

View File

@@ -28,38 +28,96 @@
/* ------------------------------------------------------------------------- */
#include <cmath>
#include "tuple.h"
#define kEpsilon 0.00001
using namespace Raytracer;
/* ------------------------------------------------------------------------- */
Raytracer::Tuple::Tuple(float a_x, float a_y, float a_z, float a_w) : m_x(a_x), m_y(a_y), m_z(a_z), m_w(a_w)
Tuple::Tuple(float a_x, float a_y, float a_z, float a_w) : m_x(a_x), m_y(a_y), m_z(a_z), m_w(a_w)
{
}
/* ------------------------------------------------------------------------- */
float Raytracer::Tuple::x(void) const
bool Tuple::operator==(const Tuple &a_copy) const
{
if (float_equal(m_x, a_copy.m_x) && float_equal(m_y, a_copy.m_y) && float_equal(m_z, a_copy.m_z) && float_equal(m_w, a_copy.m_w))
{
return true;
}
return false;
}
/* ------------------------------------------------------------------------- */
Tuple Tuple::Point(float an_x, float an_y, float an_z)
{
return Tuple(an_x, an_y, an_z, kRaytracerTuplePoint);
}
/* ------------------------------------------------------------------------- */
Tuple Tuple::Vector(float an_x, float an_y, float an_z)
{
return Tuple(an_x, an_y, an_z, kRaytracerTupleVector);
}
/* ------------------------------------------------------------------------- */
float Tuple::x(void) const
{
return m_x;
}
/* ------------------------------------------------------------------------- */
float Raytracer::Tuple::y(void) const
float Tuple::y(void) const
{
return m_y;
}
/* ------------------------------------------------------------------------- */
float Raytracer::Tuple::z(void) const
float Tuple::z(void) const
{
return m_z;
}
/* ------------------------------------------------------------------------- */
float Raytracer::Tuple::w(void) const
float Tuple::w(void) const
{
return m_w;
}
/* ------------------------------------------------------------------------- */
bool Tuple::is_point(void)
{
return (m_w == kRaytracerTuplePoint) ? true : false;
}
/* ------------------------------------------------------------------------- */
bool Tuple::is_vector(void)
{
return (m_w == kRaytracerTupleVector) ? true : false;
}
/* ------------------------------------------------------------------------- */
bool Tuple::float_equal(float a, float b) const
{
if ((std::abs((a) - (b)) < kEpsilon))
{
return true;
}
return false;
}

View File

@@ -28,8 +28,8 @@
/* ------------------------------------------------------------------------- */
#define kRCTuplesPoint 1.0
#define kRCTuplesVetor 0.0
#define kRaytracerTuplePoint 1.0
#define kRaytracerTupleVector 0.0
/* ------------------------------------------------------------------------- */
@@ -38,7 +38,12 @@ namespace Raytracer
class Tuple
{
public:
Tuple(float x, float y, float z, float w);
Tuple(float an_x, float an_y, float an_z, float an_w);
bool operator==(const Tuple &) const;
static Tuple Point(float an_x, float an_y, float an_z);
static Tuple Vector(float an_x, float an_y, float an_z);
// Access
float x(void) const;
@@ -46,11 +51,17 @@ namespace Raytracer
float z(void) const;
float w(void) const;
private:
bool is_point(void);
bool is_vector(void);
protected:
float m_x;
float m_y;
float m_z;
float m_w;
private:
bool float_equal(float a, float b) const;
};
};

View File

@@ -1,24 +0,0 @@
/*!
* vector.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: 29/01/2024
*
*/

17976
tests/catch.hpp Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -28,15 +28,55 @@
/*---------------------------------------------------------------------------*/
#include <gtest/gtest.h>
#include <catch.hpp>
#include "tuple.h"
TEST(RaytracerTuple, CreateBasicTuple)
using namespace Raytracer;
/* ------------------------------------------------------------------------- */
TEST_CASE("a tuple with w=1.0 is a point", "[Tuple]")
{
Raytracer::Tuple t(4.3, -4.2, 3.1, 1.0);
EXPECT_FLOAT_EQ(t.x(), 4.3);
EXPECT_FLOAT_EQ(t.y(), -4.2);
EXPECT_FLOAT_EQ(t.z(), 3.1);
EXPECT_FLOAT_EQ(t.w(), 1.0);
Tuple a(4.3, -4.2, 3.1, 1.0);
REQUIRE(a.x() == 4.3f);
REQUIRE(a.y() == -4.2f);
REQUIRE(a.z() == 3.1f);
REQUIRE(a.w() == 1.0f);
REQUIRE(a.is_point() == true);
REQUIRE(a.is_vector() == false);
}
/* ------------------------------------------------------------------------- */
TEST_CASE("a tuple with w=0 is a vector", "[Tuple]")
{
Tuple a(4.3, -4.2, 3.1, 0.0);
REQUIRE(a.x() == 4.3f);
REQUIRE(a.y() == -4.2f);
REQUIRE(a.z() == 3.1f);
REQUIRE(a.w() == 0.0f);
REQUIRE(a.is_point() == false);
}
/* ------------------------------------------------------------------------- */
TEST_CASE("Point() creates tuples with w=1", "[Tuple][Point]")
{
Tuple p = Tuple::Point(4, -4, 3);
REQUIRE(p == Tuple(4, -4, 3, kRaytracerTuplePoint));
}
/* ------------------------------------------------------------------------- */
TEST_CASE("Vector() creates tuples with w=0", "[Tuple][Vector]")
{
Tuple v = Tuple::Vector(4, -4, 3);
REQUIRE(v == Tuple(4, -4, 3, kRaytracerTupleVector));
}

View File

@@ -1,7 +1,7 @@
/*!
* vector.cpp
* main.cpp
*
* Copyright (c) 2024, NADAL Jean-Baptiste. All rights reserved.
* 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
@@ -22,3 +22,11 @@
* @Date: 29/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
#define CATCH_INTERNAL_CONFIG_NO_CPP17_UNCAUGHT_EXCEPTIONS
#define CATCH_CONFIG_NO_CPP17_UNCAUGHT_EXCEPTIONS
#define CATCH_CONFIG_MAIN
#include "catch.hpp"