aboutsummaryrefslogtreecommitdiff
path: root/tool_src/BspLib/BSPNode.cpp
blob: 4edd2837dd92e38c6789d3e43fdbc950808f3bb7 (plain)
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
//-----------------------------------------------------------------------------
//	BSPLIB MODULE: BSPNode.cpp
//
//  Copyright (c) 1996-1998 by Markus Hadwiger
//  All Rights Reserved.
//-----------------------------------------------------------------------------

// bsplib header files
#include "BSPNode.h"
#include "BoundingBox.h"
#include "BspFormat.h"


BSPLIB_NAMESPACE_BEGIN


// construct BSPNode ----------------------------------------------------------
//
BSPNode::BSPNode( BSPNode *front, BSPNode *back, Polygon *poly, Polygon *backpoly, Plane *sep, BoundingBox *box, int num )
{
	frontsubtree	= front;
	backsubtree		= back;
	polygon			= poly;
	backpolygon		= backpoly;
	separatorplane	= sep;
	boundingbox		= box;
	nodenumber		= num;
}


// destroy BSPNode ------------------------------------------------------------
//
BSPNode::~BSPNode()
{
	delete polygon;
	delete backpolygon;
	delete separatorplane;
	delete boundingbox;
	delete frontsubtree;
	delete backsubtree;
}


// traverse bsp tree (preorder) and number nodes as encountered ---------------
//
void BSPNode::NumberBSPNodes( int& curno )
{
	// store nodenumber
	nodenumber = ++curno;

	// assign node numbers to all polygons contained in splitter plane
	static Polygon *polylist;
	for ( polylist = polygon ? polygon->getNext() : NULL; polylist; polylist = polylist->getNext() ) {
		// only increment numbering, numbers don't get stored in polygons!
		++curno;
	}
	for ( polylist = backpolygon; polylist; polylist = polylist->getNext() ) {
		// only increment numbering, numbers don't get stored in polygons!
		++curno;
	}

	// do numbering recursively for front- and backsubtree
	if ( frontsubtree ) frontsubtree->NumberBSPNodes( curno );
	if ( backsubtree ) backsubtree->NumberBSPNodes( curno );
}


// sum number of vertices of all polygon in tree ------------------------------
//
void BSPNode::SumVertexNums( int& vtxnum )
{
	// sum number of vertices of polygons contained in this plane
	static Polygon *polylist;
	for ( polylist = polygon; polylist; polylist = polylist->getNext() )
		vtxnum += polylist->getNumVertices();
	for ( polylist = backpolygon; polylist; polylist = polylist->getNext() )
		vtxnum += polylist->getNumVertices();

	// do sum recursively for front- and backsubtree
	if ( frontsubtree ) frontsubtree->SumVertexNums( vtxnum );
	if ( backsubtree ) backsubtree->SumVertexNums( vtxnum );
}


// traverse entire tree and correct relative polygon info to new base values --
//
void BSPNode::CorrectPolygonBases( BspObject *newbaseobj, int vertexindxbase, int faceidbase, int polygonidbase )
{
	static Polygon *polylist;
	for ( polylist = polygon; polylist; polylist = polylist->getNext() )
		polylist->CorrectBase( newbaseobj, vertexindxbase, faceidbase, polygonidbase );
	for ( polylist = backpolygon; polylist; polylist = polylist->getNext() )
		polylist->CorrectBase( newbaseobj, vertexindxbase, faceidbase, polygonidbase );

	if ( frontsubtree ) frontsubtree->CorrectPolygonBases( newbaseobj, vertexindxbase, faceidbase, polygonidbase );
	if ( backsubtree ) backsubtree->CorrectPolygonBases( newbaseobj, vertexindxbase, faceidbase, polygonidbase );
}


// traverse entire tree and correct relative polygon info to new base values --
//
void BSPNode::CorrectPolygonBasesByTable( BspObject *newbaseobj, int *vtxindxmap, int faceidbase, int polygonidbase )
{
	static Polygon *polylist;
	for ( polylist = polygon; polylist; polylist = polylist->getNext() )
		polylist->CorrectBaseByTable( newbaseobj, vtxindxmap, faceidbase, polygonidbase );
	for ( polylist = backpolygon; polylist; polylist = polylist->getNext() )
		polylist->CorrectBaseByTable( newbaseobj, vtxindxmap, faceidbase, polygonidbase );

	if ( frontsubtree ) frontsubtree->CorrectPolygonBasesByTable( newbaseobj, vtxindxmap, faceidbase, polygonidbase );
	if ( backsubtree ) backsubtree->CorrectPolygonBasesByTable( newbaseobj, vtxindxmap, faceidbase, polygonidbase );
}


// write bsp tree structure to output file (preorder traversal) ---------------
//
void BSPNode::WriteBSPTree( FILE *fp )
{
	static int lnodnum, rnodnum, cnodnum, bnodnum;
	static int curnodnum;
	static Polygon *polylist;

	lnodnum = frontsubtree ? frontsubtree->nodenumber : 0;
	rnodnum = backsubtree  ? backsubtree->nodenumber  : 0;

	if ( polygon != NULL ) {
		cnodnum = polygon->getNext() ? nodenumber + 1 : 0;
		bnodnum = backpolygon ? nodenumber + polygon->getNumPolygons() : 0;
	} else {
		cnodnum = 0;
		bnodnum = 0;
	}

	if ( outputformat == OUTPUT_OLD_STYLE ) {

		if ( polygon != NULL ) {
			// print head node
			fprintf( fp, "%d: %d |%d|%d|%d-%d|\n",
					 nodenumber, polygon->getId() + 1, cnodnum, bnodnum, lnodnum, rnodnum );

			// print list of contained nodes (frontfacing)
			curnodnum = cnodnum;
			polylist  = polygon;
			while ( ( polylist = polylist->getNext() ) != NULL ) {
				cnodnum = polylist->getNext() ? ( curnodnum + 1 ) : 0;
				fprintf( fp, "%d: %d |%d|0|\n", curnodnum++, polylist->getId() + 1, cnodnum );
			}

			// print list of contained nodes (backfacing)
			curnodnum = bnodnum;
			for ( polylist = backpolygon; polylist; polylist = polylist->getNext() ) {
				bnodnum = polylist->getNext() ? ( curnodnum + 1 ) : 0;
				fprintf( fp, "%d: %d |0|%d|\n", curnodnum++, polylist->getId() + 1, bnodnum );
			}
		} else {
			//NOTE:
			// nodes not containing polygons are not supported
			// by the old ouput format!
			fprintf( fp, "no polygon contained in node %d.", nodenumber );
		}

	} else {

		// print head node
		fprintf( fp, "%d: ", nodenumber );
		if ( polygon != NULL )
			fprintf( fp, "%s %d ", BspFormat::_bspspec_polygon_str, polygon->getId() + 1 );
		if ( cnodnum != 0 )
			fprintf( fp, "%s %d ", BspFormat::_bspspec_frontlist_str, cnodnum );
		if ( bnodnum != 0 )
			fprintf( fp, "%s %d ", BspFormat::_bspspec_backlist_str, bnodnum );
		if ( lnodnum != 0 )
			fprintf( fp, "%s %d ", BspFormat::_bspspec_fronttree_str, lnodnum );
		if ( rnodnum != 0 )
			fprintf( fp, "%s %d ", BspFormat::_bspspec_backtree_str, rnodnum );
		if ( separatorplane != NULL ) {
			Vector3 normal = separatorplane->getPlaneNormal();
			fprintf( fp, "%s %f %f %f %f ", BspFormat::_bspspec_plane_str,
					 normal.getX(), normal.getY(), normal.getZ(),
					 separatorplane->getPlaneOffset() );
		}
		if ( boundingbox != NULL ) {
			Vertex3 minvert = boundingbox->getMinVertex();
			Vertex3 maxvert = boundingbox->getMaxVertex();
			fprintf( fp, "%s %f %f %f %f %f %f ", BspFormat::_bspspec_boundingbox_str, 
					 minvert.getX(), minvert.getY(), minvert.getZ(),
					 maxvert.getX(), maxvert.getY(), maxvert.getZ()	);
		}
		fprintf( fp, "\n" );

		// print list of contained nodes (frontfacing)
		if ( polygon != NULL ) {
			curnodnum = cnodnum;
			polylist  = polygon;
			while ( ( polylist = polylist->getNext() ) != NULL ) {
				fprintf( fp, "%d: ", curnodnum );
				cnodnum = polylist->getNext() ? ++curnodnum : 0;
				fprintf( fp, "%s %d ", BspFormat::_bspspec_polygon_str, polylist->getId() + 1 );
				if ( cnodnum != 0 )
					fprintf( fp, "%s %d", BspFormat::_bspspec_frontlist_str, cnodnum );
				fprintf( fp, "\n" );
			}
		}

		// print list of contained nodes (backfacing)
		curnodnum = bnodnum;
		for ( polylist = backpolygon; polylist; polylist = polylist->getNext() ) {
			fprintf( fp, "%d: ", curnodnum );
			bnodnum = polylist->getNext() ? ++curnodnum : 0;
			fprintf( fp, "%s %d ", BspFormat::_bspspec_polygon_str, polylist->getId() + 1 );
			if ( bnodnum != 0 )
				fprintf( fp, "%s %d", BspFormat::_bspspec_backlist_str, bnodnum );
			fprintf( fp, "\n" );
		}

	}

	// write out front- and backsubtree recursively
	if ( frontsubtree ) frontsubtree->WriteBSPTree( fp );
	if ( backsubtree ) backsubtree->WriteBSPTree( fp );
}


// fetch pointer to polygon with given number contained in bsp tree -----------
//
Polygon *BSPNode::FetchBSPPolygon( int polyno )
{
	// search local node and contained list (frontfacing)
	static Polygon *clist;
	for ( clist = polygon; clist; clist = clist->getNext() ) {
		if ( clist->getId() == polyno )
			return clist;
	}

	// search list of contained backfacing polygons
	for ( clist = backpolygon; clist; clist = clist->getNext() ) {
		if ( clist->getId() == polyno )
			return clist;
	}

	Polygon *search = NULL;

	if ( frontsubtree )
		search = frontsubtree->FetchBSPPolygon( polyno );
	if ( ( search == NULL ) && backsubtree )
		search = backsubtree->FetchBSPPolygon( polyno );

	return search;
}


// fetch all polygon numbers contained in specific face -----------------------
//
void BSPNode::FetchFacePolygons( int faceno, PolygonList& facepolylist )
{
	// search local node and contained list (frontfacing)
	static Polygon *clist;
	for ( clist = polygon; clist; clist = clist->getNext() ) {
		if ( clist->getFaceId() == faceno )
			facepolylist.InsertPolygon( new Polygon( NULL, clist->getId(), clist->getFaceId() ) );
	}

	// search list of contained backfacing polygons
	for ( clist = backpolygon; clist; clist = clist->getNext() ) {
		if ( clist->getFaceId() == faceno )
			facepolylist.InsertPolygon( new Polygon( NULL, clist->getId(), clist->getFaceId() ) );
	}

	// check children (subtrees)
	if ( frontsubtree ) frontsubtree->FetchFacePolygons( faceno, facepolylist );
	if ( backsubtree ) backsubtree->FetchFacePolygons( faceno, facepolylist );
}


// check edges for contained vertices and insert them as trace vertices -------
//
void BSPNode::CheckEdges()
{
	// check list of frontfacing polygons
	if ( polygon ) polygon->CheckEdges();

	// check list of backfacing polygons
	if ( backpolygon ) backpolygon->CheckEdges();

	// check children (subtrees)
	if ( frontsubtree ) frontsubtree->CheckEdges();
	if ( backsubtree ) backsubtree->CheckEdges();
}


// grow this node's bounding box by another node's ----------------------------
//
void BSPNode::GrowBoundingBox( BSPNode *othernode )
{
	if ( othernode != NULL ) {
		// ensure other node has a bounding box (calculate if not)
		if ( othernode->boundingbox == NULL )
			othernode->CalcBoundingBoxes();
		static BoundingBox *otherbox;
		otherbox = othernode->boundingbox;

		if ( boundingbox == NULL ) {
			// if this node has no bounding box copy other node's
			boundingbox = new BoundingBox( otherbox->getMinVertex(), otherbox->getMaxVertex() );
		} else {
			// merge the two bounding boxes
			boundingbox->GrowBoundingBox( otherbox );
		}
	}
}


// calculate axial bounding box for this node and all children ----------------
//
void BSPNode::CalcBoundingBoxes()
{
	if ( boundingbox == NULL ) {

		// if polygon(s) contained in this node calculate their bounding box
		if ( polygon != NULL ) {
			polygon->CalcBoundingBox( boundingbox );
			if ( backpolygon != NULL ) {
				static BoundingBox *backbox;
				backpolygon->CalcBoundingBox( backbox );
				boundingbox->GrowBoundingBox( backbox );
				delete backbox;
			}
		}

		// grow local bounding box by children's
		GrowBoundingBox( frontsubtree );
		GrowBoundingBox( backsubtree );
	}
}


// calculate separator planes from polygons for entire bsp tree ---------------
//
void BSPNode::CalcSeparatorPlanes()
{
	if ( ( separatorplane == NULL ) && ( polygon != NULL ) ) {
		separatorplane = new Plane( polygon->getFirstVertex(),
									polygon->getSecondVertex(),
									polygon->getThirdVertex() );
	}

	// check children (subtrees)
	if ( frontsubtree ) frontsubtree->CalcSeparatorPlanes();
	if ( backsubtree ) backsubtree->CalcSeparatorPlanes();
}


// format to use when writing bsp nodes to files ------------------------------
//
int BSPNode::outputformat = BSPNode::OUTPUT_KEY_VALUE_STYLE;


// construct BSPNodeFlat ------------------------------------------------------
//
BSPNodeFlat::BSPNodeFlat( int front, int back, Polygon *poly, int clist, int blist, Plane *sep, BoundingBox *box, int num )
{
	frontsubtreeindx	= front;
	backsubtreeindx		= back;
	polygon				= poly;
	containedlistindx	= clist;
	backlistindx		= blist;
	separatorplane		= sep;
	boundingbox			= box;
	nodenumber			= num;
}


// init BSPNodeFlat members ---------------------------------------------------
//
void BSPNodeFlat::InitNode( int front, int back, Polygon *poly, int clist, int blist, Plane *sep, BoundingBox *box, int num )
{
	frontsubtreeindx	= front;
	backsubtreeindx		= back;
	polygon				= poly;
	containedlistindx	= clist;
	backlistindx		= blist;
	separatorplane		= sep;
	boundingbox			= box;
	nodenumber			= num;
}


// apply scale factor to separator plane and bounding box if attached ---------
//
void BSPNodeFlat::ApplyScaleFactor( double sfac )
{
	if ( separatorplane != NULL )
		separatorplane->ApplyScaleFactor( sfac );
	if ( boundingbox != NULL )
		boundingbox->ApplyScaleFactor( sfac );
}


BSPLIB_NAMESPACE_END

//-----------------------------------------------------------------------------