aboutsummaryrefslogtreecommitdiff
path: root/tool_src/BspLib/PolygonList.cpp
blob: 4233c4279fcab08f30ef4b8a7cdf083d60c504e1 (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
//-----------------------------------------------------------------------------
//	BSPLIB MODULE: PolygonList.cpp
//
//  Copyright (c) 1997 by Markus Hadwiger
//  All Rights Reserved.
//-----------------------------------------------------------------------------

// bsplib headers
#include "PolygonList.h"
#include "BspObject.h"
#include "BspTool.h"
#include "BSPNode.h"
#include "Vertex.h"


BSPLIB_NAMESPACE_BEGIN


// init with already existing list of polygons --------------------------------
//
Polygon *PolygonListRep::InitList( Polygon *listhead )
{
	delete list;
	list = listhead;
	numpolygons	= list ? list->getNumPolygons() : 0;

	return list;
}


// merge another polygon list into this list ----------------------------------
//
void PolygonListRep::MergeLists( PolygonListRep *mergelist )
{
	if ( list != NULL ) {
		// scan to end of local list
		Polygon *poly = NULL;
		for (poly = list; poly->getNext(); poly = poly->getNext() )
			poly->setNumPolygons( poly->getNumPolygons() + mergelist->numpolygons );
		// append mergelist
		poly->setNext( mergelist->list );
		poly->setNumPolygons( poly->getNumPolygons() + mergelist->numpolygons );
		numpolygons += mergelist->numpolygons;
	} else {
		// local list is empty: simply take over mergelist
		list		= mergelist->list;
		numpolygons	= mergelist->numpolygons;
	}
}


// create new polygon and add at head of list ---------------------------------
//
Polygon *PolygonListRep::NewPolygon()
{
	list = list ? list->NewPolygon() : new Polygon( baseobject );
	numpolygons = list->getNumPolygons();

	return list;
}


// insert existing polygon at head of list ------------------------------------
//
Polygon *PolygonListRep::InsertPolygon( Polygon *poly )
{
	if ( poly != NULL ) {

		if ( list != NULL ) {
			list = list->InsertPolygon( poly );
		} else {
			poly->setNext( NULL );
			poly->setNumPolygons( 1 );
			poly->baseobject = baseobject;
			list = poly;
		}
		numpolygons = list->getNumPolygons();
	}

	return list;
}


// unlink head from list and return pointer to it -----------------------------
//
Polygon *PolygonListRep::UnlinkHead()
{
	Polygon *tmp = list;

	if ( tmp != NULL ) {
		list = tmp->getNext();
		tmp->setNumPolygons( 1 );
		tmp->setNext( NULL );
		numpolygons = list ? list->getNumPolygons() : 0;
	}

	return tmp;
}


// delete head of list and return pointer to rest of list ---------------------
//
Polygon *PolygonListRep::DeleteHead()
{
	if ( list != NULL )	{
		list = list->DeleteHead();
		numpolygons = list ? list->getNumPolygons() : 0;
	}

	return list;
}


// calculate plane normals for planes of all polygons -------------------------
//
Polygon *PolygonListRep::CalcPlaneNormals()
{
	if ( list != NULL ) {
		list->CalcPlaneNormals();
	}

	return list;
}


// check polygon planes for all polygons contained in list --------------------
//
Polygon *PolygonListRep::CheckPolygonPlanes()
{
	if ( list != NULL ) {
		// list possibly grows due to checking planes!!
		list = list->CheckPlanesAndMappings();
		numpolygons = list->getNumPolygons();
	}

	return list;
}


// build bsp tree; polygon list is resolved in the process --------------------
//
BSPNode *PolygonListRep::PartitionSpace()
{
	BSPNode *bspnode = NULL;

	if ( list != NULL ) {

		if ( list->getSplitterSelection() == Polygon::SPLITTERCRIT_RANDOM_SAMPLE ) {
			// calculate random sample probability
			list->CalcSplitterTestProbability();
		}

		bspnode		= list->PartitionSpace();
		list		= NULL;
		numpolygons = 0;
		//NOTE:
		// the polygon list is resolved in the process of
		// bsp compilation! polygons are only accessible as
		// elements of the newly created bsp tree afterwards.
	}

	return bspnode;
}


// write list of polygon numbers to text-file (reverse ordering) --------------
//
void PolygonListRep::WritePolyList( FILE *fp, int no ) const
{
	if ( list != NULL ) {
		if ( list->nextpolygon )
			list->nextpolygon->WritePolyList( fp );
		fprintf( fp, "%d\t\t; %d\n", list->getId() + 1, no );
	}
}


// class PolygonList error message handler ------------------------------------
//
void PolygonListRep::Error( int err ) const
{
	{
	StrScratch line;

	switch ( err ) {

	case E_NEWVINDX:
		strcpy( line, "***ERROR*** in PolygonList::(Append|Prepend)NewVIndx()\n" );
		break;

	case E_APPENDVINDX:
		strcpy( line, "***ERROR*** in PolygonList::AppendVIndx()\n" );
		break;

	case E_GETNUMVERTXS:
		strcpy( line, "***ERROR*** in PolygonList::getNumElements()\n" );
		break;

	default:
		strcpy( line, "***ERROR*** in object of class BspLib::PolygonList\n" );
		break;
	}

	ErrorMessage( line );
	}

	HandleCriticalError();
}


BSPLIB_NAMESPACE_END

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