Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
TAnalyticalIntegrator1D.ipp
Go to the documentation of this file.
2
3#include <Common/assertion.h>
4
5#include <algorithm>
6
7namespace ph::math
8{
9
10template<typename T>
14
15template<typename T>
17 m_x0(0), m_x1(0)
18{
20}
21
22template<typename T>
24{
25 if(func.numPoints() == 0)
26 {
27 return 0;
28 }
29
30 const TVector2<T>& p0 = func.getPoint(0);
31 const TVector2<T>& pN = func.getPoint(func.numPoints() - 1);
32
33 // while there's only 1 point, or the integration domain
34 // does not intersect with [p0.x, pN.x]
35 if(func.numPoints() == 1) return (m_x1 - m_x0) * p0.y();
36 if(m_x1 < p0.x()) return (m_x1 - m_x0) * p0.y();
37 if(m_x0 > pN.x()) return (m_x1 - m_x0) * pN.y();
38
39 T sum = 0;
40
41 // possibly add rectangular regions where x <= p0.x and x => pN.x
42 if(m_x0 <= p0.x()) sum += (p0.x() - m_x0) * p0.y();
43 if(m_x1 >= pN.x()) sum += (m_x1 - pN.x()) * pN.y();
44
45 // possibly add trapezoidal regions where x > p0.x and x < pN.x
46 // TODO: can be optimized by better finding point index from func (given x, find previous/next index?)
47 for(std::size_t i = 0; i < func.numPoints() - 1; i++)
48 {
49 // intersecting integration domain with region's domain
50 const auto& p0 = func.getPoint(i);
51 const auto& p1 = func.getPoint(i + 1);
52 const T x0 = std::max(m_x0, p0.x());
53 const T x1 = std::min(m_x1, p1.x());
54
55 // calculate the area of the trapezoid only if the intersection is valid
56 if(x0 < x1)
57 {
58 sum += (func.evaluate(x0, i, i + 1) + func.evaluate(x1, i, i + 1)) * (x1 - x0) / 2;
59 }
60 }
61
62 return sum;
63}
64
65template<typename T>
67{
68 PH_ASSERT(x1 >= x0);
69
70 m_x0 = x0;
71 m_x1 = x1;
72}
73
74}// end namespace ph::math
Definition TAnalyticalIntegrator1D.h:10
T integrate(const TPiecewiseLinear1D< T > &func) const
Definition TAnalyticalIntegrator1D.ipp:23
void setIntegrationDomain(T x0, T x1)
Definition TAnalyticalIntegrator1D.ipp:66
TAnalyticalIntegrator1D()
Definition TAnalyticalIntegrator1D.ipp:11
Definition TPiecewiseLinear1D.h:26
TVector2< T > getPoint(std::size_t pointIndex) const
Definition TPiecewiseLinear1D.h:127
std::size_t numPoints() const
Definition TPiecewiseLinear1D.h:121
T evaluate(T x) const
Definition TPiecewiseLinear1D.h:54
Represents a 2-D vector.
Definition TVector2.h:19
T & x()
Definition TVector2.ipp:38
T & y()
Definition TVector2.ipp:44
Math functions and utilities.
Definition TransformInfo.h:10