Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
TSpectrumBase.h
Go to the documentation of this file.
1#pragma once
2
5#include "Math/math_fwd.h"
8#include "Utility/utility.h"
9
10#include <cstddef>
11#include <type_traits>
12#include <concepts>
13#include <vector>
14
15namespace ph::math
16{
17
26template<typename Derived, EColorSpace COLOR_SPACE, typename T, std::size_t N>
27class TSpectrumBase : public TArithmeticArrayBase<Derived, T, N>
28{
29private:
31
32protected:
33 using Base::m;
34
35// Hide special members as this class is not intended to be used polymorphically.
36// It is derived class's choice to expose them (by defining them in public) or not.
37protected:
39
40public:
41 using Base::Base;
42
47 template<typename U>
48 explicit TSpectrumBase(const TRawColorValues<U, N>& values);
49
50 inline static constexpr auto NUM_VALUES = Base::NUM_ELEMENTS;
51
52 static consteval EColorSpace getColorSpace() noexcept;
53
54 std::size_t minComponent() const;
55 std::size_t maxComponent() const;
56
60 Derived& setColorValues(const TRawColorValues<T, N>& values);
61 Derived& setColorValues(T rawColorValue);
62 const TRawColorValues<T, N>& getColorValues() const;
64
65 template<EColorSpace SRC_COLOR_SPACE>
66 Derived& setTransformed(const auto& srcColorValues, EColorUsage usage);
67
68 template<CColorValuesInterface ImplType>
69 Derived& setTransformed(const ImplType& srcColorValues, EColorUsage usage);
70
71 template<EColorSpace DST_COLOR_SPACE>
72 auto toTransformed(EColorUsage usage) const;
73
74 void transformFrom(const auto& srcColorValues, EColorSpace srcColorSpace, EColorUsage usage);
75 void transformTo(auto* out_dstColorValues, EColorSpace dstColorSpace, EColorUsage usage) const;
76
79 template<EColorSpace SPECTRAL_COLOR_SPACE = EColorSpace::Spectral>
80 Derived& setSpectral(const TSpectralSampleValues<T>& sampleValues, EColorUsage usage);
81
84 template<CColorValuesInterface ImplType>
85 Derived& setSpectral(const ImplType& sampleValues, EColorUsage usage);
86
89 template<EColorSpace SPECTRAL_COLOR_SPACE = EColorSpace::Spectral>
91
92 // TODO: overloads for directly take another TSpectrumBase in different space
93
95
98 Derived& setLinearSRGB(const TRawColorValues<T, 3>& linearSRGB, EColorUsage usage);
99
102 TRawColorValues<T, 3> toLinearSRGB(EColorUsage usage) const;
103
104 Derived& putEnergy(T energyLevel);
105
106 // Expose public interfaces. `set()` not exposed since we want to use our better-named setters.
107
108 using Base::add;
109 using Base::addLocal;
110
111 using Base::sub;
112 using Base::subLocal;
113
114 using Base::mul;
115 using Base::mulLocal;
116
117 using Base::div;
118 using Base::divLocal;
119
120 using Base::pow;
121 using Base::powLocal;
122
123 using Base::exp;
124 using Base::expLocal;
125
126 using Base::sqrt;
127 using Base::sqrtLocal;
128
129 using Base::clamp;
130 using Base::clampLocal;
131 using Base::safeClamp;
132 using Base::safeClampLocal;
133
134 using Base::abs;
135 using Base::absLocal;
136
137 using Base::rcp;
138 using Base::rcpLocal;
139
140 using Base::complement;
141 using Base::complementLocal;
142
143 using Base::negate;
144 using Base::negateLocal;
145
146 using Base::sum;
147 using Base::avg;
148 using Base::min;
149 using Base::max;
150 using Base::ceil;
151 using Base::floor;
152 using Base::product;
153 using Base::lerp;
154 using Base::isZero;
155 using Base::isNonNegative;
156 using Base::isFinite;
157 using Base::begin;
158 using Base::end;
159 using Base::isEqual;
160 using Base::isNear;
161 using Base::toString;
162 using Base::toVector;
163
164 using Base::operator [];
165 using Base::operator ==;
166 using Base::operator !=;
167
168 using Base::operator +;
169 using Base::operator +=;
170 using Base::operator -;
171 using Base::operator -=;
172 using Base::operator *;
173 using Base::operator *=;
174 using Base::operator /;
175 using Base::operator /=;
176
177public:
181 friend Derived operator + (const T rhs, const Derived& lhs)
182 {
183 return lhs.add(rhs);
184 }
185
186 friend Derived operator - (const T rhs, const Derived& lhs)
187 {
188 return Derived{rhs}.sub(lhs);
189 }
190
191 friend Derived operator * (const T rhs, const Derived& lhs)
192 {
193 return lhs.mul(rhs);
194 }
195
196 friend Derived operator / (const T rhs, const Derived& lhs)
197 {
198 return lhs.rcp().mul(rhs);
199 }
201
202private:
207 template<EColorSpace SRC_COLOR_SPACE>
208 void setTransformedIfCompatible(const auto& srcColorValues, EColorUsage usage);
209
214 template<EColorSpace DST_COLOR_SPACE>
215 void toTransformedIfCompatible(auto* out_dstColorValues, EColorUsage usage) const;
216};
217
218}// end namespace ph::math
219
Definition TArithmeticArrayBase.h:20
std::array< T, N > m
Definition TArithmeticArrayBase.h:217
static constexpr auto NUM_ELEMENTS
Definition TArithmeticArrayBase.h:36
Derived add(const Derived &rhs) const
Definition TArithmeticArrayBase.ipp:26
Base for spectrum implementations.
Definition TSpectrumBase.h:28
TRawColorValues< T, 3 > toLinearSRGB(EColorUsage usage) const
Helper for converting this spectrum to linear-sRGB color.
Definition TSpectrumBase.ipp:245
Derived & setTransformed(const auto &srcColorValues, EColorUsage usage)
auto begin() noexcept -> typename std::array< T, N >::iterator
Definition TArithmeticArrayBase.ipp:797
Derived exp(U exponent) const
Sets the array to .
bool isZero() const
Definition TArithmeticArrayBase.ipp:549
T sum() const
Definition TArithmeticArrayBase.ipp:336
TSpectrumBase(const TRawColorValues< U, N > &values)
Set color values directly. Calling setColorValues() has the same effect. This is the most general for...
Definition TSpectrumBase.ipp:17
Derived mul(const Derived &rhs) const
Definition TArithmeticArrayBase.ipp:98
friend Derived operator*(const T rhs, const Derived &lhs)
Definition TSpectrumBase.h:191
void transformTo(auto *out_dstColorValues, EColorSpace dstColorSpace, EColorUsage usage) const
Definition TSpectrumBase.ipp:198
Derived pow(U exponent) const
friend Derived operator-(const T rhs, const Derived &lhs)
Definition TSpectrumBase.h:186
Derived sqrt() const
Definition TArithmeticArrayBase.ipp:246
std::vector< T > toVector() const
Definition TArithmeticArrayBase.ipp:842
T avg() const
Definition TArithmeticArrayBase.ipp:342
Derived complement() const
Complements the array's elements. Effectively performing 1 - (*this)[i] for each element.
Definition TArithmeticArrayBase.ipp:502
Derived floor() const
Definition TArithmeticArrayBase.ipp:447
T product() const
Definition TArithmeticArrayBase.ipp:358
T relativeLuminance(EColorUsage usage=EColorUsage::EMR) const
Definition TSpectrumBase.ipp:124
const TRawColorValues< T, N > & getColorValues() const
Definition TSpectrumBase.ipp:62
Derived & sqrtLocal()
Definition TArithmeticArrayBase.ipp:253
Derived & safeClampLocal(T lowerBound, T upperBound)
Definition TArithmeticArrayBase.ipp:307
Derived & expLocal(U exponent)
Derived safeClamp(T lowerBound, T upperBound) const
Clamps current array's elements to specific range. If a floating-point value is non-finite (e....
Definition TArithmeticArrayBase.ipp:300
void transformFrom(const auto &srcColorValues, EColorSpace srcColorSpace, EColorUsage usage)
Definition TSpectrumBase.ipp:166
static consteval EColorSpace getColorSpace() noexcept
Definition TSpectrumBase.ipp:26
std::size_t maxComponent() const
Definition TSpectrumBase.ipp:39
Derived & divLocal(const Derived &rhs)
Definition TArithmeticArrayBase.ipp:148
auto end() noexcept -> typename std::array< T, N >::iterator
Definition TArithmeticArrayBase.ipp:811
Derived & absLocal()
Definition TArithmeticArrayBase.ipp:473
Derived clamp(T lowerBound, T upperBound) const
Clamps current array's elements to specific range. None of value, lowerBound and upperBound can be Na...
Definition TArithmeticArrayBase.ipp:264
Derived & addLocal(const Derived &rhs)
Definition TArithmeticArrayBase.ipp:40
Derived lerp(const Derived &rhs, U factor) const
Derived sub(const Derived &rhs) const
Definition TArithmeticArrayBase.ipp:62
Derived div(const Derived &rhs) const
Definition TArithmeticArrayBase.ipp:134
std::string toString() const
Definition TArithmeticArrayBase.ipp:825
Derived & putEnergy(T energyLevel)
Definition TSpectrumBase.ipp:255
Derived & negateLocal()
Definition TArithmeticArrayBase.ipp:528
Derived & rcpLocal()
Definition TArithmeticArrayBase.ipp:491
T min() const
Definition TArithmeticArrayBase.ipp:364
Derived abs() const
Definition TArithmeticArrayBase.ipp:466
bool isFinite() const
Definition TArithmeticArrayBase.ipp:585
PH_DEFINE_INLINE_RULE_OF_5_MEMBERS(TSpectrumBase)
Derived negate() const
Applies a negative sign to the array's elements. These methods is only defined for signed element typ...
Definition TArithmeticArrayBase.ipp:520
Derived & mulLocal(const Derived &rhs)
Definition TArithmeticArrayBase.ipp:112
bool isNear(const Derived &other, T margin) const
Definition TArithmeticArrayBase.ipp:652
auto toTransformed(EColorUsage usage) const
Definition TSpectrumBase.ipp:110
TSpectralSampleValues< T > toSpectral(EColorUsage usage) const
Helper for getting spectral values from this spectrum.
Definition TSpectrumBase.ipp:118
T max() const
Definition TArithmeticArrayBase.ipp:396
bool isNonNegative() const
Definition TArithmeticArrayBase.ipp:565
Derived & setLinearSRGB(const TRawColorValues< T, 3 > &linearSRGB, EColorUsage usage)
Helper for setting linear-sRGB color to this spectrum.
Definition TSpectrumBase.ipp:232
Derived & clampLocal(T lowerBound, T upperBound)
Definition TArithmeticArrayBase.ipp:271
Derived add(const Derived &rhs) const
Definition TArithmeticArrayBase.ipp:26
Derived & subLocal(const Derived &rhs)
Definition TArithmeticArrayBase.ipp:76
bool isEqual(const Derived &other) const
Definition TArithmeticArrayBase.ipp:646
Derived & powLocal(U exponent)
Derived & setSpectral(const TSpectralSampleValues< T > &sampleValues, EColorUsage usage)
Helper for setting spectral values to this spectrum.
Derived ceil() const
Definition TArithmeticArrayBase.ipp:428
Derived & complementLocal()
Definition TArithmeticArrayBase.ipp:509
std::size_t minComponent() const
Definition TSpectrumBase.ipp:32
Derived & setColorValues(const TRawColorValues< T, N > &values)
Set and get raw color values directly.
Definition TSpectrumBase.ipp:46
friend Derived operator/(const T rhs, const Derived &lhs)
Definition TSpectrumBase.h:196
static constexpr auto NUM_VALUES
Definition TSpectrumBase.h:50
Derived rcp() const
Definition TArithmeticArrayBase.ipp:484
Satisfying this concept makes ImplType usable as color values.
Definition color_basics.h:75
Math functions and utilities.
Definition TransformInfo.h:10
std::array< T, N > TRawColorValues
Definition color_basics.h:45
TRawColorValues< T, SampleProps::NUM_SAMPLES > TSpectralSampleValues
Definition color_basics.h:54
EColorUsage
Definition color_enums.h:140
EColorSpace
Definition color_enums.h:7
Definition TAABB2D.h:96