darts/common.h file

Common include files and various utility functions.

Classes

template<typename T>
class Range
Python-style range: iterates from min to max in range-based for loops.
class Range::Iterator
Standard iterator support for Range.
class DartsException
Simple exception class, which stores a human-readable error description.

Functions

void darts_init(int verbosity = spdlog::level::info)
Initialize darts before parsing or rendering a scene.
template<typename... Args>
void success(const char* msg, const Args&... args)
Prints a success message (fmt::print style) and flushes the output.
template<typename T>
auto range(T end) -> Range<T>
Construct a Python-style range from a single parameter end.
template<typename T>
auto range(T start, T end, T step = T(1)) -> Range<T>
Construct a Python-style range from start, up to but excluding end, in incremenents of step.
template<typename T, typename S>
auto lerp(T a, T b, S t) -> T
Linear interpolation.
template<typename T>
auto mod(T a, T b) -> T
Always-positive modulo operation.
auto rad2deg(float value) -> float
Convert radians to degrees.
auto deg2rad(float value) -> float
Convert degrees to radians.
template<typename T>
auto sincos(T arg) -> std::pair<T, T>
Return the sine and cosine in a single function call.
auto spherical_coordinates_to_direction(const Vec2f& phi_theta) -> Vec3f
Convert spherical (phi,theta) coordinates to a unit direction in Cartesian coordinates.
auto direction_to_spherical_coordinates(const Vec3f& dir) -> Vec2f
Convert a unit direction from Cartesian coordinates to spherical (phi,theta) coordinates.
auto direction_to_spherical_uv(const Vec3f& p) -> Vec2f
Converts a unit direction to a UV coordinate using direction_to_spherical_coordinates.
auto spherical_uv_to_direction(const Vec2f& uv) -> Vec3f
Converts a spherical UV coordinate to a unit direction using spherical_coordinates_to_direction.
auto fresnel(float cos_theta_i, float ext_ior, float int_ior) -> float
Calculates the unpolarized fresnel reflection coefficient for a dielectric material.
auto refract(const Vec3f& v, const Vec3f& n, float iorIOverT, Vec3f& refracted) -> bool
Generates a refracted direction, assuming refraction is possible based on the incident direction and refractive indices.
auto reflect(const Vec3f& v, const Vec3f& n) -> Vec3f
Reflects a vector, v, over another vector, n.
auto time_string(double time, int precision = 2) -> std::string
Convert a time value in milliseconds into a human-readable string.
auto indent(const std::string& string, int amount = 2) -> std::string
Indent a string by the specified number of spaces.
auto get_file_resolver() -> filesystem::resolver&
Return the global file resolver instance.

Variables

std::atomic<uint64_t> intersection_tests
Global counter of the number of ray intersection tests.
std::atomic<uint64_t> rays_traced
Global counter of the number of rays traced.

Defines

#define put_your_code_here(txt)
Error signaling unimplemented features.

Function documentation

template<typename T, typename S>
T lerp(T a, T b, S t)

Linear interpolation.

Template parameters
T type for start and end points, and return value
S type for interpolation parameter
Parameters
a Start point
b End point
t A blending factor of a and b.
Returns Linear interpolation of a and b - a value between a and b if t is between 0 and 1.

Linearly interpolates between a and b, using parameter t.

template<typename T>
std::pair<T, T> sincos(T arg)

Return the sine and cosine in a single function call.

In C++17 you can unpack the result using auto [s,c] = sincos(theta).

Vec3f spherical_coordinates_to_direction(const Vec2f& phi_theta)

Convert spherical (phi,theta) coordinates to a unit direction in Cartesian coordinates.

Parameters
phi_theta The spherical angles with $\phi \in [0,2\pi)$ and $\theta \in [0, \pi]$ . $\theta = 0 \mapsto (0,0,1)$ and $\theta = 0 \mapsto (0,0,-1)$ .
Returns The corresponding unit-length direction vector in Cartesian coordinates.

Vec2f direction_to_spherical_coordinates(const Vec3f& dir)

Convert a unit direction from Cartesian coordinates to spherical (phi,theta) coordinates.

Parameters
dir The direction vector in Cartesian coordinates (assumed to be unit length)
Returns The spherical angles with $\phi \in [0,2\pi)$ and $\theta \in [0, \pi]$ . $\theta = 0 \mapsto (0,0,1)$ and $\theta = 0 \mapsto (0,0,-1)$ .

float fresnel(float cos_theta_i, float ext_ior, float int_ior)

Calculates the unpolarized fresnel reflection coefficient for a dielectric material.

Parameters
cos_theta_i Cosine of the angle between the normal and the incident ray
ext_ior Refractive index of the side that contains the surface normal
int_ior Refractive index of the interior

Handles incidence from either side (i.e. cos_theta_i<0 is allowed).

bool refract(const Vec3f& v, const Vec3f& n, float iorIOverT, Vec3f& refracted)

Generates a refracted direction, assuming refraction is possible based on the incident direction and refractive indices.

Parameters
v The incident ray's direction
n The surface normal of the incident ray
iorIOverT The ratio of the index of refraction on the incident side to the index of refraction on the transmitted side
refracted The returned, un-normalized, refracted direction
Returns True if refraction is possible, False if there is total internal reflection

filesystem::resolver& get_file_resolver()

Return the global file resolver instance.

This class is used to locate resource files (e.g. mesh or texture files) referenced by a scene being loaded.