source: trunk/VUT/GtpVisibilityPreprocessor/src/VssRay.cpp @ 492

Revision 492, 5.2 KB checked in by bittner, 18 years ago (diff)

Large merge - viewcells seem not functional now

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