1 | #include "VssRay.h"
|
---|
2 | #include "AxisAlignedBox3.h"
|
---|
3 |
|
---|
4 | // Static variables
|
---|
5 | int
|
---|
6 | VssRay::mailID = 0;
|
---|
7 |
|
---|
8 |
|
---|
9 | bool
|
---|
10 | VssRay::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 |
|
---|
104 | bool
|
---|
105 | VssRay::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 |
|
---|
122 | bool
|
---|
123 | VssRay::IntersectsSphere(const Vector3 ¢er,
|
---|
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 |
|
---|
145 | float
|
---|
146 | VssRay::GetDirParam(const int axis, const Vector3 dir)
|
---|
147 | {
|
---|
148 | return (axis == 0) ? atan2(dir.x, dir.z) : asin(dir.y);
|
---|
149 | }
|
---|
150 |
|
---|
151 | float
|
---|
152 | VssRay::GetDirParametrization(const int axis) const
|
---|
153 | {
|
---|
154 | Vector3 dir = GetNormalizedDir();
|
---|
155 | return GetDirParam(axis, dir);
|
---|
156 | }
|
---|
157 |
|
---|
158 |
|
---|
159 | void
|
---|
160 | GenerateExtendedConvexCombinationWeights2(float &w1,
|
---|
161 | float &w2,
|
---|
162 | const float overlap
|
---|
163 | )
|
---|
164 | {
|
---|
165 | w1 = RandomValue(-overlap, 1.0f + overlap);
|
---|
166 | w2 = 1.0f - w1;
|
---|
167 | }
|
---|
168 |
|
---|
169 | void
|
---|
170 | GenerateExtendedConvexCombinationWeights(float &w1,
|
---|
171 | float &w2,
|
---|
172 | float &w3,
|
---|
173 | const float overlap
|
---|
174 | )
|
---|
175 | {
|
---|
176 |
|
---|
177 | while (1) {
|
---|
178 | w1 = RandomValue(-overlap, 1.0f + overlap);
|
---|
179 | w2 = RandomValue(-overlap, 1.0f + overlap);
|
---|
180 | w3 = 1.0f + overlap - w1 - w2;
|
---|
181 | if (w3 >= -overlap && w3 <= 1.0f + overlap)
|
---|
182 | break;
|
---|
183 | /*
|
---|
184 | w3 = RandomValue(0.0f, 1.0f + overlap);
|
---|
185 |
|
---|
186 | float sum = w1 + w2 + w3;
|
---|
187 | if (sum>0.0f) {
|
---|
188 | w1/=sum;
|
---|
189 | w2/=sum;
|
---|
190 | w3/=sum;
|
---|
191 | break;
|
---|
192 | }
|
---|
193 | */
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 |
|
---|
198 | void
|
---|
199 | VssRayContainer::PrintStatistics(ostream &s)
|
---|
200 | {
|
---|
201 |
|
---|
202 | VssRayContainer::const_iterator it = begin(), it_end = end();
|
---|
203 | int sumContributions = 0;
|
---|
204 | float sumRelContributions = 0.0f;
|
---|
205 |
|
---|
206 | for (; it != it_end; ++it) {
|
---|
207 | VssRay *ray = *it;
|
---|
208 | sumContributions += ray->mPvsContribution;
|
---|
209 | sumRelContributions += ray->mRelativePvsContribution;
|
---|
210 | }
|
---|
211 |
|
---|
212 | s<<"##### VSS RAY STAT ##########\n";
|
---|
213 | s<<"#RAYS\n"<<(int)size()<<endl;
|
---|
214 | s<<"#AVG_RAY_PVS_CONTRIBUTION\n"<<sumContributions/(float)size()<<endl;
|
---|
215 | s<<"#AVG_RAY_RELATIVE_PVS_CONTRIBUTION\n"<<sumRelContributions/
|
---|
216 | (float)size()<<endl;
|
---|
217 |
|
---|
218 | }
|
---|