/
bspnode.cpp
128 lines (110 loc) · 3.43 KB
/
bspnode.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
/** @file bspnode.cpp World map BSP node.
*
* @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_base.h"
#include "world/bspnode.h"
#include <de/Log>
#include <de/vector1.h> /// @todo Remove me
using namespace de;
DENG2_PIMPL_NOREF(BspNode)
{
/// Space partition (half-plane).
Partition partition;
/// Right and left child elements for each half space.
MapElement *rightChild;
MapElement *leftChild;
/// Right and left bounding boxes for each half space.
AABoxd rightAABox;
AABoxd leftAABox;
Instance(Partition const &partition)
: partition(partition)
, rightChild(0)
, leftChild(0)
{}
inline MapElement **childAdr(int left) {
return left? &leftChild : &rightChild;
}
inline AABoxd &aaBox(int left) {
return left? leftAABox : rightAABox;
}
};
BspNode::BspNode(Partition const &partition)
: MapElement(DMU_BSPNODE)
, d(new Instance(partition))
{
setRightAABox(0);
setLeftAABox(0);
}
Partition const &BspNode::partition() const
{
return d->partition;
}
size_t BspNode::height() const
{
DENG2_ASSERT(hasLeft() || hasRight());
size_t rHeight = 0;
if(hasRight() && right().type() == DMU_BSPNODE)
rHeight = right().as<BspNode>().height();
size_t lHeight = 0;
if(hasLeft() && left().type() == DMU_BSPNODE)
lHeight = left().as<BspNode>().height();
return (rHeight> lHeight? rHeight : lHeight) + 1;
}
bool BspNode::hasChild(int left) const
{
return *d->childAdr(left) != 0;
}
MapElement &BspNode::child(int left)
{
if(MapElement *childElm = *d->childAdr(left))
{
return *childElm;
}
/// @throw MissingChildError The specified child element is missing.
throw MissingChildError("BspNode::child", QString("No %1 child is configured").arg(left? "left" : "right"));
}
MapElement const &BspNode::child(int left) const
{
return const_cast<MapElement &>(const_cast<BspNode *>(this)->child(left));
}
void BspNode::setChild(int left, MapElement *newChild)
{
if(!newChild || newChild != this)
{
*d->childAdr(left) = newChild;
return;
}
/// @throw InvalidChildError Attempted to set a child element to "this" element.
throw InvalidChildError("BspNode::setChild", QString("Cannot set \"this\" element as a child of itself"));
}
AABoxd const &BspNode::childAABox(int left) const
{
return d->aaBox(left);
}
void BspNode::setChildAABox(int left, AABoxd const *newAABox)
{
if(newAABox)
{
d->aaBox(left) = *newAABox;
}
else
{
d->aaBox(left).clear();
}
}