source: trunk/VUT/GtpVisibilityPreprocessor/src/Ray.cpp @ 362

Revision 362, 4.0 KB checked in by mattausch, 19 years ago (diff)

added post merging bsp view cells
fixed bug at per ray subdivision

Line 
1#include "Ray.h"
2#include "Plane3.h"
3
4// =========================================================
5// Ray .. static item used for generation of unique ID for
6// each instantiated ray
7
8// it has to start from 1, since 0 is default and for the first
9// ray initial value 0 will not work .. V.H.
10// The value 0 is reserved for particular purpose - the rays
11// that are converted to canonical space and thus the mailbox
12// rayID identification does not work for them!
13int
14Ray::genID = 1;
15
16// Precompute some Ray parameters. Most of them is used for
17// ropes traversal.
18
19void
20Ray::Init()
21{
22  //  if (mType == LOCAL_RAY)
23  //    intersections.reserve(1);
24  //  else
25  //    intersections.reserve(10);
26 
27  // apply the standard precomputation
28  Precompute();
29}
30
31void
32Ray::Precompute()
33{
34  // initialize inverted dir
35  invDir.SetValue(0.0);
36 
37  SetId();
38}
39
40void
41Ray::SetLoc(const Vector3 &l)
42{
43  loc = l;
44}
45
46// make such operation to slightly change the ray direction
47// in case any component of ray direction is zero.
48void
49Ray::CorrectZeroComponents()
50{
51  const float eps = 1e-6;
52
53  // it does change the ray direction very slightly,
54  // but the size direction vector is not practically changed
55 
56  if (fabs(dir.x) < eps) {
57    if (dir.x < 0.0)
58      dir.x = -eps;
59    else
60      dir.x = eps;
61  }
62 
63  if (fabs(dir.y) < eps) {
64    if (dir.y < 0.0)
65      dir.y = -eps;
66    else
67      dir.y = eps;
68  }
69 
70  if (fabs(dir.z) < eps) {
71    if (dir.z < 0.0)
72      dir.z = -eps;
73    else
74      dir.z = eps;
75  }
76}
77
78
79void
80Ray::ComputeInvertedDir() const
81{
82  if ( (invDir.x != 0.0) ||
83       (invDir.y != 0.0) ||
84       (invDir.z != 0.0) )
85    return; // has been already precomputed
86
87  const float eps = 1e-6;
88  const float invEps = 1e6;
89 
90  // it does change the ray direction very slightly,
91  // but the size direction vector is not practically changed
92 
93  if (fabs(dir.x) < eps) {
94    if (dir.x < 0.0)
95      invDir.x = -invEps;
96    else
97      invDir.x = invEps;
98  }
99  else
100    invDir.x = 1.0 / dir.x;
101 
102  if (fabs(dir.y) < eps) {
103    if (dir.y < 0.0)
104      invDir.y = -invEps;
105    else
106      invDir.y = invEps;
107  }
108  else
109    invDir.y = 1.0 / dir.y;
110 
111  if (fabs(dir.z) < eps) {
112    if (dir.z < 0.0)
113      invDir.z = -invEps;
114    else
115      invDir.z = invEps;
116  }
117  else
118    invDir.z = 1.0 / dir.z;
119
120  return;
121}
122
123void
124PassingRaySet::Reset()
125{
126  for (int i=0; i < 3*Resolution*Resolution; i++)
127    mDirectionalContributions[i] = 0;
128  mRays = 0;
129  mContributions = 0;
130}
131 
132void
133PassingRaySet::AddRay(const Ray &ray, const int contributions)
134{
135  int i = GetEntryIndex(ray.GetDir());
136  mRays++;
137  mContributions += contributions;
138  mDirectionalContributions[i] += contributions;
139}
140
141int
142PassingRaySet::GetEntryIndex(const Vector3 &direction) const
143{
144  // get face
145  int axis = direction.DrivingAxis();
146  Vector3 dir;
147  float k = direction[axis];
148  if ( k < 0.0f)
149    k = -k;
150
151  dir = direction/k;
152  float x, y;
153  dir.ExtractVerts(&x, &y, axis);
154  int ix = (x + 1.0f)*0.5f*Resolution;
155  int iy = (y + 1.0f)*0.5f*Resolution;
156
157  return Resolution*(Resolution*axis + iy) + ix;
158}
159
160int Ray::ClassifyPlane(const Plane3 &plane,
161                                           const float minT,
162                                           const float maxT) const
163{
164        const Vector3 entp = Extrap(minT);
165        const Vector3 extp = Extrap(maxT);
166 
167        const int entSide = plane.Side(entp);
168        const int extSide = plane.Side(extp);
169
170        if (entSide < 0)
171        {
172                if (extSide >= 0)
173                {
174                        return BACK_FRONT;
175                }
176                return BACK;
177        }
178        else if (entSide > 0)
179        {
180                if (extSide <= 0)
181                        return FRONT_BACK;
182
183                return FRONT;
184        }
185        else if (entSide == 0)
186        {
187                if (extSide > 0)
188                        return BACK_FRONT;
189                else if (extSide < 0)
190                        return FRONT_BACK;
191        }
192       
193        return COINCIDENT;
194}
195
196ostream &
197operator<<(ostream &s, const PassingRaySet &set)
198{
199  s<<"Ray Set #rays="<<set.mRays<<" #contributions="<<set.mContributions<<endl;
200  for (int i=0; i < 3*sqr(PassingRaySet::Resolution); i++)
201    s<<set.mDirectionalContributions[i]<<" ";
202  s<<endl;
203  return s;
204}
Note: See TracBrowser for help on using the repository browser.