API Reference

namespace tuple

Namespace containing 3D tuple math types and operations.

Functions

template<typename ...Ts>
constexpr tuple<4> point(Ts... args)

Creates a tuple representing a 3D point.

Parameters:

args – X, Y, Z coordinates.

Returns:

A tuple with w = 1.0.

template<typename ...Ts>
constexpr tuple<4> vector(Ts... args)

Creates a tuple representing a 3D vector.

Parameters:

args – X, Y, Z coordinates.

Returns:

A tuple with w = 0.0.

template<int N>
bool operator==(const tuple<N> &ls_tuple, const tuple<N> &rs_tuple)

Compares two tuples with epsilon 1e-5.

template<int N>
tuple<N> operator+(const tuple<N> &ls_tuple, const tuple<N> &rs_tuple)

Adds two tuples.

template<int N>
tuple<N> operator-(const tuple<N> &ls_tuple, const tuple<N> &rs_tuple)

Subtracts two tuples.

template<int N>
tuple<N> operator*(const tuple<N> &ls_tuple, const float num)

Multiply a tuple with a scalar.

template<int N>
tuple<N> operator/(const tuple<N> &ls_tuple, const float num)

Divide a tuple with a scalar.

template<int N>
float magnitude(const tuple<N> &in_tuple)

Returns the magnitude of a tuple.

template<int N>
tuple<N> normalize(const tuple<N> &in_tuple)

Returns the normalized version of the tuple.

template<int N>
float dot_product(const tuple<N> &ls_tuple, const tuple<N> &rs_tuple)

Computes the dot product of two tuples.

tuple<4> cross_product(const tuple<4> &ls_tuple, const tuple<4> &rs_tuple)

Computes the cross product of two tuples.

template<int N>
class tuple

A 4D tuple representing either a point or vector in 3D space.

The 4th element distinguishes point (w=1.0) from vector (w=0.0).

Use point(x, y, z) and vector(x, y, z) to create tuples.

Public Functions

inline tuple(const std::array<float, N> &input_data)

Constructs a tuple from a float array.

inline tuple(const std::initializer_list<float> &input_data)

Constructs a tuple from an initializer list.

inline constexpr bool is_point()
inline constexpr bool is_vector()
inline constexpr float x()
inline constexpr float y()
inline constexpr float z()
inline constexpr float w()
inline float &operator[](const int i)

returns a reference to coordinate

inline float operator[](const int i) const

returns the copy of the coordinate

inline void print() const

Public Members

std::array<float, N> data = {}
namespace color

Namespace containing color-related types and operations.

Functions

bool operator==(const color &ls_color, const color &rs_color)

Compares two colors with epsilon 1e-5.

color operator+(const color &ls_color, const color &rs_color)

Component-wise addition.

color operator-(const color &ls_color, const color &rs_color)

Component-wise subtraction.

color operator*(const color &ls_color, const float num)

Multiply color by scalar.

color operator/(const color &ls_color, const float num)

Divide color by scalar.

color operator*(const color &ls_color, const color &rs_color)

Component-wise multiplication of two colors.

class color

RGB color with three float channels.

Stores color as an array of three floats representing red, green, and blue components.

Public Functions

inline color(const std::array<float, 3> &input_data)

Construct from array of 3 floats.

inline color(const std::initializer_list<float> &input_data)

Construct from initializer list of floats.

inline float &operator[](const int i)
inline float operator[](const int i) const
inline void print() const

Public Members

std::array<float, 3> channels = {}
namespace canvas

Namespace for 2D drawing canvas functionality.

template<std::size_t w, std::size_t h>
class canvas

Fixed-size 2D canvas storing pixel colors and exporting to PPM format.

Template parameters:

  • w: width of the canvas in pixels

  • h: height of the canvas in pixels

Internally stores pixel data as an array of color::color. Provides methods to write pixels, read pixels, and export the canvas as a PPM image string.

Public Functions

inline void write_pixel(const std::size_t x, const std::size_t y, color::color input_color)

Sets the pixel color at (x, y).

inline const color::color &pixel_at(const std::size_t x, const std::size_t y)

Returns the pixel color at (x, y).

inline const std::array<char, (width * height * 11) + 30> &to_ppm()

Converts the canvas pixel data to a PPM image string.

The returned string contains the PPM header and pixel RGB values.

Public Members

std::array<color::color, width * height> pixels = {}
std::array<char, (width * height * 11) + 30> ppm_string = {}

Public Static Attributes

static constexpr std::size_t width = {w}
static constexpr std::size_t height = {h}
namespace matrix

Namespace containing matrix types and operations.

Functions

template<int row_c, int col_c>
bool operator==(const matrix<row_c, col_c> &lhs_matrix, const matrix<row_c, col_c> &rhs_matrix)
template<int row_c, int col_c>
matrix<row_c, col_c> operator*(const matrix<row_c, col_c> &ls_matrix, const float num)
template<int row_c, int col_c>
matrix<row_c, col_c> operator/(const matrix<row_c, col_c> &ls_matrix, const float num)
tuple::tuple<4> operator*(const matrix<4, 4> &ls_matrix, const tuple::tuple<4> &rs_tuple)
matrix<4, 4> operator*(const matrix<4, 4> &ls_matrix, const matrix<4, 4> &rs_matrix)

Multiplication order is reversed because I use column vectors.

matrix<4, 4> identity_matrix()

Returns 4x4 identity matrix.

template<int row_c, int col_c>
class matrix

A generic matrix class supporting various dimensions.

Template Parameters:
  • row_c – The number of columns in the matrix.

  • col_c – The number of rows in the matrix. Each tuple are columns not rowss

Public Functions

inline matrix(const std::array<tuple::tuple<row_c>, col_c> &input_data)

Constructs a matrix from an array of tuples.

inline matrix(const std::initializer_list<tuple::tuple<row_c>> &input_data)

Constructs a matrix from an initializer list of tuples.

inline tuple::tuple<row_c> &operator[](const int index)
inline tuple::tuple<row_c> operator[](const int index) const
inline float &operator()(const int row, const int col)
inline float operator()(const int row, const int col) const
inline int size()
inline matrix<col_c, row_c> transpose()

Tranposes a matrix.

inline void print() const

Prints a matrix + new line.

inline matrix<row_c - 1, col_c - 1> submatrix(const int ignore_row, const int ignore_col)

Returns the submatrix of a matrix.

inline float determinant()

Returns the determinant of a matrix with cofactor expansion.

inline float minor(const int ignore_row, const int ignore_col)

Returns the minor of a matrix.

inline float cofactor(const int ignored_row, const int ignored_col)

Returns the cofactor of a matrix.

inline bool invertible()
inline matrix inverse()

Returns the inverse of a matrix.

Public Members

std::array<tuple::tuple<row_c>, col_c> tuples = {}
namespace matrix_transformations

Functions

matrix::matrix<4, 4> translation(const int x, const int y, const int z)
matrix::matrix<4, 4> scaling(const float x, const float y, const float z)
matrix::matrix<4, 4> rotation_x(const float radians)
matrix::matrix<4, 4> rotation_y(const float radians)
matrix::matrix<4, 4> rotation_z(const float radians)
matrix::matrix<4, 4> shearing(const float xy, const float xz, const float yx, const float yz, const float zx, const float zy)
namespace ray_sphere_intersections

Functions

bool operator==(const sphere &ls_sphere, const sphere &rs_sphere)
bool operator==(const intersection &lhs, const intersection &rhs)
template<typename ...Ts>
std::vector<intersection> intersections(const Ts&... args)

Combines multiple intersections and returns them sorted by time.

template<typename ...Ts>
std::optional<intersection> hit(Ts&... args)

Finds the hit (closest non-negative intersection) from a list of intersections passed as arguments.

Template Parameters:

Ts – Variadic template parameter pack of intersections

Parameters:

args – Intersection objects to evaluate

Returns:

Optional intersection if a valid hit exists, nullopt otherwise

std::optional<intersection> hit(std::vector<intersection> &intersections)

Finds the hit (closest non-negative intersection) from a vector of intersections.

Parameters:

intersections – Vector of intersections to evaluate

Returns:

Optional intersection if a valid hit exists, nullopt otherwise

class intersection

Public Functions

inline intersection(float time, sphere &object)

Constructs an intersection given time and the intersected sphere.

Public Members

float time = {}
std::reference_wrapper<sphere> object
class ray

Represents a ray in 3D space with an origin and direction.

Public Functions

inline ray(tuple::tuple<4> origin, tuple::tuple<4> direction)

Constructs a ray given an origin and direction.

inline tuple::tuple<4> position(float time)
inline ray transform(const matrix::matrix<4, 4> &in_matrix) const

Public Members

tuple::tuple<4> origin = {}
tuple::tuple<4> direction = {}
class sphere

Public Functions

inline sphere(int id)
inline void set_transform(const matrix::matrix<4, 4> &t)

Sets the transformation matrix for the sphere.

inline std::vector<intersection> intersect(const ray &ray)

Computes intersections of a ray with this sphere. Transforms the ray into object space and solves the quadratic equation.ns.

inline tuple::tuple<4> normal_at(tuple::tuple<4> point)

Public Members

int id = {}
tuple::tuple<4> origin = {tuple::point(0, 0, 0)}
float radious = {1}
matrix::matrix<4, 4> transform
light_and_shading::material material = {}
namespace light_and_shading

Functions

tuple::tuple<4> reflect(const tuple::tuple<4> &in_vec, const tuple::tuple<4> &normal)
bool operator==(const material &a, const material &b)
color::color lighting(const material &material, const light &light, const tuple::tuple<4> &point, const tuple::tuple<4> &eyev, const tuple::tuple<4> &normalv)
class light

Public Functions

inline light(const color::color &intensity, const tuple::tuple<4> &position)

Public Members

color::color intensity = {}
tuple::tuple<4> position = {}
class material

Public Functions

material() = default
inline material(color::color &color, float ambient, float diffuse, float specular, float shininess)

Public Members

color::color color = {1, 1, 1}
float ambient = {0.1}
float diffuse = {0.9}
float specular = {0.9}
float shininess = {200.0}