1 | #include "VssRay.h"
|
---|
2 | #include "AxisAlignedBox3.h"
|
---|
3 |
|
---|
4 | namespace GtpVisibilityPreprocessor {
|
---|
5 |
|
---|
6 | // Static variables
|
---|
7 | int
|
---|
8 | VssRay::mailID = 0;
|
---|
9 |
|
---|
10 |
|
---|
11 | bool
|
---|
12 | VssRay::ComputeMinMaxT(const AxisAlignedBox3 &box,
|
---|
13 | float &tmin,
|
---|
14 | float &tmax) const
|
---|
15 | {
|
---|
16 |
|
---|
17 | float minx, maxx, miny, maxy, minz, maxz;
|
---|
18 | Vector3 dir = GetDir();
|
---|
19 |
|
---|
20 | if (fabs(dir.x) < 0.001) {
|
---|
21 | if (box.Min().x < GetOrigin().x && box.Max().x > GetOrigin().x) {
|
---|
22 | minx = -MAXFLOAT;
|
---|
23 | maxx = MAXFLOAT;
|
---|
24 | }
|
---|
25 | else
|
---|
26 | return false;
|
---|
27 | }
|
---|
28 | else {
|
---|
29 | float t1 = (box.Min().x - GetOrigin().x) / dir.x;
|
---|
30 | float t2 = (box.Max().x - GetOrigin().x) / dir.x;
|
---|
31 | if (t1 < t2) {
|
---|
32 | minx = t1;
|
---|
33 | maxx = t2;
|
---|
34 | }
|
---|
35 | else {
|
---|
36 | minx = t2;
|
---|
37 | maxx = t1;
|
---|
38 | }
|
---|
39 | if (maxx < 0)
|
---|
40 | return false;
|
---|
41 | }
|
---|
42 |
|
---|
43 | if (fabs(dir.y) < 0.001) {
|
---|
44 | if (box.Min().y < GetOrigin().y && box.Max().y > GetOrigin().y) {
|
---|
45 | miny = -MAXFLOAT;
|
---|
46 | maxy = MAXFLOAT;
|
---|
47 | }
|
---|
48 | else
|
---|
49 | return false;
|
---|
50 | }
|
---|
51 | else {
|
---|
52 | float t1 = (box.Min().y - GetOrigin().y) / dir.y;
|
---|
53 | float t2 = (box.Max().y - GetOrigin().y) / dir.y;
|
---|
54 | if (t1 < t2) {
|
---|
55 | miny = t1;
|
---|
56 | maxy = t2;
|
---|
57 | }
|
---|
58 | else {
|
---|
59 | miny = t2;
|
---|
60 | maxy = t1;
|
---|
61 | }
|
---|
62 | if (maxy < 0.0)
|
---|
63 | return false;
|
---|
64 | }
|
---|
65 |
|
---|
66 | if (fabs(dir.z) < 0.001) {
|
---|
67 | if (box.Min().z < GetOrigin().z && box.Max().z > GetOrigin().z) {
|
---|
68 | minz = -MAXFLOAT;
|
---|
69 | maxz = MAXFLOAT;
|
---|
70 | }
|
---|
71 | else
|
---|
72 | return false;
|
---|
73 | }
|
---|
74 | else {
|
---|
75 | float t1 = (box.Min().z - GetOrigin().z) / dir.z;
|
---|
76 | float t2 = (box.Max().z - GetOrigin().z) / dir.z;
|
---|
77 | if (t1 < t2) {
|
---|
78 | minz = t1;
|
---|
79 | maxz = t2;
|
---|
80 | }
|
---|
81 | else {
|
---|
82 | minz = t2;
|
---|
83 | maxz = t1;
|
---|
84 | }
|
---|
85 | if (maxz < 0.0)
|
---|
86 | return false;
|
---|
87 | }
|
---|
88 |
|
---|
89 | tmin = minx;
|
---|
90 | if (miny > tmin)
|
---|
91 | tmin = miny;
|
---|
92 | if (minz > tmin)
|
---|
93 | tmin = minz;
|
---|
94 |
|
---|
95 | tmax = maxx;
|
---|
96 | if (maxy < tmax)
|
---|
97 | tmax = maxy;
|
---|
98 | if (maxz < tmax)
|
---|
99 | tmax = maxz;
|
---|
100 |
|
---|
101 | return 1; // yes, intersection was found
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 |
|
---|
106 | bool
|
---|
107 | VssRay::Intersects(const AxisAlignedBox3 &box,
|
---|
108 | float &tmin,
|
---|
109 | float &tmax) const
|
---|
110 | {
|
---|
111 | if (!ComputeMinMaxT(box, tmin, tmax))
|
---|
112 | return false;
|
---|
113 |
|
---|
114 | if ( tmax < tmin)
|
---|
115 | return false; // the ray passes outside the box
|
---|
116 |
|
---|
117 | if ( tmax < 0.0f)
|
---|
118 | return false; // the intersection is not on the positive halfline
|
---|
119 |
|
---|
120 | return true; // ray hits the box .. origin can be outside or inside the box
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | bool
|
---|
125 | VssRay::IntersectsSphere(const Vector3 ¢er,
|
---|
126 | const float sqrRadius,
|
---|
127 | Vector3 &point,
|
---|
128 | float &t) const
|
---|
129 | {
|
---|
130 | // compute ray/plane intersection
|
---|
131 | Vector3 dir = GetDir();
|
---|
132 |
|
---|
133 | t = -DotProd(GetOrigin() - center, dir)*sqr(GetInvSize());
|
---|
134 |
|
---|
135 | if (t < 0.0f)
|
---|
136 | t = 0.0f;
|
---|
137 | else
|
---|
138 | if (t > 1.0f)
|
---|
139 | t = 1.0f;
|
---|
140 |
|
---|
141 | point = GetOrigin() + t*dir;
|
---|
142 |
|
---|
143 | return GtpVisibilityPreprocessor::SqrDistance(point, center) < sqrRadius;
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | float
|
---|
148 | VssRay::GetDirParam(const int axis, const Vector3 dir)
|
---|
149 | {
|
---|
150 | return (axis == 0) ? atan2(dir.x, dir.z) : asin(dir.y);
|
---|
151 | }
|
---|
152 |
|
---|
153 | float
|
---|
154 | VssRay::GetDirParametrization(const int axis) const
|
---|
155 | {
|
---|
156 | Vector3 dir = GetNormalizedDir();
|
---|
157 | return GetDirParam(axis, dir);
|
---|
158 | }
|
---|
159 |
|
---|
160 | float
|
---|
161 | VssRay::GetOpositeDirParametrization(const int axis) const
|
---|
162 | {
|
---|
163 | Vector3 dir = -GetNormalizedDir();
|
---|
164 | return GetDirParam(axis, dir);
|
---|
165 | }
|
---|
166 |
|
---|
167 |
|
---|
168 | void
|
---|
169 | GenerateExtendedConvexCombinationWeights2(float &w1,
|
---|
170 | float &w2,
|
---|
171 | const float overlap
|
---|
172 | )
|
---|
173 | {
|
---|
174 | w1 = RandomValue(-overlap, 1.0f + overlap);
|
---|
175 | w2 = 1.0f - w1;
|
---|
176 | }
|
---|
177 |
|
---|
178 | void
|
---|
179 | GenerateExtendedConvexCombinationWeights(float &w1,
|
---|
180 | float &w2,
|
---|
181 | float &w3,
|
---|
182 | const float overlap
|
---|
183 | )
|
---|
184 | {
|
---|
185 |
|
---|
186 | while (1) {
|
---|
187 | w1 = RandomValue(-overlap, 1.0f + overlap);
|
---|
188 | w2 = RandomValue(-overlap, 1.0f + overlap);
|
---|
189 | w3 = 1.0f + overlap - w1 - w2;
|
---|
190 | if (w3 >= -overlap && w3 <= 1.0f + overlap)
|
---|
191 | break;
|
---|
192 | /*
|
---|
193 | w3 = RandomValue(0.0f, 1.0f + overlap);
|
---|
194 |
|
---|
195 | float sum = w1 + w2 + w3;
|
---|
196 | if (sum>0.0f) {
|
---|
197 | w1/=sum;
|
---|
198 | w2/=sum;
|
---|
199 | w3/=sum;
|
---|
200 | break;
|
---|
201 | }
|
---|
202 | */
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 |
|
---|
207 | void
|
---|
208 | VssRayContainer::PrintStatistics(ostream &s)
|
---|
209 | {
|
---|
210 |
|
---|
211 | VssRayContainer::const_iterator it = begin(), it_end = end();
|
---|
212 | int sumContributions = 0;
|
---|
213 | float sumRelContributions = 0.0f;
|
---|
214 |
|
---|
215 | for (; it != it_end; ++it) {
|
---|
216 | VssRay *ray = *it;
|
---|
217 | sumContributions += ray->mPvsContribution;
|
---|
218 | sumRelContributions += ray->mRelativePvsContribution;
|
---|
219 | }
|
---|
220 |
|
---|
221 | s<<"##### VSS RAY STAT ##########\n";
|
---|
222 | s<<"#RAYS\n"<<(int)size()<<endl;
|
---|
223 | s<<"#AVG_RAY_PVS_CONTRIBUTION\n"<<sumContributions/(float)size()<<endl;
|
---|
224 | s<<"#AVG_RAY_RELATIVE_PVS_CONTRIBUTION\n"<<sumRelContributions/
|
---|
225 | (float)size()<<endl;
|
---|
226 |
|
---|
227 | }
|
---|
228 |
|
---|
229 |
|
---|
230 | int
|
---|
231 | VssRayContainer::SelectRays(const int number,
|
---|
232 | VssRayContainer &selected,
|
---|
233 | const bool copy) const
|
---|
234 | {
|
---|
235 | float p = number/(float)size();
|
---|
236 | VssRayContainer::const_iterator it = begin();
|
---|
237 |
|
---|
238 | for (; it != end(); it++)
|
---|
239 | if (Random(1.0f) < p) {
|
---|
240 | if (!copy)
|
---|
241 | selected.push_back(*it);
|
---|
242 | else
|
---|
243 | selected.push_back(new VssRay(**it));
|
---|
244 | }
|
---|
245 |
|
---|
246 | return (int)selected.size();
|
---|
247 | }
|
---|
248 |
|
---|
249 |
|
---|
250 | int
|
---|
251 | VssRayContainer::GetContributingRays(VssRayContainer &selected,
|
---|
252 | const int minPass
|
---|
253 | ) const
|
---|
254 | {
|
---|
255 | VssRayContainer::const_iterator it = begin();
|
---|
256 | // ofstream s("contrib.log");
|
---|
257 | for (; it != end(); it++) {
|
---|
258 | VssRay *ray = *it;
|
---|
259 | // s<<"(pass="<<ray->mPass<<", c="<<ray->mPvsContribution<<")"<<endl;
|
---|
260 | if (ray->mPass >= minPass && ray->mPvsContribution > 0)
|
---|
261 | selected.push_back(ray);
|
---|
262 | }
|
---|
263 | return (int)selected.size();
|
---|
264 | }
|
---|
265 |
|
---|
266 | }
|
---|