/
linesighttest.cpp
320 lines (257 loc) · 10.1 KB
/
linesighttest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/** @file linesighttest.cpp World Map Line of Sight Testing.
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2006-2013 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#include <cmath>
#include <de/aabox.h>
#include <de/fixedpoint.h>
#include "BspLeaf"
#include "BspNode"
#include "Segment"
#include "Line"
#include "Polyobj"
#include "Sector"
#include "render/r_main.h" /// For validCount, @todo Remove me.
#include "world/linesighttest.h"
using namespace de;
DENG2_PIMPL(LineSightTest)
{
/// LS_* flags @ref lineSightFlags
dint flags;
/// Ray origin.
Vector3d from;
/// Ray target.
Vector3d to;
/// Slope to bottom of target.
dfloat bottomSlope;
/// Slope to top of target.
dfloat topSlope;
/// The ray to be traced.
struct Ray
{
fixed_t origin[2];
fixed_t direction[2];
AABoxd aabox;
Ray(Vector3d const &from, Vector3d const &to)
{
origin[VX] = DBL2FIX(from.x);
origin[VY] = DBL2FIX(from.y);
direction[VX] = DBL2FIX(to.x - from.x);
direction[VY] = DBL2FIX(to.y - from.y);
ddouble v1From[2] = { from.x, from.y };
V2d_InitBox(aabox.arvec2, v1From);
ddouble v1To[2] = { to.x, to.y };
V2d_AddToBox(aabox.arvec2, v1To);
}
} ray;
Instance(Public *i, Vector3d const &from, Vector3d const to,
dfloat bottomSlope, dfloat topSlope, dint flags)
: Base(i),
flags(flags),
from(from),
to(to),
bottomSlope(bottomSlope),
topSlope(topSlope),
ray(from, to)
{}
/**
* @return @c true if the ray passes the line @a side; otherwise @c false.
*
* @todo cleanup: Much unnecessary representation flipping...
* @todo cleanup: Remove front-side assumption.
*/
bool crossLine(Line::Side const &side)
{
#define RTOP 0x1 ///< Top range.
#define RBOTTOM 0x2 ///< Bottom range.
Line const &line = side.line();
// Does the ray intercept the line on the X/Y plane?
// Try a quick bounding-box rejection.
if(line.aaBox().minX > ray.aabox.maxX ||
line.aaBox().maxX < ray.aabox.minX ||
line.aaBox().minY > ray.aabox.maxY ||
line.aaBox().maxY < ray.aabox.minY)
return true;
fixed_t lineV1OriginX[2] = { DBL2FIX(line.fromOrigin().x), DBL2FIX(line.fromOrigin().y) };
fixed_t lineV2OriginX[2] = { DBL2FIX(line.toOrigin().x), DBL2FIX(line.toOrigin().y) };
if(V2x_PointOnLineSide(lineV1OriginX, ray.origin, ray.direction) ==
V2x_PointOnLineSide(lineV2OriginX, ray.origin, ray.direction))
return true;
fixed_t lineDirectionX[2] = { DBL2FIX(line.direction().x), DBL2FIX(line.direction().y) };
fixed_t fromPointX[2] = { DBL2FIX(from.x), DBL2FIX(from.y) };
fixed_t toPointX[2] = { DBL2FIX(to.x), DBL2FIX(to.y) };
if(V2x_PointOnLineSide(fromPointX, lineV1OriginX, lineDirectionX) ==
V2x_PointOnLineSide(toPointX, lineV1OriginX, lineDirectionX))
return true;
// Is this the passable side of a one-way BSP window?
if(!side.hasSections())
return true;
if(!side.hasSector())
return false;
Sector const *frontSec = side.sectorPtr();
Sector const *backSec = side.back().sectorPtr();
bool noBack = side.considerOneSided();
if(!noBack && !(flags & LS_PASSLEFT))
{
noBack = (!( backSec->floor().height() < frontSec->ceiling().height()) ||
!(frontSec->floor().height() < backSec->ceiling().height()));
}
if(noBack)
{
// Does the ray pass from left to right?
if(flags & LS_PASSLEFT) // Allowed.
{
if(line.pointOnSide(from.x, from.y) < 0)
return true;
}
// No back side is present so if the ray is not allowed to pass over/under
// the line then end it right here.
if(!(flags & (LS_PASSOVER | LS_PASSUNDER)))
return false;
}
// Handle the case of a zero height back side in the top range.
dbyte ranges = 0;
if(noBack)
{
ranges |= RTOP;
}
else
{
if(backSec->floor().height() != frontSec->floor().height())
ranges |= RBOTTOM;
if(backSec->ceiling().height() != frontSec->ceiling().height())
ranges |= RTOP;
}
// No partially closed ranges which require testing?
if(!ranges)
return true;
dfloat frac = FIX2FLT(V2x_Intersection(lineV1OriginX, lineDirectionX, ray.origin, ray.direction));
// Does the ray pass over the top range?
if(flags & LS_PASSOVER) // Allowed.
{
if(bottomSlope > (frontSec->ceiling().height() - from.z) / frac)
return true;
}
// Does the ray pass under the bottom range?
if(flags & LS_PASSUNDER) // Allowed.
{
if(topSlope < ( frontSec->floor().height() - from.z) / frac)
return true;
}
// Test a partially closed top range?
if(ranges & RTOP)
{
dfloat const top = noBack ? frontSec->ceiling().height() :
frontSec->ceiling().height() < backSec->ceiling().height()? frontSec->ceiling().height() :
backSec->ceiling().height();
dfloat const slope = (top - from.z) / frac;
if((slope < topSlope) ^ (noBack && !(flags & LS_PASSOVER)) ||
(noBack && topSlope > (frontSec->floor().height() - from.z) / frac))
topSlope = slope;
if((slope < bottomSlope) ^ (noBack && !(flags & LS_PASSUNDER)) ||
(noBack && bottomSlope > (frontSec->floor().height() - from.z) / frac))
bottomSlope = slope;
}
// Test a partially closed bottom range?
if(ranges & RBOTTOM)
{
dfloat const bottom = noBack? frontSec->floor().height() :
frontSec->floor().height() > backSec->floor().height()? frontSec->floor().height() :
backSec->floor().height();
dfloat const slope = (bottom - from.z) / frac;
if(slope > bottomSlope)
bottomSlope = slope;
if(slope > topSlope)
topSlope = slope;
}
return topSlope <= bottomSlope? false : true;
#undef RTOP
#undef RBOTTOM
}
/**
* @return @c true if the ray passes @a bspLeaf; otherwise @c false.
*/
bool crossBspLeaf(BspLeaf const &bspLeaf)
{
if(bspLeaf.isDegenerate())
return false;
// Check polyobj lines.
foreach(Polyobj *po, bspLeaf.polyobjs())
foreach(Line *line, po->lines())
{
if(line->validCount() == validCount)
continue;
line->setValidCount(validCount);
if(!crossLine(line->front()))
return false; // Stop traversal.
}
// Check the line segment geometries.
foreach(Segment const *seg, bspLeaf.allSegments())
{
if(!seg->hasLineSide())
continue;
if(seg->line().validCount() != validCount)
{
seg->line().setValidCount(validCount);
if(!crossLine(seg->lineSide()))
return false;
}
}
return true; // Continue traversal.
}
/**
* @return @c true if the ray passes @a bspElement; otherwise @c false.
*/
bool crossBspNode(MapElement const *bspElement)
{
DENG_ASSERT(bspElement != 0);
while(bspElement->type() != DMU_BSPLEAF)
{
BspNode const *bspNode = bspElement->as<BspNode>();
// Does the ray intersect the partition?
/// @todo Optionally use the fixed precision version -ds
dint const fromSide = bspNode->partition().pointOnSide(Vector2d(from.x, from.y)) < 0;
dint const toSide = bspNode->partition().pointOnSide(Vector2d(to.x, to.y)) < 0;
if(fromSide != toSide)
{
// Yes.
if(!crossBspNode(bspNode->childPtr(fromSide)))
return false; // Cross the From side.
bspElement = bspNode->childPtr(fromSide ^ 1); // Cross the To side.
}
else
{
// No - descend!
bspElement = bspNode->childPtr(fromSide);
}
}
// We've arrived at a leaf.
return crossBspLeaf(*bspElement->as<BspLeaf>());
}
};
LineSightTest::LineSightTest(Vector3d const &from, Vector3d const &to,
dfloat bottomSlope, dfloat topSlope, dint flags)
: d(new Instance(this, from, to, bottomSlope, topSlope, flags))
{}
bool LineSightTest::trace(MapElement const &bspRoot)
{
validCount++;
d->topSlope = d->to.z + d->topSlope - d->from.z;
d->bottomSlope = d->to.z + d->bottomSlope - d->from.z;
return d->crossBspNode(&bspRoot);
}