[FEAT]Add Canvas

This commit is contained in:
NADAL Jean-Baptiste
2024-01-31 15:18:47 +01:00
parent 3c88427371
commit 770b784383
6 changed files with 202 additions and 1 deletions

View File

@@ -10,6 +10,7 @@ add_library(raytracing
src/common.cpp
src/tuple.cpp
src/color.cpp
src/canvas.cpp
)
target_include_directories(${PROJECT_NAME}

80
raytracing/src/canvas.cpp Normal file
View File

@@ -0,0 +1,80 @@
/*!
* canvas.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: 31/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 "canvas.h"
using namespace Raytracer;
/* ------------------------------------------------------------------------- */
Canvas::Canvas(void) : m_width(0), m_height(0)
{
}
/* ------------------------------------------------------------------------- */
Canvas::Canvas(uint16_t a_width, uint16_t a_height) : m_width(a_width), m_height(a_height)
{
m_pixels = std::vector<std::vector<Color>>(m_width, std::vector<Color>(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);
}
/* ------------------------------------------------------------------------- */
uint16_t Canvas::width(void) const
{
return m_width;
}
/* ------------------------------------------------------------------------- */
uint16_t Canvas::height(void) const
{
return m_height;
}
/* ------------------------------------------------------------------------- */
const Color &Canvas::pixel_at(uint16_t a_pos_x, uint16_t a_pos_y) const
{
return m_pixels[a_pos_x][a_pos_y];
}
/* ------------------------------------------------------------------------- */
bool Canvas::write_pixel(uint16_t a_pos_x, uint16_t a_pos_y, const Color &a_color)
{
m_pixels[a_pos_x][a_pos_y] = a_color;
return true;
}

59
raytracing/src/canvas.h Normal file
View File

@@ -0,0 +1,59 @@
/*!
* canvas.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: 31/01/2024
*
*/
#ifndef _RAYTRACER_CANVAS_H
#define _RAYTRACER_CANVAS_H
/* ------------------------------------------------------------------------- */
#include <vector>
#include <cstdint>
#include "color.h"
/* ------------------------------------------------------------------------- */
namespace Raytracer
{
class Canvas
{
public:
Canvas(void);
Canvas(uint16_t a_width, uint16_t a_height);
uint16_t width(void) const;
uint16_t height(void) const;
const Color &pixel_at(uint16_t a_pos_x, uint16_t a_pos_y) const;
bool write_pixel(uint16_t a_pos_x, uint16_t a_pos_y, const Color &a_color);
private:
uint16_t m_width;
uint16_t m_height;
std::vector<std::vector<Color>> m_pixels;
};
};
#endif // _RAYTRACER_CANVAS_H

View File

@@ -26,6 +26,8 @@
#ifndef _RAYTRACER_COLOR_H
#define _RAYTRACER_COLOR_H
/* ------------------------------------------------------------------------- */
namespace Raytracer
{
class Color
@@ -60,4 +62,4 @@ namespace Raytracer
};
};
#endif
#endif // _RAYTRACER_COLOR_H

58
tests/03_canvas.cpp Normal file
View File

@@ -0,0 +1,58 @@
/*!
* 03_canvas.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: 31/01/2024
*
*/
/*---------------------------------------------------------------------------*/
#include <catch.hpp>
#include "canvas.h"
using namespace Raytracer;
/* ------------------------------------------------------------------------- */
TEST_CASE("[Canvas] Creating a canvas", "[Canvas]")
{
Canvas c(10, 20);
REQUIRE(c.width() == 10);
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));
}
/* ------------------------------------------------------------------------- */
TEST_CASE("[Canvas] Writing pixels to a canvas", "[Canvas]")
{
Canvas c(10, 20);
Color red(1, 0, 0);
c.write_pixel(2, 3, red);
REQUIRE(c.pixel_at(2, 3) == red);
}

View File

@@ -12,6 +12,7 @@ add_executable(main_test
01_tuples.cpp
02_colors.cpp
03_canvas.cpp
)
include_directories("${CMAKE_SOURCE_DIR}/tests")