Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
TPwcDistribution2D.ipp
Go to the documentation of this file.
1#pragma once
2
4
5#include <Common/assertion.h>
6
7namespace ph::math
8{
9
10template<typename T>
12 const TAABB2D<T>& range,
13 const T* const weights,
14 const TVector2<std::size_t>& numWeights) :
15
16 m_marginalYs(),
17 m_conditionalXs(numWeights.y())
19 PH_ASSERT(weights && numWeights.x() > 0 && numWeights.y() > 0);
20
21 // initialize conditional distributions for each row
22 std::vector<T> rowSums(numWeights.y(), 0);
23 for(std::size_t y = 0; y < numWeights.y(); ++y)
24 {
25 const std::size_t baseIndex = y * numWeights.x();
26 for(std::size_t x = 0; x < numWeights.x(); ++x)
27 {
28 rowSums[y] += weights[baseIndex + x];
29 }
31 m_conditionalXs[y] = TPwcDistribution1D<T>(
32 range.getMinVertex().x(),
33 range.getMaxVertex().x(),
34 &(weights[baseIndex]),
35 numWeights.x());
36 }
37
38 // initialize marginal distribution for each row
39 m_marginalYs = TPwcDistribution1D<T>(
40 range.getMinVertex().y(),
41 range.getMaxVertex().y(),
42 rowSums);
43}
44
45template<typename T>
47 const T* const weights,
48 const TVector2<std::size_t>& numWeights) :
49
51 TAABB2D<T>(TVector2<T>(0), TVector2<T>(1)),
52 weights,
53 numWeights)
54{}
55
56template<typename T>
58
59template<typename T>
61 const std::array<T, 2>& sample,
62 T* const out_pdf) const
63{
64 PH_ASSERT(out_pdf);
65
66 std::size_t y;
67 T pdfY, pdfXgivenY;
68 const T sampleY = m_marginalYs.sampleContinuous(sample[1], &pdfY, &y);
69 const T sampleX = m_conditionalXs[y].sampleContinuous(sample[0], &pdfXgivenY);
70
71 *out_pdf = pdfXgivenY * pdfY;
72 PH_ASSERT(*out_pdf > 0);
73 return TVector2<T>(sampleX, sampleY);
74}
75
76template<typename T>
77inline T TPwcDistribution2D<T>::pdfContinuous(const std::array<T, 2>& sample) const
78{
79 const std::size_t y = m_marginalYs.continuousToDiscrete(sample[1]);
80
81 const T pdfY = m_marginalYs.pdfContinuous(y);
82 const T pdfXgivenY = m_conditionalXs[y].pdfContinuous(sample[0]);
83
84 return pdfXgivenY * pdfY;
85}
86
87}// end namespace ph::math
A 2-D Axis-Aligned Bounding Box (AABB).
Definition TAABB2D.h:26
const TVector2< T > & getMaxVertex() const
Definition TAABB2D.ipp:126
const TVector2< T > & getMinVertex() const
Definition TAABB2D.ipp:120
A 1-D piecewise constant distribution of floating-point type T. The sample weights can be seen as a h...
Definition TPwcDistribution1D.h:16
Definition TPwcDistribution2D.h:16
T pdfContinuous(const std::array< T, 2 > &sample) const
Definition TPwcDistribution2D.ipp:77
TVector2< T > sampleContinuous(const std::array< T, 2 > &sample, T *out_pdf) const
Definition TPwcDistribution2D.ipp:60
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