SpectralBins

struct Bin

Represents a wavelength bin with minimum, maximum, and center wavelengths.

Wavelengths are stored in meters (SI units).

Public Functions

constexpr Bin() = default
inline constexpr Bin(double new_min, double new_max)

Public Members

double min_wavelength = 0
double max_wavelength = 0
double center_wavelength = 0
template<std::size_t N, auto... Args>
class SpectralBins

A template class representing spectral data distributed across wavelength bins.

SpectralBins provides a container for spectral values (e.g., radiance, irradiance) discretized into wavelength bins. The bins can be configured with:

  • Uniform spacing (2 args: min, max wavelengths in nm)

  • Explicit pairs (2N args: min1, max1, min2, max2, …)

  • Bin edges (N+1 args: edge0, edge1, …, edgeN)

The template arguments are to be given in units of nanometers.

Supports standard container operations, arithmetic operations (element-wise and scalar), and spectral analysis functions.

Template Parameters:
  • N – The number of spectral bins.

  • Args – Variadic template parameters defining bin configuration.

Public Types

using value_type = float
using size_type = std::size_t
using reference = float&
using const_reference = const float&
using iterator = typename std::array<float, N>::iterator
using const_iterator = typename std::array<float, N>::const_iterator

Public Functions

constexpr SpectralBins()

Default constructor initializing all bins to zero.

explicit constexpr SpectralBins(const float &value)

Constructs a SpectralBins with all bins set to the same value.

Parameters:

value – The value to assign to all bins.

constexpr SpectralBins(std::initializer_list<float> init)

Constructs from an initializer list.

If the list has one element, fills all bins with that value. Otherwise, copies values from the list (up to N elements).

Parameters:

init – Initializer list of float values.

template<typename ...Args2>
constexpr SpectralBins(Args2&&... args)

Constructs from exactly N values.

Template Parameters:

Args2 – Parameter pack of convertible-to-float types.

Parameters:

args – Values for each bin (must be exactly N values).

constexpr SpectralBins(const SpectralBins&) = default
constexpr SpectralBins(SpectralBins&&) = default
constexpr SpectralBins &operator=(const SpectralBins&) = default
constexpr SpectralBins &operator=(SpectralBins&&) = default
inline constexpr reference operator[](size_type pos)
inline constexpr const_reference operator[](size_type pos) const
inline constexpr reference at(size_type pos)
inline constexpr const_reference at(size_type pos) const
inline constexpr reference front()
inline constexpr const_reference front() const
inline constexpr reference back()
inline constexpr const_reference back() const
inline constexpr float *data() noexcept
inline constexpr const float *data() const noexcept
inline constexpr iterator begin() noexcept
inline constexpr const_iterator begin() const noexcept
inline constexpr const_iterator cbegin() const noexcept
inline constexpr iterator end() noexcept
inline constexpr const_iterator end() const noexcept
inline constexpr const_iterator cend() const noexcept
inline constexpr void fill(const float &value)
float total() const

Sum of all spectral values.

Computes the sum of all spectral values.

Returns:

The total sum across all bins.

float magnitude() const

Euclidean magnitude (L2 norm) of the spectral vector.

Computes the Euclidean magnitude (L2 norm).

Returns:

The square root of the sum of squared values.

float max() const

Maximum value across all bins.

Finds the maximum value across all bins.

Returns:

The maximum spectral value, or 0.0 if N is 0.

float min() const

Minimum value across all bins.

Finds the minimum value across all bins.

Returns:

The minimum spectral value, or 0.0 if N is 0.

float integrate() const

Wavelength-weighted integral over all bins.

Computes the wavelength-weighted integral.

Integrates the spectral distribution by summing each bin’s value multiplied by its wavelength width.

Returns:

The integrated value over wavelength.

float integrate_over_band(double min_wavelength, double max_wavelength) const

Integral over specified wavelength range.

bool valid() const

Checks if all spectral values are valid (non-negative, not NaN, not infinite).

Checks if all spectral values are valid.

Validates that all values are non-negative, not NaN, and not infinite.

Returns:

True if all values are valid.

bool valid_ratio() const

Checks if all spectral values are valid albedo values.

Checks if all spectral values are valid ratios (between 0 and 1, not NaN, not infinite).

Validates that all values are between 0 and 1, not NaN, and not infinite.

Returns:

True if all values are valid abledo values.

SpectralBins sqrt() const
constexpr SpectralBins &operator+=(const SpectralBins &other)
constexpr SpectralBins &operator-=(const SpectralBins &other)
constexpr SpectralBins &operator*=(const SpectralBins &other)
constexpr SpectralBins &operator/=(const SpectralBins &other)
template<typename U>
constexpr SpectralBins &operator+=(const U &scalar)
template<typename U>
constexpr SpectralBins &operator-=(const U &scalar)
template<typename U>
constexpr SpectralBins &operator*=(const U &scalar)
template<typename U>
constexpr SpectralBins &operator/=(const U &scalar)
constexpr SpectralBins operator+() const
constexpr SpectralBins operator-() const
constexpr bool operator==(const SpectralBins &other) const
constexpr bool operator!=(const SpectralBins &other) const
std::string to_string() const
template<typename U>
constexpr SpectralBins<N, Args...> &operator+=(const U &scalar)
template<typename U>
constexpr SpectralBins<N, Args...> &operator-=(const U &scalar)
template<typename U>
constexpr SpectralBins<N, Args...> &operator*=(const U &scalar)
template<typename U>
constexpr SpectralBins<N, Args...> &operator/=(const U &scalar)

Public Static Functions

static SpectralBins integrate_over_data(const std::vector<double> &wavelengths, const std::vector<float> &values)

Integrates externally-sampled spectral data into each bin.

Given a set of (wavelength, value) pairs representing a continuous spectral function (e.g. a sensor quantum-efficiency curve loaded from a CSV), this method integrates the data over each bin’s wavelength range using the trapezoidal rule with linear interpolation at bin boundaries, then divides by the bin width to produce a mean value per bin.

bin[i] = (1 / bin_width_nm) * integral(value(λ) dλ, bin_min_nm, bin_max_nm)

Bins with no overlapping data (entirely outside the sampled range) are set to 0.

Parameters:
  • wavelengths – Sample wavelengths in nanometers, strictly ascending.

  • values – Spectral values at each corresponding wavelength.

Throws:

std::runtime_error – if lengths differ, vectors are empty, wavelengths are not positive, finite and strictly ascending, or a value is not finite.

Returns:

SpectralBins with the mean spectral value over each bin.

static inline constexpr bool empty() noexcept
static inline constexpr size_type size() noexcept
static constexpr SpectralBins from_total(float total)

Creates a spectral distribution from a total value.

Distributes the total value proportionally across bins based on their wavelength widths. Wider bins receive proportionally more of the total.

Parameters:

total – The total value to distribute.

Returns:

A SpectralBins instance with distributed values.

static inline constexpr Bin get_bin(std::size_t index)
static inline constexpr std::array<Bin, N> get_all_bins()
static constexpr SpectralBins photon_energies()

Computes photon energies for each bin’s center wavelength.

Calculates the energy of a photon at each bin’s center wavelength using the relation E = hc/lambda.

Returns:

A SpectralBins instance containing photon energies in Joules.