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
|
/*
* Copyright (C) Oscar Shrimpton 2020
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
//! Parses the BSP tree into a usable format
use super::Q3BSPFile;
use crate::coords::CoordSystem;
use crate::helpers::{slice_to_i32, slice_to_u32, slice_to_vec3i};
use crate::traits::tree::*;
use crate::types::{ParseError, Result};
const NODE_SIZE: usize = 4 + (4 * 2) + (4 * 3) + (4 * 3);
const LEAF_SIZE: usize = 4 * 6 + (4 * 3 * 2);
pub fn from_data(
nodes: &[u8],
leaves: &[u8],
leaf_faces: &[u8],
leaf_brushes: &[u8],
n_faces: u32,
n_brushes: u32,
) -> Result<BSPNode> {
if nodes.len() % NODE_SIZE != 0 || leaves.len() % LEAF_SIZE != 0 {
return Err(ParseError::Invalid);
}
Ok(compile_node(
0,
nodes,
leaves,
leaf_faces,
leaf_brushes,
n_faces,
n_brushes,
)?)
}
/// Internal function. Visits given node and all its children. Used to recursively build tree.
fn compile_node(
i: i32,
nodes: &[u8],
leaves: &[u8],
leaf_faces: &[u8],
leaf_brushes: &[u8],
n_faces: u32,
n_brushes: u32,
) -> Result<BSPNode> {
if i < 0 {
// Leaf.
let i = i.abs() - 1;
let raw = &leaves[i as usize * LEAF_SIZE..(i as usize * LEAF_SIZE) + LEAF_SIZE];
let faces_idx = {
let start = slice_to_u32(&raw[32..36]) as usize;
let n = slice_to_u32(&raw[36..40]) as usize;
let mut faces = Vec::with_capacity(n);
if n > 0 {
if start + n > leaf_faces.len() / 4 {
return Err(ParseError::Invalid);
}
for i in start..start + n {
let face_idx = slice_to_u32(&leaf_faces[i * 4..(i + 1) * 4]);
if face_idx >= n_faces {
return Err(ParseError::Invalid);
}
faces.push(face_idx);
}
}
faces.into_boxed_slice()
};
let brushes_idx = {
let start = slice_to_u32(&raw[40..44]) as usize;
let n = slice_to_u32(&raw[44..48]) as usize;
let mut brushes = Vec::with_capacity(n);
if n > 0 {
if start + n > leaf_brushes.len() / 4 {
return Err(ParseError::Invalid);
}
for i in start..start + n {
let brush_idx = slice_to_u32(&leaf_brushes[i * 4..(i + 1) * 4]);
if brush_idx >= n_brushes {
return Err(ParseError::Invalid);
}
brushes.push(brush_idx);
}
}
brushes.into_boxed_slice()
};
let leaf = BSPLeaf {
cluster_id: slice_to_u32(&raw[0..4]),
area: slice_to_i32(&raw[4..8]),
// 8..20 = min
// 20..32 = max
faces_idx,
brushes_idx,
};
Ok(BSPNode {
plane_idx: 0,
min: slice_to_vec3i(&raw[8..20]),
max: slice_to_vec3i(&raw[20..32]),
value: BSPNodeValue::Leaf(leaf),
})
} else {
// Node.
let raw = &nodes[i as usize * NODE_SIZE..(i as usize * NODE_SIZE) + NODE_SIZE];
let plane_idx = slice_to_u32(&raw[0..4]);
let child_one = compile_node(
slice_to_i32(&raw[4..8]),
nodes,
leaves,
leaf_faces,
leaf_brushes,
n_faces,
n_brushes,
)?;
let child_two = compile_node(
slice_to_i32(&raw[8..12]),
nodes,
leaves,
leaf_faces,
leaf_brushes,
n_faces,
n_brushes,
)?;
let min = slice_to_vec3i(&raw[12..24]);
let max = slice_to_vec3i(&raw[24..36]);
Ok(BSPNode {
plane_idx,
value: BSPNodeValue::Children(Box::new(child_one), Box::new(child_two)),
min,
max,
})
}
}
impl<T: CoordSystem> HasBSPTree<T> for Q3BSPFile<T> {
fn get_bsp_root(&self) -> &BSPNode {
&self.tree_root
}
}
|