/
hedge.cpp
252 lines (219 loc) · 6.29 KB
/
hedge.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
/** @file map/hedge.cpp 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>
*/
#include <de/Log>
#include "Sector"
#include "map/hedge.h"
using namespace de;
DENG2_PIMPL(HEdge)
{
/// Map Line::Side attributed to the half-edge. Can be @c 0 (partition segment).
Line::Side *lineSide;
Instance(Public *i)
: Base(i),
lineSide(0)
{}
inline HEdge **neighborAdr(ClockDirection direction) {
return direction == Clockwise? &self._next : &self._prev;
}
};
HEdge::HEdge(Vertex &from, Line::Side *lineSide)
: MapElement(DMU_HEDGE), d(new Instance(this))
{
_from = &from;
_to = 0;
_next = this;
_prev = this;
_twin = 0;
_bspLeaf = 0;
_angle = 0;
_length = 0;
_lineOffset = 0;
std::memset(_bsuf, 0, sizeof(_bsuf));
_flags = 0;
d->lineSide = lineSide;
}
HEdge::HEdge(HEdge const &other)
: MapElement(DMU_HEDGE), d(new Instance(this))
{
_from = other._from;
_to = other._to;
_next = other._next;
_prev = other._prev;
_twin = other._twin;
_bspLeaf = other._bspLeaf;
_angle = other._angle;
_length = other._length;
_lineOffset = other._lineOffset;
std::memcpy(_bsuf, other._bsuf, sizeof(_bsuf));
_flags = other._flags;
d->lineSide = other.d->lineSide;
}
HEdge::~HEdge()
{
#ifdef __CLIENT__
for(uint i = 0; i < 3; ++i)
{
if(_bsuf[i])
{
SB_DestroySurface(_bsuf[i]);
}
}
#endif
}
Vertex &HEdge::vertex(int to)
{
DENG_ASSERT((to? _to : _from) != 0);
return to? *_to : *_from;
}
Vertex const &HEdge::vertex(int to) const
{
return const_cast<Vertex const &>(const_cast<HEdge *>(this)->vertex(to));
}
HEdge &HEdge::navigate(ClockDirection direction) const
{
return **d->neighborAdr(direction);
}
bool HEdge::hasTwin() const
{
return _twin != 0;
}
HEdge &HEdge::twin() const
{
if(_twin)
{
return *_twin;
}
/// @throw MissingTwinError Attempted with no twin associated.
throw MissingTwinError("HEdge::twin", "No twin half-edge is associated");
}
bool HEdge::hasBspLeaf() const
{
return _bspLeaf != 0;
}
BspLeaf &HEdge::bspLeaf() const
{
if(_bspLeaf)
{
return *_bspLeaf;
}
/// @throw MissingBspLeafError Attempted with no BSP leaf associated.
throw MissingBspLeafError("HEdge::bspLeaf", "No BSP leaf is associated");
}
bool HEdge::hasLineSide() const
{
return d->lineSide != 0;
}
Line::Side &HEdge::lineSide() const
{
if(d->lineSide)
{
return *d->lineSide;
}
/// @throw MissingLineError Attempted with no line attributed.
throw MissingLineSideError("HEdge::lineSide", "No line.side is attributed");
}
coord_t HEdge::lineOffset() const
{
if(d->lineSide)
{
return _lineOffset;
}
/// @throw MissingLineError Attempted with no line attributed.
throw MissingLineSideError("HEdge::lineOffset", "No line.side is attributed");
}
angle_t HEdge::angle() const
{
return _angle;
}
coord_t HEdge::length() const
{
return _length;
}
HEdge::Flags HEdge::flags() const
{
return _flags;
}
void HEdge::setFlags(Flags flagsToChange, FlagOp operation)
{
applyFlagOperation(_flags, flagsToChange, operation);
}
biassurface_t &HEdge::biasSurfaceForGeometryGroup(uint groupId)
{
if(groupId <= Line::Side::Top)
{
DENG2_ASSERT(_bsuf[groupId]);
return *_bsuf[groupId];
}
/// @throw InvalidGeometryGroupError Attempted with an invalid geometry group id.
throw UnknownGeometryGroupError("HEdge::biasSurfaceForGeometryGroup", QString("Invalid group id %1").arg(groupId));
}
coord_t HEdge::pointDistance(const_pvec2d_t point, coord_t *offset) const
{
/// @todo Why are we calculating this every time?
Vector2d direction = _to->origin() - _from->origin();
coord_t fromOriginV1[2] = { fromOrigin().x, fromOrigin().y };
coord_t directionV1[2] = { direction.x, direction.y };
return V2d_PointLineDistance(point, fromOriginV1, directionV1, offset);
}
coord_t HEdge::pointOnSide(const_pvec2d_t point) const
{
DENG_ASSERT(point != 0);
/// @todo Why are we calculating this every time?
Vector2d direction = _to->origin() - _from->origin();
coord_t fromOriginV1[2] = { fromOrigin().x, fromOrigin().y };
coord_t directionV1[2] = { direction.x, direction.y };
return V2d_PointOnLineSide(point, fromOriginV1, directionV1);
}
int HEdge::property(setargs_t &args) const
{
switch(args.prop)
{
case DMU_VERTEX0:
DMU_GetValue(DMT_HEDGE_V, &_from, &args, 0);
break;
case DMU_VERTEX1:
DMU_GetValue(DMT_HEDGE_V, &_to, &args, 0);
break;
case DMU_LENGTH:
DMU_GetValue(DMT_HEDGE_LENGTH, &_length, &args, 0);
break;
case DMU_OFFSET: {
coord_t offset = d->lineSide? _lineOffset : 0;
DMU_GetValue(DMT_HEDGE_OFFSET, &offset, &args, 0);
break; }
case DMU_SIDE:
DMU_GetValue(DMT_HEDGE_SIDE, &d->lineSide, &args, 0);
break;
case DMU_LINE: {
Line *lineAdr = d->lineSide? &d->lineSide->line() : 0;
DMU_GetValue(DMT_HEDGE_LINE, &lineAdr, &args, 0);
break; }
case DMU_SECTOR: {
Sector *sector = bspLeafSectorPtr();
DMU_GetValue(DMT_HEDGE_SECTOR, §or, &args, 0);
break; }
case DMU_ANGLE:
DMU_GetValue(DMT_HEDGE_ANGLE, &_angle, &args, 0);
break;
default:
return MapElement::property(args);
}
return false; // Continue iteration.
}