Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
TLinearGradient2D.h
Go to the documentation of this file.
1#pragma once
2
4#include "Math/TVector2.h"
5
6#include <cmath>
7#include <algorithm>
8
9namespace ph::math
10{
11
15template<typename Value>
17{
18public:
19 static auto makeHorizontal(Value slope) -> TLinearGradient2D;
20 static auto makeVertical(Value slope) -> TLinearGradient2D;
21
22 TLinearGradient2D(TVector2<Value> direction, Value slope);
23
24 Value evaluate(Value x, Value y) const override;
25
26private:
27 // Think in 3D, this is basically the direction of the ramp projected on the xy-plane
28 TVector2<Value> m_unitDir;
29
30 // Amount of value change in one unit amount of movement in the direction of `m_unitDir`
31 Value m_slope;
32};
33
34template<typename Value>
35inline auto TLinearGradient2D<Value>::makeHorizontal(const Value slope)
37{
38 return TLinearGradient2D({1, 0}, slope);
39}
40
41template<typename Value>
42inline auto TLinearGradient2D<Value>::makeVertical(const Value slope)
44{
45 return TLinearGradient2D({0, 1}, slope);
46}
47
48template<typename Value>
50 : m_unitDir(direction.normalize())
51 , m_slope(slope)
52{}
53
54template<typename Value>
55inline Value TLinearGradient2D<Value>::evaluate(const Value x, const Value y) const
56{
57 const Value projectedSignedLengthOnDir = m_unitDir.dot({x, y});
58 return projectedSignedLengthOnDir * m_slope;
59}
60
61}// end namespace ph::math
A linearly increasing/decreasing gradient in a specific direction. Value at (0, 0) is 0.
Definition TLinearGradient2D.h:17
Value evaluate(Value x, Value y) const override
Definition TLinearGradient2D.h:55
static auto makeHorizontal(Value slope) -> TLinearGradient2D
Definition TLinearGradient2D.h:35
static auto makeVertical(Value slope) -> TLinearGradient2D
Definition TLinearGradient2D.h:42
TLinearGradient2D(TVector2< Value > direction, Value slope)
Definition TLinearGradient2D.h:49
Definition TMathFunction2D.h:8
Represents a 2-D vector.
Definition TVector2.h:19
Math functions and utilities.
Definition TransformInfo.h:10
void normalize(std::array< T, N > &vec)
Treating input values as a vector and normalize it. Notice that normalizing a integer typed vector wi...
Definition math.ipp:142