diff options
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/common.hpp | 4 | ||||
-rw-r--r-- | src/core/spectrum.cpp | 90 | ||||
-rw-r--r-- | src/core/spectrum.hpp | 29 |
3 files changed, 123 insertions, 0 deletions
diff --git a/src/core/common.hpp b/src/core/common.hpp index fd219bf..a3e682e 100644 --- a/src/core/common.hpp +++ b/src/core/common.hpp @@ -1,6 +1,10 @@ #ifndef COMMON_H #define COMMON_H +#include <limits> + #define ZERO_APPROX 1e-6 +#define INFINITY std::numeric_limits<double>::infinity() + #endif diff --git a/src/core/spectrum.cpp b/src/core/spectrum.cpp new file mode 100644 index 0000000..d53cf49 --- /dev/null +++ b/src/core/spectrum.cpp @@ -0,0 +1,90 @@ +#include "spectrum.hpp" + +static double clamp(double v, double low = 0, double high = 0) { + if (v < low) { + return low; + } + if (v > high) { + return high; + } + return v; +} + +Spectrum::Spectrum(double v) { + c[0] = v; + c[1] = v; + c[2] = v; +} + +Spectrum Spectrum::FromRGB(double r, double g, double b) { + Spectrum ret; + ret.c[0] = r; + ret.c[1] = g; + ret.c[2] = b; + + return ret; +} + +Spectrum &Spectrum::operator+=(const Spectrum &o) { + c[0] += o.c[0]; + c[1] += o.c[1]; + c[2] += o.c[2]; + + return *this; +} + +Spectrum &Spectrum::operator*=(double o) { + c[0] *= o; + c[1] *= o; + c[2] *= o; + + return *this; +} + +Spectrum Spectrum::operator+(const Spectrum &o) const { + Spectrum ret = *this; + + ret.c[0] += o.c[0]; + ret.c[1] += o.c[1]; + ret.c[2] += o.c[2]; + + return ret; +} + +Spectrum Spectrum::operator-(const Spectrum &o) const { + Spectrum ret = *this; + + ret.c[0] -= o.c[0]; + ret.c[1] -= o.c[1]; + ret.c[2] -= o.c[2]; + + return ret; +} + +Spectrum Spectrum::operator*(const Spectrum &o) const { + Spectrum ret = *this; + + ret.c[0] *= o.c[0]; + ret.c[1] *= o.c[1]; + ret.c[2] *= o.c[2]; + + return ret; +} + +Spectrum Spectrum::operator/(const Spectrum &o) const { + Spectrum ret = *this; + + ret.c[0] /= o.c[0]; + ret.c[1] /= o.c[1]; + ret.c[2] /= o.c[2]; + + return ret; +} + +Spectrum Spectrum::clamp(double low, double high) const { + Spectrum ret; + ret.c[0] = ::clamp(c[0], low, high); + ret.c[1] = ::clamp(c[1], low, high); + ret.c[2] = ::clamp(c[2], low, high); + return ret; +} diff --git a/src/core/spectrum.hpp b/src/core/spectrum.hpp new file mode 100644 index 0000000..de9355d --- /dev/null +++ b/src/core/spectrum.hpp @@ -0,0 +1,29 @@ +#ifndef SPECTRUM_H +#define SPECTRUM_H + +#include "core/common.hpp" + +// Contains a RGB spectrum value +class Spectrum { +public: + Spectrum(double v = 0); + static Spectrum FromRGB(double r, double g, double b); + + Spectrum &operator+=(const Spectrum &o); + Spectrum &operator*=(double o); + Spectrum operator+(const Spectrum &o) const; + Spectrum operator-(const Spectrum &o) const; + Spectrum operator*(const Spectrum &o) const; + Spectrum operator/(const Spectrum &o) const; + + Spectrum clamp(double low = 0, double high = INFINITY) const; + + double R() const { return c[0]; } + double G() const { return c[1]; } + double B() const { return c[2]; } + +private: + double c[3]; +}; + +#endif |