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 | int
|
---|
142 | PassingRaySet::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 |
|
---|
160 | int Ray::ClassifyPlane(const Plane3 &plane, float minT, float maxT)
|
---|
161 | {
|
---|
162 | Vector3 entp = Extrap(minT);
|
---|
163 | Vector3 extp = Extrap(maxT);
|
---|
164 |
|
---|
165 | int entSide = plane.Side(entp);
|
---|
166 | int extSide = plane.Side(extp);
|
---|
167 |
|
---|
168 | if ((entSide == 0) && (extSide == 0))
|
---|
169 | {
|
---|
170 | return Plane3::COINCIDENT;
|
---|
171 | }
|
---|
172 | else if ((entSide <= 0) && (entSide <= 0))
|
---|
173 | {
|
---|
174 | return Plane3::BACK_SIDE;
|
---|
175 | }
|
---|
176 | else if ((entSide >= 0) && (entSide >= 0))
|
---|
177 | {
|
---|
178 | return Plane3::FRONT_SIDE;
|
---|
179 | }
|
---|
180 |
|
---|
181 | return Plane3::SPLIT;
|
---|
182 | }
|
---|
183 |
|
---|
184 | ostream &
|
---|
185 | operator<<(ostream &s, const PassingRaySet &set)
|
---|
186 | {
|
---|
187 | s<<"Ray Set #rays="<<set.mRays<<" #contributions="<<set.mContributions<<endl;
|
---|
188 | for (int i=0; i < 3*sqr(PassingRaySet::Resolution); i++)
|
---|
189 | s<<set.mDirectionalContributions[i]<<" ";
|
---|
190 | s<<endl;
|
---|
191 | return s;
|
---|
192 | }
|
---|