Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
unary_texture_operators.h
Go to the documentation of this file.
1#pragma once
2
7#include "Utility/traits.h"
10
11#include <Common/assertion.h>
12
13#include <cstddef>
14#include <type_traits>
15#include <memory>
16#include <utility>
17#include <concepts>
18#include <array>
19
20namespace ph
21{
22
23namespace texfunc
24{
25
26template<typename OperatorType, typename InputType, typename OutputType>
27concept CUnaryOperator = requires (OperatorType op, InputType input)
28{
29 { op(input) } -> std::same_as<OutputType>;
30};
31
34template<typename InputType, typename OutputType>
36{
37public:
38 OutputType operator () (const InputType& inputValue) const
39 {
41 "`OutputType` must be buildable from `InputType`");
42
43 return OutputType(inputValue);
44 }
45};
46
49template<typename T>
51{
52public:
53 math::Spectrum operator () (const T scalarValue) const
54 {
55 return math::Spectrum(scalarValue);
56 }
57
58 math::Spectrum operator () (const std::array<T, 1>& scalarValue) const
59 {
60 return (*this)(scalarValue[0]);
61 }
62
64 {
65 return (*this)(scalarValue[0]);
66 }
67};
68
69template<typename InputType, typename OutputType>
70class TAbsolute final
71{
72public:
73 OutputType operator () (const InputType& inputValue) const
74 {
75 constexpr bool canCallAbsMethod = requires (InputType input)
76 {
77 { input.abs() } -> std::convertible_to<OutputType>;
78 };
79
80 constexpr bool canCallStdAbs = requires (InputType input)
81 {
82 { std::abs(input) } -> std::convertible_to<OutputType>;
83 };
84
85 if constexpr(canCallAbsMethod)
86 {
87 return inputValue.abs();
88 }
89 else if constexpr(canCallStdAbs)
90 {
91 return std::abs(inputValue);
92 }
93 else
94 {
95 PH_STATIC_ASSERT_DEPENDENT_FALSE(OutputType,
96 "Cannot perform absolute operation for the specified types.");
97 }
98 }
99};
100
103template
104<
105 typename InputType,
106 typename ConstantType,
107 typename OutputType,
108 CBinaryOperator<InputType, ConstantType, OutputType> BinaryOperatorType
109>
111{
112public:
113 explicit TUnaryFromBinary(ConstantType constant)
114 : m_constant(std::move(constant))
115 {}
116
117 OutputType operator () (const InputType& inputValue) const
118 {
119 static_assert(std::default_initializable<BinaryOperatorType>);
120
121 return BinaryOperatorType{}(inputValue, m_constant);
122 }
123
124private:
125 ConstantType m_constant;
126};
127
130template
131<
132 typename InputType,
133 typename ConstantTypeA,
134 typename ConstantTypeB,
135 typename OutputType,
136 CTernaryOperator<InputType, ConstantTypeA, ConstantTypeB, OutputType> TernaryOperatorType
137>
139{
140public:
141 TUnaryFromTernary(ConstantTypeA constantA, ConstantTypeB constantB)
142 : m_constantA(std::move(constantA))
143 , m_constantB(std::move(constantB))
144 {}
145
146 OutputType operator () (const InputType& inputValue) const
147 {
148 static_assert(std::default_initializable<TernaryOperatorType>);
149
150 return TernaryOperatorType{}(inputValue, m_constantA, m_constantB);
151 }
152
153private:
154 ConstantTypeA m_constantA;
155 ConstantTypeB m_constantB;
156};
157
158template<typename InputType, typename ConstantType, typename OutputType>
160 InputType,
161 ConstantType,
162 OutputType,
164
165template<typename InputType, typename ConstantType, typename OutputType>
167 InputType,
168 ConstantType,
169 OutputType,
171
172template<typename InputType, typename ConstantType, typename OutputType>
174 InputType,
175 ConstantType,
176 OutputType,
178
179template<typename InputType, typename ConstantType, typename OutputType>
181 InputType,
182 ConstantType,
183 OutputType,
185
186template<typename InputType, typename ConstantType, typename OutputType>
188 InputType,
189 ConstantType,
190 OutputType,
192
193template<typename InputType, typename ConstantTypeA, typename ConstantTypeB, typename OutputType>
195 InputType,
196 ConstantTypeA,
197 ConstantTypeB,
198 OutputType,
200
206
207}// end namespace texfunc
208
209template
210<
211 typename InputType,
212 typename OutputType,
214>
215class TUnaryTextureOperator : public TTexture<OutputType>
216{
217public:
218 using InputTexRes = std::shared_ptr<TTexture<InputType>>;
219
220 explicit TUnaryTextureOperator(InputTexRes inputTexture) requires std::default_initializable<OperatorType> :
221 TUnaryTextureOperator(std::move(inputTexture), OperatorType())
222 {}
223
224 TUnaryTextureOperator(InputTexRes inputTexture, OperatorType op) :
225 m_inputTexture(std::move(inputTexture)),
226 m_operator (std::move(op))
227 {}
228
229 void sample(const SampleLocation& sampleLocation, OutputType* const out_value) const override
230 {
231 PH_ASSERT(m_inputTexture);
232 PH_ASSERT(out_value);
233
234 InputType inputValue;
235 m_inputTexture->sample(sampleLocation, &inputValue);
236
237 *out_value = m_operator(inputValue);
238 }
239
240private:
241 InputTexRes m_inputTexture;
242 OperatorType m_operator;
243};
244
245template<typename InputType, typename OutputType>
247
248}// end namespace ph
Definition SampleLocation.h:22
Definition TTexture.h:12
Definition unary_texture_operators.h:216
TUnaryTextureOperator(InputTexRes inputTexture, OperatorType op)
Definition unary_texture_operators.h:224
TUnaryTextureOperator(InputTexRes inputTexture)
Definition unary_texture_operators.h:220
void sample(const SampleLocation &sampleLocation, OutputType *const out_value) const override
Definition unary_texture_operators.h:229
std::shared_ptr< TTexture< InputType > > InputTexRes
Definition unary_texture_operators.h:218
Definition TArithmeticArray.h:13
Definition TTristimulusSpectrum.h:11
Definition unary_texture_operators.h:71
OutputType operator()(const InputType &inputValue) const
Definition unary_texture_operators.h:73
Definition binary_texture_operators.h:33
Definition ternary_texture_operators.h:32
Constructs output value from input value.
Definition unary_texture_operators.h:36
OutputType operator()(const InputType &inputValue) const
Definition unary_texture_operators.h:38
Definition binary_texture_operators.h:72
Definition binary_texture_operators.h:59
Definition binary_texture_operators.h:85
Converts a scalar value to spectrum.
Definition unary_texture_operators.h:51
math::Spectrum operator()(const T scalarValue) const
Definition unary_texture_operators.h:53
Definition binary_texture_operators.h:46
Uses binary operator as a unary one by treating the second input as constant.
Definition unary_texture_operators.h:111
OutputType operator()(const InputType &inputValue) const
Definition unary_texture_operators.h:117
TUnaryFromBinary(ConstantType constant)
Definition unary_texture_operators.h:113
Uses ternary operator as a unary one by treating the second and third inputs as constants.
Definition unary_texture_operators.h:139
OutputType operator()(const InputType &inputValue) const
Definition unary_texture_operators.h:146
TUnaryFromTernary(ConstantTypeA constantA, ConstantTypeB constantB)
Definition unary_texture_operators.h:141
Definition unary_texture_operators.h:27
LinearSRGBSpectrum Spectrum
Definition spectrum_fwd.h:33
The root for all renderer implementations.
Definition EEngineProject.h:6
Definition TAABB2D.h:96
Check if object conversion can be made.
Definition traits.h:70