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!
|
---|
13 | int
|
---|
14 | Ray::genID = 1;
|
---|
15 |
|
---|
16 | // Precompute some Ray parameters. Most of them is used for
|
---|
17 | // ropes traversal.
|
---|
18 |
|
---|
19 | void
|
---|
20 | Ray::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 |
|
---|
31 | void
|
---|
32 | Ray::Precompute()
|
---|
33 | {
|
---|
34 | // initialize inverted dir
|
---|
35 | invDir.SetValue(0.0);
|
---|
36 |
|
---|
37 | SetId();
|
---|
38 | }
|
---|
39 |
|
---|
40 | void
|
---|
41 | Ray::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.
|
---|
48 | void
|
---|
49 | Ray::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 |
|
---|
79 | void
|
---|
80 | Ray::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 |
|
---|
123 | void
|
---|
124 | PassingRaySet::Reset()
|
---|
125 | {
|
---|
126 | for (int i=0; i < 3*Resolution*Resolution; i++)
|
---|
127 | mDirectionalContributions[i] = 0;
|
---|
128 | mRays = 0;
|
---|
129 | mContributions = 0;
|
---|
130 | }
|
---|
131 |
|
---|
132 | void
|
---|
133 | PassingRaySet::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 |
|
---|
141 | void
|
---|
142 | PassingRaySet::AddRay2(const Ray &ray,
|
---|
143 | const int objects,
|
---|
144 | const int viewcells
|
---|
145 | )
|
---|
146 | {
|
---|
147 | int i = GetEntryIndex(ray.GetDir());
|
---|
148 | mRays++;
|
---|
149 | mContributions += objects*viewcells;
|
---|
150 | mDirectionalContributions[i] += objects*viewcells;
|
---|
151 | }
|
---|
152 |
|
---|
153 | int
|
---|
154 | PassingRaySet::GetEntryIndex(const Vector3 &direction) const
|
---|
155 | {
|
---|
156 | // get face
|
---|
157 | int axis = direction.DrivingAxis();
|
---|
158 | Vector3 dir;
|
---|
159 | float k = direction[axis];
|
---|
160 | if ( k < 0.0f)
|
---|
161 | k = -k;
|
---|
162 |
|
---|
163 | dir = direction/k;
|
---|
164 | float x, y;
|
---|
165 | dir.ExtractVerts(&x, &y, axis);
|
---|
166 | int ix = (x + 1.0f)*0.5f*Resolution;
|
---|
167 | int iy = (y + 1.0f)*0.5f*Resolution;
|
---|
168 |
|
---|
169 | return Resolution*(Resolution*axis + iy) + ix;
|
---|
170 | }
|
---|
171 |
|
---|
172 | int
|
---|
173 | Ray::ClassifyPlane(const Plane3 &plane,
|
---|
174 | const float minT,
|
---|
175 | const float maxT) const
|
---|
176 | {
|
---|
177 | const Vector3 entp = Extrap(minT);
|
---|
178 | const Vector3 extp = Extrap(maxT);
|
---|
179 |
|
---|
180 | const int entSide = plane.Side(entp);
|
---|
181 | const int extSide = plane.Side(extp);
|
---|
182 |
|
---|
183 | if (entSide < 0)
|
---|
184 | {
|
---|
185 | if (extSide >= 0)
|
---|
186 | {
|
---|
187 | return BACK_FRONT;
|
---|
188 | }
|
---|
189 | return BACK;
|
---|
190 | }
|
---|
191 | else if (entSide > 0)
|
---|
192 | {
|
---|
193 | if (extSide <= 0)
|
---|
194 | return FRONT_BACK;
|
---|
195 |
|
---|
196 | return FRONT;
|
---|
197 | }
|
---|
198 | else if (entSide == 0)
|
---|
199 | {
|
---|
200 | if (extSide > 0)
|
---|
201 | return BACK_FRONT;
|
---|
202 | else if (extSide < 0)
|
---|
203 | return FRONT_BACK;
|
---|
204 | }
|
---|
205 |
|
---|
206 | return COINCIDENT;
|
---|
207 | }
|
---|
208 |
|
---|
209 |
|
---|
210 | ostream &
|
---|
211 | operator<<(ostream &s, const PassingRaySet &set)
|
---|
212 | {
|
---|
213 | s<<"Ray Set #rays="<<set.mRays<<" #contributions="<<set.mContributions<<endl;
|
---|
214 | for (int i=0; i < 3*sqr(PassingRaySet::Resolution); i++)
|
---|
215 | s<<set.mDirectionalContributions[i]<<" ";
|
---|
216 | s<<endl;
|
---|
217 | return s;
|
---|
218 | } |
---|