/
hedge.h
384 lines (319 loc) · 10.5 KB
/
hedge.h
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/** @file map/hedge.h World Map Geometry Half-Edge.
*
* @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>
*/
#ifndef DENG_WORLD_MAP_HEDGE
#define DENG_WORLD_MAP_HEDGE
#include <de/Error>
#include "MapElement"
#include "Line"
#include "Vertex"
#ifdef __CLIENT__
struct BiasSurface;
#endif
class BspLeaf;
class Sector;
namespace de {
class Polygon;
}
/**
* Map geometry half-edge.
*
* @em The twin of a half-edge is another HEdge, that which is on the other
* side. If a twin half-edge is linked, they must always be updated as one.
* (i.e., if one of the half-edges is split, the twin must be split also).
*
* @ingroup map
*/
class HEdge : public de::MapElement
{
DENG2_NO_COPY(HEdge)
DENG2_NO_ASSIGN(HEdge)
public:
/// Required neighbor half-edge is missing. @ingroup errors
DENG2_ERROR(MissingNeighborError);
/// Required polygon is missing. @ingroup errors
DENG2_ERROR(MissingPolygonError);
/// Required twin half-edge is missing. @ingroup errors
DENG2_ERROR(MissingTwinError);
/// Required line attribution is missing. @ingroup errors
DENG2_ERROR(MissingLineSideError);
#ifdef __CLIENT__
/// The referenced geometry group does not exist. @ingroup errors
DENG2_ERROR(UnknownGeometryGroupError);
#endif
enum Flag
{
FacingFront = 0x1
};
Q_DECLARE_FLAGS(Flags, Flag)
public: /// @todo Make private:
/// Vertex of the half-edge.
Vertex *_vertex;
/// Next half-edge (clockwise) around the @em face.
HEdge *_next;
/// Previous half-edge (anticlockwise) around the @em face.
HEdge *_prev;
/// Linked @em twin half-edge (that on the other side of "this" half-edge).
HEdge *_twin;
/// Point along the attributed line at which v1 occurs; otherwise @c 0.
coord_t _lineOffset;
/// World angle.
angle_t _angle;
/// Accurate length of the segment (v1 -> v2).
coord_t _length;
#ifdef __CLIENT__
/// For each section of a Line::Side.
BiasSurface *_bsuf[3];
#endif
Flags _flags;
public:
HEdge(Vertex &vertex, Line::Side *lineSide = 0);
~HEdge();
/**
* Returns the vertex of the half-edge.
*/
Vertex &vertex() const;
/**
* Convenient accessor method for returning the origin coordinates for
* the vertex of the half-edge.
*
* @see vertex()
*/
inline de::Vector2d const &origin() const { return vertex().origin(); }
/**
* Returns @c true iff the half-edge has a neighbor in the specifed direction
* around the face of the polyon.
*/
bool hasNeighbor(de::ClockDirection direction) const;
/**
* Returns the neighbor half-edge in the specified @a direction around the
* face of the polygon.
*
* @param direction Relative direction for desired neighbor half-edge.
*/
HEdge &neighbor(de::ClockDirection direction) const;
/**
* Returns @c true iff the half-edge has a next (clockwise) neighbor around
* the face of the polygon.
*
* @see hasNeighbor()
*/
inline bool hasNext() const { return hasNeighbor(de::Clockwise); }
/**
* Returns @c true iff the half-edge has a previous (anticlockwise) neighbor
* around the face of the polygon.
*
* @see hasNeighbor()
*/
inline bool hasPrev() const { return hasNeighbor(de::Anticlockwise); }
/**
* Returns the @em clockwise neighbor half-edge around the face of the
* polygon.
*
* @see neighbor(), hasNext()
*/
inline HEdge &next() const { return neighbor(de::Clockwise); }
/**
* Returns the @em anticlockwise neighbor half-edge around the face of the
* polygon.
*
* @see neighbor(), hasPrev()
*/
inline HEdge &prev() const { return neighbor(de::Anticlockwise); }
/**
* Returns @c true iff a @em twin is linked to the half-edge.
*/
bool hasTwin() const;
/**
* Returns the linked @em twin of the half-edge.
*/
HEdge &twin() const;
/**
* Returns a pointer to the linked @em twin of the half-edge; otherwise @c 0.
*
* @see hasTwin()
*/
inline HEdge *twinPtr() const { return hasTwin()? &twin() : 0; }
/**
* Returns @c true iff the half-edge is part of some Polygon.
*/
bool hasPoly() const;
/**
* Returns the Polygon the half-edge is a part of.
*
* @see hasPoly()
*/
de::Polygon &poly() const;
/**
* Change the Polygon to which the half-edge is attributed.
*
* @param newPolygon New Polygon to attribute to the half-edge. Ownership is
* unaffected. Can be @c 0 (to clear the attribution).
*
* @see hasPoly(), poly()
*/
void setPoly(de::Polygon *newPolygon);
/**
* Returns @c true iff a BspLeaf is linked to the half-edge.
*
* @see hasPoly(), Polygon::hasBspLeaf()
*/
inline bool hasBspLeaf() const { return hasPoly() && poly().hasBspLeaf(); }
/**
* Returns the BspLeaf linked to the half-edge.
*
* @see poly(), Polygon::bspLeaf()
*/
inline BspLeaf &bspLeaf() const { return poly().bspLeaf(); }
/**
* Convenience accessor which determines whether a BspLeaf with an attributed
* sector is linked to the half-edge.
*
* @see hasBspLeaf(), BspLeaf::hasSector()
*/
inline bool hasSector() const { return hasBspLeaf() && bspLeaf().hasSector(); }
/**
* Convenience accessor which returns the Sector of the BspLeaf linked to the
* half-edge.
*
* @see bspLeaf(), BspLeaf::sector()
*/
inline Sector §or() const { return bspLeaf().sector(); }
/**
* Convenience accessor which returns a pointer to the Sector of the BspLeaf
* linked to the half-edge.
*
* @return Sector attributed to the linked BspLeaf; otherwise @c 0.
*
* @see hasBspLeaf(), BspLeaf::sectorPtr()
*/
inline Sector *sectorPtr() const { return hasBspLeaf()? bspLeaf().sectorPtr() : 0; }
/**
* Returns @c true iff a Line::Side is attributed to the half-edge.
*/
bool hasLineSide() const;
/**
* Returns the Line::Side attributed to the half-edge.
*
* @see hasLineSide()
*/
Line::Side &lineSide() const;
/**
* Convenient accessor method for returning the line of the Line::Side
* attributed to the half-edge.
*
* @see hasLineSide(), lineSide()
*/
inline Line &line() const { return lineSide().line(); }
/**
* Convenient accessor method for returning the line side identifier of the
* Line::Side attributed to the half-edge.
*
* @see hasLineSide(), lineSide()
*/
inline int lineSideId() const { return lineSide().lineSideId(); }
/**
* Returns the offset point at which v1 occurs from the attributed line for
* the half-edge.
*
* @see hasLineSide()
*/
coord_t lineOffset() const;
/**
* Returns the world angle of the half-edge.
*/
angle_t angle() const;
/**
* Returns the accurate length of the half-edge from v1 to v2.
*/
coord_t length() const;
/**
* Returns the distance from @a point to the nearest point along the HEdge
* (in the inclussive range 0..1).
*
* @param point Point to measure the distance to in the map coordinate space.
*/
coord_t pointDistance(const_pvec2d_t point, coord_t *offset) const;
/**
* Returns the distance from @a point to the nearest point along the HEdge
* (in the inclussive range 0..1).
*
* @param x X axis point to measure the distance to in the map coordinate space.
* @param y Y axis point to measure the distance to in the map coordinate space.
*/
inline coord_t pointDistance(coord_t x, coord_t y, coord_t *offset) const
{
coord_t point[2] = { x, y };
return pointDistance(point, offset);
}
/**
* On which side of the HEdge does the specified @a point lie?
*
* @param point Point to test in the map coordinate space.
*
* @return @c <0 Point is to the left/back of the hedge.
* @c =0 Point lies directly on the hedge.
* @c >0 Point is to the right/front of the hedge.
*/
coord_t pointOnSide(const_pvec2d_t point) const;
/**
* On which side of the HEdge does the specified @a point lie?
*
* @param x X axis point to test in the map coordinate space.
* @param y Y axis point to test in the map coordinate space.
*
* @return @c <0 Point is to the left/back of the hedge.
* @c =0 Point lies directly on the hedge.
* @c >0 Point is to the right/front of the hedge.
*/
inline coord_t pointOnSide(coord_t x, coord_t y) const
{
coord_t point[2] = { x, y };
return pointOnSide(point);
}
/**
* Returns the current value of the flags of the half-edge.
*/
Flags flags() const;
/**
* Returns @c true iff the half-edge is flagged @a flagsToTest.
*/
inline bool isFlagged(int flagsToTest) const { return (flags() & flagsToTest) != 0; }
/**
* Change the half-edge's flags.
*
* @param flagsToChange Flags to change the value of.
* @param operation Logical operation to perform on the flags.
*/
void setFlags(Flags flagsToChange, de::FlagOp operation = de::SetFlags);
#ifdef __CLIENT__
/**
* Retrieve the bias surface for specified geometry @a groupId
*
* @param groupId Geometry group identifier for the bias surface.
*/
BiasSurface &biasSurfaceForGeometryGroup(uint groupId);
#endif // __CLIENT__
protected:
int property(setargs_t &args) const;
private:
DENG2_PRIVATE(d)
};
Q_DECLARE_OPERATORS_FOR_FLAGS(HEdge::Flags)
#endif // DENG_WORLD_MAP_HEDGE