1 | #include "SamplingStrategy.h"
|
---|
2 | #include "Ray.h"
|
---|
3 | #include "Intersectable.h"
|
---|
4 | #include "Preprocessor.h"
|
---|
5 | #include "ViewCellsManager.h"
|
---|
6 | #include "AxisAlignedBox3.h"
|
---|
7 |
|
---|
8 |
|
---|
9 | namespace GtpVisibilityPreprocessor {
|
---|
10 |
|
---|
11 |
|
---|
12 | SamplingStrategy::SamplingStrategy(const Preprocessor &preprocessor):
|
---|
13 | mPreprocessor(preprocessor)
|
---|
14 | {
|
---|
15 | }
|
---|
16 |
|
---|
17 |
|
---|
18 | SamplingStrategy::~SamplingStrategy()
|
---|
19 | {
|
---|
20 | }
|
---|
21 |
|
---|
22 |
|
---|
23 |
|
---|
24 | /*********************************************************************/
|
---|
25 | /* Individual sampling strategies implementation */
|
---|
26 | /*********************************************************************/
|
---|
27 |
|
---|
28 |
|
---|
29 | bool ObjectBasedDistribution::GenerateSample(SimpleRay &ray) const
|
---|
30 | {
|
---|
31 | Vector3 origin, direction;
|
---|
32 |
|
---|
33 | mPreprocessor.mViewCellsManager->GetViewPoint(origin);
|
---|
34 |
|
---|
35 | Vector3 point;
|
---|
36 | Vector3 normal;
|
---|
37 |
|
---|
38 | const int i = (int)RandomValue(0, float((int)mPreprocessor.mObjects.size() - 1));
|
---|
39 |
|
---|
40 | Intersectable *object = mPreprocessor.mObjects[i];
|
---|
41 |
|
---|
42 | object->GetRandomSurfacePoint(point, normal);
|
---|
43 | direction = point - origin;
|
---|
44 |
|
---|
45 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
46 | const float c = Magnitude(direction);
|
---|
47 |
|
---|
48 | if (c <= Limits::Small)
|
---|
49 | return false;
|
---|
50 |
|
---|
51 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
52 | const float pdf = 1.0f;
|
---|
53 |
|
---|
54 | direction *= 1.0f / c;
|
---|
55 | ray = SimpleRay(origin, direction, pdf);
|
---|
56 |
|
---|
57 | return true;
|
---|
58 | }
|
---|
59 |
|
---|
60 |
|
---|
61 | bool ObjectDirectionBasedDistribution::GenerateSample(SimpleRay &ray) const
|
---|
62 | {
|
---|
63 | Vector3 origin, direction;
|
---|
64 |
|
---|
65 | const int i = (int)RandomValue(0, (Real)((int)mPreprocessor.mObjects.size() - 1));
|
---|
66 |
|
---|
67 | Intersectable *object = mPreprocessor.mObjects[i];
|
---|
68 |
|
---|
69 | Vector3 normal;
|
---|
70 |
|
---|
71 | object->GetRandomSurfacePoint(origin, normal);
|
---|
72 | direction = UniformRandomVector(normal);
|
---|
73 |
|
---|
74 | origin += 0.1f * direction;
|
---|
75 |
|
---|
76 | const float c = Magnitude(direction);
|
---|
77 |
|
---|
78 | if (c <= Limits::Small)
|
---|
79 | return false;
|
---|
80 |
|
---|
81 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
82 | const float pdf = 1.0f;
|
---|
83 |
|
---|
84 | direction *= 1.0f / c;
|
---|
85 | ray = SimpleRay(origin, direction, pdf);
|
---|
86 |
|
---|
87 | return true;
|
---|
88 | }
|
---|
89 |
|
---|
90 |
|
---|
91 | bool DirectionBasedDistribution::GenerateSample(SimpleRay &ray) const
|
---|
92 | {
|
---|
93 | Vector3 origin, direction;
|
---|
94 | mPreprocessor.mViewCellsManager->GetViewPoint(origin);
|
---|
95 |
|
---|
96 | direction = UniformRandomVector();
|
---|
97 |
|
---|
98 | const float c = Magnitude(direction);
|
---|
99 |
|
---|
100 | if (c <= Limits::Small)
|
---|
101 | return false;
|
---|
102 |
|
---|
103 | const float pdf = 1.0f;
|
---|
104 |
|
---|
105 | direction *= 1.0f / c;
|
---|
106 | ray = SimpleRay(origin, direction, pdf);
|
---|
107 |
|
---|
108 | return true;
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 | bool DirectionBoxBasedDistribution::GenerateSample(SimpleRay &ray) const
|
---|
113 | {
|
---|
114 | Vector3 origin, direction;
|
---|
115 | mPreprocessor.mViewCellsManager->GetViewPoint(origin);
|
---|
116 |
|
---|
117 | const float alpha = RandomValue(0.0f, 2.0f * (float)M_PI);
|
---|
118 | const float beta = RandomValue((float)-M_PI * 0.5f, (float)M_PI * 0.5f);
|
---|
119 |
|
---|
120 | direction = VssRay::GetDirection(alpha, beta);
|
---|
121 |
|
---|
122 | const float c = Magnitude(direction);
|
---|
123 |
|
---|
124 | if (c <= Limits::Small)
|
---|
125 | return false;
|
---|
126 |
|
---|
127 | const float pdf = 1.0f;
|
---|
128 |
|
---|
129 | direction *= 1.0f / c;
|
---|
130 | ray = SimpleRay(origin, direction, pdf);
|
---|
131 |
|
---|
132 | return true;
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | bool SpatialBoxBasedDistribution::GenerateSample(SimpleRay &ray) const
|
---|
137 | {
|
---|
138 | Vector3 origin, direction;
|
---|
139 |
|
---|
140 | mPreprocessor.mViewCellsManager->GetViewPoint(origin);
|
---|
141 | direction = mPreprocessor.mKdTree->GetBox().GetRandomPoint() - origin;
|
---|
142 |
|
---|
143 | const float c = Magnitude(direction);
|
---|
144 |
|
---|
145 | if (c <= Limits::Small)
|
---|
146 | return false;
|
---|
147 |
|
---|
148 | const float pdf = 1.0f;
|
---|
149 |
|
---|
150 | direction *= 1.0f / c;
|
---|
151 | ray = SimpleRay(origin, direction, pdf);
|
---|
152 |
|
---|
153 | return true;
|
---|
154 | }
|
---|
155 |
|
---|
156 | #if 0
|
---|
157 | bool ObjectsInteriorDistribution::GenerateSample(SimpleRay &ray) const
|
---|
158 | {
|
---|
159 | Vector3 origin, direction;
|
---|
160 |
|
---|
161 | // get random object
|
---|
162 | const int i = RandomValue(0, mPreprocessor.mObjects.size() - 1);
|
---|
163 |
|
---|
164 | const Intersectable *obj = mPreprocessor.mObjects[i];
|
---|
165 |
|
---|
166 | // note: if we load the polygons as meshes,
|
---|
167 | // asymtotically every second sample is lost!
|
---|
168 | origin = obj->GetBox().GetRandomPoint();
|
---|
169 |
|
---|
170 | // uniformly distributed direction
|
---|
171 | direction = UniformRandomVector();
|
---|
172 |
|
---|
173 | const float c = Magnitude(direction);
|
---|
174 |
|
---|
175 | if (c <= Limits::Small)
|
---|
176 | return false;
|
---|
177 |
|
---|
178 | const float pdf = 1.0f;
|
---|
179 |
|
---|
180 | direction *= 1.0f / c;
|
---|
181 | ray = SimpleRay(origin, direction, pdf);
|
---|
182 |
|
---|
183 | return true;
|
---|
184 | }
|
---|
185 | #endif
|
---|
186 | } |
---|